Skip to content

Commit c922458

Browse files
camillobruniCommit Bot
authored andcommitted
Reland "[d8] Add d8 global variable"
This is a reland of 6798619 Original change's description: > [d8] Add d8 global variable > > - Add a a "d8" global variable where d8 can provide helpers. > This in in preparation of adding d8.log for testing our log parsers > written in JavaScript. > > - Separate d8 helper creation into individual functions. > > Bug: v8:10668 > Change-Id: I84e434452463afb93ae403f890d8841b20b00703 > Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2400990 > Reviewed-by: Toon Verwaest <[email protected]> > Commit-Queue: Camillo Bruni <[email protected]> > Cr-Commit-Position: refs/heads/master@{#69801} Tbr: [email protected] Bug: v8:10668 Change-Id: If3256ec4e11f01ef1dc5c2e61fa33ed6d7a6aee3 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2409274 Reviewed-by: Camillo Bruni <[email protected]> Commit-Queue: Camillo Bruni <[email protected]> Cr-Commit-Position: refs/heads/master@{#69867}
1 parent fa32bc0 commit c922458

File tree

2 files changed

+103
-67
lines changed

2 files changed

+103
-67
lines changed

src/d8/d8.cc

Lines changed: 94 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,6 +2071,11 @@ Local<String> Shell::Stringify(Isolate* isolate, Local<Value> value) {
20712071

20722072
Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
20732073
Local<ObjectTemplate> global_template = ObjectTemplate::New(isolate);
2074+
global_template->Set(Symbol::GetToStringTag(isolate),
2075+
String::NewFromUtf8Literal(isolate, "global"));
2076+
global_template->Set(isolate, "version",
2077+
FunctionTemplate::New(isolate, Version));
2078+
20742079
global_template->Set(isolate, "print", FunctionTemplate::New(isolate, Print));
20752080
global_template->Set(isolate, "printErr",
20762081
FunctionTemplate::New(isolate, PrintErr));
@@ -2089,8 +2094,79 @@ Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
20892094
if (!options.omit_quit) {
20902095
global_template->Set(isolate, "quit", FunctionTemplate::New(isolate, Quit));
20912096
}
2097+
global_template->Set(isolate, "testRunner",
2098+
Shell::CreateTestRunnerTemplate(isolate));
2099+
global_template->Set(isolate, "Realm", Shell::CreateRealmTemplate(isolate));
2100+
global_template->Set(isolate, "performance",
2101+
Shell::CreatePerformanceTemplate(isolate));
2102+
global_template->Set(isolate, "Worker", Shell::CreateWorkerTemplate(isolate));
2103+
global_template->Set(isolate, "os", Shell::CreateOSTemplate(isolate));
2104+
global_template->Set(isolate, "d8", Shell::CreateD8Template(isolate));
2105+
2106+
#ifdef V8_FUZZILLI
2107+
global_template->Set(
2108+
String::NewFromUtf8(isolate, "fuzzilli", NewStringType::kNormal)
2109+
.ToLocalChecked(),
2110+
FunctionTemplate::New(isolate, Fuzzilli), PropertyAttribute::DontEnum);
2111+
#endif // V8_FUZZILLI
2112+
2113+
if (i::FLAG_expose_async_hooks) {
2114+
global_template->Set(isolate, "async_hooks",
2115+
Shell::CreateAsyncHookTemplate(isolate));
2116+
}
2117+
2118+
return global_template;
2119+
}
2120+
2121+
Local<ObjectTemplate> Shell::CreateOSTemplate(Isolate* isolate) {
2122+
Local<ObjectTemplate> os_template = ObjectTemplate::New(isolate);
2123+
AddOSMethods(isolate, os_template);
2124+
return os_template;
2125+
}
2126+
2127+
Local<FunctionTemplate> Shell::CreateWorkerTemplate(Isolate* isolate) {
2128+
Local<FunctionTemplate> worker_fun_template =
2129+
FunctionTemplate::New(isolate, WorkerNew);
2130+
Local<Signature> worker_signature =
2131+
Signature::New(isolate, worker_fun_template);
2132+
worker_fun_template->SetClassName(
2133+
String::NewFromUtf8Literal(isolate, "Worker"));
2134+
worker_fun_template->ReadOnlyPrototype();
2135+
worker_fun_template->PrototypeTemplate()->Set(
2136+
isolate, "terminate",
2137+
FunctionTemplate::New(isolate, WorkerTerminate, Local<Value>(),
2138+
worker_signature));
2139+
worker_fun_template->PrototypeTemplate()->Set(
2140+
isolate, "terminateAndWait",
2141+
FunctionTemplate::New(isolate, WorkerTerminateAndWait, Local<Value>(),
2142+
worker_signature));
2143+
worker_fun_template->PrototypeTemplate()->Set(
2144+
isolate, "postMessage",
2145+
FunctionTemplate::New(isolate, WorkerPostMessage, Local<Value>(),
2146+
worker_signature));
2147+
worker_fun_template->PrototypeTemplate()->Set(
2148+
isolate, "getMessage",
2149+
FunctionTemplate::New(isolate, WorkerGetMessage, Local<Value>(),
2150+
worker_signature));
2151+
worker_fun_template->InstanceTemplate()->SetInternalFieldCount(1);
2152+
return worker_fun_template;
2153+
}
2154+
2155+
Local<ObjectTemplate> Shell::CreateAsyncHookTemplate(Isolate* isolate) {
2156+
Local<ObjectTemplate> async_hooks_templ = ObjectTemplate::New(isolate);
2157+
async_hooks_templ->Set(isolate, "createHook",
2158+
FunctionTemplate::New(isolate, AsyncHooksCreateHook));
2159+
async_hooks_templ->Set(
2160+
isolate, "executionAsyncId",
2161+
FunctionTemplate::New(isolate, AsyncHooksExecutionAsyncId));
2162+
async_hooks_templ->Set(
2163+
isolate, "triggerAsyncId",
2164+
FunctionTemplate::New(isolate, AsyncHooksTriggerAsyncId));
2165+
return async_hooks_templ;
2166+
}
2167+
2168+
Local<ObjectTemplate> Shell::CreateTestRunnerTemplate(Isolate* isolate) {
20922169
Local<ObjectTemplate> test_template = ObjectTemplate::New(isolate);
2093-
global_template->Set(isolate, "testRunner", test_template);
20942170
test_template->Set(isolate, "notifyDone",
20952171
FunctionTemplate::New(isolate, NotifyDone));
20962172
test_template->Set(isolate, "waitUntilDone",
@@ -2099,13 +2175,20 @@ Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
20992175
// installed on the global object can be hidden with the --omit-quit flag
21002176
// (e.g. on asan bots).
21012177
test_template->Set(isolate, "quit", FunctionTemplate::New(isolate, Quit));
2178+
return test_template;
2179+
}
21022180

2103-
global_template->Set(isolate, "version",
2104-
FunctionTemplate::New(isolate, Version));
2105-
global_template->Set(Symbol::GetToStringTag(isolate),
2106-
String::NewFromUtf8Literal(isolate, "global"));
2181+
Local<ObjectTemplate> Shell::CreatePerformanceTemplate(Isolate* isolate) {
2182+
Local<ObjectTemplate> performance_template = ObjectTemplate::New(isolate);
2183+
performance_template->Set(isolate, "now",
2184+
FunctionTemplate::New(isolate, PerformanceNow));
2185+
performance_template->Set(
2186+
isolate, "measureMemory",
2187+
FunctionTemplate::New(isolate, PerformanceMeasureMemory));
2188+
return performance_template;
2189+
}
21072190

2108-
// Bind the Realm object.
2191+
Local<ObjectTemplate> Shell::CreateRealmTemplate(Isolate* isolate) {
21092192
Local<ObjectTemplate> realm_template = ObjectTemplate::New(isolate);
21102193
realm_template->Set(isolate, "current",
21112194
FunctionTemplate::New(isolate, RealmCurrent));
@@ -2130,68 +2213,12 @@ Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
21302213
FunctionTemplate::New(isolate, RealmEval));
21312214
realm_template->SetAccessor(String::NewFromUtf8Literal(isolate, "shared"),
21322215
RealmSharedGet, RealmSharedSet);
2133-
global_template->Set(isolate, "Realm", realm_template);
2134-
2135-
Local<ObjectTemplate> performance_template = ObjectTemplate::New(isolate);
2136-
performance_template->Set(isolate, "now",
2137-
FunctionTemplate::New(isolate, PerformanceNow));
2138-
performance_template->Set(
2139-
isolate, "measureMemory",
2140-
FunctionTemplate::New(isolate, PerformanceMeasureMemory));
2141-
global_template->Set(isolate, "performance", performance_template);
2142-
2143-
Local<FunctionTemplate> worker_fun_template =
2144-
FunctionTemplate::New(isolate, WorkerNew);
2145-
Local<Signature> worker_signature =
2146-
Signature::New(isolate, worker_fun_template);
2147-
worker_fun_template->SetClassName(
2148-
String::NewFromUtf8Literal(isolate, "Worker"));
2149-
worker_fun_template->ReadOnlyPrototype();
2150-
worker_fun_template->PrototypeTemplate()->Set(
2151-
isolate, "terminate",
2152-
FunctionTemplate::New(isolate, WorkerTerminate, Local<Value>(),
2153-
worker_signature));
2154-
worker_fun_template->PrototypeTemplate()->Set(
2155-
isolate, "terminateAndWait",
2156-
FunctionTemplate::New(isolate, WorkerTerminateAndWait, Local<Value>(),
2157-
worker_signature));
2158-
worker_fun_template->PrototypeTemplate()->Set(
2159-
isolate, "postMessage",
2160-
FunctionTemplate::New(isolate, WorkerPostMessage, Local<Value>(),
2161-
worker_signature));
2162-
worker_fun_template->PrototypeTemplate()->Set(
2163-
isolate, "getMessage",
2164-
FunctionTemplate::New(isolate, WorkerGetMessage, Local<Value>(),
2165-
worker_signature));
2166-
worker_fun_template->InstanceTemplate()->SetInternalFieldCount(1);
2167-
global_template->Set(isolate, "Worker", worker_fun_template);
2168-
2169-
Local<ObjectTemplate> os_templ = ObjectTemplate::New(isolate);
2170-
AddOSMethods(isolate, os_templ);
2171-
global_template->Set(isolate, "os", os_templ);
2172-
2173-
#ifdef V8_FUZZILLI
2174-
global_template->Set(
2175-
String::NewFromUtf8(isolate, "fuzzilli", NewStringType::kNormal)
2176-
.ToLocalChecked(),
2177-
FunctionTemplate::New(isolate, Fuzzilli), PropertyAttribute::DontEnum);
2178-
#endif // V8_FUZZILLI
2179-
2180-
if (i::FLAG_expose_async_hooks) {
2181-
Local<ObjectTemplate> async_hooks_templ = ObjectTemplate::New(isolate);
2182-
async_hooks_templ->Set(
2183-
isolate, "createHook",
2184-
FunctionTemplate::New(isolate, AsyncHooksCreateHook));
2185-
async_hooks_templ->Set(
2186-
isolate, "executionAsyncId",
2187-
FunctionTemplate::New(isolate, AsyncHooksExecutionAsyncId));
2188-
async_hooks_templ->Set(
2189-
isolate, "triggerAsyncId",
2190-
FunctionTemplate::New(isolate, AsyncHooksTriggerAsyncId));
2191-
global_template->Set(isolate, "async_hooks", async_hooks_templ);
2192-
}
2216+
return realm_template;
2217+
}
21932218

2194-
return global_template;
2219+
Local<ObjectTemplate> Shell::CreateD8Template(Isolate* isolate) {
2220+
Local<ObjectTemplate> d8_template = ObjectTemplate::New(isolate);
2221+
return d8_template;
21952222
}
21962223

21972224
static void PrintMessageCallback(Local<Message> message, Local<Value> error) {

src/d8/d8.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,16 @@ class Shell : public i::AllStatic {
539539
static Local<String> Stringify(Isolate* isolate, Local<Value> value);
540540
static void RunShell(Isolate* isolate);
541541
static bool SetOptions(int argc, char* argv[]);
542+
542543
static Local<ObjectTemplate> CreateGlobalTemplate(Isolate* isolate);
544+
static Local<ObjectTemplate> CreateOSTemplate(Isolate* isolate);
545+
static Local<FunctionTemplate> CreateWorkerTemplate(Isolate* isolate);
546+
static Local<ObjectTemplate> CreateAsyncHookTemplate(Isolate* isolate);
547+
static Local<ObjectTemplate> CreateTestRunnerTemplate(Isolate* isolate);
548+
static Local<ObjectTemplate> CreatePerformanceTemplate(Isolate* isolate);
549+
static Local<ObjectTemplate> CreateRealmTemplate(Isolate* isolate);
550+
static Local<ObjectTemplate> CreateD8Template(Isolate* isolate);
551+
543552
static MaybeLocal<Context> CreateRealm(
544553
const v8::FunctionCallbackInfo<v8::Value>& args, int index,
545554
v8::MaybeLocal<Value> global_object);

0 commit comments

Comments
 (0)