ADC Home > Reference Library > Reference > Carbon > Internet & Web > JavaScriptCore Framework Reference

 


JSObjectRef.h

Includes:

Overview



Functions

JSClassCreate
Creates a JavaScript class suitable for use with JSObjectMake.
JSClassRelease
Releases a JavaScript class.
JSClassRetain
Retains a JavaScript class.
JSObjectCallAsConstructor
Calls an object as a constructor.
JSObjectCallAsFunction
Calls an object as a function.
JSObjectCopyPropertyNames
Gets the names of an object's enumerable properties.
JSObjectDeleteProperty
Deletes a property from an object.
JSObjectGetPrivate
Gets an object's private data.
JSObjectGetProperty
Gets a property from an object.
JSObjectGetPropertyAtIndex
Gets a property from an object by numeric index.
JSObjectGetPrototype
Gets an object's prototype.
JSObjectHasProperty
Tests whether an object has a given property.
JSObjectIsConstructor
Tests whether an object can be called as a constructor.
JSObjectIsFunction
Tests whether an object can be called as a function.
JSObjectMake
Creates a JavaScript object.
JSObjectMakeConstructor
Convenience method for creating a JavaScript constructor.
JSObjectMakeFunction
Creates a function with a given script as its body.
JSObjectMakeFunctionWithCallback
Convenience method for creating a JavaScript function with a given callback as its implementation.
JSObjectSetPrivate
Sets a pointer to private data on an object.
JSObjectSetProperty
Sets a property on an object.
JSObjectSetPropertyAtIndex
Sets a property on an object by numeric index.
JSObjectSetPrototype
Sets an object's prototype.
JSPropertyNameAccumulatorAddName
Adds a property name to a JavaScript property name accumulator.
JSPropertyNameArrayGetCount
Gets a count of the number of items in a JavaScript property name array.
JSPropertyNameArrayGetNameAtIndex
Gets a property name at a given index in a JavaScript property name array.
JSPropertyNameArrayRelease
Releases a JavaScript property name array.
JSPropertyNameArrayRetain
Retains a JavaScript property name array.

JSClassCreate


Creates a JavaScript class suitable for use with JSObjectMake.

JSClassRef JSClassCreate(
    const JSClassDefinition *definition);  
Parameters
definition
A JSClassDefinition that defines the class.
Return Value

A JSClass with the given definition. Ownership follows the Create Rule.


JSClassRelease


Releases a JavaScript class.

void JSClassRelease(
    JSClassRef jsClass);  
Parameters
jsClass
The JSClass to release.


JSClassRetain


Retains a JavaScript class.

JSClassRef JSClassRetain(
    JSClassRef jsClass);  
Parameters
jsClass
The JSClass to retain.
Return Value

A JSClass that is the same as jsClass.


JSObjectCallAsConstructor


Calls an object as a constructor.

JSObjectRef JSObjectCallAsConstructor(
    JSContextRef ctx,
    JSObjectRef object,
    size_t argumentCount,
    const JSValueRef arguments[],
    JSValueRef *exception);  
Parameters
ctx
The execution context to use.
object
The JSObject to call as a constructor.
argumentCount
An integer count of the number of arguments in arguments.
arguments
A JSValue array of arguments to pass to the constructor. Pass NULL if argumentCount is 0.
exception
A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
Return Value

The JSObject that results from calling object as a constructor, or NULL if an exception is thrown or object is not a constructor.


JSObjectCallAsFunction


Calls an object as a function.

JSValueRef JSObjectCallAsFunction(
    JSContextRef ctx,
    JSObjectRef object,
    JSObjectRef thisObject,
    size_t argumentCount,
    const JSValueRef arguments[],
    JSValueRef *exception);  
Parameters
ctx
The execution context to use.
object
The JSObject to call as a function.
thisObject
The object to use as "this," or NULL to use the global object as "this."
argumentCount
An integer count of the number of arguments in arguments.
arguments
A JSValue array of arguments to pass to the function. Pass NULL if argumentCount is 0.
exception
A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
Return Value

The JSValue that results from calling object as a function, or NULL if an exception is thrown or object is not a function.


JSObjectCopyPropertyNames


Gets the names of an object's enumerable properties.

JSPropertyNameArrayRef JSObjectCopyPropertyNames(
    JSContextRef ctx,
    JSObjectRef object);  
Parameters
ctx
The execution context to use.
object
The object whose property names you want to get.
Return Value

A JSPropertyNameArray containing the names object's enumerable properties.


JSObjectDeleteProperty


Deletes a property from an object.

bool JSObjectDeleteProperty(
    JSContextRef ctx,
    JSObjectRef object,
    JSStringRef propertyName,
    JSValueRef *exception);  
Parameters
ctx
The execution context to use.
object
The JSObject whose property you want to delete.
propertyName
A JSString containing the property's name.
exception
A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
Return Value

true if the delete operation succeeds, otherwise false (for example, if the property has the kJSPropertyAttributeDontDelete attribute set).


JSObjectGetPrivate


Gets an object's private data.

void* JSObjectGetPrivate(
    JSObjectRef object);  
Parameters
object
A JSObject whose private data you want to get.
Return Value

A void* that is the object's private data, if the object has private data, otherwise NULL.


JSObjectGetProperty


Gets a property from an object.

JSValueRef JSObjectGetProperty(
    JSContextRef ctx,
    JSObjectRef object,
    JSStringRef propertyName,
    JSValueRef *exception);  
Parameters
ctx
The execution context to use.
object
The JSObject whose property you want to get.
propertyName
A JSString containing the property's name.
exception
A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
Return Value

The property's value if object has the property, otherwise the undefined value.


JSObjectGetPropertyAtIndex


Gets a property from an object by numeric index.

JSValueRef JSObjectGetPropertyAtIndex(
    JSContextRef ctx,
    JSObjectRef object,
    unsigned propertyIndex,
    JSValueRef *exception);  
Parameters
ctx
The execution context to use.
object
The JSObject whose property you want to get.
propertyIndex
An integer value that is the property's name.
exception
A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
Return Value

The property's value if object has the property, otherwise the undefined value.

Discussion

Calling JSObjectGetPropertyAtIndex is equivalent to calling JSObjectGetProperty with a string containing propertyIndex, but JSObjectGetPropertyAtIndex provides optimized access to numeric properties.


JSObjectGetPrototype


Gets an object's prototype.

JSValueRef JSObjectGetPrototype(
    JSContextRef ctx,
    JSObjectRef object);  
Parameters
ctx
The execution context to use.
object
A JSObject whose prototype you want to get.
Return Value

A JSValue that is the object's prototype.


JSObjectHasProperty


Tests whether an object has a given property.

bool JSObjectHasProperty(
    JSContextRef ctx,
    JSObjectRef object,
    JSStringRef propertyName);  
Parameters
object
The JSObject to test.
propertyName
A JSString containing the property's name.
Return Value

true if the object has a property whose name matches propertyName, otherwise false.


JSObjectIsConstructor


Tests whether an object can be called as a constructor.

bool JSObjectIsConstructor(
    JSContextRef ctx,
    JSObjectRef object);  
Parameters
ctx
The execution context to use.
object
The JSObject to test.
Return Value

true if the object can be called as a constructor, otherwise false.


JSObjectIsFunction


Tests whether an object can be called as a function.

bool JSObjectIsFunction(
    JSContextRef ctx,
    JSObjectRef object);  
Parameters
ctx
The execution context to use.
object
The JSObject to test.
Return Value

true if the object can be called as a function, otherwise false.


JSObjectMake


Creates a JavaScript object.

JSObjectRef JSObjectMake(
    JSContextRef ctx,
    JSClassRef jsClass,
    void *data);  
Parameters
ctx
The execution context to use.
jsClass
The JSClass to assign to the object. Pass NULL to use the default object class.
data
A void* to set as the object's private data. Pass NULL to specify no private data.
Return Value

A JSObject with the given class and private data.

Discussion

The default object class does not allocate storage for private data, so you must provide a non-NULL jsClass to JSObjectMake if you want your object to be able to store private data.

data is set on the created object before the intialize methods in its class chain are called. This enables the initialize methods to retrieve and manipulate data through JSObjectGetPrivate.


JSObjectMakeConstructor


Convenience method for creating a JavaScript constructor.

JSObjectRef JSObjectMakeConstructor(
    JSContextRef ctx,
    JSClassRef jsClass,
    JSObjectCallAsConstructorCallback callAsConstructor);  
Parameters
ctx
The execution context to use.
jsClass
A JSClass that is the class your constructor will assign to the objects its constructs. jsClass will be used to set the constructor's .prototype property, and to evaluate 'instanceof' expressions. Pass NULL to use the default object class.
callAsConstructor
A JSObjectCallAsConstructorCallback to invoke when your constructor is used in a 'new' expression. Pass NULL to use the default object constructor.
Return Value

A JSObject that is a constructor. The object's prototype will be the default object prototype.

Discussion

The default object constructor takes no arguments and constructs an object of class jsClass with no private data.


JSObjectMakeFunction


Creates a function with a given script as its body.

JSObjectRef JSObjectMakeFunction(
    JSContextRef ctx,
    JSStringRef name,
    unsigned parameterCount,
    const JSStringRef parameterNames[],
    JSStringRef body,
    JSStringRef sourceURL,
    int startingLineNumber,
    JSValueRef *exception);  
Parameters
ctx
The execution context to use.
name
A JSString containing the function's name. This will be used when converting the function to string. Pass NULL to create an anonymous function.
parameterCount
An integer count of the number of parameter names in parameterNames.
parameterNames
A JSString array containing the names of the function's parameters. Pass NULL if parameterCount is 0.
body
A JSString containing the script to use as the function's body.
sourceURL
A JSString containing a URL for the script's source file. This is only used when reporting exceptions. Pass NULL if you do not care to include source file information in exceptions.
startingLineNumber
An integer value specifying the script's starting line number in the file located at sourceURL. This is only used when reporting exceptions.
exception
A pointer to a JSValueRef in which to store a syntax error exception, if any. Pass NULL if you do not care to store a syntax error exception.
Return Value

A JSObject that is a function, or NULL if either body or parameterNames contains a syntax error. The object's prototype will be the default function prototype.

Discussion

Use this method when you want to execute a script repeatedly, to avoid the cost of re-parsing the script before each execution.


JSObjectMakeFunctionWithCallback


Convenience method for creating a JavaScript function with a given callback as its implementation.

JSObjectRef JSObjectMakeFunctionWithCallback(
    JSContextRef ctx,
    JSStringRef name,
    JSObjectCallAsFunctionCallback callAsFunction);  
Parameters
ctx
The execution context to use.
name
A JSString containing the function's name. This will be used when converting the function to string. Pass NULL to create an anonymous function.
callAsFunction
The JSObjectCallAsFunctionCallback to invoke when the function is called.
Return Value

A JSObject that is a function. The object's prototype will be the default function prototype.


JSObjectSetPrivate


Sets a pointer to private data on an object.

bool JSObjectSetPrivate(
    JSObjectRef object,
    void *data);  
Parameters
object
The JSObject whose private data you want to set.
data
A void* to set as the object's private data.
Return Value

true if object can store private data, otherwise false.

Discussion

The default object class does not allocate storage for private data. Only objects created with a non-NULL JSClass can store private data.


JSObjectSetProperty


Sets a property on an object.

void JSObjectSetProperty(
    JSContextRef ctx,
    JSObjectRef object,
    JSStringRef propertyName,
    JSValueRef value,
    JSPropertyAttributes attributes,
    JSValueRef *exception);  
Parameters
ctx
The execution context to use.
object
The JSObject whose property you want to set.
propertyName
A JSString containing the property's name.
value
A JSValue to use as the property's value.
exception
A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
attributes
A logically ORed set of JSPropertyAttributes to give to the property.


JSObjectSetPropertyAtIndex


Sets a property on an object by numeric index.

void JSObjectSetPropertyAtIndex(
    JSContextRef ctx,
    JSObjectRef object,
    unsigned propertyIndex,
    JSValueRef value,
    JSValueRef *exception);  
Parameters
ctx
The execution context to use.
object
The JSObject whose property you want to set.
propertyIndex
The property's name as a number.
value
A JSValue to use as the property's value.
exception
A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
Discussion

Calling JSObjectSetPropertyAtIndex is equivalent to calling JSObjectSetProperty with a string containing propertyIndex, but JSObjectSetPropertyAtIndex provides optimized access to numeric properties.


JSObjectSetPrototype


Sets an object's prototype.

void JSObjectSetPrototype(
    JSContextRef ctx,
    JSObjectRef object,
    JSValueRef value);  
Parameters
ctx
The execution context to use.
object
The JSObject whose prototype you want to set.
value
A JSValue to set as the object's prototype.


JSPropertyNameAccumulatorAddName


Adds a property name to a JavaScript property name accumulator.

void JSPropertyNameAccumulatorAddName(
    JSPropertyNameAccumulatorRef accumulator,
    JSStringRef propertyName);  
Parameters
accumulator
The accumulator object to which to add the property name.
propertyName
The property name to add.


JSPropertyNameArrayGetCount


Gets a count of the number of items in a JavaScript property name array.

size_t JSPropertyNameArrayGetCount(
    JSPropertyNameArrayRef array);  
Parameters
array
The array from which to retrieve the count.
Return Value

An integer count of the number of names in array.


JSPropertyNameArrayGetNameAtIndex


Gets a property name at a given index in a JavaScript property name array.

JSStringRef JSPropertyNameArrayGetNameAtIndex(
    JSPropertyNameArrayRef array,
    size_t index);  
Parameters
array
The array from which to retrieve the property name.
index
The index of the property name to retrieve.
Return Value

A JSStringRef containing the property name.


JSPropertyNameArrayRelease


Releases a JavaScript property name array.

void JSPropertyNameArrayRelease(
    JSPropertyNameArrayRef array);  
Parameters
array
The JSPropetyNameArray to release.


JSPropertyNameArrayRetain


Retains a JavaScript property name array.

JSPropertyNameArrayRef JSPropertyNameArrayRetain(
    JSPropertyNameArrayRef array);  
Parameters
array
The JSPropertyNameArray to retain.
Return Value

A JSPropertyNameArray that is the same as array.

Constants


kJSClassDefinitionEmpty


A JSClassDefinition structure of the current version, filled with NULL pointers and having no attributes.

extern const JSClassDefinition kJSClassDefinitionEmpty;  
Discussion

Use this constant as a convenience when creating class definitions. For example, to create a class definition with only a finalize method:

JSClassDefinition definition = kJSClassDefinitionEmpty; definition.finalize = Finalize;

Typedefs


JSClassAttributes


A set of JSClassAttributes. Combine multiple attributes by logically ORing them together.

typedef unsigned JSClassAttributes;  


JSClassDefinition


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.

typedef struct { 
    int version; // current (and only) version is 0 
    JSClassAttributes attributes;  
    const char *className; 
    JSClassRef parentClass;  
    const JSStaticValue *staticValues; 
    const JSStaticFunction *staticFunctions;  
    JSObjectInitializeCallback initialize; 
    JSObjectFinalizeCallback finalize; 
    JSObjectHasPropertyCallback hasProperty; 
    JSObjectGetPropertyCallback getProperty; 
    JSObjectSetPropertyCallback setProperty; 
    JSObjectDeletePropertyCallback deleteProperty; 
    JSObjectGetPropertyNamesCallback getPropertyNames; 
    JSObjectCallAsFunctionCallback callAsFunction; 
    JSObjectCallAsConstructorCallback callAsConstructor; 
    JSObjectHasInstanceCallback hasInstance; 
    JSObjectConvertToTypeCallback convertToType; 
} JSClassDefinition;  
Fields
version
The version number of this structure. The current version is 0.
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
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
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.
initialize
The callback invoked when an object is first created. Use this callback to initialize the object.
finalize
The callback invoked when an object is finalized (prepared for garbage collection). Use this callback to release resources allocated for the object, and perform other cleanup.
hasProperty
The callback invoked when determining whether an object has a property. If this field is NULL, getProperty is called instead. The hasProperty callback enables optimization in cases where only a property's existence needs to be known, not its value, and computing its value is expensive.
getProperty
The callback invoked when getting a property's value.
setProperty
The callback invoked when setting a property's value.
deleteProperty
The callback invoked when deleting a property.
getPropertyNames
The callback invoked when collecting the names of an object's properties.
callAsFunction
The callback invoked when an object is called as a function.
hasInstance
The callback invoked when an object is used as the target of an 'instanceof' expression.
callAsConstructor
The callback invoked when an object is used as a constructor in a 'new' expression.
convertToType
The callback invoked when converting an object to a particular JavaScript type.
Discussion

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.


JSObjectCallAsConstructorCallback


The callback invoked when an object is used as a constructor in a 'new' expression.

typedef JSObjectRef ( *JSObjectCallAsConstructorCallback) (
    JSContextRef ctx,
    JSObjectRef constructor,
    size_t argumentCount,
    const JSValueRef arguments[],
    JSValueRef *exception);  
Parameters
ctx
The execution context to use.
constructor
A JSObject that is the constructor being called.
argumentCount
An integer count of the number of arguments in arguments.
arguments
A JSValue array of the arguments passed to the function.
exception
A pointer to a JSValueRef in which to return an exception, if any.
Return Value

A JSObject that is the constructor's return value.

Discussion

If you named your function CallAsConstructor, you would declare it like this:

JSObjectRef CallAsConstructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);

If your callback were invoked by the JavaScript expression 'new myConstructor()', constructor would be set to myConstructor.

If this callback is NULL, using your object as a constructor in a 'new' expression will throw an exception.


JSObjectCallAsFunctionCallback


The callback invoked when an object is called as a function.

typedef JSValueRef ( *JSObjectCallAsFunctionCallback) (
    JSContextRef ctx,
    JSObjectRef function,
    JSObjectRef thisObject,
    size_t argumentCount,
    const JSValueRef arguments[],
    JSValueRef *exception);  
Parameters
ctx
The execution context to use.
function
A JSObject that is the function being called.
thisObject
A JSObject that is the 'this' variable in the function's scope.
argumentCount
An integer count of the number of arguments in arguments.
arguments
A JSValue array of the arguments passed to the function.
exception
A pointer to a JSValueRef in which to return an exception, if any.
Return Value

A JSValue that is the function's return value.

Discussion

If you named your function CallAsFunction, you would declare it like this:

JSValueRef CallAsFunction(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);

If your callback were invoked by the JavaScript expression 'myObject.myFunction()', function would be set to myFunction, and thisObject would be set to myObject.

If this callback is NULL, calling your object as a function will throw an exception.


JSObjectConvertToTypeCallback


The callback invoked when converting an object to a particular JavaScript type.

typedef JSValueRef ( *JSObjectConvertToTypeCallback) (
    JSContextRef ctx,
    JSObjectRef object,
    JSType type,
    JSValueRef *exception);  
Parameters
ctx
The execution context to use.
object
The JSObject to convert.
type
A JSType specifying the JavaScript type to convert to.
exception
A pointer to a JSValueRef in which to return an exception, if any.
Return Value

The objects's converted value, or NULL if the object was not converted.

Discussion

If you named your function ConvertToType, you would declare it like this:

JSValueRef ConvertToType(JSContextRef ctx, JSObjectRef object, JSType type, JSValueRef* exception);

If this function returns false, the conversion request forwards to object's parent class chain (which includes the default object class).

This function is only invoked when converting an object to number or string. An object converted to boolean is 'true.' An object converted to object is itself.


JSObjectDeletePropertyCallback


The callback invoked when deleting a property.

typedef bool ( *JSObjectDeletePropertyCallback) (
    JSContextRef ctx,
    JSObjectRef object,
    JSStringRef propertyName,
    JSValueRef *exception);  
Parameters
ctx
The execution context to use.
object
The JSObject in which to delete the property.
propertyName
A JSString containing the name of the property to delete.
exception
A pointer to a JSValueRef in which to return an exception, if any.
Return Value

true if propertyName was successfully deleted, otherwise false.

Discussion

If you named your function DeleteProperty, you would declare it like this:

bool DeleteProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception);

If this function returns false, the delete request forwards to object's statically declared properties, then its parent class chain (which includes the default object class).


JSObjectFinalizeCallback


The callback invoked when an object is finalized (prepared for garbage collection). An object may be finalized on any thread.

typedef void ( *JSObjectFinalizeCallback) (
    JSObjectRef object);  
Parameters
object
The JSObject being finalized.
Discussion

If you named your function Finalize, you would declare it like this:

void Finalize(JSObjectRef object);

The finalize callback is called on the most derived class first, and the least derived class (the parent class) last.

You must not call any function that may cause a garbage collection or an allocation of a garbage collected object from within a JSObjectFinalizeCallback. This includes all functions that have a JSContextRef parameter.


JSObjectGetPropertyCallback


The callback invoked when getting a property's value.

typedef JSValueRef ( *JSObjectGetPropertyCallback) (
    JSContextRef ctx,
    JSObjectRef object,
    JSStringRef propertyName,
    JSValueRef *exception);  
Parameters
ctx
The execution context to use.
object
The JSObject to search for the property.
propertyName
A JSString containing the name of the property to get.
exception
A pointer to a JSValueRef in which to return an exception, if any.
Return Value

The property's value if object has the property, otherwise NULL.

Discussion

If you named your function GetProperty, you would declare it like this:

JSValueRef GetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception);

If this function returns NULL, the get request forwards to object's statically declared properties, then its parent class chain (which includes the default object class), then its prototype chain.


JSObjectGetPropertyNamesCallback


The callback invoked when collecting the names of an object's properties.

typedef void ( *JSObjectGetPropertyNamesCallback) (
    JSContextRef ctx,
    JSObjectRef object,
    JSPropertyNameAccumulatorRef propertyNames);  
Parameters
ctx
The execution context to use.
object
The JSObject whose property names are being collected.
propertyNames
A JavaScript property name accumulator in which to accumulate the names of object's properties.
Discussion

If you named your function GetPropertyNames, you would declare it like this:

void GetPropertyNames(JSContextRef ctx, JSObjectRef object, JSPropertyNameAccumulatorRef accumulator);

Property name accumulators are used by JSObjectCopyPropertyNames and JavaScript for...in loops.

Use JSPropertyNameAccumulatorAddName to add property names to accumulator. A class's getPropertyNames callback only needs to provide the names of properties that the class vends through a custom getProperty or setProperty callback. Other properties, including statically declared properties, properties vended by other classes, and properties belonging to object's prototype, are added independently.


JSObjectHasInstanceCallback


hasInstance The callback invoked when an object is used as the target of an 'instanceof' expression.

typedef bool ( *JSObjectHasInstanceCallback) (
    JSContextRef ctx,
    JSObjectRef constructor,
    JSValueRef possibleInstance,
    JSValueRef *exception);  
Parameters
ctx
The execution context to use.
constructor
The JSObject that is the target of the 'instanceof' expression.
possibleInstance
The JSValue being tested to determine if it is an instance of constructor.
exception
A pointer to a JSValueRef in which to return an exception, if any.
Return Value

true if possibleInstance is an instance of constructor, otherwise false.

Discussion

If you named your function HasInstance, you would declare it like this:

bool HasInstance(JSContextRef ctx, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception);

If your callback were invoked by the JavaScript expression 'someValue instanceof myObject', constructor would be set to myObject and possibleInstance would be set to someValue.

If this callback is NULL, 'instanceof' expressions that target your object will return false.

Standard JavaScript practice calls for objects that implement the callAsConstructor callback to implement the hasInstance callback as well.


JSObjectHasPropertyCallback


The callback invoked when determining whether an object has a property.

typedef bool ( *JSObjectHasPropertyCallback) (
    JSContextRef ctx,
    JSObjectRef object,
    JSStringRef propertyName);  
Parameters
ctx
The execution context to use.
object
The JSObject to search for the property.
propertyName
A JSString containing the name of the property look up.
Return Value

true if object has the property, otherwise false.

Discussion

If you named your function HasProperty, you would declare it like this:

bool HasProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName);

If this function returns false, the hasProperty request forwards to object's statically declared properties, then its parent class chain (which includes the default object class), then its prototype chain.

This callback enables optimization in cases where only a property's existence needs to be known, not its value, and computing its value would be expensive.

If this callback is NULL, the getProperty callback will be used to service hasProperty requests.


JSObjectInitializeCallback


The callback invoked when an object is first created.

typedef void ( *JSObjectInitializeCallback) (
    JSContextRef ctx,
    JSObjectRef object);  
Parameters
ctx
The execution context to use.
object
The JSObject being created.
Discussion

If you named your function Initialize, you would declare it like this:

void Initialize(JSContextRef ctx, JSObjectRef object);

Unlike the other object callbacks, the initialize callback is called on the least derived class (the parent class) first, and the most derived class last.


JSObjectSetPropertyCallback


The callback invoked when setting a property's value.

typedef bool ( *JSObjectSetPropertyCallback) (
    JSContextRef ctx,
    JSObjectRef object,
    JSStringRef propertyName,
    JSValueRef value,
    JSValueRef *exception);  
Parameters
ctx
The execution context to use.
object
The JSObject on which to set the property's value.
propertyName
A JSString containing the name of the property to set.
value
A JSValue to use as the property's value.
exception
A pointer to a JSValueRef in which to return an exception, if any.
Return Value

true if the property was set, otherwise false.

Discussion

If you named your function SetProperty, you would declare it like this:

bool SetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception);

If this function returns false, the set request forwards to object's statically declared properties, then its parent class chain (which includes the default object class).


JSPropertyAttributes


A set of JSPropertyAttributes. Combine multiple attributes by logically ORing them together.

typedef unsigned JSPropertyAttributes;  


JSStaticFunction


This structure describes a statically declared function property.

typedef struct { 
    const char *const name; 
    JSObjectCallAsFunctionCallback callAsFunction; 
    JSPropertyAttributes attributes; 
} JSStaticFunction;  
Fields
name
A null-terminated UTF8 string containing the property's name.
callAsFunction
A JSObjectCallAsFunctionCallback to invoke when the property is called as a function.
attributes
A logically ORed set of JSPropertyAttributes to give to the property.


JSStaticValue


This structure describes a statically declared value property.

typedef struct { 
    const char *const name; 
    JSObjectGetPropertyCallback getProperty; 
    JSObjectSetPropertyCallback setProperty; 
    JSPropertyAttributes attributes; 
} JSStaticValue;  
Fields
name
A null-terminated UTF8 string containing the property's name.
getProperty
A JSObjectGetPropertyCallback to invoke when getting the property's value.
setProperty
A JSObjectSetPropertyCallback to invoke when setting the property's value. May be NULL if the ReadOnly attribute is set.
attributes
A logically ORed set of JSPropertyAttributes to give to the property.

Enumerations


JSClassAttribute


enum { 
    kJSClassAttributeNone = 0, 
    kJSClassAttributeNoAutomaticPrototype = 1 << 1 
};  
Constants
kJSClassAttributeNone
Specifies that a class has no special attributes.
kJSClassAttributeNoAutomaticPrototype
Specifies that a class should not automatically generate a shared prototype for its instance objects. Use kJSClassAttributeNoAutomaticPrototype in combination with JSObjectSetPrototype to manage prototypes manually.


JSPropertyAttribute


enum { 
    kJSPropertyAttributeNone = 0, 
    kJSPropertyAttributeReadOnly = 1 << 1, 
    kJSPropertyAttributeDontEnum = 1 << 2, 
    kJSPropertyAttributeDontDelete = 1 << 3 
};  
Constants
kJSPropertyAttributeNone
Specifies that a property has no special attributes.
kJSPropertyAttributeReadOnly
Specifies that a property is read-only.
kJSPropertyAttributeDontEnum
Specifies that a property should not be enumerated by JSPropertyEnumerators and JavaScript for...in loops.
kJSPropertyAttributeDontDelete
Specifies that the delete operation should fail on a property.


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.
Last Updated: 2008-03-11