-
Notifications
You must be signed in to change notification settings - Fork 90
/
index.js
86 lines (72 loc) · 2.09 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// @flow
const { parse } = require("@webassemblyjs/wast-parser");
const { decode } = require("@webassemblyjs/wasm-parser");
const { Instance } = require("./interpreter");
const { Memory } = require("./interpreter/runtime/values/memory");
const { Table } = require("./interpreter/runtime/values/table");
const { RuntimeError, CompileError, LinkError } = require("./errors");
const { createCompiledModule, Module } = require("./compiler/compile/module");
const { checkEndianness } = require("./check-endianness");
const WebAssembly = {
instantiate(
buff: ArrayBuffer,
importObject: ImportObject = {}
): Promise<InstansitatedInstanceAndModule> {
return new Promise((resolve, reject) => {
if (checkEndianness() === false) {
return reject(
new RuntimeError("expected the system to be little-endian")
);
}
if (
buff instanceof ArrayBuffer === false &&
buff instanceof Uint8Array === false
) {
return reject(
"Module must be either an ArrayBuffer or an Uint8Array (BufferSource), " +
typeof buff +
" given."
);
}
const ast = decode(buff);
const module = createCompiledModule(ast);
const instance = new Instance(module, importObject);
resolve({
instance,
module
});
});
},
compile(buff: ArrayBuffer): Promise<CompiledModule> {
return new Promise(resolve => {
const ast = decode(buff);
resolve(createCompiledModule(ast));
});
},
validate(buff: ArrayBuffer): boolean {
try {
createCompiledModule(decode(buff));
return true;
} catch (e) {
return false;
}
},
// FIXME(sven): remove this
instantiateFromSource(
content: string,
importObject: ImportObject = {}
): Instance {
const ast = parse(content);
const module = createCompiledModule(ast);
console.warn("using deprecated instantiateFromSource");
return new Instance(module, importObject);
},
Instance,
Module,
Memory,
Table,
RuntimeError,
LinkError,
CompileError
};
module.exports = WebAssembly;