In worker_func the runtime is created using the default allocation functions (JS_NewRuntime does that)
In the same vein as js_worker_new_context_func we should have a js_worker_new_runtime_func which would allowto pass whatever allocation function we want.
Bonus point: we should also be able to provide freeing callbacks for worker's runtime and context as in the current state, we are unable to release any resource we would have allocated and stored in user_opaque for context (and runtime as well once we have a callback for allocation)
Proposed changes in quickjs-libc.c
Add a user pointer
static void * js_worker_user_opaque = NULL;
Modify existing callback to take user opaque:
static JSContext *(*js_worker_new_context_func)(JSRuntime *rt, void *);
Add a 3 static callbacks:
static JSRuntime *(*js_worker_new_runtime_func)(void *);
static void(*js_worker_free_runtime_func)(JSRuntime *, void *);
static void(*js_worker_free_context_func)(JSContext *, void *);
Add setters for the user opaque and the new callbacks
- Adapt signature of
js_std_set_worker_new_context_func to take an additional void *
- Add
void js_std_set_worker_new_runtime_func(JSRuntime *(*func)(void * user_opaque))
- Add
void js_std_set_worker_free_context_func(void(*func)(JSContext *ctx, void * user_opaque))
- Add
void js_std_set_worker_free_runtime_func(void(*func)(JSRuntime *rt, void * user_opaque))
In worker_func:
- Replace
rt = JS_NewRuntime(); by rt = js_worker_new_runtime_func(js_worker_user_opaque );
- Replace
ctx = js_worker_new_context_func(rt); by ctx = js_worker_new_context_func(rt, js_worker_user_opaque);
- Replace
JS_FreeContext(ctx); by js_worker_free_context_func(ctx, js_worker_user_opaque);
- Replace
JS_FreeRuntime(rt); by js_worker_free_runtime_func(rt, js_worker_user_opaque);
In
worker_functhe runtime is created using the default allocation functions (JS_NewRuntimedoes that)In the same vein as
js_worker_new_context_funcwe should have ajs_worker_new_runtime_funcwhich would allowto pass whatever allocation function we want.Bonus point: we should also be able to provide freeing callbacks for worker's runtime and context as in the current state, we are unable to release any resource we would have allocated and stored in
user_opaquefor context (and runtime as well once we have a callback for allocation)Proposed changes in
quickjs-libc.cAdd a user pointer
static void * js_worker_user_opaque = NULL;Modify existing callback to take user opaque:
static JSContext *(*js_worker_new_context_func)(JSRuntime *rt, void *);Add a 3 static callbacks:
static JSRuntime *(*js_worker_new_runtime_func)(void *);static void(*js_worker_free_runtime_func)(JSRuntime *, void *);static void(*js_worker_free_context_func)(JSContext *, void *);Add setters for the user opaque and the new callbacks
js_std_set_worker_new_context_functo take an additionalvoid *void js_std_set_worker_new_runtime_func(JSRuntime *(*func)(void * user_opaque))void js_std_set_worker_free_context_func(void(*func)(JSContext *ctx, void * user_opaque))void js_std_set_worker_free_runtime_func(void(*func)(JSRuntime *rt, void * user_opaque))In worker_func:
rt = JS_NewRuntime();byrt = js_worker_new_runtime_func(js_worker_user_opaque );ctx = js_worker_new_context_func(rt);byctx = js_worker_new_context_func(rt, js_worker_user_opaque);JS_FreeContext(ctx);byjs_worker_free_context_func(ctx, js_worker_user_opaque);JS_FreeRuntime(rt);byjs_worker_free_runtime_func(rt, js_worker_user_opaque);