Skip to content

Commit 68d980f

Browse files
committed
Add support for retrieving the Engine from a JSContext
... and use that wherever we previously stored a pointer to the `Engine` in a static global. Signed-off-by: Till Schneidereit <till@tillschneidereit.net>
1 parent 7b341c9 commit 68d980f

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

builtins/web/fetch/fetch-api.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ bool fetch_https(JSContext *cx, HandleObject request_obj, HostString method, Hos
107107
// If the request body is streamed, we need to wait for streaming to complete
108108
// before marking the request as pending.
109109
if (!streaming) {
110-
ENGINE->queue_async_task(new ResponseFutureTask(request_obj, pending_handle));
110+
api::Engine::from_context(cx)
111+
.queue_async_task(new ResponseFutureTask(request_obj, pending_handle));
111112
}
112113

113114
SetReservedSlot(request_obj, static_cast<uint32_t>(Request::Slots::ResponsePromise),
@@ -345,8 +346,6 @@ bool fetch(JSContext *cx, unsigned argc, Value *vp) {
345346
const JSFunctionSpec methods[] = {JS_FN("fetch", fetch, 2, JSPROP_ENUMERATE), JS_FS_END};
346347

347348
bool install(api::Engine *engine) {
348-
ENGINE = engine;
349-
350349
if (!JS_DefineFunctions(engine->cx(), engine->global(), methods))
351350
return false;
352351
if (!request_response::install(engine)) {

builtins/web/fetch/request-response.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ using form_data::MultipartFormData;
5252

5353
using namespace std::literals;
5454

55-
static api::Engine *ENGINE;
56-
5755
bool error_stream_controller_with_pending_exception(JSContext *cx, HandleObject stream) {
5856
RootedValue exn(cx);
5957
if (!JS_GetPendingException(cx, &exn))
@@ -528,7 +526,8 @@ bool finish_outgoing_body_streaming(JSContext *cx, HandleObject body_owner) {
528526
.toPrivate());
529527
SetReservedSlot(body_owner, static_cast<uint32_t>(Request::Slots::PendingResponseHandle),
530528
PrivateValue(nullptr));
531-
ENGINE->queue_async_task(new ResponseFutureTask(body_owner, pending_handle));
529+
api::Engine::from_context(cx)
530+
.queue_async_task(new ResponseFutureTask(body_owner, pending_handle));
532531
}
533532

534533
return true;
@@ -538,9 +537,10 @@ bool RequestOrResponse::append_body(JSContext *cx, JS::HandleObject self, JS::Ha
538537
api::TaskCompletionCallback callback, HandleObject callback_receiver) {
539538
MOZ_ASSERT(!body_used(source));
540539
MOZ_ASSERT(self != source);
540+
auto engine = api::Engine::from_context(cx);
541541
host_api::HttpIncomingBody *source_body = incoming_body_handle(source);
542542
host_api::HttpOutgoingBody *dest_body = outgoing_body_handle(self);
543-
auto res = dest_body->append(ENGINE, source_body, callback, callback_receiver);
543+
auto res = dest_body->append(&engine, source_body, callback, callback_receiver);
544544
if (auto *err = res.to_err()) {
545545
HANDLE_ERROR(cx, *err);
546546
return false;
@@ -1008,7 +1008,7 @@ bool do_body_source_pull(JSContext *cx, HandleObject source, HandleObject body_o
10081008
return true;
10091009
}
10101010

1011-
ENGINE->queue_async_task(new BodyFutureTask(source));
1011+
api::Engine::from_context(cx).queue_async_task(new BodyFutureTask(source));
10121012
return true;
10131013
}
10141014

@@ -1118,7 +1118,7 @@ bool reader_for_outgoing_body_then_handler(JSContext *cx, JS::HandleObject body_
11181118
// Uint8Array?
11191119
fprintf(stderr, "Error: read operation on body ReadableStream didn't respond with a "
11201120
"Uint8Array. Received value: ");
1121-
ENGINE->dump_value(val, stderr);
1121+
api::Engine::from_context(cx).dump_value(val, stderr);
11221122
return false;
11231123
}
11241124

@@ -1179,7 +1179,7 @@ bool reader_for_outgoing_body_catch_handler(JSContext *cx, JS::HandleObject body
11791179
// stream errored during the streaming send. Not much we can do, but at least
11801180
// close the stream, and warn.
11811181
fprintf(stderr, "Warning: body ReadableStream closed during body streaming. Exception: ");
1182-
ENGINE->dump_value(args.get(0), stderr);
1182+
api::Engine::from_context(cx).dump_value(args.get(0), stderr);
11831183

11841184
return finish_outgoing_body_streaming(cx, body_owner);
11851185
}
@@ -1198,7 +1198,8 @@ bool RequestOrResponse::maybe_stream_body(JSContext *cx, JS::HandleObject body_o
11981198
if (is_incoming(body_owner)) {
11991199
auto *source_body = incoming_body_handle(body_owner);
12001200
auto *dest_body = destination->body().unwrap();
1201-
auto res = dest_body->append(ENGINE, source_body, finish_outgoing_body_streaming, nullptr);
1201+
auto engine = api::Engine::from_context(cx);
1202+
auto res = dest_body->append(&engine, source_body, finish_outgoing_body_streaming, nullptr);
12021203
if (auto *err = res.to_err()) {
12031204
HANDLE_ERROR(cx, *err);
12041205
return false;
@@ -2687,8 +2688,6 @@ JSObject *Response::create_incoming(JSContext *cx, host_api::HttpIncomingRespons
26872688
namespace request_response {
26882689

26892690
bool install(api::Engine *engine) {
2690-
ENGINE = engine;
2691-
26922691
if (!Request::init_class(engine->cx(), engine->global()))
26932692
return false;
26942693
if (!Response::init_class(engine->cx(), engine->global()))

builtins/web/timers.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class TimersMap {
2828
}
2929

3030
static PersistentRooted<js::UniquePtr<TimersMap>> TIMERS_MAP;
31-
static api::Engine *ENGINE;
3231

3332
class TimerTask final : public api::AsyncTask {
3433
using TimerArgumentsVector = std::vector<JS::Heap<JS::Value>>;
@@ -119,12 +118,12 @@ class TimerTask final : public api::AsyncTask {
119118
}
120119
}
121120

122-
static bool clear(int32_t timer_id) {
121+
static bool clear(api::Engine &engine, int32_t timer_id) {
123122
if (!TIMERS_MAP->timers_.contains(timer_id)) {
124123
return false;
125124
}
126125

127-
ENGINE->cancel_async_task(TIMERS_MAP->timers_[timer_id]);
126+
engine.cancel_async_task(TIMERS_MAP->timers_[timer_id]);
128127
TIMERS_MAP->timers_.erase(timer_id);
129128
return true;
130129
}
@@ -171,7 +170,7 @@ template <bool repeat> bool setTimeout_or_interval(JSContext *cx, const unsigned
171170
}
172171

173172
const auto timer = new TimerTask(delay, repeat, handler, handler_args);
174-
ENGINE->queue_async_task(timer);
173+
api::Engine::from_context(cx).queue_async_task(timer);
175174
args.rval().setInt32(timer->timer_id());
176175

177176
return true;
@@ -193,7 +192,7 @@ template <bool interval> bool clearTimeout_or_interval(JSContext *cx, unsigned a
193192
return false;
194193
}
195194

196-
TimerTask::clear(id);
195+
TimerTask::clear(api::Engine::from_context(cx), id);
197196

198197
args.rval().setUndefined();
199198
return true;
@@ -206,7 +205,6 @@ constexpr JSFunctionSpec methods[] = {
206205
JS_FN("clearTimeout", clearTimeout_or_interval<false>, 1, JSPROP_ENUMERATE), JS_FS_END};
207206

208207
bool install(api::Engine *engine) {
209-
ENGINE = engine;
210208
TIMERS_MAP.init(engine->cx(), js::MakeUnique<TimersMap>());
211209
return JS_DefineFunctions(engine->cx(), engine->global(), methods);
212210
}

include/extension-api.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,26 @@ class Engine {
8383

8484
public:
8585
explicit Engine(std::unique_ptr<EngineConfig> config);
86+
87+
/**
88+
* Returns the `Engine` associated with the `JSContext` instance.
89+
*
90+
* Currently, StarlingMonkey uses exactly one `Engine` and one `JSContext`. Since that might
91+
* change in the future, it's heavily recommended to not store either in static globals.
92+
*
93+
* @return The `Engine` associated with the `JSContext` instance
94+
*/
95+
// TODO: change to static Engine& from_context(JSContext *cx);
8696
static Engine *get(JSContext *cx);
8797

98+
/**
99+
* Returns the `JSContext` associated with the `Engine` instance.
100+
*
101+
* Currently, StarlingMonkey uses exactly one `Engine` and one `JSContext`. Since that might
102+
* change in the future, it's heavily recommended to not store either in static globals.
103+
*
104+
* @return The JSContext associated with the `Engine` instance
105+
*/
88106
JSContext *cx();
89107
JS::HandleObject global();
90108
EngineState state();

0 commit comments

Comments
 (0)