Skip to content

Commit 6b3917b

Browse files
authored
Remove FetchEvent singleton (#1422)
1 parent 761fac1 commit 6b3917b

4 files changed

Lines changed: 25 additions & 19 deletions

File tree

runtime/fastly/builtins/fetch-event.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,6 @@ namespace {
532532

533533
api::Engine *ENGINE;
534534

535-
PersistentRooted<JSObject *> INSTANCE;
536535
JS::PersistentRootedObjectVector *FETCH_HANDLERS;
537536

538537
void inc_pending_promise_count(JSObject *self) {
@@ -798,6 +797,10 @@ bool response_promise_then_handler(JSContext *cx, JS::HandleObject event, JS::Ha
798797
// very different.)
799798
JS::RootedObject response_obj(cx, &args[0].toObject());
800799

800+
// Store the FetchEvent on the Response so it can be accessed later
801+
JS::SetReservedSlot(response_obj, static_cast<uint32_t>(Response::Slots::FetchEvent),
802+
JS::ObjectValue(*event));
803+
801804
if (Response::is_upstream(response_obj)) {
802805
JS::RootedObject headers(cx, Response::headers(cx, response_obj));
803806
// Calling get_list() transitions to Mode::ContentOnly or Mode::CachedInContent.
@@ -1059,7 +1062,6 @@ JSObject *FetchEvent::create(JSContext *cx) {
10591062
return nullptr;
10601063
}
10611064

1062-
INSTANCE.init(cx, self);
10631065
return self;
10641066
}
10651067

@@ -1085,12 +1087,6 @@ bool FetchEvent::reset(JSContext *cx, JS::HandleObject self) {
10851087
return true;
10861088
}
10871089

1088-
JS::HandleObject FetchEvent::instance() {
1089-
MOZ_ASSERT(INSTANCE);
1090-
MOZ_ASSERT(is_instance(INSTANCE));
1091-
return INSTANCE;
1092-
}
1093-
10941090
bool FetchEvent::is_active(JSObject *self) {
10951091
MOZ_ASSERT(is_instance(self));
10961092
// Note: we also treat the FetchEvent as active if it's in `responseStreaming`

runtime/fastly/builtins/fetch/request-response.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,7 +1754,14 @@ bool RequestOrResponse::body_reader_then_handler(JSContext *cx, JS::HandleObject
17541754
// `responseDone`.
17551755
if (Response::is_instance(body_owner)) {
17561756
ENGINE->decr_event_loop_interest();
1757-
FetchEvent::set_state(FetchEvent::instance(), FetchEvent::State::responseDone);
1757+
JS::RootedValue fetch_event_val(
1758+
cx, JS::GetReservedSlot(body_owner, static_cast<uint32_t>(Response::Slots::FetchEvent)));
1759+
if (!fetch_event_val.isObject()) {
1760+
JS_ReportErrorASCII(cx, "Response does not have an associated FetchEvent");
1761+
return false;
1762+
}
1763+
JS::RootedObject fetch_event(cx, &fetch_event_val.toObject());
1764+
FetchEvent::set_state(fetch_event, FetchEvent::State::responseDone);
17581765
}
17591766

17601767
auto res = body.close();
@@ -1844,7 +1851,14 @@ bool RequestOrResponse::body_reader_catch_handler(JSContext *cx, JS::HandleObjec
18441851
// a response at all failed.)
18451852
if (Response::is_instance(body_owner)) {
18461853
ENGINE->decr_event_loop_interest();
1847-
FetchEvent::set_state(FetchEvent::instance(), FetchEvent::State::responseDone);
1854+
JS::RootedValue fetch_event_val(
1855+
cx, JS::GetReservedSlot(body_owner, static_cast<uint32_t>(Response::Slots::FetchEvent)));
1856+
if (!fetch_event_val.isObject()) {
1857+
JS_ReportErrorASCII(cx, "Response does not have an associated FetchEvent");
1858+
return false;
1859+
}
1860+
JS::RootedObject fetch_event(cx, &fetch_event_val.toObject());
1861+
FetchEvent::set_state(fetch_event, FetchEvent::State::responseDone);
18481862
}
18491863
return true;
18501864
}

runtime/fastly/builtins/fetch/request-response.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class RequestOrResponse final {
2222
Backend,
2323
CacheEntry,
2424
SourceRequest, // Tracks the original Request when body is proxied via TransformStream
25+
FetchEvent,
2526
Count,
2627
};
2728

@@ -174,6 +175,7 @@ class Request final : public builtins::BuiltinImpl<Request> {
174175
Backend = static_cast<int>(RequestOrResponse::Slots::Backend),
175176
CacheEntry = static_cast<int>(RequestOrResponse::Slots::CacheEntry),
176177
SourceRequest = static_cast<int>(RequestOrResponse::Slots::SourceRequest),
178+
FetchEvent = static_cast<int>(RequestOrResponse::Slots::FetchEvent),
177179
Method = static_cast<int>(RequestOrResponse::Slots::Count),
178180
OverrideCacheKey,
179181
CacheOverride,
@@ -265,6 +267,7 @@ class Response final : public builtins::FinalizableBuiltinImpl<Response> {
265267
Backend = static_cast<int>(RequestOrResponse::Slots::Backend),
266268
CacheEntry = static_cast<int>(RequestOrResponse::Slots::CacheEntry),
267269
SourceRequest = static_cast<int>(RequestOrResponse::Slots::SourceRequest),
270+
FetchEvent = static_cast<int>(RequestOrResponse::Slots::FetchEvent),
268271
IsUpstream = static_cast<int>(RequestOrResponse::Slots::Count),
269272
Status,
270273
StatusMessage,

runtime/fastly/handler.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ void handle_incoming(host_api::Request req) {
3636
start = system_clock::now();
3737
}
3838

39-
__wasilibc_initialize_environ();
39+
__wasilibc_ensure_environ();
4040

4141
if (ENGINE->debug_logging_enabled()) {
4242
printf("Running JS handleRequest function for Fastly Compute service version %s\n",
4343
getenv("FASTLY_SERVICE_VERSION"));
4444
fflush(stdout);
4545
}
4646

47-
HandleObject fetch_event = FetchEvent::instance();
47+
RootedObject fetch_event(ENGINE->cx(), FetchEvent::create(ENGINE->cx()));
4848
if (!FetchEvent::init_request(ENGINE->cx(), fetch_event, req.req, req.body)) {
4949
ENGINE->dump_pending_exception("initialization of FetchEvent");
5050
return;
@@ -176,13 +176,6 @@ int main(int argc, const char *argv[]) {
176176
HANDLE_ERROR(ENGINE->cx(), *req.to_err());
177177
return -1;
178178
}
179-
180-
// The FetchEvent instance is a singleton that we re-initialize here. It's originally
181-
// initialized during engine setup.
182-
if (!FetchEvent::reset(fastly::runtime::ENGINE->cx(), FetchEvent::instance())) {
183-
fprintf(stderr, "Failed to reset FetchEvent instance for new request, exiting process.\n");
184-
return -1;
185-
}
186179
}
187180

188181
if (fastly::runtime::ENGINE->debug_logging_enabled()) {

0 commit comments

Comments
 (0)