|
Ultralight C++ API 2.0.0
|
#include <JavaScriptCore/JSObjectRef.h>
This structure contains properties and callbacks that define a type of object. All fields other than the version field are optional. Any pointer may be NULL.
version The version number of this structure. Use 0 for standard callbacks or 1000 for extended callbacks. attributes A logically ORed set of JSClassAttributes to give to the class. className A null-terminated UTF8 string containing the class's name. parentClass A JSClass to set as the class's parent class. Pass NULL use the default object class. staticValues (Version 0) A JSStaticValue array containing the class's statically declared value properties. Pass NULL to specify no statically declared value properties. The array must be terminated by a JSStaticValue whose name field is NULL. staticFunctions (Version 0) A JSStaticFunction array containing the class's statically declared function properties. Pass NULL to specify no statically declared function properties. The array must be terminated by a JSStaticFunction whose name field is NULL. staticValuesEx (Version 1000) A JSStaticValueEx array for extended static values that receive JSClassRef in callbacks. staticFunctionsEx (Version 1000) A JSStaticFunctionEx array for extended static functions that receive JSClassRef in callbacks. initialize/initializeEx The callback invoked when an object is first created. Use version 0 'initialize' or version 1000 'initializeEx'. finalize/finalizeEx The callback invoked when an object is finalized (prepared for garbage collection). Use version 0 'finalize' or version 1000 'finalizeEx'. hasProperty/hasPropertyEx The callback invoked when determining whether an object has a property. If this field is NULL, getProperty is called instead. Use version 0 'hasProperty' or version 1000 'hasPropertyEx'. getProperty/getPropertyEx The callback invoked when getting a property's value. Use version 0 'getProperty' or version 1000 'getPropertyEx'. setProperty/setPropertyEx The callback invoked when setting a property's value. Use version 0 'setProperty' or version 1000 'setPropertyEx'. deleteProperty/deletePropertyEx The callback invoked when deleting a property. Use version 0 'deleteProperty' or version 1000 'deletePropertyEx'. getPropertyNames/getPropertyNamesEx The callback invoked when collecting the names of an object's properties. Use version 0 'getPropertyNames' or version 1000 'getPropertyNamesEx'. callAsFunction/callAsFunctionEx The callback invoked when an object is called as a function. Use version 0 'callAsFunction' or version 1000 'callAsFunctionEx'. hasInstance/hasInstanceEx The callback invoked when an object is used as the target of an 'instanceof' expression. Use version 0 'hasInstance' or version 1000 'hasInstanceEx'. callAsConstructor/callAsConstructorEx The callback invoked when an object is used as a constructor in a 'new' expression. Use version 0 'callAsConstructor' or version 1000 'callAsConstructorEx'. convertToType/convertToTypeEx The callback invoked when converting an object to a particular JavaScript type. Use version 0 'convertToType' or version 1000 'convertToTypeEx'. privateData Private data storage for the JSClass itself (version 1000 only). This allows you to associate arbitrary data with the class definition.Version System and Callback Types:
The JSClassDefinition structure supports two versions of callbacks through a union with anonymous structs:
Version 0 (Standard Callbacks):
Version 1000 (Extended Callbacks):
The union structure allows both callback sets to share the same memory space, with anonymous structs enabling direct field access based on version (e.g., definition.initialize for v0 or definition.initializeEx for v1000).
Example Usage:
Version 0 (Standard): JSClassDefinition definition = kJSClassDefinitionEmpty; definition.version = 0; // or omit since 0 is default definition.className = "MyClass"; definition.initialize = MyInitialize; // Direct access definition.finalize = MyFinalize;
Version 1000 (Extended): JSClassDefinition definition = kJSClassDefinitionEmpty; definition.version = 1000; definition.className = "MyClass"; definition.initializeEx = MyInitializeEx; // Direct access with Ex suffix definition.finalizeEx = MyFinalizeEx; definition.privateData = myClassData; // Store class-specific data
JSClassRef jsClass = JSClassCreate(&definition); Later retrieve private data in callbacks: void* data = JSClassGetPrivate(jsClass);
The staticValues and staticFunctions arrays are the simplest and most efficient means for vending custom properties. Statically declared properties autmatically service requests like getProperty, setProperty, and getPropertyNames. Property access callbacks are required only to implement unusual properties, like array indexes, whose names are not known at compile-time.
If you named your getter function "GetX" and your setter function "SetX", you would declare a JSStaticValue array containing "X" like this:
JSStaticValue StaticValueArray[] = { { "X", GetX, SetX, kJSPropertyAttributeNone }, { 0, 0, 0, 0 } };
Standard JavaScript practice calls for storing function objects in prototypes, so they can be shared. The default JSClass created by JSClassCreate follows this idiom, instantiating objects with a shared, automatically generating prototype containing the class's function objects. The kJSClassAttributeNoAutomaticPrototype attribute specifies that a JSClass should not automatically generate such a prototype. The resulting JSClass instantiates objects with the default object prototype, and gives each instance object its own copy of the class's function objects.
A NULL callback specifies that the default object callback should substitute, except in the case of hasProperty, where it specifies that getProperty should substitute.
It is not possible to use JS subclassing with objects created from a class definition that sets callAsConstructor by default. Subclassing is supported via the JSObjectMakeConstructor function, however.
Classes | |
| union | [union].__unnamed0__ |
Public Attributes | |
| int | version |
| JSClassAttributes | attributes |
| const char * | className |
| JSClassRef | parentClass |
| union | |
| void * | privateData |
| union JSClassDefinition::[union].__unnamed0__ @100354210027362142001077053252342143006105167033 |
| JSClassAttributes attributes |
| const char* className |
| JSClassRef parentClass |
| void* privateData |
| int version |