Skip to content

Commit 6f58f29

Browse files
Jake ChampionJakeChampion
authored andcommitted
Use an enum to explicitly state what the execution phase is that the application is currenlty in
resolves #23
1 parent 4c87f8f commit 6f58f29

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

c-dependencies/js-compute-runtime/js-compute-builtins.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ using JS::MutableHandleValue;
5050

5151
using JS::PersistentRooted;
5252

53+
enum class Mode { PreWizening, PostWizening };
54+
55+
Mode execution_mode = Mode::PreWizening;
56+
57+
bool hasWizeningFinished() { return execution_mode == Mode::PostWizening; }
58+
59+
bool isWizening() { return execution_mode == Mode::PreWizening; }
60+
61+
void markWizeningAsFinished() { execution_mode = Mode::PostWizening; }
62+
5363
typedef bool InternalMethod(JSContext *cx, HandleObject receiver, HandleValue extra, CallArgs args);
5464
template <InternalMethod fun> bool internal_method(JSContext *cx, unsigned argc, Value *vp) {
5565
CallArgs args = CallArgsFromVp(argc, vp);
@@ -487,7 +497,7 @@ static const uint32_t class_flags = 0;
487497
}
488498

489499
#define REQUEST_HANDLER_ONLY(name) \
490-
if (!FetchEvent::instance()) { \
500+
if (isWizening()) { \
491501
JS_ReportErrorUTF8(cx, \
492502
"%s can only be used during request handling, " \
493503
"not during initialization", \
@@ -496,7 +506,7 @@ static const uint32_t class_flags = 0;
496506
}
497507

498508
#define INIT_ONLY(name) \
499-
if (FetchEvent::instance()) { \
509+
if (hasWizeningFinished()) { \
500510
JS_ReportErrorUTF8(cx, \
501511
"%s can only be used during initialization, " \
502512
"not during request handling", \

c-dependencies/js-compute-runtime/js-compute-builtins.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
#pragma clang diagnostic pop
1616

17+
bool hasWizeningFinished();
18+
bool isWizening();
19+
void markWizeningAsFinished();
20+
1721
bool define_fastly_sys(JSContext *cx, JS::HandleObject global);
1822

1923
namespace FetchEvent {

c-dependencies/js-compute-runtime/js-compute-runtime.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ static bool dump_mem_stats(JSContext *cx) {
6868
/* The class of the global object. */
6969
static JSClass global_class = {"global", JSCLASS_GLOBAL_FLAGS, &JS::DefaultGlobalClassOps};
7070

71-
bool INITIALIZED = false;
7271
JSContext *CONTEXT = nullptr;
7372

7473
JS::PersistentRootedObject GLOBAL;
@@ -220,7 +219,7 @@ static void abort(JSContext *cx, const char *description) {
220219

221220
// Respond with status `500` if no response was ever sent.
222221
HandleObject fetch_event = FetchEvent::instance();
223-
if (INITIALIZED && !FetchEvent::response_started(fetch_event))
222+
if (hasWizeningFinished() && !FetchEvent::response_started(fetch_event))
224223
FetchEvent::respondWithError(cx, fetch_event);
225224

226225
fflush(stderr);
@@ -352,7 +351,7 @@ static bool addEventListener(JSContext *cx, unsigned argc, Value *vp) {
352351
}
353352

354353
void init() {
355-
assert(!INITIALIZED);
354+
assert(isWizening());
356355

357356
if (!init_js())
358357
exit(1);
@@ -396,7 +395,7 @@ void init() {
396395
dump_mem_stats(cx);
397396
#endif
398397

399-
INITIALIZED = true;
398+
markWizeningAsFinished();
400399
}
401400

402401
WIZER_INIT(init);
@@ -469,9 +468,9 @@ static void wait_for_backends(JSContext *cx, double *total_compute) {
469468
}
470469

471470
int main(int argc, const char *argv[]) {
472-
if (!INITIALIZED) {
471+
if (isWizening()) {
473472
init();
474-
assert(INITIALIZED);
473+
assert(hasWizeningFinished());
475474
// fprintf(stderr, "js.wasm must be initialized with a JS source file using
476475
// Wizer\n"); exit(-1);
477476
}

0 commit comments

Comments
 (0)