-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathqjs-wasm.js
More file actions
199 lines (164 loc) · 4.53 KB
/
qjs-wasm.js
File metadata and controls
199 lines (164 loc) · 4.53 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const kWasmModule = Symbol('kWasmModule');
const kWasmModuleRef = Symbol('kWasmModuleRef');
const kWasmExports = Symbol('kWasmExports');
const kWasmInstance = Symbol('kWasmInstance');
const kWasmInstances = Symbol('kWasmInstances');
const kWasiLinked = Symbol('kWasiLinked');
const kWasiStarted = Symbol('kWasiStarted');
const kWasiOptions = Symbol('kWasiOptions');
class CompileError extends Error {
constructor(...args) {
super(...args);
this.name = 'CompileError';
}
}
class LinkError extends Error {
constructor(...args) {
super(...args);
this.name = 'LinkError';
}
}
class RuntimeError extends Error {
constructor(...args) {
super(...args);
this.name = 'RuntimeError';
}
}
function getWasmError(e) {
switch (e.wasmError) {
case 'CompileError':
return new CompileError(e.message);
case 'LinkError':
return new LinkError(e.message);
case 'RuntimeError':
return new RuntimeError(e.message);
default:
return new TypeError(`Invalid WASM error: ${e.wasmError}`);
}
}
function callWasmFunction(name, ...args) {
const instance = this;
try {
return instance.callFunction(name, ...args);
} catch (e) {
if (e.wasmError) {
throw getWasmError(e);
} else {
throw e;
}
}
}
function buildInstance(mod) {
try {
return wasm.buildInstance(mod);
} catch (e) {
if (e.wasmError) {
throw getWasmError(e);
} else {
throw e;
}
}
}
function linkWasi(instance) {
try {
instance.linkWasi();
} catch (e) {
if (e.wasmError) {
throw getWasmError(e);
} else {
throw e;
}
}
}
function parseModule(buf) {
try {
return wasm.parseModule(buf);
} catch (e) {
if (e.wasmError) {
throw getWasmError(e);
} else {
throw e;
}
}
}
class Module {
constructor(buf) {
this[kWasmModule] = parseModule(buf);
}
static exports(module) {
return wasm.moduleExports(module[kWasmModule]);
}
// eslint-disable-next-line no-unused-vars
static imports(module) {
// TODO.
return {};
}
}
class Instance {
constructor(module, importObject = {}) {
const instance = buildInstance(module[kWasmModule]);
if (importObject.wasi_unstable) {
linkWasi(instance);
this[kWasiLinked] = true;
}
const _exports = Module.exports(module);
const exports = Object.create(null);
for (const item of _exports) {
if (item.kind === 'function') {
exports[item.name] = callWasmFunction.bind(instance, item.name);
}
}
this[kWasmInstance] = instance;
this[kWasmExports] = Object.freeze(exports);
this[kWasmModuleRef] = module;
globalThis.WebAssembly[kWasmInstances].push(this);
}
get exports() {
return this[kWasmExports];
}
}
class WASI {
wasiImport = 'w4s1'; // Doesn't matter right now.
constructor(options = {args: [], env: {}, preopens: {}}) {
this[kWasiStarted] = false;
if (options === null || typeof options !== 'object') {
throw new TypeError('options must be an object');
}
this[kWasiOptions] = JSON.parse(JSON.stringify(options));
}
start(instance) {
if (this[kWasiStarted]) {
throw new Error('WASI instance has already started');
}
if (!instance[kWasiLinked]) {
throw new Error('WASM instance doesn\'t have WASI linked');
}
if (!instance.exports._start) {
throw new TypeError('WASI entrypoint not found');
}
this[kWasiStarted] = true;
instance.exports._start(...(this[kWasiOptions].args ?? []));
}
}
class WebAssembly {
Module = Module;
Instance = Instance;
CompileError = CompileError;
LinkError = LinkError;
RuntimeError = RuntimeError;
WASI = WASI;
constructor() {
this[kWasmInstances] = [];
}
async compile(src) {
return new Module(src);
}
async instantiate(src, importObject) {
const module = await this.compile(src);
const instance = new Instance(module, importObject);
return {module, instance};
}
}
Object.defineProperty(globalThis, 'WebAssembly', {
enumerable: true, configurable: true, writable: true, value: new WebAssembly()
});