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

How to use quickjs-emscripten inside an ESM Service Worker? #222

Open
uttk opened this issue Feb 23, 2025 · 0 comments
Open

How to use quickjs-emscripten inside an ESM Service Worker? #222

uttk opened this issue Feb 23, 2025 · 0 comments

Comments

@uttk
Copy link

uttk commented Feb 23, 2025

I'm trying to use quickjs-emscripten inside a Service Worker that is written as an ECMAScript module (ESM).
However, it fails to execute due to the use of dynamic import() inside the library.

Since dynamic imports are not allowed in Service Workers, is there any workaround to make quickjs-emscripten work in this environment?

example:

// worker.js
import { getQuickJS } from 'https://esm.sh/[email protected]';

async function main() {
  const QuickJS = await getQuickJS()
  const vm = QuickJS.newContext()

  const world = vm.newString("world")
  vm.setProp(vm.global, "NAME", world)
  world.dispose()

  const result = vm.evalCode(`"Hello " + NAME + "!"`)

  if (result.error) {
    console.log("Execution failed:", vm.dump(result.error))
    result.error.dispose()
  } else {
    console.log("Success:", vm.dump(result.value))
    result.value.dispose()
  }

  vm.dispose();
}

main();
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Quick.js Example in Service Worker</title>
</head>
<body>
  <script>
    navigator.serviceWorker.register("/worker.js", { scope: "/", type: "module" });
  </script>  
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant