Giter Site home page Giter Site logo

justjake / quickjs-emscripten Goto Github PK

View Code? Open in Web Editor NEW
1.1K 15.0 87.0 37.86 MB

Safely execute untrusted Javascript in your Javascript, and execute synchronous code that uses async functions

Home Page: https://www.npmjs.com/package/quickjs-emscripten

License: Other

Makefile 18.09% C 4.62% JavaScript 0.62% Shell 0.16% TypeScript 76.51%
quickjs eval javascript wasm plugins

quickjs-emscripten's Introduction

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.

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.

// 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/[email protected]"
      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/[email protected]/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

  • Switch to 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)
  • Enable debug log messages from the Javascript part of this library with setDebugMode:

    import { setDebugMode } from "quickjs-emscripten"
    
    setDebugMode(true)

    With quickjs-emscripten-core:

    import { setDebugMode } from "quickjs-emscripten-core"
    
    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.

Related

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.

quickjs-emscripten's People

Contributors

arcanis avatar atifsyedali avatar avi avatar bickellukas avatar dependabot[bot] avatar flaque avatar justjake avatar ldarren avatar namuol avatar swimmadude66 avatar tbrockman avatar torywheelwright avatar yar2001 avatar yourwaifu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

quickjs-emscripten's Issues

Exposing a handle for a function inside the VM to the host

I'm trying to use this library for building a secure plugins system, and one of the important things that plugins should be able to do is pass callbacks to the host system, the problem is as far as I'm understanding there's currently only basic support for passing JSON-serializable values back and forth, and for passing functions from the host to the vm, but not the other way around.

What would the best way to implement something like that given the provided APIs?

I'm thinking perhaps functions could be serialized into something like { __uniqueFnId: 123 }, which then could be passed successfully to the host, and the host could ask the guest to execute that function, which the guest should be able to resolve. It sounds hacky though, is there maybe a much better way to do this kind of things currently?

[library] quickjs-emscripten-sync - easily pass objects between host and guest

Thank you for creating this wonderful library.

I have developed "quickjs-emscripten-sync" to facilitate the passing of objects between QuickJS VMs and hosts.

https://github.com/reearth/quickjs-emscripten-sync

It is using a limited API to exchange objects, which may not be efficient, but It is currently able to pass objects smoothly.

In the TODO of quickjs-emscripten, it says that they are planning to support binding of classes and other objects. This implementation may be useful at that time.

Illegal invocation error with quickjs-emecripten-sync

Hi

I'm trying something very naughty:

arena.expose({ mydiv: document.createElement("div") });

and then

console.log(arena.evalCode('() => mydiv.tagName')());

but am left with this:

quickjs.js:651 Uncaught (in promise) TypeError: Illegal invocation
    at QuickJSVm.unwrapResult (quickjs.js:651)
    at eval (quickjs-emscripten-sync.modern.js:18)
    at h (quickjs-emscripten-sync.modern.js:18)
    at eval (quickjs-emscripten-sync.modern.js:18)
    at eval (app.tsx:55)

I guess I'm violating some rule that maybe is documented somewhere? :)

P.S. Awesome work!

Having troubles setting a getter onto an object def

Hey!

Im trying to create a global object that uses descriptors like get and set, however im not able to get past this error

image

As soon as i comment out the get it runs with no errors.

However i also see that even when it throws an error, i am still calling the get() method before the application crashed.

I tried taking the source from one of your tests & still no dice

vm.defineProp(vm.global, 'Name', {
    enumerable: true,
    configurable: true,
    get: () => vm.newString('World'),
  })

Memory issues on iOS Safari?

We're having issues with Safari on iOS that seem pretty strange. We're running into a lot of "Out of bounds memory access" errors, which seem like they should have been fixed by moving args onto the heap in 0.11.

More interestingly though, when we switched from manual memory management to using Scopes, we get the Assertion failed: list_empty(&rt->gc_obj_list), at: quickjs/quickjs.c,1964,JS_FreeRuntime error on iOS safari, but not on desktop Chrome for the same page.

Any idea why the memory behavior would be different across these two browsers?

How to delete the field of an object through `callFunction`?

I wrote a unit test to try, but there are some problems with the result

image

import { getQuickJS, QuickJSVm } from 'quickjs-emscripten'
import { withScope } from '../withScope'

describe('测试 deleteKey', () => {
  let vm: QuickJSVm = undefined as any
  beforeEach(async () => {
    const quickJS = await getQuickJS()
    vm = quickJS.createVm()
  })

  afterEach(() => {
    vm.dispose()
    vm = undefined as any
  })
  it('基本示例', () => {
    withScope(vm, (vm) => {
      vm.setProp(vm.global, 'name', vm.newString('liuli'))
      const deleteProperty = vm.unwrapResult(
        vm.evalCode('Reflect.deleteProperty'),
      )
      expect(vm.dump(vm.getProp(vm.global, 'name'))).toBe('liuli')
      vm.callFunction(
        deleteProperty,
        vm.unwrapResult(vm.evalCode('globalThis')),
        vm.newString('name'),
      ) // Not deleted here
      console.log(vm.dump(vm.getProp(vm.global, 'name')))
      vm.evalCode(`Reflect.deleteProperty(globalThis, 'name')`) // This step really takes effect, but I want to do it from the main process
      console.log(vm.dump(vm.getProp(vm.global, 'name')))
    }).dispose()
  })
})

Apparent memory leak under certain circumstances

Hi there, thanks for this really cool project. I've been toying with it, and noticed that the app that I built seems to be leaking a lot of memory. At first I thought I was just doing something wrong, so I tried to scope it down to the simplest example that leaked, and ended up with this. It looks like vm.dump is the problem; if I comment that line out, I don't see memory growth. It's still possible I'm doing something wrong, but I think this is real?

You can run the test by running yarn && yarn build && yarn start, and it will periodically print the "external" memory usage, as per process.memoryUsage. It only takes a few moments to notice the trend, and if you let it run overnight, it will eventually crash.

How to send class to quickjs vm

I'm having a problem where I declare a class in the browser, but I can't send the class into the quickjs vm by using the current API.
After I looked at the implementation of newFunction, I found that it is just a function call, my appeal is how to send a function to quickjs.

WebAssembly.instantiate(): table import has no maximum length

When I clone the repo and install, I'm getting a RuntimeError.

git clone https://github.com/justjake/quickjs-emscripten
cd quickjs-emscripten
yarn

After yarn make-release and tsc finish, the test runs:

failed to asynchronously prepare wasm: LinkError: WebAssembly.instantiate(): table import 21 has no maximum length, expected 618
[LinkError: WebAssembly.instantiate(): table import 21 has no maximum length, expected 618]
[LinkError: WebAssembly.instantiate(): table import 21 has no maximum length, expected 618]
RuntimeError: abort(LinkError: WebAssembly.instantiate(): table import 21 has no maximum length, expected 618). Build with -s ASSERTIONS=1 for more info.
    at C (~/quickjs-emscripten/dist/quickjs-emscripten-module.js:161:55)
    at /quickjs-emscripten/dist/quickjs-emscripten-module.js:186:175

I tentatively searched for a cause, but couldn't find much information about this error.

It feels like a compatibility issue (?) so I checked with emcc -v.

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 1.39.18
clang version 6.0.1  (emscripten 1.39.18 : 1.39.18)
Target: x86_64-apple-darwin18.2.0
Thread model: posix
InstalledDir: /usr/local/opt/emscripten/libexec/llvm/bin
shared:INFO: (Emscripten: Running sanity checks)

Node.js version is v12.14.1.

Feature request: Module APIs

It would be great if this project supported es2015 modules and dynamic import(). I believe QuickJS supports this if its function JS_SetModuleLoaderFunc is called with an appropriate callback that returned the contents for a give module URL.

My idea for an API would look like this:

import { getQuickJS } from 'quickjs-emscripten'

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

  vm.setModuleLoaderFunc(name => {
    switch (name) {
      case '/dir/a.js':
        return `
          export let x = 'b has not run'
          export function setX(_x) {
            x = _x
          }
        `
      case '/dir/b.js':
        return `
          import {setX} from './a.js'
          setX('b has run')
        `
    }
    // Returning null/undefined in this function causes the module loading to fail with a generic error.
    // Alternatively an error can be thrown here and its message is passed into QuickJS.
  })

  // mod here is a QuickJSHandle representing the kind of object you get from the
  // statement `import * as mod from '...'`.
  const mod = vm.runModule(`
    import {x} from './dir/a.js'
    import './dir/b.js'
    export {x}
    export function test() { return 123; }
  `)

  // should output "mod x: b has run"
  console.log('mod x:', vm.getProp(mod, 'x').consume(x => vm.dump(x)));

  const testHandle = vm.getProp(mod, 'test')
  const result = vm.callFunction(testHandle, vm.undefined)
  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()
  }
  // should output "Success: 123"

  testHandle.dispose()
  mod.dispose()
  vm.dispose()
}

main()

This introduces the vm.setModuleLoaderFunc and vm.runModule functions.

vm.runModule executes code in module mode instead of script mode so that static imports work, and it returns a reference to the kind of object you get from import * as ... from '...' statements so that the module's exports can be accessed easily.

vm.setModuleLoaderFunc is meant to be a straight-forward wrapper around QuickJS's JS_SetModuleLoaderFunc, and can be replaced at any time, can dynamically generate modules, can do side-effects like logging, etc. (Ideally it would support an asynchronous callback so it could fetch remote modules on demand, but sadly it seems like QuickJS only supports synchronous module loader functions. I'm interested in trying to address that in the future, but that seems like it would take changes in QuickJS itself and I would find the existing sync API still useful.)

Note that the function passed to JS_SetModuleLoaderFunc and therefore also the function passed here to vm.setModuleLoaderFunc receives the resolved name of the module, not necessarily the literal value in the import ... from '...path...' statement. I believe QuickJS handles this part automatically, but if this behavior needs to be customized then a resolver function can also be supplied to QuickJS through JS_SetModuleLoaderFunc, and maybe this project could expose an API for passing a custom function there too. (Maybe vm.setModuleLoaderFunc would have a second optional parameter, or an overload where you give it a {moduleLoader: Function, resolver?: Function} object.)

For the common case where vm.setModuleLoaderFunc is called with a function that just maps names to fixed values, there could be a helper function like this:

import { getQuickJS, createFixedModuleLoader } from 'quickjs-emscripten'
// ...
  vm.setModuleLoaderFunc(createFixedModuleLoader({
    '/dir/a.js': `
      export let x = 'b has not run'
      export function setX(_x) {
        x = _x
      }
    `,
    '/dir/b.js': `
      import {setX} from './a.js'
      setX('b has run')
    `
  }))

Ideally vm.setModuleLoaderFunc being used would allow dynamic import() to work within code executed with either vm.runModule or vm.evalCode.

Error with NextJS

Hello, first is incredible work this package. It's amazing.

I attempted to use this package in my NextJS project, but I received the following error:

image

My component is like that:

import { useEffect, useState } from 'react';
import { getQuickJS } from 'quickjs-emscripten';

export default function QuickJS() {
  const [rta, setRta] = useState<unknown>(0);

  const runCode = async () => {
    const QuickJS = await getQuickJS();
    const rta = QuickJS.evalCode(`1 + "1"`);
    setRta(rta);
  };

  useEffect(() => {
    runCode();
  });

  return <h1>{rta}</h1>;
}

I guess the error is because NextJS has a hydration process. The package first detects the node environment and then, with NextJS hydration run in the browser, the environment keeps with the node and enters this statement.

image

It's just my guess, when this package from the server works, for example,

import { getQuickJS } from 'quickjs-emscripten';

export default function QuickJS({ rta }) {
  return <h1>{rta}</h1>;
}

export const getServerSideProps = async () => {
  const QuickJS = await getQuickJS();
  const rta = QuickJS.evalCode(`1 + "1"`);
  return {
    props: {
      rta,
    },
  };
};

But the idea is run quickjs from client side.

Error disposing a context after a stack overflow

I'm finding that I get an error if I try to dispose of a context after overflowing the stack. I've put together a minimal example here. In the repro app, there are four test variants: sync/async, and using scope/not using scope variants. Neither of those factors seem to be significant, though. When I try to call QuickJSContext.dispose following the stack overflow exception, I get a trace like this:

$ yarn build && yarn start
yarn run v1.22.17
warning package.json: No license field
$ tsc -b .
Done in 0.19s.
yarn run v1.22.17
warning package.json: No license field
$ node dist
caught: RangeError: Maximum call stack size exceeded
    at wasm-function[23]:0x1900
    at wasm-function[237]:0xd46a
    at wasm-function[237]:0xca8c
    at wasm-function[237]:0xca8c
    at wasm-function[237]:0xca8c
    at wasm-function[237]:0xca8c
    at wasm-function[237]:0xca8c
    at wasm-function[237]:0xca8c
    at wasm-function[237]:0xca8c
    at wasm-function[237]:0xca8c
Aborted(Assertion failed: list_empty(&rt->gc_obj_list), at: quickjs/quickjs.c,1983,JS_FreeRuntime)
/home/tory/quickjs-emscripten-repro-2/node_modules/quickjs-emscripten/dist/generated/emscripten-module.WASM_RELEASE_SYNC.js:16
                throw b;
                ^

RuntimeError: Aborted(Assertion failed: list_empty(&rt->gc_obj_list), at: quickjs/quickjs.c,1983,JS_FreeRuntime). Build with -s ASSERTIONS=1 for more info.
    at E (/home/tory/quickjs-emscripten-repro-2/node_modules/quickjs-emscripten/dist/generated/emscripten-module.WASM_RELEASE_SYNC.js:143:71)
    at a (/home/tory/quickjs-emscripten-repro-2/node_modules/quickjs-emscripten/dist/generated/emscripten-module.WASM_RELEASE_SYNC.js:226:47)
    at wasm-function[645]:0x2c1a9
    at wasm-function[981]:0x43e75
    at QuickJSFFI.a._QTS_FreeRuntime [as QTS_FreeRuntime] (/home/tory/quickjs-emscripten-repro-2/node_modules/quickjs-emscripten/dist/generated/emscripten-module.WASM_RELEASE_SYNC.js:313:82)
    at Lifetime.disposer (/home/tory/quickjs-emscripten-repro-2/node_modules/quickjs-emscripten/dist/module.js:185:22)
    at Lifetime.dispose (/home/tory/quickjs-emscripten-repro-2/node_modules/quickjs-emscripten/dist/lifetime.js:71:18)
    at Scope.dispose (/home/tory/quickjs-emscripten-repro-2/node_modules/quickjs-emscripten/dist/lifetime.js:220:26)
    at QuickJSRuntime.dispose (/home/tory/quickjs-emscripten-repro-2/node_modules/quickjs-emscripten/dist/runtime.js:124:27)
    at Scope.dispose (/home/tory/quickjs-emscripten-repro-2/node_modules/quickjs-emscripten/dist/lifetime.js:220:26)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Could it support vm.newSymbol()?

There are many scenes of using Symbol, but I cant make it with vm.newSymbol. I see JS_NewAtomWithSymbol and JS_SymbolGetIterato in quickjs. Can you add it? Really appreciate it.

Promise inside QuickJs cannot work

I try to use the libsodium-wrapper in QuickJS follow the document, but it hang when await ready .

after some test, i took the strange thing, the Promise will break the VM then i use the code from document .

the mini code in follow :

import {getQuickJS, QuickJSContext, QuickJSHandle, Scope, shouldInterruptAfterDeadline} from 'quickjs-emscripten';


(async () => {
    const QuickJS = await getQuickJS();

    await Scope.withScopeAsync(async (scope) => {
        const vm = scope.manage(QuickJS.newContext());

        const result = vm.evalCode(
            `
(new Promise((resolve, reject) => {
    setTimeout(args => {
        resolve(undefined);
    }, 1000);
})).then(T=>{});
`
        );

        // run in sync way, it return ok.
        // // output:
        // //    Success: {}
        // //    undefined
        // 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();
        // }
        // return;



        // run in async way, it return into strange ?
        // output:
        //    promiseHandle
        //    resolvedResult
        //    resolvedResultPromise Promise { <pending> }
        //    
        // then the process end.


        console.log('promiseHandle');
        const promiseHandle = (vm.unwrapResult(result));

        console.log('resolvedResult');
        const resolvedResultPromise = vm.resolvePromise(promiseHandle);

        console.log('resolvedResultPromise', resolvedResultPromise);
        const resolvedResult = await resolvedResultPromise;    // <============ end on the `await` op

        console.log('promiseHandle.dispose');    // <============ this code be never run
        promiseHandle.dispose();

        console.log('resolvedHandle');
        const resolvedHandle = (vm.unwrapResult(resolvedResult));

        console.log('resolvedHandle', vm.getString(resolvedHandle));

        console.log('resolvedHandle.dispose');
        resolvedHandle.dispose();
    });
})().then(T => {
    console.log(T);
}).catch(E => {
    console.error(E);
});

anything wrong ?

Would this be possible to run on cloudflare workers?

I've been experimenting with trying to get it to run on cf workers but it seems like my lack of knowledge makes it impossible to even know where to start. I've got it to run locally on miniflare but not remote.

Would it even be possible?

if so would it be possible to point me to the right direction on learning about how to do it?

ReferenceError when running in browser

When running in a browser i get the following error:

Uncaught ReferenceError: process is not defined
    at node_modules/quickjs-emscripten/dist/debug.js (debug.ts:1:43)
    at __require2 (chunk-TMRFOBSY.js?v=93c0b0dc:47:50)
    at node_modules/quickjs-emscripten/dist/lifetime.js (lifetime.ts:2:1)
    at __require2 (chunk-TMRFOBSY.js?v=93c0b0dc:47:50)
    at node_modules/quickjs-emscripten/dist/index.js (index.ts:43:1)
    at __require2 (chunk-TMRFOBSY.js?v=93c0b0dc:47:50)
    at dep:quickjs-emscripten:1:16

If I change the line in debug.ts manually, everything works as expected.

Help with async fetch call

Hi justjake,

Thank you for creating this package. It's so useful.

Can I ask for your help implementing fetch within the vm please? I've been trying to follow the documentation and the examples.

Here's the sample code I'm calling on vm.evalCode (the value of code below):

async () => {
return await fetch('http://api.weatherapi.com/v1/current.json?key=XXXXXXXXXX&q=EC1M&aqi=no');
}();

And then here's how I'm implementing quickjs-emscripten:

async function TestCode(code) {

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

    const fetchHandle = vm.newFunction('fetch', (urlHandle) => {
        const url = vm.getString(urlHandle);
        const deferred = vm.newPromise();
        fetch(url).then(result => result.json()).then(deferred.resolve);
        return deferred.handle;
    });
    vm.setProp(vm.global, 'fetch', fetchHandle);
    fetchHandle.dispose();
    
    const evalResult = vm.unwrapResult(vm.evalCode(code));
    vm.executePendingJobs()

    const error = evalResult.error;
    const value = evalResult.value;
    const results = {
        result: error ? "failed" : "success",
        error: error ? vm.dump(error) : null,
        value: error ? null : vm.dump(value)
    };

    evalResult.dispose();
    vm.dispose();

    return results;
}

When I run this, I get the following stack trace:

Assertion failed: list_empty(&rt->gc_obj_list), at: quickjs/quickjs.c,1983,JS_FreeRuntime
Assertion failed: list_empty(&rt->gc_obj_list), at: quickjs/quickjs.c,1983,JS_FreeRuntime
API error RuntimeError: abort(Assertion failed: list_empty(&rt->gc_obj_list), at: quickjs/quickjs.c,1983,JS_FreeRuntime). Build with -s ASSERTIONS=1 for more info.
    at C ({dir}/node_modules/quickjs-emscripten/dist/quickjs-emscripten-module.js:159:55)
    at a ({dir}/node_modules/quickjs-emscripten/dist/quickjs-emscripten-module.js:251:47)
    at <anonymous>:wasm-function[590]:0x2050a
    at D (<anonymous>:wasm-function[1106]:0x3c7f9)
    at QuickJSFFI.a._QTS_FreeRuntime [as QTS_FreeRuntime] ({dir}/node_modules/quickjs-emscripten/dist/quickjs-emscripten-module.js:340:82)
    at Lifetime.disposer ({dir}/node_modules/quickjs-emscripten/dist/quickjs.js:925:23)
    at Lifetime.dispose ({dir}/node_modules/quickjs-emscripten/dist/lifetime.js:131:18)
    at Scope.dispose ({dir}/node_modules/quickjs-emscripten/dist/lifetime.js:284:26)
    at QuickJSVm.dispose ({dir}/node_modules/quickjs-emscripten/dist/quickjs.js:772:21)
    at TestCode (webpack-internal:///./lib/tester.js:30:8)
Error: Lifetime not alive
    at Lifetime.assertAlive ({dir}/node_modules/quickjs-emscripten/dist/lifetime.js:137:19)
    at Lifetime.get [as value] ({dir}/node_modules/quickjs-emscripten/dist/lifetime.js:89:18)
    at {dir}/node_modules/quickjs-emscripten/dist/quickjs.js:581:49
    at Lifetime.consume ({dir}/node_modules/quickjs-emscripten/dist/lifetime.js:121:22)
    at QuickJSVm.callFunction ({dir}/node_modules/quickjs-emscripten/dist/quickjs.js:580:51)
    at QuickJSDeferredPromise.resolve ({dir}/node_modules/quickjs-emscripten/dist/quickjs.js:108:43)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
Error: Lifetime not alive
    at Lifetime.assertAlive ({dir}/node_modules/quickjs-emscripten/dist/lifetime.js:137:19)
    at Lifetime.get [as value] ({dir}/node_modules/quickjs-emscripten/dist/lifetime.js:89:18)
    at {dir}/node_modules/quickjs-emscripten/dist/quickjs.js:581:49
    at Lifetime.consume ({dir}/node_modules/quickjs-emscripten/dist/lifetime.js:121:22)
    at QuickJSVm.callFunction ({dir}/node_modules/quickjs-emscripten/dist/quickjs.js:580:51)
    at QuickJSDeferredPromise.resolve ({dir}/node_modules/quickjs-emscripten/dist/quickjs.js:108:43)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
error - unhandledRejection: Error: Lifetime not alive
error - uncaughtException: RuntimeError: abort(Error: Lifetime not alive). Build with -s ASSERTIONS=1 for more info.

As far as I understand, something isn't waiting for the promise to resolve before the vm is getting disposed (the Lifetime not alive error), but I can't work out how to prevent that from happening.

I believe this implementation is correct according to the docs, but there are example implementations which are different, e.g. defining the promise outside of the newFunction call so that you can await deferred later on: https://github.com/justjake/quickjs-emscripten/blob/master/ts/quickjs.test.ts#L527 I've tried doing it that way too but can't get it to work. I can get different errors, but it's not clear what's happening.

Any help would be appreciated!

Thank you.

bug?: An error occurred when `evalCode` returned Promise

This seems a bit strange, I'm not sure if it is a bug

Error message

	RuntimeError: abort(Assertion failed: list_empty(&rt->gc_obj_list), at: quickjs/quickjs.c,1983,JS_FreeRuntime). Build with -s ASSERTIONS=1 for more info.
		at Object.<anonymous> src/__tests__/quick.test.ts:79:5

	From console:
		Assertion failed: list_empty(&rt->gc_obj_list), at: quickjs/quickjs.c,1983,JS_FreeRuntime 

		Assertion failed: list_empty(&rt->gc_obj_list), at: quickjs/quickjs.c,1983,JS_FreeRuntime 

		Assertion failed: list_empty(&rt->gc_obj_list), at: quickjs/quickjs.c,1983,JS_FreeRuntime 

		Assertion failed: list_empty(&rt->gc_obj_list), at: quickjs/quickjs.c,1983,JS_FreeRuntime 

Source code

const vm = (await getQuickJS()).createVm()
vm.evalCode('(async ()=>{  })()')
vm.dispose()

Issue in using export default and evalCode

Hello, first of all thank you for the great work on this library.

I'm not sure if I'm missing something but when running vm.evalCode with the following code:

function testFn(){
  console.log('Test');
}

export default testFn;

I get this error:

{
  name: 'SyntaxError',
  message: 'unsupported keyword: export'
}

While running this, it works as expected:

export default function testFn(){
  console.log('Test');
}

I've also tried by specifying as options:

{
  type: 'module',
  compileOnly: true
}

Not sure if I'm missing something in the usage or in code.
Here's a small script to reproduce the error:

import { getQuickJS, Scope } from 'quickjs-emscripten';

const notWorkingCode = `
function testFn(){
  console.log('Test');
}

export default testFn;`;

const workingCode = `
export default function testFn(){
  console.log('Test');
}`;

async function main() {
  const QuickJS = await getQuickJS();
  return Scope.withScope((scope) => {
    const vm = scope.manage(QuickJS.newContext());
    const options = {
      type: 'module',
      compileOnly: true,
    };
    const result = vm.evalCode(notWorkingCode, options);
    if (result.error) {
      const dump = result.error.consume((error) => vm.dump(error));
      console.log(dump);
      return;
    }
    console.log('All good');
  });
}

main()

How to catch error in async function?

If async function doesn't have await stmt, hasPendingJob return false
throw Error in this function, I can't get the error msg

const asyncCode = `
async function main() {
    throw new Error('a')
  }
  
  main();
`

const syncCode = `
  function main() {
    throw new Error('a')
  }
  
  main();
`

const resultHd = vm.evalCode(code)

// asyncCode and syncCode both are false
vm.runtime.hasPendingJob()
import { getQuickJS } from "quickjs-emscripten"


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

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


    const resultHd = vm.evalCode(code)
    if (resultHd.error) {
        const error = vm.dump(
            resultHd.error
        )
        console.error('error1', error)
        return
    }

    console.error('vm.runtime.hasPendingJob()', vm.runtime.hasPendingJob())
    if (vm.runtime.hasPendingJob()) {
        const result = vm.unwrapResult(
            resultHd
        )
        const promise = result.consume((result) => vm.resolvePromise(result))
        vm.runtime.executePendingJobs()
        const asyncResult = await promise

        if (asyncResult.error) {
            const error = vm.dump(asyncResult.error)
            asyncResult.error.dispose()
            console.error('error2', error)
            return
        }
    }

}

async function main() {
    const asyncCode = `
    async function main() {
        throw new Error('a')
      }
      
      main();
  `

    const syncCode = `
      function main() {
        throw new Error('a')
      }
      
      main();
  `

    await exec(syncCode)
    await exec(asyncCode)

}

main()

Issue with getting a result from an async function call

Hey,

first of all - thanks for this great library.

Now - I'm having slight issues with getting a result from an async function call.

Full code example below (I took https://github.com/justjake/quickjs-emscripten#promises as a reference example).

I tried to write proper comments in the code, but to sum-up

  1. I'm creating an instance of the QJS context
  2. I'm evaluating the script with the definition of the "handle" function - a function that is than supposed to be called many times with different params
  3. I'm calling the "handle" function
  4. the "handle" is being logged in the console
  5. The "Result1" is being logged in the console
  6. The "Result2" (i.e. the result from "handle" function call) is NEVER logged in the console..

The full result from the console is:

❯ node tools/quickjs.mjs
QuickJS: handle
Result1

It works fine when the handle is not defined as async.

Now - am I'm doing here sth wrong - or is such usecase simply not yet supported by the library?

import {getQuickJS} from 'quickjs-emscripten';

// the example smart contract code loaded from Arweave blockchain
const code = `
(() => {
    async function handle(state, action) {
      // this is being logged
      console.log('handle');
      return 1;
    }
    return handle;
})();
`.trim();

async function main() {

  // 1. creating the QJS context
  const QuickJS = await getQuickJS();
  const vm = QuickJS.newContext();

  // 2. registering "console.log" API
  const logHandle = vm.newFunction("log", (...args) => {
    const nativeArgs = args.map(vm.dump)
    console.log("QuickJS:", ...nativeArgs)
  });
  const consoleHandle = vm.newObject();
  vm.setProp(consoleHandle, "log", logHandle);
  vm.setProp(vm.global, "console", consoleHandle);
  consoleHandle.dispose();
  logHandle.dispose();

  // 3. registering the "handle" function in a global scope
  const handle = vm.evalCode(code);
  vm.setProp(vm.global, 'handle', handle.value);

  // 4. calling the "handle" function
  const result = vm.evalCode(`(async () => {
       return await handle();
     })()`);

  // execute pending jobs - is it necessary here?
  vm.runtime.executePendingJobs();

  if (result.error) {
    console.log('Execution failed:', vm.dump(result.error));
    result.error.dispose();
  } else {
    const promiseHandle = vm.unwrapResult(result)
    // the below is being logged
    console.log("Result1");
    const resolvedResult = await vm.resolvePromise(promiseHandle)
    promiseHandle.dispose();
    const resolvedHandle = vm.unwrapResult(resolvedResult);
    // the below is NEVER logged
    console.log("Result2:", vm.getNumber(resolvedHandle));
  }

  vm.dispose();
}

main().finally();

defineProp leaks

It seems like the get/set functions that defineProps creates does not get cleaned up.

Test case:

import { getQuickJS } from "quickjs-emscripten";

describe("JsExecutorTests", () => {
  it("should run basic JS code", async () => {
    const QuickJS = await getQuickJS();

    const vm = QuickJS.createVm();
    const world = vm.newString("world");
    let result;
    vm.defineProp(vm.global, "Name", {
      configurable: false,
      enumerable: false,
      get: function getWorld() {
        return world;
      },
    });

    try {
      result = vm.evalCode(`"Hello " + Name + "!"`);
      if (result.error) {
        const error = new Error(vm.dump(result.error));
        throw error;
      } else {
        const ret = vm.dump(result.value);
        expect(ret).toEqual("Hello world!");
      }
    } finally {
      if (world.alive) {
        world.dispose();
      }
      if (result) {
        if (result.error?.alive) {
          result.error.dispose();
        }
        if (result.value?.alive) {
          result.value.dispose();
        }
      }
      vm.dispose(); // <--- throws an error because something is not cleaned up.
    }
  });
});

How to an object in a function

Let's say I want to parse JSON.stringify to the context of my code. How do I go about writing this code? I tried

const jsonHandle = vm.newObject();

const stringifyHandle = vm.newFunction('stringify', (objHandle) => {
  // what exactly do I put here? How do I read this handle and get the object
  // there are only getString and getNumber methods available
  // And the getProp requires knowing what keys are available
});
vm.setProp(jsonHandle, 'stringify', stringifyHandle);

vm.setProp(vm.global, 'JSON', jsonHandle);

Namespace 'QuickJSRaw' has no exported member 'QuickJSEmscriptenModule'

Hi,

When trying to use the package in another typescript project, I get the error

/dist/quickjs.d.ts:3:68 - error TS2694: Namespace 'QuickJSRaw' has no exported member 'QuickJSEmscriptenModule'.

declare const QuickJSModule: import("./quickjs-emscripten-module").QuickJSEmscriptenModule;

I got the codebase building locally while trying to discover what the issue was, I noticed that the quickjs-emscripten-module.d.ts in the ts folder was different to the file that ended up in the dist folder after building.

Do you have any thoughts on this issue?

Thanks for your work on this project, very helpful 👍

Question: how to inject setInterval?

First off -- this library is amazing. My hats off to @justjake and all other contributors for doing this. :)

I'd like to inject a simple setInterval function into my VM, and I'm curious as to how to do that.

I tried the below:

  private injectSetInterval() {
    const setIntervalHandler = this.vm.newFunction('setInterval', (funcHandle, timeoutHandle) => {
      const timeout = this.vm.getNumber(timeoutHandle);
      setInterval(() => {
        this.vm.callFunction(funcHandle, {});
      }, timeout);
    });
    this.vm.setProp(this.vm.global, 'setInterval', setIntervalHandler);
    setIntervalHandler.dispose();
  }

And I'm getting a QuickJSUseAfterFree: Lifetime not alive error.

I'm guessing the tricky thing I'm trying to figure out here is how to store a handle to a function value internal to the VM and call it later in my node-native setInterval. Would appreciate any help! Thanks :)

Is there a way turning async in host to sync in vm?

Hello! Thanks for your great library. I am providing a simple javascript API for user to add custom feature. The user js code will be executed in node server. However, my users don't have too much concept of javascript and I don't want to introduce async/await concept for them. So I'm looking for some way to fetch data from database without letting user write await and turn all function into async.

The following code can not work but I hope there is a way to achieve it.

vm.newFunction('getNumberFooFromDB', async() => {
  const result = await mongoose.find({...}).exec()
  return vm.newNumber(result.foo)
}).consume(fnHandle => vm.setProp(vm.global, 'getNumberFooFromDB', fnHandle))

vm.unwrapResult(vm.evalCode(`const foo=getNumberFooFromDB(); foo+1`))

Traditional vm solution such as vm2 and ShadowRealm can not work. Some old js interpreter in js can only support es5. Directly blocking js thread to convert async has performance issue. Modifying the AST and regenerating has some boundary cases. The only way to do this is to develop an interpreter, quickjspp seems able to do this but a lot of uncertainty factor to handle. Is it possible to achieve it in an ease way?

I also found some background information.

Is it possible to limit CPUtime?

Hi there,

This project looks promising to me!
But im wondering if its possible to limit the CPU time.
Like:
runtime.setCPUTimeLimit(miliseconds here)

Feature idea: Separate distribution for target web and node

I started a sandbox for playing with QuickJS in a React full-stack project.

The file where I'm importing quickjs-emscripten runs on both client and server-side. I'm having an issue with quickjs-emscripten-module.js, where it detects if window is undefined, and uses require('fs') and require('path').

I'm using Webpack to create client and server bundles. When the code runs on the client-side, for some reason it tries to require fs and path - but since require doesn't exist in the browser, it throws.

It depends on the bundler - when I use browserify, it bundles correctly to run on the cilent-side.


Digging into how quickjs-emscripten-module.js is generated, I found that I can pass an option for emcc to target the environment specifically.

CFLAGS_EMCC+=-s ENVIRONMENT=web # or "node"

When the target is web, the generated quickjs-emscripten-module.js does not include any require statements. That helps with my issue, so the client-side code can import (and bundle) a web-only version.

I wondered, would you consider publishing separate packages built for web and node targets? Just an idea, since I thought it might help others using this module.

How to do setInterval function?

I have the following:

var _setInterval = vm.newFunction("setInterval", (...args) => {
            const nativeArgs = args.map(vm.dump);
            console.log(...nativeArgs)
            return vm.newNumber(setInterval(...nativeArgs));
});
vm.setProp(vm.global, "setInterval", _setInterval);
_setInterval.dispose();

and the following code is ran in the quickjs sandbox:

setInterval(function() {
      console.log("a");
}, 2000);

This doesn't work. I get an error:

Uncaught SyntaxError: Function statements require a function name

Even when I do set a function name like so:

setInterval(function q() {
      console.log("a");
}, 2000);

It still doesn't work, nothing is outputted to console, not even an error.

evalCodeAsync running in module mode throws `proxy: cannot set property` error

I'm not sure how much detail I can provide here, but essentially I am running some code which calls a proxy's set method. Running the code in global works exactly as expected, but running in module type throws the error mentioned in the title. The funny thing is, the setter does actually fire and succeed, so I am not sure where the error is coming from.

We are trying module type to address another bug which only happens in global type, where await Promise.resolve() throws an error about requiring a semicolon before the await.

Grasping at straws here so I'd love any help you can provide

Provide ways to pass in plain objects and boolean values

Thanks for the nice module.

there are 4 methods to create values:

newFunction
newNumber
newObject
newString

but we can only set one prop for newObject each time, which is not very easy to use.

  1. is there a way to dump a plain object into the vm?

Curerntly I am using evalCode((${JSON.stringify(obj)})) as a workaround

  1. is there a way to create a boolean value just like newString and newNumber?

Getting it to run on Cloudflare Workers

...and other V8 based runtimes probably.

I'm currently trying to use this on Workers and eventually I figured out that there is one main issue:
cloudflare/workers-sdk#1366 (comment)

TLDR: On workers WASM must be included via import foo from "foo.wasm" and can't by compiled dynamically via a string param.

Would it be possible to change / allow loading to this way?

Checking if a function with a given name exists?

Hey,

First, thanks for the great project! I've been toying around with quickjs-emscripted in the browser but am running into a minor issue. I'm trying to call a function inside a VM, but only if the user has defined it. The following currently works as expected:

vm.evalCode('function hello() { return 42; }');
const handle = vm.getProp(vm.global, 'hello');
let result = vm.callFunction(handle, vm.undefined);
if (result.error) {
  console.log('calling function failed')
} else {
  console.log('success', vm.getNumber(result.value));
}

If the user hasn't defined the particular function being called, I can handle it in the error case. However, I'm wondering if there's a more elegant way to do this without having to handle the error. Would it be possible to hack up a checkFunctionHandle() function?

Thanks again!

Suggestion/Feature: upstream web compatible version?

Thanks so much for working on the project!

I want to build a small js playground on the web and I couldn't make quickjs-emscripten work :(

I forked the project and republished as esm-quickjs-emscripten (repo: https://github.com/twop/quickjs-emscripten)

My goals:

  • esmodule output ready to be consumed on the web
  • shipped as the latest ecmascript target.
  • ship actual .wasm file

I'm not comfortable modifying make files and I'm more familiar with rust/wasm toolchain, thus the quality of my changes might be not superb ^_^

If you think that these changes are valuable I would be happy to collaborate to upstream.

Side note:
I published the wasm file to npm, but the required step to make it available by ./quickjs-emscripten-module.wasm is manual and tool dependent (I just copied the wasm file to my dist folder). To my knoweledge emscripten doesn't allow to specify wasm file location (wasm-pack does).

Side note 2:
I'm thinking that expanding the api surface area for wasm file itself might get tricky overtime. I usually use a different approach with rust/wasm.

// this is binary representation of a command + required data needed to be executed by wasm
const serializedCmd: Uint8Array = {...} 

// tell wasm that we need that much space to store it
// return result is a VIEW of the wasm memory
const wasmMemView: Uint8Array = wasm.allocate_space(serializedCmd.lenght);

// copy cmd + data to wasm memory
wasmMemView.set(serializedCmd);

// tell wasm that the memory is filled with payload + handle it
// returns VIEW of the wasm memory that holds the result
const responseView: Uint8Array = wasmpApi.handle_message();

// decode the result into something meaningful, like a normal js object
const result = decode(responseView);

One really cool thing about this approach is that js -> wasm boundary is explicit and it can be expensive because of all the bookkeeping that needs to be done with GC and memory management, and this approach helps with that by not using any manual resource management.

Also, it seems that this approach produces the smallest js and wasm.

Happy to chat more if you are curious about it

QuickJS breaks on OOM in a vm

Hi @justjake

When a vm fails due to the "out of memory" issue, I cannot neither dispose it nor create a new one. It keeps failing with the RuntimeError: memory access out of bounds.

How to reproduce

  1. run vm.evalScript() with a code, big enough to violate the memory limit
  2. you will get the RuntimeError: memory access out of bounds error
  3. try calling vm.dispose() or quickJS.createVm() - you will be getting the same error

BigInt/BigDecimal/BigFloat Support

This version of QuickJS doesn't seem to have support for JS BigInts or QuickJS BigDecimal and BigFloat types. Are you building with CONFIG_BIGNUM off?

QuickJSContext had no callback with id error when newFunction was called many times

const ctx = (await getQuickJS()).newContext();

for (let i = 0; i < 50000; i++) {
  const fn = ctx.newFunction("", () => {});
  ctx.unwrapResult(ctx.callFunction(fn, ctx.undefined));
  fn.dispose();
}

ctx.dispose();

prints

[C to host error: returning null] Error: QuickJSContext had no callback with id -15536
    at Object.callFunction (/quickjs-emscripten-sync/node_modules/quickjs-emscripten/dist/context.js:115:27)
    at /quickjs-emscripten-sync/node_modules/quickjs-emscripten/dist/module.js:36:31
    at QuickJSModuleCallbacks.handleAsyncify (/quickjs-emscripten-sync/node_modules/quickjs-emscripten/dist/module.js:145:23)
    at QuickJSEmscriptenModuleCallbacks.callFunction (/quickjs-emscripten-sync/node_modules/quickjs-emscripten/dist/module.js:30:80)
    at o (/quickjs-emscripten-sync/node_modules/quickjs-emscripten/dist/generated/emscripten-module.WASM_RELEASE_SYNC.js:281:62)
    at wasm://wasm/00174c1a:wasm-function[1105]:0x48b6d
    at wasm://wasm/00174c1a:wasm-function[1077]:0x4823d
    at wasm://wasm/00174c1a:wasm-function[238]:0xbb70
    at wasm://wasm/00174c1a:wasm-function[41]:0x1f06
    at wasm://wasm/00174c1a:wasm-function[822]:0x3aa8a
    at a._QTS_Call [as QTS_Call] (/quickjs-emscripten-sync/node_modules/quickjs-emscripten/dist/generated/emscripten-module.WASM_RELEASE_SYNC.js:335:68)
    at /quickjs-emscripten-sync/node_modules/quickjs-emscripten/dist/context.js:468:49
    at Lifetime.consume (/quickjs-emscripten-sync/node_modules/quickjs-emscripten/dist/lifetime.js:61:24)
    at QuickJSContext.callFunction (/quickjs-emscripten-sync/node_modules/quickjs-emscripten/dist/context.js:468:14)
    at /quickjs-emscripten-sync/src/edge.test.ts:63:26
    at async runTest (/quickjs-emscripten-sync/node_modules/vitest/dist/chunk-runtime-error.1104e45a.mjs:488:5)
    at async runSuite (/quickjs-emscripten-sync/node_modules/vitest/dist/chunk-runtime-error.1104e45a.mjs:576:13)
    at async runFiles (/quickjs-emscripten-sync/node_modules/vitest/dist/chunk-runtime-error.1104e45a.mjs:620:5)
    at async startTestsNode (/quickjs-emscripten-sync/node_modules/vitest/dist/chunk-runtime-error.1104e45a.mjs:638:3)
    at async /quickjs-emscripten-sync/node_modules/vitest/dist/entry.mjs:76:9
    at async Module.withEnv (/quickjs-emscripten-sync/node_modules/vitest/dist/chunk-runtime-error.1104e45a.mjs:259:5)
    at async run (/quickjs-emscripten-sync/node_modules/vitest/dist/entry.mjs:71:5)
    at async file:///quickjs-emscripten-sync/node_modules/tinypool/dist/esm/worker.js:99:20

(this test code was executed on vitest)

Note that If the number of repetitions is small, no error occurs.

Although 50,000 times may seem like a lot, it is an error that is realistically encountered in a code of a certain size.

  • MacOS Venture 13.2 on MacBook Pro M1 2020
  • quickjs-emscripten: v0.21.0 and v0.21.1
  • Node.js: v18.11.0 and Google Chrome 110.0.5481.100(Official Build) (arm64)

Memory handling

https://bellard.org/quickjs/quickjs.html#Memory-handling

It looks like quickJS can let memory limits, that would be useful if you wanted to let users upload their own functions but limit the amount of allowed memory. Being able to get the current memory usage, such as profiling too. I see there is a way to dump memory usage in QuickJS. If so, then someone could use that and the termination hooks to write their own logic to enforce memory usage.

Also be useful if the Execution timeout and interrupts timeouts was there. Could see this maybe being useful for things you might use V8 Isolates for, but not sure if this would be lighter weight on the memory side.

Very interesting project though, could see this being useful on both the server side and client side too. Say if you were making a virtual world or a game engine, could let people run sandbox limited server scripts or even local scripts on the client side.

`executePendingJobs` can't catch error from async function

Reproduction:

import { getQuickJS } from 'quickjs-emscripten'

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

  const result = vm.evalCode(`(async () => { throw new Error() })()`)  // throw an error
  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()
  }

  const res = vm.executePendingJobs()
  console.log(res)  // { value: 0 }

  vm.dispose()
}

main()

exchange TypedArray or Buffer between host and vm

can i exchange TypedArray or Buffer between host and vm ?


i want to use libsodium-wrapper in vm, but it need crypto.getRandomValues function,

so when i try to impl the crypto.getRandomValues in vm, i notice that this function is a node impl and the web polyfill use randombytes to wrap browser crypto.randomBytes function to impl crypto.getRandomValues function.

but the randomBytes and getRandomValues all process the Buffer or TypedArray .

    // crypto.randomBytes  define in nodejs crypto
    function randomBytes(size: number): Buffer;
    function randomBytes(size: number, callback: (err: Error | null, buf: Buffer) => void): void;
    function pseudoRandomBytes(size: number): Buffer;
    function pseudoRandomBytes(size: number, callback: (err: Error | null, buf: Buffer) => void): void;
    // crypto.getRandomValues  define in `lib.dom.d.ts`
    getRandomValues<T extends ArrayBufferView | null>(array: T): T;

so , i think it can impl like :

vm request some random -> tell the host -> host generate crypto random Buffer -> pass random Buffer to vm -> get the crypto random Buffer 

vm.randomBytes() call -> host.randomBytes() call -> host return random buffer -> vm.randomBytes() return

can i pass TypedArray or Buffer from host to vm ?

Feature Request: Interrupt handler (or equivalent) for contexts (not just runtimes)

I am working on a feature in our product to allow tracing "time in vm" by code execution, along with the ability to "cancel" running functions if they surpass some limit or if cancellation is manually requested. We currently use a single context and runtime with a singular setInterruptHandler to stop long-running executions, but that doesn't provide enough granularity for our usecase.

I began the work to split our vm into multiple contexts under a singular runtime to benefit from the configured limits, only to discover the interrupt handler lives at the runtime level. Is there any chance that this method or something similar could be ported down to either the evalCode/evalCodeAsync level, or at least the context?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.