Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rylanc committed Jul 15, 2014
1 parent f84e7c0 commit 09fef0a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
22 changes: 9 additions & 13 deletions src/RubyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,32 @@ Local<Object> RubyModule::ToV8(VALUE mod)

Local<ObjectTemplate> tpl = NanNew<ObjectTemplate>();
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);
for (int i = 0; i < RARRAY_LEN(constants); i++) {
ID constantID = SYM2ID(rb_ary_entry(constants, i));

VALUE val = rb_const_get(mod, constantID);
tpl->Set(NanNew<String>(rb_id2name(constantID)), rubyToV8(val));
v8Mod->Set(NanNew<String>(rb_id2name(constantID)), rubyToV8(val));
}

if (TYPE(mod) == T_CLASS) {
// TODO: new or newInstance?
ID newID = rb_intern("new");
Local<FunctionTemplate> newTemplate =
NanNew<FunctionTemplate>(CallNew, EXTERNAL_WRAP((void*)newID));
tpl->Set(NanNew<String>("new"), newTemplate->GetFunction());
v8Mod->Set(NanNew<String>("new"), newTemplate->GetFunction());

Local<FunctionTemplate> defMethTpl = NanNew<FunctionTemplate>(DefineMethod);
tpl->Set(NanNew<String>("_defineMethod"), defMethTpl->GetFunction());
}

v8Mod = tpl->NewInstance();
NanSetInternalFieldPointer(v8Mod, 0, (void*)mod);

if (TYPE(mod) == T_CLASS) {
v8Mod->Set(NanNew<String>("_defineMethod"), defMethTpl->GetFunction());

Handle<Value> argv[] = { v8Mod };
Local<Function> createCtor = NanNew<Function>(s_createCtor);
v8Mod = NanMakeCallback(NanGetCurrentContext()->Global(),
Expand All @@ -76,7 +72,7 @@ Local<Object> RubyModule::ToV8(VALUE mod)
return NanEscapeScope(v8Mod);
}

inline void RubyModule::AddMethods(Handle<ObjectTemplate> tpl, VALUE methods)
inline void RubyModule::AddMethods(Handle<Object> tpl, VALUE methods)
{
for (int i = 0; i < RARRAY_LEN(methods); i++) {
ID methodID = SYM2ID(rb_ary_entry(methods, i));
Expand Down
2 changes: 1 addition & 1 deletion src/RubyModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class RubyModule

private:

static void AddMethods(v8::Handle<v8::ObjectTemplate> tpl, VALUE methods);
static void AddMethods(v8::Handle<v8::Object> tpl, VALUE methods);

static NAN_METHOD(CallMethod);
static NAN_METHOD(CallNew);
Expand Down
3 changes: 2 additions & 1 deletion src/RubyObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ class RubyObject : public node::ObjectWrap
VALUE m_obj;
// The pure JS object that holds the reference to this
v8::Persistent<v8::Object>* m_owner;

#if (NODE_MODULE_VERSION > 0x000B)
typedef std::map<VALUE, v8::CopyablePersistentTraits<v8::FunctionTemplate>::CopyablePersistent> TplMap;
#else
typedef std::map<VALUE, v8::Persistent<v8::FunctionTemplate> > TplMap;
#endif
// TODO: Still needed?

static TplMap s_functionTemplates;
static VALUE s_wrappedClass;
};
Expand Down

0 comments on commit 09fef0a

Please sign in to comment.