diff options
author | ko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-02-05 18:50:35 +0000 |
---|---|---|
committer | ko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-02-05 18:50:35 +0000 |
commit | a4a81019653287291f88563964d482798da6221f (patch) | |
tree | ec7c2bdf59d69e9f2b87b1c45e51977b0ed792e2 | |
parent | 0d70d8864359e6d8c410dd2727de618ba7cc3dc7 (diff) | |
download | ruby-a4a81019653287291f88563964d482798da6221f.tar.gz |
* proc.c: support Binding#eval.
* yarvtest/test_eval.rb: add a test for above change.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11635 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | proc.c | 10 | ||||
-rw-r--r-- | yarvtest/test_eval.rb | 434 |
3 files changed, 234 insertions, 216 deletions
@@ -1,3 +1,9 @@ +Tue Feb 6 03:47:58 2007 Koichi Sasada <ko1@atdot.net> + + * proc.c: support Binding#eval. + + * yarvtest/test_eval.rb: add a test for above change. + Tue Feb 6 03:13:33 2007 Koichi Sasada <ko1@atdot.net> * proc.c: refactoring (remove K&R style, move Binding stuffs from @@ -282,10 +282,13 @@ rb_f_binding(VALUE self) */ static VALUE -bind_eval(int argc, VALUE *argv, VALUE bind) +bind_eval(int argc, VALUE *argv, VALUE bindval) { - UNSUPPORTED(bind_eval); - return Qnil; + VALUE args[4]; + + rb_scan_args(argc, argv, "12", &args[0], &args[2], &args[3]); + args[1] = bindval; + return rb_f_eval(argc+1, args, Qnil /* self will be searched in eval */); } #define PROC_TSHIFT (FL_USHIFT+1) @@ -1584,6 +1587,7 @@ Init_Binding(void) rb_undef_method(CLASS_OF(rb_cBinding), "new"); rb_define_method(rb_cBinding, "clone", binding_clone, 0); rb_define_method(rb_cBinding, "dup", binding_dup, 0); + rb_define_method(rb_cBinding, "eval", bind_eval, -1); rb_define_global_function("binding", rb_f_binding, 0); } diff --git a/yarvtest/test_eval.rb b/yarvtest/test_eval.rb index fc4ac0372d..8a82c4cff4 100644 --- a/yarvtest/test_eval.rb +++ b/yarvtest/test_eval.rb @@ -1,213 +1,221 @@ -require 'yarvtest/yarvtest'
-
-class TestEval < YarvTestBase
- def test_eval
- ae %q{
- eval('1')
- }
- ae %q{
- eval('a=1; a')
- }
- ae %q{
- a = 1
- eval('a')
- }
- end
-
- def test_eval_with_send
- ae %q{
- __send! :eval, %{
- :ok
- }
- }
- ae %q{
- 1.__send! :instance_eval, %{
- :ok
- }
- }
- end
-
- def test_module_eval
- ae %q{
- Const = :top
- class C
- Const = :C
- end
- C.module_eval{
- Const
- }
- }
- ae %q{
- Const = :top
- class C
- Const = :C
- end
- C.module_eval %{
- Const
- }
- } if false # TODO: Ruby 1.9 error
-
- ae %q{
- Const = :top
- class C
- Const = :C
- end
- C.class_eval %{
- def m
- Const
- end
- }
- C.new.m
- }
- ae %q{
- Const = :top
- class C
- Const = :C
- end
- C.class_eval{
- def m
- Const
- end
- }
- C.new.m
- }
- end
-
- def test_instance_eval
- ae %q{
- 1.instance_eval{
- self
- }
- }
- ae %q{
- 'foo'.instance_eval{
- self
- }
- }
- ae %q{
- class Fixnum
- Const = 1
- end
- 1.instance_eval %{
- Const
- }
- }
- end
-
- def test_nest_eval
- ae %q{
- Const = :top
- class C
- Const = :C
- end
- $nest = false
- $ans = []
- def m
- $ans << Const
- C.module_eval %{
- $ans << Const
- Boo = false unless defined? Boo
- unless $nest
- $nest = true
- m
- end
- }
- end
- m
- $ans
- }
- ae %q{
- $nested = false
- $ans = []
- $pr = proc{
- $ans << self
- unless $nested
- $nested = true
- $pr.call
- end
- }
- class C
- def initialize &b
- 10.instance_eval(&b)
- end
- end
- C.new(&$pr)
- $ans
- }
- end
-
- def test_binding
- ae %q{
- def m
- a = :ok
- $b = binding
- end
- m
- eval('a', $b)
- }
- ae %q{
- def m
- a = :ok
- $b = binding
- end
- m
- eval('b = :ok2', $b)
- eval('[a, b]', $b)
- }
- ae %q{
- $ans = []
- def m
- $b = binding
- end
- m
- $ans << eval(%q{
- $ans << eval(%q{
- a
- }, $b)
- a = 1
- }, $b)
- $ans
- }
- ae %q{
- Const = :top
- class C
- Const = :C
- def m
- binding
- end
- end
- eval('Const', C.new.m)
- }
- ae %q{
- Const = :top
- a = 1
- class C
- Const = :C
- def m
- eval('Const', TOPLEVEL_BINDING)
- end
- end
- C.new.m
- }
- ae %q{
- class C
- $b = binding
- end
- eval %q{
- def m
- :ok
- end
- }, $b
- p C.new.m
- }
- ae %q{
- b = proc{
- a = :ok
- binding
- }.call
- a = :ng
- eval("a", b)
- }
- end
-end
-
+require 'yarvtest/yarvtest' + +class TestEval < YarvTestBase + def test_eval + ae %q{ + eval('1') + } + ae %q{ + eval('a=1; a') + } + ae %q{ + a = 1 + eval('a') + } + end + + def test_eval_with_send + ae %q{ + __send! :eval, %{ + :ok + } + } + ae %q{ + 1.__send! :instance_eval, %{ + :ok + } + } + end + + def test_module_eval + ae %q{ + Const = :top + class C + Const = :C + end + C.module_eval{ + Const + } + } + ae %q{ + Const = :top + class C + Const = :C + end + C.module_eval %{ + Const + } + } if false # TODO: Ruby 1.9 error + + ae %q{ + Const = :top + class C + Const = :C + end + C.class_eval %{ + def m + Const + end + } + C.new.m + } + ae %q{ + Const = :top + class C + Const = :C + end + C.class_eval{ + def m + Const + end + } + C.new.m + } + end + + def test_instance_eval + ae %q{ + 1.instance_eval{ + self + } + } + ae %q{ + 'foo'.instance_eval{ + self + } + } + ae %q{ + class Fixnum + Const = 1 + end + 1.instance_eval %{ + Const + } + } + end + + def test_nest_eval + ae %q{ + Const = :top + class C + Const = :C + end + $nest = false + $ans = [] + def m + $ans << Const + C.module_eval %{ + $ans << Const + Boo = false unless defined? Boo + unless $nest + $nest = true + m + end + } + end + m + $ans + } + ae %q{ + $nested = false + $ans = [] + $pr = proc{ + $ans << self + unless $nested + $nested = true + $pr.call + end + } + class C + def initialize &b + 10.instance_eval(&b) + end + end + C.new(&$pr) + $ans + } + end + + def test_binding + ae %q{ + def m + a = :ok + $b = binding + end + m + eval('a', $b) + } + ae %q{ + def m + a = :ok + $b = binding + end + m + eval('b = :ok2', $b) + eval('[a, b]', $b) + } + ae %q{ + $ans = [] + def m + $b = binding + end + m + $ans << eval(%q{ + $ans << eval(%q{ + a + }, $b) + a = 1 + }, $b) + $ans + } + ae %q{ + Const = :top + class C + Const = :C + def m + binding + end + end + eval('Const', C.new.m) + } + ae %q{ + Const = :top + a = 1 + class C + Const = :C + def m + eval('Const', TOPLEVEL_BINDING) + end + end + C.new.m + } + ae %q{ + class C + $b = binding + end + eval %q{ + def m + :ok + end + }, $b + p C.new.m + } + ae %q{ + b = proc{ + a = :ok + binding + }.call + a = :ng + eval("a", b) + } + ae %q{ + class C + def foo + binding + end + end + C.new.foo.eval("self.class.to_s") + } + end +end + |