Using WASM on Workers to run isolated JS content

I am trying to make use of WASM with QuickJS to dynamically evaluate JS code in an isolated environment, though, I am getting the following error:
Aborted(both async and sync fetching of the wasm failed). Build with -sASSERTIONS for more info.
Aborted(both async and sync fetching of the wasm failed). Build with -sASSERTIONS for more info.
Please find a working minimal code in nodejs that makes use of QuickJS but fails when performed on a cloudflare worker:
import { getQuickJS } from "quickjs-emscripten";

const QuickJS = await getQuickJS();
const vm = QuickJS.newContext();

const result = vm.evalCode(`(() => ({ message: "Hello from QuickJS" }))()`);

if (result.error) {
console.error("Script failed:", result.error);
} else {
const value = result.value;
const native = vm.dump(value);
console.log("User script result:", native);
value.dispose();
}
import { getQuickJS } from "quickjs-emscripten";

const QuickJS = await getQuickJS();
const vm = QuickJS.newContext();

const result = vm.evalCode(`(() => ({ message: "Hello from QuickJS" }))()`);

if (result.error) {
console.error("Script failed:", result.error);
} else {
const value = result.value;
const native = vm.dump(value);
console.log("User script result:", native);
value.dispose();
}
I am making use of it in a worker like so:
import { getQuickJS } from 'quickjs-emscripten';

export default {
async fetch(request: Request, env: Env): Promise<Response> {
const QuickJS = await getQuickJS();
const vm = QuickJS.newContext();

const result = vm.evalCode(`(() => ({ message: "Hello from QuickJS" }))()`);

if (result.error) {
console.error('Script failed:', result.error);
} else {
const value = result.value;
const native = vm.dump(value);
console.log('User script result:', native);
value.dispose();
}
return new Response('', { status: 200 });
},
};
import { getQuickJS } from 'quickjs-emscripten';

export default {
async fetch(request: Request, env: Env): Promise<Response> {
const QuickJS = await getQuickJS();
const vm = QuickJS.newContext();

const result = vm.evalCode(`(() => ({ message: "Hello from QuickJS" }))()`);

if (result.error) {
console.error('Script failed:', result.error);
} else {
const value = result.value;
const native = vm.dump(value);
console.log('User script result:', native);
value.dispose();
}
return new Response('', { status: 200 });
},
};
Should I be enabling WASM somewhere in the console/wrangler.toml?
1 Reply
zalo
zalo4mo ago
It's a bit late, but you might benefit from this: https://github.com/cloudflare/cloudflare-docs/pull/23945/files

Did you find this page helpful?