-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathd8_main.js
More file actions
53 lines (45 loc) · 1.26 KB
/
d8_main.js
File metadata and controls
53 lines (45 loc) · 1.26 KB
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
load("d8_common.js");
function compile(filename) {
var status = new base.Status(function(message) {
print(message);
});
var parser = base.createParser(sources.grammar, status);
var text = read(filename);
var module = base.frontend(sources.systemWASM, filename, text, parser, status);
if (status.num_errors > 0) {
return null;
}
module = desugar.process(module);
var compiled = base.astToCompiledJS(module, sources.systemJS, {}, status);
if (status.num_errors > 0) {
return null;
}
// Generate binary encoding
var buffer = wasm_backend_v8.generate(module);
print("bytes:", new Uint8Array(buffer));
print("num bytes:", buffer.byteLength);
print();
// Instantiate
var foreign = {
sinF32: function(value) {
return Math.fround(Math.sin(value));
},
printI32: function(value) {
print("print", value);
},
flipBuffer: function(ptr) {
print("flip", ptr);
},
};
var instanceJS = compiled(foreign);
var instanceV8 = WASM.instantiateModule(buffer, foreign);
print("JS result:", instanceJS.main());
print("V8 result:", instanceV8.main());
}
if (arguments.length != 1) {
print("Usage: d8 d8_main.js -- file.wasm");
// TODO exit code.
} else {
var filename = arguments[0];
compile(filename);
}