WASM/QuickJS - callback from getQuickJS() is never entered?

119 views Asked by At

I'm using quickjs-emscripten to try to evaluate some JS within a Cloudflare Worker (i.e. serverless). I know the pitfalls of JS evaluation and don't need to debate that here.

The docs say this should work:

import { getQuickJS, shouldInterruptAfterDeadline } from "quickjs-emscripten"
getQuickJS().then((QuickJS) => {
    console.log('start'); //<-- never happens
    const result = QuickJS.evalCode("1 + 1", {
        shouldInterrupt: shouldInterruptAfterDeadline(Date.now() + 1000),
        memoryLimitBytes: 1024 * 1024,
    })
})

...but the callback is never entered and no error is thrown (tried wrapping it in a try-catch).

Anyone idea what could be wrong?

1

There are 1 answers

1
DeveloperMindset.com On

Okay, it's been fun to do a deep dive on this!

You're looking to do a custom WASM loader for the Emscripten.

import { shouldInterruptAfterDeadline } from "quickjs-emscripten";

import type { QuickJSWASMModule } from "quickjs-emscripten";
import {
  newQuickJSWASMModule,
  DEBUG_SYNC as baseVariant,
  newVariant,
} from "quickjs-emscripten";
import cloudflareWasmModule from "./DEBUG_SYNC.wasm";
import cloudflareWasmModuleSourceMap from "./DEBUG_SYNC.wasm.map.txt";

const cloudflareVariant = newVariant(baseVariant, {
  wasmModule: cloudflareWasmModule,
  wasmSourceMapData: cloudflareWasmModuleSourceMap,
});

let QuickJS: QuickJSWASMModule | undefined;

and then later inside your addEventListener

QuickJS = await newQuickJSWASMModule(cloudflareVariant);

I covered my research in a post here.