quickjs-done-nextgen/tests/test_worker.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-09-06 17:07:30 +00:00
import * as os from "os";
2024-10-10 19:39:12 +00:00
import { assert } from "./assert.js";
2020-09-06 17:07:30 +00:00
var worker;
function test_worker()
{
var counter;
2020-09-06 17:10:15 +00:00
worker = new os.Worker("./test_worker_module.js");
2020-09-06 17:07:30 +00:00
counter = 0;
worker.onmessage = function (e) {
var ev = e.data;
// print("recv", JSON.stringify(ev));
switch(ev.type) {
case "num":
assert(ev.num, counter);
counter++;
if (counter == 10) {
/* test SharedArrayBuffer modification */
let sab = new SharedArrayBuffer(10);
let buf = new Uint8Array(sab);
worker.postMessage({ type: "sab", buf: buf });
}
break;
case "sab_done":
{
let buf = ev.buf;
/* check that the SharedArrayBuffer was modified */
assert(buf[2], 10);
worker.postMessage({ type: "abort" });
}
break;
case "done":
/* terminate */
worker.onmessage = null;
break;
}
};
}
if (os.platform !== 'win32') {
test_worker();
}