Skip to content

Commit c26f53f

Browse files
committed
fix: Pass ctx.props to default handlers
1 parent a0b8d14 commit c26f53f

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/workerd/api/global-scope.h

+29
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,35 @@ struct ExportedHandler {
382382
jsg::Optional<jsg::Ref<ExecutionContext>> getCtx() {
383383
return ctx.map([&](jsg::Ref<ExecutionContext>& p) { return p.addRef(); });
384384
}
385+
386+
template <typename T>
387+
static jsg::LenientOptional<T> ADDREF(jsg::LenientOptional<T>& function, jsg::Lock& js) {
388+
return function.map([&](T& a) { return a.addRef(js); });
389+
}
390+
391+
#define CLONE(x) \
392+
.x { \
393+
ADDREF(x, js) \
394+
}
395+
396+
ExportedHandler clone(jsg::Lock& js) {
397+
return ExportedHandler{
398+
CLONE(fetch),
399+
CLONE(tail),
400+
CLONE(trace),
401+
CLONE(tailStream),
402+
CLONE(scheduled),
403+
CLONE(alarm),
404+
CLONE(test),
405+
CLONE(webSocketMessage),
406+
CLONE(webSocketClose),
407+
CLONE(webSocketError),
408+
.self{js.v8Isolate, v8::Object::New(js.v8Isolate)},
409+
.env{env.addRef(js)},
410+
.ctx{getCtx()},
411+
.missingSuperclass = missingSuperclass,
412+
};
413+
}
385414
};
386415

387416
// An approximation of Node.js setImmediate `Immediate` object.

src/workerd/io/compatibility-date.capnp

+5
Original file line numberDiff line numberDiff line change
@@ -692,4 +692,9 @@ struct CompatibilityFlags @0x8f8c1b68151b6cef {
692692
$experimental
693693
$neededByFl;
694694
# Enables cache settings specified request in fetch api cf object to override cache rules. (only for user owned or grey-clouded sites)
695+
696+
uniqueCtxPerInvocation @73: Bool
697+
$compatEnableFlag("unique_ctx_per_invocation")
698+
$compatDisableFlag("nonclass_entrypoint_reuses_ctx_accros_invocations")
699+
$compatEnableDate("2025-02-24");
695700
}

src/workerd/io/worker.c++

+9-1
Original file line numberDiff line numberDiff line change
@@ -1971,7 +1971,15 @@ kj::Maybe<kj::Own<api::ExportedHandler>> Worker::Lock::getExportedHandler(
19711971

19721972
kj::StringPtr n = name.orDefault("default"_kj);
19731973
KJ_IF_SOME(h, worker.impl->namedHandlers.find(n)) {
1974-
return fakeOwn(h);
1974+
kj::Own<api::ExportedHandler> ret = fakeOwn(h);
1975+
jsg::Lock& js = *this;
1976+
if (FeatureFlags::get(js).getUniqueCtxPerInvocation()) {
1977+
api::ExportedHandler constructedHandler = h.clone(js);
1978+
constructedHandler.ctx = jsg::alloc<api::ExecutionContext>(js, props.toJs(js));
1979+
kj::Own<api::ExportedHandler> handlerPtr = kj::heap(kj::mv(constructedHandler));
1980+
return handlerPtr;
1981+
}
1982+
return ret;
19751983
} else KJ_IF_SOME(cls, worker.impl->statelessClasses.find(n)) {
19761984
jsg::Lock& js = *this;
19771985
auto handler = kj::heap(cls(js, jsg::alloc<api::ExecutionContext>(js, props.toJs(js)),

0 commit comments

Comments
 (0)