diff --git a/quickjs-libc.c b/quickjs-libc.c index 2c7ac7704..ae013004d 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -3654,7 +3654,8 @@ typedef struct { uint64_t buf[]; } JSSABHeader; -static JSContext *(*js_worker_new_context_func)(JSRuntime *rt); +static JSRuntime *(*js_worker_new_runtime_func)(void) = JS_NewRuntime; +static JSContext *(*js_worker_new_context_func)(JSRuntime *rt) = JS_NewContext; static int atomic_add_int(int *ptr, int v) { @@ -3783,7 +3784,7 @@ static void worker_func(void *opaque) JSContext *ctx; JSValue val; - rt = JS_NewRuntime(); + rt = js_worker_new_runtime_func(); if (rt == NULL) { fprintf(stderr, "JS_NewRuntime failure"); exit(1); @@ -4062,6 +4063,13 @@ static const JSCFunctionListEntry js_worker_proto_funcs[] = { #endif /* USE_WORKER */ +void js_std_set_worker_new_runtime_func(JSRuntime *(*func)(void)) +{ +#ifdef USE_WORKER + js_worker_new_runtime_func = func; +#endif +} + void js_std_set_worker_new_context_func(JSContext *(*func)(JSRuntime *rt)) { #ifdef USE_WORKER diff --git a/quickjs-libc.h b/quickjs-libc.h index 6af5a639d..8617700ef 100644 --- a/quickjs-libc.h +++ b/quickjs-libc.h @@ -65,6 +65,11 @@ JS_EXTERN void js_std_promise_rejection_tracker(JSContext *ctx, JSValueConst reason, bool is_handled, void *opaque); +// Defaults to JS_NewRuntime, no-op if compiled without worker support. +// Call before creating the first worker thread. +JS_EXTERN void js_std_set_worker_new_runtime_func(JSRuntime *(*func)(void)); +// Defaults to JS_NewContext, no-op if compiled without worker support. +// Call before creating the first worker thread. JS_EXTERN void js_std_set_worker_new_context_func(JSContext *(*func)(JSRuntime *rt)); #undef JS_EXTERN