Skip to main content

Property Access

These functions provide access to JavaScript object properties from C code. Properties can be accessed using atoms, strings, or numeric indices.

Getting Properties

JS_GetProperty

Gets a property value using an atom.
Parameters:
  • ctx - JavaScript context
  • this_obj - Object to get property from
  • prop - Property name as atom
Returns: Property value, or JS_EXCEPTION on error Example:

JS_GetPropertyStr

Gets a property value using a C string.
Parameters:
  • ctx - JavaScript context
  • this_obj - Object to get property from
  • prop - Property name as null-terminated string
Returns: Property value, or JS_EXCEPTION on error Example:

JS_GetPropertyUint32

Gets a property value using a numeric index.
Parameters:
  • ctx - JavaScript context
  • this_obj - Object to get property from
  • idx - Property index (0-based)
Returns: Property value, or JS_EXCEPTION on error Example:

JS_GetPropertyInt64

Gets a property value using a 64-bit integer index.
Parameters:
  • ctx - JavaScript context
  • this_obj - Object to get property from
  • idx - Property index
Returns: Property value, or JS_EXCEPTION on error

Setting Properties

JS_SetProperty

Sets a property value using an atom.
Parameters:
  • ctx - JavaScript context
  • this_obj - Object to set property on
  • prop - Property name as atom
  • val - Value to set (ownership transferred)
Returns:
  • 1 on success
  • 0 on failure
  • -1 on exception
Note: This function takes ownership of val. Do not call JS_FreeValue() on it. Example:

JS_SetPropertyStr

Sets a property value using a C string.
Parameters:
  • ctx - JavaScript context
  • this_obj - Object to set property on
  • prop - Property name as null-terminated string
  • val - Value to set (ownership transferred)
Returns:
  • 1 on success
  • 0 on failure
  • -1 on exception
Example:

JS_SetPropertyUint32

Sets a property value using a numeric index.
Parameters:
  • ctx - JavaScript context
  • this_obj - Object to set property on
  • idx - Property index (0-based)
  • val - Value to set (ownership transferred)
Returns:
  • 1 on success
  • 0 on failure
  • -1 on exception
Example:

JS_SetPropertyInt64

Sets a property value using a 64-bit integer index.
Parameters:
  • ctx - JavaScript context
  • this_obj - Object to set property on
  • idx - Property index
  • val - Value to set (ownership transferred)
Returns:
  • 1 on success
  • 0 on failure
  • -1 on exception

Checking Properties

JS_HasProperty

Checks if an object has a property.
Parameters:
  • ctx - JavaScript context
  • this_obj - Object to check
  • prop - Property name as atom
Returns:
  • 1 if property exists
  • 0 if property does not exist
  • -1 on exception
Example:

JS_DeleteProperty

Deletes a property from an object.
Parameters:
  • ctx - JavaScript context
  • obj - Object to delete property from
  • prop - Property name as atom
  • flags - Combination of JS_PROP_* flags
Returns:
  • 1 on success
  • 0 on failure
  • -1 on exception

Advanced Property Operations

JS_DefineProperty

Defines a property with a descriptor.
Parameters:
  • ctx - JavaScript context
  • this_obj - Object to define property on
  • prop - Property name as atom
  • val - Property value (or JS_UNDEFINED)
  • getter - Getter function (or JS_UNDEFINED)
  • setter - Setter function (or JS_UNDEFINED)
  • flags - Property flags (see below)
Returns:
  • 0 on success
  • -1 on exception
Flags:

JS_DefinePropertyValue

Defines a data property with a value.
Parameters:
  • ctx - JavaScript context
  • this_obj - Object to define property on
  • prop - Property name as atom
  • val - Property value (ownership transferred)
  • flags - Property flags
Returns:
  • 0 on success
  • -1 on exception

JS_DefinePropertyValueStr

Defines a data property with a value using a string name.
Parameters:
  • ctx - JavaScript context
  • this_obj - Object to define property on
  • prop - Property name as null-terminated string
  • val - Property value (ownership transferred)
  • flags - Property flags
Returns:
  • 0 on success
  • -1 on exception

JS_DefinePropertyValueUint32

Defines a data property with a value using a numeric index.
Parameters:
  • ctx - JavaScript context
  • this_obj - Object to define property on
  • idx - Property index
  • val - Property value (ownership transferred)
  • flags - Property flags
Returns:
  • 0 on success
  • -1 on exception

JS_DefinePropertyGetSet

Defines an accessor property with getter and setter.
Parameters:
  • ctx - JavaScript context
  • this_obj - Object to define property on
  • prop - Property name as atom
  • getter - Getter function (ownership transferred)
  • setter - Setter function (ownership transferred)
  • flags - Property flags
Returns:
  • 0 on success
  • -1 on exception

JS_GetOwnProperty

Gets the property descriptor for an own property.
Parameters:
  • ctx - JavaScript context
  • desc - Pointer to property descriptor structure
  • obj - Object to query
  • prop - Property name as atom
Returns:
  • 1 if property exists
  • 0 if property does not exist
  • -1 on exception
Property Descriptor:

JS_GetOwnPropertyNames

Gets an array of property names from an object.
Parameters:
  • ctx - JavaScript context
  • ptab - Pointer to receive property array
  • plen - Pointer to receive property count
  • obj - Object to query
  • flags - Combination of JS_GPN_* flags
Returns:
  • 0 on success
  • -1 on exception
Flags:
Example:

JS_FreePropertyEnum

Frees a property enumeration array.
Parameters:
  • ctx - JavaScript context
  • tab - Property array to free
  • len - Number of properties

Special Property Operations

JS_GetPrototype

Gets the prototype of an object.
Parameters:
  • ctx - JavaScript context
  • val - Object to get prototype from
Returns: The prototype object, or JS_EXCEPTION on error

JS_SetPrototype

Sets the prototype of an object.
Parameters:
  • ctx - JavaScript context
  • obj - Object to set prototype on
  • proto_val - New prototype object or JS_NULL
Returns:
  • 0 on success
  • -1 on exception

JS_GetLength

Gets the length property of an object.
Parameters:
  • ctx - JavaScript context
  • obj - Object to get length from
  • pres - Pointer to store the length
Returns:
  • 0 on success
  • -1 on exception

JS_SetLength

Sets the length property of an object.
Parameters:
  • ctx - JavaScript context
  • obj - Object to set length on
  • len - New length value
Returns:
  • 0 on success
  • -1 on exception