-
Notifications
You must be signed in to change notification settings - Fork 0
/
trap.js
64 lines (46 loc) · 1.49 KB
/
trap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"use strict";
const path = require("path");
const fs = require("fs");
const asc = require("assemblyscript/cli/asc");
const loader = require("assemblyscript/lib/loader");
const wamem = require("./build/Release/wamem");
// https://www.assemblyscript.org/memory.html#internals
main();
async function main() {
await asc.ready;
wamem.setupTrap();
console.time("compileString");
const script = fs.readFileSync(path.join(__dirname, "trap.as"), "utf-8");
const { binary } = asc.compileString(script, {
optimize: 2,
exportRuntime: true,
importMemory: true,
memoryBase: 4096, // at COMPILE time
});
console.timeEnd("compileString");
const stdMem = new WebAssembly.Memory({
initial: 100,
maximum: 100,
});
const customMem = wamem.createMemory();
await runFromMemory(binary, stdMem);
await runFromMemory(binary, customMem);
}
async function runFromMemory(binary, memory) {
console.log("----------------------------");
const bufferSize = memory.buffer.byteLength;
console.log("bufferSize", bufferSize);
wamem.printArrayBufferBackingStoreFlags(memory.buffer);
console.time("instantiate");
const module = await loader.instantiate(binary, { env: { memory } });
console.timeEnd("instantiate");
const { testRead, testWrite, __getArrayView, __pin } = module.exports;
testWrite(10, 42);
console.log(testRead(10));
try {
testWrite(bufferSize + 1, 66);
} catch (err) {
console.error("CAUGHT", err);
}
console.log("----------------------------");
}