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)); // 42new ShadowRealm(options?: GuestRealmOptions)Creates a new ShadowRealm with its own V8 Context (and dedicated Isolate
unless options.isolate is provided).
options(GuestRealmOptions) — Optional underlyingGuestRealmOptions.
Methods
-
[dispose](): voidDisposable protocol hook for
usingdeclarations. -
dispose(): voidDisposes the underlying V8-wasm
GuestRealmand releases all handles. -
evaluate(sourceText: string): ShadowRealmPrimitive | ShadowRealmWrappedFunctionEvaluates
sourceTextsynchronously inside thisShadowRealmand returns the completion value after passing it through the TC39 callable boundary.Returns: A
ShadowRealmPrimitiveorShadowRealmWrappedFunction.Throws:
TypeErrorifsourceTextis 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
specifierandbindingNameare strings and rejects with aTypeErrorwhen no host module resolver is configured.Returns: A promise that rejects with
TypeErrorunless 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.