Skip to content

Commit

Permalink
Experiment: QuickJS lands in polyscript
Browse files Browse the repository at this point in the history
  • Loading branch information
WebReflection committed Oct 4, 2023
1 parent 96a5e18 commit c292e6e
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
74 changes: 74 additions & 0 deletions esm/interpreter/quickjs-emscripten.js
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 */
3 changes: 2 additions & 1 deletion esm/interpreters.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ const register = (interpreter) => {
//:RUNTIMES
import micropython from './interpreter/micropython.js';
import pyodide from './interpreter/pyodide.js';
import quickjs_emscripten from './interpreter/quickjs-emscripten.js';
import ruby_wasm_wasi from './interpreter/ruby-wasm-wasi.js';
import wasmoon from './interpreter/wasmoon.js';
for (const interpreter of [micropython, pyodide, ruby_wasm_wasi, wasmoon])
for (const interpreter of [micropython, pyodide, quickjs_emscripten, ruby_wasm_wasi, wasmoon])
register(interpreter);
22 changes: 22 additions & 0 deletions test/quickjs-emscripten.html
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>

0 comments on commit c292e6e

Please sign in to comment.