Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions api-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,73 @@ static void pending_job_lifetime(void)
JS_FreeRuntime(rt);
}

typedef struct {
uint64_t current_tag;
int count;
JSJob *jobs[4];
uint64_t tags[4];
} JobHookState;

static uint64_t job_hook_tag(JSRuntime *rt, void *opaque)
{
return ((JobHookState *)opaque)->current_tag;
}

static void job_hook_enqueue(JSRuntime *rt, JSJob *job, uint64_t tag,
void *opaque)
{
JobHookState *s = opaque;
assert(s->count < countof(s->jobs));
s->jobs[s->count] = job;
s->tags[s->count] = tag;
s->count++;
}

// A promise reaction job carries the tag current when the reaction was
// registered; the embedder executes or frees every job it receives.
static void job_hooks(void)
{
JSRuntime *rt = new_runtime();
JSContext *ctx = JS_NewContext(rt);
JSContext *job_ctx;
JobHookState s = { 0 };
JSValue ret;
const char *order;

JS_SetJobHooks(rt, job_hook_tag, job_hook_enqueue, &s);
ret = eval(ctx, "globalThis.order = '';"
"globalThis.p = new Promise(r => globalThis.resolve = r);");
JS_FreeValue(ctx, ret);
s.current_tag = 1;
ret = eval(ctx, "p.then(() => order += 'registered by 1')");
JS_FreeValue(ctx, ret);
s.current_tag = 2;
ret = eval(ctx, "Promise.resolve().then(() => order += ', 2'); resolve()");
JS_FreeValue(ctx, ret);
assert(s.count == 2);
assert(s.tags[0] == 2);
assert(s.tags[1] == 1);
assert(!JS_IsJobPending(rt));
assert(JS_ExecutePendingJob(rt, &job_ctx) == 0);

assert(JS_ExecuteJob(rt, s.jobs[1], &job_ctx) == 1);
assert(JS_ExecuteJob(rt, s.jobs[0], &job_ctx) == 1);
ret = eval(ctx, "order");
order = JS_ToCString(ctx, ret);
assert(order && !strcmp(order, "registered by 1, 2"));
JS_FreeCString(ctx, order);
JS_FreeValue(ctx, ret);

s.current_tag = 3;
ret = eval(ctx, "Promise.resolve({ held: [1, 2, 3] }).then(v => v)");
JS_FreeValue(ctx, ret);
assert(s.count == 3);
assert(s.tags[2] == 3);
JS_FreeJob(rt, s.jobs[2]);
JS_FreeContext(ctx);
JS_FreeRuntime(rt);
}

static void pending_module_lifetime(void)
{
static const char module_source[] =
Expand Down Expand Up @@ -2540,6 +2607,7 @@ int main(void)
module_serde();
module_pending_tla_import();
pending_job_lifetime();
job_hooks();
pending_module_lifetime();
finalization_registry_realm_lifetime();
module_unhandled_rejection();
Expand Down
66 changes: 54 additions & 12 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ struct JSRuntime {
void *host_promise_rejection_tracker_opaque;

struct list_head job_list; /* list of JSJobEntry.link */
JSJobTagFunc *job_tag_func;
JSEnqueueJobHook *enqueue_job_hook;
void *job_hooks_opaque;

bool module_normalize_has_attr;
union {
Expand Down Expand Up @@ -2492,9 +2495,23 @@ void JS_SetSharedArrayBufferFunctions(JSRuntime *rt,
rt->sab_funcs = *sf;
}

void JS_SetJobHooks(JSRuntime *rt, JSJobTagFunc *tag_func,
JSEnqueueJobHook *enqueue_hook, void *opaque)
{
rt->job_tag_func = tag_func;
rt->enqueue_job_hook = enqueue_hook;
rt->job_hooks_opaque = opaque;
}

static uint64_t js_job_tag(JSRuntime *rt)
{
return rt->job_tag_func ? rt->job_tag_func(rt, rt->job_hooks_opaque) : 0;
}

/* return 0 if OK, < 0 if exception */
int JS_EnqueueJob(JSContext *ctx, JSJobFunc *job_func,
int argc, JSValueConst *argv)
static int js_enqueue_job_with_tag(JSContext *ctx, uint64_t tag,
JSJobFunc *job_func,
int argc, JSValueConst *argv)
{
JSRuntime *rt = ctx->rt;
JSJobEntry *e;
Expand All @@ -2511,10 +2528,21 @@ int JS_EnqueueJob(JSContext *ctx, JSJobFunc *job_func,
for(i = 0; i < argc; i++) {
e->argv[i] = js_dup(argv[i]);
}
list_add_tail(&e->link, &rt->job_list);
if (rt->enqueue_job_hook)
rt->enqueue_job_hook(rt, e, tag, rt->job_hooks_opaque);
else
list_add_tail(&e->link, &rt->job_list);
return 0;
}

/* return 0 if OK, < 0 if exception */
int JS_EnqueueJob(JSContext *ctx, JSJobFunc *job_func,
int argc, JSValueConst *argv)
{
return js_enqueue_job_with_tag(ctx, js_job_tag(ctx->rt), job_func,
argc, argv);
}

bool JS_IsJobPending(JSRuntime *rt)
{
return !list_empty(&rt->job_list);
Expand All @@ -2534,10 +2562,7 @@ JSContext *JS_GetPendingJobContext(JSRuntime *rt)
destroyed or if no job was pending. */
int JS_ExecutePendingJob(JSRuntime *rt, JSContext **pctx)
{
JSContext *ctx;
JSJobEntry *e;
JSValue res;
int i, ret;

if (list_empty(&rt->job_list)) {
if (pctx)
Expand All @@ -2548,6 +2573,25 @@ int JS_ExecutePendingJob(JSRuntime *rt, JSContext **pctx)
/* get the first pending job and execute it */
e = list_entry(rt->job_list.next, JSJobEntry, link);
list_del(&e->link);
return JS_ExecuteJob(rt, e, pctx);
}

void JS_FreeJob(JSRuntime *rt, JSJob *e)
{
int i;

for(i = 0; i < e->argc; i++)
JS_FreeValueRT(rt, e->argv[i]);
JS_FreeContext(e->realm);
js_free_rt(rt, e);
}

int JS_ExecuteJob(JSRuntime *rt, JSJob *e, JSContext **pctx)
{
JSContext *ctx;
JSValue res;
int i, ret;

ctx = e->realm;
res = e->job_func(ctx, e->argc, vc(e->argv));
for(i = 0; i < e->argc; i++)
Expand Down Expand Up @@ -2662,11 +2706,7 @@ void JS_FreeRuntime(JSRuntime *rt)
JS_FreeValueRT(rt, rt->current_exception);

list_for_each_safe(el, el1, &rt->job_list) {
JSJobEntry *e = list_entry(el, JSJobEntry, link);
for(i = 0; i < e->argc; i++)
JS_FreeValueRT(rt, e->argv[i]);
JS_FreeContext(e->realm);
js_free_rt(rt, e);
JS_FreeJob(rt, list_entry(el, JSJobEntry, link));
}
init_list_head(&rt->job_list);

Expand Down Expand Up @@ -55553,6 +55593,7 @@ typedef struct JSPromiseReactionData {
struct list_head link; /* not used in promise_reaction_job */
JSValue resolving_funcs[2];
JSValue handler;
uint64_t tag; /* job tag when the reaction was registered */
} JSPromiseReactionData;

JSPromiseStateEnum JS_PromiseState(JSContext *ctx, JSValueConst promise)
Expand Down Expand Up @@ -55738,7 +55779,7 @@ static void fulfill_or_reject_promise(JSContext *ctx, JSValueConst promise,
hook_data = JS_GetOpaque(rd->resolving_funcs[0],
JS_CLASS_PROMISE_RESOLVE_FUNCTION);
args[5] = hook_data ? hook_data->promise : promise;
JS_EnqueueJob(ctx, promise_reaction_job, 6, args);
js_enqueue_job_with_tag(ctx, rd->tag, promise_reaction_job, 6, args);
list_del(&rd->link);
promise_reaction_data_free(ctx->rt, rd);
}
Expand Down Expand Up @@ -56544,6 +56585,7 @@ static __exception int perform_promise_then(JSContext *ctx,
if (!JS_IsFunction(ctx, handler))
handler = JS_UNDEFINED;
rd->handler = js_dup(handler);
rd->tag = js_job_tag(ctx->rt);
rd_array[i] = rd;
}

Expand Down
25 changes: 25 additions & 0 deletions quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,31 @@ JS_EXTERN bool JS_IsJobPending(JSRuntime *rt);
JS_EXTERN JSContext *JS_GetPendingJobContext(JSRuntime *rt);
JS_EXTERN int JS_ExecutePendingJob(JSRuntime *rt, JSContext **pctx);

/* Job hooks let the embedder keep the jobs, for example to run the jobs of
different callers separately.

Every job has a tag: the value the tag function returned when a promise
reaction was registered (then, await) for a promise reaction job, and the
value it returned when the job was enqueued for any other job. Without a
tag function, every tag is 0.

With an enqueue hook, QuickJS passes every new job and its tag to the hook
instead of queuing it, and JS_IsJobPending and JS_ExecutePendingJob see no
jobs. The embedder then owns the job: it must pass it exactly once to
JS_ExecuteJob or JS_FreeJob, before JS_FreeRuntime. The hooks may be called
during garbage collection and while QuickJS settles a promise, so they must
not call into QuickJS. */
typedef struct JSJobEntry JSJob;
typedef uint64_t JSJobTagFunc(JSRuntime *rt, void *opaque);
typedef void JSEnqueueJobHook(JSRuntime *rt, JSJob *job, uint64_t tag,
void *opaque);
JS_EXTERN void JS_SetJobHooks(JSRuntime *rt, JSJobTagFunc *tag_func,
JSEnqueueJobHook *enqueue_hook, void *opaque);
/* Executes and frees the job. Returns and reports '*pctx' like
JS_ExecutePendingJob. */
JS_EXTERN int JS_ExecuteJob(JSRuntime *rt, JSJob *job, JSContext **pctx);
JS_EXTERN void JS_FreeJob(JSRuntime *rt, JSJob *job);

/* Structure to retrieve (de)serialized SharedArrayBuffer objects. */
typedef struct JSSABTab {
uint8_t **tab;
Expand Down
Loading