Visit Mozilla.org

JS NewArrayObject

From MDC

Create a new Array object.

Contents

[edit] Syntax

JSObject * JS_NewArrayObject(JSContext *cx, jsint length, jsval *vector);
Name Type Description
cx JSContext * The context in which to create the new array.

Requires request. (In a JS_THREADSAFE build, the caller must be in a request on this JSContext.)

length jsint Number of elements to include in the array.
vector jsval * Pointer to the initial values for the array's elements.

[edit] Description

JS_NewArrayObject creates a new array object in the JSRuntime of the specified JSContext, cx. If array creation is successful, JS_NewArrayObject initializes each element identified by index i from 0 to length - 1 to have the value vector[i], and then returns a pointer to the new object. Otherwise JS_NewArrayObject returns NULL.

length specifies the number of elements in the array. If length is 0, JS_NewArrayObject creates an array object of length 0 and ignores vector.

[edit] Notes

It is often better to call JS_NewArrayObject(cx, 0, NULL), store the returned object in a GC root using JS_AddRoot, and then populate its elements with JS_SetElement or JS_DefineElement, to then drop the root object using JS_RemoveRoot. This avoids unrooted jsvals in vector from being subject to garbage collection until the new object has been populated.

[edit] See Also

LXR ID Search for JS_NewArrayObject

JS_DefineElement, JS_DeleteElement, JS_GetArrayLength, JS_GetElement, JS_IsArrayObject, JS_LookupElement, JS_SetArrayLength, JS_SetElement