Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "jsarray.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "mozilla/ArrayUtils.h" |
michael@0 | 10 | #include "mozilla/DebugOnly.h" |
michael@0 | 11 | #include "mozilla/FloatingPoint.h" |
michael@0 | 12 | #include "mozilla/MathAlgorithms.h" |
michael@0 | 13 | |
michael@0 | 14 | #include "jsapi.h" |
michael@0 | 15 | #include "jsatom.h" |
michael@0 | 16 | #include "jscntxt.h" |
michael@0 | 17 | #include "jsfriendapi.h" |
michael@0 | 18 | #include "jsfun.h" |
michael@0 | 19 | #include "jsiter.h" |
michael@0 | 20 | #include "jsnum.h" |
michael@0 | 21 | #include "jsobj.h" |
michael@0 | 22 | #include "jstypes.h" |
michael@0 | 23 | #include "jsutil.h" |
michael@0 | 24 | |
michael@0 | 25 | #include "ds/Sort.h" |
michael@0 | 26 | #include "gc/Heap.h" |
michael@0 | 27 | #include "vm/ArgumentsObject.h" |
michael@0 | 28 | #include "vm/ForkJoin.h" |
michael@0 | 29 | #include "vm/Interpreter.h" |
michael@0 | 30 | #include "vm/NumericConversions.h" |
michael@0 | 31 | #include "vm/Shape.h" |
michael@0 | 32 | #include "vm/StringBuffer.h" |
michael@0 | 33 | |
michael@0 | 34 | #include "jsatominlines.h" |
michael@0 | 35 | |
michael@0 | 36 | #include "vm/ArgumentsObject-inl.h" |
michael@0 | 37 | #include "vm/ArrayObject-inl.h" |
michael@0 | 38 | #include "vm/Interpreter-inl.h" |
michael@0 | 39 | #include "vm/Runtime-inl.h" |
michael@0 | 40 | |
michael@0 | 41 | using namespace js; |
michael@0 | 42 | using namespace js::gc; |
michael@0 | 43 | using namespace js::types; |
michael@0 | 44 | |
michael@0 | 45 | using mozilla::Abs; |
michael@0 | 46 | using mozilla::ArrayLength; |
michael@0 | 47 | using mozilla::CeilingLog2; |
michael@0 | 48 | using mozilla::DebugOnly; |
michael@0 | 49 | using mozilla::IsNaN; |
michael@0 | 50 | using mozilla::PointerRangeSize; |
michael@0 | 51 | |
michael@0 | 52 | bool |
michael@0 | 53 | js::GetLengthProperty(JSContext *cx, HandleObject obj, uint32_t *lengthp) |
michael@0 | 54 | { |
michael@0 | 55 | if (obj->is<ArrayObject>()) { |
michael@0 | 56 | *lengthp = obj->as<ArrayObject>().length(); |
michael@0 | 57 | return true; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | if (obj->is<ArgumentsObject>()) { |
michael@0 | 61 | ArgumentsObject &argsobj = obj->as<ArgumentsObject>(); |
michael@0 | 62 | if (!argsobj.hasOverriddenLength()) { |
michael@0 | 63 | *lengthp = argsobj.initialLength(); |
michael@0 | 64 | return true; |
michael@0 | 65 | } |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | RootedValue value(cx); |
michael@0 | 69 | if (!JSObject::getProperty(cx, obj, obj, cx->names().length, &value)) |
michael@0 | 70 | return false; |
michael@0 | 71 | |
michael@0 | 72 | if (value.isInt32()) { |
michael@0 | 73 | *lengthp = uint32_t(value.toInt32()); // uint32_t cast does ToUint32 |
michael@0 | 74 | return true; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | return ToUint32(cx, value, lengthp); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | /* |
michael@0 | 81 | * Determine if the id represents an array index. |
michael@0 | 82 | * |
michael@0 | 83 | * An id is an array index according to ECMA by (15.4): |
michael@0 | 84 | * |
michael@0 | 85 | * "Array objects give special treatment to a certain class of property names. |
michael@0 | 86 | * A property name P (in the form of a string value) is an array index if and |
michael@0 | 87 | * only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal |
michael@0 | 88 | * to 2^32-1." |
michael@0 | 89 | * |
michael@0 | 90 | * This means the largest allowed index is actually 2^32-2 (4294967294). |
michael@0 | 91 | * |
michael@0 | 92 | * In our implementation, it would be sufficient to check for JSVAL_IS_INT(id) |
michael@0 | 93 | * except that by using signed 31-bit integers we miss the top half of the |
michael@0 | 94 | * valid range. This function checks the string representation itself; note |
michael@0 | 95 | * that calling a standard conversion routine might allow strings such as |
michael@0 | 96 | * "08" or "4.0" as array indices, which they are not. |
michael@0 | 97 | * |
michael@0 | 98 | */ |
michael@0 | 99 | JS_FRIEND_API(bool) |
michael@0 | 100 | js::StringIsArrayIndex(JSLinearString *str, uint32_t *indexp) |
michael@0 | 101 | { |
michael@0 | 102 | const jschar *s = str->chars(); |
michael@0 | 103 | uint32_t length = str->length(); |
michael@0 | 104 | const jschar *end = s + length; |
michael@0 | 105 | |
michael@0 | 106 | if (length == 0 || length > (sizeof("4294967294") - 1) || !JS7_ISDEC(*s)) |
michael@0 | 107 | return false; |
michael@0 | 108 | |
michael@0 | 109 | uint32_t c = 0, previous = 0; |
michael@0 | 110 | uint32_t index = JS7_UNDEC(*s++); |
michael@0 | 111 | |
michael@0 | 112 | /* Don't allow leading zeros. */ |
michael@0 | 113 | if (index == 0 && s != end) |
michael@0 | 114 | return false; |
michael@0 | 115 | |
michael@0 | 116 | for (; s < end; s++) { |
michael@0 | 117 | if (!JS7_ISDEC(*s)) |
michael@0 | 118 | return false; |
michael@0 | 119 | |
michael@0 | 120 | previous = index; |
michael@0 | 121 | c = JS7_UNDEC(*s); |
michael@0 | 122 | index = 10 * index + c; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | /* Make sure we didn't overflow. */ |
michael@0 | 126 | if (previous < (MAX_ARRAY_INDEX / 10) || (previous == (MAX_ARRAY_INDEX / 10) && |
michael@0 | 127 | c <= (MAX_ARRAY_INDEX % 10))) { |
michael@0 | 128 | JS_ASSERT(index <= MAX_ARRAY_INDEX); |
michael@0 | 129 | *indexp = index; |
michael@0 | 130 | return true; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | return false; |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | static bool |
michael@0 | 137 | ToId(JSContext *cx, double index, MutableHandleId id) |
michael@0 | 138 | { |
michael@0 | 139 | if (index == uint32_t(index)) |
michael@0 | 140 | return IndexToId(cx, uint32_t(index), id); |
michael@0 | 141 | |
michael@0 | 142 | Value tmp = DoubleValue(index); |
michael@0 | 143 | return ValueToId<CanGC>(cx, HandleValue::fromMarkedLocation(&tmp), id); |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | static bool |
michael@0 | 147 | ToId(JSContext *cx, uint32_t index, MutableHandleId id) |
michael@0 | 148 | { |
michael@0 | 149 | return IndexToId(cx, index, id); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | /* |
michael@0 | 153 | * If the property at the given index exists, get its value into location |
michael@0 | 154 | * pointed by vp and set *hole to false. Otherwise set *hole to true and *vp |
michael@0 | 155 | * to JSVAL_VOID. This function assumes that the location pointed by vp is |
michael@0 | 156 | * properly rooted and can be used as GC-protected storage for temporaries. |
michael@0 | 157 | */ |
michael@0 | 158 | template<typename IndexType> |
michael@0 | 159 | static inline bool |
michael@0 | 160 | DoGetElement(JSContext *cx, HandleObject obj, HandleObject receiver, |
michael@0 | 161 | IndexType index, bool *hole, MutableHandleValue vp) |
michael@0 | 162 | { |
michael@0 | 163 | RootedId id(cx); |
michael@0 | 164 | if (!ToId(cx, index, &id)) |
michael@0 | 165 | return false; |
michael@0 | 166 | |
michael@0 | 167 | RootedObject obj2(cx); |
michael@0 | 168 | RootedShape prop(cx); |
michael@0 | 169 | if (!JSObject::lookupGeneric(cx, obj, id, &obj2, &prop)) |
michael@0 | 170 | return false; |
michael@0 | 171 | |
michael@0 | 172 | if (!prop) { |
michael@0 | 173 | vp.setUndefined(); |
michael@0 | 174 | *hole = true; |
michael@0 | 175 | } else { |
michael@0 | 176 | if (!JSObject::getGeneric(cx, obj, receiver, id, vp)) |
michael@0 | 177 | return false; |
michael@0 | 178 | *hole = false; |
michael@0 | 179 | } |
michael@0 | 180 | return true; |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | template<typename IndexType> |
michael@0 | 184 | static void |
michael@0 | 185 | AssertGreaterThanZero(IndexType index) |
michael@0 | 186 | { |
michael@0 | 187 | JS_ASSERT(index >= 0); |
michael@0 | 188 | JS_ASSERT(index == floor(index)); |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | template<> |
michael@0 | 192 | void |
michael@0 | 193 | AssertGreaterThanZero(uint32_t index) |
michael@0 | 194 | { |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | template<typename IndexType> |
michael@0 | 198 | static bool |
michael@0 | 199 | GetElement(JSContext *cx, HandleObject obj, HandleObject receiver, |
michael@0 | 200 | IndexType index, bool *hole, MutableHandleValue vp) |
michael@0 | 201 | { |
michael@0 | 202 | AssertGreaterThanZero(index); |
michael@0 | 203 | if (obj->isNative() && index < obj->getDenseInitializedLength()) { |
michael@0 | 204 | vp.set(obj->getDenseElement(uint32_t(index))); |
michael@0 | 205 | if (!vp.isMagic(JS_ELEMENTS_HOLE)) { |
michael@0 | 206 | *hole = false; |
michael@0 | 207 | return true; |
michael@0 | 208 | } |
michael@0 | 209 | } |
michael@0 | 210 | if (obj->is<ArgumentsObject>()) { |
michael@0 | 211 | if (obj->as<ArgumentsObject>().maybeGetElement(uint32_t(index), vp)) { |
michael@0 | 212 | *hole = false; |
michael@0 | 213 | return true; |
michael@0 | 214 | } |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | return DoGetElement(cx, obj, receiver, index, hole, vp); |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | template<typename IndexType> |
michael@0 | 221 | static inline bool |
michael@0 | 222 | GetElement(JSContext *cx, HandleObject obj, IndexType index, bool *hole, MutableHandleValue vp) |
michael@0 | 223 | { |
michael@0 | 224 | return GetElement(cx, obj, obj, index, hole, vp); |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | static bool |
michael@0 | 228 | GetElementsSlow(JSContext *cx, HandleObject aobj, uint32_t length, Value *vp) |
michael@0 | 229 | { |
michael@0 | 230 | for (uint32_t i = 0; i < length; i++) { |
michael@0 | 231 | if (!JSObject::getElement(cx, aobj, aobj, i, MutableHandleValue::fromMarkedLocation(&vp[i]))) |
michael@0 | 232 | return false; |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | return true; |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | bool |
michael@0 | 239 | js::GetElements(JSContext *cx, HandleObject aobj, uint32_t length, Value *vp) |
michael@0 | 240 | { |
michael@0 | 241 | if (aobj->is<ArrayObject>() && length <= aobj->getDenseInitializedLength() && |
michael@0 | 242 | !ObjectMayHaveExtraIndexedProperties(aobj)) |
michael@0 | 243 | { |
michael@0 | 244 | /* No other indexed properties so hole = undefined */ |
michael@0 | 245 | const Value *srcbeg = aobj->getDenseElements(); |
michael@0 | 246 | const Value *srcend = srcbeg + length; |
michael@0 | 247 | const Value *src = srcbeg; |
michael@0 | 248 | for (Value *dst = vp; src < srcend; ++dst, ++src) |
michael@0 | 249 | *dst = src->isMagic(JS_ELEMENTS_HOLE) ? UndefinedValue() : *src; |
michael@0 | 250 | return true; |
michael@0 | 251 | } |
michael@0 | 252 | |
michael@0 | 253 | if (aobj->is<ArgumentsObject>()) { |
michael@0 | 254 | ArgumentsObject &argsobj = aobj->as<ArgumentsObject>(); |
michael@0 | 255 | if (!argsobj.hasOverriddenLength()) { |
michael@0 | 256 | if (argsobj.maybeGetElements(0, length, vp)) |
michael@0 | 257 | return true; |
michael@0 | 258 | } |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | return GetElementsSlow(cx, aobj, length, vp); |
michael@0 | 262 | } |
michael@0 | 263 | |
michael@0 | 264 | /* |
michael@0 | 265 | * Set the value of the property at the given index to v assuming v is rooted. |
michael@0 | 266 | */ |
michael@0 | 267 | static bool |
michael@0 | 268 | SetArrayElement(JSContext *cx, HandleObject obj, double index, HandleValue v) |
michael@0 | 269 | { |
michael@0 | 270 | JS_ASSERT(index >= 0); |
michael@0 | 271 | |
michael@0 | 272 | if (obj->is<ArrayObject>() && !obj->isIndexed()) { |
michael@0 | 273 | Rooted<ArrayObject*> arr(cx, &obj->as<ArrayObject>()); |
michael@0 | 274 | /* Predicted/prefetched code should favor the remains-dense case. */ |
michael@0 | 275 | JSObject::EnsureDenseResult result = JSObject::ED_SPARSE; |
michael@0 | 276 | do { |
michael@0 | 277 | if (index > uint32_t(-1)) |
michael@0 | 278 | break; |
michael@0 | 279 | uint32_t idx = uint32_t(index); |
michael@0 | 280 | if (idx >= arr->length() && !arr->lengthIsWritable()) { |
michael@0 | 281 | JS_ReportErrorFlagsAndNumber(cx, JSREPORT_ERROR, js_GetErrorMessage, nullptr, |
michael@0 | 282 | JSMSG_CANT_REDEFINE_ARRAY_LENGTH); |
michael@0 | 283 | return false; |
michael@0 | 284 | } |
michael@0 | 285 | result = arr->ensureDenseElements(cx, idx, 1); |
michael@0 | 286 | if (result != JSObject::ED_OK) |
michael@0 | 287 | break; |
michael@0 | 288 | if (idx >= arr->length()) |
michael@0 | 289 | arr->setLengthInt32(idx + 1); |
michael@0 | 290 | arr->setDenseElementWithType(cx, idx, v); |
michael@0 | 291 | return true; |
michael@0 | 292 | } while (false); |
michael@0 | 293 | |
michael@0 | 294 | if (result == JSObject::ED_FAILED) |
michael@0 | 295 | return false; |
michael@0 | 296 | JS_ASSERT(result == JSObject::ED_SPARSE); |
michael@0 | 297 | } |
michael@0 | 298 | |
michael@0 | 299 | RootedId id(cx); |
michael@0 | 300 | if (!ToId(cx, index, &id)) |
michael@0 | 301 | return false; |
michael@0 | 302 | |
michael@0 | 303 | RootedValue tmp(cx, v); |
michael@0 | 304 | return JSObject::setGeneric(cx, obj, obj, id, &tmp, true); |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | /* |
michael@0 | 308 | * Attempt to delete the element |index| from |obj| as if by |
michael@0 | 309 | * |obj.[[Delete]](index)|. |
michael@0 | 310 | * |
michael@0 | 311 | * If an error occurs while attempting to delete the element (that is, the call |
michael@0 | 312 | * to [[Delete]] threw), return false. |
michael@0 | 313 | * |
michael@0 | 314 | * Otherwise set *succeeded to indicate whether the deletion attempt succeeded |
michael@0 | 315 | * (that is, whether the call to [[Delete]] returned true or false). (Deletes |
michael@0 | 316 | * generally fail only when the property is non-configurable, but proxies may |
michael@0 | 317 | * implement different semantics.) |
michael@0 | 318 | */ |
michael@0 | 319 | static bool |
michael@0 | 320 | DeleteArrayElement(JSContext *cx, HandleObject obj, double index, bool *succeeded) |
michael@0 | 321 | { |
michael@0 | 322 | JS_ASSERT(index >= 0); |
michael@0 | 323 | JS_ASSERT(floor(index) == index); |
michael@0 | 324 | |
michael@0 | 325 | if (obj->is<ArrayObject>() && !obj->isIndexed()) { |
michael@0 | 326 | if (index <= UINT32_MAX) { |
michael@0 | 327 | uint32_t idx = uint32_t(index); |
michael@0 | 328 | if (idx < obj->getDenseInitializedLength()) { |
michael@0 | 329 | obj->markDenseElementsNotPacked(cx); |
michael@0 | 330 | obj->setDenseElement(idx, MagicValue(JS_ELEMENTS_HOLE)); |
michael@0 | 331 | if (!js_SuppressDeletedElement(cx, obj, idx)) |
michael@0 | 332 | return false; |
michael@0 | 333 | } |
michael@0 | 334 | } |
michael@0 | 335 | |
michael@0 | 336 | *succeeded = true; |
michael@0 | 337 | return true; |
michael@0 | 338 | } |
michael@0 | 339 | |
michael@0 | 340 | if (index <= UINT32_MAX) |
michael@0 | 341 | return JSObject::deleteElement(cx, obj, uint32_t(index), succeeded); |
michael@0 | 342 | |
michael@0 | 343 | return JSObject::deleteByValue(cx, obj, DoubleValue(index), succeeded); |
michael@0 | 344 | } |
michael@0 | 345 | |
michael@0 | 346 | /* ES6 20130308 draft 9.3.5 */ |
michael@0 | 347 | static bool |
michael@0 | 348 | DeletePropertyOrThrow(JSContext *cx, HandleObject obj, double index) |
michael@0 | 349 | { |
michael@0 | 350 | bool succeeded; |
michael@0 | 351 | if (!DeleteArrayElement(cx, obj, index, &succeeded)) |
michael@0 | 352 | return false; |
michael@0 | 353 | if (succeeded) |
michael@0 | 354 | return true; |
michael@0 | 355 | |
michael@0 | 356 | RootedId id(cx); |
michael@0 | 357 | RootedValue indexv(cx, NumberValue(index)); |
michael@0 | 358 | if (!ValueToId<CanGC>(cx, indexv, &id)) |
michael@0 | 359 | return false; |
michael@0 | 360 | return obj->reportNotConfigurable(cx, id, JSREPORT_ERROR); |
michael@0 | 361 | } |
michael@0 | 362 | |
michael@0 | 363 | bool |
michael@0 | 364 | js::SetLengthProperty(JSContext *cx, HandleObject obj, double length) |
michael@0 | 365 | { |
michael@0 | 366 | RootedValue v(cx, NumberValue(length)); |
michael@0 | 367 | return JSObject::setProperty(cx, obj, obj, cx->names().length, &v, true); |
michael@0 | 368 | } |
michael@0 | 369 | |
michael@0 | 370 | /* |
michael@0 | 371 | * Since SpiderMonkey supports cross-class prototype-based delegation, we have |
michael@0 | 372 | * to be careful about the length getter and setter being called on an object |
michael@0 | 373 | * not of Array class. For the getter, we search obj's prototype chain for the |
michael@0 | 374 | * array that caused this getter to be invoked. In the setter case to overcome |
michael@0 | 375 | * the JSPROP_SHARED attribute, we must define a shadowing length property. |
michael@0 | 376 | */ |
michael@0 | 377 | static bool |
michael@0 | 378 | array_length_getter(JSContext *cx, HandleObject obj_, HandleId id, MutableHandleValue vp) |
michael@0 | 379 | { |
michael@0 | 380 | RootedObject obj(cx, obj_); |
michael@0 | 381 | do { |
michael@0 | 382 | if (obj->is<ArrayObject>()) { |
michael@0 | 383 | vp.setNumber(obj->as<ArrayObject>().length()); |
michael@0 | 384 | return true; |
michael@0 | 385 | } |
michael@0 | 386 | if (!JSObject::getProto(cx, obj, &obj)) |
michael@0 | 387 | return false; |
michael@0 | 388 | } while (obj); |
michael@0 | 389 | return true; |
michael@0 | 390 | } |
michael@0 | 391 | |
michael@0 | 392 | static bool |
michael@0 | 393 | array_length_setter(JSContext *cx, HandleObject obj, HandleId id, bool strict, MutableHandleValue vp) |
michael@0 | 394 | { |
michael@0 | 395 | if (!obj->is<ArrayObject>()) { |
michael@0 | 396 | return JSObject::defineProperty(cx, obj, cx->names().length, vp, |
michael@0 | 397 | nullptr, nullptr, JSPROP_ENUMERATE); |
michael@0 | 398 | } |
michael@0 | 399 | |
michael@0 | 400 | Rooted<ArrayObject*> arr(cx, &obj->as<ArrayObject>()); |
michael@0 | 401 | MOZ_ASSERT(arr->lengthIsWritable(), |
michael@0 | 402 | "setter shouldn't be called if property is non-writable"); |
michael@0 | 403 | return ArraySetLength<SequentialExecution>(cx, arr, id, JSPROP_PERMANENT, vp, strict); |
michael@0 | 404 | } |
michael@0 | 405 | |
michael@0 | 406 | struct ReverseIndexComparator |
michael@0 | 407 | { |
michael@0 | 408 | bool operator()(const uint32_t& a, const uint32_t& b, bool *lessOrEqualp) { |
michael@0 | 409 | MOZ_ASSERT(a != b, "how'd we get duplicate indexes?"); |
michael@0 | 410 | *lessOrEqualp = b <= a; |
michael@0 | 411 | return true; |
michael@0 | 412 | } |
michael@0 | 413 | }; |
michael@0 | 414 | |
michael@0 | 415 | template <ExecutionMode mode> |
michael@0 | 416 | bool |
michael@0 | 417 | js::CanonicalizeArrayLengthValue(typename ExecutionModeTraits<mode>::ContextType cx, |
michael@0 | 418 | HandleValue v, uint32_t *newLen) |
michael@0 | 419 | { |
michael@0 | 420 | double d; |
michael@0 | 421 | |
michael@0 | 422 | if (mode == ParallelExecution) { |
michael@0 | 423 | if (v.isObject()) |
michael@0 | 424 | return false; |
michael@0 | 425 | |
michael@0 | 426 | if (!NonObjectToUint32(cx, v, newLen)) |
michael@0 | 427 | return false; |
michael@0 | 428 | |
michael@0 | 429 | if (!NonObjectToNumber(cx, v, &d)) |
michael@0 | 430 | return false; |
michael@0 | 431 | } else { |
michael@0 | 432 | if (!ToUint32(cx->asJSContext(), v, newLen)) |
michael@0 | 433 | return false; |
michael@0 | 434 | |
michael@0 | 435 | if (!ToNumber(cx->asJSContext(), v, &d)) |
michael@0 | 436 | return false; |
michael@0 | 437 | } |
michael@0 | 438 | |
michael@0 | 439 | if (d == *newLen) |
michael@0 | 440 | return true; |
michael@0 | 441 | |
michael@0 | 442 | if (cx->isJSContext()) |
michael@0 | 443 | JS_ReportErrorNumber(cx->asJSContext(), js_GetErrorMessage, nullptr, |
michael@0 | 444 | JSMSG_BAD_ARRAY_LENGTH); |
michael@0 | 445 | return false; |
michael@0 | 446 | } |
michael@0 | 447 | |
michael@0 | 448 | template bool |
michael@0 | 449 | js::CanonicalizeArrayLengthValue<SequentialExecution>(JSContext *cx, |
michael@0 | 450 | HandleValue v, uint32_t *newLen); |
michael@0 | 451 | template bool |
michael@0 | 452 | js::CanonicalizeArrayLengthValue<ParallelExecution>(ForkJoinContext *cx, |
michael@0 | 453 | HandleValue v, uint32_t *newLen); |
michael@0 | 454 | |
michael@0 | 455 | /* ES6 20130308 draft 8.4.2.4 ArraySetLength */ |
michael@0 | 456 | template <ExecutionMode mode> |
michael@0 | 457 | bool |
michael@0 | 458 | js::ArraySetLength(typename ExecutionModeTraits<mode>::ContextType cxArg, |
michael@0 | 459 | Handle<ArrayObject*> arr, HandleId id, |
michael@0 | 460 | unsigned attrs, HandleValue value, bool setterIsStrict) |
michael@0 | 461 | { |
michael@0 | 462 | MOZ_ASSERT(cxArg->isThreadLocal(arr)); |
michael@0 | 463 | MOZ_ASSERT(id == NameToId(cxArg->names().length)); |
michael@0 | 464 | |
michael@0 | 465 | /* Steps 1-2 are irrelevant in our implementation. */ |
michael@0 | 466 | |
michael@0 | 467 | /* Steps 3-5. */ |
michael@0 | 468 | uint32_t newLen; |
michael@0 | 469 | if (!CanonicalizeArrayLengthValue<mode>(cxArg, value, &newLen)) |
michael@0 | 470 | return false; |
michael@0 | 471 | |
michael@0 | 472 | // Abort if we're being asked to change enumerability or configurability. |
michael@0 | 473 | // (The length property of arrays is non-configurable, so such attempts |
michael@0 | 474 | // must fail.) This behavior is spread throughout the ArraySetLength spec |
michael@0 | 475 | // algorithm, but we only need check it once as our array implementation |
michael@0 | 476 | // is internally so different from the spec algorithm. (ES5 and ES6 define |
michael@0 | 477 | // behavior by delegating to the default define-own-property algorithm -- |
michael@0 | 478 | // OrdinaryDefineOwnProperty in ES6, the default [[DefineOwnProperty]] in |
michael@0 | 479 | // ES5 -- but we reimplement all the conflict-detection bits ourselves here |
michael@0 | 480 | // so that we can use a customized length representation.) |
michael@0 | 481 | if (!(attrs & JSPROP_PERMANENT) || (attrs & JSPROP_ENUMERATE)) { |
michael@0 | 482 | if (!setterIsStrict) |
michael@0 | 483 | return true; |
michael@0 | 484 | // Bail for strict mode in parallel execution, as we need to go back |
michael@0 | 485 | // to sequential mode to throw the error. |
michael@0 | 486 | if (mode == ParallelExecution) |
michael@0 | 487 | return false; |
michael@0 | 488 | return Throw(cxArg->asJSContext(), id, JSMSG_CANT_REDEFINE_PROP); |
michael@0 | 489 | } |
michael@0 | 490 | |
michael@0 | 491 | /* Steps 6-7. */ |
michael@0 | 492 | bool lengthIsWritable = arr->lengthIsWritable(); |
michael@0 | 493 | #ifdef DEBUG |
michael@0 | 494 | { |
michael@0 | 495 | RootedShape lengthShape(cxArg, arr->nativeLookupPure(id)); |
michael@0 | 496 | MOZ_ASSERT(lengthShape); |
michael@0 | 497 | MOZ_ASSERT(lengthShape->writable() == lengthIsWritable); |
michael@0 | 498 | } |
michael@0 | 499 | #endif |
michael@0 | 500 | |
michael@0 | 501 | uint32_t oldLen = arr->length(); |
michael@0 | 502 | |
michael@0 | 503 | /* Steps 8-9 for arrays with non-writable length. */ |
michael@0 | 504 | if (!lengthIsWritable) { |
michael@0 | 505 | if (newLen == oldLen) |
michael@0 | 506 | return true; |
michael@0 | 507 | |
michael@0 | 508 | if (!cxArg->isJSContext()) |
michael@0 | 509 | return false; |
michael@0 | 510 | |
michael@0 | 511 | if (setterIsStrict) { |
michael@0 | 512 | return JS_ReportErrorFlagsAndNumber(cxArg->asJSContext(), |
michael@0 | 513 | JSREPORT_ERROR, js_GetErrorMessage, nullptr, |
michael@0 | 514 | JSMSG_CANT_REDEFINE_ARRAY_LENGTH); |
michael@0 | 515 | } |
michael@0 | 516 | |
michael@0 | 517 | return JSObject::reportReadOnly(cxArg->asJSContext(), id, |
michael@0 | 518 | JSREPORT_STRICT | JSREPORT_WARNING); |
michael@0 | 519 | } |
michael@0 | 520 | |
michael@0 | 521 | /* Step 8. */ |
michael@0 | 522 | bool succeeded = true; |
michael@0 | 523 | do { |
michael@0 | 524 | // The initialized length and capacity of an array only need updating |
michael@0 | 525 | // when non-hole elements are added or removed, which doesn't happen |
michael@0 | 526 | // when array length stays the same or increases. |
michael@0 | 527 | if (newLen >= oldLen) |
michael@0 | 528 | break; |
michael@0 | 529 | |
michael@0 | 530 | // Attempt to propagate dense-element optimization tricks, if possible, |
michael@0 | 531 | // and avoid the generic (and accordingly slow) deletion code below. |
michael@0 | 532 | // We can only do this if there are only densely-indexed elements. |
michael@0 | 533 | // Once there's a sparse indexed element, there's no good way to know, |
michael@0 | 534 | // save by enumerating all the properties to find it. But we *have* to |
michael@0 | 535 | // know in case that sparse indexed element is non-configurable, as |
michael@0 | 536 | // that element must prevent any deletions below it. Bug 586842 should |
michael@0 | 537 | // fix this inefficiency by moving indexed storage to be entirely |
michael@0 | 538 | // separate from non-indexed storage. |
michael@0 | 539 | if (!arr->isIndexed()) { |
michael@0 | 540 | uint32_t oldCapacity = arr->getDenseCapacity(); |
michael@0 | 541 | uint32_t oldInitializedLength = arr->getDenseInitializedLength(); |
michael@0 | 542 | MOZ_ASSERT(oldCapacity >= oldInitializedLength); |
michael@0 | 543 | if (oldInitializedLength > newLen) |
michael@0 | 544 | arr->setDenseInitializedLength(newLen); |
michael@0 | 545 | if (oldCapacity > newLen) |
michael@0 | 546 | arr->shrinkElements(cxArg, newLen); |
michael@0 | 547 | |
michael@0 | 548 | // We've done the work of deleting any dense elements needing |
michael@0 | 549 | // deletion, and there are no sparse elements. Thus we can skip |
michael@0 | 550 | // straight to defining the length. |
michael@0 | 551 | break; |
michael@0 | 552 | } |
michael@0 | 553 | |
michael@0 | 554 | // Bail from parallel execution if need to perform step 15, which is |
michael@0 | 555 | // unsafe and isn't a common case. |
michael@0 | 556 | if (mode == ParallelExecution) |
michael@0 | 557 | return false; |
michael@0 | 558 | |
michael@0 | 559 | JSContext *cx = cxArg->asJSContext(); |
michael@0 | 560 | |
michael@0 | 561 | // Step 15. |
michael@0 | 562 | // |
michael@0 | 563 | // Attempt to delete all elements above the new length, from greatest |
michael@0 | 564 | // to least. If any of these deletions fails, we're supposed to define |
michael@0 | 565 | // the length to one greater than the index that couldn't be deleted, |
michael@0 | 566 | // *with the property attributes specified*. This might convert the |
michael@0 | 567 | // length to be not the value specified, yet non-writable. (You may be |
michael@0 | 568 | // forgiven for thinking these are interesting semantics.) Example: |
michael@0 | 569 | // |
michael@0 | 570 | // var arr = |
michael@0 | 571 | // Object.defineProperty([0, 1, 2, 3], 1, { writable: false }); |
michael@0 | 572 | // Object.defineProperty(arr, "length", |
michael@0 | 573 | // { value: 0, writable: false }); |
michael@0 | 574 | // |
michael@0 | 575 | // will convert |arr| to an array of non-writable length two, then |
michael@0 | 576 | // throw a TypeError. |
michael@0 | 577 | // |
michael@0 | 578 | // We implement this behavior, in the relevant lops below, by setting |
michael@0 | 579 | // |succeeded| to false. Then we exit the loop, define the length |
michael@0 | 580 | // appropriately, and only then throw a TypeError, if necessary. |
michael@0 | 581 | uint32_t gap = oldLen - newLen; |
michael@0 | 582 | const uint32_t RemoveElementsFastLimit = 1 << 24; |
michael@0 | 583 | if (gap < RemoveElementsFastLimit) { |
michael@0 | 584 | // If we're removing a relatively small number of elements, just do |
michael@0 | 585 | // it exactly by the spec. |
michael@0 | 586 | while (newLen < oldLen) { |
michael@0 | 587 | /* Step 15a. */ |
michael@0 | 588 | oldLen--; |
michael@0 | 589 | |
michael@0 | 590 | /* Steps 15b-d. */ |
michael@0 | 591 | bool deleteSucceeded; |
michael@0 | 592 | if (!JSObject::deleteElement(cx, arr, oldLen, &deleteSucceeded)) |
michael@0 | 593 | return false; |
michael@0 | 594 | if (!deleteSucceeded) { |
michael@0 | 595 | newLen = oldLen + 1; |
michael@0 | 596 | succeeded = false; |
michael@0 | 597 | break; |
michael@0 | 598 | } |
michael@0 | 599 | } |
michael@0 | 600 | } else { |
michael@0 | 601 | // If we're removing a large number of elements from an array |
michael@0 | 602 | // that's probably sparse, try a different tack. Get all the own |
michael@0 | 603 | // property names, sift out the indexes in the deletion range into |
michael@0 | 604 | // a vector, sort the vector greatest to least, then delete the |
michael@0 | 605 | // indexes greatest to least using that vector. See bug 322135. |
michael@0 | 606 | // |
michael@0 | 607 | // This heuristic's kind of a huge guess -- "large number of |
michael@0 | 608 | // elements" and "probably sparse" are completely unprincipled |
michael@0 | 609 | // predictions. In the long run, bug 586842 will support the right |
michael@0 | 610 | // fix: store sparse elements in a sorted data structure that |
michael@0 | 611 | // permits fast in-reverse-order traversal and concurrent removals. |
michael@0 | 612 | |
michael@0 | 613 | Vector<uint32_t> indexes(cx); |
michael@0 | 614 | { |
michael@0 | 615 | AutoIdVector props(cx); |
michael@0 | 616 | if (!GetPropertyNames(cx, arr, JSITER_OWNONLY | JSITER_HIDDEN, &props)) |
michael@0 | 617 | return false; |
michael@0 | 618 | |
michael@0 | 619 | for (size_t i = 0; i < props.length(); i++) { |
michael@0 | 620 | if (!CheckForInterrupt(cx)) |
michael@0 | 621 | return false; |
michael@0 | 622 | |
michael@0 | 623 | uint32_t index; |
michael@0 | 624 | if (!js_IdIsIndex(props[i], &index)) |
michael@0 | 625 | continue; |
michael@0 | 626 | |
michael@0 | 627 | if (index >= newLen && index < oldLen) { |
michael@0 | 628 | if (!indexes.append(index)) |
michael@0 | 629 | return false; |
michael@0 | 630 | } |
michael@0 | 631 | } |
michael@0 | 632 | } |
michael@0 | 633 | |
michael@0 | 634 | uint32_t count = indexes.length(); |
michael@0 | 635 | { |
michael@0 | 636 | // We should use radix sort to be O(n), but this is uncommon |
michael@0 | 637 | // enough that we'll punt til someone complains. |
michael@0 | 638 | Vector<uint32_t> scratch(cx); |
michael@0 | 639 | if (!scratch.resize(count)) |
michael@0 | 640 | return false; |
michael@0 | 641 | MOZ_ALWAYS_TRUE(MergeSort(indexes.begin(), count, scratch.begin(), |
michael@0 | 642 | ReverseIndexComparator())); |
michael@0 | 643 | } |
michael@0 | 644 | |
michael@0 | 645 | uint32_t index = UINT32_MAX; |
michael@0 | 646 | for (uint32_t i = 0; i < count; i++) { |
michael@0 | 647 | MOZ_ASSERT(indexes[i] < index, "indexes should never repeat"); |
michael@0 | 648 | index = indexes[i]; |
michael@0 | 649 | |
michael@0 | 650 | /* Steps 15b-d. */ |
michael@0 | 651 | bool deleteSucceeded; |
michael@0 | 652 | if (!JSObject::deleteElement(cx, arr, index, &deleteSucceeded)) |
michael@0 | 653 | return false; |
michael@0 | 654 | if (!deleteSucceeded) { |
michael@0 | 655 | newLen = index + 1; |
michael@0 | 656 | succeeded = false; |
michael@0 | 657 | break; |
michael@0 | 658 | } |
michael@0 | 659 | } |
michael@0 | 660 | } |
michael@0 | 661 | } while (false); |
michael@0 | 662 | |
michael@0 | 663 | /* Steps 12, 16. */ |
michael@0 | 664 | |
michael@0 | 665 | // Yes, we totally drop a non-stub getter/setter from a defineProperty |
michael@0 | 666 | // API call on the floor here. Given that getter/setter will go away in |
michael@0 | 667 | // the long run, with accessors replacing them both internally and at the |
michael@0 | 668 | // API level, just run with this. |
michael@0 | 669 | RootedShape lengthShape(cxArg, mode == ParallelExecution |
michael@0 | 670 | ? arr->nativeLookupPure(id) |
michael@0 | 671 | : arr->nativeLookup(cxArg->asJSContext(), id)); |
michael@0 | 672 | if (!JSObject::changeProperty<mode>(cxArg, arr, lengthShape, attrs, |
michael@0 | 673 | JSPROP_PERMANENT | JSPROP_READONLY | JSPROP_SHARED, |
michael@0 | 674 | array_length_getter, array_length_setter)) |
michael@0 | 675 | { |
michael@0 | 676 | return false; |
michael@0 | 677 | } |
michael@0 | 678 | |
michael@0 | 679 | if (mode == ParallelExecution) { |
michael@0 | 680 | // Overflowing int32 requires changing TI state. |
michael@0 | 681 | if (newLen > INT32_MAX) |
michael@0 | 682 | return false; |
michael@0 | 683 | arr->setLengthInt32(newLen); |
michael@0 | 684 | } else { |
michael@0 | 685 | JSContext *cx = cxArg->asJSContext(); |
michael@0 | 686 | arr->setLength(cx, newLen); |
michael@0 | 687 | } |
michael@0 | 688 | |
michael@0 | 689 | |
michael@0 | 690 | // All operations past here until the |!succeeded| code must be infallible, |
michael@0 | 691 | // so that all element fields remain properly synchronized. |
michael@0 | 692 | |
michael@0 | 693 | // Trim the initialized length, if needed, to preserve the <= length |
michael@0 | 694 | // invariant. (Capacity was already reduced during element deletion, if |
michael@0 | 695 | // necessary.) |
michael@0 | 696 | ObjectElements *header = arr->getElementsHeader(); |
michael@0 | 697 | header->initializedLength = Min(header->initializedLength, newLen); |
michael@0 | 698 | |
michael@0 | 699 | if (attrs & JSPROP_READONLY) { |
michael@0 | 700 | header->setNonwritableArrayLength(); |
michael@0 | 701 | |
michael@0 | 702 | // When an array's length becomes non-writable, writes to indexes |
michael@0 | 703 | // greater than or equal to the length don't change the array. We |
michael@0 | 704 | // handle this with a check for non-writable length in most places. |
michael@0 | 705 | // But in JIT code every check counts -- so we piggyback the check on |
michael@0 | 706 | // the already-required range check for |index < capacity| by making |
michael@0 | 707 | // capacity of arrays with non-writable length never exceed the length. |
michael@0 | 708 | if (arr->getDenseCapacity() > newLen) { |
michael@0 | 709 | arr->shrinkElements(cxArg, newLen); |
michael@0 | 710 | arr->getElementsHeader()->capacity = newLen; |
michael@0 | 711 | } |
michael@0 | 712 | } |
michael@0 | 713 | |
michael@0 | 714 | if (setterIsStrict && !succeeded) { |
michael@0 | 715 | // We can't have arrived here under ParallelExecution, as we have |
michael@0 | 716 | // returned from the function before step 15 above. |
michael@0 | 717 | JSContext *cx = cxArg->asJSContext(); |
michael@0 | 718 | RootedId elementId(cx); |
michael@0 | 719 | if (!IndexToId(cx, newLen - 1, &elementId)) |
michael@0 | 720 | return false; |
michael@0 | 721 | return arr->reportNotConfigurable(cx, elementId); |
michael@0 | 722 | } |
michael@0 | 723 | |
michael@0 | 724 | return true; |
michael@0 | 725 | } |
michael@0 | 726 | |
michael@0 | 727 | template bool |
michael@0 | 728 | js::ArraySetLength<SequentialExecution>(JSContext *cx, Handle<ArrayObject*> arr, |
michael@0 | 729 | HandleId id, unsigned attrs, HandleValue value, |
michael@0 | 730 | bool setterIsStrict); |
michael@0 | 731 | template bool |
michael@0 | 732 | js::ArraySetLength<ParallelExecution>(ForkJoinContext *cx, Handle<ArrayObject*> arr, |
michael@0 | 733 | HandleId id, unsigned attrs, HandleValue value, |
michael@0 | 734 | bool setterIsStrict); |
michael@0 | 735 | |
michael@0 | 736 | bool |
michael@0 | 737 | js::WouldDefinePastNonwritableLength(ThreadSafeContext *cx, |
michael@0 | 738 | HandleObject obj, uint32_t index, bool strict, |
michael@0 | 739 | bool *definesPast) |
michael@0 | 740 | { |
michael@0 | 741 | if (!obj->is<ArrayObject>()) { |
michael@0 | 742 | *definesPast = false; |
michael@0 | 743 | return true; |
michael@0 | 744 | } |
michael@0 | 745 | |
michael@0 | 746 | Rooted<ArrayObject*> arr(cx, &obj->as<ArrayObject>()); |
michael@0 | 747 | uint32_t length = arr->length(); |
michael@0 | 748 | if (index < length) { |
michael@0 | 749 | *definesPast = false; |
michael@0 | 750 | return true; |
michael@0 | 751 | } |
michael@0 | 752 | |
michael@0 | 753 | if (arr->lengthIsWritable()) { |
michael@0 | 754 | *definesPast = false; |
michael@0 | 755 | return true; |
michael@0 | 756 | } |
michael@0 | 757 | |
michael@0 | 758 | *definesPast = true; |
michael@0 | 759 | |
michael@0 | 760 | // Error in strict mode code or warn with strict option. |
michael@0 | 761 | unsigned flags = strict ? JSREPORT_ERROR : (JSREPORT_STRICT | JSREPORT_WARNING); |
michael@0 | 762 | if (cx->isForkJoinContext()) |
michael@0 | 763 | return cx->asForkJoinContext()->reportError(ParallelBailoutUnsupportedVM, flags); |
michael@0 | 764 | |
michael@0 | 765 | if (!cx->isJSContext()) |
michael@0 | 766 | return true; |
michael@0 | 767 | |
michael@0 | 768 | JSContext *ncx = cx->asJSContext(); |
michael@0 | 769 | |
michael@0 | 770 | if (!strict && !ncx->options().extraWarnings()) |
michael@0 | 771 | return true; |
michael@0 | 772 | |
michael@0 | 773 | // XXX include the index and maybe array length in the error message |
michael@0 | 774 | return JS_ReportErrorFlagsAndNumber(ncx, flags, js_GetErrorMessage, nullptr, |
michael@0 | 775 | JSMSG_CANT_DEFINE_PAST_ARRAY_LENGTH); |
michael@0 | 776 | } |
michael@0 | 777 | |
michael@0 | 778 | static bool |
michael@0 | 779 | array_addProperty(JSContext *cx, HandleObject obj, HandleId id, MutableHandleValue vp) |
michael@0 | 780 | { |
michael@0 | 781 | Rooted<ArrayObject*> arr(cx, &obj->as<ArrayObject>()); |
michael@0 | 782 | |
michael@0 | 783 | uint32_t index; |
michael@0 | 784 | if (!js_IdIsIndex(id, &index)) |
michael@0 | 785 | return true; |
michael@0 | 786 | |
michael@0 | 787 | uint32_t length = arr->length(); |
michael@0 | 788 | if (index >= length) { |
michael@0 | 789 | MOZ_ASSERT(arr->lengthIsWritable(), |
michael@0 | 790 | "how'd this element get added if length is non-writable?"); |
michael@0 | 791 | arr->setLength(cx, index + 1); |
michael@0 | 792 | } |
michael@0 | 793 | return true; |
michael@0 | 794 | } |
michael@0 | 795 | |
michael@0 | 796 | bool |
michael@0 | 797 | js::ObjectMayHaveExtraIndexedProperties(JSObject *obj) |
michael@0 | 798 | { |
michael@0 | 799 | /* |
michael@0 | 800 | * Whether obj may have indexed properties anywhere besides its dense |
michael@0 | 801 | * elements. This includes other indexed properties in its shape hierarchy, |
michael@0 | 802 | * and indexed properties or elements along its prototype chain. |
michael@0 | 803 | */ |
michael@0 | 804 | |
michael@0 | 805 | JS_ASSERT(obj->isNative()); |
michael@0 | 806 | |
michael@0 | 807 | if (obj->isIndexed()) |
michael@0 | 808 | return true; |
michael@0 | 809 | |
michael@0 | 810 | /* |
michael@0 | 811 | * Walk up the prototype chain and see if this indexed element already |
michael@0 | 812 | * exists. If we hit the end of the prototype chain, it's safe to set the |
michael@0 | 813 | * element on the original object. |
michael@0 | 814 | */ |
michael@0 | 815 | while ((obj = obj->getProto()) != nullptr) { |
michael@0 | 816 | /* |
michael@0 | 817 | * If the prototype is a non-native object (possibly a dense array), or |
michael@0 | 818 | * a native object (possibly a slow array) that has indexed properties, |
michael@0 | 819 | * return true. |
michael@0 | 820 | */ |
michael@0 | 821 | if (!obj->isNative()) |
michael@0 | 822 | return true; |
michael@0 | 823 | if (obj->isIndexed()) |
michael@0 | 824 | return true; |
michael@0 | 825 | if (obj->getDenseInitializedLength() > 0) |
michael@0 | 826 | return true; |
michael@0 | 827 | if (obj->is<TypedArrayObject>()) |
michael@0 | 828 | return true; |
michael@0 | 829 | } |
michael@0 | 830 | |
michael@0 | 831 | return false; |
michael@0 | 832 | } |
michael@0 | 833 | |
michael@0 | 834 | const Class ArrayObject::class_ = { |
michael@0 | 835 | "Array", |
michael@0 | 836 | JSCLASS_HAS_CACHED_PROTO(JSProto_Array), |
michael@0 | 837 | array_addProperty, |
michael@0 | 838 | JS_DeletePropertyStub, /* delProperty */ |
michael@0 | 839 | JS_PropertyStub, /* getProperty */ |
michael@0 | 840 | JS_StrictPropertyStub, /* setProperty */ |
michael@0 | 841 | JS_EnumerateStub, |
michael@0 | 842 | JS_ResolveStub, |
michael@0 | 843 | JS_ConvertStub, |
michael@0 | 844 | nullptr, |
michael@0 | 845 | nullptr, /* call */ |
michael@0 | 846 | nullptr, /* hasInstance */ |
michael@0 | 847 | nullptr, /* construct */ |
michael@0 | 848 | nullptr /* trace */ |
michael@0 | 849 | }; |
michael@0 | 850 | |
michael@0 | 851 | static bool |
michael@0 | 852 | AddLengthProperty(ExclusiveContext *cx, HandleObject obj) |
michael@0 | 853 | { |
michael@0 | 854 | /* |
michael@0 | 855 | * Add the 'length' property for a newly created array, |
michael@0 | 856 | * and update the elements to be an empty array owned by the object. |
michael@0 | 857 | * The shared emptyObjectElements singleton cannot be used for slow arrays, |
michael@0 | 858 | * as accesses to 'length' will use the elements header. |
michael@0 | 859 | */ |
michael@0 | 860 | |
michael@0 | 861 | RootedId lengthId(cx, NameToId(cx->names().length)); |
michael@0 | 862 | JS_ASSERT(!obj->nativeLookup(cx, lengthId)); |
michael@0 | 863 | |
michael@0 | 864 | return JSObject::addProperty(cx, obj, lengthId, array_length_getter, array_length_setter, |
michael@0 | 865 | SHAPE_INVALID_SLOT, JSPROP_PERMANENT | JSPROP_SHARED, 0, |
michael@0 | 866 | /* allowDictionary = */ false); |
michael@0 | 867 | } |
michael@0 | 868 | |
michael@0 | 869 | #if JS_HAS_TOSOURCE |
michael@0 | 870 | MOZ_ALWAYS_INLINE bool |
michael@0 | 871 | IsArray(HandleValue v) |
michael@0 | 872 | { |
michael@0 | 873 | return v.isObject() && v.toObject().is<ArrayObject>(); |
michael@0 | 874 | } |
michael@0 | 875 | |
michael@0 | 876 | MOZ_ALWAYS_INLINE bool |
michael@0 | 877 | array_toSource_impl(JSContext *cx, CallArgs args) |
michael@0 | 878 | { |
michael@0 | 879 | JS_ASSERT(IsArray(args.thisv())); |
michael@0 | 880 | |
michael@0 | 881 | Rooted<JSObject*> obj(cx, &args.thisv().toObject()); |
michael@0 | 882 | RootedValue elt(cx); |
michael@0 | 883 | |
michael@0 | 884 | AutoCycleDetector detector(cx, obj); |
michael@0 | 885 | if (!detector.init()) |
michael@0 | 886 | return false; |
michael@0 | 887 | |
michael@0 | 888 | StringBuffer sb(cx); |
michael@0 | 889 | |
michael@0 | 890 | if (detector.foundCycle()) { |
michael@0 | 891 | if (!sb.append("[]")) |
michael@0 | 892 | return false; |
michael@0 | 893 | goto make_string; |
michael@0 | 894 | } |
michael@0 | 895 | |
michael@0 | 896 | if (!sb.append('[')) |
michael@0 | 897 | return false; |
michael@0 | 898 | |
michael@0 | 899 | uint32_t length; |
michael@0 | 900 | if (!GetLengthProperty(cx, obj, &length)) |
michael@0 | 901 | return false; |
michael@0 | 902 | |
michael@0 | 903 | for (uint32_t index = 0; index < length; index++) { |
michael@0 | 904 | bool hole; |
michael@0 | 905 | if (!CheckForInterrupt(cx) || |
michael@0 | 906 | !GetElement(cx, obj, index, &hole, &elt)) { |
michael@0 | 907 | return false; |
michael@0 | 908 | } |
michael@0 | 909 | |
michael@0 | 910 | /* Get element's character string. */ |
michael@0 | 911 | JSString *str; |
michael@0 | 912 | if (hole) { |
michael@0 | 913 | str = cx->runtime()->emptyString; |
michael@0 | 914 | } else { |
michael@0 | 915 | str = ValueToSource(cx, elt); |
michael@0 | 916 | if (!str) |
michael@0 | 917 | return false; |
michael@0 | 918 | } |
michael@0 | 919 | |
michael@0 | 920 | /* Append element to buffer. */ |
michael@0 | 921 | if (!sb.append(str)) |
michael@0 | 922 | return false; |
michael@0 | 923 | if (index + 1 != length) { |
michael@0 | 924 | if (!sb.append(", ")) |
michael@0 | 925 | return false; |
michael@0 | 926 | } else if (hole) { |
michael@0 | 927 | if (!sb.append(',')) |
michael@0 | 928 | return false; |
michael@0 | 929 | } |
michael@0 | 930 | } |
michael@0 | 931 | |
michael@0 | 932 | /* Finalize the buffer. */ |
michael@0 | 933 | if (!sb.append(']')) |
michael@0 | 934 | return false; |
michael@0 | 935 | |
michael@0 | 936 | make_string: |
michael@0 | 937 | JSString *str = sb.finishString(); |
michael@0 | 938 | if (!str) |
michael@0 | 939 | return false; |
michael@0 | 940 | |
michael@0 | 941 | args.rval().setString(str); |
michael@0 | 942 | return true; |
michael@0 | 943 | } |
michael@0 | 944 | |
michael@0 | 945 | static bool |
michael@0 | 946 | array_toSource(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 947 | { |
michael@0 | 948 | JS_CHECK_RECURSION(cx, return false); |
michael@0 | 949 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 950 | return CallNonGenericMethod<IsArray, array_toSource_impl>(cx, args); |
michael@0 | 951 | } |
michael@0 | 952 | #endif |
michael@0 | 953 | |
michael@0 | 954 | struct EmptySeparatorOp |
michael@0 | 955 | { |
michael@0 | 956 | bool operator()(JSContext *, StringBuffer &sb) { return true; } |
michael@0 | 957 | }; |
michael@0 | 958 | |
michael@0 | 959 | struct CharSeparatorOp |
michael@0 | 960 | { |
michael@0 | 961 | jschar sep; |
michael@0 | 962 | CharSeparatorOp(jschar sep) : sep(sep) {}; |
michael@0 | 963 | bool operator()(JSContext *, StringBuffer &sb) { return sb.append(sep); } |
michael@0 | 964 | }; |
michael@0 | 965 | |
michael@0 | 966 | struct StringSeparatorOp |
michael@0 | 967 | { |
michael@0 | 968 | const jschar *sepchars; |
michael@0 | 969 | size_t seplen; |
michael@0 | 970 | |
michael@0 | 971 | StringSeparatorOp(const jschar *sepchars, size_t seplen) |
michael@0 | 972 | : sepchars(sepchars), seplen(seplen) {}; |
michael@0 | 973 | |
michael@0 | 974 | bool operator()(JSContext *cx, StringBuffer &sb) { |
michael@0 | 975 | return sb.append(sepchars, seplen); |
michael@0 | 976 | } |
michael@0 | 977 | }; |
michael@0 | 978 | |
michael@0 | 979 | template <bool Locale, typename SeparatorOp> |
michael@0 | 980 | static bool |
michael@0 | 981 | ArrayJoinKernel(JSContext *cx, SeparatorOp sepOp, HandleObject obj, uint32_t length, |
michael@0 | 982 | StringBuffer &sb) |
michael@0 | 983 | { |
michael@0 | 984 | uint32_t i = 0; |
michael@0 | 985 | |
michael@0 | 986 | if (!Locale && obj->is<ArrayObject>() && !ObjectMayHaveExtraIndexedProperties(obj)) { |
michael@0 | 987 | // This loop handles all elements up to initializedLength. If |
michael@0 | 988 | // length > initLength we rely on the second loop to add the |
michael@0 | 989 | // other elements. |
michael@0 | 990 | uint32_t initLength = obj->getDenseInitializedLength(); |
michael@0 | 991 | while (i < initLength) { |
michael@0 | 992 | if (!CheckForInterrupt(cx)) |
michael@0 | 993 | return false; |
michael@0 | 994 | |
michael@0 | 995 | const Value &elem = obj->getDenseElement(i); |
michael@0 | 996 | |
michael@0 | 997 | if (elem.isString()) { |
michael@0 | 998 | if (!sb.append(elem.toString())) |
michael@0 | 999 | return false; |
michael@0 | 1000 | } else if (elem.isNumber()) { |
michael@0 | 1001 | if (!NumberValueToStringBuffer(cx, elem, sb)) |
michael@0 | 1002 | return false; |
michael@0 | 1003 | } else if (elem.isBoolean()) { |
michael@0 | 1004 | if (!BooleanToStringBuffer(elem.toBoolean(), sb)) |
michael@0 | 1005 | return false; |
michael@0 | 1006 | } else if (elem.isObject()) { |
michael@0 | 1007 | /* |
michael@0 | 1008 | * Object stringifying could modify the initialized length or make |
michael@0 | 1009 | * the array sparse. Delegate it to a separate loop to keep this |
michael@0 | 1010 | * one tight. |
michael@0 | 1011 | */ |
michael@0 | 1012 | break; |
michael@0 | 1013 | } else { |
michael@0 | 1014 | JS_ASSERT(elem.isMagic(JS_ELEMENTS_HOLE) || elem.isNullOrUndefined()); |
michael@0 | 1015 | } |
michael@0 | 1016 | |
michael@0 | 1017 | if (++i != length && !sepOp(cx, sb)) |
michael@0 | 1018 | return false; |
michael@0 | 1019 | } |
michael@0 | 1020 | } |
michael@0 | 1021 | |
michael@0 | 1022 | if (i != length) { |
michael@0 | 1023 | RootedValue v(cx); |
michael@0 | 1024 | while (i < length) { |
michael@0 | 1025 | if (!CheckForInterrupt(cx)) |
michael@0 | 1026 | return false; |
michael@0 | 1027 | |
michael@0 | 1028 | bool hole; |
michael@0 | 1029 | if (!GetElement(cx, obj, i, &hole, &v)) |
michael@0 | 1030 | return false; |
michael@0 | 1031 | if (!hole && !v.isNullOrUndefined()) { |
michael@0 | 1032 | if (Locale) { |
michael@0 | 1033 | JSObject *robj = ToObject(cx, v); |
michael@0 | 1034 | if (!robj) |
michael@0 | 1035 | return false; |
michael@0 | 1036 | RootedId id(cx, NameToId(cx->names().toLocaleString)); |
michael@0 | 1037 | if (!robj->callMethod(cx, id, 0, nullptr, &v)) |
michael@0 | 1038 | return false; |
michael@0 | 1039 | } |
michael@0 | 1040 | if (!ValueToStringBuffer(cx, v, sb)) |
michael@0 | 1041 | return false; |
michael@0 | 1042 | } |
michael@0 | 1043 | |
michael@0 | 1044 | if (++i != length && !sepOp(cx, sb)) |
michael@0 | 1045 | return false; |
michael@0 | 1046 | } |
michael@0 | 1047 | } |
michael@0 | 1048 | |
michael@0 | 1049 | return true; |
michael@0 | 1050 | } |
michael@0 | 1051 | |
michael@0 | 1052 | template <bool Locale> |
michael@0 | 1053 | static bool |
michael@0 | 1054 | ArrayJoin(JSContext *cx, CallArgs &args) |
michael@0 | 1055 | { |
michael@0 | 1056 | // This method is shared by Array.prototype.join and |
michael@0 | 1057 | // Array.prototype.toLocaleString. The steps in ES5 are nearly the same, so |
michael@0 | 1058 | // the annotations in this function apply to both toLocaleString and join. |
michael@0 | 1059 | |
michael@0 | 1060 | // Step 1 |
michael@0 | 1061 | RootedObject obj(cx, ToObject(cx, args.thisv())); |
michael@0 | 1062 | if (!obj) |
michael@0 | 1063 | return false; |
michael@0 | 1064 | |
michael@0 | 1065 | AutoCycleDetector detector(cx, obj); |
michael@0 | 1066 | if (!detector.init()) |
michael@0 | 1067 | return false; |
michael@0 | 1068 | |
michael@0 | 1069 | if (detector.foundCycle()) { |
michael@0 | 1070 | args.rval().setString(cx->names().empty); |
michael@0 | 1071 | return true; |
michael@0 | 1072 | } |
michael@0 | 1073 | |
michael@0 | 1074 | // Steps 2 and 3 |
michael@0 | 1075 | uint32_t length; |
michael@0 | 1076 | if (!GetLengthProperty(cx, obj, &length)) |
michael@0 | 1077 | return false; |
michael@0 | 1078 | |
michael@0 | 1079 | // Steps 4 and 5 |
michael@0 | 1080 | RootedString sepstr(cx, nullptr); |
michael@0 | 1081 | const jschar *sepchars; |
michael@0 | 1082 | size_t seplen; |
michael@0 | 1083 | if (!Locale && args.hasDefined(0)) { |
michael@0 | 1084 | sepstr = ToString<CanGC>(cx, args[0]); |
michael@0 | 1085 | if (!sepstr) |
michael@0 | 1086 | return false; |
michael@0 | 1087 | sepchars = sepstr->getChars(cx); |
michael@0 | 1088 | if (!sepchars) |
michael@0 | 1089 | return false; |
michael@0 | 1090 | seplen = sepstr->length(); |
michael@0 | 1091 | } else { |
michael@0 | 1092 | HandlePropertyName comma = cx->names().comma; |
michael@0 | 1093 | sepstr = comma; |
michael@0 | 1094 | sepchars = comma->chars(); |
michael@0 | 1095 | seplen = comma->length(); |
michael@0 | 1096 | } |
michael@0 | 1097 | |
michael@0 | 1098 | JS::Anchor<JSString*> anchor(sepstr); |
michael@0 | 1099 | |
michael@0 | 1100 | // Step 6 is implicit in the loops below. |
michael@0 | 1101 | |
michael@0 | 1102 | // An optimized version of a special case of steps 7-11: when length==1 and |
michael@0 | 1103 | // the 0th element is a string, ToString() of that element is a no-op and |
michael@0 | 1104 | // so it can be immediately returned as the result. |
michael@0 | 1105 | if (length == 1 && !Locale && obj->is<ArrayObject>() && |
michael@0 | 1106 | obj->getDenseInitializedLength() == 1) |
michael@0 | 1107 | { |
michael@0 | 1108 | const Value &elem0 = obj->getDenseElement(0); |
michael@0 | 1109 | if (elem0.isString()) { |
michael@0 | 1110 | args.rval().setString(elem0.toString()); |
michael@0 | 1111 | return true; |
michael@0 | 1112 | } |
michael@0 | 1113 | } |
michael@0 | 1114 | |
michael@0 | 1115 | StringBuffer sb(cx); |
michael@0 | 1116 | |
michael@0 | 1117 | // The separator will be added |length - 1| times, reserve space for that |
michael@0 | 1118 | // so that we don't have to unnecessarily grow the buffer. |
michael@0 | 1119 | if (length > 0 && !sb.reserve(seplen * (length - 1))) |
michael@0 | 1120 | return false; |
michael@0 | 1121 | |
michael@0 | 1122 | // Various optimized versions of steps 7-10. |
michael@0 | 1123 | if (seplen == 0) { |
michael@0 | 1124 | EmptySeparatorOp op; |
michael@0 | 1125 | if (!ArrayJoinKernel<Locale>(cx, op, obj, length, sb)) |
michael@0 | 1126 | return false; |
michael@0 | 1127 | } else if (seplen == 1) { |
michael@0 | 1128 | CharSeparatorOp op(sepchars[0]); |
michael@0 | 1129 | if (!ArrayJoinKernel<Locale>(cx, op, obj, length, sb)) |
michael@0 | 1130 | return false; |
michael@0 | 1131 | } else { |
michael@0 | 1132 | StringSeparatorOp op(sepchars, seplen); |
michael@0 | 1133 | if (!ArrayJoinKernel<Locale>(cx, op, obj, length, sb)) |
michael@0 | 1134 | return false; |
michael@0 | 1135 | } |
michael@0 | 1136 | |
michael@0 | 1137 | // Step 11 |
michael@0 | 1138 | JSString *str = sb.finishString(); |
michael@0 | 1139 | if (!str) |
michael@0 | 1140 | return false; |
michael@0 | 1141 | args.rval().setString(str); |
michael@0 | 1142 | return true; |
michael@0 | 1143 | } |
michael@0 | 1144 | |
michael@0 | 1145 | /* ES5 15.4.4.2. NB: The algorithm here differs from the one in ES3. */ |
michael@0 | 1146 | static bool |
michael@0 | 1147 | array_toString(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 1148 | { |
michael@0 | 1149 | JS_CHECK_RECURSION(cx, return false); |
michael@0 | 1150 | |
michael@0 | 1151 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 1152 | RootedObject obj(cx, ToObject(cx, args.thisv())); |
michael@0 | 1153 | if (!obj) |
michael@0 | 1154 | return false; |
michael@0 | 1155 | |
michael@0 | 1156 | RootedValue join(cx, args.calleev()); |
michael@0 | 1157 | if (!JSObject::getProperty(cx, obj, obj, cx->names().join, &join)) |
michael@0 | 1158 | return false; |
michael@0 | 1159 | |
michael@0 | 1160 | if (!js_IsCallable(join)) { |
michael@0 | 1161 | JSString *str = JS_BasicObjectToString(cx, obj); |
michael@0 | 1162 | if (!str) |
michael@0 | 1163 | return false; |
michael@0 | 1164 | args.rval().setString(str); |
michael@0 | 1165 | return true; |
michael@0 | 1166 | } |
michael@0 | 1167 | |
michael@0 | 1168 | InvokeArgs args2(cx); |
michael@0 | 1169 | if (!args2.init(0)) |
michael@0 | 1170 | return false; |
michael@0 | 1171 | |
michael@0 | 1172 | args2.setCallee(join); |
michael@0 | 1173 | args2.setThis(ObjectValue(*obj)); |
michael@0 | 1174 | |
michael@0 | 1175 | /* Do the call. */ |
michael@0 | 1176 | if (!Invoke(cx, args2)) |
michael@0 | 1177 | return false; |
michael@0 | 1178 | args.rval().set(args2.rval()); |
michael@0 | 1179 | return true; |
michael@0 | 1180 | } |
michael@0 | 1181 | |
michael@0 | 1182 | /* ES5 15.4.4.3 */ |
michael@0 | 1183 | static bool |
michael@0 | 1184 | array_toLocaleString(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 1185 | { |
michael@0 | 1186 | JS_CHECK_RECURSION(cx, return false); |
michael@0 | 1187 | |
michael@0 | 1188 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 1189 | return ArrayJoin<true>(cx, args); |
michael@0 | 1190 | } |
michael@0 | 1191 | |
michael@0 | 1192 | /* ES5 15.4.4.5 */ |
michael@0 | 1193 | static bool |
michael@0 | 1194 | array_join(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 1195 | { |
michael@0 | 1196 | JS_CHECK_RECURSION(cx, return false); |
michael@0 | 1197 | |
michael@0 | 1198 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 1199 | return ArrayJoin<false>(cx, args); |
michael@0 | 1200 | } |
michael@0 | 1201 | |
michael@0 | 1202 | static inline bool |
michael@0 | 1203 | InitArrayTypes(JSContext *cx, TypeObject *type, const Value *vector, unsigned count) |
michael@0 | 1204 | { |
michael@0 | 1205 | if (!type->unknownProperties()) { |
michael@0 | 1206 | AutoEnterAnalysis enter(cx); |
michael@0 | 1207 | |
michael@0 | 1208 | HeapTypeSet *types = type->getProperty(cx, JSID_VOID); |
michael@0 | 1209 | if (!types) |
michael@0 | 1210 | return false; |
michael@0 | 1211 | |
michael@0 | 1212 | for (unsigned i = 0; i < count; i++) { |
michael@0 | 1213 | if (vector[i].isMagic(JS_ELEMENTS_HOLE)) |
michael@0 | 1214 | continue; |
michael@0 | 1215 | Type valtype = GetValueType(vector[i]); |
michael@0 | 1216 | types->addType(cx, valtype); |
michael@0 | 1217 | } |
michael@0 | 1218 | } |
michael@0 | 1219 | return true; |
michael@0 | 1220 | } |
michael@0 | 1221 | |
michael@0 | 1222 | enum ShouldUpdateTypes |
michael@0 | 1223 | { |
michael@0 | 1224 | UpdateTypes = true, |
michael@0 | 1225 | DontUpdateTypes = false |
michael@0 | 1226 | }; |
michael@0 | 1227 | |
michael@0 | 1228 | /* vector must point to rooted memory. */ |
michael@0 | 1229 | static bool |
michael@0 | 1230 | InitArrayElements(JSContext *cx, HandleObject obj, uint32_t start, uint32_t count, const Value *vector, ShouldUpdateTypes updateTypes) |
michael@0 | 1231 | { |
michael@0 | 1232 | JS_ASSERT(count <= MAX_ARRAY_INDEX); |
michael@0 | 1233 | |
michael@0 | 1234 | if (count == 0) |
michael@0 | 1235 | return true; |
michael@0 | 1236 | |
michael@0 | 1237 | types::TypeObject *type = obj->getType(cx); |
michael@0 | 1238 | if (!type) |
michael@0 | 1239 | return false; |
michael@0 | 1240 | if (updateTypes && !InitArrayTypes(cx, type, vector, count)) |
michael@0 | 1241 | return false; |
michael@0 | 1242 | |
michael@0 | 1243 | /* |
michael@0 | 1244 | * Optimize for dense arrays so long as adding the given set of elements |
michael@0 | 1245 | * wouldn't otherwise make the array slow or exceed a non-writable array |
michael@0 | 1246 | * length. |
michael@0 | 1247 | */ |
michael@0 | 1248 | do { |
michael@0 | 1249 | if (!obj->is<ArrayObject>()) |
michael@0 | 1250 | break; |
michael@0 | 1251 | if (ObjectMayHaveExtraIndexedProperties(obj)) |
michael@0 | 1252 | break; |
michael@0 | 1253 | |
michael@0 | 1254 | if (obj->shouldConvertDoubleElements()) |
michael@0 | 1255 | break; |
michael@0 | 1256 | |
michael@0 | 1257 | Rooted<ArrayObject*> arr(cx, &obj->as<ArrayObject>()); |
michael@0 | 1258 | |
michael@0 | 1259 | if (!arr->lengthIsWritable() && start + count > arr->length()) |
michael@0 | 1260 | break; |
michael@0 | 1261 | |
michael@0 | 1262 | JSObject::EnsureDenseResult result = arr->ensureDenseElements(cx, start, count); |
michael@0 | 1263 | if (result != JSObject::ED_OK) { |
michael@0 | 1264 | if (result == JSObject::ED_FAILED) |
michael@0 | 1265 | return false; |
michael@0 | 1266 | JS_ASSERT(result == JSObject::ED_SPARSE); |
michael@0 | 1267 | break; |
michael@0 | 1268 | } |
michael@0 | 1269 | |
michael@0 | 1270 | uint32_t newlen = start + count; |
michael@0 | 1271 | if (newlen > arr->length()) |
michael@0 | 1272 | arr->setLengthInt32(newlen); |
michael@0 | 1273 | |
michael@0 | 1274 | JS_ASSERT(count < UINT32_MAX / sizeof(Value)); |
michael@0 | 1275 | arr->copyDenseElements(start, vector, count); |
michael@0 | 1276 | JS_ASSERT_IF(count != 0, !arr->getDenseElement(newlen - 1).isMagic(JS_ELEMENTS_HOLE)); |
michael@0 | 1277 | return true; |
michael@0 | 1278 | } while (false); |
michael@0 | 1279 | |
michael@0 | 1280 | const Value* end = vector + count; |
michael@0 | 1281 | while (vector < end && start <= MAX_ARRAY_INDEX) { |
michael@0 | 1282 | if (!CheckForInterrupt(cx) || |
michael@0 | 1283 | !SetArrayElement(cx, obj, start++, HandleValue::fromMarkedLocation(vector++))) { |
michael@0 | 1284 | return false; |
michael@0 | 1285 | } |
michael@0 | 1286 | } |
michael@0 | 1287 | |
michael@0 | 1288 | if (vector == end) |
michael@0 | 1289 | return true; |
michael@0 | 1290 | |
michael@0 | 1291 | JS_ASSERT(start == MAX_ARRAY_INDEX + 1); |
michael@0 | 1292 | RootedValue value(cx); |
michael@0 | 1293 | RootedId id(cx); |
michael@0 | 1294 | RootedValue indexv(cx); |
michael@0 | 1295 | double index = MAX_ARRAY_INDEX + 1; |
michael@0 | 1296 | do { |
michael@0 | 1297 | value = *vector++; |
michael@0 | 1298 | indexv = DoubleValue(index); |
michael@0 | 1299 | if (!ValueToId<CanGC>(cx, indexv, &id) || |
michael@0 | 1300 | !JSObject::setGeneric(cx, obj, obj, id, &value, true)) |
michael@0 | 1301 | { |
michael@0 | 1302 | return false; |
michael@0 | 1303 | } |
michael@0 | 1304 | index += 1; |
michael@0 | 1305 | } while (vector != end); |
michael@0 | 1306 | |
michael@0 | 1307 | return true; |
michael@0 | 1308 | } |
michael@0 | 1309 | |
michael@0 | 1310 | static bool |
michael@0 | 1311 | array_reverse(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 1312 | { |
michael@0 | 1313 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 1314 | RootedObject obj(cx, ToObject(cx, args.thisv())); |
michael@0 | 1315 | if (!obj) |
michael@0 | 1316 | return false; |
michael@0 | 1317 | |
michael@0 | 1318 | uint32_t len; |
michael@0 | 1319 | if (!GetLengthProperty(cx, obj, &len)) |
michael@0 | 1320 | return false; |
michael@0 | 1321 | |
michael@0 | 1322 | do { |
michael@0 | 1323 | if (!obj->is<ArrayObject>()) |
michael@0 | 1324 | break; |
michael@0 | 1325 | if (ObjectMayHaveExtraIndexedProperties(obj)) |
michael@0 | 1326 | break; |
michael@0 | 1327 | |
michael@0 | 1328 | /* An empty array or an array with no elements is already reversed. */ |
michael@0 | 1329 | if (len == 0 || obj->getDenseCapacity() == 0) { |
michael@0 | 1330 | args.rval().setObject(*obj); |
michael@0 | 1331 | return true; |
michael@0 | 1332 | } |
michael@0 | 1333 | |
michael@0 | 1334 | /* |
michael@0 | 1335 | * It's actually surprisingly complicated to reverse an array due to the |
michael@0 | 1336 | * orthogonality of array length and array capacity while handling |
michael@0 | 1337 | * leading and trailing holes correctly. Reversing seems less likely to |
michael@0 | 1338 | * be a common operation than other array mass-mutation methods, so for |
michael@0 | 1339 | * now just take a probably-small memory hit (in the absence of too many |
michael@0 | 1340 | * holes in the array at its start) and ensure that the capacity is |
michael@0 | 1341 | * sufficient to hold all the elements in the array if it were full. |
michael@0 | 1342 | */ |
michael@0 | 1343 | JSObject::EnsureDenseResult result = obj->ensureDenseElements(cx, len, 0); |
michael@0 | 1344 | if (result != JSObject::ED_OK) { |
michael@0 | 1345 | if (result == JSObject::ED_FAILED) |
michael@0 | 1346 | return false; |
michael@0 | 1347 | JS_ASSERT(result == JSObject::ED_SPARSE); |
michael@0 | 1348 | break; |
michael@0 | 1349 | } |
michael@0 | 1350 | |
michael@0 | 1351 | /* Fill out the array's initialized length to its proper length. */ |
michael@0 | 1352 | obj->ensureDenseInitializedLength(cx, len, 0); |
michael@0 | 1353 | |
michael@0 | 1354 | RootedValue origlo(cx), orighi(cx); |
michael@0 | 1355 | |
michael@0 | 1356 | uint32_t lo = 0, hi = len - 1; |
michael@0 | 1357 | for (; lo < hi; lo++, hi--) { |
michael@0 | 1358 | origlo = obj->getDenseElement(lo); |
michael@0 | 1359 | orighi = obj->getDenseElement(hi); |
michael@0 | 1360 | obj->setDenseElement(lo, orighi); |
michael@0 | 1361 | if (orighi.isMagic(JS_ELEMENTS_HOLE) && |
michael@0 | 1362 | !js_SuppressDeletedProperty(cx, obj, INT_TO_JSID(lo))) { |
michael@0 | 1363 | return false; |
michael@0 | 1364 | } |
michael@0 | 1365 | obj->setDenseElement(hi, origlo); |
michael@0 | 1366 | if (origlo.isMagic(JS_ELEMENTS_HOLE) && |
michael@0 | 1367 | !js_SuppressDeletedProperty(cx, obj, INT_TO_JSID(hi))) { |
michael@0 | 1368 | return false; |
michael@0 | 1369 | } |
michael@0 | 1370 | } |
michael@0 | 1371 | |
michael@0 | 1372 | /* |
michael@0 | 1373 | * Per ECMA-262, don't update the length of the array, even if the new |
michael@0 | 1374 | * array has trailing holes (and thus the original array began with |
michael@0 | 1375 | * holes). |
michael@0 | 1376 | */ |
michael@0 | 1377 | args.rval().setObject(*obj); |
michael@0 | 1378 | return true; |
michael@0 | 1379 | } while (false); |
michael@0 | 1380 | |
michael@0 | 1381 | RootedValue lowval(cx), hival(cx); |
michael@0 | 1382 | for (uint32_t i = 0, half = len / 2; i < half; i++) { |
michael@0 | 1383 | bool hole, hole2; |
michael@0 | 1384 | if (!CheckForInterrupt(cx) || |
michael@0 | 1385 | !GetElement(cx, obj, i, &hole, &lowval) || |
michael@0 | 1386 | !GetElement(cx, obj, len - i - 1, &hole2, &hival)) |
michael@0 | 1387 | { |
michael@0 | 1388 | return false; |
michael@0 | 1389 | } |
michael@0 | 1390 | |
michael@0 | 1391 | if (!hole && !hole2) { |
michael@0 | 1392 | if (!SetArrayElement(cx, obj, i, hival)) |
michael@0 | 1393 | return false; |
michael@0 | 1394 | if (!SetArrayElement(cx, obj, len - i - 1, lowval)) |
michael@0 | 1395 | return false; |
michael@0 | 1396 | } else if (hole && !hole2) { |
michael@0 | 1397 | if (!SetArrayElement(cx, obj, i, hival)) |
michael@0 | 1398 | return false; |
michael@0 | 1399 | if (!DeletePropertyOrThrow(cx, obj, len - i - 1)) |
michael@0 | 1400 | return false; |
michael@0 | 1401 | } else if (!hole && hole2) { |
michael@0 | 1402 | if (!DeletePropertyOrThrow(cx, obj, i)) |
michael@0 | 1403 | return false; |
michael@0 | 1404 | if (!SetArrayElement(cx, obj, len - i - 1, lowval)) |
michael@0 | 1405 | return false; |
michael@0 | 1406 | } else { |
michael@0 | 1407 | // No action required. |
michael@0 | 1408 | } |
michael@0 | 1409 | } |
michael@0 | 1410 | args.rval().setObject(*obj); |
michael@0 | 1411 | return true; |
michael@0 | 1412 | } |
michael@0 | 1413 | |
michael@0 | 1414 | namespace { |
michael@0 | 1415 | |
michael@0 | 1416 | inline bool |
michael@0 | 1417 | CompareStringValues(JSContext *cx, const Value &a, const Value &b, bool *lessOrEqualp) |
michael@0 | 1418 | { |
michael@0 | 1419 | if (!CheckForInterrupt(cx)) |
michael@0 | 1420 | return false; |
michael@0 | 1421 | |
michael@0 | 1422 | JSString *astr = a.toString(); |
michael@0 | 1423 | JSString *bstr = b.toString(); |
michael@0 | 1424 | int32_t result; |
michael@0 | 1425 | if (!CompareStrings(cx, astr, bstr, &result)) |
michael@0 | 1426 | return false; |
michael@0 | 1427 | |
michael@0 | 1428 | *lessOrEqualp = (result <= 0); |
michael@0 | 1429 | return true; |
michael@0 | 1430 | } |
michael@0 | 1431 | |
michael@0 | 1432 | static const uint64_t powersOf10[] = { |
michael@0 | 1433 | 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 1000000000000ULL |
michael@0 | 1434 | }; |
michael@0 | 1435 | |
michael@0 | 1436 | static inline unsigned |
michael@0 | 1437 | NumDigitsBase10(uint32_t n) |
michael@0 | 1438 | { |
michael@0 | 1439 | /* |
michael@0 | 1440 | * This is just floor_log10(n) + 1 |
michael@0 | 1441 | * Algorithm taken from |
michael@0 | 1442 | * http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 |
michael@0 | 1443 | */ |
michael@0 | 1444 | uint32_t log2 = CeilingLog2(n); |
michael@0 | 1445 | uint32_t t = log2 * 1233 >> 12; |
michael@0 | 1446 | return t - (n < powersOf10[t]) + 1; |
michael@0 | 1447 | } |
michael@0 | 1448 | |
michael@0 | 1449 | static inline bool |
michael@0 | 1450 | CompareLexicographicInt32(const Value &a, const Value &b, bool *lessOrEqualp) |
michael@0 | 1451 | { |
michael@0 | 1452 | int32_t aint = a.toInt32(); |
michael@0 | 1453 | int32_t bint = b.toInt32(); |
michael@0 | 1454 | |
michael@0 | 1455 | /* |
michael@0 | 1456 | * If both numbers are equal ... trivial |
michael@0 | 1457 | * If only one of both is negative --> arithmetic comparison as char code |
michael@0 | 1458 | * of '-' is always less than any other digit |
michael@0 | 1459 | * If both numbers are negative convert them to positive and continue |
michael@0 | 1460 | * handling ... |
michael@0 | 1461 | */ |
michael@0 | 1462 | if (aint == bint) { |
michael@0 | 1463 | *lessOrEqualp = true; |
michael@0 | 1464 | } else if ((aint < 0) && (bint >= 0)) { |
michael@0 | 1465 | *lessOrEqualp = true; |
michael@0 | 1466 | } else if ((aint >= 0) && (bint < 0)) { |
michael@0 | 1467 | *lessOrEqualp = false; |
michael@0 | 1468 | } else { |
michael@0 | 1469 | uint32_t auint = Abs(aint); |
michael@0 | 1470 | uint32_t buint = Abs(bint); |
michael@0 | 1471 | |
michael@0 | 1472 | /* |
michael@0 | 1473 | * ... get number of digits of both integers. |
michael@0 | 1474 | * If they have the same number of digits --> arithmetic comparison. |
michael@0 | 1475 | * If digits_a > digits_b: a < b*10e(digits_a - digits_b). |
michael@0 | 1476 | * If digits_b > digits_a: a*10e(digits_b - digits_a) <= b. |
michael@0 | 1477 | */ |
michael@0 | 1478 | unsigned digitsa = NumDigitsBase10(auint); |
michael@0 | 1479 | unsigned digitsb = NumDigitsBase10(buint); |
michael@0 | 1480 | if (digitsa == digitsb) { |
michael@0 | 1481 | *lessOrEqualp = (auint <= buint); |
michael@0 | 1482 | } else if (digitsa > digitsb) { |
michael@0 | 1483 | JS_ASSERT((digitsa - digitsb) < ArrayLength(powersOf10)); |
michael@0 | 1484 | *lessOrEqualp = (uint64_t(auint) < uint64_t(buint) * powersOf10[digitsa - digitsb]); |
michael@0 | 1485 | } else { /* if (digitsb > digitsa) */ |
michael@0 | 1486 | JS_ASSERT((digitsb - digitsa) < ArrayLength(powersOf10)); |
michael@0 | 1487 | *lessOrEqualp = (uint64_t(auint) * powersOf10[digitsb - digitsa] <= uint64_t(buint)); |
michael@0 | 1488 | } |
michael@0 | 1489 | } |
michael@0 | 1490 | |
michael@0 | 1491 | return true; |
michael@0 | 1492 | } |
michael@0 | 1493 | |
michael@0 | 1494 | static inline bool |
michael@0 | 1495 | CompareSubStringValues(JSContext *cx, const jschar *s1, size_t l1, |
michael@0 | 1496 | const jschar *s2, size_t l2, bool *lessOrEqualp) |
michael@0 | 1497 | { |
michael@0 | 1498 | if (!CheckForInterrupt(cx)) |
michael@0 | 1499 | return false; |
michael@0 | 1500 | |
michael@0 | 1501 | if (!s1 || !s2) |
michael@0 | 1502 | return false; |
michael@0 | 1503 | |
michael@0 | 1504 | int32_t result = CompareChars(s1, l1, s2, l2); |
michael@0 | 1505 | *lessOrEqualp = (result <= 0); |
michael@0 | 1506 | return true; |
michael@0 | 1507 | } |
michael@0 | 1508 | |
michael@0 | 1509 | struct SortComparatorStrings |
michael@0 | 1510 | { |
michael@0 | 1511 | JSContext *const cx; |
michael@0 | 1512 | |
michael@0 | 1513 | SortComparatorStrings(JSContext *cx) |
michael@0 | 1514 | : cx(cx) {} |
michael@0 | 1515 | |
michael@0 | 1516 | bool operator()(const Value &a, const Value &b, bool *lessOrEqualp) { |
michael@0 | 1517 | return CompareStringValues(cx, a, b, lessOrEqualp); |
michael@0 | 1518 | } |
michael@0 | 1519 | }; |
michael@0 | 1520 | |
michael@0 | 1521 | struct SortComparatorLexicographicInt32 |
michael@0 | 1522 | { |
michael@0 | 1523 | bool operator()(const Value &a, const Value &b, bool *lessOrEqualp) { |
michael@0 | 1524 | return CompareLexicographicInt32(a, b, lessOrEqualp); |
michael@0 | 1525 | } |
michael@0 | 1526 | }; |
michael@0 | 1527 | |
michael@0 | 1528 | struct StringifiedElement |
michael@0 | 1529 | { |
michael@0 | 1530 | size_t charsBegin; |
michael@0 | 1531 | size_t charsEnd; |
michael@0 | 1532 | size_t elementIndex; |
michael@0 | 1533 | }; |
michael@0 | 1534 | |
michael@0 | 1535 | struct SortComparatorStringifiedElements |
michael@0 | 1536 | { |
michael@0 | 1537 | JSContext *const cx; |
michael@0 | 1538 | const StringBuffer &sb; |
michael@0 | 1539 | |
michael@0 | 1540 | SortComparatorStringifiedElements(JSContext *cx, const StringBuffer &sb) |
michael@0 | 1541 | : cx(cx), sb(sb) {} |
michael@0 | 1542 | |
michael@0 | 1543 | bool operator()(const StringifiedElement &a, const StringifiedElement &b, bool *lessOrEqualp) { |
michael@0 | 1544 | return CompareSubStringValues(cx, sb.begin() + a.charsBegin, a.charsEnd - a.charsBegin, |
michael@0 | 1545 | sb.begin() + b.charsBegin, b.charsEnd - b.charsBegin, |
michael@0 | 1546 | lessOrEqualp); |
michael@0 | 1547 | } |
michael@0 | 1548 | }; |
michael@0 | 1549 | |
michael@0 | 1550 | struct SortComparatorFunction |
michael@0 | 1551 | { |
michael@0 | 1552 | JSContext *const cx; |
michael@0 | 1553 | const Value &fval; |
michael@0 | 1554 | FastInvokeGuard &fig; |
michael@0 | 1555 | |
michael@0 | 1556 | SortComparatorFunction(JSContext *cx, const Value &fval, FastInvokeGuard &fig) |
michael@0 | 1557 | : cx(cx), fval(fval), fig(fig) { } |
michael@0 | 1558 | |
michael@0 | 1559 | bool operator()(const Value &a, const Value &b, bool *lessOrEqualp); |
michael@0 | 1560 | }; |
michael@0 | 1561 | |
michael@0 | 1562 | bool |
michael@0 | 1563 | SortComparatorFunction::operator()(const Value &a, const Value &b, bool *lessOrEqualp) |
michael@0 | 1564 | { |
michael@0 | 1565 | /* |
michael@0 | 1566 | * array_sort deals with holes and undefs on its own and they should not |
michael@0 | 1567 | * come here. |
michael@0 | 1568 | */ |
michael@0 | 1569 | JS_ASSERT(!a.isMagic() && !a.isUndefined()); |
michael@0 | 1570 | JS_ASSERT(!a.isMagic() && !b.isUndefined()); |
michael@0 | 1571 | |
michael@0 | 1572 | if (!CheckForInterrupt(cx)) |
michael@0 | 1573 | return false; |
michael@0 | 1574 | |
michael@0 | 1575 | InvokeArgs &args = fig.args(); |
michael@0 | 1576 | if (!args.init(2)) |
michael@0 | 1577 | return false; |
michael@0 | 1578 | |
michael@0 | 1579 | args.setCallee(fval); |
michael@0 | 1580 | args.setThis(UndefinedValue()); |
michael@0 | 1581 | args[0].set(a); |
michael@0 | 1582 | args[1].set(b); |
michael@0 | 1583 | |
michael@0 | 1584 | if (!fig.invoke(cx)) |
michael@0 | 1585 | return false; |
michael@0 | 1586 | |
michael@0 | 1587 | double cmp; |
michael@0 | 1588 | if (!ToNumber(cx, args.rval(), &cmp)) |
michael@0 | 1589 | return false; |
michael@0 | 1590 | |
michael@0 | 1591 | /* |
michael@0 | 1592 | * XXX eport some kind of error here if cmp is NaN? ECMA talks about |
michael@0 | 1593 | * 'consistent compare functions' that don't return NaN, but is silent |
michael@0 | 1594 | * about what the result should be. So we currently ignore it. |
michael@0 | 1595 | */ |
michael@0 | 1596 | *lessOrEqualp = (IsNaN(cmp) || cmp <= 0); |
michael@0 | 1597 | return true; |
michael@0 | 1598 | } |
michael@0 | 1599 | |
michael@0 | 1600 | struct NumericElement |
michael@0 | 1601 | { |
michael@0 | 1602 | double dv; |
michael@0 | 1603 | size_t elementIndex; |
michael@0 | 1604 | }; |
michael@0 | 1605 | |
michael@0 | 1606 | bool |
michael@0 | 1607 | ComparatorNumericLeftMinusRight(const NumericElement &a, const NumericElement &b, |
michael@0 | 1608 | bool *lessOrEqualp) |
michael@0 | 1609 | { |
michael@0 | 1610 | *lessOrEqualp = (a.dv <= b.dv); |
michael@0 | 1611 | return true; |
michael@0 | 1612 | } |
michael@0 | 1613 | |
michael@0 | 1614 | bool |
michael@0 | 1615 | ComparatorNumericRightMinusLeft(const NumericElement &a, const NumericElement &b, |
michael@0 | 1616 | bool *lessOrEqualp) |
michael@0 | 1617 | { |
michael@0 | 1618 | *lessOrEqualp = (b.dv <= a.dv); |
michael@0 | 1619 | return true; |
michael@0 | 1620 | } |
michael@0 | 1621 | |
michael@0 | 1622 | typedef bool (*ComparatorNumeric)(const NumericElement &a, const NumericElement &b, |
michael@0 | 1623 | bool *lessOrEqualp); |
michael@0 | 1624 | |
michael@0 | 1625 | ComparatorNumeric SortComparatorNumerics[] = { |
michael@0 | 1626 | nullptr, |
michael@0 | 1627 | nullptr, |
michael@0 | 1628 | ComparatorNumericLeftMinusRight, |
michael@0 | 1629 | ComparatorNumericRightMinusLeft |
michael@0 | 1630 | }; |
michael@0 | 1631 | |
michael@0 | 1632 | bool |
michael@0 | 1633 | ComparatorInt32LeftMinusRight(const Value &a, const Value &b, bool *lessOrEqualp) |
michael@0 | 1634 | { |
michael@0 | 1635 | *lessOrEqualp = (a.toInt32() <= b.toInt32()); |
michael@0 | 1636 | return true; |
michael@0 | 1637 | } |
michael@0 | 1638 | |
michael@0 | 1639 | bool |
michael@0 | 1640 | ComparatorInt32RightMinusLeft(const Value &a, const Value &b, bool *lessOrEqualp) |
michael@0 | 1641 | { |
michael@0 | 1642 | *lessOrEqualp = (b.toInt32() <= a.toInt32()); |
michael@0 | 1643 | return true; |
michael@0 | 1644 | } |
michael@0 | 1645 | |
michael@0 | 1646 | typedef bool (*ComparatorInt32)(const Value &a, const Value &b, bool *lessOrEqualp); |
michael@0 | 1647 | |
michael@0 | 1648 | ComparatorInt32 SortComparatorInt32s[] = { |
michael@0 | 1649 | nullptr, |
michael@0 | 1650 | nullptr, |
michael@0 | 1651 | ComparatorInt32LeftMinusRight, |
michael@0 | 1652 | ComparatorInt32RightMinusLeft |
michael@0 | 1653 | }; |
michael@0 | 1654 | |
michael@0 | 1655 | // Note: Values for this enum must match up with SortComparatorNumerics |
michael@0 | 1656 | // and SortComparatorInt32s. |
michael@0 | 1657 | enum ComparatorMatchResult { |
michael@0 | 1658 | Match_Failure = 0, |
michael@0 | 1659 | Match_None, |
michael@0 | 1660 | Match_LeftMinusRight, |
michael@0 | 1661 | Match_RightMinusLeft |
michael@0 | 1662 | }; |
michael@0 | 1663 | |
michael@0 | 1664 | /* |
michael@0 | 1665 | * Specialize behavior for comparator functions with particular common bytecode |
michael@0 | 1666 | * patterns: namely, |return x - y| and |return y - x|. |
michael@0 | 1667 | */ |
michael@0 | 1668 | ComparatorMatchResult |
michael@0 | 1669 | MatchNumericComparator(JSContext *cx, const Value &v) |
michael@0 | 1670 | { |
michael@0 | 1671 | if (!v.isObject()) |
michael@0 | 1672 | return Match_None; |
michael@0 | 1673 | |
michael@0 | 1674 | JSObject &obj = v.toObject(); |
michael@0 | 1675 | if (!obj.is<JSFunction>()) |
michael@0 | 1676 | return Match_None; |
michael@0 | 1677 | |
michael@0 | 1678 | JSFunction *fun = &obj.as<JSFunction>(); |
michael@0 | 1679 | if (!fun->isInterpreted()) |
michael@0 | 1680 | return Match_None; |
michael@0 | 1681 | |
michael@0 | 1682 | JSScript *script = fun->getOrCreateScript(cx); |
michael@0 | 1683 | if (!script) |
michael@0 | 1684 | return Match_Failure; |
michael@0 | 1685 | |
michael@0 | 1686 | jsbytecode *pc = script->code(); |
michael@0 | 1687 | |
michael@0 | 1688 | uint16_t arg0, arg1; |
michael@0 | 1689 | if (JSOp(*pc) != JSOP_GETARG) |
michael@0 | 1690 | return Match_None; |
michael@0 | 1691 | arg0 = GET_ARGNO(pc); |
michael@0 | 1692 | pc += JSOP_GETARG_LENGTH; |
michael@0 | 1693 | |
michael@0 | 1694 | if (JSOp(*pc) != JSOP_GETARG) |
michael@0 | 1695 | return Match_None; |
michael@0 | 1696 | arg1 = GET_ARGNO(pc); |
michael@0 | 1697 | pc += JSOP_GETARG_LENGTH; |
michael@0 | 1698 | |
michael@0 | 1699 | if (JSOp(*pc) != JSOP_SUB) |
michael@0 | 1700 | return Match_None; |
michael@0 | 1701 | pc += JSOP_SUB_LENGTH; |
michael@0 | 1702 | |
michael@0 | 1703 | if (JSOp(*pc) != JSOP_RETURN) |
michael@0 | 1704 | return Match_None; |
michael@0 | 1705 | |
michael@0 | 1706 | if (arg0 == 0 && arg1 == 1) |
michael@0 | 1707 | return Match_LeftMinusRight; |
michael@0 | 1708 | |
michael@0 | 1709 | if (arg0 == 1 && arg1 == 0) |
michael@0 | 1710 | return Match_RightMinusLeft; |
michael@0 | 1711 | |
michael@0 | 1712 | return Match_None; |
michael@0 | 1713 | } |
michael@0 | 1714 | |
michael@0 | 1715 | template<typename K, typename C> |
michael@0 | 1716 | static inline bool |
michael@0 | 1717 | MergeSortByKey(K keys, size_t len, K scratch, C comparator, AutoValueVector *vec) |
michael@0 | 1718 | { |
michael@0 | 1719 | MOZ_ASSERT(vec->length() >= len); |
michael@0 | 1720 | |
michael@0 | 1721 | /* Sort keys. */ |
michael@0 | 1722 | if (!MergeSort(keys, len, scratch, comparator)) |
michael@0 | 1723 | return false; |
michael@0 | 1724 | |
michael@0 | 1725 | /* |
michael@0 | 1726 | * Reorder vec by keys in-place, going element by element. When an out-of- |
michael@0 | 1727 | * place element is encountered, move that element to its proper position, |
michael@0 | 1728 | * displacing whatever element was at *that* point to its proper position, |
michael@0 | 1729 | * and so on until an element must be moved to the current position. |
michael@0 | 1730 | * |
michael@0 | 1731 | * At each outer iteration all elements up to |i| are sorted. If |
michael@0 | 1732 | * necessary each inner iteration moves some number of unsorted elements |
michael@0 | 1733 | * (including |i|) directly to sorted position. Thus on completion |*vec| |
michael@0 | 1734 | * is sorted, and out-of-position elements have moved once. Complexity is |
michael@0 | 1735 | * Θ(len) + O(len) == O(2*len), with each element visited at most twice. |
michael@0 | 1736 | */ |
michael@0 | 1737 | for (size_t i = 0; i < len; i++) { |
michael@0 | 1738 | size_t j = keys[i].elementIndex; |
michael@0 | 1739 | if (i == j) |
michael@0 | 1740 | continue; // fixed point |
michael@0 | 1741 | |
michael@0 | 1742 | MOZ_ASSERT(j > i, "Everything less than |i| should be in the right place!"); |
michael@0 | 1743 | Value tv = (*vec)[j]; |
michael@0 | 1744 | do { |
michael@0 | 1745 | size_t k = keys[j].elementIndex; |
michael@0 | 1746 | keys[j].elementIndex = j; |
michael@0 | 1747 | (*vec)[j] = (*vec)[k]; |
michael@0 | 1748 | j = k; |
michael@0 | 1749 | } while (j != i); |
michael@0 | 1750 | |
michael@0 | 1751 | // We could assert the loop invariant that |i == keys[i].elementIndex| |
michael@0 | 1752 | // here if we synced |keys[i].elementIndex|. But doing so would render |
michael@0 | 1753 | // the assertion vacuous, so don't bother, even in debug builds. |
michael@0 | 1754 | (*vec)[i] = tv; |
michael@0 | 1755 | } |
michael@0 | 1756 | |
michael@0 | 1757 | return true; |
michael@0 | 1758 | } |
michael@0 | 1759 | |
michael@0 | 1760 | /* |
michael@0 | 1761 | * Sort Values as strings. |
michael@0 | 1762 | * |
michael@0 | 1763 | * To minimize #conversions, SortLexicographically() first converts all Values |
michael@0 | 1764 | * to strings at once, then sorts the elements by these cached strings. |
michael@0 | 1765 | */ |
michael@0 | 1766 | bool |
michael@0 | 1767 | SortLexicographically(JSContext *cx, AutoValueVector *vec, size_t len) |
michael@0 | 1768 | { |
michael@0 | 1769 | JS_ASSERT(vec->length() >= len); |
michael@0 | 1770 | |
michael@0 | 1771 | StringBuffer sb(cx); |
michael@0 | 1772 | Vector<StringifiedElement, 0, TempAllocPolicy> strElements(cx); |
michael@0 | 1773 | |
michael@0 | 1774 | /* MergeSort uses the upper half as scratch space. */ |
michael@0 | 1775 | if (!strElements.reserve(2 * len)) |
michael@0 | 1776 | return false; |
michael@0 | 1777 | |
michael@0 | 1778 | /* Convert Values to strings. */ |
michael@0 | 1779 | size_t cursor = 0; |
michael@0 | 1780 | for (size_t i = 0; i < len; i++) { |
michael@0 | 1781 | if (!CheckForInterrupt(cx)) |
michael@0 | 1782 | return false; |
michael@0 | 1783 | |
michael@0 | 1784 | if (!ValueToStringBuffer(cx, (*vec)[i], sb)) |
michael@0 | 1785 | return false; |
michael@0 | 1786 | |
michael@0 | 1787 | StringifiedElement el = { cursor, sb.length(), i }; |
michael@0 | 1788 | strElements.infallibleAppend(el); |
michael@0 | 1789 | cursor = sb.length(); |
michael@0 | 1790 | } |
michael@0 | 1791 | |
michael@0 | 1792 | /* Resize strElements so we can perform MergeSort. */ |
michael@0 | 1793 | JS_ALWAYS_TRUE(strElements.resize(2 * len)); |
michael@0 | 1794 | |
michael@0 | 1795 | /* Sort Values in vec alphabetically. */ |
michael@0 | 1796 | return MergeSortByKey(strElements.begin(), len, strElements.begin() + len, |
michael@0 | 1797 | SortComparatorStringifiedElements(cx, sb), vec); |
michael@0 | 1798 | } |
michael@0 | 1799 | |
michael@0 | 1800 | /* |
michael@0 | 1801 | * Sort Values as numbers. |
michael@0 | 1802 | * |
michael@0 | 1803 | * To minimize #conversions, SortNumerically first converts all Values to |
michael@0 | 1804 | * numerics at once, then sorts the elements by these cached numerics. |
michael@0 | 1805 | */ |
michael@0 | 1806 | bool |
michael@0 | 1807 | SortNumerically(JSContext *cx, AutoValueVector *vec, size_t len, ComparatorMatchResult comp) |
michael@0 | 1808 | { |
michael@0 | 1809 | JS_ASSERT(vec->length() >= len); |
michael@0 | 1810 | |
michael@0 | 1811 | Vector<NumericElement, 0, TempAllocPolicy> numElements(cx); |
michael@0 | 1812 | |
michael@0 | 1813 | /* MergeSort uses the upper half as scratch space. */ |
michael@0 | 1814 | if (!numElements.reserve(2 * len)) |
michael@0 | 1815 | return false; |
michael@0 | 1816 | |
michael@0 | 1817 | /* Convert Values to numerics. */ |
michael@0 | 1818 | for (size_t i = 0; i < len; i++) { |
michael@0 | 1819 | if (!CheckForInterrupt(cx)) |
michael@0 | 1820 | return false; |
michael@0 | 1821 | |
michael@0 | 1822 | double dv; |
michael@0 | 1823 | if (!ToNumber(cx, vec->handleAt(i), &dv)) |
michael@0 | 1824 | return false; |
michael@0 | 1825 | |
michael@0 | 1826 | NumericElement el = { dv, i }; |
michael@0 | 1827 | numElements.infallibleAppend(el); |
michael@0 | 1828 | } |
michael@0 | 1829 | |
michael@0 | 1830 | /* Resize strElements so we can perform MergeSort. */ |
michael@0 | 1831 | JS_ALWAYS_TRUE(numElements.resize(2 * len)); |
michael@0 | 1832 | |
michael@0 | 1833 | /* Sort Values in vec numerically. */ |
michael@0 | 1834 | return MergeSortByKey(numElements.begin(), len, numElements.begin() + len, |
michael@0 | 1835 | SortComparatorNumerics[comp], vec); |
michael@0 | 1836 | } |
michael@0 | 1837 | |
michael@0 | 1838 | } /* namespace anonymous */ |
michael@0 | 1839 | |
michael@0 | 1840 | bool |
michael@0 | 1841 | js::array_sort(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 1842 | { |
michael@0 | 1843 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 1844 | |
michael@0 | 1845 | RootedValue fvalRoot(cx); |
michael@0 | 1846 | Value &fval = fvalRoot.get(); |
michael@0 | 1847 | |
michael@0 | 1848 | if (args.hasDefined(0)) { |
michael@0 | 1849 | if (args[0].isPrimitive()) { |
michael@0 | 1850 | JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_BAD_SORT_ARG); |
michael@0 | 1851 | return false; |
michael@0 | 1852 | } |
michael@0 | 1853 | fval = args[0]; /* non-default compare function */ |
michael@0 | 1854 | } else { |
michael@0 | 1855 | fval.setNull(); |
michael@0 | 1856 | } |
michael@0 | 1857 | |
michael@0 | 1858 | RootedObject obj(cx, ToObject(cx, args.thisv())); |
michael@0 | 1859 | if (!obj) |
michael@0 | 1860 | return false; |
michael@0 | 1861 | |
michael@0 | 1862 | uint32_t len; |
michael@0 | 1863 | if (!GetLengthProperty(cx, obj, &len)) |
michael@0 | 1864 | return false; |
michael@0 | 1865 | if (len < 2) { |
michael@0 | 1866 | /* [] and [a] remain unchanged when sorted. */ |
michael@0 | 1867 | args.rval().setObject(*obj); |
michael@0 | 1868 | return true; |
michael@0 | 1869 | } |
michael@0 | 1870 | |
michael@0 | 1871 | /* |
michael@0 | 1872 | * We need a temporary array of 2 * len Value to hold the array elements |
michael@0 | 1873 | * and the scratch space for merge sort. Check that its size does not |
michael@0 | 1874 | * overflow size_t, which would allow for indexing beyond the end of the |
michael@0 | 1875 | * malloc'd vector. |
michael@0 | 1876 | */ |
michael@0 | 1877 | #if JS_BITS_PER_WORD == 32 |
michael@0 | 1878 | if (size_t(len) > size_t(-1) / (2 * sizeof(Value))) { |
michael@0 | 1879 | js_ReportAllocationOverflow(cx); |
michael@0 | 1880 | return false; |
michael@0 | 1881 | } |
michael@0 | 1882 | #endif |
michael@0 | 1883 | |
michael@0 | 1884 | /* |
michael@0 | 1885 | * Initialize vec as a root. We will clear elements of vec one by |
michael@0 | 1886 | * one while increasing the rooted amount of vec when we know that the |
michael@0 | 1887 | * property at the corresponding index exists and its value must be rooted. |
michael@0 | 1888 | * |
michael@0 | 1889 | * In this way when sorting a huge mostly sparse array we will not |
michael@0 | 1890 | * access the tail of vec corresponding to properties that do not |
michael@0 | 1891 | * exist, allowing OS to avoiding committing RAM. See bug 330812. |
michael@0 | 1892 | */ |
michael@0 | 1893 | size_t n, undefs; |
michael@0 | 1894 | { |
michael@0 | 1895 | AutoValueVector vec(cx); |
michael@0 | 1896 | if (!vec.reserve(2 * size_t(len))) |
michael@0 | 1897 | return false; |
michael@0 | 1898 | |
michael@0 | 1899 | /* |
michael@0 | 1900 | * By ECMA 262, 15.4.4.11, a property that does not exist (which we |
michael@0 | 1901 | * call a "hole") is always greater than an existing property with |
michael@0 | 1902 | * value undefined and that is always greater than any other property. |
michael@0 | 1903 | * Thus to sort holes and undefs we simply count them, sort the rest |
michael@0 | 1904 | * of elements, append undefs after them and then make holes after |
michael@0 | 1905 | * undefs. |
michael@0 | 1906 | */ |
michael@0 | 1907 | undefs = 0; |
michael@0 | 1908 | bool allStrings = true; |
michael@0 | 1909 | bool allInts = true; |
michael@0 | 1910 | RootedValue v(cx); |
michael@0 | 1911 | for (uint32_t i = 0; i < len; i++) { |
michael@0 | 1912 | if (!CheckForInterrupt(cx)) |
michael@0 | 1913 | return false; |
michael@0 | 1914 | |
michael@0 | 1915 | /* Clear vec[newlen] before including it in the rooted set. */ |
michael@0 | 1916 | bool hole; |
michael@0 | 1917 | if (!GetElement(cx, obj, i, &hole, &v)) |
michael@0 | 1918 | return false; |
michael@0 | 1919 | if (hole) |
michael@0 | 1920 | continue; |
michael@0 | 1921 | if (v.isUndefined()) { |
michael@0 | 1922 | ++undefs; |
michael@0 | 1923 | continue; |
michael@0 | 1924 | } |
michael@0 | 1925 | vec.infallibleAppend(v); |
michael@0 | 1926 | allStrings = allStrings && v.isString(); |
michael@0 | 1927 | allInts = allInts && v.isInt32(); |
michael@0 | 1928 | } |
michael@0 | 1929 | |
michael@0 | 1930 | |
michael@0 | 1931 | /* |
michael@0 | 1932 | * If the array only contains holes, we're done. But if it contains |
michael@0 | 1933 | * undefs, those must be sorted to the front of the array. |
michael@0 | 1934 | */ |
michael@0 | 1935 | n = vec.length(); |
michael@0 | 1936 | if (n == 0 && undefs == 0) { |
michael@0 | 1937 | args.rval().setObject(*obj); |
michael@0 | 1938 | return true; |
michael@0 | 1939 | } |
michael@0 | 1940 | |
michael@0 | 1941 | /* Here len == n + undefs + number_of_holes. */ |
michael@0 | 1942 | if (fval.isNull()) { |
michael@0 | 1943 | /* |
michael@0 | 1944 | * Sort using the default comparator converting all elements to |
michael@0 | 1945 | * strings. |
michael@0 | 1946 | */ |
michael@0 | 1947 | if (allStrings) { |
michael@0 | 1948 | JS_ALWAYS_TRUE(vec.resize(n * 2)); |
michael@0 | 1949 | if (!MergeSort(vec.begin(), n, vec.begin() + n, SortComparatorStrings(cx))) |
michael@0 | 1950 | return false; |
michael@0 | 1951 | } else if (allInts) { |
michael@0 | 1952 | JS_ALWAYS_TRUE(vec.resize(n * 2)); |
michael@0 | 1953 | if (!MergeSort(vec.begin(), n, vec.begin() + n, |
michael@0 | 1954 | SortComparatorLexicographicInt32())) { |
michael@0 | 1955 | return false; |
michael@0 | 1956 | } |
michael@0 | 1957 | } else { |
michael@0 | 1958 | if (!SortLexicographically(cx, &vec, n)) |
michael@0 | 1959 | return false; |
michael@0 | 1960 | } |
michael@0 | 1961 | } else { |
michael@0 | 1962 | ComparatorMatchResult comp = MatchNumericComparator(cx, fval); |
michael@0 | 1963 | if (comp == Match_Failure) |
michael@0 | 1964 | return false; |
michael@0 | 1965 | |
michael@0 | 1966 | if (comp != Match_None) { |
michael@0 | 1967 | if (allInts) { |
michael@0 | 1968 | JS_ALWAYS_TRUE(vec.resize(n * 2)); |
michael@0 | 1969 | if (!MergeSort(vec.begin(), n, vec.begin() + n, SortComparatorInt32s[comp])) |
michael@0 | 1970 | return false; |
michael@0 | 1971 | } else { |
michael@0 | 1972 | if (!SortNumerically(cx, &vec, n, comp)) |
michael@0 | 1973 | return false; |
michael@0 | 1974 | } |
michael@0 | 1975 | } else { |
michael@0 | 1976 | FastInvokeGuard fig(cx, fval); |
michael@0 | 1977 | MOZ_ASSERT(!InParallelSection(), |
michael@0 | 1978 | "Array.sort() can't currently be used from parallel code"); |
michael@0 | 1979 | JS_ALWAYS_TRUE(vec.resize(n * 2)); |
michael@0 | 1980 | if (!MergeSort(vec.begin(), n, vec.begin() + n, |
michael@0 | 1981 | SortComparatorFunction(cx, fval, fig))) |
michael@0 | 1982 | { |
michael@0 | 1983 | return false; |
michael@0 | 1984 | } |
michael@0 | 1985 | } |
michael@0 | 1986 | } |
michael@0 | 1987 | |
michael@0 | 1988 | if (!InitArrayElements(cx, obj, 0, uint32_t(n), vec.begin(), DontUpdateTypes)) |
michael@0 | 1989 | return false; |
michael@0 | 1990 | } |
michael@0 | 1991 | |
michael@0 | 1992 | /* Set undefs that sorted after the rest of elements. */ |
michael@0 | 1993 | while (undefs != 0) { |
michael@0 | 1994 | --undefs; |
michael@0 | 1995 | if (!CheckForInterrupt(cx) || !SetArrayElement(cx, obj, n++, UndefinedHandleValue)) |
michael@0 | 1996 | return false; |
michael@0 | 1997 | } |
michael@0 | 1998 | |
michael@0 | 1999 | /* Re-create any holes that sorted to the end of the array. */ |
michael@0 | 2000 | while (len > n) { |
michael@0 | 2001 | if (!CheckForInterrupt(cx) || !DeletePropertyOrThrow(cx, obj, --len)) |
michael@0 | 2002 | return false; |
michael@0 | 2003 | } |
michael@0 | 2004 | args.rval().setObject(*obj); |
michael@0 | 2005 | return true; |
michael@0 | 2006 | } |
michael@0 | 2007 | |
michael@0 | 2008 | bool |
michael@0 | 2009 | js::NewbornArrayPush(JSContext *cx, HandleObject obj, const Value &v) |
michael@0 | 2010 | { |
michael@0 | 2011 | Rooted<ArrayObject*> arr(cx, &obj->as<ArrayObject>()); |
michael@0 | 2012 | |
michael@0 | 2013 | JS_ASSERT(!v.isMagic()); |
michael@0 | 2014 | JS_ASSERT(arr->lengthIsWritable()); |
michael@0 | 2015 | |
michael@0 | 2016 | uint32_t length = arr->length(); |
michael@0 | 2017 | JS_ASSERT(length <= arr->getDenseCapacity()); |
michael@0 | 2018 | |
michael@0 | 2019 | if (!arr->ensureElements(cx, length + 1)) |
michael@0 | 2020 | return false; |
michael@0 | 2021 | |
michael@0 | 2022 | arr->setDenseInitializedLength(length + 1); |
michael@0 | 2023 | arr->setLengthInt32(length + 1); |
michael@0 | 2024 | arr->initDenseElementWithType(cx, length, v); |
michael@0 | 2025 | return true; |
michael@0 | 2026 | } |
michael@0 | 2027 | |
michael@0 | 2028 | /* ES5 15.4.4.7 */ |
michael@0 | 2029 | bool |
michael@0 | 2030 | js::array_push(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 2031 | { |
michael@0 | 2032 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 2033 | |
michael@0 | 2034 | /* Step 1. */ |
michael@0 | 2035 | RootedObject obj(cx, ToObject(cx, args.thisv())); |
michael@0 | 2036 | if (!obj) |
michael@0 | 2037 | return false; |
michael@0 | 2038 | |
michael@0 | 2039 | /* Steps 2-3. */ |
michael@0 | 2040 | uint32_t length; |
michael@0 | 2041 | if (!GetLengthProperty(cx, obj, &length)) |
michael@0 | 2042 | return false; |
michael@0 | 2043 | |
michael@0 | 2044 | /* Fast path for native objects with dense elements. */ |
michael@0 | 2045 | do { |
michael@0 | 2046 | if (!obj->isNative() || obj->is<TypedArrayObject>()) |
michael@0 | 2047 | break; |
michael@0 | 2048 | |
michael@0 | 2049 | if (obj->is<ArrayObject>() && !obj->as<ArrayObject>().lengthIsWritable()) |
michael@0 | 2050 | break; |
michael@0 | 2051 | |
michael@0 | 2052 | if (ObjectMayHaveExtraIndexedProperties(obj)) |
michael@0 | 2053 | break; |
michael@0 | 2054 | |
michael@0 | 2055 | uint32_t argCount = args.length(); |
michael@0 | 2056 | JSObject::EnsureDenseResult result = obj->ensureDenseElements(cx, length, argCount); |
michael@0 | 2057 | if (result == JSObject::ED_FAILED) |
michael@0 | 2058 | return false; |
michael@0 | 2059 | |
michael@0 | 2060 | if (result == JSObject::ED_OK) { |
michael@0 | 2061 | for (uint32_t i = 0, index = length; i < argCount; index++, i++) |
michael@0 | 2062 | obj->setDenseElementWithType(cx, index, args[i]); |
michael@0 | 2063 | uint32_t newlength = length + argCount; |
michael@0 | 2064 | args.rval().setNumber(newlength); |
michael@0 | 2065 | if (obj->is<ArrayObject>()) { |
michael@0 | 2066 | obj->as<ArrayObject>().setLengthInt32(newlength); |
michael@0 | 2067 | return true; |
michael@0 | 2068 | } |
michael@0 | 2069 | return SetLengthProperty(cx, obj, newlength); |
michael@0 | 2070 | } |
michael@0 | 2071 | |
michael@0 | 2072 | MOZ_ASSERT(result == JSObject::ED_SPARSE); |
michael@0 | 2073 | } while (false); |
michael@0 | 2074 | |
michael@0 | 2075 | /* Steps 4-5. */ |
michael@0 | 2076 | if (!InitArrayElements(cx, obj, length, args.length(), args.array(), UpdateTypes)) |
michael@0 | 2077 | return false; |
michael@0 | 2078 | |
michael@0 | 2079 | /* Steps 6-7. */ |
michael@0 | 2080 | double newlength = length + double(args.length()); |
michael@0 | 2081 | args.rval().setNumber(newlength); |
michael@0 | 2082 | return SetLengthProperty(cx, obj, newlength); |
michael@0 | 2083 | } |
michael@0 | 2084 | |
michael@0 | 2085 | /* ES6 20130308 draft 15.4.4.6. */ |
michael@0 | 2086 | bool |
michael@0 | 2087 | js::array_pop(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 2088 | { |
michael@0 | 2089 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 2090 | |
michael@0 | 2091 | /* Step 1. */ |
michael@0 | 2092 | RootedObject obj(cx, ToObject(cx, args.thisv())); |
michael@0 | 2093 | if (!obj) |
michael@0 | 2094 | return false; |
michael@0 | 2095 | |
michael@0 | 2096 | /* Steps 2-3. */ |
michael@0 | 2097 | uint32_t index; |
michael@0 | 2098 | if (!GetLengthProperty(cx, obj, &index)) |
michael@0 | 2099 | return false; |
michael@0 | 2100 | |
michael@0 | 2101 | /* Steps 4-5. */ |
michael@0 | 2102 | if (index == 0) { |
michael@0 | 2103 | /* Step 4b. */ |
michael@0 | 2104 | args.rval().setUndefined(); |
michael@0 | 2105 | } else { |
michael@0 | 2106 | /* Step 5a. */ |
michael@0 | 2107 | index--; |
michael@0 | 2108 | |
michael@0 | 2109 | /* Step 5b, 5e. */ |
michael@0 | 2110 | bool hole; |
michael@0 | 2111 | if (!GetElement(cx, obj, index, &hole, args.rval())) |
michael@0 | 2112 | return false; |
michael@0 | 2113 | |
michael@0 | 2114 | /* Step 5c. */ |
michael@0 | 2115 | if (!hole && !DeletePropertyOrThrow(cx, obj, index)) |
michael@0 | 2116 | return false; |
michael@0 | 2117 | } |
michael@0 | 2118 | |
michael@0 | 2119 | // If this was an array, then there are no elements above the one we just |
michael@0 | 2120 | // deleted (if we deleted an element). Thus we can shrink the dense |
michael@0 | 2121 | // initialized length accordingly. (This is fine even if the array length |
michael@0 | 2122 | // is non-writable: length-changing occurs after element-deletion effects.) |
michael@0 | 2123 | // Don't do anything if this isn't an array, as any deletion above has no |
michael@0 | 2124 | // effect on any elements after the "last" one indicated by the "length" |
michael@0 | 2125 | // property. |
michael@0 | 2126 | if (obj->is<ArrayObject>() && obj->getDenseInitializedLength() > index) |
michael@0 | 2127 | obj->setDenseInitializedLength(index); |
michael@0 | 2128 | |
michael@0 | 2129 | /* Steps 4a, 5d. */ |
michael@0 | 2130 | return SetLengthProperty(cx, obj, index); |
michael@0 | 2131 | } |
michael@0 | 2132 | |
michael@0 | 2133 | void |
michael@0 | 2134 | js::ArrayShiftMoveElements(JSObject *obj) |
michael@0 | 2135 | { |
michael@0 | 2136 | JS_ASSERT(obj->is<ArrayObject>()); |
michael@0 | 2137 | JS_ASSERT(obj->as<ArrayObject>().lengthIsWritable()); |
michael@0 | 2138 | |
michael@0 | 2139 | /* |
michael@0 | 2140 | * At this point the length and initialized length have already been |
michael@0 | 2141 | * decremented and the result fetched, so just shift the array elements |
michael@0 | 2142 | * themselves. |
michael@0 | 2143 | */ |
michael@0 | 2144 | uint32_t initlen = obj->getDenseInitializedLength(); |
michael@0 | 2145 | obj->moveDenseElementsNoPreBarrier(0, 1, initlen); |
michael@0 | 2146 | } |
michael@0 | 2147 | |
michael@0 | 2148 | /* ES5 15.4.4.9 */ |
michael@0 | 2149 | bool |
michael@0 | 2150 | js::array_shift(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 2151 | { |
michael@0 | 2152 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 2153 | |
michael@0 | 2154 | /* Step 1. */ |
michael@0 | 2155 | RootedObject obj(cx, ToObject(cx, args.thisv())); |
michael@0 | 2156 | if (!obj) |
michael@0 | 2157 | return false; |
michael@0 | 2158 | |
michael@0 | 2159 | /* Steps 2-3. */ |
michael@0 | 2160 | uint32_t len; |
michael@0 | 2161 | if (!GetLengthProperty(cx, obj, &len)) |
michael@0 | 2162 | return false; |
michael@0 | 2163 | |
michael@0 | 2164 | /* Step 4. */ |
michael@0 | 2165 | if (len == 0) { |
michael@0 | 2166 | /* Step 4a. */ |
michael@0 | 2167 | if (!SetLengthProperty(cx, obj, 0)) |
michael@0 | 2168 | return false; |
michael@0 | 2169 | |
michael@0 | 2170 | /* Step 4b. */ |
michael@0 | 2171 | args.rval().setUndefined(); |
michael@0 | 2172 | return true; |
michael@0 | 2173 | } |
michael@0 | 2174 | |
michael@0 | 2175 | uint32_t newlen = len - 1; |
michael@0 | 2176 | |
michael@0 | 2177 | /* Fast paths. */ |
michael@0 | 2178 | if (obj->is<ArrayObject>() && |
michael@0 | 2179 | obj->getDenseInitializedLength() > 0 && |
michael@0 | 2180 | newlen < obj->getDenseCapacity() && |
michael@0 | 2181 | !ObjectMayHaveExtraIndexedProperties(obj)) |
michael@0 | 2182 | { |
michael@0 | 2183 | args.rval().set(obj->getDenseElement(0)); |
michael@0 | 2184 | if (args.rval().isMagic(JS_ELEMENTS_HOLE)) |
michael@0 | 2185 | args.rval().setUndefined(); |
michael@0 | 2186 | |
michael@0 | 2187 | obj->moveDenseElements(0, 1, obj->getDenseInitializedLength() - 1); |
michael@0 | 2188 | obj->setDenseInitializedLength(obj->getDenseInitializedLength() - 1); |
michael@0 | 2189 | |
michael@0 | 2190 | if (!SetLengthProperty(cx, obj, newlen)) |
michael@0 | 2191 | return false; |
michael@0 | 2192 | |
michael@0 | 2193 | return js_SuppressDeletedProperty(cx, obj, INT_TO_JSID(newlen)); |
michael@0 | 2194 | } |
michael@0 | 2195 | |
michael@0 | 2196 | /* Steps 5, 10. */ |
michael@0 | 2197 | bool hole; |
michael@0 | 2198 | if (!GetElement(cx, obj, uint32_t(0), &hole, args.rval())) |
michael@0 | 2199 | return false; |
michael@0 | 2200 | |
michael@0 | 2201 | /* Steps 6-7. */ |
michael@0 | 2202 | RootedValue value(cx); |
michael@0 | 2203 | for (uint32_t i = 0; i < newlen; i++) { |
michael@0 | 2204 | if (!CheckForInterrupt(cx)) |
michael@0 | 2205 | return false; |
michael@0 | 2206 | if (!GetElement(cx, obj, i + 1, &hole, &value)) |
michael@0 | 2207 | return false; |
michael@0 | 2208 | if (hole) { |
michael@0 | 2209 | if (!DeletePropertyOrThrow(cx, obj, i)) |
michael@0 | 2210 | return false; |
michael@0 | 2211 | } else { |
michael@0 | 2212 | if (!SetArrayElement(cx, obj, i, value)) |
michael@0 | 2213 | return false; |
michael@0 | 2214 | } |
michael@0 | 2215 | } |
michael@0 | 2216 | |
michael@0 | 2217 | /* Step 8. */ |
michael@0 | 2218 | if (!DeletePropertyOrThrow(cx, obj, newlen)) |
michael@0 | 2219 | return false; |
michael@0 | 2220 | |
michael@0 | 2221 | /* Step 9. */ |
michael@0 | 2222 | return SetLengthProperty(cx, obj, newlen); |
michael@0 | 2223 | } |
michael@0 | 2224 | |
michael@0 | 2225 | static bool |
michael@0 | 2226 | array_unshift(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 2227 | { |
michael@0 | 2228 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 2229 | RootedObject obj(cx, ToObject(cx, args.thisv())); |
michael@0 | 2230 | if (!obj) |
michael@0 | 2231 | return false; |
michael@0 | 2232 | |
michael@0 | 2233 | uint32_t length; |
michael@0 | 2234 | if (!GetLengthProperty(cx, obj, &length)) |
michael@0 | 2235 | return false; |
michael@0 | 2236 | |
michael@0 | 2237 | double newlen = length; |
michael@0 | 2238 | if (args.length() > 0) { |
michael@0 | 2239 | /* Slide up the array to make room for all args at the bottom. */ |
michael@0 | 2240 | if (length > 0) { |
michael@0 | 2241 | bool optimized = false; |
michael@0 | 2242 | do { |
michael@0 | 2243 | if (!obj->is<ArrayObject>()) |
michael@0 | 2244 | break; |
michael@0 | 2245 | if (ObjectMayHaveExtraIndexedProperties(obj)) |
michael@0 | 2246 | break; |
michael@0 | 2247 | if (!obj->as<ArrayObject>().lengthIsWritable()) |
michael@0 | 2248 | break; |
michael@0 | 2249 | JSObject::EnsureDenseResult result = obj->ensureDenseElements(cx, length, args.length()); |
michael@0 | 2250 | if (result != JSObject::ED_OK) { |
michael@0 | 2251 | if (result == JSObject::ED_FAILED) |
michael@0 | 2252 | return false; |
michael@0 | 2253 | JS_ASSERT(result == JSObject::ED_SPARSE); |
michael@0 | 2254 | break; |
michael@0 | 2255 | } |
michael@0 | 2256 | obj->moveDenseElements(args.length(), 0, length); |
michael@0 | 2257 | for (uint32_t i = 0; i < args.length(); i++) |
michael@0 | 2258 | obj->setDenseElement(i, MagicValue(JS_ELEMENTS_HOLE)); |
michael@0 | 2259 | optimized = true; |
michael@0 | 2260 | } while (false); |
michael@0 | 2261 | |
michael@0 | 2262 | if (!optimized) { |
michael@0 | 2263 | double last = length; |
michael@0 | 2264 | double upperIndex = last + args.length(); |
michael@0 | 2265 | RootedValue value(cx); |
michael@0 | 2266 | do { |
michael@0 | 2267 | --last, --upperIndex; |
michael@0 | 2268 | bool hole; |
michael@0 | 2269 | if (!CheckForInterrupt(cx)) |
michael@0 | 2270 | return false; |
michael@0 | 2271 | if (!GetElement(cx, obj, last, &hole, &value)) |
michael@0 | 2272 | return false; |
michael@0 | 2273 | if (hole) { |
michael@0 | 2274 | if (!DeletePropertyOrThrow(cx, obj, upperIndex)) |
michael@0 | 2275 | return false; |
michael@0 | 2276 | } else { |
michael@0 | 2277 | if (!SetArrayElement(cx, obj, upperIndex, value)) |
michael@0 | 2278 | return false; |
michael@0 | 2279 | } |
michael@0 | 2280 | } while (last != 0); |
michael@0 | 2281 | } |
michael@0 | 2282 | } |
michael@0 | 2283 | |
michael@0 | 2284 | /* Copy from args to the bottom of the array. */ |
michael@0 | 2285 | if (!InitArrayElements(cx, obj, 0, args.length(), args.array(), UpdateTypes)) |
michael@0 | 2286 | return false; |
michael@0 | 2287 | |
michael@0 | 2288 | newlen += args.length(); |
michael@0 | 2289 | } |
michael@0 | 2290 | if (!SetLengthProperty(cx, obj, newlen)) |
michael@0 | 2291 | return false; |
michael@0 | 2292 | |
michael@0 | 2293 | /* Follow Perl by returning the new array length. */ |
michael@0 | 2294 | args.rval().setNumber(newlen); |
michael@0 | 2295 | return true; |
michael@0 | 2296 | } |
michael@0 | 2297 | |
michael@0 | 2298 | static inline void |
michael@0 | 2299 | TryReuseArrayType(JSObject *obj, ArrayObject *narr) |
michael@0 | 2300 | { |
michael@0 | 2301 | /* |
michael@0 | 2302 | * Try to change the type of a newly created array narr to the same type |
michael@0 | 2303 | * as obj. This can only be performed if the original object is an array |
michael@0 | 2304 | * and has the same prototype. |
michael@0 | 2305 | */ |
michael@0 | 2306 | JS_ASSERT(narr->getProto()->hasNewType(&ArrayObject::class_, narr->type())); |
michael@0 | 2307 | |
michael@0 | 2308 | if (obj->is<ArrayObject>() && !obj->hasSingletonType() && obj->getProto() == narr->getProto()) |
michael@0 | 2309 | narr->setType(obj->type()); |
michael@0 | 2310 | } |
michael@0 | 2311 | |
michael@0 | 2312 | /* |
michael@0 | 2313 | * Returns true if this is a dense array whose |count| properties starting from |
michael@0 | 2314 | * |startingIndex| may be accessed (get, set, delete) directly through its |
michael@0 | 2315 | * contiguous vector of elements without fear of getters, setters, etc. along |
michael@0 | 2316 | * the prototype chain, or of enumerators requiring notification of |
michael@0 | 2317 | * modifications. |
michael@0 | 2318 | */ |
michael@0 | 2319 | static inline bool |
michael@0 | 2320 | CanOptimizeForDenseStorage(HandleObject arr, uint32_t startingIndex, uint32_t count, JSContext *cx) |
michael@0 | 2321 | { |
michael@0 | 2322 | /* If the desired properties overflow dense storage, we can't optimize. */ |
michael@0 | 2323 | if (UINT32_MAX - startingIndex < count) |
michael@0 | 2324 | return false; |
michael@0 | 2325 | |
michael@0 | 2326 | /* There's no optimizing possible if it's not an array. */ |
michael@0 | 2327 | if (!arr->is<ArrayObject>()) |
michael@0 | 2328 | return false; |
michael@0 | 2329 | |
michael@0 | 2330 | /* |
michael@0 | 2331 | * Don't optimize if the array might be in the midst of iteration. We |
michael@0 | 2332 | * rely on this to be able to safely move dense array elements around with |
michael@0 | 2333 | * just a memmove (see JSObject::moveDenseArrayElements), without worrying |
michael@0 | 2334 | * about updating any in-progress enumerators for properties implicitly |
michael@0 | 2335 | * deleted if a hole is moved from one location to another location not yet |
michael@0 | 2336 | * visited. See bug 690622. |
michael@0 | 2337 | * |
michael@0 | 2338 | * Another potential wrinkle: what if the enumeration is happening on an |
michael@0 | 2339 | * object which merely has |arr| on its prototype chain? It turns out this |
michael@0 | 2340 | * case can't happen, because any dense array used as the prototype of |
michael@0 | 2341 | * another object is first slowified, for type inference's sake. |
michael@0 | 2342 | */ |
michael@0 | 2343 | types::TypeObject *arrType = arr->getType(cx); |
michael@0 | 2344 | if (MOZ_UNLIKELY(!arrType || arrType->hasAllFlags(OBJECT_FLAG_ITERATED))) |
michael@0 | 2345 | return false; |
michael@0 | 2346 | |
michael@0 | 2347 | /* |
michael@0 | 2348 | * Now watch out for getters and setters along the prototype chain or in |
michael@0 | 2349 | * other indexed properties on the object. (Note that non-writable length |
michael@0 | 2350 | * is subsumed by the initializedLength comparison.) |
michael@0 | 2351 | */ |
michael@0 | 2352 | return !ObjectMayHaveExtraIndexedProperties(arr) && |
michael@0 | 2353 | startingIndex + count <= arr->getDenseInitializedLength(); |
michael@0 | 2354 | } |
michael@0 | 2355 | |
michael@0 | 2356 | /* ES5 15.4.4.12. */ |
michael@0 | 2357 | bool |
michael@0 | 2358 | js::array_splice(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 2359 | { |
michael@0 | 2360 | return array_splice_impl(cx, argc, vp, true); |
michael@0 | 2361 | } |
michael@0 | 2362 | |
michael@0 | 2363 | bool |
michael@0 | 2364 | js::array_splice_impl(JSContext *cx, unsigned argc, Value *vp, bool returnValueIsUsed) |
michael@0 | 2365 | { |
michael@0 | 2366 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 2367 | |
michael@0 | 2368 | /* Step 1. */ |
michael@0 | 2369 | RootedObject obj(cx, ToObject(cx, args.thisv())); |
michael@0 | 2370 | if (!obj) |
michael@0 | 2371 | return false; |
michael@0 | 2372 | |
michael@0 | 2373 | /* Steps 3-4. */ |
michael@0 | 2374 | uint32_t len; |
michael@0 | 2375 | if (!GetLengthProperty(cx, obj, &len)) |
michael@0 | 2376 | return false; |
michael@0 | 2377 | |
michael@0 | 2378 | /* Step 5. */ |
michael@0 | 2379 | double relativeStart; |
michael@0 | 2380 | if (!ToInteger(cx, args.get(0), &relativeStart)) |
michael@0 | 2381 | return false; |
michael@0 | 2382 | |
michael@0 | 2383 | /* Step 6. */ |
michael@0 | 2384 | uint32_t actualStart; |
michael@0 | 2385 | if (relativeStart < 0) |
michael@0 | 2386 | actualStart = Max(len + relativeStart, 0.0); |
michael@0 | 2387 | else |
michael@0 | 2388 | actualStart = Min(relativeStart, double(len)); |
michael@0 | 2389 | |
michael@0 | 2390 | /* Step 7. */ |
michael@0 | 2391 | uint32_t actualDeleteCount; |
michael@0 | 2392 | if (args.length() != 1) { |
michael@0 | 2393 | double deleteCountDouble; |
michael@0 | 2394 | RootedValue cnt(cx, args.length() >= 2 ? args[1] : Int32Value(0)); |
michael@0 | 2395 | if (!ToInteger(cx, cnt, &deleteCountDouble)) |
michael@0 | 2396 | return false; |
michael@0 | 2397 | actualDeleteCount = Min(Max(deleteCountDouble, 0.0), double(len - actualStart)); |
michael@0 | 2398 | } else { |
michael@0 | 2399 | /* |
michael@0 | 2400 | * Non-standard: if start was specified but deleteCount was omitted, |
michael@0 | 2401 | * delete to the end of the array. See bug 668024 for discussion. |
michael@0 | 2402 | */ |
michael@0 | 2403 | actualDeleteCount = len - actualStart; |
michael@0 | 2404 | } |
michael@0 | 2405 | |
michael@0 | 2406 | JS_ASSERT(len - actualStart >= actualDeleteCount); |
michael@0 | 2407 | |
michael@0 | 2408 | /* Steps 2, 8-9. */ |
michael@0 | 2409 | Rooted<ArrayObject*> arr(cx); |
michael@0 | 2410 | if (CanOptimizeForDenseStorage(obj, actualStart, actualDeleteCount, cx)) { |
michael@0 | 2411 | if (returnValueIsUsed) { |
michael@0 | 2412 | arr = NewDenseCopiedArray(cx, actualDeleteCount, obj, actualStart); |
michael@0 | 2413 | if (!arr) |
michael@0 | 2414 | return false; |
michael@0 | 2415 | TryReuseArrayType(obj, arr); |
michael@0 | 2416 | } |
michael@0 | 2417 | } else { |
michael@0 | 2418 | arr = NewDenseAllocatedArray(cx, actualDeleteCount); |
michael@0 | 2419 | if (!arr) |
michael@0 | 2420 | return false; |
michael@0 | 2421 | TryReuseArrayType(obj, arr); |
michael@0 | 2422 | |
michael@0 | 2423 | RootedValue fromValue(cx); |
michael@0 | 2424 | for (uint32_t k = 0; k < actualDeleteCount; k++) { |
michael@0 | 2425 | bool hole; |
michael@0 | 2426 | if (!CheckForInterrupt(cx) || |
michael@0 | 2427 | !GetElement(cx, obj, actualStart + k, &hole, &fromValue) || |
michael@0 | 2428 | (!hole && !JSObject::defineElement(cx, arr, k, fromValue))) |
michael@0 | 2429 | { |
michael@0 | 2430 | return false; |
michael@0 | 2431 | } |
michael@0 | 2432 | } |
michael@0 | 2433 | } |
michael@0 | 2434 | |
michael@0 | 2435 | /* Step 11. */ |
michael@0 | 2436 | uint32_t itemCount = (args.length() >= 2) ? (args.length() - 2) : 0; |
michael@0 | 2437 | |
michael@0 | 2438 | if (itemCount < actualDeleteCount) { |
michael@0 | 2439 | /* Step 12: the array is being shrunk. */ |
michael@0 | 2440 | uint32_t sourceIndex = actualStart + actualDeleteCount; |
michael@0 | 2441 | uint32_t targetIndex = actualStart + itemCount; |
michael@0 | 2442 | uint32_t finalLength = len - actualDeleteCount + itemCount; |
michael@0 | 2443 | |
michael@0 | 2444 | if (CanOptimizeForDenseStorage(obj, 0, len, cx)) { |
michael@0 | 2445 | /* Steps 12(a)-(b). */ |
michael@0 | 2446 | obj->moveDenseElements(targetIndex, sourceIndex, len - sourceIndex); |
michael@0 | 2447 | |
michael@0 | 2448 | /* |
michael@0 | 2449 | * Update the initialized length. Do so before shrinking so that we |
michael@0 | 2450 | * can apply the write barrier to the old slots. |
michael@0 | 2451 | */ |
michael@0 | 2452 | obj->setDenseInitializedLength(finalLength); |
michael@0 | 2453 | |
michael@0 | 2454 | /* Steps 12(c)-(d). */ |
michael@0 | 2455 | obj->shrinkElements(cx, finalLength); |
michael@0 | 2456 | } else { |
michael@0 | 2457 | /* |
michael@0 | 2458 | * This is all very slow if the length is very large. We don't yet |
michael@0 | 2459 | * have the ability to iterate in sorted order, so we just do the |
michael@0 | 2460 | * pessimistic thing and let CheckForInterrupt handle the |
michael@0 | 2461 | * fallout. |
michael@0 | 2462 | */ |
michael@0 | 2463 | |
michael@0 | 2464 | /* Steps 12(a)-(b). */ |
michael@0 | 2465 | RootedValue fromValue(cx); |
michael@0 | 2466 | for (uint32_t from = sourceIndex, to = targetIndex; from < len; from++, to++) { |
michael@0 | 2467 | if (!CheckForInterrupt(cx)) |
michael@0 | 2468 | return false; |
michael@0 | 2469 | |
michael@0 | 2470 | bool hole; |
michael@0 | 2471 | if (!GetElement(cx, obj, from, &hole, &fromValue)) |
michael@0 | 2472 | return false; |
michael@0 | 2473 | if (hole) { |
michael@0 | 2474 | if (!DeletePropertyOrThrow(cx, obj, to)) |
michael@0 | 2475 | return false; |
michael@0 | 2476 | } else { |
michael@0 | 2477 | if (!SetArrayElement(cx, obj, to, fromValue)) |
michael@0 | 2478 | return false; |
michael@0 | 2479 | } |
michael@0 | 2480 | } |
michael@0 | 2481 | |
michael@0 | 2482 | /* Steps 12(c)-(d). */ |
michael@0 | 2483 | for (uint32_t k = len; k > finalLength; k--) { |
michael@0 | 2484 | if (!DeletePropertyOrThrow(cx, obj, k - 1)) |
michael@0 | 2485 | return false; |
michael@0 | 2486 | } |
michael@0 | 2487 | } |
michael@0 | 2488 | } else if (itemCount > actualDeleteCount) { |
michael@0 | 2489 | /* Step 13. */ |
michael@0 | 2490 | |
michael@0 | 2491 | /* |
michael@0 | 2492 | * Optimize only if the array is already dense and we can extend it to |
michael@0 | 2493 | * its new length. It would be wrong to extend the elements here for a |
michael@0 | 2494 | * number of reasons. |
michael@0 | 2495 | * |
michael@0 | 2496 | * First, this could cause us to fall into the fast-path below. This |
michael@0 | 2497 | * would cause elements to be moved into places past the non-writable |
michael@0 | 2498 | * length. And when the dense initialized length is updated, that'll |
michael@0 | 2499 | * cause the |in| operator to think that those elements actually exist, |
michael@0 | 2500 | * even though, properly, setting them must fail. |
michael@0 | 2501 | * |
michael@0 | 2502 | * Second, extending the elements here will trigger assertions inside |
michael@0 | 2503 | * ensureDenseElements that the elements aren't being extended past the |
michael@0 | 2504 | * length of a non-writable array. This is because extending elements |
michael@0 | 2505 | * will extend capacity -- which might extend them past a non-writable |
michael@0 | 2506 | * length, violating the |capacity <= length| invariant for such |
michael@0 | 2507 | * arrays. And that would make the various JITted fast-path method |
michael@0 | 2508 | * implementations of [].push, [].unshift, and so on wrong. |
michael@0 | 2509 | * |
michael@0 | 2510 | * If the array length is non-writable, this method *will* throw. For |
michael@0 | 2511 | * simplicity, have the slow-path code do it. (Also note that the slow |
michael@0 | 2512 | * path may validly *not* throw -- if all the elements being moved are |
michael@0 | 2513 | * holes.) |
michael@0 | 2514 | */ |
michael@0 | 2515 | if (obj->is<ArrayObject>()) { |
michael@0 | 2516 | Rooted<ArrayObject*> arr(cx, &obj->as<ArrayObject>()); |
michael@0 | 2517 | if (arr->lengthIsWritable()) { |
michael@0 | 2518 | JSObject::EnsureDenseResult res = |
michael@0 | 2519 | arr->ensureDenseElements(cx, arr->length(), itemCount - actualDeleteCount); |
michael@0 | 2520 | if (res == JSObject::ED_FAILED) |
michael@0 | 2521 | return false; |
michael@0 | 2522 | } |
michael@0 | 2523 | } |
michael@0 | 2524 | |
michael@0 | 2525 | if (CanOptimizeForDenseStorage(obj, len, itemCount - actualDeleteCount, cx)) { |
michael@0 | 2526 | obj->moveDenseElements(actualStart + itemCount, |
michael@0 | 2527 | actualStart + actualDeleteCount, |
michael@0 | 2528 | len - (actualStart + actualDeleteCount)); |
michael@0 | 2529 | obj->setDenseInitializedLength(len + itemCount - actualDeleteCount); |
michael@0 | 2530 | } else { |
michael@0 | 2531 | RootedValue fromValue(cx); |
michael@0 | 2532 | for (double k = len - actualDeleteCount; k > actualStart; k--) { |
michael@0 | 2533 | if (!CheckForInterrupt(cx)) |
michael@0 | 2534 | return false; |
michael@0 | 2535 | |
michael@0 | 2536 | double from = k + actualDeleteCount - 1; |
michael@0 | 2537 | double to = k + itemCount - 1; |
michael@0 | 2538 | |
michael@0 | 2539 | bool hole; |
michael@0 | 2540 | if (!GetElement(cx, obj, from, &hole, &fromValue)) |
michael@0 | 2541 | return false; |
michael@0 | 2542 | |
michael@0 | 2543 | if (hole) { |
michael@0 | 2544 | if (!DeletePropertyOrThrow(cx, obj, to)) |
michael@0 | 2545 | return false; |
michael@0 | 2546 | } else { |
michael@0 | 2547 | if (!SetArrayElement(cx, obj, to, fromValue)) |
michael@0 | 2548 | return false; |
michael@0 | 2549 | } |
michael@0 | 2550 | } |
michael@0 | 2551 | } |
michael@0 | 2552 | } |
michael@0 | 2553 | |
michael@0 | 2554 | /* Step 10. */ |
michael@0 | 2555 | Value *items = args.array() + 2; |
michael@0 | 2556 | |
michael@0 | 2557 | /* Steps 14-15. */ |
michael@0 | 2558 | for (uint32_t k = actualStart, i = 0; i < itemCount; i++, k++) { |
michael@0 | 2559 | if (!SetArrayElement(cx, obj, k, HandleValue::fromMarkedLocation(&items[i]))) |
michael@0 | 2560 | return false; |
michael@0 | 2561 | } |
michael@0 | 2562 | |
michael@0 | 2563 | /* Step 16. */ |
michael@0 | 2564 | double finalLength = double(len) - actualDeleteCount + itemCount; |
michael@0 | 2565 | if (!SetLengthProperty(cx, obj, finalLength)) |
michael@0 | 2566 | return false; |
michael@0 | 2567 | |
michael@0 | 2568 | /* Step 17. */ |
michael@0 | 2569 | if (returnValueIsUsed) |
michael@0 | 2570 | args.rval().setObject(*arr); |
michael@0 | 2571 | |
michael@0 | 2572 | return true; |
michael@0 | 2573 | } |
michael@0 | 2574 | |
michael@0 | 2575 | #ifdef JS_ION |
michael@0 | 2576 | bool |
michael@0 | 2577 | js::array_concat_dense(JSContext *cx, Handle<ArrayObject*> arr1, Handle<ArrayObject*> arr2, |
michael@0 | 2578 | Handle<ArrayObject*> result) |
michael@0 | 2579 | { |
michael@0 | 2580 | uint32_t initlen1 = arr1->getDenseInitializedLength(); |
michael@0 | 2581 | JS_ASSERT(initlen1 == arr1->length()); |
michael@0 | 2582 | |
michael@0 | 2583 | uint32_t initlen2 = arr2->getDenseInitializedLength(); |
michael@0 | 2584 | JS_ASSERT(initlen2 == arr2->length()); |
michael@0 | 2585 | |
michael@0 | 2586 | /* No overflow here due to nelements limit. */ |
michael@0 | 2587 | uint32_t len = initlen1 + initlen2; |
michael@0 | 2588 | |
michael@0 | 2589 | if (!result->ensureElements(cx, len)) |
michael@0 | 2590 | return false; |
michael@0 | 2591 | |
michael@0 | 2592 | JS_ASSERT(!result->getDenseInitializedLength()); |
michael@0 | 2593 | result->setDenseInitializedLength(len); |
michael@0 | 2594 | |
michael@0 | 2595 | result->initDenseElements(0, arr1->getDenseElements(), initlen1); |
michael@0 | 2596 | result->initDenseElements(initlen1, arr2->getDenseElements(), initlen2); |
michael@0 | 2597 | result->setLengthInt32(len); |
michael@0 | 2598 | return true; |
michael@0 | 2599 | } |
michael@0 | 2600 | #endif /* JS_ION */ |
michael@0 | 2601 | |
michael@0 | 2602 | /* |
michael@0 | 2603 | * Python-esque sequence operations. |
michael@0 | 2604 | */ |
michael@0 | 2605 | bool |
michael@0 | 2606 | js::array_concat(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 2607 | { |
michael@0 | 2608 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 2609 | |
michael@0 | 2610 | /* Treat our |this| object as the first argument; see ECMA 15.4.4.4. */ |
michael@0 | 2611 | Value *p = args.array() - 1; |
michael@0 | 2612 | |
michael@0 | 2613 | /* Create a new Array object and root it using *vp. */ |
michael@0 | 2614 | RootedObject aobj(cx, ToObject(cx, args.thisv())); |
michael@0 | 2615 | if (!aobj) |
michael@0 | 2616 | return false; |
michael@0 | 2617 | |
michael@0 | 2618 | Rooted<ArrayObject*> narr(cx); |
michael@0 | 2619 | uint32_t length; |
michael@0 | 2620 | if (aobj->is<ArrayObject>() && !aobj->isIndexed()) { |
michael@0 | 2621 | length = aobj->as<ArrayObject>().length(); |
michael@0 | 2622 | uint32_t initlen = aobj->getDenseInitializedLength(); |
michael@0 | 2623 | narr = NewDenseCopiedArray(cx, initlen, aobj, 0); |
michael@0 | 2624 | if (!narr) |
michael@0 | 2625 | return false; |
michael@0 | 2626 | TryReuseArrayType(aobj, narr); |
michael@0 | 2627 | narr->setLength(cx, length); |
michael@0 | 2628 | args.rval().setObject(*narr); |
michael@0 | 2629 | if (argc == 0) |
michael@0 | 2630 | return true; |
michael@0 | 2631 | argc--; |
michael@0 | 2632 | p++; |
michael@0 | 2633 | } else { |
michael@0 | 2634 | narr = NewDenseEmptyArray(cx); |
michael@0 | 2635 | if (!narr) |
michael@0 | 2636 | return false; |
michael@0 | 2637 | args.rval().setObject(*narr); |
michael@0 | 2638 | length = 0; |
michael@0 | 2639 | } |
michael@0 | 2640 | |
michael@0 | 2641 | /* Loop over [0, argc] to concat args into narr, expanding all Arrays. */ |
michael@0 | 2642 | for (unsigned i = 0; i <= argc; i++) { |
michael@0 | 2643 | if (!CheckForInterrupt(cx)) |
michael@0 | 2644 | return false; |
michael@0 | 2645 | HandleValue v = HandleValue::fromMarkedLocation(&p[i]); |
michael@0 | 2646 | if (v.isObject()) { |
michael@0 | 2647 | RootedObject obj(cx, &v.toObject()); |
michael@0 | 2648 | if (ObjectClassIs(obj, ESClass_Array, cx)) { |
michael@0 | 2649 | uint32_t alength; |
michael@0 | 2650 | if (!GetLengthProperty(cx, obj, &alength)) |
michael@0 | 2651 | return false; |
michael@0 | 2652 | RootedValue tmp(cx); |
michael@0 | 2653 | for (uint32_t slot = 0; slot < alength; slot++) { |
michael@0 | 2654 | bool hole; |
michael@0 | 2655 | if (!CheckForInterrupt(cx) || !GetElement(cx, obj, slot, &hole, &tmp)) |
michael@0 | 2656 | return false; |
michael@0 | 2657 | |
michael@0 | 2658 | /* |
michael@0 | 2659 | * Per ECMA 262, 15.4.4.4, step 9, ignore nonexistent |
michael@0 | 2660 | * properties. |
michael@0 | 2661 | */ |
michael@0 | 2662 | if (!hole && !SetArrayElement(cx, narr, length + slot, tmp)) |
michael@0 | 2663 | return false; |
michael@0 | 2664 | } |
michael@0 | 2665 | length += alength; |
michael@0 | 2666 | continue; |
michael@0 | 2667 | } |
michael@0 | 2668 | } |
michael@0 | 2669 | |
michael@0 | 2670 | if (!SetArrayElement(cx, narr, length, v)) |
michael@0 | 2671 | return false; |
michael@0 | 2672 | length++; |
michael@0 | 2673 | } |
michael@0 | 2674 | |
michael@0 | 2675 | return SetLengthProperty(cx, narr, length); |
michael@0 | 2676 | } |
michael@0 | 2677 | |
michael@0 | 2678 | static bool |
michael@0 | 2679 | array_slice(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 2680 | { |
michael@0 | 2681 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 2682 | |
michael@0 | 2683 | RootedObject obj(cx, ToObject(cx, args.thisv())); |
michael@0 | 2684 | if (!obj) |
michael@0 | 2685 | return false; |
michael@0 | 2686 | |
michael@0 | 2687 | uint32_t length; |
michael@0 | 2688 | if (!GetLengthProperty(cx, obj, &length)) |
michael@0 | 2689 | return false; |
michael@0 | 2690 | |
michael@0 | 2691 | uint32_t begin = 0; |
michael@0 | 2692 | uint32_t end = length; |
michael@0 | 2693 | if (args.length() > 0) { |
michael@0 | 2694 | double d; |
michael@0 | 2695 | if (!ToInteger(cx, args[0], &d)) |
michael@0 | 2696 | return false; |
michael@0 | 2697 | if (d < 0) { |
michael@0 | 2698 | d += length; |
michael@0 | 2699 | if (d < 0) |
michael@0 | 2700 | d = 0; |
michael@0 | 2701 | } else if (d > length) { |
michael@0 | 2702 | d = length; |
michael@0 | 2703 | } |
michael@0 | 2704 | begin = (uint32_t)d; |
michael@0 | 2705 | |
michael@0 | 2706 | if (args.hasDefined(1)) { |
michael@0 | 2707 | if (!ToInteger(cx, args[1], &d)) |
michael@0 | 2708 | return false; |
michael@0 | 2709 | if (d < 0) { |
michael@0 | 2710 | d += length; |
michael@0 | 2711 | if (d < 0) |
michael@0 | 2712 | d = 0; |
michael@0 | 2713 | } else if (d > length) { |
michael@0 | 2714 | d = length; |
michael@0 | 2715 | } |
michael@0 | 2716 | end = (uint32_t)d; |
michael@0 | 2717 | } |
michael@0 | 2718 | } |
michael@0 | 2719 | |
michael@0 | 2720 | if (begin > end) |
michael@0 | 2721 | begin = end; |
michael@0 | 2722 | |
michael@0 | 2723 | Rooted<ArrayObject*> narr(cx); |
michael@0 | 2724 | narr = NewDenseAllocatedArray(cx, end - begin); |
michael@0 | 2725 | if (!narr) |
michael@0 | 2726 | return false; |
michael@0 | 2727 | TryReuseArrayType(obj, narr); |
michael@0 | 2728 | |
michael@0 | 2729 | if (obj->is<ArrayObject>() && !ObjectMayHaveExtraIndexedProperties(obj)) { |
michael@0 | 2730 | if (obj->getDenseInitializedLength() > begin) { |
michael@0 | 2731 | uint32_t numSourceElements = obj->getDenseInitializedLength() - begin; |
michael@0 | 2732 | uint32_t initLength = Min(numSourceElements, end - begin); |
michael@0 | 2733 | narr->setDenseInitializedLength(initLength); |
michael@0 | 2734 | narr->initDenseElements(0, &obj->getDenseElement(begin), initLength); |
michael@0 | 2735 | } |
michael@0 | 2736 | args.rval().setObject(*narr); |
michael@0 | 2737 | return true; |
michael@0 | 2738 | } |
michael@0 | 2739 | |
michael@0 | 2740 | if (js::SliceOp op = obj->getOps()->slice) { |
michael@0 | 2741 | // Ensure that we have dense elements, so that DOM can use js::UnsafeDefineElement. |
michael@0 | 2742 | JSObject::EnsureDenseResult result = narr->ensureDenseElements(cx, 0, end - begin); |
michael@0 | 2743 | if (result == JSObject::ED_FAILED) |
michael@0 | 2744 | return false; |
michael@0 | 2745 | |
michael@0 | 2746 | if (result == JSObject::ED_OK) { |
michael@0 | 2747 | if (!op(cx, obj, begin, end, narr)) |
michael@0 | 2748 | return false; |
michael@0 | 2749 | |
michael@0 | 2750 | args.rval().setObject(*narr); |
michael@0 | 2751 | return true; |
michael@0 | 2752 | } |
michael@0 | 2753 | |
michael@0 | 2754 | // Fallthrough |
michael@0 | 2755 | JS_ASSERT(result == JSObject::ED_SPARSE); |
michael@0 | 2756 | } |
michael@0 | 2757 | |
michael@0 | 2758 | |
michael@0 | 2759 | if (!SliceSlowly(cx, obj, obj, begin, end, narr)) |
michael@0 | 2760 | return false; |
michael@0 | 2761 | |
michael@0 | 2762 | args.rval().setObject(*narr); |
michael@0 | 2763 | return true; |
michael@0 | 2764 | } |
michael@0 | 2765 | |
michael@0 | 2766 | JS_FRIEND_API(bool) |
michael@0 | 2767 | js::SliceSlowly(JSContext* cx, HandleObject obj, HandleObject receiver, |
michael@0 | 2768 | uint32_t begin, uint32_t end, HandleObject result) |
michael@0 | 2769 | { |
michael@0 | 2770 | RootedValue value(cx); |
michael@0 | 2771 | for (uint32_t slot = begin; slot < end; slot++) { |
michael@0 | 2772 | bool hole; |
michael@0 | 2773 | if (!CheckForInterrupt(cx) || |
michael@0 | 2774 | !GetElement(cx, obj, receiver, slot, &hole, &value)) |
michael@0 | 2775 | { |
michael@0 | 2776 | return false; |
michael@0 | 2777 | } |
michael@0 | 2778 | if (!hole && !JSObject::defineElement(cx, result, slot - begin, value)) |
michael@0 | 2779 | return false; |
michael@0 | 2780 | } |
michael@0 | 2781 | return true; |
michael@0 | 2782 | } |
michael@0 | 2783 | |
michael@0 | 2784 | /* ES5 15.4.4.20. */ |
michael@0 | 2785 | static bool |
michael@0 | 2786 | array_filter(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 2787 | { |
michael@0 | 2788 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 2789 | |
michael@0 | 2790 | /* Step 1. */ |
michael@0 | 2791 | RootedObject obj(cx, ToObject(cx, args.thisv())); |
michael@0 | 2792 | if (!obj) |
michael@0 | 2793 | return false; |
michael@0 | 2794 | |
michael@0 | 2795 | /* Step 2-3. */ |
michael@0 | 2796 | uint32_t len; |
michael@0 | 2797 | if (!GetLengthProperty(cx, obj, &len)) |
michael@0 | 2798 | return false; |
michael@0 | 2799 | |
michael@0 | 2800 | /* Step 4. */ |
michael@0 | 2801 | if (args.length() == 0) { |
michael@0 | 2802 | js_ReportMissingArg(cx, args.calleev(), 0); |
michael@0 | 2803 | return false; |
michael@0 | 2804 | } |
michael@0 | 2805 | RootedObject callable(cx, ValueToCallable(cx, args[0], args.length() - 1)); |
michael@0 | 2806 | if (!callable) |
michael@0 | 2807 | return false; |
michael@0 | 2808 | |
michael@0 | 2809 | /* Step 5. */ |
michael@0 | 2810 | RootedValue thisv(cx, args.length() >= 2 ? args[1] : UndefinedValue()); |
michael@0 | 2811 | |
michael@0 | 2812 | /* Step 6. */ |
michael@0 | 2813 | RootedObject arr(cx, NewDenseAllocatedArray(cx, 0)); |
michael@0 | 2814 | if (!arr) |
michael@0 | 2815 | return false; |
michael@0 | 2816 | TypeObject *newtype = GetTypeCallerInitObject(cx, JSProto_Array); |
michael@0 | 2817 | if (!newtype) |
michael@0 | 2818 | return false; |
michael@0 | 2819 | arr->setType(newtype); |
michael@0 | 2820 | |
michael@0 | 2821 | /* Step 7. */ |
michael@0 | 2822 | uint32_t k = 0; |
michael@0 | 2823 | |
michael@0 | 2824 | /* Step 8. */ |
michael@0 | 2825 | uint32_t to = 0; |
michael@0 | 2826 | |
michael@0 | 2827 | /* Step 9. */ |
michael@0 | 2828 | JS_ASSERT(!InParallelSection()); |
michael@0 | 2829 | FastInvokeGuard fig(cx, ObjectValue(*callable)); |
michael@0 | 2830 | InvokeArgs &args2 = fig.args(); |
michael@0 | 2831 | RootedValue kValue(cx); |
michael@0 | 2832 | while (k < len) { |
michael@0 | 2833 | if (!CheckForInterrupt(cx)) |
michael@0 | 2834 | return false; |
michael@0 | 2835 | |
michael@0 | 2836 | /* Step a, b, and c.i. */ |
michael@0 | 2837 | bool kNotPresent; |
michael@0 | 2838 | if (!GetElement(cx, obj, k, &kNotPresent, &kValue)) |
michael@0 | 2839 | return false; |
michael@0 | 2840 | |
michael@0 | 2841 | /* Step c.ii-iii. */ |
michael@0 | 2842 | if (!kNotPresent) { |
michael@0 | 2843 | if (!args2.init(3)) |
michael@0 | 2844 | return false; |
michael@0 | 2845 | args2.setCallee(ObjectValue(*callable)); |
michael@0 | 2846 | args2.setThis(thisv); |
michael@0 | 2847 | args2[0].set(kValue); |
michael@0 | 2848 | args2[1].setNumber(k); |
michael@0 | 2849 | args2[2].setObject(*obj); |
michael@0 | 2850 | if (!fig.invoke(cx)) |
michael@0 | 2851 | return false; |
michael@0 | 2852 | |
michael@0 | 2853 | if (ToBoolean(args2.rval())) { |
michael@0 | 2854 | if (!SetArrayElement(cx, arr, to, kValue)) |
michael@0 | 2855 | return false; |
michael@0 | 2856 | to++; |
michael@0 | 2857 | } |
michael@0 | 2858 | } |
michael@0 | 2859 | |
michael@0 | 2860 | /* Step d. */ |
michael@0 | 2861 | k++; |
michael@0 | 2862 | } |
michael@0 | 2863 | |
michael@0 | 2864 | /* Step 10. */ |
michael@0 | 2865 | args.rval().setObject(*arr); |
michael@0 | 2866 | return true; |
michael@0 | 2867 | } |
michael@0 | 2868 | |
michael@0 | 2869 | static bool |
michael@0 | 2870 | array_isArray(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 2871 | { |
michael@0 | 2872 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 2873 | bool isArray = args.length() > 0 && IsObjectWithClass(args[0], ESClass_Array, cx); |
michael@0 | 2874 | args.rval().setBoolean(isArray); |
michael@0 | 2875 | return true; |
michael@0 | 2876 | } |
michael@0 | 2877 | |
michael@0 | 2878 | static bool |
michael@0 | 2879 | IsArrayConstructor(const Value &v) |
michael@0 | 2880 | { |
michael@0 | 2881 | // This must only return true if v is *the* Array constructor for the |
michael@0 | 2882 | // current compartment; we rely on the fact that any other Array |
michael@0 | 2883 | // constructor would be represented as a wrapper. |
michael@0 | 2884 | return v.isObject() && |
michael@0 | 2885 | v.toObject().is<JSFunction>() && |
michael@0 | 2886 | v.toObject().as<JSFunction>().isNative() && |
michael@0 | 2887 | v.toObject().as<JSFunction>().native() == js_Array; |
michael@0 | 2888 | } |
michael@0 | 2889 | |
michael@0 | 2890 | static bool |
michael@0 | 2891 | ArrayFromCallArgs(JSContext *cx, RootedTypeObject &type, CallArgs &args) |
michael@0 | 2892 | { |
michael@0 | 2893 | if (!InitArrayTypes(cx, type, args.array(), args.length())) |
michael@0 | 2894 | return false; |
michael@0 | 2895 | JSObject *obj = (args.length() == 0) |
michael@0 | 2896 | ? NewDenseEmptyArray(cx) |
michael@0 | 2897 | : NewDenseCopiedArray(cx, args.length(), args.array()); |
michael@0 | 2898 | if (!obj) |
michael@0 | 2899 | return false; |
michael@0 | 2900 | obj->setType(type); |
michael@0 | 2901 | args.rval().setObject(*obj); |
michael@0 | 2902 | return true; |
michael@0 | 2903 | } |
michael@0 | 2904 | |
michael@0 | 2905 | static bool |
michael@0 | 2906 | array_of(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 2907 | { |
michael@0 | 2908 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 2909 | |
michael@0 | 2910 | if (IsArrayConstructor(args.thisv()) || !IsConstructor(args.thisv())) { |
michael@0 | 2911 | // IsArrayConstructor(this) will usually be true in practice. This is |
michael@0 | 2912 | // the most common path. |
michael@0 | 2913 | RootedTypeObject type(cx, GetTypeCallerInitObject(cx, JSProto_Array)); |
michael@0 | 2914 | if (!type) |
michael@0 | 2915 | return false; |
michael@0 | 2916 | return ArrayFromCallArgs(cx, type, args); |
michael@0 | 2917 | } |
michael@0 | 2918 | |
michael@0 | 2919 | // Step 4. |
michael@0 | 2920 | RootedObject obj(cx); |
michael@0 | 2921 | { |
michael@0 | 2922 | RootedValue v(cx); |
michael@0 | 2923 | Value argv[1] = {NumberValue(args.length())}; |
michael@0 | 2924 | if (!InvokeConstructor(cx, args.thisv(), 1, argv, v.address())) |
michael@0 | 2925 | return false; |
michael@0 | 2926 | obj = ToObject(cx, v); |
michael@0 | 2927 | if (!obj) |
michael@0 | 2928 | return false; |
michael@0 | 2929 | } |
michael@0 | 2930 | |
michael@0 | 2931 | // Step 8. |
michael@0 | 2932 | for (unsigned k = 0; k < args.length(); k++) { |
michael@0 | 2933 | if (!JSObject::defineElement(cx, obj, k, args[k])) |
michael@0 | 2934 | return false; |
michael@0 | 2935 | } |
michael@0 | 2936 | |
michael@0 | 2937 | // Steps 9-10. |
michael@0 | 2938 | RootedValue v(cx, NumberValue(args.length())); |
michael@0 | 2939 | if (!JSObject::setProperty(cx, obj, obj, cx->names().length, &v, true)) |
michael@0 | 2940 | return false; |
michael@0 | 2941 | |
michael@0 | 2942 | // Step 11. |
michael@0 | 2943 | args.rval().setObject(*obj); |
michael@0 | 2944 | return true; |
michael@0 | 2945 | } |
michael@0 | 2946 | |
michael@0 | 2947 | #define GENERIC JSFUN_GENERIC_NATIVE |
michael@0 | 2948 | |
michael@0 | 2949 | static const JSFunctionSpec array_methods[] = { |
michael@0 | 2950 | #if JS_HAS_TOSOURCE |
michael@0 | 2951 | JS_FN(js_toSource_str, array_toSource, 0,0), |
michael@0 | 2952 | #endif |
michael@0 | 2953 | JS_FN(js_toString_str, array_toString, 0,0), |
michael@0 | 2954 | JS_FN(js_toLocaleString_str,array_toLocaleString,0,0), |
michael@0 | 2955 | |
michael@0 | 2956 | /* Perl-ish methods. */ |
michael@0 | 2957 | JS_FN("join", array_join, 1,JSFUN_GENERIC_NATIVE), |
michael@0 | 2958 | JS_FN("reverse", array_reverse, 0,JSFUN_GENERIC_NATIVE), |
michael@0 | 2959 | JS_FN("sort", array_sort, 1,JSFUN_GENERIC_NATIVE), |
michael@0 | 2960 | JS_FN("push", array_push, 1,JSFUN_GENERIC_NATIVE), |
michael@0 | 2961 | JS_FN("pop", array_pop, 0,JSFUN_GENERIC_NATIVE), |
michael@0 | 2962 | JS_FN("shift", array_shift, 0,JSFUN_GENERIC_NATIVE), |
michael@0 | 2963 | JS_FN("unshift", array_unshift, 1,JSFUN_GENERIC_NATIVE), |
michael@0 | 2964 | JS_FN("splice", array_splice, 2,JSFUN_GENERIC_NATIVE), |
michael@0 | 2965 | |
michael@0 | 2966 | /* Pythonic sequence methods. */ |
michael@0 | 2967 | JS_FN("concat", array_concat, 1,JSFUN_GENERIC_NATIVE), |
michael@0 | 2968 | JS_FN("slice", array_slice, 2,JSFUN_GENERIC_NATIVE), |
michael@0 | 2969 | |
michael@0 | 2970 | JS_SELF_HOSTED_FN("lastIndexOf", "ArrayLastIndexOf", 1,0), |
michael@0 | 2971 | JS_SELF_HOSTED_FN("indexOf", "ArrayIndexOf", 1,0), |
michael@0 | 2972 | JS_SELF_HOSTED_FN("forEach", "ArrayForEach", 1,0), |
michael@0 | 2973 | JS_SELF_HOSTED_FN("map", "ArrayMap", 1,0), |
michael@0 | 2974 | JS_SELF_HOSTED_FN("reduce", "ArrayReduce", 1,0), |
michael@0 | 2975 | JS_SELF_HOSTED_FN("reduceRight", "ArrayReduceRight", 1,0), |
michael@0 | 2976 | JS_FN("filter", array_filter, 1,JSFUN_GENERIC_NATIVE), |
michael@0 | 2977 | JS_SELF_HOSTED_FN("some", "ArraySome", 1,0), |
michael@0 | 2978 | JS_SELF_HOSTED_FN("every", "ArrayEvery", 1,0), |
michael@0 | 2979 | |
michael@0 | 2980 | #ifdef ENABLE_PARALLEL_JS |
michael@0 | 2981 | /* Parallelizable and pure methods. */ |
michael@0 | 2982 | JS_SELF_HOSTED_FN("mapPar", "ArrayMapPar", 2,0), |
michael@0 | 2983 | JS_SELF_HOSTED_FN("reducePar", "ArrayReducePar", 2,0), |
michael@0 | 2984 | JS_SELF_HOSTED_FN("scanPar", "ArrayScanPar", 2,0), |
michael@0 | 2985 | JS_SELF_HOSTED_FN("scatterPar", "ArrayScatterPar", 5,0), |
michael@0 | 2986 | JS_SELF_HOSTED_FN("filterPar", "ArrayFilterPar", 2,0), |
michael@0 | 2987 | #endif |
michael@0 | 2988 | |
michael@0 | 2989 | /* ES6 additions */ |
michael@0 | 2990 | JS_SELF_HOSTED_FN("find", "ArrayFind", 1,0), |
michael@0 | 2991 | JS_SELF_HOSTED_FN("findIndex", "ArrayFindIndex", 1,0), |
michael@0 | 2992 | |
michael@0 | 2993 | JS_SELF_HOSTED_FN("fill", "ArrayFill", 3,0), |
michael@0 | 2994 | |
michael@0 | 2995 | JS_SELF_HOSTED_FN("@@iterator", "ArrayValues", 0,0), |
michael@0 | 2996 | JS_SELF_HOSTED_FN("entries", "ArrayEntries", 0,0), |
michael@0 | 2997 | JS_SELF_HOSTED_FN("keys", "ArrayKeys", 0,0), |
michael@0 | 2998 | JS_FS_END |
michael@0 | 2999 | }; |
michael@0 | 3000 | |
michael@0 | 3001 | static const JSFunctionSpec array_static_methods[] = { |
michael@0 | 3002 | JS_FN("isArray", array_isArray, 1,0), |
michael@0 | 3003 | JS_SELF_HOSTED_FN("lastIndexOf", "ArrayStaticLastIndexOf", 2,0), |
michael@0 | 3004 | JS_SELF_HOSTED_FN("indexOf", "ArrayStaticIndexOf", 2,0), |
michael@0 | 3005 | JS_SELF_HOSTED_FN("forEach", "ArrayStaticForEach", 2,0), |
michael@0 | 3006 | JS_SELF_HOSTED_FN("map", "ArrayStaticMap", 2,0), |
michael@0 | 3007 | JS_SELF_HOSTED_FN("every", "ArrayStaticEvery", 2,0), |
michael@0 | 3008 | JS_SELF_HOSTED_FN("some", "ArrayStaticSome", 2,0), |
michael@0 | 3009 | JS_SELF_HOSTED_FN("reduce", "ArrayStaticReduce", 2,0), |
michael@0 | 3010 | JS_SELF_HOSTED_FN("reduceRight", "ArrayStaticReduceRight", 2,0), |
michael@0 | 3011 | JS_FN("of", array_of, 0,0), |
michael@0 | 3012 | |
michael@0 | 3013 | #ifdef ENABLE_PARALLEL_JS |
michael@0 | 3014 | JS_SELF_HOSTED_FN("build", "ArrayStaticBuild", 2,0), |
michael@0 | 3015 | /* Parallelizable and pure static methods. */ |
michael@0 | 3016 | JS_SELF_HOSTED_FN("buildPar", "ArrayStaticBuildPar", 3,0), |
michael@0 | 3017 | #endif |
michael@0 | 3018 | |
michael@0 | 3019 | JS_FS_END |
michael@0 | 3020 | }; |
michael@0 | 3021 | |
michael@0 | 3022 | /* ES5 15.4.2 */ |
michael@0 | 3023 | bool |
michael@0 | 3024 | js_Array(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 3025 | { |
michael@0 | 3026 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 3027 | RootedTypeObject type(cx, GetTypeCallerInitObject(cx, JSProto_Array)); |
michael@0 | 3028 | if (!type) |
michael@0 | 3029 | return false; |
michael@0 | 3030 | |
michael@0 | 3031 | if (args.length() != 1 || !args[0].isNumber()) |
michael@0 | 3032 | return ArrayFromCallArgs(cx, type, args); |
michael@0 | 3033 | |
michael@0 | 3034 | uint32_t length; |
michael@0 | 3035 | if (args[0].isInt32()) { |
michael@0 | 3036 | int32_t i = args[0].toInt32(); |
michael@0 | 3037 | if (i < 0) { |
michael@0 | 3038 | JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_BAD_ARRAY_LENGTH); |
michael@0 | 3039 | return false; |
michael@0 | 3040 | } |
michael@0 | 3041 | length = uint32_t(i); |
michael@0 | 3042 | } else { |
michael@0 | 3043 | double d = args[0].toDouble(); |
michael@0 | 3044 | length = ToUint32(d); |
michael@0 | 3045 | if (d != double(length)) { |
michael@0 | 3046 | JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_BAD_ARRAY_LENGTH); |
michael@0 | 3047 | return false; |
michael@0 | 3048 | } |
michael@0 | 3049 | } |
michael@0 | 3050 | |
michael@0 | 3051 | /* |
michael@0 | 3052 | * Allocate dense elements eagerly for small arrays, to avoid reallocating |
michael@0 | 3053 | * elements when filling the array. |
michael@0 | 3054 | */ |
michael@0 | 3055 | RootedObject obj(cx); |
michael@0 | 3056 | obj = (length <= ArrayObject::EagerAllocationMaxLength) |
michael@0 | 3057 | ? NewDenseAllocatedArray(cx, length) |
michael@0 | 3058 | : NewDenseUnallocatedArray(cx, length); |
michael@0 | 3059 | if (!obj) |
michael@0 | 3060 | return false; |
michael@0 | 3061 | Rooted<ArrayObject*> arr(cx, &obj->as<ArrayObject>()); |
michael@0 | 3062 | |
michael@0 | 3063 | arr->setType(type); |
michael@0 | 3064 | |
michael@0 | 3065 | /* If the length calculation overflowed, make sure that is marked for the new type. */ |
michael@0 | 3066 | if (arr->length() > INT32_MAX) |
michael@0 | 3067 | arr->setLength(cx, arr->length()); |
michael@0 | 3068 | |
michael@0 | 3069 | args.rval().setObject(*arr); |
michael@0 | 3070 | return true; |
michael@0 | 3071 | } |
michael@0 | 3072 | |
michael@0 | 3073 | JSObject * |
michael@0 | 3074 | js_InitArrayClass(JSContext *cx, HandleObject obj) |
michael@0 | 3075 | { |
michael@0 | 3076 | JS_ASSERT(obj->isNative()); |
michael@0 | 3077 | |
michael@0 | 3078 | Rooted<GlobalObject*> global(cx, &obj->as<GlobalObject>()); |
michael@0 | 3079 | |
michael@0 | 3080 | RootedObject proto(cx, global->getOrCreateObjectPrototype(cx)); |
michael@0 | 3081 | if (!proto) |
michael@0 | 3082 | return nullptr; |
michael@0 | 3083 | |
michael@0 | 3084 | RootedTypeObject type(cx, cx->getNewType(&ArrayObject::class_, proto.get())); |
michael@0 | 3085 | if (!type) |
michael@0 | 3086 | return nullptr; |
michael@0 | 3087 | |
michael@0 | 3088 | JSObject *metadata = nullptr; |
michael@0 | 3089 | if (!NewObjectMetadata(cx, &metadata)) |
michael@0 | 3090 | return nullptr; |
michael@0 | 3091 | |
michael@0 | 3092 | RootedShape shape(cx, EmptyShape::getInitialShape(cx, &ArrayObject::class_, TaggedProto(proto), |
michael@0 | 3093 | proto->getParent(), metadata, |
michael@0 | 3094 | gc::FINALIZE_OBJECT0)); |
michael@0 | 3095 | if (!shape) |
michael@0 | 3096 | return nullptr; |
michael@0 | 3097 | |
michael@0 | 3098 | RootedObject arrayProto(cx, JSObject::createArray(cx, gc::FINALIZE_OBJECT4, gc::TenuredHeap, shape, type, 0)); |
michael@0 | 3099 | if (!arrayProto || !JSObject::setSingletonType(cx, arrayProto) || !AddLengthProperty(cx, arrayProto)) |
michael@0 | 3100 | return nullptr; |
michael@0 | 3101 | |
michael@0 | 3102 | RootedFunction ctor(cx); |
michael@0 | 3103 | ctor = global->createConstructor(cx, js_Array, cx->names().Array, 1); |
michael@0 | 3104 | if (!ctor) |
michael@0 | 3105 | return nullptr; |
michael@0 | 3106 | |
michael@0 | 3107 | /* |
michael@0 | 3108 | * The default 'new' type of Array.prototype is required by type inference |
michael@0 | 3109 | * to have unknown properties, to simplify handling of e.g. heterogenous |
michael@0 | 3110 | * arrays in JSON and script literals and allows setDenseArrayElement to |
michael@0 | 3111 | * be used without updating the indexed type set for such default arrays. |
michael@0 | 3112 | */ |
michael@0 | 3113 | if (!JSObject::setNewTypeUnknown(cx, &ArrayObject::class_, arrayProto)) |
michael@0 | 3114 | return nullptr; |
michael@0 | 3115 | |
michael@0 | 3116 | if (!LinkConstructorAndPrototype(cx, ctor, arrayProto)) |
michael@0 | 3117 | return nullptr; |
michael@0 | 3118 | |
michael@0 | 3119 | if (!DefinePropertiesAndBrand(cx, arrayProto, nullptr, array_methods) || |
michael@0 | 3120 | !DefinePropertiesAndBrand(cx, ctor, nullptr, array_static_methods)) |
michael@0 | 3121 | { |
michael@0 | 3122 | return nullptr; |
michael@0 | 3123 | } |
michael@0 | 3124 | |
michael@0 | 3125 | if (!GlobalObject::initBuiltinConstructor(cx, global, JSProto_Array, ctor, arrayProto)) |
michael@0 | 3126 | return nullptr; |
michael@0 | 3127 | |
michael@0 | 3128 | return arrayProto; |
michael@0 | 3129 | } |
michael@0 | 3130 | |
michael@0 | 3131 | /* |
michael@0 | 3132 | * Array allocation functions. |
michael@0 | 3133 | */ |
michael@0 | 3134 | |
michael@0 | 3135 | static inline bool |
michael@0 | 3136 | EnsureNewArrayElements(ExclusiveContext *cx, JSObject *obj, uint32_t length) |
michael@0 | 3137 | { |
michael@0 | 3138 | /* |
michael@0 | 3139 | * If ensureElements creates dynamically allocated slots, then having |
michael@0 | 3140 | * fixedSlots is a waste. |
michael@0 | 3141 | */ |
michael@0 | 3142 | DebugOnly<uint32_t> cap = obj->getDenseCapacity(); |
michael@0 | 3143 | |
michael@0 | 3144 | if (!obj->ensureElements(cx, length)) |
michael@0 | 3145 | return false; |
michael@0 | 3146 | |
michael@0 | 3147 | JS_ASSERT_IF(cap, !obj->hasDynamicElements()); |
michael@0 | 3148 | |
michael@0 | 3149 | return true; |
michael@0 | 3150 | } |
michael@0 | 3151 | |
michael@0 | 3152 | template<bool allocateCapacity> |
michael@0 | 3153 | static MOZ_ALWAYS_INLINE ArrayObject * |
michael@0 | 3154 | NewArray(ExclusiveContext *cxArg, uint32_t length, |
michael@0 | 3155 | JSObject *protoArg, NewObjectKind newKind = GenericObject) |
michael@0 | 3156 | { |
michael@0 | 3157 | gc::AllocKind allocKind = GuessArrayGCKind(length); |
michael@0 | 3158 | JS_ASSERT(CanBeFinalizedInBackground(allocKind, &ArrayObject::class_)); |
michael@0 | 3159 | allocKind = GetBackgroundAllocKind(allocKind); |
michael@0 | 3160 | |
michael@0 | 3161 | NewObjectCache::EntryIndex entry = -1; |
michael@0 | 3162 | if (JSContext *cx = cxArg->maybeJSContext()) { |
michael@0 | 3163 | NewObjectCache &cache = cx->runtime()->newObjectCache; |
michael@0 | 3164 | if (newKind == GenericObject && |
michael@0 | 3165 | !cx->compartment()->hasObjectMetadataCallback() && |
michael@0 | 3166 | cache.lookupGlobal(&ArrayObject::class_, cx->global(), allocKind, &entry)) |
michael@0 | 3167 | { |
michael@0 | 3168 | gc::InitialHeap heap = GetInitialHeap(newKind, &ArrayObject::class_); |
michael@0 | 3169 | JSObject *obj = cache.newObjectFromHit<NoGC>(cx, entry, heap); |
michael@0 | 3170 | if (obj) { |
michael@0 | 3171 | /* Fixup the elements pointer and length, which may be incorrect. */ |
michael@0 | 3172 | ArrayObject *arr = &obj->as<ArrayObject>(); |
michael@0 | 3173 | arr->setFixedElements(); |
michael@0 | 3174 | arr->setLength(cx, length); |
michael@0 | 3175 | if (allocateCapacity && !EnsureNewArrayElements(cx, arr, length)) |
michael@0 | 3176 | return nullptr; |
michael@0 | 3177 | return arr; |
michael@0 | 3178 | } else { |
michael@0 | 3179 | RootedObject proto(cxArg, protoArg); |
michael@0 | 3180 | obj = cache.newObjectFromHit<CanGC>(cx, entry, heap); |
michael@0 | 3181 | JS_ASSERT(!obj); |
michael@0 | 3182 | protoArg = proto; |
michael@0 | 3183 | } |
michael@0 | 3184 | } |
michael@0 | 3185 | } |
michael@0 | 3186 | |
michael@0 | 3187 | RootedObject proto(cxArg, protoArg); |
michael@0 | 3188 | if (protoArg) |
michael@0 | 3189 | JS::PoisonPtr(&protoArg); |
michael@0 | 3190 | |
michael@0 | 3191 | if (!proto && !GetBuiltinPrototype(cxArg, JSProto_Array, &proto)) |
michael@0 | 3192 | return nullptr; |
michael@0 | 3193 | |
michael@0 | 3194 | RootedTypeObject type(cxArg, cxArg->getNewType(&ArrayObject::class_, proto.get())); |
michael@0 | 3195 | if (!type) |
michael@0 | 3196 | return nullptr; |
michael@0 | 3197 | |
michael@0 | 3198 | JSObject *metadata = nullptr; |
michael@0 | 3199 | if (!NewObjectMetadata(cxArg, &metadata)) |
michael@0 | 3200 | return nullptr; |
michael@0 | 3201 | |
michael@0 | 3202 | /* |
michael@0 | 3203 | * Get a shape with zero fixed slots, regardless of the size class. |
michael@0 | 3204 | * See JSObject::createArray. |
michael@0 | 3205 | */ |
michael@0 | 3206 | RootedShape shape(cxArg, EmptyShape::getInitialShape(cxArg, &ArrayObject::class_, |
michael@0 | 3207 | TaggedProto(proto), cxArg->global(), |
michael@0 | 3208 | metadata, gc::FINALIZE_OBJECT0)); |
michael@0 | 3209 | if (!shape) |
michael@0 | 3210 | return nullptr; |
michael@0 | 3211 | |
michael@0 | 3212 | Rooted<ArrayObject*> arr(cxArg, JSObject::createArray(cxArg, allocKind, |
michael@0 | 3213 | GetInitialHeap(newKind, &ArrayObject::class_), |
michael@0 | 3214 | shape, type, length)); |
michael@0 | 3215 | if (!arr) |
michael@0 | 3216 | return nullptr; |
michael@0 | 3217 | |
michael@0 | 3218 | if (shape->isEmptyShape()) { |
michael@0 | 3219 | if (!AddLengthProperty(cxArg, arr)) |
michael@0 | 3220 | return nullptr; |
michael@0 | 3221 | shape = arr->lastProperty(); |
michael@0 | 3222 | EmptyShape::insertInitialShape(cxArg, shape, proto); |
michael@0 | 3223 | } |
michael@0 | 3224 | |
michael@0 | 3225 | if (newKind == SingletonObject && !JSObject::setSingletonType(cxArg, arr)) |
michael@0 | 3226 | return nullptr; |
michael@0 | 3227 | |
michael@0 | 3228 | if (entry != -1) { |
michael@0 | 3229 | cxArg->asJSContext()->runtime()->newObjectCache.fillGlobal(entry, &ArrayObject::class_, |
michael@0 | 3230 | cxArg->global(), allocKind, arr); |
michael@0 | 3231 | } |
michael@0 | 3232 | |
michael@0 | 3233 | if (allocateCapacity && !EnsureNewArrayElements(cxArg, arr, length)) |
michael@0 | 3234 | return nullptr; |
michael@0 | 3235 | |
michael@0 | 3236 | probes::CreateObject(cxArg, arr); |
michael@0 | 3237 | return arr; |
michael@0 | 3238 | } |
michael@0 | 3239 | |
michael@0 | 3240 | ArrayObject * JS_FASTCALL |
michael@0 | 3241 | js::NewDenseEmptyArray(JSContext *cx, JSObject *proto /* = nullptr */, |
michael@0 | 3242 | NewObjectKind newKind /* = GenericObject */) |
michael@0 | 3243 | { |
michael@0 | 3244 | return NewArray<false>(cx, 0, proto, newKind); |
michael@0 | 3245 | } |
michael@0 | 3246 | |
michael@0 | 3247 | ArrayObject * JS_FASTCALL |
michael@0 | 3248 | js::NewDenseAllocatedArray(ExclusiveContext *cx, uint32_t length, JSObject *proto /* = nullptr */, |
michael@0 | 3249 | NewObjectKind newKind /* = GenericObject */) |
michael@0 | 3250 | { |
michael@0 | 3251 | return NewArray<true>(cx, length, proto, newKind); |
michael@0 | 3252 | } |
michael@0 | 3253 | |
michael@0 | 3254 | ArrayObject * JS_FASTCALL |
michael@0 | 3255 | js::NewDenseUnallocatedArray(ExclusiveContext *cx, uint32_t length, JSObject *proto /* = nullptr */, |
michael@0 | 3256 | NewObjectKind newKind /* = GenericObject */) |
michael@0 | 3257 | { |
michael@0 | 3258 | return NewArray<false>(cx, length, proto, newKind); |
michael@0 | 3259 | } |
michael@0 | 3260 | |
michael@0 | 3261 | ArrayObject * |
michael@0 | 3262 | js::NewDenseCopiedArray(JSContext *cx, uint32_t length, HandleObject src, uint32_t elementOffset, |
michael@0 | 3263 | JSObject *proto /* = nullptr */) |
michael@0 | 3264 | { |
michael@0 | 3265 | JS_ASSERT(!src->isIndexed()); |
michael@0 | 3266 | |
michael@0 | 3267 | ArrayObject* arr = NewArray<true>(cx, length, proto); |
michael@0 | 3268 | if (!arr) |
michael@0 | 3269 | return nullptr; |
michael@0 | 3270 | |
michael@0 | 3271 | JS_ASSERT(arr->getDenseCapacity() >= length); |
michael@0 | 3272 | |
michael@0 | 3273 | const Value* vp = src->getDenseElements() + elementOffset; |
michael@0 | 3274 | arr->setDenseInitializedLength(vp ? length : 0); |
michael@0 | 3275 | |
michael@0 | 3276 | if (vp) |
michael@0 | 3277 | arr->initDenseElements(0, vp, length); |
michael@0 | 3278 | |
michael@0 | 3279 | return arr; |
michael@0 | 3280 | } |
michael@0 | 3281 | |
michael@0 | 3282 | // values must point at already-rooted Value objects |
michael@0 | 3283 | ArrayObject * |
michael@0 | 3284 | js::NewDenseCopiedArray(JSContext *cx, uint32_t length, const Value *values, |
michael@0 | 3285 | JSObject *proto /* = nullptr */, NewObjectKind newKind /* = GenericObject */) |
michael@0 | 3286 | { |
michael@0 | 3287 | ArrayObject* arr = NewArray<true>(cx, length, proto); |
michael@0 | 3288 | if (!arr) |
michael@0 | 3289 | return nullptr; |
michael@0 | 3290 | |
michael@0 | 3291 | JS_ASSERT(arr->getDenseCapacity() >= length); |
michael@0 | 3292 | |
michael@0 | 3293 | arr->setDenseInitializedLength(values ? length : 0); |
michael@0 | 3294 | |
michael@0 | 3295 | if (values) |
michael@0 | 3296 | arr->initDenseElements(0, values, length); |
michael@0 | 3297 | |
michael@0 | 3298 | return arr; |
michael@0 | 3299 | } |
michael@0 | 3300 | |
michael@0 | 3301 | ArrayObject * |
michael@0 | 3302 | js::NewDenseAllocatedArrayWithTemplate(JSContext *cx, uint32_t length, JSObject *templateObject) |
michael@0 | 3303 | { |
michael@0 | 3304 | gc::AllocKind allocKind = GuessArrayGCKind(length); |
michael@0 | 3305 | JS_ASSERT(CanBeFinalizedInBackground(allocKind, &ArrayObject::class_)); |
michael@0 | 3306 | allocKind = GetBackgroundAllocKind(allocKind); |
michael@0 | 3307 | |
michael@0 | 3308 | RootedTypeObject type(cx, templateObject->type()); |
michael@0 | 3309 | if (!type) |
michael@0 | 3310 | return nullptr; |
michael@0 | 3311 | |
michael@0 | 3312 | RootedShape shape(cx, templateObject->lastProperty()); |
michael@0 | 3313 | if (!shape) |
michael@0 | 3314 | return nullptr; |
michael@0 | 3315 | |
michael@0 | 3316 | gc::InitialHeap heap = GetInitialHeap(GenericObject, &ArrayObject::class_); |
michael@0 | 3317 | Rooted<ArrayObject *> arr(cx, JSObject::createArray(cx, allocKind, heap, shape, type, length)); |
michael@0 | 3318 | if (!arr) |
michael@0 | 3319 | return nullptr; |
michael@0 | 3320 | |
michael@0 | 3321 | if (!EnsureNewArrayElements(cx, arr, length)) |
michael@0 | 3322 | return nullptr; |
michael@0 | 3323 | |
michael@0 | 3324 | probes::CreateObject(cx, arr); |
michael@0 | 3325 | |
michael@0 | 3326 | return arr; |
michael@0 | 3327 | } |
michael@0 | 3328 | |
michael@0 | 3329 | #ifdef DEBUG |
michael@0 | 3330 | bool |
michael@0 | 3331 | js_ArrayInfo(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 3332 | { |
michael@0 | 3333 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 3334 | JSObject *obj; |
michael@0 | 3335 | |
michael@0 | 3336 | for (unsigned i = 0; i < args.length(); i++) { |
michael@0 | 3337 | RootedValue arg(cx, args[i]); |
michael@0 | 3338 | |
michael@0 | 3339 | char *bytes = DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, arg, NullPtr()); |
michael@0 | 3340 | if (!bytes) |
michael@0 | 3341 | return false; |
michael@0 | 3342 | if (arg.isPrimitive() || |
michael@0 | 3343 | !(obj = arg.toObjectOrNull())->is<ArrayObject>()) { |
michael@0 | 3344 | fprintf(stderr, "%s: not array\n", bytes); |
michael@0 | 3345 | js_free(bytes); |
michael@0 | 3346 | continue; |
michael@0 | 3347 | } |
michael@0 | 3348 | fprintf(stderr, "%s: (len %u", bytes, obj->as<ArrayObject>().length()); |
michael@0 | 3349 | fprintf(stderr, ", capacity %u", obj->getDenseCapacity()); |
michael@0 | 3350 | fputs(")\n", stderr); |
michael@0 | 3351 | js_free(bytes); |
michael@0 | 3352 | } |
michael@0 | 3353 | |
michael@0 | 3354 | args.rval().setUndefined(); |
michael@0 | 3355 | return true; |
michael@0 | 3356 | } |
michael@0 | 3357 | #endif |