From 67a1cc5c9ef610512c7f8ac3cec954ecdaab0f80 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Mon, 21 Mar 2011 16:56:44 -0500 Subject: [PATCH] set JavaScript function() names from Ruby. --- ext/v8/v8_func.cpp | 2 ++ lib/v8/function.rb | 14 ++++++++++++++ spec/ext/func_spec.rb | 15 +++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/ext/v8/v8_func.cpp b/ext/v8/v8_func.cpp index 2d1a564..65e12a4 100644 --- a/ext/v8/v8_func.cpp +++ b/ext/v8/v8_func.cpp @@ -39,9 +39,11 @@ namespace { return rr_v8_ref_create(rr_cV8_C_Object, function->NewInstance(argc, argv)); } VALUE GetName(VALUE self) { + HandleScope scope; return rr_v82rb(unwrap(self)->GetName()); } VALUE SetName(VALUE self, VALUE name) { + HandleScope scope; Local str = V8_Ref_Get(name); unwrap(self)->SetName(str); return Qnil; diff --git a/lib/v8/function.rb b/lib/v8/function.rb index 5092b8a..67bd79e 100644 --- a/lib/v8/function.rb +++ b/lib/v8/function.rb @@ -24,5 +24,19 @@ module V8 to.rb(@native.NewInstance(to.v8(args))) end end + + def name + @portal.open do |to| + to.rb(@native.GetName()) + end + end + + def name=(name) + name.tap do + @portal.open do |to| + @native.SetName(to.v8(name)) + end + end + end end end \ No newline at end of file diff --git a/spec/ext/func_spec.rb b/spec/ext/func_spec.rb index b1be29d..87d3ca2 100644 --- a/spec/ext/func_spec.rb +++ b/spec/ext/func_spec.rb @@ -46,4 +46,19 @@ describe C::Function do cxt.eval('say("Hello", 3)').should == "HelloHelloHello" end end + + it "has a name" do + Context.new do |cxt| + f = cxt.eval('(function hi() {return "Hello World"})', '') + f.name.should == "hi" + end + end + + it "can have its name set" do + Context.new do |cxt| + f = cxt.eval('(function () {return "Goodbye World"})', '') + f.name = 'bye' + f.name.should == 'bye' + end + end end \ No newline at end of file