Skip to content

Commit

Permalink
tla reworking
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Apr 18, 2024
1 parent e1f749d commit 2023e05
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 37 deletions.
14 changes: 6 additions & 8 deletions builtins/web/fetch/fetch_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,16 +589,14 @@ void exports_wasi_http_incoming_handler(exports_wasi_http_incoming_request reque

dispatch_fetch_event(fetch_event, &total_compute);

RootedValue result(ENGINE->cx());
if (!ENGINE->run_event_loop(&result)) {
fflush(stdout);
fprintf(stderr, "Error running event loop: ");
ENGINE->dump_value(result, stderr);
return;
}
bool success = ENGINE->run_event_loop();

if (JS_IsExceptionPending(ENGINE->cx())) {
ENGINE->dump_pending_exception("Error evaluating code: ");
ENGINE->dump_pending_exception("evaluating incoming request");
}

if (!success) {
fprintf(stderr, "Internal error.");
}

if (ENGINE->debug_logging_enabled() && ENGINE->has_pending_async_tasks()) {
Expand Down
2 changes: 1 addition & 1 deletion include/extension-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Engine {
void enable_module_mode(bool enable);
bool eval_toplevel(const char *path, MutableHandleValue result);

bool run_event_loop(MutableHandleValue result);
bool run_event_loop();

/**
* Get the JS value associated with the top-level script execution -
Expand Down
40 changes: 20 additions & 20 deletions runtime/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,25 @@ bool api::Engine::eval_toplevel(const char *path, MutableHandleValue result) {
return false;
}

// Drive TLA as far as possible. Rejections during pre-initialization are
// treated as top-level exceptions. TLA may remain unresolved, in which case
// it will continue tasks at runtime. Rejections after pre-intialization remain
// unhandled rejections for now.
SCRIPT_VALUE.init(cx, ns);

// Ensure that any pending promise reactions are run before taking the
// snapshot.
while (js::HasJobsPending(cx)) {
js::RunJobs(cx);

if (JS_IsExceptionPending(cx))
abort("running Promise reactions");
}

if (!core::EventLoop::run_event_loop(this, 0)) {
return false;
}

// TLA rejections during pre-initialization are treated as top-level exceptions.
// TLA may remain unresolved, in which case it will continue tasks at runtime.
// Rejections after pre-intialization remain unhandled rejections for now.
if (tla_promise.isObject()) {
if (!core::EventLoop::run_event_loop(this, 0, &tla_promise)) {
return false;
}
RootedObject promise_obj(cx, &tla_promise.toObject());
JS::PromiseState state = JS::GetPromiseState(promise_obj);
if (state == JS::PromiseState::Rejected) {
Expand All @@ -377,17 +388,6 @@ bool api::Engine::eval_toplevel(const char *path, MutableHandleValue result) {
}
}

SCRIPT_VALUE.init(cx, ns);

// Ensure that any pending promise reactions are run before taking the
// snapshot.
while (js::HasJobsPending(cx)) {
js::RunJobs(cx);

if (JS_IsExceptionPending(cx))
abort("running Promise reactions");
}

// Report any promise rejections that weren't handled before snapshotting.
// TODO: decide whether we should abort in this case, instead of just
// reporting.
Expand Down Expand Up @@ -424,8 +424,8 @@ bool api::Engine::eval_toplevel(const char *path, MutableHandleValue result) {
return true;
}

bool api::Engine::run_event_loop(MutableHandleValue result) {
return core::EventLoop::run_event_loop(this, 0, result);
bool api::Engine::run_event_loop() {
return core::EventLoop::run_event_loop(this, 0);
}

bool api::Engine::dump_value(JS::Value val, FILE *fp) { return ::dump_value(CONTEXT, val, fp); }
Expand Down
5 changes: 2 additions & 3 deletions runtime/event_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ bool EventLoop::cancel_async_task(api::Engine *engine, const int32_t id) {

bool EventLoop::has_pending_async_tasks() { return !queue.get().tasks.empty(); }

bool EventLoop::run_event_loop(api::Engine *engine, double total_compute,
MutableHandleValue result) {
bool EventLoop::run_event_loop(api::Engine *engine, double total_compute) {
// Loop until no more resolved promises or backend requests are pending.
// LOG("Start processing async jobs ...\n");

Expand All @@ -51,7 +50,7 @@ bool EventLoop::run_event_loop(api::Engine *engine, double total_compute,
js::RunJobs(cx);

if (JS_IsExceptionPending(cx))
engine->abort("running Promise reactions");
return false;
}

// TODO: add general mechanism for extending the event loop duration.
Expand Down
2 changes: 1 addition & 1 deletion runtime/event_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class EventLoop {
*
* The loop terminates once both of these steps are null-ops.
*/
static bool run_event_loop(api::Engine *engine, double total_compute, MutableHandleValue result);
static bool run_event_loop(api::Engine *engine, double total_compute);

/**
* Queue a new async task.
Expand Down
5 changes: 2 additions & 3 deletions runtime/js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bool initialize(const char *filename) {

RootedValue result(engine.cx());
bool success = engine.eval_toplevel(filename, &result);
success = success && engine.run_event_loop(&result);
success = success && engine.run_event_loop();

if (JS_IsExceptionPending(engine.cx())) {
engine.dump_pending_exception("pre-initializing");
Expand All @@ -51,8 +51,7 @@ bool initialize(const char *filename) {

if (!success) {
fflush(stdout);
fprintf(stderr, "Error running event loop: ");
engine.dump_value(result, stderr);
fprintf(stderr, "Internal error");
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/cases/syntax-err/expect_wizer_stderr.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Exception while pre-initializing: (new SyntaxError("expected expression, got end of script", "syntax-err.js", 1))
Exception while pre-initializing: (new SyntaxError("expected expression, got end of script", "syntax-err.js", 2))

0 comments on commit 2023e05

Please sign in to comment.