Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nodejs: Add a test for callback garbage collection #3864

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
52 changes: 52 additions & 0 deletions api/node/__test__/api.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import test from 'ava'
import * as path from 'node:path';
import { fileURLToPath } from 'url';
import { setFlagsFromString } from 'v8';
import { runInNewContext } from 'vm';

setFlagsFromString('--expose_gc');
const gc = runInNewContext('gc');

import { loadFile, loadSource, CompileError } from '../index.js'

Expand Down Expand Up @@ -159,3 +164,50 @@ test('loadSource component instances and modules are sealed', (t) => {
test.no_such_callback = () => { };
}, { instanceOf: TypeError });
})

test('callback closure cyclic references do not prevent GC', async (t) => {

// Setup:
// A component instance with a callback installed from JS:
// The callback captures the surrounding environment, which
// includes an extra reference to the component instance itself
// --> a cyclic reference
//
// Note: WeakRef's deref is used to observe the GC. This means that we must
// separate the test into different jobs with await, to permit for collection.
// (See https://tc39.es/ecma262/multipage/managing-memory.html#sec-weak-ref.prototype.deref)

let demo_module = loadFile(path.join(dirname, "resources/test-gc.slint")) as any;
let demo = new demo_module.Test();
t.is(demo.check, "initial value");
t.true(Object.hasOwn(demo, "say_hello"));

let demo_weak = new WeakRef(demo);

function scope() {
let copy = demo;
copy.say_hello = () => {
console.log(copy.check);
};
}
scope();

t.true(demo_weak.deref() !== undefined);

// After the first GC, the instance should not have been collected because the
// current environment's demo variable is a strong reference.
await new Promise(resolve => setTimeout(resolve, 0));
gc();

t.true(demo_weak.deref() !== undefined);

// Clear the strong reference here
demo = null;

// After the this GC call, the instance should have been collected. Strong references
// in Rust should not keep it alive.
await new Promise(resolve => setTimeout(resolve, 0));
gc();

t.is(demo_weak.deref(), undefined, "The demo instance should have been collected and the weak ref should deref to undefined");
})
10 changes: 10 additions & 0 deletions api/node/__test__/resources/test-gc.slint
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial


export component Test {
callback say_hello();
in-out property <string> check: "initial value";
}
Loading