@@ -27,6 +27,7 @@ using v8::ScriptOrigin;
2727using v8::Set;
2828using v8::SideEffectType;
2929using v8::String;
30+ using v8::Undefined;
3031using v8::Value;
3132
3233BuiltinLoader BuiltinLoader::instance_;
@@ -390,8 +391,12 @@ MaybeLocal<Function> BuiltinLoader::LookupAndCompile(
390391 FIXED_ONE_BYTE_STRING (isolate, " exports" ),
391392 FIXED_ONE_BYTE_STRING (isolate, " primordials" ),
392393 };
393- } else if (strncmp (id, " internal/main/" , strlen (" internal/main/" )) == 0 ) {
394- // internal/main/*: process, require, internalBinding, primordials
394+ } else if (strncmp (id, " internal/main/" , strlen (" internal/main/" )) == 0 ||
395+ strncmp (id,
396+ " internal/bootstrap/" ,
397+ strlen (" internal/bootstrap/" )) == 0 ) {
398+ // internal/main/*, internal/bootstrap/*: process, require,
399+ // internalBinding, primordials
395400 parameters = {
396401 FIXED_ONE_BYTE_STRING (isolate, " process" ),
397402 FIXED_ONE_BYTE_STRING (isolate, " require" ),
@@ -404,16 +409,6 @@ MaybeLocal<Function> BuiltinLoader::LookupAndCompile(
404409 FIXED_ONE_BYTE_STRING (isolate, " process" ),
405410 FIXED_ONE_BYTE_STRING (isolate, " require" ),
406411 };
407- } else if (strncmp (id,
408- " internal/bootstrap/" ,
409- strlen (" internal/bootstrap/" )) == 0 ) {
410- // internal/bootstrap/*: process, require, internalBinding, primordials
411- parameters = {
412- FIXED_ONE_BYTE_STRING (isolate, " process" ),
413- FIXED_ONE_BYTE_STRING (isolate, " require" ),
414- FIXED_ONE_BYTE_STRING (isolate, " internalBinding" ),
415- FIXED_ONE_BYTE_STRING (isolate, " primordials" ),
416- };
417412 } else {
418413 // others: exports, require, module, process, internalBinding, primordials
419414 parameters = {
@@ -434,6 +429,76 @@ MaybeLocal<Function> BuiltinLoader::LookupAndCompile(
434429 return maybe;
435430}
436431
432+ MaybeLocal<Value> BuiltinLoader::CompileAndCall (Local<Context> context,
433+ const char * id,
434+ Realm* realm) {
435+ Isolate* isolate = context->GetIsolate ();
436+ // Arguments must match the parameters specified in
437+ // BuiltinLoader::LookupAndCompile().
438+ std::vector<Local<Value>> arguments;
439+ // Detects parameters of the scripts based on module ids.
440+ // internal/bootstrap/loaders: process, getLinkedBinding,
441+ // getInternalBinding, primordials
442+ if (strcmp (id, " internal/bootstrap/loaders" ) == 0 ) {
443+ Local<Value> get_linked_binding;
444+ Local<Value> get_internal_binding;
445+ if (!NewFunctionTemplate (isolate, binding::GetLinkedBinding)
446+ ->GetFunction (context)
447+ .ToLocal (&get_linked_binding) ||
448+ !NewFunctionTemplate (isolate, binding::GetInternalBinding)
449+ ->GetFunction (context)
450+ .ToLocal (&get_internal_binding)) {
451+ return MaybeLocal<Value>();
452+ }
453+ arguments = {realm->process_object (),
454+ get_linked_binding,
455+ get_internal_binding,
456+ realm->primordials ()};
457+ } else if (strncmp (id, " internal/main/" , strlen (" internal/main/" )) == 0 ||
458+ strncmp (id,
459+ " internal/bootstrap/" ,
460+ strlen (" internal/bootstrap/" )) == 0 ) {
461+ // internal/main/*, internal/bootstrap/*: process, require,
462+ // internalBinding, primordials
463+ arguments = {realm->process_object (),
464+ realm->builtin_module_require (),
465+ realm->internal_binding_loader (),
466+ realm->primordials ()};
467+ } else if (strncmp (id, " embedder_main_" , strlen (" embedder_main_" )) == 0 ) {
468+ // Synthetic embedder main scripts from LoadEnvironment(): process, require
469+ arguments = {
470+ realm->process_object (),
471+ realm->builtin_module_require (),
472+ };
473+ } else {
474+ // This should be invoked with the other CompileAndCall() methods, as
475+ // we are unable to generate the arguments.
476+ // Currently there are two cases:
477+ // internal/per_context/*: the arguments are generated in
478+ // InitializePrimordials()
479+ // all the other cases: the arguments are generated in the JS-land loader.
480+ UNREACHABLE ();
481+ }
482+ return CompileAndCall (
483+ context, id, arguments.size (), arguments.data (), realm->env ());
484+ }
485+
486+ MaybeLocal<Value> BuiltinLoader::CompileAndCall (Local<Context> context,
487+ const char * id,
488+ int argc,
489+ Local<Value> argv[],
490+ Environment* optional_env) {
491+ // Arguments must match the parameters specified in
492+ // BuiltinLoader::LookupAndCompile().
493+ MaybeLocal<Function> maybe_fn = LookupAndCompile (context, id, optional_env);
494+ Local<Function> fn;
495+ if (!maybe_fn.ToLocal (&fn)) {
496+ return MaybeLocal<Value>();
497+ }
498+ Local<Value> undefined = Undefined (context->GetIsolate ());
499+ return fn->Call (context, undefined, argc, argv);
500+ }
501+
437502bool BuiltinLoader::CompileAllBuiltins (Local<Context> context) {
438503 BuiltinLoader* loader = GetInstance ();
439504 std::vector<std::string> ids = loader->GetBuiltinIds ();
0 commit comments