<feed xmlns='http://www.w3.org/2005/Atom'>
<title>ruby.git/test/ruby/test_eval.rb, branch v4.0.2</title>
<subtitle>The Ruby Programming Language</subtitle>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/'/>
<entry>
<title>Omit TestEval#test_outer_local_variable_under_gc_compact_stress with s390x</title>
<updated>2024-12-23T00:39:08+00:00</updated>
<author>
<name>Hiroshi SHIBATA</name>
<email>hsbt@ruby-lang.org</email>
</author>
<published>2024-12-23T00:39:06+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=60c814607d5a1cc0522303cfe8378183a427dd79'/>
<id>60c814607d5a1cc0522303cfe8378183a427dd79</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>Fix GC compaction crash when using local variables in eval</title>
<updated>2024-12-20T13:24:54+00:00</updated>
<author>
<name>Peter Zhu</name>
<email>peter@peterzhu.ca</email>
</author>
<published>2024-12-17T19:14:36+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=e23a60b929cff951252167935e8868b69ae3dc74'/>
<id>e23a60b929cff951252167935e8868b69ae3dc74</id>
<content type='text'>
If we have local variables outside of the eval, the local variables names
are IDs. We convert these IDs to char * using rb_id2name. However, these
char * are actually Ruby strings, which may be embedded. This means that
it is not safe to rb_id2name and call any potential GC entrypoints because
if a GC compaction runs, the embedded string may move and the pointer will
change.

For example, if you compile with `-DRGENGC_CHECK_MODE=1`, then the following
script will crash:

    GC.auto_compact = :empty
    GC.stress = true

    o = Object.new

    eval("def o.m(k: 0) k end")

The crash message is:

    test.rb:6: [BUG] Local with constant_id 1 does not exist
    ruby 3.4.0dev (2024-12-17T18:34:57Z prism-local-compac.. 434346726c) +PRISM [arm64-darwin24]

    -- C level backtrace information -------------------------------------------
    miniruby(rb_print_backtrace+0x24) [0x10312fec4] vm_dump.c:823
    miniruby(rb_print_backtrace) (null):0
    miniruby(rb_vm_bugreport+0x2d4) [0x1031301b8] vm_dump.c:1155
    miniruby(rb_bug_without_die_internal+0xa8) [0x102dd6a94] error.c:1097
    miniruby(rb_bug+0x28) [0x102dd6b00] error.c:1115
    miniruby(pm_lookup_local_index+0xec) [0x102d61e4c] prism_compile.c:1237
    miniruby(pm_compile_node+0x45d0) [0x102d252f4] prism_compile.c:9334
    miniruby(pm_compile_node+0x1864) [0x102d22588] prism_compile.c:8650
    miniruby(pm_compile_node+0x65ec) [0x102d27310] prism_compile.c:9897
    miniruby(pm_compile_scope_node+0x3008) [0x102d77bcc] prism_compile.c:6584
    miniruby(pm_compile_node+0x5ec4) [0x102d26be8] prism_compile.c:9768
    miniruby(pm_iseq_compile_node+0xac) [0x102d20bf0] prism_compile.c:10069
    miniruby(pm_iseq_new_with_opt_try+0x2c) [0x102e7d088] iseq.c:1029
    miniruby(rb_protect+0x108) [0x102dea9bc] eval.c:1033
    miniruby(pm_iseq_new_with_opt+0x264) [0x102e7c444] iseq.c:1082
    miniruby(pm_iseq_new_eval+0xec) [0x102e7c8e0] iseq.c:961
    miniruby(pm_eval_make_iseq+0x594) [0x1031209cc] vm_eval.c:1770
    miniruby(eval_make_iseq+0x54) [0x103120068] vm_eval.c:1799
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
If we have local variables outside of the eval, the local variables names
are IDs. We convert these IDs to char * using rb_id2name. However, these
char * are actually Ruby strings, which may be embedded. This means that
it is not safe to rb_id2name and call any potential GC entrypoints because
if a GC compaction runs, the embedded string may move and the pointer will
change.

For example, if you compile with `-DRGENGC_CHECK_MODE=1`, then the following
script will crash:

    GC.auto_compact = :empty
    GC.stress = true

    o = Object.new

    eval("def o.m(k: 0) k end")

The crash message is:

    test.rb:6: [BUG] Local with constant_id 1 does not exist
    ruby 3.4.0dev (2024-12-17T18:34:57Z prism-local-compac.. 434346726c) +PRISM [arm64-darwin24]

    -- C level backtrace information -------------------------------------------
    miniruby(rb_print_backtrace+0x24) [0x10312fec4] vm_dump.c:823
    miniruby(rb_print_backtrace) (null):0
    miniruby(rb_vm_bugreport+0x2d4) [0x1031301b8] vm_dump.c:1155
    miniruby(rb_bug_without_die_internal+0xa8) [0x102dd6a94] error.c:1097
    miniruby(rb_bug+0x28) [0x102dd6b00] error.c:1115
    miniruby(pm_lookup_local_index+0xec) [0x102d61e4c] prism_compile.c:1237
    miniruby(pm_compile_node+0x45d0) [0x102d252f4] prism_compile.c:9334
    miniruby(pm_compile_node+0x1864) [0x102d22588] prism_compile.c:8650
    miniruby(pm_compile_node+0x65ec) [0x102d27310] prism_compile.c:9897
    miniruby(pm_compile_scope_node+0x3008) [0x102d77bcc] prism_compile.c:6584
    miniruby(pm_compile_node+0x5ec4) [0x102d26be8] prism_compile.c:9768
    miniruby(pm_iseq_compile_node+0xac) [0x102d20bf0] prism_compile.c:10069
    miniruby(pm_iseq_new_with_opt_try+0x2c) [0x102e7d088] iseq.c:1029
    miniruby(rb_protect+0x108) [0x102dea9bc] eval.c:1033
    miniruby(pm_iseq_new_with_opt+0x264) [0x102e7c444] iseq.c:1082
    miniruby(pm_iseq_new_eval+0xec) [0x102e7c8e0] iseq.c:961
    miniruby(pm_eval_make_iseq+0x594) [0x1031209cc] vm_eval.c:1770
    miniruby(eval_make_iseq+0x54) [0x103120068] vm_eval.c:1799
</pre>
</div>
</content>
</entry>
<entry>
<title>Fix memory leak in prism when syntax error in iseq compilation</title>
<updated>2024-11-08T20:43:41+00:00</updated>
<author>
<name>Peter Zhu</name>
<email>peter@peterzhu.ca</email>
</author>
<published>2024-11-08T19:33:48+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=51ffef281996727c60571771cd07c1459ba58cd2'/>
<id>51ffef281996727c60571771cd07c1459ba58cd2</id>
<content type='text'>
If there's a syntax error during iseq compilation then prism would leak
memory because it would not free the pm_parse_result_t.

This commit changes pm_iseq_new_with_opt to have a rb_protect to catch
when an error is raised, and return NULL and set error_state to a value
that can be raised by calling rb_jump_tag after memory has been freed.

For example:

    10.times do
      10_000.times do
        eval("/[/=~s")
      rescue SyntaxError
      end

      puts `ps -o rss= -p #{$$}`
    end

Before:

    39280
    68736
    99232
    128864
    158896
    188208
    217344
    246304
    275376
    304592

After:

    12192
    13200
    14256
    14848
    16000
    16000
    16000
    16064
    17232
    17952
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
If there's a syntax error during iseq compilation then prism would leak
memory because it would not free the pm_parse_result_t.

This commit changes pm_iseq_new_with_opt to have a rb_protect to catch
when an error is raised, and return NULL and set error_state to a value
that can be raised by calling rb_jump_tag after memory has been freed.

For example:

    10.times do
      10_000.times do
        eval("/[/=~s")
      rescue SyntaxError
      end

      puts `ps -o rss= -p #{$$}`
    end

Before:

    39280
    68736
    99232
    128864
    158896
    188208
    217344
    246304
    275376
    304592

After:

    12192
    13200
    14256
    14848
    16000
    16000
    16000
    16064
    17232
    17952
</pre>
</div>
</content>
</entry>
<entry>
<title>Raise a compile error for break/next/redo inside eval in cases where it is optimized away</title>
<updated>2024-09-18T23:54:56+00:00</updated>
<author>
<name>Jeremy Evans</name>
<email>code@jeremyevans.net</email>
</author>
<published>2024-09-18T23:54:56+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=268c72377b06b7d84a0998ca241340d0f58768f6'/>
<id>268c72377b06b7d84a0998ca241340d0f58768f6</id>
<content type='text'>
In cases where break/next/redo are not valid syntax, they should
raise a SyntaxError even if inside a conditional block that is
optimized away.

Fixes [Bug #20597]

Co-authored-by: Kevin Newton &lt;kddnewton@gmail.com&gt;</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
In cases where break/next/redo are not valid syntax, they should
raise a SyntaxError even if inside a conditional block that is
optimized away.

Fixes [Bug #20597]

Co-authored-by: Kevin Newton &lt;kddnewton@gmail.com&gt;</pre>
</div>
</content>
</entry>
<entry>
<title>Use the caller location as default filename for eval family of methods</title>
<updated>2023-07-24T12:51:20+00:00</updated>
<author>
<name>Jean Boussier</name>
<email>byroot@ruby-lang.org</email>
</author>
<published>2023-07-13T09:49:28+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=43a5c191358699fe8b19314763998cb8ca77ed90'/>
<id>43a5c191358699fe8b19314763998cb8ca77ed90</id>
<content type='text'>
[Feature #19755]

Before (in /tmp/test.rb):

```ruby
Object.class_eval("p __FILE__") # =&gt; "(eval)"
```

After:

```ruby
Object.class_eval("p __FILE__") # =&gt; "(eval at /tmp/test.rb:1)"
```

This makes it much easier to track down generated code in case
the author forgot to provide a filename argument.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
[Feature #19755]

Before (in /tmp/test.rb):

```ruby
Object.class_eval("p __FILE__") # =&gt; "(eval)"
```

After:

```ruby
Object.class_eval("p __FILE__") # =&gt; "(eval at /tmp/test.rb:1)"
```

This makes it much easier to track down generated code in case
the author forgot to provide a filename argument.
</pre>
</div>
</content>
</entry>
<entry>
<title>Fix small issues concerning namespacing in test-all suite</title>
<updated>2023-03-16T23:46:37+00:00</updated>
<author>
<name>lukeg</name>
<email>luke.gru@gmail.com</email>
</author>
<published>2023-03-13T22:13:21+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=d2c6dca8f4a9fb3e6b9748758ee9eb75fa18622a'/>
<id>d2c6dca8f4a9fb3e6b9748758ee9eb75fa18622a</id>
<content type='text'>
* Fix temporary methods on Object leaking across test cases.
* Remove temporary classes/modules leaking across test cases.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
* Fix temporary methods on Object leaking across test cases.
* Remove temporary classes/modules leaking across test cases.
</pre>
</div>
</content>
</entry>
<entry>
<title>Lazily create singletons on instance_{exec,eval} (#5146)</title>
<updated>2021-12-02T23:53:39+00:00</updated>
<author>
<name>John Hawthorn</name>
<email>john@hawthorn.email</email>
</author>
<published>2021-12-02T23:53:39+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=733500e9d02b11ff60fbbdb8daa43c2e9cfbd750'/>
<id>733500e9d02b11ff60fbbdb8daa43c2e9cfbd750</id>
<content type='text'>
* Lazily create singletons on instance_{exec,eval}

Previously when instance_exec or instance_eval was called on an object,
that object would be given a singleton class so that method
definitions inside the block would be added to the object rather than
its class.

This commit aims to improve performance by delaying the creation of the
singleton class unless/until one is needed for method definition. Most
of the time instance_eval is used without any method definition.

This was implemented by adding a flag to the cref indicating that it
represents a singleton of the object rather than a class itself. In this
case CREF_CLASS returns the object's existing class, but in cases that
we are defining a method (either via definemethod or
VM_SPECIAL_OBJECT_CBASE which is used for undef and alias).

This also happens to fix what I believe is a bug. Previously
instance_eval behaved differently with regards to constant access for
true/false/nil than for all other objects. I don't think this was
intentional.

    String::Foo = "foo"
    "".instance_eval("Foo")   # =&gt; "foo"
    Integer::Foo = "foo"
    123.instance_eval("Foo")  # =&gt; "foo"
    TrueClass::Foo = "foo"
    true.instance_eval("Foo") # NameError: uninitialized constant Foo

This also slightly changes the error message when trying to define a method
through instance_eval on an object which can't have a singleton class.

Before:

    $ ruby -e '123.instance_eval { def foo; end }'
    -e:1:in `block in &lt;main&gt;': no class/module to add method (TypeError)

After:

    $ ./ruby -e '123.instance_eval { def foo; end }'
    -e:1:in `block in &lt;main&gt;': can't define singleton (TypeError)

IMO this error is a small improvement on the original and better matches
the (both old and new) message when definging a method using `def self.`

    $ ruby -e '123.instance_eval{ def self.foo; end }'
    -e:1:in `block in &lt;main&gt;': can't define singleton (TypeError)

Co-authored-by: Matthew Draper &lt;matthew@trebex.net&gt;

* Remove "under" argument from yield_under

* Move CREF_SINGLETON_SET into vm_cref_new

* Simplify vm_get_const_base

* Fix leaf VM_SPECIAL_OBJECT_CONST_BASE

Co-authored-by: Matthew Draper &lt;matthew@trebex.net&gt;</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
* Lazily create singletons on instance_{exec,eval}

Previously when instance_exec or instance_eval was called on an object,
that object would be given a singleton class so that method
definitions inside the block would be added to the object rather than
its class.

This commit aims to improve performance by delaying the creation of the
singleton class unless/until one is needed for method definition. Most
of the time instance_eval is used without any method definition.

This was implemented by adding a flag to the cref indicating that it
represents a singleton of the object rather than a class itself. In this
case CREF_CLASS returns the object's existing class, but in cases that
we are defining a method (either via definemethod or
VM_SPECIAL_OBJECT_CBASE which is used for undef and alias).

This also happens to fix what I believe is a bug. Previously
instance_eval behaved differently with regards to constant access for
true/false/nil than for all other objects. I don't think this was
intentional.

    String::Foo = "foo"
    "".instance_eval("Foo")   # =&gt; "foo"
    Integer::Foo = "foo"
    123.instance_eval("Foo")  # =&gt; "foo"
    TrueClass::Foo = "foo"
    true.instance_eval("Foo") # NameError: uninitialized constant Foo

This also slightly changes the error message when trying to define a method
through instance_eval on an object which can't have a singleton class.

Before:

    $ ruby -e '123.instance_eval { def foo; end }'
    -e:1:in `block in &lt;main&gt;': no class/module to add method (TypeError)

After:

    $ ./ruby -e '123.instance_eval { def foo; end }'
    -e:1:in `block in &lt;main&gt;': can't define singleton (TypeError)

IMO this error is a small improvement on the original and better matches
the (both old and new) message when definging a method using `def self.`

    $ ruby -e '123.instance_eval{ def self.foo; end }'
    -e:1:in `block in &lt;main&gt;': can't define singleton (TypeError)

Co-authored-by: Matthew Draper &lt;matthew@trebex.net&gt;

* Remove "under" argument from yield_under

* Move CREF_SINGLETON_SET into vm_cref_new

* Simplify vm_get_const_base

* Fix leaf VM_SPECIAL_OBJECT_CONST_BASE

Co-authored-by: Matthew Draper &lt;matthew@trebex.net&gt;</pre>
</div>
</content>
</entry>
<entry>
<title>support multi-run for test/ruby/test_eval.rb</title>
<updated>2020-01-28T06:23:51+00:00</updated>
<author>
<name>Koichi Sasada</name>
<email>ko1@atdot.net</email>
</author>
<published>2020-01-28T04:39:50+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=962c7abb1373ee1ff7d5ce443b2872deac452b9d'/>
<id>962c7abb1373ee1ff7d5ce443b2872deac452b9d</id>
<content type='text'>
need to remove a Constant.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
need to remove a Constant.
</pre>
</div>
</content>
</entry>
<entry>
<title>Make eval(code, binding) use (eval) as __FILE__ and 1 as __LINE__</title>
<updated>2020-01-04T04:13:09+00:00</updated>
<author>
<name>Jeremy Evans</name>
<email>code@jeremyevans.net</email>
</author>
<published>2019-07-30T23:15:19+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=0eeed5bcc5530edb0af2af2ccff09d067c59e8f9'/>
<id>0eeed5bcc5530edb0af2af2ccff09d067c59e8f9</id>
<content type='text'>
This removes the warning that was added in
3802fb92ff8c83eed3e867db20f72c53932f542d, and switches the behavior
so that the eval does not use the binding's __FILE__ and __LINE__
implicitly.

Fixes [Bug #4352]
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
This removes the warning that was added in
3802fb92ff8c83eed3e867db20f72c53932f542d, and switches the behavior
so that the eval does not use the binding's __FILE__ and __LINE__
implicitly.

Fixes [Bug #4352]
</pre>
</div>
</content>
</entry>
<entry>
<title>Fix dangling path name from fstring</title>
<updated>2019-08-03T04:48:29+00:00</updated>
<author>
<name>Nobuyoshi Nakada</name>
<email>nobu@ruby-lang.org</email>
</author>
<published>2019-08-03T04:41:55+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=5931857281ce45c1c277aa86d1588119ab00a955'/>
<id>5931857281ce45c1c277aa86d1588119ab00a955</id>
<content type='text'>
* parse.y (yycompile): make sure in advance that the `__FILE__`
  object shares a fstring, to get rid of dangling path name.
  Fixed up 53e9908d8afc7f03109b0aafd1698ab35f512b05.  [Bug #16041]

* vm_eval.c (eval_make_iseq): ditto.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
* parse.y (yycompile): make sure in advance that the `__FILE__`
  object shares a fstring, to get rid of dangling path name.
  Fixed up 53e9908d8afc7f03109b0aafd1698ab35f512b05.  [Bug #16041]

* vm_eval.c (eval_make_iseq): ditto.
</pre>
</div>
</content>
</entry>
</feed>
