@@ -205,10 +205,10 @@ V8JS_METHOD(var_dump) /* {{{ */
205205V8JS_METHOD (require)
206206{
207207 v8::Isolate *isolate = info.GetIsolate ();
208+ v8js_ctx *c = (v8js_ctx *) isolate->GetData (0 );
208209
209- // Get the extension context
210- v8::Local<v8::External> data = v8::Local<v8::External>::Cast (info.Data ());
211- v8js_ctx *c = static_cast <v8js_ctx*>(data->Value ());
210+ v8::String::Utf8Value module_base (info.Data ());
211+ const char *module_base_cstr = ToCString (module_base);
212212
213213 // Check that we have a module loader
214214 if (Z_TYPE (c->module_loader ) == IS_NULL) {
@@ -225,7 +225,7 @@ V8JS_METHOD(require)
225225 normalised_path = (char *)emalloc (PATH_MAX);
226226 module_name = (char *)emalloc (PATH_MAX);
227227
228- v8js_commonjs_normalise_identifier (c-> modules_base . back () , module_id, normalised_path, module_name);
228+ v8js_commonjs_normalise_identifier (module_base_cstr , module_id, normalised_path, module_name);
229229 }
230230 else {
231231 // Call custom normaliser
@@ -238,7 +238,7 @@ V8JS_METHOD(require)
238238 isolate->Exit ();
239239 v8::Unlocker unlocker (isolate);
240240
241- ZVAL_STRING (¶ms[0 ], c-> modules_base . back () );
241+ ZVAL_STRING (¶ms[0 ], module_base_cstr );
242242 ZVAL_STRING (¶ms[1 ], module_id);
243243
244244 call_result = call_user_function_ex (EG (function_table), NULL , &c->module_normaliser ,
@@ -445,7 +445,7 @@ V8JS_METHOD(require)
445445 v8::Local<v8::String> source = V8JS_ZSTR (Z_STR (module_code));
446446 zval_ptr_dtor (&module_code);
447447
448- source = v8::String::Concat (V8JS_SYM (" (function (exports, module) {" ), source);
448+ source = v8::String::Concat (V8JS_SYM (" (function (exports, module, require ) {" ), source);
449449 source = v8::String::Concat (source, V8JS_SYM (" \n });" ));
450450
451451 // Create and compile script
@@ -459,9 +459,19 @@ V8JS_METHOD(require)
459459 return ;
460460 }
461461
462+ v8::Local<v8::String> base_path = V8JS_STR (normalised_path);
463+ v8::MaybeLocal<v8::Function> require_fn = v8::FunctionTemplate::New (isolate, V8JS_MN (require), base_path)->GetFunction ();
464+
465+ if (require_fn.IsEmpty ()) {
466+ efree (normalised_path);
467+ efree (normalised_module_id);
468+ info.GetReturnValue ().Set (isolate->ThrowException (V8JS_SYM (" Failed to create require method" )));
469+ return ;
470+ }
471+
472+
462473 // Add this module and path to the stack
463474 c->modules_stack .push_back (normalised_module_id);
464- c->modules_base .push_back (normalised_path);
465475
466476 // Run script to evaluate closure
467477 v8::Local<v8::Value> module_function = script->Run ();
@@ -474,20 +484,22 @@ V8JS_METHOD(require)
474484 module ->Set (V8JS_SYM (" exports" ), exports);
475485
476486 if (module_function->IsFunction ()) {
477- v8::Local<v8::Value> *jsArgv = static_cast <v8::Local<v8::Value> *>(alloca (2 * sizeof (v8::Local<v8::Value>)));
487+ v8::Local<v8::Value> *jsArgv = static_cast <v8::Local<v8::Value> *>(alloca (3 * sizeof (v8::Local<v8::Value>)));
478488 new (&jsArgv[0 ]) v8::Local<v8::Value>;
479489 jsArgv[0 ] = exports;
480490
481491 new (&jsArgv[1 ]) v8::Local<v8::Value>;
482492 jsArgv[1 ] = module ;
483493
494+ new (&jsArgv[2 ]) v8::Local<v8::Value>;
495+ jsArgv[2 ] = require_fn.ToLocalChecked ();
496+
484497 // actually call the module
485- v8::Local<v8::Function>::Cast (module_function)->Call (exports, 2 , jsArgv);
498+ v8::Local<v8::Function>::Cast (module_function)->Call (exports, 3 , jsArgv);
486499 }
487500
488501 // Remove this module and path from the stack
489502 c->modules_stack .pop_back ();
490- c->modules_base .pop_back ();
491503
492504 efree (normalised_path);
493505
@@ -532,8 +544,8 @@ void v8js_register_methods(v8::Local<v8::ObjectTemplate> global, v8js_ctx *c) /*
532544 global->Set (V8JS_SYM (" print" ), v8::FunctionTemplate::New (isolate, V8JS_MN (print)), v8::ReadOnly);
533545 global->Set (V8JS_SYM (" var_dump" ), v8::FunctionTemplate::New (isolate, V8JS_MN (var_dump)), v8::ReadOnly);
534546
535- c-> modules_base . push_back (" " );
536- global->Set (V8JS_SYM (" require" ), v8::FunctionTemplate::New (isolate, V8JS_MN (require), v8::External::New (isolate, c) ), v8::ReadOnly);
547+ v8::Local<v8::String> base_path = V8JS_STRL (" " , 0 );
548+ global->Set (V8JS_SYM (" require" ), v8::FunctionTemplate::New (isolate, V8JS_MN (require), base_path ), v8::ReadOnly);
537549}
538550/* }}} */
539551
0 commit comments