Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment: QuickJS lands in polyscript #57

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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});

Check failure on line 52 in esm/interpreter/quickjs-emscripten.js

View workflow job for this annotation

GitHub Actions / build (20)

A space is required after '{'

Check failure on line 52 in esm/interpreter/quickjs-emscripten.js

View workflow job for this annotation

GitHub Actions / build (20)

A space is required before '}'

Check failure on line 52 in esm/interpreter/quickjs-emscripten.js

View workflow job for this annotation

GitHub Actions / build (20)

A space is required after '{'

Check failure on line 52 in esm/interpreter/quickjs-emscripten.js

View workflow job for this annotation

GitHub Actions / build (20)

A space is required before '}'
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});

Check failure on line 61 in esm/interpreter/quickjs-emscripten.js

View workflow job for this annotation

GitHub Actions / build (20)

A space is required after '{'

Check failure on line 61 in esm/interpreter/quickjs-emscripten.js

View workflow job for this annotation

GitHub Actions / build (20)

A space is required before '}'

Check failure on line 61 in esm/interpreter/quickjs-emscripten.js

View workflow job for this annotation

GitHub Actions / build (20)

A space is required after '{'

Check failure on line 61 in esm/interpreter/quickjs-emscripten.js

View workflow job for this annotation

GitHub Actions / build (20)

A space is required before '}'
return interpreter.dump(interpreter.unwrapResult(result));
}
catch (error) {
io.get(interpreter).stderr(error);
}
},
runEvent(interpreter, code, event) {

Check failure on line 68 in esm/interpreter/quickjs-emscripten.js

View workflow job for this annotation

GitHub Actions / build (20)

'interpreter' is defined but never used

Check failure on line 68 in esm/interpreter/quickjs-emscripten.js

View workflow job for this annotation

GitHub Actions / build (20)

'code' is defined but never used

Check failure on line 68 in esm/interpreter/quickjs-emscripten.js

View workflow job for this annotation

GitHub Actions / build (20)

'event' is defined but never used

Check failure on line 68 in esm/interpreter/quickjs-emscripten.js

View workflow job for this annotation

GitHub Actions / build (20)

'interpreter' is defined but never used

Check failure on line 68 in esm/interpreter/quickjs-emscripten.js

View workflow job for this annotation

GitHub Actions / build (20)

'code' is defined but never used

Check failure on line 68 in esm/interpreter/quickjs-emscripten.js

View workflow job for this annotation

GitHub Actions / build (20)

'event' is defined but never used

},
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>
Loading