Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/core/dylink/test/malloc/app.js
1393 views
1
const WASI = require("../../node_modules/wasi-js/dist/").default;
2
const bindings = require("../../node_modules/wasi-js/dist/bindings/node").default;
3
const importWebAssemblyDlopen = require("../../dist").default;
4
const { nonzeroPositions } = require("../../dist/util");
5
const { readFileSync } = require("fs");
6
const assert = require("assert");
7
8
function importWebAssemblySync(path, importObject) {
9
const binary = new Uint8Array(readFileSync(path));
10
const mod = new WebAssembly.Module(binary);
11
return new WebAssembly.Instance(mod, importObject);
12
}
13
14
const table = new WebAssembly.Table({ initial: 1500, element: "anyfunc" });
15
exports.table = table;
16
17
async function main() {
18
const memory = new WebAssembly.Memory({ initial: 100 });
19
const wasi = new WASI({ bindings });
20
const importObject = {
21
wasi_snapshot_preview1: wasi.wasiImport,
22
env: {
23
memory,
24
__indirect_function_table: table,
25
},
26
};
27
const instance = await importWebAssemblyDlopen({
28
path: "app.wasm",
29
importWebAssemblySync,
30
importObject,
31
stub: "silent",
32
readFileSync,
33
allowMainExports: true,
34
});
35
wasi.start(instance, memory);
36
exports.instance = instance;
37
exports.wasi = wasi;
38
}
39
40
main();
41
42