diff --git a/src/d8/d8.cc b/src/d8/d8.cc index e76eb23a419b..216b3f41057b 100644 --- a/src/d8/d8.cc +++ b/src/d8/d8.cc @@ -2071,6 +2071,11 @@ Local Shell::Stringify(Isolate* isolate, Local value) { Local Shell::CreateGlobalTemplate(Isolate* isolate) { Local global_template = ObjectTemplate::New(isolate); + global_template->Set(Symbol::GetToStringTag(isolate), + String::NewFromUtf8Literal(isolate, "global")); + global_template->Set(isolate, "version", + FunctionTemplate::New(isolate, Version)); + global_template->Set(isolate, "print", FunctionTemplate::New(isolate, Print)); global_template->Set(isolate, "printErr", FunctionTemplate::New(isolate, PrintErr)); @@ -2089,8 +2094,79 @@ Local Shell::CreateGlobalTemplate(Isolate* isolate) { if (!options.omit_quit) { global_template->Set(isolate, "quit", FunctionTemplate::New(isolate, Quit)); } + global_template->Set(isolate, "testRunner", + Shell::CreateTestRunnerTemplate(isolate)); + global_template->Set(isolate, "Realm", Shell::CreateRealmTemplate(isolate)); + global_template->Set(isolate, "performance", + Shell::CreatePerformanceTemplate(isolate)); + global_template->Set(isolate, "Worker", Shell::CreateWorkerTemplate(isolate)); + global_template->Set(isolate, "os", Shell::CreateOSTemplate(isolate)); + global_template->Set(isolate, "d8", Shell::CreateD8Template(isolate)); + +#ifdef V8_FUZZILLI + global_template->Set( + String::NewFromUtf8(isolate, "fuzzilli", NewStringType::kNormal) + .ToLocalChecked(), + FunctionTemplate::New(isolate, Fuzzilli), PropertyAttribute::DontEnum); +#endif // V8_FUZZILLI + + if (i::FLAG_expose_async_hooks) { + global_template->Set(isolate, "async_hooks", + Shell::CreateAsyncHookTemplate(isolate)); + } + + return global_template; +} + +Local Shell::CreateOSTemplate(Isolate* isolate) { + Local os_template = ObjectTemplate::New(isolate); + AddOSMethods(isolate, os_template); + return os_template; +} + +Local Shell::CreateWorkerTemplate(Isolate* isolate) { + Local worker_fun_template = + FunctionTemplate::New(isolate, WorkerNew); + Local worker_signature = + Signature::New(isolate, worker_fun_template); + worker_fun_template->SetClassName( + String::NewFromUtf8Literal(isolate, "Worker")); + worker_fun_template->ReadOnlyPrototype(); + worker_fun_template->PrototypeTemplate()->Set( + isolate, "terminate", + FunctionTemplate::New(isolate, WorkerTerminate, Local(), + worker_signature)); + worker_fun_template->PrototypeTemplate()->Set( + isolate, "terminateAndWait", + FunctionTemplate::New(isolate, WorkerTerminateAndWait, Local(), + worker_signature)); + worker_fun_template->PrototypeTemplate()->Set( + isolate, "postMessage", + FunctionTemplate::New(isolate, WorkerPostMessage, Local(), + worker_signature)); + worker_fun_template->PrototypeTemplate()->Set( + isolate, "getMessage", + FunctionTemplate::New(isolate, WorkerGetMessage, Local(), + worker_signature)); + worker_fun_template->InstanceTemplate()->SetInternalFieldCount(1); + return worker_fun_template; +} + +Local Shell::CreateAsyncHookTemplate(Isolate* isolate) { + Local async_hooks_templ = ObjectTemplate::New(isolate); + async_hooks_templ->Set(isolate, "createHook", + FunctionTemplate::New(isolate, AsyncHooksCreateHook)); + async_hooks_templ->Set( + isolate, "executionAsyncId", + FunctionTemplate::New(isolate, AsyncHooksExecutionAsyncId)); + async_hooks_templ->Set( + isolate, "triggerAsyncId", + FunctionTemplate::New(isolate, AsyncHooksTriggerAsyncId)); + return async_hooks_templ; +} + +Local Shell::CreateTestRunnerTemplate(Isolate* isolate) { Local test_template = ObjectTemplate::New(isolate); - global_template->Set(isolate, "testRunner", test_template); test_template->Set(isolate, "notifyDone", FunctionTemplate::New(isolate, NotifyDone)); test_template->Set(isolate, "waitUntilDone", @@ -2099,13 +2175,20 @@ Local Shell::CreateGlobalTemplate(Isolate* isolate) { // installed on the global object can be hidden with the --omit-quit flag // (e.g. on asan bots). test_template->Set(isolate, "quit", FunctionTemplate::New(isolate, Quit)); + return test_template; +} - global_template->Set(isolate, "version", - FunctionTemplate::New(isolate, Version)); - global_template->Set(Symbol::GetToStringTag(isolate), - String::NewFromUtf8Literal(isolate, "global")); +Local Shell::CreatePerformanceTemplate(Isolate* isolate) { + Local performance_template = ObjectTemplate::New(isolate); + performance_template->Set(isolate, "now", + FunctionTemplate::New(isolate, PerformanceNow)); + performance_template->Set( + isolate, "measureMemory", + FunctionTemplate::New(isolate, PerformanceMeasureMemory)); + return performance_template; +} - // Bind the Realm object. +Local Shell::CreateRealmTemplate(Isolate* isolate) { Local realm_template = ObjectTemplate::New(isolate); realm_template->Set(isolate, "current", FunctionTemplate::New(isolate, RealmCurrent)); @@ -2130,68 +2213,12 @@ Local Shell::CreateGlobalTemplate(Isolate* isolate) { FunctionTemplate::New(isolate, RealmEval)); realm_template->SetAccessor(String::NewFromUtf8Literal(isolate, "shared"), RealmSharedGet, RealmSharedSet); - global_template->Set(isolate, "Realm", realm_template); - - Local performance_template = ObjectTemplate::New(isolate); - performance_template->Set(isolate, "now", - FunctionTemplate::New(isolate, PerformanceNow)); - performance_template->Set( - isolate, "measureMemory", - FunctionTemplate::New(isolate, PerformanceMeasureMemory)); - global_template->Set(isolate, "performance", performance_template); - - Local worker_fun_template = - FunctionTemplate::New(isolate, WorkerNew); - Local worker_signature = - Signature::New(isolate, worker_fun_template); - worker_fun_template->SetClassName( - String::NewFromUtf8Literal(isolate, "Worker")); - worker_fun_template->ReadOnlyPrototype(); - worker_fun_template->PrototypeTemplate()->Set( - isolate, "terminate", - FunctionTemplate::New(isolate, WorkerTerminate, Local(), - worker_signature)); - worker_fun_template->PrototypeTemplate()->Set( - isolate, "terminateAndWait", - FunctionTemplate::New(isolate, WorkerTerminateAndWait, Local(), - worker_signature)); - worker_fun_template->PrototypeTemplate()->Set( - isolate, "postMessage", - FunctionTemplate::New(isolate, WorkerPostMessage, Local(), - worker_signature)); - worker_fun_template->PrototypeTemplate()->Set( - isolate, "getMessage", - FunctionTemplate::New(isolate, WorkerGetMessage, Local(), - worker_signature)); - worker_fun_template->InstanceTemplate()->SetInternalFieldCount(1); - global_template->Set(isolate, "Worker", worker_fun_template); - - Local os_templ = ObjectTemplate::New(isolate); - AddOSMethods(isolate, os_templ); - global_template->Set(isolate, "os", os_templ); - -#ifdef V8_FUZZILLI - global_template->Set( - String::NewFromUtf8(isolate, "fuzzilli", NewStringType::kNormal) - .ToLocalChecked(), - FunctionTemplate::New(isolate, Fuzzilli), PropertyAttribute::DontEnum); -#endif // V8_FUZZILLI - - if (i::FLAG_expose_async_hooks) { - Local async_hooks_templ = ObjectTemplate::New(isolate); - async_hooks_templ->Set( - isolate, "createHook", - FunctionTemplate::New(isolate, AsyncHooksCreateHook)); - async_hooks_templ->Set( - isolate, "executionAsyncId", - FunctionTemplate::New(isolate, AsyncHooksExecutionAsyncId)); - async_hooks_templ->Set( - isolate, "triggerAsyncId", - FunctionTemplate::New(isolate, AsyncHooksTriggerAsyncId)); - global_template->Set(isolate, "async_hooks", async_hooks_templ); - } + return realm_template; +} - return global_template; +Local Shell::CreateD8Template(Isolate* isolate) { + Local d8_template = ObjectTemplate::New(isolate); + return d8_template; } static void PrintMessageCallback(Local message, Local error) { diff --git a/src/d8/d8.h b/src/d8/d8.h index 6b2c08fd6eac..d5c9d111a8cf 100644 --- a/src/d8/d8.h +++ b/src/d8/d8.h @@ -539,7 +539,16 @@ class Shell : public i::AllStatic { static Local Stringify(Isolate* isolate, Local value); static void RunShell(Isolate* isolate); static bool SetOptions(int argc, char* argv[]); + static Local CreateGlobalTemplate(Isolate* isolate); + static Local CreateOSTemplate(Isolate* isolate); + static Local CreateWorkerTemplate(Isolate* isolate); + static Local CreateAsyncHookTemplate(Isolate* isolate); + static Local CreateTestRunnerTemplate(Isolate* isolate); + static Local CreatePerformanceTemplate(Isolate* isolate); + static Local CreateRealmTemplate(Isolate* isolate); + static Local CreateD8Template(Isolate* isolate); + static MaybeLocal CreateRealm( const v8::FunctionCallbackInfo& args, int index, v8::MaybeLocal global_object);