Skip to content

Commit

Permalink
fix quickjs-ng sync module exports
Browse files Browse the repository at this point in the history
  • Loading branch information
justjake committed Feb 19, 2024
1 parent 6093e5f commit 58b2475
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
4 changes: 2 additions & 2 deletions c/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
63 changes: 37 additions & 26 deletions packages/quickjs-emscripten/src/quickjs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand All @@ -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
}
})
})

Expand Down Expand Up @@ -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)
})
}
}
})

Expand Down

0 comments on commit 58b2475

Please sign in to comment.