michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* JS Array interface. */ michael@0: michael@0: #ifndef jsarray_h michael@0: #define jsarray_h michael@0: michael@0: #include "jsobj.h" michael@0: #include "jspubtd.h" michael@0: michael@0: namespace js { michael@0: /* 2^32-2, inclusive */ michael@0: const uint32_t MAX_ARRAY_INDEX = 4294967294u; michael@0: } michael@0: michael@0: inline bool michael@0: js_IdIsIndex(jsid id, uint32_t *indexp) michael@0: { michael@0: if (JSID_IS_INT(id)) { michael@0: int32_t i = JSID_TO_INT(id); michael@0: JS_ASSERT(i >= 0); michael@0: *indexp = (uint32_t)i; michael@0: return true; michael@0: } michael@0: michael@0: if (MOZ_UNLIKELY(!JSID_IS_STRING(id))) michael@0: return false; michael@0: michael@0: return js::StringIsArrayIndex(JSID_TO_ATOM(id), indexp); michael@0: } michael@0: michael@0: extern JSObject * michael@0: js_InitArrayClass(JSContext *cx, js::HandleObject obj); michael@0: michael@0: extern bool michael@0: js_InitContextBusyArrayTable(JSContext *cx); michael@0: michael@0: namespace js { michael@0: michael@0: class ArrayObject; michael@0: michael@0: /* Create a dense array with no capacity allocated, length set to 0. */ michael@0: extern ArrayObject * JS_FASTCALL michael@0: NewDenseEmptyArray(JSContext *cx, JSObject *proto = nullptr, michael@0: NewObjectKind newKind = GenericObject); michael@0: michael@0: /* Create a dense array with length and capacity == 'length', initialized length set to 0. */ michael@0: extern ArrayObject * JS_FASTCALL michael@0: NewDenseAllocatedArray(ExclusiveContext *cx, uint32_t length, JSObject *proto = nullptr, michael@0: NewObjectKind newKind = GenericObject); michael@0: michael@0: /* michael@0: * Create a dense array with a set length, but without allocating space for the michael@0: * contents. This is useful, e.g., when accepting length from the user. michael@0: */ michael@0: extern ArrayObject * JS_FASTCALL michael@0: NewDenseUnallocatedArray(ExclusiveContext *cx, uint32_t length, JSObject *proto = nullptr, michael@0: NewObjectKind newKind = GenericObject); michael@0: michael@0: /* Create a dense array with a copy of the dense array elements in src. */ michael@0: extern ArrayObject * michael@0: NewDenseCopiedArray(JSContext *cx, uint32_t length, HandleObject src, uint32_t elementOffset, JSObject *proto = nullptr); michael@0: michael@0: /* Create a dense array from the given array values, which must be rooted */ michael@0: extern ArrayObject * michael@0: NewDenseCopiedArray(JSContext *cx, uint32_t length, const Value *values, JSObject *proto = nullptr, michael@0: NewObjectKind newKind = GenericObject); michael@0: michael@0: /* Create a dense array based on templateObject with the given length. */ michael@0: extern ArrayObject * michael@0: NewDenseAllocatedArrayWithTemplate(JSContext *cx, uint32_t length, JSObject *templateObject); michael@0: michael@0: /* michael@0: * Determines whether a write to the given element on |obj| should fail because michael@0: * |obj| is an Array with a non-writable length, and writing that element would michael@0: * increase the length of the array. michael@0: */ michael@0: extern bool michael@0: WouldDefinePastNonwritableLength(ThreadSafeContext *cx, michael@0: HandleObject obj, uint32_t index, bool strict, michael@0: bool *definesPast); michael@0: michael@0: /* michael@0: * Canonicalize |vp| to a uint32_t value potentially suitable for use as an michael@0: * array length. michael@0: * michael@0: * For parallel execution we can only canonicalize non-object values. michael@0: */ michael@0: template michael@0: extern bool michael@0: CanonicalizeArrayLengthValue(typename ExecutionModeTraits::ContextType cx, michael@0: HandleValue v, uint32_t *canonicalized); michael@0: michael@0: extern bool michael@0: GetLengthProperty(JSContext *cx, HandleObject obj, uint32_t *lengthp); michael@0: michael@0: extern bool michael@0: SetLengthProperty(JSContext *cx, HandleObject obj, double length); michael@0: michael@0: extern bool michael@0: ObjectMayHaveExtraIndexedProperties(JSObject *obj); michael@0: michael@0: /* michael@0: * Copy 'length' elements from aobj to vp. michael@0: * michael@0: * This function assumes 'length' is effectively the result of calling michael@0: * js_GetLengthProperty on aobj. vp must point to rooted memory. michael@0: */ michael@0: extern bool michael@0: GetElements(JSContext *cx, HandleObject aobj, uint32_t length, js::Value *vp); michael@0: michael@0: /* Natives exposed for optimization by the interpreter and JITs. */ michael@0: michael@0: extern bool michael@0: array_sort(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: array_push(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: array_pop(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: array_splice(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: array_splice_impl(JSContext *cx, unsigned argc, js::Value *vp, bool pop); michael@0: michael@0: extern bool michael@0: array_concat(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: array_concat_dense(JSContext *cx, Handle arr1, Handle arr2, michael@0: Handle result); michael@0: michael@0: extern void michael@0: ArrayShiftMoveElements(JSObject *obj); michael@0: michael@0: extern bool michael@0: array_shift(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: /* michael@0: * Append the given (non-hole) value to the end of an array. The array must be michael@0: * a newborn array -- that is, one which has not been exposed to script for michael@0: * arbitrary manipulation. (This method optimizes on the assumption that michael@0: * extending the array to accommodate the element will never make the array michael@0: * sparse, which requires that the array be completely filled.) michael@0: */ michael@0: extern bool michael@0: NewbornArrayPush(JSContext *cx, HandleObject obj, const Value &v); michael@0: michael@0: } /* namespace js */ michael@0: michael@0: #ifdef DEBUG michael@0: extern bool michael@0: js_ArrayInfo(JSContext *cx, unsigned argc, js::Value *vp); michael@0: #endif michael@0: michael@0: /* Array constructor native. Exposed only so the JIT can know its address. */ michael@0: bool michael@0: js_Array(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: #endif /* jsarray_h */