-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
68 lines (62 loc) · 1.76 KB
/
index.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
var fs = require("fs");
// Define imports. Just logs to console for the sake of this example.
var pson = {
onNull: function() {
console.log("null");
},
onTrue: function() {
console.log("true");
},
onFalse: function() {
console.log("false");
},
onEObject: function() {
console.log("{}");
},
onEArray: function() {
console.log("[]");
},
onEString: function() {
console.log("\"\"");
},
onObject: function(size) {
console.log("{" + size + "}")
},
onArray: function(size) {
console.log("[" + size + "]");
},
onInteger: function(value) {
console.log("integer: " + value);
},
onLong: function(valueLow, valueHigh) {
console.log("long: " + valueLow + ", " + valueHigh);
},
onFloat: function(value) {
console.log("float: " + value);
},
onDouble: function(value) {
console.log("double: " + value);
},
onString: function(offset, length) {
console.log("string(length=" + length + "): " + Buffer.from(mem.slice(offset, offset + length)).toString());
},
onBinary: function(offset, length) {
console.log("binary(length=" + length + "): " + mem.slice(offset, offset + length));
}
};
// Instantiate the module
var mod = new WebAssembly.Module(fs.readFileSync(__dirname + "/build/optimized.wasm"));
var ins = new WebAssembly.Instance(mod, { pson: pson });
var mem = new Uint8Array(ins.exports.memory.buffer);
// Export API
exports.decode = function(buffer) {
// grow memory if necessary
if (mem.length < buffer.length) {
ins.exports.memory.grow(Math.ceil((buffer.length - mem.length) / 65536));
mem = new Uint8Array(ins.exports.memory.buffer);
}
// copy buffer to memory
mem.set(buffer);
// start decoding (calls the imports defined above)
ins.exports.decode(buffer.length);
}