LogoPear Docs

bare-vm

Isolated JavaScript contexts for Bare

stable

bare-vm — Isolated JavaScript contexts for Bare.

npm i bare-vm

Usage

const vm = require('bare-vm')

const context = vm.createContext()
vm.runInContext('x = 40; x += 2', context) // 42

vm.runInNewContext('x = 40; x += 2') // 42

API

Functions

createContext(): Record<string | number | symbol, unknown>

Source

Create and return a new isolated global context that code can be run in with runInContext().

Returns Record<string | number | symbol, unknown> — A new isolated global context that code can be run in with runInContext().

runInContext

runInContext(code: string, context: Record<string | number | symbol, unknown>, opts?: {
    filename?: string
    offset?: number
  }): unknown
Source

Run code inside context, a context previously created with createContext(), and return the result.

Parameters

ParameterTypeDefaultDescription
codestringThe JavaScript source to run.
contextRecord<string | number | symbol, unknown>A context previously created with createContext().
opts?{ filename?: string offset?: number }Options. filename is the script name used in stack traces (default '<anonymous>'); offset shifts the reported line numbers (default 0, also accepted as lineOffset for Node.js compatibility).

Returns unknown — The completion value of code.

runInNewContext

runInNewContext(code: string, opts?: {
    filename?: string
    offset?: number
  }): unknown
Source

Create a new context and run code inside it in one step, equivalent to calling createContext() followed by runInContext().

Parameters

ParameterTypeDefaultDescription
codestringThe JavaScript source to run.
opts?{ filename?: string offset?: number }Options. filename is the script name used in stack traces (default '<anonymous>'); offset shifts the reported line numbers (default 0, also accepted as lineOffset for Node.js compatibility).

Returns unknown — The completion value of code.

See also

On this page