Skip to content

Truly independent web browser

2.4 KB
Raw Blame
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
    // A media element that picks its resource from <source> children hands its fetch a failure callback that captures
    // its source element selector. That capture mustn't keep the selector alive: The selector holds the element, so a
    // GC root would pin the element and its whole document as long as the element lives. Load a <video> with a <source>
    // child in an iframe, replace the document repeatedly, and check that every replaced document is collected.
    promiseTest(async () => {
        const iframe = document.createElement("iframe");
        document.body.append(iframe);

        const mediaURL = new URL("../../../Assets/test-webm.webm", location.href).href;
        const navigate = i =>
            new Promise(resolve => {
                iframe.addEventListener("load", resolve, { once: true });
                iframe.srcdoc = `<!DOCTYPE html><video preload="auto"><source src="${mediaURL}" type="video/webm"></video><p>${i}</p>`;
            });

        // Record each document, then navigate away from it. An expando preserves the document's wrapper for as long as
        // the document itself lives — so the weak reference tracks the document rather than a wrapper that JS may drop
        // early. The recording starts with the second document: the first one reuses the initial about:blank document's
        // window — whose realm the navigable's WindowProxy keeps alive for as long as the iframe lives. And the record-
        // ing runs in a nested function — so that no document lingers in a register of this function's frame while the
        // documents are expected to be collected.
        const weakDocuments = await (async () => {
            const weakDocuments = [];
            await navigate(0);
            await navigate(1);
            for (let i = 2; i <= 6; i++) {
                const replacedDocument = iframe.contentDocument;
                replacedDocument.preserveWrapper = true;
                weakDocuments.push(new WeakRef(replacedDocument));
                await navigate(i);
            }
            return weakDocuments;
        })();

        await collectGarbageUntilCleared(weakDocuments);

        const collected = weakDocuments.filter(weak => weak.deref() === undefined).length;
        println(`collected ${collected} of ${weakDocuments.length} replaced documents`);
        iframe.remove();
    });
</script>