Skip to content

Commit 4fbdb7d

Browse files
committed
Merge pull request #233 from stesie/issue-230
Don't call ObjectTemplate.Set with Object instances, fixes #230
2 parents 5fa653d + b21ba32 commit 4fbdb7d

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

v8js_methods.cc

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ V8JS_METHOD(require)
208208
V8JS_TSRMLS_FETCH();
209209

210210
// Get the extension context
211-
v8::Handle<v8::External> data = v8::Handle<v8::External>::Cast(info.Data());
211+
v8::Local<v8::External> data = v8::Local<v8::External>::Cast(info.Data());
212212
v8js_ctx *c = static_cast<v8js_ctx*>(data->Value());
213213

214214
// Check that we have a module loader
@@ -411,25 +411,23 @@ V8JS_METHOD(require)
411411
}
412412

413413
// Create a template for the global object and set the built-in global functions
414-
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
415-
global->Set(V8JS_SYM("print"), v8::FunctionTemplate::New(isolate, V8JS_MN(print)), v8::ReadOnly);
416-
global->Set(V8JS_SYM("var_dump"), v8::FunctionTemplate::New(isolate, V8JS_MN(var_dump)), v8::ReadOnly);
417-
global->Set(V8JS_SYM("sleep"), v8::FunctionTemplate::New(isolate, V8JS_MN(sleep)), v8::ReadOnly);
418-
global->Set(V8JS_SYM("require"), v8::FunctionTemplate::New(isolate, V8JS_MN(require), v8::External::New(isolate, c)), v8::ReadOnly);
414+
v8::Local<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New();
415+
global_template->Set(V8JS_SYM("print"), v8::FunctionTemplate::New(isolate, V8JS_MN(print)), v8::ReadOnly);
416+
global_template->Set(V8JS_SYM("var_dump"), v8::FunctionTemplate::New(isolate, V8JS_MN(var_dump)), v8::ReadOnly);
417+
global_template->Set(V8JS_SYM("sleep"), v8::FunctionTemplate::New(isolate, V8JS_MN(sleep)), v8::ReadOnly);
418+
global_template->Set(V8JS_SYM("require"), v8::FunctionTemplate::New(isolate, V8JS_MN(require), v8::External::New(isolate, c)), v8::ReadOnly);
419419

420420
// Add the exports object in which the module can return its API
421421
v8::Local<v8::ObjectTemplate> exports_template = v8::ObjectTemplate::New();
422-
v8::Local<v8::Object> exports = exports_template->NewInstance();
423-
global->Set(V8JS_SYM("exports"), exports);
422+
global_template->Set(V8JS_SYM("exports"), exports_template);
424423

425424
// Add the module object in which the module can have more fine-grained control over what it can return
426-
v8::Handle<v8::ObjectTemplate> module_template = v8::ObjectTemplate::New();
427-
v8::Handle<v8::Object> module = module_template->NewInstance();
428-
module->Set(V8JS_SYM("id"), V8JS_STR(normalised_module_id));
429-
global->Set(V8JS_SYM("module"), module);
425+
v8::Local<v8::ObjectTemplate> module_template = v8::ObjectTemplate::New();
426+
module_template->Set(V8JS_SYM("id"), V8JS_STR(normalised_module_id));
427+
global_template->Set(V8JS_SYM("module"), module_template);
430428

431429
// Each module gets its own context so different modules do not affect each other
432-
v8::Local<v8::Context> context = v8::Local<v8::Context>::New(isolate, v8::Context::New(isolate, NULL, global));
430+
v8::Local<v8::Context> context = v8::Local<v8::Context>::New(isolate, v8::Context::New(isolate, NULL, global_template));
433431

434432
// Catch JS exceptions
435433
v8::TryCatch try_catch;
@@ -487,16 +485,19 @@ V8JS_METHOD(require)
487485
return;
488486
}
489487

490-
v8::Handle<v8::Object> newobj;
488+
v8::Local<v8::Object> newobj;
491489

492490
// Cache the module so it doesn't need to be compiled and run again
493491
// Ensure compatibility with CommonJS implementations such as NodeJS by playing nicely with module.exports and exports
494-
if (module->Has(V8JS_SYM("exports")) && !module->Get(V8JS_SYM("exports"))->IsUndefined()) {
492+
if (context->Global()->Has(V8JS_SYM("module"))
493+
&& context->Global()->Get(V8JS_SYM("module"))->IsObject()
494+
&& context->Global()->Get(V8JS_SYM("module"))->ToObject()->Has(V8JS_SYM("exports"))
495+
&& context->Global()->Get(V8JS_SYM("module"))->ToObject()->Get(V8JS_SYM("exports"))->IsObject()) {
495496
// If module.exports has been set then we cache this arbitrary value...
496-
newobj = module->Get(V8JS_SYM("exports"))->ToObject();
497+
newobj = context->Global()->Get(V8JS_SYM("module"))->ToObject()->Get(V8JS_SYM("exports"))->ToObject();
497498
} else {
498499
// ...otherwise we cache the exports object itself
499-
newobj = exports;
500+
newobj = context->Global()->Get(V8JS_SYM("exports"))->ToObject();
500501
}
501502

502503
c->modules_loaded[normalised_module_id].Reset(isolate, newobj);

0 commit comments

Comments
 (0)