bare-vm
Isolated JavaScript contexts for Bare
stable
Source
Source
Source
bare-vm — Isolated JavaScript contexts for Bare.
npm i bare-vmUsage
const vm = require('bare-vm')
const context = vm.createContext()
vm.runInContext('x = 40; x += 2', context) // 42
vm.runInNewContext('x = 40; x += 2') // 42API
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
}): unknownRun code inside context, a context previously created with createContext(), and return the result.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
code | string | — | The JavaScript source to run. |
context | Record<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
}): unknownCreate a new context and run code inside it in one step, equivalent to calling createContext() followed by runInContext().
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
code | string | — | The 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
- Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.