13d0ce075e
* Integrate with emnapi * resolve conflict * ignore wasm * generate wasi file * Add wasi test to workflow * Fix wasi template * emnapi new initialize api * Finish test * Purne tsconfig * Generate wasi worker * Fix electron test * Finalize check * Noop adjust_external_memory * Apply cr suggestions
51 lines
1.1 KiB
JavaScript
51 lines
1.1 KiB
JavaScript
import fs from "node:fs";
|
|
import { createRequire } from "node:module";
|
|
import { parentPort, Worker } from "node:worker_threads";
|
|
|
|
import { instantiateNapiModuleSync, MessageHandler } from "@emnapi/core";
|
|
import { WASI } from "@tybys/wasm-util";
|
|
|
|
const require = createRequire(import.meta.url);
|
|
|
|
if (parentPort) {
|
|
parentPort.on("message", (data) => {
|
|
globalThis.onmessage({ data });
|
|
});
|
|
}
|
|
|
|
Object.assign(globalThis, {
|
|
self: globalThis,
|
|
require,
|
|
Worker,
|
|
importScripts: function (f) {
|
|
;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f);
|
|
},
|
|
postMessage: function (msg) {
|
|
if (parentPort) {
|
|
parentPort.postMessage(msg);
|
|
}
|
|
},
|
|
});
|
|
|
|
const handler = new MessageHandler({
|
|
onLoad({ wasmModule, wasmMemory }) {
|
|
const wasi = new WASI({ fs });
|
|
|
|
return instantiateNapiModuleSync(wasmModule, {
|
|
childThread: true,
|
|
wasi,
|
|
overwriteImports(importObject) {
|
|
importObject.env = {
|
|
...importObject.env,
|
|
...importObject.napi,
|
|
...importObject.emnapi,
|
|
memory: wasmMemory
|
|
};
|
|
},
|
|
});
|
|
},
|
|
});
|
|
|
|
globalThis.onmessage = function (e) {
|
|
handler.handle(e);
|
|
};
|