-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_glue.js
70 lines (56 loc) · 1.85 KB
/
node_glue.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
const fs = require("fs");
// const readlineSync = require('readline-sync');
// const textEncoding = require('text-encoding');
var memory = new WebAssembly.Memory({ initial: 4 });
function log(offset) {
const location = new Uint32Array(new Uint8Array(memory.buffer, offset))[0];
// console.log("offset", location)
let bytes = new Uint8Array(memory.buffer, location);
const length = Number(bytes[0])
// console.log("length", length)
let s = ""
for (char of bytes.slice(1, length + 1)) {
s += String.fromCharCode(char)
}
console.log(s);
}
function intToString(offset, pointer) {
let int = new Int32Array(memory.buffer, offset)[0];
let memory = new Uint8Array(memory.buffer, pointer);
let str = int.toString()
memory[0] = str.length
for (let i = 1; i < str.length; i++) {
memory[i] = str[i]
}
}
function read(offset) {
const data = readlineSync.prompt()
const memAccess = new Uint8Array(memory.buffer);
let i = offset;
for (offset; i < data.length + offset; i++) {
memAccess[i] = data.charCodeAt(i-offset)
}
memAccess[i] = 0;
return data.length;
}
function dispInt(offset) {
let bytes = new Uint8Array(memory.buffer, offset);
let int = bytes[0];
int += bytes[1] * 256;
int += bytes[2] * 256 ** 2;
int += bytes[3] * 256 ** 3
console.log(">", int);
}
const js = { read: read, mem: memory, write: dispInt, toString: intToString }
const core = { memory }
async function run() {
//const stdFile = fs.readFileSync(".wasm");
//const stdModule = await WebAssembly.instantiate(stdFile, { core, js });
//const stdlib = stdModule.instance.exports;
fs.readFile("pascal.wasm", {}, (err, data) => {
WebAssembly.instantiate(data, { core, js }).then(wasm => {
wasm.instance.exports.__main__();
})
});
}
run();