-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experiment: QuickJS lands in polyscript
- Loading branch information
1 parent
96a5e18
commit c292e6e
Showing
3 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>QuickJS</title> | ||
<script type="module"> | ||
import { define } from '../core.js'; | ||
define(null, { | ||
interpreter: 'quickjs-emscripten', | ||
onInterpreterReady({ run }) { | ||
console.log(run('{a: 123}')); | ||
} | ||
}); | ||
</script> | ||
</head> | ||
<body> | ||
<script type="quickjs-emscripten"> | ||
console.log({hello: "World"}); | ||
</script> | ||
</body> | ||
</html> |