Does anyone have a working example of evaluating JavaScript via WASM?

(This is a spin-off from this thread, which ultimately asks a different question and has become quite lengthy.) I need to evaluate some JavaScript, but understand that eval() / new Function() is blocked on workers. I understand my only route to evalulation is via WASM. But I'm struggling to implement a working example of JS evaluation via WASM in Workers. I'm completely new to WASM and am focusing on packages that do the heavy lifting for you. Here's what I've tried, using quickjs-emscripten:
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,
})
console.log(result)
})
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,
})
console.log(result)
})
This throws no error but simply never gets into the callback. I've also tried wasm-jseval, via:
const { duktapeEval, quickjsEval } = require('wasm-jseval')
duktapeEval.getInstance().then(mod => {
console.log(mod.eval('1+1')) // 2
const add = mod.newFunction(['a', 'b'], 'return a+b')
console.log(add(1, 2)) // 3
})
const { duktapeEval, quickjsEval } = require('wasm-jseval')
duktapeEval.getInstance().then(mod => {
console.log(mod.eval('1+1')) // 2
const add = mod.newFunction(['a', 'b'], 'return a+b')
console.log(add(1, 2)) // 3
})
But this errors - it seems the package seems to think I'm working in the browser, as the error pertains to it trying to access location.href. So does anyone have a working cut-and-paste version of evaluating JS via a package like this I could use? I'd be most grateful!
2 Replies
Víctor Miranda
Víctor Miranda5mo ago
You was able to fix this ?
Mitya
Mitya5mo ago
No - I found a workaround. But I did get some responses which may help: - Package author response to my GH issue - SO answer
GitHub
getQuickJS() callback never resolves? · Issue #142 · justjake/qui...
Not sure what I'm doing wrong here. I'm trying to use this within a CloudFlare Worker. My code is taken from the demo: import { getQuickJS, shouldInterruptAfterDeadline } from "quickjs...
Stack Overflow
WASM/QuickJS - callback from getQuickJS() is never entered?
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