From 45b53341e2dbc61148a3b1482e032fbb60949e0f Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Tue, 26 Apr 2011 11:30:17 -0500 Subject: [PATCH] Handle#MakeWeak() now has a signature faithful to its v8 counterpart. --- ext/v8/v8_handle.cpp | 20 +++++++++++--------- lib/v8/portal/proxies.rb | 12 +++++------- spec/ext/mem_spec.rb | 7 ++----- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/ext/v8/v8_handle.cpp b/ext/v8/v8_handle.cpp index 6950d51..e2c9483 100644 --- a/ext/v8/v8_handle.cpp +++ b/ext/v8/v8_handle.cpp @@ -56,21 +56,23 @@ namespace { } void RubyWeakReferenceCallback(Persistent value, void* parameter) { + VALUE self = (VALUE)parameter; + v8_handle* handle = rr_v8_handle_raw(self); + VALUE callback = rr_v8_handle_get_internal(self, "weakref_callback"); + VALUE parameters = rr_v8_handle_get_internal(self, "weakref_callback_parameters"); + if (RTEST(callback)) { + rb_funcall(callback, rb_intern("call"), 2, self, parameters); + } value.Dispose(); - v8_handle* handle = rr_v8_handle_raw((VALUE)parameter); handle->handle.Dispose(); handle->handle.Clear(); handle->dead = true; - VALUE callback = rr_v8_handle_get_internal((VALUE)parameter, "weakref_callback"); - if (RTEST(callback)) { - rb_funcall(callback, rb_intern("call"), 0); - } + } - - VALUE MakeWeak(VALUE self) { - VALUE callback = rb_block_given_p() ? rb_block_proc() : Qnil; + VALUE MakeWeak(VALUE self, VALUE parameters, VALUE callback) { rr_v8_handle_set_internal(self,"weakref_callback", callback); + rr_v8_handle_set_internal(self, "weakref_callback_parameters", parameters); rr_v8_handle(self).MakeWeak((void*)self, RubyWeakReferenceCallback); return Qnil; } @@ -100,7 +102,7 @@ void rr_init_handle() { rr_define_method(HandleClass, "IsEmpty", IsEmpty, 0); rr_define_method(HandleClass, "Clear", Clear, 0); rr_define_method(HandleClass, "Dispose", Dispose, 0); - rr_define_method(HandleClass, "MakeWeak", MakeWeak, 0); + rr_define_method(HandleClass, "MakeWeak", MakeWeak, 2); rr_define_method(HandleClass, "ClearWeak", ClearWeak, 0); rr_define_method(HandleClass, "IsNearDeath", IsNearDeath, 0); rr_define_method(HandleClass, "IsWeak", IsWeak, 0); diff --git a/lib/v8/portal/proxies.rb b/lib/v8/portal/proxies.rb index 03b635c..663b796 100644 --- a/lib/v8/portal/proxies.rb +++ b/lib/v8/portal/proxies.rb @@ -39,7 +39,7 @@ module V8 @js_proxies_js2rb[proxy] = target @js_proxies_rb2js[target] = proxy - proxy.MakeWeak(&clear_js_proxy(proxy)) + proxy.MakeWeak(nil, method(:clear_js_proxy)) end def rb_object_2_js_proxy(object) @@ -68,12 +68,10 @@ module V8 @rb_proxies_rb2js[proxy.object_id] end - def clear_js_proxy(proxy) - lambda do - rb = @js_proxies_js2rb[proxy] - @js_proxies_js2rb.delete(proxy) - @js_proxies_rb2js.delete(rb) - end + def clear_js_proxy(proxy, parameter) + rb = @js_proxies_js2rb[proxy] + @js_proxies_js2rb.delete(proxy) + @js_proxies_rb2js.delete(rb) end def clear_rb_proxy(proxy_id) diff --git a/spec/ext/mem_spec.rb b/spec/ext/mem_spec.rb index ffa0a8e..6273379 100644 --- a/spec/ext/mem_spec.rb +++ b/spec/ext/mem_spec.rb @@ -11,7 +11,7 @@ describe "Memory:" do it "has a strong reference from the ruby side, which is not released until the Ruby reference goes away" do handle = c::Handle::New(object = c::Object::New()) - handle.MakeWeak(&@weakref_callback) + handle.MakeWeak(nil, @weakref_callback) ruby_gc do v8_gc @weakref_callback.should_not have_been_invoked @@ -28,11 +28,8 @@ describe "Memory:" do private class WeakrefCallback - def to_proc - method(:invoke).to_proc - end - def invoke + def call(value, parameters) @invoked = true end