@@ -208,7 +208,7 @@ V8JS_METHOD(require)
208
208
V8JS_TSRMLS_FETCH ();
209
209
210
210
// 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 ());
212
212
v8js_ctx *c = static_cast <v8js_ctx*>(data->Value ());
213
213
214
214
// Check that we have a module loader
@@ -411,25 +411,23 @@ V8JS_METHOD(require)
411
411
}
412
412
413
413
// 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);
419
419
420
420
// Add the exports object in which the module can return its API
421
421
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);
424
423
425
424
// 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);
430
428
431
429
// 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 ));
433
431
434
432
// Catch JS exceptions
435
433
v8::TryCatch try_catch;
@@ -487,16 +485,19 @@ V8JS_METHOD(require)
487
485
return ;
488
486
}
489
487
490
- v8::Handle <v8::Object> newobj;
488
+ v8::Local <v8::Object> newobj;
491
489
492
490
// Cache the module so it doesn't need to be compiled and run again
493
491
// 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 ()) {
495
496
// 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 ();
497
498
} else {
498
499
// ...otherwise we cache the exports object itself
499
- newobj = exports;
500
+ newobj = context-> Global ()-> Get ( V8JS_SYM ( " exports" ))-> ToObject () ;
500
501
}
501
502
502
503
c->modules_loaded [normalised_module_id].Reset (isolate, newobj);
0 commit comments