Arrays
QuickJS provides functions to create and work with JavaScript arrays.Creating Arrays
JS_NewArray
Create a new empty JavaScript array.Parameters
ctx- The JavaScript context
Returns
Returns a new empty array.Example
JS_NewArrayFrom
Create a new array from a C array of values.Parameters
ctx- The JavaScript contextcount- Number of elementsvalues- Array of values (ownership transferred)
Returns
Returns a new array containing the specified values.Example
Array Type Checking
JS_IsArray
Check if a value is an array.Example
JS_GetProxyTarget() first.
Array Operations
Getting Array Length
Example
Setting Array Length
Example
Accessing Array Elements
Use property access functions with numeric indices:Example
Iterating Over Arrays
Typed Arrays and Array Buffers
QuickJS also supports TypedArrays and ArrayBuffers through intrinsic functions. These are available after callingJS_AddIntrinsicTypedArrays():
JS_NewUint8Array()- Create Uint8ArrayJS_NewArrayBuffer()- Create ArrayBufferJS_GetUint8Array()- Get data from Uint8ArrayJS_GetArrayBuffer()- Get data from ArrayBuffer
Array-like Operations
Pushing Values
Calling Array Methods
UseJS_Invoke() to call array methods:
Notes
- Arrays are regular objects with a
lengthproperty - Use
JS_SetPropertyUint32()for numeric indices JS_NewArrayFrom()takes ownership of the values array- Array indices are
uint32_t(0 to 2^32-1) - For large indices or negative indices, use
JS_GetPropertyInt64() - Always check return values and free JSValues properly