Skip to main content

Value Conversion

These functions convert JavaScript values to C types and vice versa. Most conversion functions can throw JavaScript exceptions, which must be checked by the caller.

To C Types

JS_ToBool

Converts a JavaScript value to a C boolean.
Parameters:
  • ctx - JavaScript context
  • val - Value to convert
Returns:
  • 1 for truthy values
  • 0 for falsy values
  • -1 for JS_EXCEPTION
Example:

JS_ToBoolean

Converts a JavaScript value to a JavaScript boolean.
Parameters:
  • ctx - JavaScript context
  • val - Value to convert
Returns: A new JavaScript boolean value

JS_ToInt32

Converts a JavaScript value to a 32-bit signed integer.
Parameters:
  • ctx - JavaScript context
  • pres - Pointer to store the result
  • val - Value to convert
Returns:
  • 0 on success
  • -1 on exception
Example:

JS_ToUint32

Converts a JavaScript value to a 32-bit unsigned integer.
Parameters:
  • ctx - JavaScript context
  • pres - Pointer to store the result
  • val - Value to convert
Returns:
  • 0 on success
  • -1 on exception

JS_ToInt64

Converts a JavaScript value to a 64-bit signed integer.
Parameters:
  • ctx - JavaScript context
  • pres - Pointer to store the result
  • val - Value to convert
Returns:
  • 0 on success
  • -1 on exception

JS_ToInt64Ext

Converts a JavaScript value (including BigInt) to a 64-bit signed integer.
Parameters:
  • ctx - JavaScript context
  • pres - Pointer to store the result
  • val - Value to convert (Number or BigInt)
Returns:
  • 0 on success
  • -1 on exception
Note: Unlike JS_ToInt64(), this function also accepts BigInt values.

JS_ToIndex

Converts a JavaScript value to a valid array index (0 to 2^53-1).
Parameters:
  • ctx - JavaScript context
  • plen - Pointer to store the result
  • val - Value to convert
Returns:
  • 0 on success
  • -1 on exception

JS_ToFloat64

Converts a JavaScript value to a double-precision floating point number.
Parameters:
  • ctx - JavaScript context
  • pres - Pointer to store the result
  • val - Value to convert
Returns:
  • 0 on success
  • -1 on exception
Example:

JS_ToBigInt64

Converts a JavaScript BigInt to a 64-bit signed integer.
Parameters:
  • ctx - JavaScript context
  • pres - Pointer to store the result
  • val - BigInt value to convert
Returns:
  • 0 on success
  • -1 on exception (including if val is a Number)
Note: Returns an exception if val is a Number instead of a BigInt.

JS_ToBigUint64

Converts a JavaScript BigInt to a 64-bit unsigned integer.
Parameters:
  • ctx - JavaScript context
  • pres - Pointer to store the result
  • val - BigInt value to convert
Returns:
  • 0 on success
  • -1 on exception

To String

JS_ToString

Converts a JavaScript value to a JavaScript string.
Parameters:
  • ctx - JavaScript context
  • val - Value to convert
Returns: A new JavaScript string, or JS_EXCEPTION on error

JS_ToCString

Converts a JavaScript value to a null-terminated C string.
Parameters:
  • ctx - JavaScript context
  • val1 - Value to convert
Returns: A null-terminated UTF-8 string, or NULL on error Example:
Note: The returned string must be freed with JS_FreeCString() when no longer needed.

JS_ToCStringLen

Converts a JavaScript value to a C string with length.
Parameters:
  • ctx - JavaScript context
  • plen - Pointer to store the string length (can be NULL)
  • val1 - Value to convert
Returns: A UTF-8 string (not necessarily null-terminated), or NULL on error Example:

JS_ToCStringLen2

Converts a JavaScript value to a C string with optional CESU-8 encoding.
Parameters:
  • ctx - JavaScript context
  • plen - Pointer to store the string length (can be NULL)
  • val1 - Value to convert
  • cesu8 - If true, use CESU-8 encoding; otherwise use UTF-8
Returns: A string in the specified encoding, or NULL on error

JS_ToCStringUTF16

Converts a JavaScript value to a UTF-16 encoded C string.
Parameters:
  • ctx - JavaScript context
  • val1 - Value to convert
Returns: A UTF-16 encoded string (not null-terminated), or NULL on error Note: The returned string must be freed with JS_FreeCStringUTF16(). The string may contain unmatched surrogates and is not null-terminated.

JS_ToCStringLenUTF16

Converts a JavaScript value to a UTF-16 encoded C string with length.
Parameters:
  • ctx - JavaScript context
  • plen - Pointer to store the length in uint16_t units (can be NULL)
  • val1 - Value to convert
Returns: A UTF-16 encoded string in native endianness, or NULL on error Note: The length is in uint16_t units, not code points. A surrogate pair has length 2, an unmatched surrogate has length 1.

JS_FreeCString

Frees a C string returned by conversion functions.
Parameters:
  • ctx - JavaScript context
  • ptr - String to free (can be NULL)

JS_FreeCStringRT

Frees a C string using the runtime.
Parameters:
  • rt - JavaScript runtime
  • ptr - String to free (can be NULL)

JS_FreeCStringUTF16

Frees a UTF-16 C string.
Parameters:
  • ctx - JavaScript context
  • ptr - UTF-16 string to free (can be NULL)

JS_FreeCStringRT_UTF16

Frees a UTF-16 C string using the runtime.
Parameters:
  • rt - JavaScript runtime
  • ptr - UTF-16 string to free (can be NULL)

To Object

JS_ToObject

Converts a JavaScript value to an object.
Parameters:
  • ctx - JavaScript context
  • val - Value to convert
Returns: A new JavaScript object, or JS_EXCEPTION on error Example:

JS_ToObjectString

Converts a JavaScript value to an Object wrapper string.
Parameters:
  • ctx - JavaScript context
  • val - Value to convert
Returns: A new String object, or JS_EXCEPTION on error

To Other Types

JS_ToNumber

Converts a JavaScript value to a number.
Parameters:
  • ctx - JavaScript context
  • val - Value to convert
Returns: A new JavaScript number, or JS_EXCEPTION on error

JS_ToPropertyKey

Converts a JavaScript value to a property key (string or symbol).
Parameters:
  • ctx - JavaScript context
  • val - Value to convert
Returns: A new JavaScript string or symbol, or JS_EXCEPTION on error

Error Handling

Most conversion functions can fail and return an error indicator:
For string conversions: