-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapping.js
71 lines (51 loc) · 1.72 KB
/
mapping.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
65
66
67
68
69
70
71
"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();
const FILE = "/tmp/as-test";
async function main() {
await asc.ready;
wamem.setupTrap();
console.time("compileString");
const script = fs.readFileSync(path.join(__dirname, "mapping.as"), "utf-8");
console.timeEnd("compileString");
const reservation = 512 * 1024 * 1024;
const { binary } = asc.compileString(script, {
optimize: 2,
exportRuntime: true,
importMemory: true,
memoryBase: reservation, // at COMPILE time
});
const memory = wamem.createMemory(reservation);
const bufferSize = memory.buffer.byteLength;
console.time("instantiate");
const module = await loader.instantiate(binary, { env: { memory } });
console.timeEnd("instantiate");
const { testRead, testWrite } = module.exports;
{
const content = Buffer.from([0x10, 0x20, 0x30, 0x40, 0x50, 0x60]);
fs.writeFileSync(FILE, content);
const id = wamem.vmMapFile(FILE, 0x1000, 4096, false);
console.log("should be 0x20", testRead(0x1001));
try {
testWrite(0x1000, 42);
} catch (err) {
console.log("should fail", err);
}
wamem.vmUnmapFile(id);
}
{
const content = Buffer.from([0, 0, 0, 0, 0, 0]);
fs.writeFileSync(FILE, content);
const id = wamem.vmMapFile(FILE, 0x1000, 4096, true);
console.log("should be 0x0", testRead(0x1001));
testWrite(0x1002, 42);
wamem.vmUnmapFile(id);
const newContent = fs.readFileSync(FILE);
console.log("should be 0, 0, 42, 0, 0, 0", newContent);
}
}