justjake/quickjs-emscripten logo

justjake/quickjs-emscripten

Free

Javascript/Typescript bindings for QuickJS, a modern Javascript interpreter written in C by Fabrice Bellard.

FreeFree tier
Type
Open Source

About justjake/quickjs-emscripten

Javascript/Typescript bindings for QuickJS, a modern Javascript interpreter written in C by Fabrice Bellard, compiled to WebAssembly. It allows safely evaluating untrusted JavaScript (supports most of ES2023), creating and manipulating values inside the QuickJS runtime, exposing host functions to the QuickJS runtime, and executing synchronous code that uses asynchronous functions via asyncify. Supports browsers, NodeJS, Deno, Bun, Cloudflare Workers, and QuickJS (via quickjs-for-quickjs). Provides memory management with using statement, Scope, and Lifetime.consume(fn). Includes an async module loader and testing utilities.

Key Features

Safely evaluate untrusted JavaScript (supports most of ES2023)
Create and manipulate values inside the QuickJS runtime
Expose host functions to the QuickJS runtime
Execute synchronous code that uses asynchronous functions with asyncify
Supports browsers, NodeJS, Deno, Bun, Cloudflare Workers, and QuickJS itself
TypeScript bindings with full type definitions
Memory management using `using` statement, Scope, Lifetime.consume(fn)
Async module loader for dynamic imports
Provides evalCode, newContext, and other runtime methods

Pros & Cons

Pros
  • Open source and free to use
  • Supports modern JavaScript features up to ES2023
  • Cross-platform: works in browsers, Node.js, Deno, Bun, Cloudflare Workers
  • TypeScript-first with comprehensive type definitions
  • Enables safe execution of untrusted code without side effects
  • Asyncify allows using synchronous APIs for async tasks
Cons
  • Not a full Node.js environment (no native modules, no DOM)
  • Asyncify may introduce performance overhead
  • Requires WebAssembly support in the runtime

Best For

Safely evaluating untrusted JavaScript code in a sandboxed environmentEmbedding a JavaScript interpreter in web applications or serverless functionsRunning isolated scripts with custom host APIs and timeout controlsTesting JavaScript code in a controlled, lightweight runtimeExecuting synchronous code that relies on async operations via asyncify

FAQ

How do I install quickjs-emscripten?
Install via npm: npm install --save quickjs-emscripten or yarn add quickjs-emscripten.
How do I evaluate JavaScript code safely?
Call getQuickJS() to get a QuickJSWASMModule, then use evalCode() on a context. For example: const result = QuickJS.evalCode('1+1', { shouldInterrupt: shouldInterruptAfterDeadline(Date.now()+1000) }).
Can I use async functions inside QuickJS?
Yes, you can use asyncify to execute synchronous code that uses asynchronous functions. See the Asyncify section in the documentation.
Is quickjs-emscripten supported in browsers?
Yes, it supports browsers, NodeJS, Deno, Bun, and Cloudflare Workers.
How do I expose host functions to the QuickJS runtime?
Use the newContext() method and then define functions on the context by setting properties on the global object. See the 'Exposing APIs' documentation.