Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@jitl/quickjs-ng-wasmfile-release-asyncify

justjake24MIT0.31.0TypeScript support: included

Variant of quickjs library: Variant with separate .WASM file. Supports browser ESM, NodeJS ESM, and NodeJS CommonJS.

readme

quickjs-emscripten

Javascript/Typescript bindings for QuickJS, a modern Javascript interpreter, compiled to WebAssembly.

  • Safely evaluate untrusted Javascript (supports most of ES2023).
  • Create and manipulate values inside the QuickJS runtime (more).
  • Expose host functions to the QuickJS runtime (more).
  • Execute synchronous code that uses asynchronous functions, with asyncify.
  • Supports browsers, NodeJS, Deno, Bun, Cloudflare Workers, QuickJS (via quickjs-for-quickjs).

Github | NPM | API Documentation | Variants | Examples

import { getQuickJS } from "quickjs-emscripten"

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()

Usage

Install from npm: npm install --save quickjs-emscripten or yarn add quickjs-emscripten.

The root entrypoint of this library is the getQuickJS function, which returns a promise that resolves to a QuickJSWASMModule when the QuickJS WASM module is ready.

Once getQuickJS has been awaited at least once, you also can use the getQuickJSSync function to directly access the singleton in your synchronous code.

Safely evaluate Javascript code

See QuickJSWASMModule.evalCode

import { getQuickJS, shouldInterruptAfterDeadline } from "quickjs-emscripten"

getQuickJS().then((QuickJS) => {
  const result = QuickJS.evalCode("1 + 1", {
    shouldInterrupt: shouldInterruptAfterDeadline(Date.now() + 1000),
    memoryLimitBytes: 1024 * 1024,
  })
  console.log(result)
})

Interfacing with the interpreter

You can use QuickJSContext to build a scripting environment by modifying globals and exposing functions into the QuickJS interpreter.

Each QuickJSContext instance has its own environment -- globals, built-in classes -- and actions from one context won't leak into other contexts or runtimes (with one exception, see Asyncify).

Every context is created inside a QuickJSRuntime. A runtime represents a Javascript heap, and you can even share values between contexts in the same runtime.

const vm = QuickJS.newContext()
let state = 0

const fnHandle = vm.newFunction("nextId", () => {
  return vm.newNumber(++state)
})

vm.setProp(vm.global, "nextId", fnHandle)
fnHandle.dispose()

const nextId = vm.unwrapResult(vm.evalCode(`nextId(); nextId(); nextId()`))
console.log("vm result:", vm.getNumber(nextId), "native state:", state)

nextId.dispose()
vm.dispose()

When you create a context from a top-level API like in the example above, instead of by calling runtime.newContext(), a runtime is automatically created for the lifetime of the context, and disposed of when you dispose the context.

Runtime

The runtime has APIs for CPU and memory limits that apply to all contexts within the runtime in aggregate. You can also use the runtime to configure EcmaScript module loading.

const runtime = QuickJS.newRuntime()
// "Should be enough for everyone" -- attributed to B. Gates
runtime.setMemoryLimit(1024 * 640)
// Limit stack size
runtime.setMaxStackSize(1024 * 320)
// Interrupt computation after 1024 calls to the interrupt handler
let interruptCycles = 0
runtime.setInterruptHandler(() => ++interruptCycles > 1024)
// Toy module system that always returns the module name
// as the default export
runtime.setModuleLoader((moduleName) => `export default '${moduleName}'`)
const context = runtime.newContext()
const ok = context.evalCode(`
import fooName from './foo.js'
globalThis.result = fooName
`)
context.unwrapResult(ok).dispose()
// logs "foo.js"
console.log(context.getProp(context.global, "result").consume(context.dump))
context.dispose()
runtime.dispose()

EcmaScript Module Exports

When you evaluate code as an ES Module, the result will be a handle to the module's exports, or a handle to a promise that resolves to the module's exports if the module depends on a top-level await.

const context = QuickJS.newContext()
const result = context.evalCode(
  `
  export const name = 'Jake'
  export const favoriteBean = 'wax bean'
  export default 'potato'
`,
  "jake.js",
  { type: "module" },
)
const moduleExports = context.unwrapResult(result)
console.log(context.dump(moduleExports))
// -> { name: 'Jake', favoriteBean: 'wax bean', default: 'potato' }
moduleExports.dispose()

Memory Management

Many methods in this library return handles to memory allocated inside the WebAssembly heap. These types cannot be garbage-collected as usual in Javascript. Instead, you must manually manage their memory by calling a .dispose() method to free the underlying resources. Once a handle has been disposed, it cannot be used anymore. Note that in the example above, we call .dispose() on each handle once it is no longer needed.

Calling QuickJSContext.dispose() will throw a RuntimeError if you've forgotten to dispose any handles associated with that VM, so it's good practice to create a new VM instance for each of your tests, and to call vm.dispose() at the end of every test.

const vm = QuickJS.newContext()
const numberHandle = vm.newNumber(42)
// Note: numberHandle not disposed, so it leaks memory.
vm.dispose()
// throws RuntimeError: abort(Assertion failed: list_empty(&rt->gc_obj_list), at: quickjs/quickjs.c,1963,JS_FreeRuntime)

Here are some strategies to reduce the toil of calling .dispose() on each handle you create:

using statement

The using statement is a Stage 3 (as of 2023-12-29) proposal for Javascript that declares a constant variable and automatically calls the [Symbol.dispose]() method of an object when it goes out of scope. Read more in this Typescript release announcement. Here's the "Interfacing with the interpreter" example re-written using using:

using vm = QuickJS.newContext()
let state = 0

// The block here isn't needed for correctness, but it shows
// how to get a tighter bound on the lifetime of `fnHandle`.
{
  using fnHandle = vm.newFunction("nextId", () => {
    return vm.newNumber(++state)
  })

  vm.setProp(vm.global, "nextId", fnHandle)
  // fnHandle.dispose() is called automatically when the block exits
}

using nextId = vm.unwrapResult(vm.evalCode(`nextId(); nextId(); nextId()`))
console.log("vm result:", vm.getNumber(nextId), "native state:", state)
// nextId.dispose() is called automatically when the block exits
// vm.dispose() is called automatically when the block exits

Scope

A Scope instance manages a set of disposables and calls their .dispose() method in the reverse order in which they're added to the scope. Here's the "Interfacing with the interpreter" example re-written using Scope:

Scope.withScope((scope) => {
  const vm = scope.manage(QuickJS.newContext())
  let state = 0

  const fnHandle = scope.manage(
    vm.newFunction("nextId", () => {
      return vm.newNumber(++state)
    }),
  )

  vm.setProp(vm.global, "nextId", fnHandle)

  const nextId = scope.manage(vm.unwrapResult(vm.evalCode(`nextId(); nextId(); nextId()`)))
  console.log("vm result:", vm.getNumber(nextId), "native state:", state)

  // When the withScope block exits, it calls scope.dispose(), which in turn calls
  // the .dispose() methods of all the disposables managed by the scope.
})

You can also create Scope instances with new Scope() if you want to manage calling scope.dispose() yourself.

Lifetime.consume(fn)

Lifetime.consume is sugar for the common pattern of using a handle and then immediately disposing of it. Lifetime.consume takes a map function that produces a result of any type. The map fuction is called with the handle, then the handle is disposed, then the result is returned.

Here's the "Interfacing with interpreter" example re-written using .consume():

const vm = QuickJS.newContext()
let state = 0

vm.newFunction("nextId", () => {
  return vm.newNumber(++state)
}).consume((fnHandle) => vm.setProp(vm.global, "nextId", fnHandle))

vm.unwrapResult(vm.evalCode(`nextId(); nextId(); nextId()`)).consume((nextId) =>
  console.log("vm result:", vm.getNumber(nextId), "native state:", state),
)

vm.dispose()

Generally working with Scope leads to more straight-forward code, but Lifetime.consume can be handy sugar as part of a method call chain.

Exposing APIs

To add APIs inside the QuickJS environment, you'll need to create objects to define the shape of your API, and add properties and functions to those objects to allow code inside QuickJS to call code on the host. The newFunction documentation covers writing functions in detail.

By default, no host functionality is exposed to code running inside QuickJS.

const vm = QuickJS.newContext()
// `console.log`
const logHandle = vm.newFunction("log", (...args) => {
  const nativeArgs = args.map(vm.dump)
  console.log("QuickJS:", ...nativeArgs)
})
// Partially implement `console` object
const consoleHandle = vm.newObject()
vm.setProp(consoleHandle, "log", logHandle)
vm.setProp(vm.global, "console", consoleHandle)
consoleHandle.dispose()
logHandle.dispose()

vm.unwrapResult(vm.evalCode(`console.log("Hello from QuickJS!")`)).dispose()

Promises

To expose an asynchronous function that returns a promise to callers within QuickJS, your function can return the handle of a QuickJSDeferredPromise created via context.newPromise().

When you resolve a QuickJSDeferredPromise -- and generally whenever async behavior completes for the VM -- pending listeners inside QuickJS may not execute immediately. Your code needs to explicitly call runtime.executePendingJobs() to resume execution inside QuickJS. This API gives your code maximum control to schedule when QuickJS will block the host's event loop by resuming execution.

To work with QuickJS handles that contain a promise inside the environment, there are two options:

context.getPromiseState(handle)

You can synchronously peek into a QuickJS promise handle and get its state without introducing asynchronous host code, described by the type JSPromiseState:

type JSPromiseState =
  | { type: "pending"; error: Error }
  | { type: "fulfilled"; value: QuickJSHandle; notAPromise?: boolean }
  | { type: "rejected"; error: QuickJSHandle }

The result conforms to the SuccessOrFail type returned by context.evalCode, so you can use context.unwrapResult(context.getPromiseState(promiseHandle)) to assert a promise is settled successfully and retrieve its value. Calling context.unwrapResult on a pending or rejected promise will throw an error.

const promiseHandle = context.evalCode(`Promise.resolve(42)`)
const resultHandle = context.unwrapResult(context.getPromiseState(promiseHandle))
context.getNumber(resultHandle) === 42 // true
resultHandle.dispose()
promiseHandle.dispose()
context.resolvePromise(handle)

You can convert the QuickJSHandle into a native promise using context.resolvePromise(). Take care with this API to avoid 'deadlocks' where the host awaits a guest promise, but the guest cannot make progress until the host calls runtime.executePendingJobs(). The simplest way to avoid this kind of deadlock is to always schedule executePendingJobs after any promise is settled.

const vm = QuickJS.newContext()
const fakeFileSystem = new Map([["example.txt", "Example file content"]])

// Function that simulates reading data asynchronously
const readFileHandle = vm.newFunction("readFile", (pathHandle) => {
  const path = vm.getString(pathHandle)
  const promise = vm.newPromise()
  setTimeout(() => {
    const content = fakeFileSystem.get(path)
    promise.resolve(vm.newString(content || ""))
  }, 100)
  // IMPORTANT: Once you resolve an async action inside QuickJS,
  // call runtime.executePendingJobs() to run any code that was
  // waiting on the promise or callback.
  promise.settled.then(vm.runtime.executePendingJobs)
  return promise.handle
})
readFileHandle.consume((handle) => vm.setProp(vm.global, "readFile", handle))

// Evaluate code that uses `readFile`, which returns a promise
const result = vm.evalCode(`(async () => {
  const content = await readFile('example.txt')
  return content.toUpperCase()
})()`)
const promiseHandle = vm.unwrapResult(result)

// Convert the promise handle into a native promise and await it.
// If code like this deadlocks, make sure you are calling
// runtime.executePendingJobs appropriately.
const resolvedResult = await vm.resolvePromise(promiseHandle)
promiseHandle.dispose()
const resolvedHandle = vm.unwrapResult(resolvedResult)
console.log("Result:", vm.getString(resolvedHandle))
resolvedHandle.dispose()

Asyncify

Sometimes, we want to create a function that's synchronous from the perspective of QuickJS, but prefer to implement that function asynchronously in your host code. The most obvious use-case is for EcmaScript module loading. The underlying QuickJS C library expects the module loader function to return synchronously, but loading data synchronously in the browser or server is somewhere between "a bad idea" and "impossible". QuickJS also doesn't expose an API to "pause" the execution of a runtime, and adding such an API is tricky due to the VM's implementation.

As a work-around, we provide an alternate build of QuickJS processed by Emscripten/Binaryen's ASYNCIFY compiler transform. Here's how Emscripten's documentation describes Asyncify:

Asyncify lets synchronous C or C++ code interact with asynchronous [host] JavaScript. This allows things like:

  • A synchronous call in C that yields to the event loop, which allows browser events to be handled.
  • A synchronous call in C that waits for an asynchronous operation in [host] JS to complete.

Asyncify automatically transforms ... code into a form that can be paused and resumed ..., so that it is asynchronous (hence the name “Asyncify”) even though [it is written] in a normal synchronous way.

This means we can suspend an entire WebAssembly module (which could contain multiple runtimes and contexts) while our host Javascript loads data asynchronously, and then resume execution once the data load completes. This is a very handy superpower, but it comes with a couple of major limitations:

  1. An asyncified WebAssembly module can only suspend to wait for a single asynchronous call at a time. You may call back into a suspended WebAssembly module eg. to create a QuickJS value to return a result, but the system will crash if this call tries to suspend again. Take a look at Emscripten's documentation on reentrancy.

  2. Asyncified code is bigger and runs slower. The asyncified build of Quickjs-emscripten library is 1M, 2x larger than the 500K of the default version. There may be room for further optimization Of our build in the future.

To use asyncify features, use the following functions:

These functions are asynchronous because they always create a new underlying WebAssembly module so that each instance can suspend and resume independently, and instantiating a WebAssembly module is an async operation. This also adds substantial overhead compared to creating a runtime or context inside an existing module; if you only need to wait for a single async action at a time, you can create a single top-level module and create runtimes or contexts inside of it.

Async module loader

Here's an example of valuating a script that loads React asynchronously as an ES module. In our example, we're loading from the filesystem for reproducibility, but you can use this technique to load using fetch.

const module = await newQuickJSAsyncWASMModule()
const runtime = module.newRuntime()
const path = await import("path")
const { promises: fs } = await import("fs")

const importsPath = path.join(__dirname, "../examples/imports") + "/"
// Module loaders can return promises.
// Execution will suspend until the promise resolves.
runtime.setModuleLoader((moduleName) => {
  const modulePath = path.join(importsPath, moduleName)
  if (!modulePath.startsWith(importsPath)) {
    throw new Error("out of bounds")
  }
  console.log("loading", moduleName, "from", modulePath)
  return fs.readFile(modulePath, "utf-8")
})

// evalCodeAsync is required when execution may suspend.
const context = runtime.newContext()
const result = await context.evalCodeAsync(`
import * as React from 'esm.sh/react@17'
import * as ReactDOMServer from 'esm.sh/react-dom@17/server'
const e = React.createElement
globalThis.html = ReactDOMServer.renderToStaticMarkup(
  e('div', null, e('strong', null, 'Hello world!'))
)
`)
context.unwrapResult(result).dispose()
const html = context.getProp(context.global, "html").consume(context.getString)
console.log(html) // <div><strong>Hello world!</strong></div>
Async on host, sync in QuickJS

Here's an example of turning an async function into a sync function inside the VM.

const context = await newAsyncContext()
const path = await import("path")
const { promises: fs } = await import("fs")

const importsPath = path.join(__dirname, "../examples/imports") + "/"
const readFileHandle = context.newAsyncifiedFunction("readFile", async (pathHandle) => {
  const pathString = path.join(importsPath, context.getString(pathHandle))
  if (!pathString.startsWith(importsPath)) {
    throw new Error("out of bounds")
  }
  const data = await fs.readFile(pathString, "utf-8")
  return context.newString(data)
})
readFileHandle.consume((fn) => context.setProp(context.global, "readFile", fn))

// evalCodeAsync is required when execution may suspend.
const result = await context.evalCodeAsync(`
// Not a promise! Sync! vvvvvvvvvvvvvvvvvvvv 
const data = JSON.parse(readFile('data.json'))
data.map(x => x.toUpperCase()).join(' ')
`)
const upperCaseData = context.unwrapResult(result).consume(context.getString)
console.log(upperCaseData) // 'VERY USEFUL DATA'

Testing your code

This library is complicated to use, so please consider automated testing your implementation. We highly writing your test suite to run with both the "release" build variant of quickjs-emscripten, and also the DEBUG_SYNC build variant. The debug sync build variant has extra instrumentation code for detecting memory leaks.

The class TestQuickJSWASMModule exposes the memory leak detection API, although this API is only accurate when using DEBUG_SYNC variant. You can also enable debug logging to help diagnose failures.

// Define your test suite in a function, so that you can test against
// different module loaders.
function myTests(moduleLoader: () => Promise<QuickJSWASMModule>) {
  let QuickJS: TestQuickJSWASMModule
  beforeEach(async () => {
    // Get a unique TestQuickJSWASMModule instance for each test.
    const wasmModule = await moduleLoader()
    QuickJS = new TestQuickJSWASMModule(wasmModule)
  })
  afterEach(() => {
    // Assert that the test disposed all handles. The DEBUG_SYNC build
    // variant will show detailed traces for each leak.
    QuickJS.assertNoMemoryAllocated()
  })

  it("works well", () => {
    // TODO: write a test using QuickJS
    const context = QuickJS.newContext()
    context.unwrapResult(context.evalCode("1 + 1")).dispose()
    context.dispose()
  })
}

// Run the test suite against a matrix of module loaders.
describe("Check for memory leaks with QuickJS DEBUG build", () => {
  const moduleLoader = memoizePromiseFactory(() => newQuickJSWASMModule(DEBUG_SYNC))
  myTests(moduleLoader)
})

describe("Realistic test with QuickJS RELEASE build", () => {
  myTests(getQuickJS)
})

For more testing examples, please explore the typescript source of quickjs-emscripten repository.

Packaging

The main quickjs-emscripten package includes several build variants of the WebAssembly module:

  • RELEASE... build variants should be used in production. They offer better performance and smaller file size compared to DEBUG... build variants.
    • RELEASE_SYNC: This is the default variant used when you don't explicitly provide one. It offers the fastest performance and smallest file size.
    • RELEASE_ASYNC: The default variant if you need asyncify magic, which comes at a performance cost. See the asyncify docs for details.
  • DEBUG... build variants can be helpful during development and testing. They include source maps and assertions for catching bugs in your code. We recommend running your tests with both a debug build variant and the release build variant you'll use in production.
    • DEBUG_SYNC: Instrumented to detect memory leaks, in addition to assertions and source maps.
    • DEBUG_ASYNC: An asyncify variant with source maps.

To use a variant, call newQuickJSWASMModule or newQuickJSAsyncWASMModule with the variant object. These functions return a promise that resolves to a QuickJSWASMModule, the same as getQuickJS.

import {
  newQuickJSWASMModule,
  newQuickJSAsyncWASMModule,
  RELEASE_SYNC,
  DEBUG_SYNC,
  RELEASE_ASYNC,
  DEBUG_ASYNC,
} from "quickjs-emscripten"

const QuickJSReleaseSync = await newQuickJSWASMModule(RELEASE_SYNC)
const QuickJSDebugSync = await newQuickJSWASMModule(DEBUG_SYNC)
const QuickJSReleaseAsync = await newQuickJSAsyncWASMModule(RELEASE_ASYNC)
const QuickJSDebugAsync = await newQuickJSAsyncWASMModule(DEBUG_ASYNC)

for (const quickjs of [
  QuickJSReleaseSync,
  QuickJSDebugSync,
  QuickJSReleaseAsync,
  QuickJSDebugAsync,
]) {
  const vm = quickjs.newContext()
  const result = vm.unwrapResult(vm.evalCode("1 + 1")).consume(vm.getNumber)
  console.log(result)
  vm.dispose()
  quickjs.dispose()
}

Reducing package size

Including 4 different copies of the WebAssembly module in the main package gives it an install size of about 9.04mb. If you're building a CLI package or library of your own, or otherwise don't need to include 4 different variants in your node_modules, you can switch to the quickjs-emscripten-core package, which contains only the Javascript code for this library, and install one (or more) variants a-la-carte as separate packages.

The most minimal setup would be to install quickjs-emscripten-core and @jitl/quickjs-wasmfile-release-sync (1.3mb total):

yarn add quickjs-emscripten-core @jitl/quickjs-wasmfile-release-sync
du -h node_modules
# 640K    node_modules/@jitl/quickjs-wasmfile-release-sync
#  80K    node_modules/@jitl/quickjs-ffi-types
# 588K    node_modules/quickjs-emscripten-core
# 1.3M    node_modules

Then, you can use quickjs-emscripten-core's newQuickJSWASMModuleFromVariant to create a QuickJS module (see the minimal example):

// src/quickjs.mjs
import { newQuickJSWASMModuleFromVariant } from "quickjs-emscripten-core"
import RELEASE_SYNC from "@jitl/quickjs-wasmfile-release-sync"
export const QuickJS = await newQuickJSWASMModuleFromVariant(RELEASE_SYNC)

// src/app.mjs
import { QuickJS } from "./quickjs.mjs"
console.log(QuickJS.evalCode("1 + 1"))

See the documentation of quickjs-emscripten-core for more details and the list of variant packages.

WebAssembly loading

To run QuickJS, we need to load a WebAssembly module into the host Javascript runtime's memory (usually as an ArrayBuffer or TypedArray) and compile it to a WebAssembly.Module. This means we need to find the file path or URI of the WebAssembly module, and then read it using an API like fetch (browser) or fs.readFile (NodeJS). quickjs-emscripten tries to handle this automatically using patterns like new URL('./local-path', import.meta.url) that work in the browser or are handled automatically by bundlers, or __dirname in NodeJS, but you may need to configure this manually if these don't work in your environment, or you want more control about how the WebAssembly module is loaded.

To customize the loading of an existing variant, create a new variant with your loading settings using newVariant, passing CustomizeVariantOptions. For example, you need to customize loading in Cloudflare Workers (see the full example).

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

/**
 * We need to make a new variant that directly passes the imported WebAssembly.Module
 * to Emscripten. Normally we'd load the wasm file as bytes from a URL, but
 * that's forbidden in Cloudflare workers.
 */
const cloudflareVariant = newVariant(baseVariant, {
  wasmModule: cloudflareWasmModule,
  wasmSourceMapData: cloudflareWasmModuleSourceMap,
})

quickjs-ng

quickjs-ng/quickjs (aka quickjs-ng) is a fork of the original bellard/quickjs under active development. It implements more EcmaScript standards and removes some of quickjs's custom language features like BigFloat.

There are several variants of quickjs-ng available, and quickjs-emscripten may switch to using quickjs-ng by default in the future. See the list of variants.

Using in the browser without a build step

You can use quickjs-emscripten directly from an HTML file in two ways:

  1. Import it in an ES Module script tag

    <!doctype html>
    <!-- Import from a ES Module CDN -->
    <script type="module">
      import { getQuickJS } from "https://esm.sh/quickjs-emscripten@0.25.0"
      const QuickJS = await getQuickJS()
      console.log(QuickJS.evalCode("1+1"))
    </script>
  2. In edge cases, you might want to use the IIFE build which provides QuickJS as the global QJS. You should probably use the ES module though, any recent browser supports it.

    <!doctype html>
    <!-- Add a script tag to load the library as the QJS global -->
    <script
      src="https://cdn.jsdelivr.net/npm/quickjs-emscripten@0.25.0/dist/index.global.js"
      type="text/javascript"
    ></script>
    <!-- Then use the QJS global in a script tag -->
    <script type="text/javascript">
      QJS.getQuickJS().then((QuickJS) => {
        console.log(QuickJS.evalCode("1+1"))
      })
    </script>

Debugging

Debug logging can be enabled globally, or for specific runtimes. You need to use a DEBUG build variant of the WebAssembly module to see debug log messages from the C part of this library.

import { newQuickJSWASMModule, DEBUG_SYNC } from "quickjs-emscripten"

const QuickJS = await newQuickJSWASMModule(DEBUG_SYNC)

With quickjs-emscripten-core:

import { newQuickJSWASMModuleFromVariant } from "quickjs-emscripten-core"
import DEBUG_SYNC from "@jitl/quickjs-wasmfile-debug-sync"

const QuickJS = await newQuickJSWASMModuleFromVariant(DEBUG_SYNC)

To enable debug logging globally, call setDebugMode. This affects global Javascript parts of the library, like the module loader and asyncify internals, and is inherited by runtimes created after the call.

import { setDebugMode } from "quickjs-emscripten"

setDebugMode(true)

With quickjs-emscripten-core:

import { setDebugMode } from "quickjs-emscripten-core"

setDebugMode(true)

To enable debug logging for a specific runtime, call setDebugModeRt. This affects only the runtime and its associated contexts.

const runtime = QuickJS.newRuntime()
runtime.setDebugMode(true)

const context = QuickJS.newContext()
context.runtime.setDebugMode(true)

Supported Platforms

quickjs-emscripten and related packages should work in any environment that supports ES2020.

More Documentation

Github | NPM | API Documentation | Variants | Examples

Background

This was inspired by seeing https://github.com/maple3142/duktape-eval on Hacker News and Figma's blogposts about using building a Javascript plugin runtime:

Status & Roadmap

Stability: Because the version number of this project is below 1.0.0, *expect occasional breaking API changes.

Security: This project makes every effort to be secure, but has not been audited. Please use with care in production settings.

Roadmap: I work on this project in my free time, for fun. Here's I'm thinking comes next. Last updated 2022-03-18.

  1. Further work on module loading APIs:

    • Create modules via Javascript, instead of source text.
    • Scan source text for imports, for ahead of time or concurrent loading. (This is possible with third-party tools, so lower priority.)
  2. Higher-level tools for reading QuickJS values:

    • Type guard functions: context.isArray(handle), context.isPromise(handle), etc.
    • Iteration utilities: context.getIterable(handle), context.iterateObjectEntries(handle). This better supports user-level code to deserialize complex handle objects.
  3. Higher-level tools for creating QuickJS values:

    • Devise a way to avoid needing to mess around with handles when setting up the environment.
    • Consider integrating quickjs-emscripten-sync for automatic translation.
    • Consider class-based or interface-type-based marshalling.
  4. SQLite integration.

Developing

This library is implemented in two languages: C (compiled to WASM with Emscripten), and Typescript.

You will need node, yarn, make, and emscripten to build this project.

The C parts

The ./c directory contains C code that wraps the QuickJS C library (in ./quickjs). Public functions (those starting with QTS_) in ./c/interface.c are automatically exported to native code (via a generated header) and to Typescript (via a generated FFI class). See ./generate.ts for how this works.

The C code builds with emscripten (using emcc), to produce WebAssembly. The version of Emscripten used by the project is defined in templates/Variant.mk.

  • On ARM64, you should install emscripten on your machine. For example on macOS, brew install emscripten.
  • If the correct version of emcc is not in your PATH, compilation falls back to using Docker. On ARM64, this is 10-50x slower than native compilation, but it's just fine on x64.

We produce multiple build variants of the C code compiled to WebAssembly using a template script the ./packages directory. Each build variant uses its own copy of a Makefile to build the C code. The Makefile is generated from a template in ./templates/Variant.mk.

Related NPM scripts:

  • yarn vendor:update updates vendor/quickjs and vendor/quickjs-ng to the latest versions on Github.
  • yarn build:codegen updates the ./packages from the template script ./prepareVariants.ts and Variant.mk.
  • yarn build:packages builds the variant packages in parallel.

The Typescript parts

The Javascript/Typescript code is also organized into several NPM packages in ./packages:

  • ./packages/quickjs-ffi-types: Low-level types that define the FFI interface to the C code. Each variant exposes an API conforming to these types that's consumed by the higher-level library.
  • ./packages/quickjs-emscripten-core: The higher-level Typescript that implements the user-facing abstractions of the library. This package doesn't link directly to the WebAssembly/C code; callers must provide a build variant.
  • ./packages/quicks-emscripten: The main entrypoint of the library, which provides the getQuickJS function. This package combines quickjs-emscripten-core with platform-appropriate WebAssembly/C code.

Related NPM scripts:

  • yarn check runs all available checks (build, format, tests, etc).
  • yarn build builds all the packages and generates the docs.
  • yarn test runs the tests for all packages.
    • yarn test:fast runs the tests using only fast build variants.
  • yarn doc generates the docs into ./doc.
    • yarn doc:serve previews the current ./doc in a browser.
  • yarn prettier formats the repo.

Yarn updates

Just run yarn set version from sources to upgrade the Yarn release.

changelog

Changelog

v0.31.0

  • #137
    • Add quickjs-for-quickjs, a package that can run inside quickjs, so you can put quickjs inside your quickjs.
    • Possibly breaking: Fix a build system bug that made commonjs or esm variants include both types, thus being larger than they needed to be. After upgrading a variant to this release, you should verify that it can be imported/required as expected. You may need to add additional variants if you were using an "esm" variant from both cjs and esm.

v0.30.0

  • #200 Inspect and iterate handles, equality, changes to result types, changes to debug logging.
  • #195 Export setDebugMode

Collection & Iteration

  • For objects and arrays: add context.getOwnPropertyNames(handle, options) to iterate the key or array index handles.
  • For arrays: add context.getLength(handle) which reads handle.length and returns it as a number or undefined to make writing for (i=0;i<length;i++) loops easier.
  • For iterable collections like Map, Set, Array: add context.getIterator(handle) calls handle[Symbol.iterator]() and then exposes the result as an IterableIterator to host javascript.

Usability improvements

  • The SuccessOrFail<T, QuickJSHandle> return type is largely replaced with a new return type DisposableSuccess<T> | DisposableFail<QuickJSHandle>. The new type implements result.unwrap() as a replacement for context.unwrapResult(result). It also implements dispose() directly, so you no longer need to distinguish between success and failure to clean up.
  • add context.callMethod(handle, 'methodName'), this makes it easier to call methods like context.callMethod(handle, 'keys') or context.callMethod('values') which can be used with the new iterator.

Equality

  • Added context.eq(a, b), context.sameValue(a, b), context.sameValueZero(a, b)

Debug logging changes

Debug logging is now disabled by default, even when using a DEBUG variant. It can be enabled on a runtime-by-runtime basis with runtime.setDebugMode(boolean) or context.runtime.setDebugMode(boolean), or globally using setDebugMode(boolean). As with before, you should use a DEBUG variant to see logs from the WebAssembly C code.

v0.29.2

  • #179 Add a work-around for a bug in Webkit ARM to quickjs build variants. quickjs-ng is still affected by this bug.

v0.29.1

  • #161 Fix a bug where context.evalCode(..., { type: 'module' }) would return success when some kinds of error occurred when using quickjs variants.
    • Also adds context.getPromiseState(handle) to resolve promises synchronously.

v0.29.0

  • #154 ESModule exports
    • context.evalCode(code, filename, { type: "module" }) or when code is detected to be a module: now returns a handle to the module's exports, or a promise handle that resolves to the module's exports.
    • Added context.getPromiseState(handle) which returns the state of a promise handle, and can be unwrapped with context.unwrapResult(promiseState).
  • #159 add LICENSE file and SPDX license identifiers to all packages.

v0.28.0

  • #155 Update library versions and add versions to documentation.
    • quickjs version 2024-01-13+229b07b9 vendored to quickjs-emscripten on 2024-02-11.
      • Evaluating a ES Module with context.evalCode(...) now returns a Promise object instead of undefined.
    • quickjs-ng version git+229b07b9 vendored to quickjs-emscripten on 2024-02-11.

v0.27.0

  • #147 Support providing and retrieving WebAssembly.Memory
    • Fixes #146 by adding wasmMemory: WebAssembly.Memory option for newVariant, and mod.getWasmMemory() method for QuickJS[Async]WASMModule.
    • Fixes #138 by
      • removing internal use of using statement.
      • Use ESBuild Symbol.for('Symbol.dispose') if Symbol.dispose isn't defined globally.

v0.26.0

  • #136, #116 (thanks to @GrantMatejka) Expose ability to configure Context's intrinsic objects.
  • #135 (thanks to @saghul) Add quickjs-ng variants. quickjs-ng is a fork of quickjs under active development. It implements more EcmaScript standards and removes some of quickjs's custom language features like BigFloat.
  • #134 Support using statement for Disposable. If you using value = vm.unwrapResult(vm.evalCode("1+1")), the value will be automatically disposed when the scope exits.
  • #133 WebAssembly loading options & Cloudflare Worker support. Added an example of using quickjs-emscripten in a Cloudflare Worker.

v0.25.1

  • #130 Fix some README and docs quibbles.

v0.25.0

  • #129 Improve packaging strategy, native ES Modules, and browser-first builds.

New package layout

quickjs-emscripten is re-organized into several NPM packages in a monorepo structure:

  • quickjs-emscripten (install size 7.5mb) - mostly backwards compatible all-in-one package. This package installs the WASM files for DEBUG_SYNC, DEBUG_ASYNC, RELEASE_SYNC and RELEASE_ASYNC variants as NPM dependencies. Its total install size is reduced from ~
  • quickjs-emscripten-core (install size 573kb) - just the Typescript code, should be compatible with almost any runtime. You can use this with a single a-la-carte build variant, such as @jitl/quickjs-wasmfile-release-sync to reduce the total install size needed to run QuickJS to ~1.1mb.
  • Several a-la-carte build variants, named @jitl/quickjs-{wasmfile,singlefile-{esm,cjs,browser}}-{debug,release}-{sync,asyncify}. See the quickjs-emscripten-core docs for more details.

ESModules & Browser support

quickjs-emscripten uses package.json export conditions to provide native ES Modules for NodeJS and the browser when appropriate. Most bundlers, like Webpack@5 and Vite, will understand conditions and work out of the box. This should enable advanced tree-shaking, although I'm not sure how much benefit is possible with the library.

You can also use quickjs-emscripten directly from an HTML file in two ways:

<!doctype html>
<!-- Import from a ES Module CDN -->
<script type="module">
  import { getQuickJS } from "https://esm.run/quickjs-emscripten@0.25.0"
  const QuickJS = await getQuickJS()
  console.log(QuickJS.evalCode("1+1"))
</script>

In edge cases, you might want to use the IIFE build which provides QuickJS as the global QJS.

<!doctype html>
<!-- Add a script tag to load the library as the QJS global -->
<script
  src="https://cdn.jsdelivr.net/npm/quickjs-emscripten@0.25.0-rc.11/dist/index.global.js"
  type="text/javascript"
></script>
<!-- Then use the QJS global in a script tag -->
<script type="text/javascript">
  QJS.getQuickJS().then((QuickJS) => {
    console.log(QuickJS.evalCode("1+1"))
  })
</script>

Breaking Changes

  • We now differentiate between runtime environments at build time using Node.JS package.json export conditions in subpath exports, instead of including support for all environments in a single build. While this resolves open issues with relatively modern bundlers like webpack@5 and vite, it may cause regressions if you use an older bundler that doesn't understand the "browser" condition.
  • The release variants - RELEASE_SYNC (the default), and RELEASE_ASYNC - no longer embed the WebAssembly code inside a JS file. Instead, they attempt to load the WebAssembly code from a separate file. Very old bundlers may not understand this syntax, or your web server may not serve the .wasm files correctly. Please test in a staging environment to verify your production build works as expected.
  • quickjs-emscripten now uses subpath exports in package.json. Imports of specific files must be removed. The only valid import paths for quickjs-emscripten are:

    • import * from 'quickjs-emscripten' (the library)
    • import packageJson from 'quickjs-emscripten/package.json'
    • import { DEBUG_SYNC, DEBUG_ASYNC, RELEASE_SYNC, RELEASE_ASYNC } from 'quickjs-emscripten/variants' (these are exported from the main import, but are also available as their own files)

    You should update your imports to use one of these paths. Notably, the WASM files can no longer be directly imported from the quickjs-emscripten package.

  • If you have errors about missing type files, you may need to upgrade to typescript@5 and set moduleResolution to node16 in your tsconfig.json. This shouldn't be necessary for most users.

v0.24.0

  • #127 Upgrade to quickjs 2023-12-09:

    • added Object.hasOwn, {String|Array|TypedArray}.prototype.at, {Array|TypedArray}.prototype.findLast{Index}
    • BigInt support is enabled even if CONFIG_BIGNUM disabled
    • updated to Unicode 15.0.0
    • misc bug fixes
  • #125 (thanks to @tbrockman):

    • Synchronizes quickjs to include the recent commit to address CVE-2023-31922.
  • #111 (thanks to @yourWaifu) ArrayBuffer and binary json encoding:

    • context.newArrayBuffer(bufferLike) creates an ArrayBuffer in the VM from a buffer-like object.
    • context.getArrayBuffer(handle) creates a view on an ArrayBuffer in the VM as a UInt8Array on the host.
    • context.encodeBinaryJSON(handle) encodes a QuickJS handle in QuickJS's binary format (like JSON.stringify)
    • context.decodeBinaryJSON(handle) decodes a QuickJS handle containing the binary format (like JSON.parse)

v0.23.0

  • #114 (thanks to @yar2001) improve stack size for ASYNCIFY build variants:
    • Change the default ASYNCIFY_STACK_SIZE from 4096 bytes to 81920 bytes. This equates to an increase from approximately 12 to 297 function frames. See the PR for more details.
    • QuickJSAsyncRuntime.setMaxStackSize(stackSizeBytes) now also adjusts the ASYNCIFY_STACK_SIZE for the entire module.

v0.22.0

  • #78, #105 (thanks to @ayaboy) add Symbol helpers context.newUniqueSymbol, context.newSymbolFor, as well as support for symbols in context.dump.
  • #104 BigInt support.
  • #100 Breaking change upgrade Emscripten version and switch to async import(...) for loading variants. We also drop support for older browsers and Node versions:

    • Node >= 16 is required
    • Safari >= 14.1 is required
    • Typescript >= 4.7 is recommended, but not required.

v0.21.2

  • #94 (thanks to @swimmadude66) allows QuickJS to create many more functions before overflowing.

v0.21.1

  • #66 (thanks to @BickelLukas) fixes ReferenceError when running in browser due to global.process

v0.21.0

  • #61 (thanks to @torywheelwright):
    • Add QuickJSRuntime.setMaxStackSize(stackSizeBytes) to protect against stack overflows.
    • Add option maxStackSizeBytes when creating runtimes.
  • Change default branch master to main.
  • Add option memoryLimitBytes when creating runtimes.

v0.20.0

This is a large release! The summary is:

  • There are several breaking API changes to align our abstractions with the underlying QuickJS library.
  • There's a new build variant build with Emscripten's ASYNCIFY that allows synchronous code inside QuickJS to use asynchronous code running on the host.
  • Both build variants have basic support for loading and evaluating EcmaScript modules, but only the ASYNCIFY variant can asynchronously load EcmaScript code.

New features

This release introduces class QuickJSRuntime. This class wraps QuickJS's JSRuntime* type:

JSRuntime represents a Javascript runtime corresponding to an object heap. Several runtimes can exist at the same time but they cannot exchange objects. Inside a given runtime, no multi-threading is supported.

  • QuickJSRuntime.newContext creates a new context inside an existing runtime.
  • QuickJSRuntime.setModuleLoader enables EcmaScript module loading.

This release renames QuickJSVm to class QuickJSContext, and removes some methods. The new class wraps QuickJS's JSContext* type:

JSContext represents a Javascript context (or Realm). Each JSContext has its own global objects and system objects. There can be several JSContexts per JSRuntime [...], similar to frames of the same origin sharing Javascript objects in a web browser.

QuickJSContext replaces QuickJSVm as the main way to interact with the environment inside the QuickJS virtual machine.

  • QuickJSContext.runtime provides access to a context's parent runtime.
  • QuickJSContext.evalCode now takes an options argument to set eval mode to 'module'.

There are also Asyncified versions of both QuickJSRuntime (QuickJSRuntimeAsync) and QuickJSContext (QuickJSContextAsync). These variants trade some runtime performance for additional features.

  • QuickJSRuntimeAsync.setModuleLoader accepts module loaders that return Promise<string>.
  • QuickJSContextAsync.newAsyncifiedFunction allows creating async functions that act like sync functions inside the VM.

Breaking changes

class QuickJSVm is removed. Most functionality is available on QuickJSContext, with identical function signatures. Functionality related to runtime limits, memory limits, and pending jobs moved to QuickJSRuntime.

Migration guide:

  1. Replace usages with QuickJSContext.
  2. Replace the following methods:
    • vm.hasPendingJob -> context.runtime.hasPendingJob
    • vm.setInterruptHandler -> context.runtime.setInterruptHandler
    • vm.removeInterruptHandler -> context.runtime.removeInterruptHandler
    • vm.executePendingJobs -> context.runtime.executePendingJobs
    • vm.setMemoryLimit -> context.runtime.setMemoryLimit
    • vm.computeMemoryUsage -> context.runtime.computeMemoryUsage

QuickJS.createVm() is removed.

Migration guide: use QuickJS.newContext() instead.

v0.15.0

This is the last version containing class QuickJSVm and constructor QuickJS.createVm().