From f6a618aecc3ed71cbd7e79a0554276bfd58d4203 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Sun, 10 Apr 2011 23:19:26 -0500 Subject: [PATCH] substitute generic handle struct for everywhere that was using refs --- ext/v8/rr.cpp | 3 ++- ext/v8/v8_array.cpp | 10 +++++----- ext/v8/v8_cxt.cpp | 14 +++++++------- ext/v8/v8_date.cpp | 10 +++++----- ext/v8/v8_exception.cpp | 17 ++++++++--------- ext/v8/v8_external.cpp | 10 +++++----- ext/v8/v8_func.cpp | 19 ++++++++++--------- ext/v8/v8_func.h | 1 - ext/v8/v8_handle.cpp | 14 ++------------ ext/v8/v8_handle.h | 2 +- ext/v8/v8_msg.cpp | 11 +++++------ ext/v8/v8_obj.cpp | 19 +++++++++---------- ext/v8/v8_ref.cpp | 37 ------------------------------------- ext/v8/v8_ref.h | 28 ---------------------------- ext/v8/v8_script.cpp | 8 ++++---- ext/v8/v8_str.cpp | 14 +++++++------- ext/v8/v8_template.cpp | 33 ++++++++++++++++++--------------- ext/v8/v8_v8.cpp | 7 +++++++ ext/v8/v8_value.cpp | 10 +++++----- spec/ext/handle_spec.rb | 10 +++++++--- 20 files changed, 107 insertions(+), 170 deletions(-) delete mode 100644 ext/v8/v8_ref.cpp delete mode 100644 ext/v8/v8_ref.h diff --git a/ext/v8/rr.cpp b/ext/v8/rr.cpp index 4dacae9..a89f71e 100644 --- a/ext/v8/rr.cpp +++ b/ext/v8/rr.cpp @@ -1,5 +1,6 @@ #include "rr.h" #include "v8_cxt.h" +#include "v8_handle.h" #include "v8_value.h" #include "v8_obj.h" #include "v8_func.h" @@ -142,7 +143,7 @@ Handle rr_rb2v8(VALUE value) { case T_FALSE: return False(); case T_DATA: - return V8_Ref_Get(value); + return rr_v8_handle(value); case T_OBJECT: case T_CLASS: case T_ICLASS: diff --git a/ext/v8/v8_array.cpp b/ext/v8/v8_array.cpp index 53a4354..fff097f 100644 --- a/ext/v8/v8_array.cpp +++ b/ext/v8/v8_array.cpp @@ -1,5 +1,5 @@ +#include "v8_handle.h" #include "v8_array.h" -#include "v8_ref.h" #include "v8_obj.h" using namespace v8; @@ -9,8 +9,8 @@ namespace { VALUE ArrayClass; - Local unwrap(VALUE self) { - return V8_Ref_Get(self); + Persistent& unwrap(VALUE self) { + return rr_v8_handle(self); } VALUE New(int argc, VALUE *argv, VALUE self) { @@ -24,7 +24,7 @@ namespace { length = INT2FIX(0); } HandleScope scope; - return rr_v8_ref_create(self, Array::New(NUM2INT(length))); + return rr_v8_handle_new(self, Array::New(NUM2INT(length))); } VALUE Length(VALUE self) { @@ -46,5 +46,5 @@ void rr_init_v8_array() { VALUE rr_reflect_v8_array(Handle value) { Local array(Array::Cast(*value)); Local peer = array->GetHiddenValue(String::NewSymbol("TheRubyRacer::RubyObject")); - return peer.IsEmpty() ? rr_v8_ref_create(ArrayClass, value) : (VALUE)External::Unwrap(peer); + return peer.IsEmpty() ? rr_v8_handle_new(ArrayClass, value) : (VALUE)External::Unwrap(peer); } \ No newline at end of file diff --git a/ext/v8/v8_cxt.cpp b/ext/v8/v8_cxt.cpp index 1f22a10..578d5ee 100644 --- a/ext/v8/v8_cxt.cpp +++ b/ext/v8/v8_cxt.cpp @@ -1,5 +1,5 @@ #include "rr.h" -#include "v8_ref.h" +#include "v8_handle.h" #include "v8_cxt.h" #include "v8_msg.h" #include "v8_template.h" @@ -11,18 +11,18 @@ namespace { VALUE ContextClass; - Local unwrap(VALUE value) { - return V8_Ref_Get(value); + Persistent& unwrap(VALUE value) { + return rr_v8_handle(value); } VALUE New(int argc, VALUE *argv, VALUE self) { HandleScope handles; VALUE global_template; VALUE global_object; rb_scan_args(argc,argv, "02", &global_template, &global_object); - Handle v8_global_template(NIL_P(global_template) ? Handle() : V8_Ref_Get(global_template)); - Handle v8_global_object(NIL_P(global_object) ? Handle() : V8_Ref_Get(global_object)); + Handle v8_global_template(NIL_P(global_template) ? Handle() : rr_v8_handle(global_template)); + Handle v8_global_object(NIL_P(global_object) ? Handle() : rr_v8_handle(global_object)); Persistent cxt(Context::New(0, v8_global_template, v8_global_object)); - VALUE ref = rr_v8_ref_create(self, cxt); + VALUE ref = rr_v8_handle_new(self, cxt); cxt.Dispose(); return ref; } @@ -35,7 +35,7 @@ namespace { HandleScope handles; if (Context::InContext()) { Local current = Context::GetEntered(); - return rr_v8_ref_create(self, current); + return rr_v8_handle_new(self, current); } else { return Qnil; } diff --git a/ext/v8/v8_date.cpp b/ext/v8/v8_date.cpp index 94a413a..7de6e2f 100644 --- a/ext/v8/v8_date.cpp +++ b/ext/v8/v8_date.cpp @@ -1,7 +1,7 @@ #include "rr.h" #include "v8_date.h" #include "v8_value.h" -#include "v8_ref.h" +#include "v8_handle.h" using namespace v8; @@ -11,14 +11,13 @@ namespace { VALUE New(VALUE self, VALUE time) { HandleScope scope; - return rr_v8_ref_create(self, Date::New(NUM2DBL(time))); + return rr_v8_handle_new(self, Date::New(NUM2DBL(time))); } // Override Value::NumberValue in order to ensure that we call the more specific and optimized // Number Value in v8::Date VALUE NumberValue(VALUE self) { - HandleScope scope; - Local date = V8_Ref_Get(self); + Persistent date = rr_v8_handle(self); return rr_v82rb(date->NumberValue()); } } @@ -30,6 +29,7 @@ void rr_init_v8_date() { } VALUE rr_reflect_v8_date(Handle value) { + HandleScope hs; Local date(Date::Cast(*value)); - return rr_v8_ref_create(DateClass, date); + return rr_v8_handle_new(DateClass, date); } \ No newline at end of file diff --git a/ext/v8/v8_exception.cpp b/ext/v8/v8_exception.cpp index 73b53ef..2f46981 100644 --- a/ext/v8/v8_exception.cpp +++ b/ext/v8/v8_exception.cpp @@ -1,6 +1,6 @@ #include "v8_exception.h" #include "rr.h" -#include "v8_ref.h" +#include "v8_handle.h" #include "execinfo.h" #include "signal.h" @@ -44,8 +44,8 @@ namespace { VALUE StackFrameClass; namespace Trace { - Local trace(VALUE value) { - return V8_Ref_Get(value); + Persistent& trace(VALUE value) { + return rr_v8_handle(value); } VALUE GetFrame(VALUE self, VALUE index) { HandleScope scope; @@ -53,8 +53,7 @@ namespace { } VALUE GetFrameCount(VALUE self) { HandleScope scope; - Local t = trace(self); - return rr_v82rb(t->GetFrameCount()); + return rr_v82rb(trace(self)->GetFrameCount()); } VALUE AsArray(VALUE self) { return rr_v82rb(trace(self)->AsArray()); @@ -65,8 +64,8 @@ namespace { } namespace Frame { - Local frame(VALUE value) { - return V8_Ref_Get(value); + Persistent& frame(VALUE value) { + return rr_v8_handle(value); } VALUE GetLineNumber(VALUE self) { HandleScope scope; @@ -127,8 +126,8 @@ void rr_init_v8_exception() { } VALUE rr_reflect_v8_stacktrace(Handle value) { - return rr_v8_ref_create(StackTraceClass, value); + return rr_v8_handle_new(StackTraceClass, value); } VALUE rr_reflect_v8_stackframe(Handle value) { - return rr_v8_ref_create(StackFrameClass, value); + return rr_v8_handle_new(StackFrameClass, value); } diff --git a/ext/v8/v8_external.cpp b/ext/v8/v8_external.cpp index 832f3fc..0f8f528 100644 --- a/ext/v8/v8_external.cpp +++ b/ext/v8/v8_external.cpp @@ -1,7 +1,7 @@ #include "rr.h" #include "v8_external.h" -#include "v8_ref.h" +#include "v8_handle.h" #include "v8_value.h" using namespace v8; @@ -11,12 +11,12 @@ namespace { VALUE New(VALUE rbclass, VALUE value) { HandleScope scope; - return rr_v8_ref_create(rbclass, rr_v8_external_create(value)); + return rr_v8_handle_new(rbclass, rr_v8_external_create(value)); } VALUE Unwrap(VALUE self, VALUE value) { HandleScope scope; if (rb_obj_is_kind_of(value, self)) { - return (VALUE)External::Unwrap(V8_Ref_Get(self)); + return (VALUE)External::Unwrap(rr_v8_handle(self)); } else { rb_raise(rb_eArgError, "cannot unwrap %s. It is not a kind of %s", RSTRING_PTR(rb_class_name(rb_class_of(value))), RSTRING_PTR(rb_class_name(self))); return Qnil; @@ -24,7 +24,7 @@ namespace { } VALUE _Value(VALUE self) { HandleScope scope; - return (VALUE)V8_Ref_Get(self)->Value(); + return (VALUE)rr_v8_handle(self)->Value(); } void GCWeakReferenceCallback(Persistent object, void* parameter) { Local external(External::Cast(*object)); @@ -42,7 +42,7 @@ void rr_init_v8_external() { } VALUE rr_reflect_v8_external(Handle external) { - return rr_v8_ref_create(ExternalClass, external); + return rr_v8_handle_new(ExternalClass, external); } Handle rr_v8_external_create(VALUE value) { diff --git a/ext/v8/v8_func.cpp b/ext/v8/v8_func.cpp index 65e12a4..27140d6 100644 --- a/ext/v8/v8_func.cpp +++ b/ext/v8/v8_func.cpp @@ -1,14 +1,15 @@ #include "v8_func.h" #include "v8_obj.h" +#include "v8_handle.h" using namespace v8; namespace { VALUE FunctionClass; - Local unwrap(VALUE value) { - return V8_Ref_Get(value); + Persistent& unwrap(VALUE value) { + return rr_v8_handle(value); } VALUE Call(VALUE self, VALUE recv, VALUE arguments) { HandleScope handles; @@ -16,9 +17,9 @@ namespace { rb_raise(rb_eScriptError, "no open V8 Context in V8::C::Function::Call()"); return Qnil; } - Local function = unwrap(self); + Handle function = unwrap(self); Local thisObj = rr_rb2v8(recv)->ToObject(); - Handle args = V8_Ref_Get(arguments); + Handle args = rr_v8_handle(arguments); int argc = args->Length(); Handle argv[argc]; for (int i = 0; i < argc; i++) { @@ -29,14 +30,14 @@ namespace { VALUE NewInstance(VALUE self, VALUE arguments) { HandleScope scope; - Local function = unwrap(self); - Handle args = V8_Ref_Get(arguments); + Handle function = unwrap(self); + Handle args = rr_v8_handle(arguments); int argc = args->Length(); Handle argv[argc]; for (int i = 0; i < argc; i++) { argv[i] = args->Get(i); } - return rr_v8_ref_create(rr_cV8_C_Object, function->NewInstance(argc, argv)); + return rr_v8_handle_new(rr_cV8_C_Object, function->NewInstance(argc, argv)); } VALUE GetName(VALUE self) { HandleScope scope; @@ -44,7 +45,7 @@ namespace { } VALUE SetName(VALUE self, VALUE name) { HandleScope scope; - Local str = V8_Ref_Get(name); + Handle str = rr_v8_handle(name); unwrap(self)->SetName(str); return Qnil; } @@ -64,5 +65,5 @@ void rr_init_func() { VALUE rr_reflect_v8_function(Handle value) { Local function(Function::Cast(*value)); - return rr_v8_ref_create(FunctionClass, function); + return rr_v8_handle_new(FunctionClass, function); } diff --git a/ext/v8/v8_func.h b/ext/v8/v8_func.h index 555cc96..d4ec72b 100644 --- a/ext/v8/v8_func.h +++ b/ext/v8/v8_func.h @@ -3,7 +3,6 @@ #include "rr.h" #include "v8.h" -#include "v8_ref.h" void rr_init_func(); diff --git a/ext/v8/v8_handle.cpp b/ext/v8/v8_handle.cpp index 2efa59a..9f4c3ad 100644 --- a/ext/v8/v8_handle.cpp +++ b/ext/v8/v8_handle.cpp @@ -1,6 +1,5 @@ #include "rr.h" -#include "v8_ref.h" #include "v8_handle.h" using namespace v8; @@ -21,9 +20,8 @@ namespace { VALUE New(VALUE self, VALUE handle) { if (RTEST(handle)) { - v8_ref* ref = 0; - Data_Get_Struct(handle, struct v8_ref, ref); - return rr_v8_handle_new(self, ref->handle); + Persistent that = rr_v8_handle(handle); + return rr_v8_handle_new(self, that); } else { return rr_v8_handle_new(self, Handle()); } @@ -62,12 +60,6 @@ namespace { VALUE IsWeak(VALUE self) { return rr_v82rb(rr_v8_handle(self).IsWeak()); } - - VALUE NewContext(VALUE self) { - Persistent cxt(Context::New(0)); - return rr_v8_handle_new(self, cxt); - cxt.Dispose(); - } } void rr_init_handle() { @@ -80,8 +72,6 @@ void rr_init_handle() { rr_define_method(HandleClass, "ClearWeak", ClearWeak, 0); rr_define_method(HandleClass, "IsNearDeath", IsNearDeath, 0); rr_define_method(HandleClass, "IsWeak", IsWeak, 0); - - rr_define_singleton_method(HandleClass, "NewContext", NewContext, 0); } VALUE rr_v8_handle_new(VALUE klass, v8::Handle handle) { diff --git a/ext/v8/v8_handle.h b/ext/v8/v8_handle.h index eba2344..cdee1fd 100644 --- a/ext/v8/v8_handle.h +++ b/ext/v8/v8_handle.h @@ -15,7 +15,7 @@ void rr_init_handle(); template v8::Persistent& rr_v8_handle(VALUE value) { v8_handle* handle = 0; Data_Get_Struct(value, struct v8_handle, handle); - return handle->handle; + return (v8::Persistent&)handle->handle; } VALUE rr_v8_handle_new(VALUE rbclass, v8::Handle handle); diff --git a/ext/v8/v8_msg.cpp b/ext/v8/v8_msg.cpp index 4d36349..d547b6e 100644 --- a/ext/v8/v8_msg.cpp +++ b/ext/v8/v8_msg.cpp @@ -1,18 +1,17 @@ #include "v8_msg.h" -#include "v8_ref.h" +#include "v8_handle.h" using namespace v8; namespace { VALUE MessageClass; - Local unwrap(VALUE self) { - return V8_Ref_Get(self); + Persistent& unwrap(VALUE self) { + return rr_v8_handle(self); } VALUE Get(VALUE self) { - Local message(unwrap(self)); - return rr_v82rb(message->Get()); + return rr_v82rb(unwrap(self)->Get()); } VALUE GetSourceLine(VALUE self) { @@ -63,6 +62,6 @@ void rr_init_msg() { } VALUE rr_reflect_v8_message(Handle value) { - return rr_v8_ref_create(MessageClass, value); + return rr_v8_handle_new(MessageClass, value); } diff --git a/ext/v8/v8_obj.cpp b/ext/v8/v8_obj.cpp index 729f4cb..4e44167 100644 --- a/ext/v8/v8_obj.cpp +++ b/ext/v8/v8_obj.cpp @@ -1,5 +1,5 @@ +#include "v8_handle.h" #include "v8_obj.h" -#include "v8_ref.h" #include "v8_value.h" #include "v8_template.h" #include "v8_external.h" @@ -12,13 +12,13 @@ VALUE rr_cV8_C_Object; namespace { - Local unwrap(VALUE robj) { - return V8_Ref_Get(robj); + Persistent& unwrap(VALUE object) { + return rr_v8_handle(object); } VALUE Get(VALUE self, VALUE key) { HandleScope handles; - Local obj(unwrap(self)); + Persistent obj(unwrap(self)); if (rb_obj_is_kind_of(key, rb_cNumeric)) { return rr_v82rb(obj->Get(NUM2UINT(key))); } else { @@ -32,12 +32,12 @@ namespace { rb_raise(rb_eScriptError, "Object::New() called without an entered Context"); return Qnil; } - return rr_v8_ref_create(rbclass, Object::New()); + return rr_v8_handle_new(rbclass, Object::New()); } VALUE Set(VALUE self, VALUE key, VALUE value) { HandleScope handles; - Local obj = unwrap(self); + Persistent obj = unwrap(self); if (rb_obj_is_kind_of(key, rb_cNumeric)) { return rr_v82rb(obj->Set(NUM2UINT(key), rr_rb2v8(value))); } else { @@ -47,7 +47,7 @@ namespace { VALUE GetPropertyNames(VALUE self) { HandleScope handles; - Local object = unwrap(self); + Persistent object = unwrap(self); Local names = object->GetPropertyNames(); return rr_v82rb(names); } @@ -70,8 +70,7 @@ namespace { } VALUE SetPrototype(VALUE self, VALUE prototype) { HandleScope scope; - Handle proto = rr_rb2v8(prototype); - Local me = unwrap(self); + Handle proto(rr_rb2v8(prototype)); return rr_v82rb(unwrap(self)->SetPrototype(rr_rb2v8(prototype))); } } @@ -91,6 +90,6 @@ void rr_init_obj() { VALUE rr_reflect_v8_object(Handle value) { Local object(Object::Cast(*value)); Local peer = object->GetHiddenValue(String::NewSymbol("TheRubyRacer::RubyObject")); - return peer.IsEmpty() ? rr_v8_ref_create(rr_cV8_C_Object, object) : (VALUE)External::Unwrap(peer); + return peer.IsEmpty() ? rr_v8_handle_new(rr_cV8_C_Object, object) : (VALUE)External::Unwrap(peer); } diff --git a/ext/v8/v8_ref.cpp b/ext/v8/v8_ref.cpp deleted file mode 100644 index 194212d..0000000 --- a/ext/v8/v8_ref.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include "stdio.h" -using namespace v8; - -v8_ref::v8_ref(Handle object) : handle(Persistent::New(object)) { - this->references = rb_hash_new(); -} - -v8_ref::~v8_ref() { - handle.Dispose(); -} - -void v8_ref::set(const char *name, VALUE ref) { - if (ref != 0 && RTEST(ref)) { - rb_hash_aset(this->references, rb_str_new2(name), ref); - } -} - -namespace { - void gc_mark(v8_ref* ref) { - rb_gc_mark(ref->references); - } - - void gc_free(v8_ref* ref) { - delete ref; - } -} - -VALUE rr_v8_ref_create(VALUE rbclass, v8::Handle handle) { - return Data_Wrap_Struct(rbclass, gc_mark, gc_free, new v8_ref(handle)); -} - -void rr_v8_ref_setref(VALUE handle, const char *name, VALUE newref) { - v8_ref *ref = 0; - Data_Get_Struct(handle, struct v8_ref, ref); - ref->set(name, newref); -} \ No newline at end of file diff --git a/ext/v8/v8_ref.h b/ext/v8/v8_ref.h deleted file mode 100644 index 237173a..0000000 --- a/ext/v8/v8_ref.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef _RUBY_V8_REF_ -#define _RUBY_V8_REF_ - -#include -#include "ruby.h" - -//the v8_ref wraps a v8 handle so that ruby can hold a reference to it. - -struct v8_ref { - //takes a handle object and adds a new persistent handle for - //the referenced object - v8_ref(v8::Handle object); - virtual ~v8_ref(); - void set(const char *name, VALUE ref); - v8::Persistent handle; - VALUE references; -}; - -void rr_v8_ref_setref(VALUE handle, const char *name, VALUE ref); -VALUE rr_v8_ref_create(VALUE rbclass, v8::Handle handle); - -template v8::Local V8_Ref_Get(VALUE object) { - v8_ref* ref = 0; - Data_Get_Struct(object, struct v8_ref, ref); - return (T *)*ref->handle; -} - -#endif diff --git a/ext/v8/v8_script.cpp b/ext/v8/v8_script.cpp index c610742..caf0fce 100644 --- a/ext/v8/v8_script.cpp +++ b/ext/v8/v8_script.cpp @@ -1,5 +1,5 @@ #include "v8.h" -#include "v8_ref.h" +#include "v8_handle.h" #include "v8_script.h" using namespace v8; @@ -10,19 +10,19 @@ namespace { HandleScope scope; Local src(rr_rb2v8(source)->ToString()); Local src_name(rr_rb2v8(source_name)->ToString()); - return rr_v8_ref_create(self, Script::Compile(src, src_name)); + return rr_v8_handle_new(self, Script::Compile(src, src_name)); } VALUE Compile(VALUE self, VALUE source, VALUE source_name) { HandleScope scope; Local src(rr_rb2v8(source)->ToString()); Local src_name(rr_rb2v8(source_name)->ToString()); - return rr_v8_ref_create(self, Script::Compile(src, src_name)); + return rr_v8_handle_new(self, Script::Compile(src, src_name)); } VALUE Run(VALUE self) { HandleScope scope; - Local