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.ctx- JavaScript contextval- Value to convert
1for truthy values0for falsy values-1forJS_EXCEPTION
JS_ToBoolean
Converts a JavaScript value to a JavaScript boolean.ctx- JavaScript contextval- Value to convert
JS_ToInt32
Converts a JavaScript value to a 32-bit signed integer.ctx- JavaScript contextpres- Pointer to store the resultval- Value to convert
0on success-1on exception
JS_ToUint32
Converts a JavaScript value to a 32-bit unsigned integer.ctx- JavaScript contextpres- Pointer to store the resultval- Value to convert
0on success-1on exception
JS_ToInt64
Converts a JavaScript value to a 64-bit signed integer.ctx- JavaScript contextpres- Pointer to store the resultval- Value to convert
0on success-1on exception
JS_ToInt64Ext
Converts a JavaScript value (including BigInt) to a 64-bit signed integer.ctx- JavaScript contextpres- Pointer to store the resultval- Value to convert (Number or BigInt)
0on success-1on exception
JS_ToInt64(), this function also accepts BigInt values.
JS_ToIndex
Converts a JavaScript value to a valid array index (0 to 2^53-1).ctx- JavaScript contextplen- Pointer to store the resultval- Value to convert
0on success-1on exception
JS_ToFloat64
Converts a JavaScript value to a double-precision floating point number.ctx- JavaScript contextpres- Pointer to store the resultval- Value to convert
0on success-1on exception
JS_ToBigInt64
Converts a JavaScript BigInt to a 64-bit signed integer.ctx- JavaScript contextpres- Pointer to store the resultval- BigInt value to convert
0on success-1on exception (including ifvalis a Number)
val is a Number instead of a BigInt.
JS_ToBigUint64
Converts a JavaScript BigInt to a 64-bit unsigned integer.ctx- JavaScript contextpres- Pointer to store the resultval- BigInt value to convert
0on success-1on exception
To String
JS_ToString
Converts a JavaScript value to a JavaScript string.ctx- JavaScript contextval- Value to convert
JS_EXCEPTION on error
JS_ToCString
Converts a JavaScript value to a null-terminated C string.ctx- JavaScript contextval1- Value to convert
NULL on error
Example:
JS_FreeCString() when no longer needed.
JS_ToCStringLen
Converts a JavaScript value to a C string with length.ctx- JavaScript contextplen- Pointer to store the string length (can beNULL)val1- Value to convert
NULL on error
Example:
JS_ToCStringLen2
Converts a JavaScript value to a C string with optional CESU-8 encoding.ctx- JavaScript contextplen- Pointer to store the string length (can beNULL)val1- Value to convertcesu8- If true, use CESU-8 encoding; otherwise use UTF-8
NULL on error
JS_ToCStringUTF16
Converts a JavaScript value to a UTF-16 encoded C string.ctx- JavaScript contextval1- Value to convert
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.ctx- JavaScript contextplen- Pointer to store the length in uint16_t units (can beNULL)val1- Value to convert
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.ctx- JavaScript contextptr- String to free (can beNULL)
JS_FreeCStringRT
Frees a C string using the runtime.rt- JavaScript runtimeptr- String to free (can beNULL)
JS_FreeCStringUTF16
Frees a UTF-16 C string.ctx- JavaScript contextptr- UTF-16 string to free (can beNULL)
JS_FreeCStringRT_UTF16
Frees a UTF-16 C string using the runtime.rt- JavaScript runtimeptr- UTF-16 string to free (can beNULL)
To Object
JS_ToObject
Converts a JavaScript value to an object.ctx- JavaScript contextval- Value to convert
JS_EXCEPTION on error
Example:
JS_ToObjectString
Converts a JavaScript value to an Object wrapper string.ctx- JavaScript contextval- Value to convert
JS_EXCEPTION on error
To Other Types
JS_ToNumber
Converts a JavaScript value to a number.ctx- JavaScript contextval- Value to convert
JS_EXCEPTION on error
JS_ToPropertyKey
Converts a JavaScript value to a property key (string or symbol).ctx- JavaScript contextval- Value to convert
JS_EXCEPTION on error