Skip to Content
V8-wasm
API Reference

ShadowRealm Polyfill

TC39 ShadowRealm implementation backed by a real V8-wasm Isolate and Context

Import path: @browser-ports/v8-wasm/shadowrealm-polyfill

@browser-ports/v8-wasm/shadowrealm-polyfill — TC39 ShadowRealm implementation backed by a real V8-wasm Isolate and Context.

Enforces the TC39 callable-boundary contract: only primitive values and wrapped functions may cross the realm boundary, and guest exceptions are surfaced in the caller realm as TypeError instances.

ShadowRealm

TC39 ShadowRealm backed by an isolated V8-wasm GuestRealm.

Each ShadowRealm owns an independent global environment and disjoint set of ECMAScript built-ins. Values crossing evaluate or wrapped function calls are strictly restricted to primitives and ShadowRealmWrappedFunction callables.

Example:

import { ShadowRealm } from '@browser-ports/v8-wasm/shadowrealm-polyfill';

using realm = new ShadowRealm();
const sum = realm.evaluate('(a, b) => a + b') as (a: number, b: number) => number;
console.log(sum(19, 23)); // 42
new ShadowRealm(options?: GuestRealmOptions)

Creates a new ShadowRealm with its own V8 Context (and dedicated Isolate unless options.isolate is provided).

  • options (GuestRealmOptions) — Optional underlying GuestRealmOptions.

Methods

  • [dispose](): void

    Disposable protocol hook for using declarations.

  • dispose(): void

    Disposes the underlying V8-wasm GuestRealm and releases all handles.

  • evaluate(sourceText: string): ShadowRealmPrimitive | ShadowRealmWrappedFunction

    Evaluates sourceText synchronously inside this ShadowRealm and returns the completion value after passing it through the TC39 callable boundary.

    Returns: A ShadowRealmPrimitive or ShadowRealmWrappedFunction.

    Throws: TypeError if sourceText is not a string, if evaluation throws, or if the completion value is a non-callable object.

    • sourceText (string) — JavaScript source code to evaluate.
  • importValue(specifier: string, bindingName: string): Promise<ShadowRealmPrimitive | ShadowRealmWrappedFunction>

    TC39 ShadowRealm.prototype.importValue(specifier, bindingName) entrypoint.

    Validates that specifier and bindingName are strings and rejects with a TypeError when no host module resolver is configured.

    Returns: A promise that rejects with TypeError unless a host resolver is configured.

    • specifier (string) — Module specifier string.
    • bindingName (string) — Exported binding name to retrieve.

Interfaces & Types

ShadowRealmPrimitive

type ShadowRealmPrimitive = undefined | null | boolean | number | string | bigint | symbol;

JavaScript primitive values allowed to cross the TC39 ShadowRealm boundary directly by value.

ShadowRealmWrappedFunction

type ShadowRealmWrappedFunction = (...args: unknown[]) => ShadowRealmPrimitive | ShadowRealmWrappedFunction;

Callable wrapper returned when a function crosses the ShadowRealm boundary.

All arguments and return values are validated against the callable boundary (TypeError is thrown if a non-callable object is passed or returned), and exceptions thrown inside the wrapped function are converted to caller-realm TypeError instances.

On this page