Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export setupContext publically to be used by additional contexts. #3067

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 31 additions & 32 deletions src/workerd/io/worker.c++
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,6 @@ private:
kj::MutexGuarded<State> state;
};

// Defined later in this file.
void setWebAssemblyModuleHasInstance(jsg::Lock& lock, v8::Local<v8::Context> context);

static thread_local const Worker::Api* currentApi = nullptr;

const Worker::Api& Worker::Api::current() {
Expand Down Expand Up @@ -599,40 +596,12 @@ struct Worker::Isolate::Impl {
KJ_DISALLOW_COPY_AND_MOVE(Lock);

void setupContext(v8::Local<v8::Context> context) {
// Set WebAssembly.Module @@HasInstance
setWebAssemblyModuleHasInstance(*lock, context);

// The V8Inspector implements the `console` object.
KJ_IF_SOME(i, impl.inspector) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only real change is that Set WebAssembly.Module @@HasInstance is now only set after alerting the inspector of a new context.
This shouldn't have any actual effect in practice.

i.get()->contextCreated(
v8_inspector::V8ContextInfo(context, 1, jsg::toInspectorStringView("Worker")));
}

// We replace the default V8 console.log(), etc. methods, to give the worker access to
// logged content, and log formatted values to stdout/stderr locally.
auto global = context->Global();
auto consoleStr = jsg::v8StrIntern(lock->v8Isolate, "console");
auto console = jsg::check(global->Get(context, consoleStr)).As<v8::Object>();
auto mode = consoleMode;

auto setHandler = [&](const char* method, LogLevel level) {
auto methodStr = jsg::v8StrIntern(lock->v8Isolate, method);
v8::Global<v8::Function> original(
lock->v8Isolate, jsg::check(console->Get(context, methodStr)).As<v8::Function>());

auto f = lock->wrapSimpleFunction(context,
[mode, level, original = kj::mv(original)](
jsg::Lock& js, const v8::FunctionCallbackInfo<v8::Value>& info) {
handleLog(js, mode, level, original, info);
});
jsg::check(console->Set(context, methodStr, f));
};

setHandler("debug", LogLevel::DEBUG_);
setHandler("error", LogLevel::ERROR);
setHandler("info", LogLevel::INFO);
setHandler("log", LogLevel::LOG);
setHandler("warn", LogLevel::WARN);
Worker::setupContext(*lock, context, consoleMode);
}

void disposeContext(jsg::JsContext<api::ServiceWorkerGlobalScope> context) {
Expand Down Expand Up @@ -1471,6 +1440,36 @@ void setWebAssemblyModuleHasInstance(jsg::Lock& lock, v8::Local<v8::Context> con
module->DefineOwnProperty(context, v8::Symbol::GetHasInstance(lock.v8Isolate), function));
}

void Worker::setupContext(
jsg::Lock& lock, v8::Local<v8::Context> context, Worker::ConsoleMode consoleMode) {
// Set WebAssembly.Module @@HasInstance
setWebAssemblyModuleHasInstance(lock, context);

// We replace the default V8 console.log(), etc. methods, to give the worker access to
// logged content, and log formatted values to stdout/stderr locally.
auto global = context->Global();
auto consoleStr = jsg::v8StrIntern(lock.v8Isolate, "console");
auto console = jsg::check(global->Get(context, consoleStr)).As<v8::Object>();

auto setHandler = [&](const char* method, LogLevel level) {
auto methodStr = jsg::v8StrIntern(lock.v8Isolate, method);
v8::Global<v8::Function> original(
lock.v8Isolate, jsg::check(console->Get(context, methodStr)).As<v8::Function>());

auto f = lock.wrapSimpleFunction(context,
[consoleMode, level, original = kj::mv(original)](
jsg::Lock& js, const v8::FunctionCallbackInfo<v8::Value>& info) {
handleLog(js, consoleMode, level, original, info);
});
jsg::check(console->Set(context, methodStr, f));
};

setHandler("debug", LogLevel::DEBUG_);
setHandler("error", LogLevel::ERROR);
setHandler("info", LogLevel::INFO);
setHandler("log", LogLevel::LOG);
setHandler("warn", LogLevel::WARN);
}
// =======================================================================================

namespace {
Expand Down
3 changes: 3 additions & 0 deletions src/workerd/io/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ class Worker: public kj::AtomicRefcounted {
void setConnectOverride(kj::String networkAddress, ConnectFn connectFn);
kj::Maybe<ConnectFn&> getConnectOverride(kj::StringPtr networkAddress);

static void setupContext(
jsg::Lock& lock, v8::Local<v8::Context> context, Worker::ConsoleMode consoleMode);

private:
kj::Own<const Script> script;

Expand Down