Skip to content

Commit

Permalink
Address issue with dynamic bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Feb 27, 2025
1 parent 9a88837 commit f07505e
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/workerd/io/worker.c++
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ struct Worker::Impl {
kj::Maybe<jsg::JsContext<api::ServiceWorkerGlobalScope>> context;

// The environment blob to pass to handlers.
kj::Maybe<jsg::Value> env;
kj::Maybe<jsg::Value> ctxExports;

// Note: The default export is given the string name "default", because that's what V8 tells us,
Expand Down Expand Up @@ -1628,7 +1629,9 @@ Worker::Worker(kj::Own<const Script> scriptParam,
if (script->isModular()) {
// Use `env` variable.
bindingsScope = v8::Object::New(lock.v8Isolate);
lock.setWorkerEnv(lock.v8Ref(bindingsScope.As<v8::Value>()));
if (!FeatureFlags::get(js).getDisableImportableEnv()) {
lock.setWorkerEnv(lock.v8Ref(bindingsScope.As<v8::Value>()));
}
} else {
// Use global-scope bindings.
bindingsScope = context->Global();
Expand Down Expand Up @@ -1658,6 +1661,7 @@ Worker::Worker(kj::Own<const Script> scriptParam,
KJ_CASE_ONEOF(mainModule, kj::Path) {
KJ_IF_SOME(ns,
tryResolveMainModule(lock, mainModule, *jsContext, *script, limitErrorOrTime)) {
impl->env = lock.v8Ref(bindingsScope.As<v8::Value>());
impl->ctxExports = lock.v8Ref(ctxExports.As<v8::Value>());

auto& api = script->isolate->getApi();
Expand Down Expand Up @@ -1986,7 +1990,7 @@ kj::Maybe<kj::Own<api::ExportedHandler>> Worker::Lock::getExportedHandler(
auto handler = kj::heap(cls(js,
jsg::alloc<api::ExecutionContext>(js,
jsg::JsValue(KJ_ASSERT_NONNULL(worker.impl->ctxExports).getHandle(js)), props.toJs(js)),
KJ_ASSERT_NONNULL(js.getWorkerEnv())));
KJ_ASSERT_NONNULL(worker.impl->env).addRef(js)));

// HACK: We set handler.env and handler.ctx to undefined because we already passed the real
// env and ctx into the constructor, and we want the handler methods to act like they take
Expand Down Expand Up @@ -3486,7 +3490,7 @@ kj::Promise<void> Worker::Actor::ensureConstructedImpl(IoContext& context, Actor
jsg::JsRef<jsg::JsValue>(
js, KJ_ASSERT_NONNULL(lock.getWorker().impl->ctxExports).addRef(js)),
kj::mv(storage), kj::mv(impl->container), containerRunning),
KJ_ASSERT_NONNULL(js.getWorkerEnv()));
KJ_ASSERT_NONNULL(lock.getWorker().impl->env).addRef(js));

// HACK: We set handler.env to undefined because we already passed the real env into the
// constructor, and we want the handler methods to act like they take just one parameter.
Expand Down

0 comments on commit f07505e

Please sign in to comment.