@@ -26,6 +26,7 @@ using v8::ScriptOrigin;
2626using v8::Set;
2727using v8::SideEffectType;
2828using v8::String;
29+ using v8::Undefined;
2930using v8::Value;
3031
3132BuiltinLoader BuiltinLoader::instance_;
@@ -352,8 +353,12 @@ MaybeLocal<Function> BuiltinLoader::LookupAndCompile(
352353 FIXED_ONE_BYTE_STRING (isolate, " exports" ),
353354 FIXED_ONE_BYTE_STRING (isolate, " primordials" ),
354355 };
355- } else if (strncmp (id, " internal/main/" , strlen (" internal/main/" )) == 0 ) {
356- // internal/main/*: process, require, internalBinding, primordials
356+ } else if (strncmp (id, " internal/main/" , strlen (" internal/main/" )) == 0 ||
357+ strncmp (id,
358+ " internal/bootstrap/" ,
359+ strlen (" internal/bootstrap/" )) == 0 ) {
360+ // internal/main/*, internal/bootstrap/*: process, require,
361+ // internalBinding, primordials
357362 parameters = {
358363 FIXED_ONE_BYTE_STRING (isolate, " process" ),
359364 FIXED_ONE_BYTE_STRING (isolate, " require" ),
@@ -366,16 +371,6 @@ MaybeLocal<Function> BuiltinLoader::LookupAndCompile(
366371 FIXED_ONE_BYTE_STRING (isolate, " process" ),
367372 FIXED_ONE_BYTE_STRING (isolate, " require" ),
368373 };
369- } else if (strncmp (id,
370- " internal/bootstrap/" ,
371- strlen (" internal/bootstrap/" )) == 0 ) {
372- // internal/bootstrap/*: process, require, internalBinding, primordials
373- parameters = {
374- FIXED_ONE_BYTE_STRING (isolate, " process" ),
375- FIXED_ONE_BYTE_STRING (isolate, " require" ),
376- FIXED_ONE_BYTE_STRING (isolate, " internalBinding" ),
377- FIXED_ONE_BYTE_STRING (isolate, " primordials" ),
378- };
379374 } else {
380375 // others: exports, require, module, process, internalBinding, primordials
381376 parameters = {
@@ -396,6 +391,76 @@ MaybeLocal<Function> BuiltinLoader::LookupAndCompile(
396391 return maybe;
397392}
398393
394+ MaybeLocal<Value> BuiltinLoader::CompileAndCall (Local<Context> context,
395+ const char * id,
396+ Realm* realm) {
397+ Isolate* isolate = context->GetIsolate ();
398+ // Arguments must match the parameters specified in
399+ // BuiltinLoader::LookupAndCompile().
400+ std::vector<Local<Value>> arguments;
401+ // Detects parameters of the scripts based on module ids.
402+ // internal/bootstrap/loaders: process, getLinkedBinding,
403+ // getInternalBinding, primordials
404+ if (strcmp (id, " internal/bootstrap/loaders" ) == 0 ) {
405+ Local<Value> get_linked_binding;
406+ Local<Value> get_internal_binding;
407+ if (!NewFunctionTemplate (isolate, binding::GetLinkedBinding)
408+ ->GetFunction (context)
409+ .ToLocal (&get_linked_binding) ||
410+ !NewFunctionTemplate (isolate, binding::GetInternalBinding)
411+ ->GetFunction (context)
412+ .ToLocal (&get_internal_binding)) {
413+ return MaybeLocal<Value>();
414+ }
415+ arguments = {realm->process_object (),
416+ get_linked_binding,
417+ get_internal_binding,
418+ realm->primordials ()};
419+ } else if (strncmp (id, " internal/main/" , strlen (" internal/main/" )) == 0 ||
420+ strncmp (id,
421+ " internal/bootstrap/" ,
422+ strlen (" internal/bootstrap/" )) == 0 ) {
423+ // internal/main/*, internal/bootstrap/*: process, require,
424+ // internalBinding, primordials
425+ arguments = {realm->process_object (),
426+ realm->builtin_module_require (),
427+ realm->internal_binding_loader (),
428+ realm->primordials ()};
429+ } else if (strncmp (id, " embedder_main_" , strlen (" embedder_main_" )) == 0 ) {
430+ // Synthetic embedder main scripts from LoadEnvironment(): process, require
431+ arguments = {
432+ realm->process_object (),
433+ realm->builtin_module_require (),
434+ };
435+ } else {
436+ // This should be invoked with the other CompileAndCall() methods, as
437+ // we are unable to generate the arguments.
438+ // Currently there are two cases:
439+ // internal/per_context/*: the arguments are generated in
440+ // InitializePrimordials()
441+ // all the other cases: the arguments are generated in the JS-land loader.
442+ UNREACHABLE ();
443+ }
444+ return CompileAndCall (
445+ context, id, arguments.size (), arguments.data (), realm->env ());
446+ }
447+
448+ MaybeLocal<Value> BuiltinLoader::CompileAndCall (Local<Context> context,
449+ const char * id,
450+ int argc,
451+ Local<Value> argv[],
452+ Environment* optional_env) {
453+ // Arguments must match the parameters specified in
454+ // BuiltinLoader::LookupAndCompile().
455+ MaybeLocal<Function> maybe_fn = LookupAndCompile (context, id, optional_env);
456+ Local<Function> fn;
457+ if (!maybe_fn.ToLocal (&fn)) {
458+ return MaybeLocal<Value>();
459+ }
460+ Local<Value> undefined = Undefined (context->GetIsolate ());
461+ return fn->Call (context, undefined, argc, argv);
462+ }
463+
399464bool BuiltinLoader::CompileAllBuiltins (Local<Context> context) {
400465 BuiltinLoader* loader = GetInstance ();
401466 std::vector<std::string> ids = loader->GetBuiltinIds ();
0 commit comments