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

context.evalCode(..., filename, { type: "module" }) returns module exports #154

Merged
merged 16 commits into from
Feb 19, 2024
Prev Previous commit
Next Next commit
fix quickjs-ng sync module exports
  • Loading branch information
justjake committed Feb 19, 2024
commit 58b2475b2fd819ee149bada63540987d9db4f205
4 changes: 2 additions & 2 deletions c/interface.c
Original file line number Diff line number Diff line change
@@ -727,11 +727,11 @@ MaybeAsync(JSValue *) QTS_Eval(JSContext *ctx, BorrowedHeapChar *js_code, size_t
if (
// quickjs@2024-01-14 evaluating module
// produced a promise
state == JS_PROMISE_FULFILLED
(state == JS_PROMISE_FULFILLED)
// quickjs in compile mode
// quickjs-ng before rebasing on quickjs@2024-01-14
// not a promise.
|| state < 0) {
|| (state == -1)) {
QTS_DEBUG("QTS_Eval: result: JS_PROMISE_FULFILLED or not a promise")
JS_FreeValue(ctx, eval_result);
return jsvalue_to_heap(JS_GetModuleNamespace(ctx, module));
63 changes: 37 additions & 26 deletions packages/quickjs-emscripten/src/quickjs.test.ts
Original file line number Diff line number Diff line change
@@ -468,31 +468,25 @@ function contextTests(getContext: GetTestContext, isDebug = false) {
const s = vm.getProp(modExports, "s").consume(vm.dump)
const n = vm.getProp(modExports, "n").consume(vm.dump)
const defaultExport = vm.getProp(modExports, "default").consume(vm.dump)
modExports.dispose()
assert.equal(s, "hello")
assert.equal(n, 42)
assert.equal(defaultExport, "the default")
const asJson = modExports.consume(vm.dump)
try {
assert.equal(s, "hello")
assert.equal(n, 42)
assert.equal(defaultExport, "the default")
} catch (error) {
console.log("Error with module exports:", asJson)
throw error
}
})

it("returns a promise of module exports", () => {
const log = vm.newFunction("log", (msg) => {
console.log(`vmlog: ${vm.getString(msg)}`)
})
vm.setProp(vm.global, "log", log)
log.dispose()

const promise = vm.unwrapResult(
vm.evalCode(
`
log('eval started');
await Promise.resolve(0);
log('await finished');
export const s = "hello";
log('exported s');
export const n = 42;
log('exported n');
export default "the default";
log('exported default');
`,
"mod.js",
{ type: "module" },
@@ -509,10 +503,15 @@ log('exported default');
const s = vm.getProp(modExports, "s").consume(vm.dump)
const n = vm.getProp(modExports, "n").consume(vm.dump)
const defaultExport = vm.getProp(modExports, "default").consume(vm.dump)
modExports.dispose()
assert.equal(s, "hello")
assert.equal(n, 42)
assert.equal(defaultExport, "the default")
const asJson = modExports.consume(vm.dump)
try {
assert.equal(s, "hello")
assert.equal(n, 42)
assert.equal(defaultExport, "the default")
} catch (error) {
console.log("Error with module exports:", asJson)
throw error
}
})
})

@@ -1216,13 +1215,25 @@ describe("QuickJSContext", function () {
}

if (TEST_NG) {
describe("quickjs-ng RELEASE_SYNC", function () {
const loader = memoizePromiseFactory(() =>
newQuickJSWASMModule(import("@jitl/quickjs-ng-wasmfile-release-sync")),
)
const getContext: GetTestContext = (opts) => loader().then((mod) => mod.newContext(opts))
contextTests(getContext)
})
if (TEST_RELEASE) {
describe("quickjs-ng RELEASE_SYNC", function () {
const loader = memoizePromiseFactory(() =>
newQuickJSWASMModule(import("@jitl/quickjs-ng-wasmfile-release-sync")),
)
const getContext: GetTestContext = (opts) => loader().then((mod) => mod.newContext(opts))
contextTests(getContext)
})
}

if (TEST_DEBUG) {
describe("quickjs-ng DEBUG_SYNC", function () {
const loader = memoizePromiseFactory(() =>
newQuickJSWASMModule(import("@jitl/quickjs-ng-wasmfile-debug-sync")),
)
const getContext: GetTestContext = (opts) => loader().then((mod) => mod.newContext(opts))
contextTests(getContext)
})
}
}
})

Loading