diff --git a/spike.rb b/spike.rb index 58f3310..4a3e54e 100644 --- a/spike.rb +++ b/spike.rb @@ -32,9 +32,9 @@ o = V8::C::ObjectTemplate.new o.Set("hello", f) o.Set("nello", f2) V8::C::Context.new(o).open do |cxt| - puts "r1: " + cxt.eval('nello()') + puts "r1: " + cxt.eval('nello()').to_s puts "r2: " + cxt.eval('hello()') -end; +end diff --git a/v8_cxt.cpp b/v8_cxt.cpp index 87a25ef..b596865 100644 --- a/v8_cxt.cpp +++ b/v8_cxt.cpp @@ -11,20 +11,20 @@ VALUE v8_Context_New(int argc, VALUE *argv, VALUE self) { if (NIL_P(scope)) { return V8_Ref_Create(self, Context::New()); } else { - V8_Ref_Get(ObjectTemplate, t, scope); + Local t = V8_Ref_Get(scope); return V8_Ref_Create(self, Context::New(0, t)); } } VALUE v8_cxt_Global(VALUE self) { - V8_Ref_Get(Context, cxt, self); + Local cxt = V8_Ref_Get(self); cxt->Global(); return Qnil; } VALUE v8_cxt_open(VALUE self) { HandleScope handles; - V8_Ref_Get(Context, cxt, self); + Local cxt = V8_Ref_Get(self); Context::Scope enter(cxt); if (rb_block_given_p()) { return rb_yield(self); diff --git a/v8_ref.cpp b/v8_ref.cpp index 6997a44..0d586d1 100644 --- a/v8_ref.cpp +++ b/v8_ref.cpp @@ -22,3 +22,6 @@ void v8_ref_free(v8_ref* ref) { delete ref; } +VALUE V8_Ref_Create(VALUE ruby_class, v8::Handle handle, VALUE ref) { + return Data_Wrap_Struct(ruby_class, v8_ref_mark, v8_ref_free, new v8_ref(handle, ref)); +} diff --git a/v8_ref.h b/v8_ref.h index 0408de0..e4b3ae2 100644 --- a/v8_ref.h +++ b/v8_ref.h @@ -20,23 +20,12 @@ struct v8_ref { void v8_ref_mark(v8_ref* ref); void v8_ref_free(v8_ref* ref); +VALUE V8_Ref_Create(VALUE ruby_class, v8::Handle handle, VALUE ref = 0); -//macros for helping make references - -//creates a ruby VALUE reference out of any v8::Handle -// *clazz* a ruby VALUE corresponding to the ruby class of the resulting reference -// *handle* a v8::Handle object to reference from ruby. -// example: -// VALUE myclass = rb_define_class("MyClass"); -// VALUE instance = V8_Ref_Create(clazz, String::New("This is my instance")); - -#define V8_Ref_Create(clazz,handle) Data_Wrap_Struct(clazz,v8_ref_mark, v8_ref_free, new v8_ref(handle)) -#define V8_Ref_Create2(clazz,handle, references) Data_Wrap_Struct(clazz,v8_ref_mark, v8_ref_free, new v8_ref(handle, references)) - -//Dereferences a ruby reference to a V8 object and place it in a Local handle. -// *type* V8 type of the reference. e.g. Context, Object, FunctionTemplate -// *var* the name of the variable to stuff the V8 object in. -// *value* the ruby VALUE object which contains the V8 reference (usually created with V8_Ref_Create()) -#define V8_Ref_Get(type,var,value) v8_ref* __ ## var ## _ref__ = 0; Data_Get_Struct(value, struct v8_ref, __ ## var ## _ref__); Local var = (type *)*__ ## var ## _ref__->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 \ No newline at end of file diff --git a/v8_script.cpp b/v8_script.cpp index e18f660..c3e9944 100644 --- a/v8_script.cpp +++ b/v8_script.cpp @@ -11,13 +11,13 @@ using namespace v8; VALUE v8_script_new(VALUE self, VALUE source) { HandleScope handles; - V8_Ref_Get(String, src, source); + Local src = V8_Ref_Get(source); return V8_Ref_Create(self, Script::New(src)); } VALUE v8_script_Run(VALUE self) { HandleScope handles; - V8_Ref_Get(Script, script, self); + Local