Bare Docs

bare-realm

Realm support for Bare

Documented against v2.1.1
stable

bare-realm — Realm support for Bare. It is a native addon and requires Bare >=1.37.0. Before v2.1.0, it required Bare >=1.5.0.

npm i bare-realm

Usage

const Realm = require('bare-realm')

const realm = new Realm()

realm.evaluate('globalThis.foo = 42')
// 42

typeof globalThis.foo
// undefined

API

Realm

evaluate(code: string, options?: EvaluateOptions): any

Evaluate code in the realm's isolated global scope and return its completion value. The realm has its own globalThis, so assignments to globals made by code do not leak into the caller's environment.

Parameters

ParameterTypeDefaultDescription
codestring—The JavaScript source to evaluate.
options?EvaluateOptions—Options. filename is the script name used in stack traces (default '<anonymous>'); offset shifts the reported line numbers (default 0). Fixed in v2.1.1: the script name in stack traces ended in a NUL character, as in x.js\u0000:1:7.

Returns any — The completion value of code.

Realm.globalOf(value: unknown): object | null

Get the global object of the realm that created value. Added in v2.1.0.

Parameters

ParameterTypeDefaultDescription
valueunknown—The value to look up.

Returns object | null — The globalThis of the realm that created value, or null if value is not an object or the engine cannot tell which realm created it.

Realm.isFrom(value: unknown, global: object): boolean

Check whether value was created by the realm whose global object is global. This is faster than comparing the result of Realm.globalOf(), as it can be called directly from optimized code. Added in v2.1.0.

Parameters

ParameterTypeDefaultDescription
valueunknown—The value to check.
globalobject—The globalThis of the realm.

Returns boolean — true if value is an object created by the realm of global, and false otherwise.

Types

EvaluateOptions

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

See also

Last updated on

Was this helpful?

On this page