Skip to content

Allow specifying allocation functions for workers #1245

@Vizepi

Description

@Vizepi

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);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions