1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jsarray.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,167 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +/* JS Array interface. */ 1.11 + 1.12 +#ifndef jsarray_h 1.13 +#define jsarray_h 1.14 + 1.15 +#include "jsobj.h" 1.16 +#include "jspubtd.h" 1.17 + 1.18 +namespace js { 1.19 +/* 2^32-2, inclusive */ 1.20 +const uint32_t MAX_ARRAY_INDEX = 4294967294u; 1.21 +} 1.22 + 1.23 +inline bool 1.24 +js_IdIsIndex(jsid id, uint32_t *indexp) 1.25 +{ 1.26 + if (JSID_IS_INT(id)) { 1.27 + int32_t i = JSID_TO_INT(id); 1.28 + JS_ASSERT(i >= 0); 1.29 + *indexp = (uint32_t)i; 1.30 + return true; 1.31 + } 1.32 + 1.33 + if (MOZ_UNLIKELY(!JSID_IS_STRING(id))) 1.34 + return false; 1.35 + 1.36 + return js::StringIsArrayIndex(JSID_TO_ATOM(id), indexp); 1.37 +} 1.38 + 1.39 +extern JSObject * 1.40 +js_InitArrayClass(JSContext *cx, js::HandleObject obj); 1.41 + 1.42 +extern bool 1.43 +js_InitContextBusyArrayTable(JSContext *cx); 1.44 + 1.45 +namespace js { 1.46 + 1.47 +class ArrayObject; 1.48 + 1.49 +/* Create a dense array with no capacity allocated, length set to 0. */ 1.50 +extern ArrayObject * JS_FASTCALL 1.51 +NewDenseEmptyArray(JSContext *cx, JSObject *proto = nullptr, 1.52 + NewObjectKind newKind = GenericObject); 1.53 + 1.54 +/* Create a dense array with length and capacity == 'length', initialized length set to 0. */ 1.55 +extern ArrayObject * JS_FASTCALL 1.56 +NewDenseAllocatedArray(ExclusiveContext *cx, uint32_t length, JSObject *proto = nullptr, 1.57 + NewObjectKind newKind = GenericObject); 1.58 + 1.59 +/* 1.60 + * Create a dense array with a set length, but without allocating space for the 1.61 + * contents. This is useful, e.g., when accepting length from the user. 1.62 + */ 1.63 +extern ArrayObject * JS_FASTCALL 1.64 +NewDenseUnallocatedArray(ExclusiveContext *cx, uint32_t length, JSObject *proto = nullptr, 1.65 + NewObjectKind newKind = GenericObject); 1.66 + 1.67 +/* Create a dense array with a copy of the dense array elements in src. */ 1.68 +extern ArrayObject * 1.69 +NewDenseCopiedArray(JSContext *cx, uint32_t length, HandleObject src, uint32_t elementOffset, JSObject *proto = nullptr); 1.70 + 1.71 +/* Create a dense array from the given array values, which must be rooted */ 1.72 +extern ArrayObject * 1.73 +NewDenseCopiedArray(JSContext *cx, uint32_t length, const Value *values, JSObject *proto = nullptr, 1.74 + NewObjectKind newKind = GenericObject); 1.75 + 1.76 +/* Create a dense array based on templateObject with the given length. */ 1.77 +extern ArrayObject * 1.78 +NewDenseAllocatedArrayWithTemplate(JSContext *cx, uint32_t length, JSObject *templateObject); 1.79 + 1.80 +/* 1.81 + * Determines whether a write to the given element on |obj| should fail because 1.82 + * |obj| is an Array with a non-writable length, and writing that element would 1.83 + * increase the length of the array. 1.84 + */ 1.85 +extern bool 1.86 +WouldDefinePastNonwritableLength(ThreadSafeContext *cx, 1.87 + HandleObject obj, uint32_t index, bool strict, 1.88 + bool *definesPast); 1.89 + 1.90 +/* 1.91 + * Canonicalize |vp| to a uint32_t value potentially suitable for use as an 1.92 + * array length. 1.93 + * 1.94 + * For parallel execution we can only canonicalize non-object values. 1.95 + */ 1.96 +template <ExecutionMode mode> 1.97 +extern bool 1.98 +CanonicalizeArrayLengthValue(typename ExecutionModeTraits<mode>::ContextType cx, 1.99 + HandleValue v, uint32_t *canonicalized); 1.100 + 1.101 +extern bool 1.102 +GetLengthProperty(JSContext *cx, HandleObject obj, uint32_t *lengthp); 1.103 + 1.104 +extern bool 1.105 +SetLengthProperty(JSContext *cx, HandleObject obj, double length); 1.106 + 1.107 +extern bool 1.108 +ObjectMayHaveExtraIndexedProperties(JSObject *obj); 1.109 + 1.110 +/* 1.111 + * Copy 'length' elements from aobj to vp. 1.112 + * 1.113 + * This function assumes 'length' is effectively the result of calling 1.114 + * js_GetLengthProperty on aobj. vp must point to rooted memory. 1.115 + */ 1.116 +extern bool 1.117 +GetElements(JSContext *cx, HandleObject aobj, uint32_t length, js::Value *vp); 1.118 + 1.119 +/* Natives exposed for optimization by the interpreter and JITs. */ 1.120 + 1.121 +extern bool 1.122 +array_sort(JSContext *cx, unsigned argc, js::Value *vp); 1.123 + 1.124 +extern bool 1.125 +array_push(JSContext *cx, unsigned argc, js::Value *vp); 1.126 + 1.127 +extern bool 1.128 +array_pop(JSContext *cx, unsigned argc, js::Value *vp); 1.129 + 1.130 +extern bool 1.131 +array_splice(JSContext *cx, unsigned argc, js::Value *vp); 1.132 + 1.133 +extern bool 1.134 +array_splice_impl(JSContext *cx, unsigned argc, js::Value *vp, bool pop); 1.135 + 1.136 +extern bool 1.137 +array_concat(JSContext *cx, unsigned argc, js::Value *vp); 1.138 + 1.139 +extern bool 1.140 +array_concat_dense(JSContext *cx, Handle<ArrayObject*> arr1, Handle<ArrayObject*> arr2, 1.141 + Handle<ArrayObject*> result); 1.142 + 1.143 +extern void 1.144 +ArrayShiftMoveElements(JSObject *obj); 1.145 + 1.146 +extern bool 1.147 +array_shift(JSContext *cx, unsigned argc, js::Value *vp); 1.148 + 1.149 +/* 1.150 + * Append the given (non-hole) value to the end of an array. The array must be 1.151 + * a newborn array -- that is, one which has not been exposed to script for 1.152 + * arbitrary manipulation. (This method optimizes on the assumption that 1.153 + * extending the array to accommodate the element will never make the array 1.154 + * sparse, which requires that the array be completely filled.) 1.155 + */ 1.156 +extern bool 1.157 +NewbornArrayPush(JSContext *cx, HandleObject obj, const Value &v); 1.158 + 1.159 +} /* namespace js */ 1.160 + 1.161 +#ifdef DEBUG 1.162 +extern bool 1.163 +js_ArrayInfo(JSContext *cx, unsigned argc, js::Value *vp); 1.164 +#endif 1.165 + 1.166 +/* Array constructor native. Exposed only so the JIT can know its address. */ 1.167 +bool 1.168 +js_Array(JSContext *cx, unsigned argc, js::Value *vp); 1.169 + 1.170 +#endif /* jsarray_h */