Skip to content

Commit dd1cb46

Browse files
authored
Add js_std_set_worker_new_runtime_func() (#1246)
Fixes: #1245
1 parent bd4ee68 commit dd1cb46

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

quickjs-libc.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3654,7 +3654,8 @@ typedef struct {
36543654
uint64_t buf[];
36553655
} JSSABHeader;
36563656

3657-
static JSContext *(*js_worker_new_context_func)(JSRuntime *rt);
3657+
static JSRuntime *(*js_worker_new_runtime_func)(void) = JS_NewRuntime;
3658+
static JSContext *(*js_worker_new_context_func)(JSRuntime *rt) = JS_NewContext;
36583659

36593660
static int atomic_add_int(int *ptr, int v)
36603661
{
@@ -3783,7 +3784,7 @@ static void worker_func(void *opaque)
37833784
JSContext *ctx;
37843785
JSValue val;
37853786

3786-
rt = JS_NewRuntime();
3787+
rt = js_worker_new_runtime_func();
37873788
if (rt == NULL) {
37883789
fprintf(stderr, "JS_NewRuntime failure");
37893790
exit(1);
@@ -4062,6 +4063,13 @@ static const JSCFunctionListEntry js_worker_proto_funcs[] = {
40624063

40634064
#endif /* USE_WORKER */
40644065

4066+
void js_std_set_worker_new_runtime_func(JSRuntime *(*func)(void))
4067+
{
4068+
#ifdef USE_WORKER
4069+
js_worker_new_runtime_func = func;
4070+
#endif
4071+
}
4072+
40654073
void js_std_set_worker_new_context_func(JSContext *(*func)(JSRuntime *rt))
40664074
{
40674075
#ifdef USE_WORKER

quickjs-libc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ JS_EXTERN void js_std_promise_rejection_tracker(JSContext *ctx,
6565
JSValueConst reason,
6666
bool is_handled,
6767
void *opaque);
68+
// Defaults to JS_NewRuntime, no-op if compiled without worker support.
69+
// Call before creating the first worker thread.
70+
JS_EXTERN void js_std_set_worker_new_runtime_func(JSRuntime *(*func)(void));
71+
// Defaults to JS_NewContext, no-op if compiled without worker support.
72+
// Call before creating the first worker thread.
6873
JS_EXTERN void js_std_set_worker_new_context_func(JSContext *(*func)(JSRuntime *rt));
6974

7075
#undef JS_EXTERN

0 commit comments

Comments
 (0)