Skip to content

Instantly share code, notes, and snippets.

@cometkim
Created May 24, 2023 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cometkim/96bb3bdc642b435cc2ab230f99350f80 to your computer and use it in GitHub Desktop.
Save cometkim/96bb3bdc642b435cc2ab230f99350f80 to your computer and use it in GitHub Desktop.
Is growable SAB GC-able?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>SharedArrayBuffer GC test</title>
</head>
<body>
<div id="root"></div>
<script type="module">
let registry = new FinalizationRegistry(v => {
console.log('Just finalized ', v);
});
let sab = new SharedArrayBuffer(4 * 1024 * 1024);
let growableSab = new SharedArrayBuffer(0, { maxByteLength: 4 * 1024 * 1024 });
registry.register(sab, "sab");
registry.register(growableSab, "growable sab");
let worker = new Worker('worker.js', { type: 'module' });
worker.postMessage({ sab });
worker.postMessage({ sab: growableSab });
(function() {
console.log("grow sab");
growableSab.grow(4 * 1024 * 1024);
registry.register({}, "tmp");
})();
</script>
</body>
</html>
console.log('Hello from Worker!', location.origin);
self.addEventListener('message', e => {
test(e.data.sab);
});
function test(sab) {
console.log('Initilized with ', { sab });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment