Python bindings for Apple's WebKit and JavaScriptCore frameworks on macOS, enabling web browser functionality and JavaScript execution in Python applications
—
JavaScript execution environment and value management through Apple's JavaScriptCore framework. Provides a complete JavaScript interpreter that can execute JavaScript code, bridge between Python and JavaScript objects, handle exceptions, and manage JavaScript values within Python applications.
JavaScript execution context that manages the global JavaScript environment.
class JSContext:
def init(self) -> JSContext: ...
def initWithVirtualMachine_(self, virtualMachine: JSVirtualMachine) -> JSContext: ...
# Script execution
def evaluateScript_(self, script: str) -> JSValue: ...
def evaluateScript_withSourceURL_(self, script: str, sourceURL: NSURL) -> JSValue: ...
# Exception handling
@property
def exception(self) -> JSValue: ...
@property
def exceptionHandler(self) -> callable: ...
def setExceptionHandler_(self, handler: callable): ...
# Global object access
@property
def globalObject(self) -> JSValue: ...
# Virtual machine
@property
def virtualMachine(self) -> JSVirtualMachine: ...
# Inspection
def isInspectable(self) -> bool: ...
def setInspectable_(self, inspectable: bool): ...
@property
def name(self) -> str: ...
# Object lookup
def objectForKeyedSubscript_(self, key: str) -> JSValue: ...
def setObject_forKeyedSubscript_(self, object: JSValue, key: str): ...JavaScript virtual machine for managing JavaScript execution.
class JSVirtualMachine:
def init(self) -> JSVirtualMachine: ...
def addManagedReference_withOwner_(self, object: JSValue, owner: JSValue): ...
def removeManagedReference_withOwner_(self, object: JSValue, owner: JSValue): ...Represents a JavaScript value and provides methods for type checking, conversion, and property access.
class JSValue:
# Context creation
@classmethod
def valueWithObject_inContext_(cls, value: object, context: JSContext) -> JSValue: ...
@classmethod
def valueWithBool_inContext_(cls, value: bool, context: JSContext) -> JSValue: ...
@classmethod
def valueWithDouble_inContext_(cls, value: float, context: JSContext) -> JSValue: ...
@classmethod
def valueWithInt32_inContext_(cls, value: int, context: JSContext) -> JSValue: ...
@classmethod
def valueWithUInt32_inContext_(cls, value: int, context: JSContext) -> JSValue: ...
@classmethod
def valueWithPoint_inContext_(cls, point: CGPoint, context: JSContext) -> JSValue: ...
@classmethod
def valueWithRange_inContext_(cls, range: NSRange, context: JSContext) -> JSValue: ...
@classmethod
def valueWithRect_inContext_(cls, rect: CGRect, context: JSContext) -> JSValue: ...
@classmethod
def valueWithSize_inContext_(cls, size: CGSize, context: JSContext) -> JSValue: ...
@classmethod
def valueWithNullInContext_(cls, context: JSContext) -> JSValue: ...
@classmethod
def valueWithUndefinedInContext_(cls, context: JSContext) -> JSValue: ...
@classmethod
def valueWithNewObjectInContext_(cls, context: JSContext) -> JSValue: ...
@classmethod
def valueWithNewArrayInContext_(cls, context: JSContext) -> JSValue: ...
@classmethod
def valueWithNewRegularExpressionFromPattern_flags_inContext_(cls, pattern: str, flags: str, context: JSContext) -> JSValue: ...
@classmethod
def valueWithNewErrorFromMessage_inContext_(cls, message: str, context: JSContext) -> JSValue: ...
@classmethod
def valueWithNewPromiseInContext_fromExecutor_(cls, context: JSContext, executor: callable) -> JSValue: ...
@classmethod
def valueWithNewPromiseResolvedWithResult_inContext_(cls, result: JSValue, context: JSContext) -> JSValue: ...
@classmethod
def valueWithNewPromiseRejectedWithReason_inContext_(cls, reason: JSValue, context: JSContext) -> JSValue: ...
# Type checking
def isUndefined(self) -> bool: ...
def isNull(self) -> bool: ...
def isBoolean(self) -> bool: ...
def isNumber(self) -> bool: ...
def isString(self) -> bool: ...
def isObject(self) -> bool: ...
def isArray(self) -> bool: ...
def isDate(self) -> bool: ...
def isSymbol(self) -> bool: ...
# Type conversion
def toBool(self) -> bool: ...
def toNumber(self) -> float: ...
def toInt32(self) -> int: ...
def toUInt32(self) -> int: ...
def toString(self) -> str: ...
def toDate(self) -> NSDate: ...
def toArray(self) -> list: ...
def toDictionary(self) -> dict: ...
def toObject(self) -> object: ...
def toObjectOfClass_(self, expectedClass: type) -> object: ...
def toPoint(self) -> CGPoint: ...
def toRange(self) -> NSRange: ...
def toRect(self) -> CGRect: ...
def toSize(self) -> CGSize: ...
# Property access
def hasProperty_(self, property: str) -> bool: ...
def deleteProperty_(self, property: str) -> bool: ...
def valueForProperty_(self, property: str) -> JSValue: ...
def setValue_forProperty_(self, value: JSValue, property: str): ...
def defineProperty_descriptor_(self, property: str, descriptor: dict) -> bool: ...
# Array/object access
def valueAtIndex_(self, index: int) -> JSValue: ...
def setValue_atIndex_(self, value: JSValue, index: int): ...
def objectForKeyedSubscript_(self, key: str) -> JSValue: ...
def setObject_forKeyedSubscript_(self, object: JSValue, key: str): ...
def objectAtIndexedSubscript_(self, index: int) -> JSValue: ...
def setObject_atIndexedSubscript_(self, object: JSValue, index: int): ...
# Function calls
def callWithArguments_(self, arguments: list) -> JSValue: ...
def constructWithArguments_(self, arguments: list) -> JSValue: ...
def invokeMethod_withArguments_(self, method: str, arguments: list) -> JSValue: ...
# Comparison
def isEqualToObject_(self, value: JSValue) -> bool: ...
def isEqualWithTypeCoercionToObject_(self, value: JSValue) -> bool: ...
def isInstanceOf_(self, constructor: JSValue) -> bool: ...
# Context and prototype
@property
def context(self) -> JSContext: ...
@property
def prototype(self) -> JSValue: ...
def setPrototype_(self, prototype: JSValue): ...Managed JavaScript value for memory management in Objective-C contexts.
class JSManagedValue:
@classmethod
def managedValueWithValue_(cls, value: JSValue) -> JSManagedValue: ...
@classmethod
def managedValueWithValue_andOwner_(cls, value: JSValue, owner: object) -> JSManagedValue: ...
@property
def value(self) -> JSValue: ...Base protocol for exporting Python classes to JavaScript.
# JSExport is a protocol - implement in your Python classes
class MyExportedClass:
# Methods to be exported to JavaScript
def exportedMethod(self): ...
def anotherExportedMethod_(self, parameter): ...Decorator function for customizing JavaScript method names.
def JSExportAs(PropertyName: str, Selector: callable) -> tuple:
"""
Export a Python method to JavaScript with a custom name.
Args:
PropertyName: The name to use in JavaScript
Selector: The Python method to export
Returns:
Tuple of selectors for the export system
"""
...Direct access to JavaScriptCore C functions for advanced use cases.
def JSValueMakeString(context: JSContextRef, string: JSStringRef) -> JSValueRef: ...
def JSValueMakeNumber(context: JSContextRef, number: float) -> JSValueRef: ...
def JSValueMakeBoolean(context: JSContextRef, boolean: bool) -> JSValueRef: ...
def JSValueMakeNull(context: JSContextRef) -> JSValueRef: ...
def JSValueMakeUndefined(context: JSContextRef) -> JSValueRef: ...def JSValueGetType(context: JSContextRef, value: JSValueRef) -> JSType: ...
def JSValueToBoolean(context: JSContextRef, value: JSValueRef) -> bool: ...
def JSValueToNumber(context: JSContextRef, value: JSValueRef, exception: JSValueRef) -> float: ...
def JSValueCreateJSONString(context: JSContextRef, value: JSValueRef, indent: int, exception: JSValueRef) -> JSStringRef: ...def JSObjectSetPropertyForKey(context: JSContextRef, object: JSObjectRef, propertyKey: JSValueRef, value: JSValueRef, attributes: JSPropertyAttributes, exception: JSValueRef): ...
def JSObjectDeletePropertyForKey(context: JSContextRef, object: JSObjectRef, propertyKey: JSValueRef, exception: JSValueRef) -> bool: ...
def JSObjectMakeError(context: JSContextRef, argumentCount: int, arguments: JSValueRef, exception: JSValueRef) -> JSValueRef: ...
def JSObjectMakeTypedArray(context: JSContextRef, arrayType: JSTypedArrayType, length: int, exception: JSValueRef) -> JSValueRef: ...
def JSObjectGetArrayBufferBytesPtr(context: JSContextRef, object: JSObjectRef, exception: JSValueRef) -> bytes: ...
def JSObjectGetTypedArrayByteOffset(context: JSContextRef, object: JSObjectRef, exception: JSValueRef) -> int: ...
def JSValueGetTypedArrayType(context: JSContextRef, value: JSValueRef, exception: JSValueRef) -> JSTypedArrayType: ...def JSStringCreateWithCFString(string: CFStringRef) -> JSStringRef: ...
def JSStringCopyCFString(allocator: CFAllocatorRef, string: JSStringRef) -> CFStringRef: ...def JSClassRetain(jsClass: JSClassRef) -> JSClassRef: ...# JSType enumeration
kJSTypeUndefined = 0
kJSTypeNull = 1
kJSTypeBoolean = 2
kJSTypeNumber = 3
kJSTypeString = 4
kJSTypeObject = 5
kJSTypeSymbol = 6# JSPropertyAttributes
kJSPropertyAttributeNone = 0
kJSPropertyAttributeReadOnly = 2
kJSPropertyAttributeDontEnum = 4
kJSPropertyAttributeDontDelete = 8# JSClassAttributes
kJSClassAttributeNone = 0
kJSClassAttributeNoAutomaticPrototype = 2# JSTypedArrayType enumeration
kJSTypedArrayTypeInt8Array = 0
kJSTypedArrayTypeInt16Array = 1
kJSTypedArrayTypeInt32Array = 2
kJSTypedArrayTypeUint8Array = 3
kJSTypedArrayTypeUint8ClampedArray = 4
kJSTypedArrayTypeUint16Array = 5
kJSTypedArrayTypeUint32Array = 6
kJSTypedArrayTypeFloat32Array = 7
kJSTypedArrayTypeFloat64Array = 8
kJSTypedArrayTypeArrayBuffer = 9
kJSTypedArrayTypeNone = 10
kJSTypedArrayTypeBigInt64Array = 11
kJSTypedArrayTypeBigUint64Array = 12JSPropertyDescriptorConfigurableKey = "configurable"
JSPropertyDescriptorEnumerableKey = "enumerable"
JSPropertyDescriptorGetKey = "get"
JSPropertyDescriptorSetKey = "set"
JSPropertyDescriptorValueKey = "value"
JSPropertyDescriptorWritableKey = "writable"import JavaScriptCore
# Create context
context = JavaScriptCore.JSContext.alloc().init()
# Execute JavaScript
result = context.evaluateScript_("2 + 3 * 4")
print(result.toNumber()) # Output: 14.0
# Work with JavaScript objects
context.evaluateScript_("var person = {name: 'John', age: 30};")
person = context.objectForKeyedSubscript_("person")
name = person.valueForProperty_("name")
print(name.toString()) # Output: Johnimport JavaScriptCore
context = JavaScriptCore.JSContext.alloc().init()
def exception_handler(context, exception):
print(f"JavaScript Error: {exception.toString()}")
print(f"Stack trace: {exception.valueForProperty_('stack').toString()}")
context.setExceptionHandler_(exception_handler)
# This will trigger the exception handler
context.evaluateScript_("throw new Error('Something went wrong');")import JavaScriptCore
from objc import protocol
@protocol("JSExport")
class Calculator:
def add_to_(self, a, b):
return a + b
def multiply_by_(self, a, b):
return a * b
context = JavaScriptCore.JSContext.alloc().init()
calculator = Calculator()
# Make calculator available in JavaScript
context.setObject_forKeyedSubscript_(calculator, "calculator")
# Use from JavaScript
result = context.evaluateScript_("calculator.add_to_(5, 3)")
print(result.toNumber()) # Output: 8.0import JavaScriptCore
context = JavaScriptCore.JSContext.alloc().init()
# Create JavaScript array
js_array = JavaScriptCore.JSValue.valueWithNewArrayInContext_(context)
# Add elements
js_array.setValue_atIndex_(JavaScriptCore.JSValue.valueWithDouble_inContext_(1.5, context), 0)
js_array.setValue_atIndex_(JavaScriptCore.JSValue.valueWithObject_inContext_("hello", context), 1)
js_array.setValue_atIndex_(JavaScriptCore.JSValue.valueWithBool_inContext_(True, context), 2)
# Make available in JavaScript context
context.setObject_forKeyedSubscript_(js_array, "myArray")
# Access from JavaScript
length = context.evaluateScript_("myArray.length")
print(length.toNumber()) # Output: 3.0
first_element = context.evaluateScript_("myArray[0]")
print(first_element.toNumber()) # Output: 1.5import JavaScriptCore
context = JavaScriptCore.JSContext.alloc().init()
def promise_executor(resolve, reject):
# Simulate async operation
import time
time.sleep(0.1)
resolve.callWithArguments_(["Success!"])
# Create promise
promise = JavaScriptCore.JSValue.valueWithNewPromiseInContext_fromExecutor_(context, promise_executor)
# Make available in JavaScript
context.setObject_forKeyedSubscript_(promise, "myPromise")
# Handle promise in JavaScript
context.evaluateScript_("""
myPromise.then(function(result) {
console.log('Promise resolved:', result);
}).catch(function(error) {
console.log('Promise rejected:', error);
});
""")Install with Tessl CLI
npx tessl i tessl/pypi-pyobjc-framework-webkit