-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathquickjs-emscripten.js
74 lines (66 loc) · 2.7 KB
/
quickjs-emscripten.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
import { fetchFiles, fetchPaths, io, stdio, writeFileShim } from './_utils.js';
const type = 'quickjs-emscripten';
const moduleLoader = (name, interpreter) => {
return modules.get(interpreter).get(name).text;
};
const modules = new WeakMap;
// REQUIRES INTEGRATION TEST
/* c8 ignore start */
export default {
type,
module: (version = '0.0.2') =>
`https://cdn.jsdelivr.net/npm/@webreflection/quickjs-emscripten@${version}/index.js`,
async engine({ getQuickJS }, config) {
const QuickJS = await getQuickJS();
const { stderr, stdout, get } = stdio();
const interpreter = await get(QuickJS.newContext());
// TODO: this should not be needed if CLI flags are passed along
const console = interpreter.newObject();
const log = interpreter.newFunction('log', (...args) => {
for (const value of args)
interpreter.module.out(interpreter.dump(value));
});
interpreter.setProp(console, 'log', log);
interpreter.setProp(interpreter.global, 'console', console);
// TODO: these two are actually ignored completely
interpreter.module.stderr = stderr;
interpreter.module.stdout = stdout;
interpreter.runtime.setModuleLoader(moduleLoader);
modules.set(interpreter, new Map);
if (config.files) await fetchFiles(this, interpreter, config.files);
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
return interpreter;
},
registerJSModule(interpreter, name, value) {
// TODO: wrap all module things and create a better export per field
// considering the `default` as special.
// Value also cannot be just passed to setProp as it is.
const m = modules.get(interpreter);
const id = `__${name}`;
interpreter.setProp(interpreter.global, id, value);
m.set(name, { id, value, text: `export default ${id};` });
},
run(interpreter, code) {
try {
const result = interpreter.evalCode(code, {strict: true});
return interpreter.dump(interpreter.unwrapResult(result));
}
catch (error) {
io.get(interpreter).stderr(error);
}
},
async runAsync(interpreter, code) {
try {
const result = await interpreter.evalCodeAsync(code, {strict: true});
return interpreter.dump(interpreter.unwrapResult(result));
}
catch (error) {
io.get(interpreter).stderr(error);
}
},
runEvent(interpreter, code, event) {
},
transform: (_, value) => value,
writeFile: ({ module: { FS } }, path, buffer) => writeFileShim(FS, path, buffer),
};
/* c8 ignore stop */