Skip to content

Implement AddToKeptObjects and add JS_ClearKeptObjects - #1606

Open
littledivy wants to merge 1 commit into
quickjs-ng:masterfrom
littledivy:add-clear-kept-objects
Open

Implement AddToKeptObjects and add JS_ClearKeptObjects#1606
littledivy wants to merge 1 commit into
quickjs-ng:masterfrom
littledivy:add-clear-kept-objects

Conversation

@littledivy

@littledivy littledivy commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

quickjs is missing AddToKeptObjects: a WeakRef target must survive until the end of the job. Refcounting makes it observable without gc:

let o = { x: 1 };
const w = new WeakRef(o);
o = null;
w.deref();  // undefined. spec: still the object

Fix: the WeakRef constructor and deref() hold a strong ref to the target on a runtime list. JS_ExecutePendingJob drops the list on entry, so targets live exactly until the next job starts. Embedders that don't run jobs can drop it themselves with the new JS_ClearKeptObjects (ClearKeptObjects, same as v8::Isolate::ClearKeptObjects).

Disclaimer: This is an AI-assisted patch from the v8x project.

@littledivy
littledivy force-pushed the add-clear-kept-objects branch 4 times, most recently from 1b2060f to 329255f Compare July 26, 2026 04:37
@littledivy littledivy closed this Jul 26, 2026
@littledivy littledivy reopened this Jul 26, 2026
A WeakRef target must stay alive until the end of the current job
(AddToKeptObjects in the WeakRef constructor and in deref, cleared by
ClearKeptObjects when a job completes). Because quickjs reclaims by
reference counting, the violation is observable in plain JS:

    let o = { x: 1 };
    const w = new WeakRef(o);
    o = null;
    w.deref();  // undefined, spec says the object

Keep a list of retained targets on the runtime. JS_ExecutePendingJob
clears it on entry: the host calling it signals that the previous
script or job finished, and the usual drain loop's final call clears
the last job's set. Embedders that do not drain jobs can call the new
JS_ClearKeptObjects directly. V8 exposes the same operation as
v8::Isolate::ClearKeptObjects.

tests/bug652.js asserted the old behavior (it exists to check deref
does not throw); its expectation is updated.
@littledivy
littledivy force-pushed the add-clear-kept-objects branch from 329255f to 027906d Compare July 26, 2026 05:00
@saghul

saghul commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

When is the public API used in general?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants