use emulator.destroy() over emulator.stop() to properly remove v86 instances

emulator.stop sometimes works (when the GC can figure out that .start()
is unreachable and nothing can call into the instance). However, some
resources, such as any WebSocket connection, need to be closed manually.
This commit is contained in:
Fabian 2024-12-20 10:35:55 -07:00
parent 5d867bd592
commit 23411f337e
20 changed files with 17 additions and 21 deletions

View file

@ -38,7 +38,7 @@ process.stdin.on("data", function(c)
if(c === "\u0003")
{
// ctrl c
emulator.stop();
emulator.destroy();
process.stdin.pause();
}
else

View file

@ -42,7 +42,7 @@ process.stdin.on("data", async function(c)
if(c === "\u0003")
{
// ctrl c
emulator.stop();
emulator.destroy();
process.stdin.pause();
}
else if(c === "\x1b\x4f\x51")

View file

@ -1997,7 +1997,7 @@
$("exit").onclick = function()
{
emulator.stop();
emulator.destroy();
location.href = location.pathname;
};

View file

@ -1,7 +1,7 @@
#!/usr/bin/env node
"use strict";
// This test checks that calling emulator.stop() will remove all event
// This test checks that calling emulator.destroy() will remove all event
// listeners, so that the nodejs process cleanly and automatically exits.
const TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;
@ -28,6 +28,6 @@ const emulator = new V86(config);
setTimeout(function()
{
console.error("Calling stop()");
emulator.stop();
emulator.destroy();
console.error("Called stop()");
}, 3000);

View file

@ -44,7 +44,7 @@ setTimeout(async () =>
emulator.keyboard_send_text("dir A:\n");
await emulator.wait_until_vga_screen_contains("FDOS <DIR>");
console.log("Got FDOS");
emulator.stop();
emulator.destroy();
clearTimeout(timeout);
//clearInterval(interval);
}, 1000);

View file

@ -44,7 +44,7 @@ emulator.add_listener("serial0-output-byte", function(byte)
if(serial_text.endsWith("Files send via emulator appear in /mnt/"))
{
console.log("Ok");
emulator.stop();
emulator.destroy();
clearTimeout(timeout);
}
}

View file

@ -36,7 +36,7 @@ emulator.add_listener("serial0-output-byte", function(byte)
serial_text = "";
if(did_restart) {
console.log("Ok");
emulator.stop();
emulator.destroy();
}
else {
console.log("Calling restart()");

View file

@ -49,6 +49,6 @@ emulator.add_listener("serial0-output-byte", function(byte)
assert("da1fb5b421123c58080a59832675632505b8c139a8d7ecd1c31591ca5c65cea6" === hash.digest("hex"));
console.log("ok");
clearTimeout(timeout);
emulator.stop();
emulator.destroy();
}
});

View file

@ -87,7 +87,7 @@ async function run_test(name, config, done)
}
console.log("Done: %s", name);
emulator.stop();
emulator.destroy();
}
(async function() {

View file

@ -69,7 +69,7 @@ emulator.add_listener("serial0-output-byte", function(byte)
if(line === "* Trademarks are property of their respective holder.")
{
emulator.stop();
emulator.destroy();
if(BENCH_COLLECT_STATS)
{

View file

@ -54,7 +54,7 @@ emulator.add_listener("serial0-output-byte", function(byte)
if(line.startsWith("sys"))
{
emulator.stop();
emulator.destroy();
if(BENCH_COLLECT_STATS)
{

View file

@ -71,7 +71,7 @@ emulator.add_listener("serial0-output-byte", function(byte)
const end_time = Date.now();
const elapsed = end_time - start_time;
console.log("Done in %dms", elapsed);
emulator.stop();
emulator.destroy();
if(BENCH_COLLECT_STATS)
{

View file

@ -265,7 +265,6 @@ emulator.add_listener("serial0-output-byte", function(byte)
if(test_num >= tests.length)
{
emulator.stop();
emulator.destroy();
console.log("Tests finished.");

View file

@ -59,7 +59,7 @@ emulator.add_listener("serial0-output-byte", function(byte)
if(line.endsWith("pong"))
{
console.log("\nTest passed");
emulator.stop();
emulator.destroy();
}
});

View file

@ -162,7 +162,6 @@ emulator.add_listener("serial0-output-byte", function(byte)
if(test_num >= tests.length)
{
emulator.stop();
emulator.destroy();
console.log("Tests finished.");

View file

@ -1146,7 +1146,6 @@ function run_test(test, done)
clearInterval(screen_interval);
}
emulator.stop();
emulator.destroy();
if(test.failure_allowed)

View file

@ -63,7 +63,7 @@ emulator.add_listener("serial0-output-byte", async function(byte)
const data = await emulator.read_file("/result");
emulator.stop();
emulator.destroy();
let result = Buffer.from(data).toString();
if(result !== "test_shared passed\ntest_consecutive_written passed\n")

View file

@ -19,7 +19,6 @@ window.onload = function()
});
setTimeout(() => {
emulator.stop();
emulator.destroy();
console.log("Emulator freed. Check using devtools (in chromium: Memory -> Heap Snapshot -> click collect garbage -> take snapshot).");
}, 3 * 1000);

View file

@ -64,6 +64,6 @@ emulator.add_listener("serial0-output-byte", async function(byte)
console.error("Got result, writing to stdout");
process.stdout.write(Buffer.from(data));
emulator.stop();
emulator.destroy();
}
});

View file

@ -51,7 +51,7 @@ emulator.add_listener("serial0-output-byte", function(byte)
{
if(e) throw e;
console.log("Saved as " + OUTPUT_FILE);
emulator.stop();
emulator.destroy();
});
}, 10 * 1000);
}