From 09fef0a83a23a44e919f7219220e5d37e85db93b Mon Sep 17 00:00:00 2001 From: Rylan Collins Date: Tue, 15 Jul 2014 23:38:03 +0000 Subject: [PATCH] Cleanup --- src/RubyModule.cpp | 22 +++++++++------------- src/RubyModule.h | 2 +- src/RubyObject.h | 3 ++- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/RubyModule.cpp b/src/RubyModule.cpp index 4f0864c..92adb15 100644 --- a/src/RubyModule.cpp +++ b/src/RubyModule.cpp @@ -21,11 +21,12 @@ Local RubyModule::ToV8(VALUE mod) Local tpl = NanNew(); tpl->SetInternalFieldCount(1); + v8Mod = tpl->NewInstance(); + NanSetInternalFieldPointer(v8Mod, 0, (void*)mod); - // TODO: Should we create the instance here or after we add the methods? Does it matter? // Class methods - AddMethods(tpl, rb_class_public_instance_methods(0, NULL, CLASS_OF(mod))); - AddMethods(tpl, rb_obj_singleton_methods(0, NULL, mod)); + AddMethods(v8Mod, rb_class_public_instance_methods(0, NULL, CLASS_OF(mod))); + AddMethods(v8Mod, rb_obj_singleton_methods(0, NULL, mod)); // Constants VALUE constants = rb_mod_constants(0, NULL, mod); @@ -33,7 +34,7 @@ Local RubyModule::ToV8(VALUE mod) ID constantID = SYM2ID(rb_ary_entry(constants, i)); VALUE val = rb_const_get(mod, constantID); - tpl->Set(NanNew(rb_id2name(constantID)), rubyToV8(val)); + v8Mod->Set(NanNew(rb_id2name(constantID)), rubyToV8(val)); } if (TYPE(mod) == T_CLASS) { @@ -41,16 +42,11 @@ Local RubyModule::ToV8(VALUE mod) ID newID = rb_intern("new"); Local newTemplate = NanNew(CallNew, EXTERNAL_WRAP((void*)newID)); - tpl->Set(NanNew("new"), newTemplate->GetFunction()); + v8Mod->Set(NanNew("new"), newTemplate->GetFunction()); Local defMethTpl = NanNew(DefineMethod); - tpl->Set(NanNew("_defineMethod"), defMethTpl->GetFunction()); - } - - v8Mod = tpl->NewInstance(); - NanSetInternalFieldPointer(v8Mod, 0, (void*)mod); - - if (TYPE(mod) == T_CLASS) { + v8Mod->Set(NanNew("_defineMethod"), defMethTpl->GetFunction()); + Handle argv[] = { v8Mod }; Local createCtor = NanNew(s_createCtor); v8Mod = NanMakeCallback(NanGetCurrentContext()->Global(), @@ -76,7 +72,7 @@ Local RubyModule::ToV8(VALUE mod) return NanEscapeScope(v8Mod); } -inline void RubyModule::AddMethods(Handle tpl, VALUE methods) +inline void RubyModule::AddMethods(Handle tpl, VALUE methods) { for (int i = 0; i < RARRAY_LEN(methods); i++) { ID methodID = SYM2ID(rb_ary_entry(methods, i)); diff --git a/src/RubyModule.h b/src/RubyModule.h index ec41e00..025ed5a 100644 --- a/src/RubyModule.h +++ b/src/RubyModule.h @@ -17,7 +17,7 @@ class RubyModule private: - static void AddMethods(v8::Handle tpl, VALUE methods); + static void AddMethods(v8::Handle tpl, VALUE methods); static NAN_METHOD(CallMethod); static NAN_METHOD(CallNew); diff --git a/src/RubyObject.h b/src/RubyObject.h index 8d18066..fba3554 100644 --- a/src/RubyObject.h +++ b/src/RubyObject.h @@ -44,12 +44,13 @@ class RubyObject : public node::ObjectWrap VALUE m_obj; // The pure JS object that holds the reference to this v8::Persistent* m_owner; + #if (NODE_MODULE_VERSION > 0x000B) typedef std::map::CopyablePersistent> TplMap; #else typedef std::map > TplMap; #endif - // TODO: Still needed? + static TplMap s_functionTemplates; static VALUE s_wrappedClass; };