Skip to Content
V8-wasm
API Reference

JavaScriptCore C-API

Drop-in TypeScript port of Apple's JavaScriptCore C-API backed by full-JIT V8-wasm

Import path: @browser-ports/v8-wasm/jsc

@browser-ports/v8-wasm/jsc — JavaScriptCore C-API drop-in backed by full-JIT V8-wasm.

Implements the core Apple JavaScriptCore C-API surface (JSGlobalContextCreate, JSEvaluateScript, JSValueMake*, JSObject*, JSString*) directly over V8-wasm isolates, contexts, and persistent handles.

Functions

JSCheckScriptSyntax

function JSCheckScriptSyntax(ctx: JSGlobalContextRef, script: JSStringRef, _sourceURL?: JSStringRef | null, _startingLineNumber?: number, exception?: JSExceptionBox | null): boolean

Checks whether script parses with valid JavaScript syntax in ctx without executing it.

Returns: true if script compiled without syntax errors; false otherwise.

Parameters

  • ctx (JSGlobalContextRef) — Execution context.
  • script (JSStringRef) — Source code to parse.
  • _sourceURL (JSStringRef | null) — Reserved source URL parameter (optional).
  • _startingLineNumber (number) — Reserved starting line number (optional).
  • exception (JSExceptionBox | null) — Optional JSExceptionBox populated on syntax error.

JSContextGetGlobalObject

function JSContextGetGlobalObject(ctx: JSGlobalContextRef): JSValueRef

Returns the global object (globalThis) of ctx.

Parameters

  • ctx (JSGlobalContextRef) — Target global context.

JSEvaluateScript

function JSEvaluateScript(ctx: JSGlobalContextRef, script: JSStringRef, _thisObject?: JSValueRef | null, _sourceURL?: JSStringRef | null, _startingLineNumber?: number, exception?: JSExceptionBox | null): JSValueRef | null

Compiles and evaluates a JavaScript script inside ctx.

If compilation or execution throws, stores the exception in exception.value (when provided) and returns null.

Returns: Completion value handle, or null if an exception was thrown.

Parameters

  • ctx (JSGlobalContextRef) — Execution context.
  • script (JSStringRef) — Source code as a JSStringRef.
  • _thisObject (JSValueRef | null) — Reserved this binding parameter (optional).
  • _sourceURL (JSStringRef | null) — Reserved source URL parameter (optional).
  • _startingLineNumber (number) — Reserved starting line number (optional).
  • exception (JSExceptionBox | null) — Optional JSExceptionBox populated on error.

JSGarbageCollect

function JSGarbageCollect(ctx: JSGlobalContextRef): void

Advises the backing V8 isolate to perform garbage collection (lowMemoryNotification()).

Parameters

  • ctx (JSGlobalContextRef) — Target global context.

JSGlobalContextCreate

function JSGlobalContextCreate(sharedIsolate?: Isolate | null): JSGlobalContextRef

Creates a new global JavaScript execution context backed by a fresh V8 Context (and a dedicated V8 Isolate unless sharedIsolate is supplied).

Release with JSGlobalContextRelease when done.

Returns: Newly allocated JSGlobalContextRef.

Parameters

  • sharedIsolate (Isolate | null) — Optional existing V8 Isolate to share across contexts.

JSGlobalContextRelease

function JSGlobalContextRelease(ctx: JSGlobalContextRef): void

Releases a JSGlobalContextRef, resetting all boxed JSValueRef handles, releasing the backing V8 Context, and disposing the Isolate if owned.

Parameters

  • ctx (JSGlobalContextRef) — Global context to destroy.

JSObjectCallAsFunction

function JSObjectCallAsFunction(ctx: JSGlobalContextRef, fnObj: JSValueRef, thisObject: JSValueRef | null, args?: readonly JSValueRef[], exception?: JSExceptionBox | null): JSValueRef | null

Invokes fnObj as a function with receiver thisObject and arguments args.

Returns null and populates exception.value (if provided) when invocation throws.

JSObjectDeleteProperty

function JSObjectDeleteProperty(ctx: JSGlobalContextRef, obj: JSValueRef, propertyName: JSStringRef): boolean

Deletes property propertyName from obj, returning true on success.

JSObjectGetProperty

function JSObjectGetProperty(ctx: JSGlobalContextRef, obj: JSValueRef, propertyName: JSStringRef): JSValueRef

Reads property propertyName from obj (returning undefined if absent).

JSObjectHasProperty

function JSObjectHasProperty(ctx: JSGlobalContextRef, obj: JSValueRef, propertyName: JSStringRef): boolean

Returns true if obj has property propertyName (including its prototype chain).

JSObjectMake

function JSObjectMake(ctx: JSGlobalContextRef): JSValueRef

Creates a new empty plain JavaScript object ({}) in ctx.

JSObjectMakeArray

function JSObjectMakeArray(ctx: JSGlobalContextRef, elements?: readonly JSValueRef[]): JSValueRef

Creates a new JavaScript Array in ctx initialized with elements.

Parameters

  • ctx (JSGlobalContextRef) — Target global context.
  • elements (readonly JSValueRef[]) — Initial element values (default empty).

JSObjectMakeFunctionWithCallback

function JSObjectMakeFunctionWithCallback(ctx: JSGlobalContextRef, _name: JSStringRef | null, callback: JSObjectCallAsFunctionCallback): JSValueRef

Creates a JavaScript function object backed by a host TypeScript callback.

Parameters

  • ctx (JSGlobalContextRef) — Target global context.
  • _name (JSStringRef | null) — Optional function name reference.
  • callback (JSObjectCallAsFunctionCallback) — Host callback receiving (ctx, thisObject, args) and returning a JSValueRef.

JSObjectMakeTypedArray

function JSObjectMakeTypedArray(ctx: JSGlobalContextRef, length: number): { bytes: Uint8Array; disposeBackingStore: () => void; ref: JSValueRef }

Allocates a zero-initialized Uint8Array of length bytes inside ctx and returns both the guest object handle and a zero-copy host Uint8Array view over its backing store in V8-wasm linear memory.

Call disposeBackingStore() when finished reading or writing bytes.

Parameters

  • ctx (JSGlobalContextRef) — Target global context.
  • length (number) — Byte length of the typed array.

JSObjectSetProperty

function JSObjectSetProperty(ctx: JSGlobalContextRef, obj: JSValueRef, propertyName: JSStringRef, value: JSValueRef): void

Assigns value to property propertyName on obj.

JSStringCreateWithUTF8CString

function JSStringCreateWithUTF8CString(str: string): JSStringRef

Creates a JSStringRef from a UTF-8 JavaScript string.

Parameters

  • str (string) — Input string.

JSStringGetLength

function JSStringGetLength(ref: JSStringRef): number

Returns the UTF-16 code-unit length of a JSStringRef.

Parameters

  • ref (JSStringRef) — String reference to measure.

JSStringGetUTF8CString

function JSStringGetUTF8CString(ref: JSStringRef): string

Extracts the host JavaScript string from a JSStringRef.

Parameters

  • ref (JSStringRef) — String reference to read.

JSStringRelease

function JSStringRelease(_ref: JSStringRef): void

Releases a JSStringRef (no-op in TypeScript; provided for C-API source compatibility).

Parameters

  • _ref (JSStringRef) — String reference to release.

JSValueGetType

function JSValueGetType(ctx: JSGlobalContextRef, val: JSValueRef): JSType

Returns the JSType tag of val.

JSValueIsBoolean

function JSValueIsBoolean(ctx: JSGlobalContextRef, val: JSValueRef): boolean

Returns true if val is a boolean primitive.

JSValueIsNull

function JSValueIsNull(ctx: JSGlobalContextRef, val: JSValueRef): boolean

Returns true if val is null.

JSValueIsNumber

function JSValueIsNumber(ctx: JSGlobalContextRef, val: JSValueRef): boolean

Returns true if val is a number primitive.

JSValueIsObject

function JSValueIsObject(ctx: JSGlobalContextRef, val: JSValueRef): boolean

Returns true if val is an object (including functions and arrays).

JSValueIsString

function JSValueIsString(ctx: JSGlobalContextRef, val: JSValueRef): boolean

Returns true if val is a string primitive.

JSValueIsUndefined

function JSValueIsUndefined(ctx: JSGlobalContextRef, val: JSValueRef): boolean

Returns true if val is undefined.

JSValueMakeBoolean

function JSValueMakeBoolean(ctx: JSGlobalContextRef, value: boolean): JSValueRef

Creates a JSValueRef representing a JavaScript boolean primitive.

JSValueMakeNull

function JSValueMakeNull(ctx: JSGlobalContextRef): JSValueRef

Creates a JSValueRef representing JavaScript null.

JSValueMakeNumber

function JSValueMakeNumber(ctx: JSGlobalContextRef, value: number): JSValueRef

Creates a JSValueRef representing an IEEE-754 double-precision number.

JSValueMakeString

function JSValueMakeString(ctx: JSGlobalContextRef, str: JSStringRef): JSValueRef

Creates a JSValueRef representing a JavaScript string primitive from str.

JSValueMakeUndefined

function JSValueMakeUndefined(ctx: JSGlobalContextRef): JSValueRef

Creates a JSValueRef representing JavaScript undefined.

JSValueToBoolean

function JSValueToBoolean(ctx: JSGlobalContextRef, val: JSValueRef): boolean

Converts val to a boolean using ECMAScript ToBoolean.

JSValueToNumber

function JSValueToNumber(ctx: JSGlobalContextRef, val: JSValueRef, exception?: JSExceptionBox | null): number

Converts val to a number using ECMAScript ToNumber.

Returns NaN and populates exception.value (if provided) when conversion throws.

JSValueToStringCopy

function JSValueToStringCopy(ctx: JSGlobalContextRef, val: JSValueRef, exception?: JSExceptionBox | null): JSStringRef | null

Converts val to a JSStringRef copy using ECMAScript ToString.

Returns null and populates exception.value (if provided) when conversion throws.

Enumerations

JSType

JavaScriptCore value type tags returned by JSValueGetType.

MemberValueDescription
kJSTypeBoolean2A primitive boolean value (true or false).
kJSTypeNull1The unique null value.
kJSTypeNumber3A primitive numeric value.
kJSTypeObject5A JavaScript object (including functions and arrays).
kJSTypeString4A primitive string value.
kJSTypeSymbol6A primitive symbol value.
kJSTypeUndefined0The unique undefined value.

Interfaces & Types

JSExceptionBox

Out-parameter container used to receive uncaught JS exceptions from JSC API calls.

interface JSExceptionBox {
  value: JSValueRef | null;
}
  • value — Caught exception value, or null if no exception occurred.

JSGlobalContextRef

Opaque handle representing a global JavaScriptCore execution context backed by a V8 Context.

interface JSGlobalContextRef {
  readonly __brand: "JSGlobalContextRef";
  readonly context: Global<Context>;
  readonly handles: Set<Global<Value>>;
  readonly isolate: Isolate;
  readonly managed: ManagedIsolate | null;
}
  • context — Persistent handle to the backing V8 Context.
  • handles — Set of all boxed JSValueRef handles owned by this context for cleanup on release.
  • isolate — Backing V8 Isolate.
  • managed — Owned isolate wrapper when created without a shared isolate (null if shared).

JSStringRef

Opaque handle representing a JavaScriptCore UTF-8 string reference.

interface JSStringRef {
  readonly __brand: "JSStringRef";
  value: string;
}
  • value — Underlying JavaScript string content.

JSValueRef

Opaque handle representing a JavaScript value pinned in a JSGlobalContextRef.

interface JSValueRef {
  readonly __brand: "JSValueRef";
  readonly ctx: JSGlobalContextRef;
  readonly global: Global<Value>;
}
  • ctx — Owning global context.
  • global — Underlying V8 persistent Global<Value> handle.

JSObjectCallAsFunctionCallback

type JSObjectCallAsFunctionCallback = (ctx: JSGlobalContextRef, thisObject: JSObjectRef, args: JSValueRef[]) => JSValueRef;

Host callback invoked when a function created by JSObjectMakeFunctionWithCallback is called from guest JavaScript.

JSObjectRef

type JSObjectRef = JSValueRef;

Opaque handle representing a JavaScript object (alias of JSValueRef).

On this page