JS_SetArrayLength

Set the length property of an object.

Syntax

bool
JS_SetArrayLength(JSContext *cx, JS::Handle<JSObject*> obj, uint32_t length);
Name Type Description
cx JSContext * The context in which to change the length of the array. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
obj JS::Handle&lt;JSObject*&gt; The array to modify.
length uint32_t Number of array elements to set.

Description

JS_SetArrayLength sets the .length property of an object obj. length indicates the number of elements. JS_SetArrayLength(cx, obj, n) is exactly the same as setting the length property of obj to n using JS_SetProperty. This is true even if obj is not an Array object.

You can call JS_SetArrayLength either to set the number of elements for an array object you created without specifying an initial number of elements, or to change the number of elements allocated for an array. If you set a shorter array length on an existing array, the elements that no longer fit in the array are destroyed.

Setting the number of array elements does not initialize those elements. To initialize an element call JS_DefineElement. If you call JS_SetArrayLength on an existing array, and length is less than the highest index number for previously defined elements, all elements greater than or equal to length are automatically deleted.

On success, JS_SetArrayLength returns true. Otherwise it returns false.

See Also