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): booleanChecks 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) — OptionalJSExceptionBoxpopulated on syntax error.
JSContextGetGlobalObject
function JSContextGetGlobalObject(ctx: JSGlobalContextRef): JSValueRefReturns 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 | nullCompiles 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 aJSStringRef._thisObject(JSValueRef | null) — Reservedthisbinding parameter (optional)._sourceURL(JSStringRef | null) — Reserved source URL parameter (optional)._startingLineNumber(number) — Reserved starting line number (optional).exception(JSExceptionBox | null) — OptionalJSExceptionBoxpopulated on error.
JSGarbageCollect
function JSGarbageCollect(ctx: JSGlobalContextRef): voidAdvises the backing V8 isolate to perform garbage collection (lowMemoryNotification()).
Parameters
ctx(JSGlobalContextRef) — Target global context.
JSGlobalContextCreate
function JSGlobalContextCreate(sharedIsolate?: Isolate | null): JSGlobalContextRefCreates 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 V8Isolateto share across contexts.
JSGlobalContextRelease
function JSGlobalContextRelease(ctx: JSGlobalContextRef): voidReleases 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 | nullInvokes 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): booleanDeletes property propertyName from obj, returning true on success.
JSObjectGetProperty
function JSObjectGetProperty(ctx: JSGlobalContextRef, obj: JSValueRef, propertyName: JSStringRef): JSValueRefReads property propertyName from obj (returning undefined if absent).
JSObjectHasProperty
function JSObjectHasProperty(ctx: JSGlobalContextRef, obj: JSValueRef, propertyName: JSStringRef): booleanReturns true if obj has property propertyName (including its prototype chain).
JSObjectMake
function JSObjectMake(ctx: JSGlobalContextRef): JSValueRefCreates a new empty plain JavaScript object ({}) in ctx.
JSObjectMakeArray
function JSObjectMakeArray(ctx: JSGlobalContextRef, elements?: readonly JSValueRef[]): JSValueRefCreates 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): JSValueRefCreates 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 aJSValueRef.
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): voidAssigns value to property propertyName on obj.
JSStringCreateWithUTF8CString
function JSStringCreateWithUTF8CString(str: string): JSStringRefCreates a JSStringRef from a UTF-8 JavaScript string.
Parameters
str(string) — Input string.
JSStringGetLength
function JSStringGetLength(ref: JSStringRef): numberReturns the UTF-16 code-unit length of a JSStringRef.
Parameters
ref(JSStringRef) — String reference to measure.
JSStringGetUTF8CString
function JSStringGetUTF8CString(ref: JSStringRef): stringExtracts the host JavaScript string from a JSStringRef.
Parameters
ref(JSStringRef) — String reference to read.
JSStringRelease
function JSStringRelease(_ref: JSStringRef): voidReleases 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): JSTypeReturns the JSType tag of val.
JSValueIsBoolean
function JSValueIsBoolean(ctx: JSGlobalContextRef, val: JSValueRef): booleanReturns true if val is a boolean primitive.
JSValueIsNull
function JSValueIsNull(ctx: JSGlobalContextRef, val: JSValueRef): booleanReturns true if val is null.
JSValueIsNumber
function JSValueIsNumber(ctx: JSGlobalContextRef, val: JSValueRef): booleanReturns true if val is a number primitive.
JSValueIsObject
function JSValueIsObject(ctx: JSGlobalContextRef, val: JSValueRef): booleanReturns true if val is an object (including functions and arrays).
JSValueIsString
function JSValueIsString(ctx: JSGlobalContextRef, val: JSValueRef): booleanReturns true if val is a string primitive.
JSValueIsUndefined
function JSValueIsUndefined(ctx: JSGlobalContextRef, val: JSValueRef): booleanReturns true if val is undefined.
JSValueMakeBoolean
function JSValueMakeBoolean(ctx: JSGlobalContextRef, value: boolean): JSValueRefCreates a JSValueRef representing a JavaScript boolean primitive.
JSValueMakeNull
function JSValueMakeNull(ctx: JSGlobalContextRef): JSValueRefCreates a JSValueRef representing JavaScript null.
JSValueMakeNumber
function JSValueMakeNumber(ctx: JSGlobalContextRef, value: number): JSValueRefCreates a JSValueRef representing an IEEE-754 double-precision number.
JSValueMakeString
function JSValueMakeString(ctx: JSGlobalContextRef, str: JSStringRef): JSValueRefCreates a JSValueRef representing a JavaScript string primitive from str.
JSValueMakeUndefined
function JSValueMakeUndefined(ctx: JSGlobalContextRef): JSValueRefCreates a JSValueRef representing JavaScript undefined.
JSValueToBoolean
function JSValueToBoolean(ctx: JSGlobalContextRef, val: JSValueRef): booleanConverts val to a boolean using ECMAScript ToBoolean.
JSValueToNumber
function JSValueToNumber(ctx: JSGlobalContextRef, val: JSValueRef, exception?: JSExceptionBox | null): numberConverts 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 | nullConverts 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.
| Member | Value | Description |
|---|---|---|
kJSTypeBoolean | 2 | A primitive boolean value (true or false). |
kJSTypeNull | 1 | The unique null value. |
kJSTypeNumber | 3 | A primitive numeric value. |
kJSTypeObject | 5 | A JavaScript object (including functions and arrays). |
kJSTypeString | 4 | A primitive string value. |
kJSTypeSymbol | 6 | A primitive symbol value. |
kJSTypeUndefined | 0 | The 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, ornullif 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 V8Context.handles— Set of all boxedJSValueRefhandles owned by this context for cleanup on release.isolate— Backing V8Isolate.managed— Owned isolate wrapper when created without a shared isolate (nullif 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 persistentGlobal<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).