LogoPear Docs
ReferencesBareModules

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

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

Returns Context — A new isolated global context that code can be run in with runInContext().

runInContext(code: string, context: Context, options?: RunOptions): unknown

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

Parameters

ParameterTypeDefaultDescription
codestringThe JavaScript source to run.
contextContextA context previously created with createContext().
options?RunOptionsOptions. 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(code: string, options?: RunOptions): unknown

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.
options?RunOptionsOptions. 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.

Types

Context

interface Context {
}

RunOptions

interface RunOptions {
  filename?: string
  offset?: number
}

See also

On this page