Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1 | // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5 | #include "src/runtime/runtime-utils.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6 | |
| 7 | #include "src/arguments.h" |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8 | #include "src/code-stubs.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9 | #include "src/conversions-inl.h" |
| 10 | #include "src/elements.h" |
| 11 | #include "src/factory.h" |
| 12 | #include "src/isolate-inl.h" |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 13 | #include "src/keys.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14 | #include "src/messages.h" |
| 15 | #include "src/prototype.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 16 | |
| 17 | namespace v8 { |
| 18 | namespace internal { |
| 19 | |
| 20 | RUNTIME_FUNCTION(Runtime_FinishArrayPrototypeSetup) { |
| 21 | HandleScope scope(isolate); |
| 22 | DCHECK(args.length() == 1); |
| 23 | CONVERT_ARG_HANDLE_CHECKED(JSArray, prototype, 0); |
| 24 | Object* length = prototype->length(); |
| 25 | RUNTIME_ASSERT(length->IsSmi() && Smi::cast(length)->value() == 0); |
| 26 | RUNTIME_ASSERT(prototype->HasFastSmiOrObjectElements()); |
| 27 | // This is necessary to enable fast checks for absence of elements |
| 28 | // on Array.prototype and below. |
| 29 | prototype->set_elements(isolate->heap()->empty_fixed_array()); |
| 30 | return Smi::FromInt(0); |
| 31 | } |
| 32 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 33 | static void InstallCode(Isolate* isolate, Handle<JSObject> holder, |
| 34 | const char* name, Handle<Code> code) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 35 | Handle<String> key = isolate->factory()->InternalizeUtf8String(name); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 36 | Handle<JSFunction> optimized = |
| 37 | isolate->factory()->NewFunctionWithoutPrototype(key, code); |
| 38 | optimized->shared()->DontAdaptArguments(); |
| 39 | JSObject::AddProperty(holder, key, optimized, NONE); |
| 40 | } |
| 41 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 42 | static void InstallBuiltin(Isolate* isolate, Handle<JSObject> holder, |
| 43 | const char* name, Builtins::Name builtin_name) { |
| 44 | InstallCode(isolate, holder, name, |
| 45 | handle(isolate->builtins()->builtin(builtin_name), isolate)); |
| 46 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 47 | |
| 48 | RUNTIME_FUNCTION(Runtime_SpecialArrayFunctions) { |
| 49 | HandleScope scope(isolate); |
| 50 | DCHECK(args.length() == 0); |
| 51 | Handle<JSObject> holder = |
| 52 | isolate->factory()->NewJSObject(isolate->object_function()); |
| 53 | |
| 54 | InstallBuiltin(isolate, holder, "pop", Builtins::kArrayPop); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 55 | FastArrayPushStub stub(isolate); |
| 56 | InstallCode(isolate, holder, "push", stub.GetCode()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 57 | InstallBuiltin(isolate, holder, "shift", Builtins::kArrayShift); |
| 58 | InstallBuiltin(isolate, holder, "unshift", Builtins::kArrayUnshift); |
| 59 | InstallBuiltin(isolate, holder, "slice", Builtins::kArraySlice); |
| 60 | InstallBuiltin(isolate, holder, "splice", Builtins::kArraySplice); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 61 | |
| 62 | return *holder; |
| 63 | } |
| 64 | |
| 65 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 66 | RUNTIME_FUNCTION(Runtime_FixedArrayGet) { |
| 67 | SealHandleScope shs(isolate); |
| 68 | DCHECK(args.length() == 2); |
| 69 | CONVERT_ARG_CHECKED(FixedArray, object, 0); |
| 70 | CONVERT_SMI_ARG_CHECKED(index, 1); |
| 71 | return object->get(index); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | RUNTIME_FUNCTION(Runtime_FixedArraySet) { |
| 76 | SealHandleScope shs(isolate); |
| 77 | DCHECK(args.length() == 3); |
| 78 | CONVERT_ARG_CHECKED(FixedArray, object, 0); |
| 79 | CONVERT_SMI_ARG_CHECKED(index, 1); |
| 80 | CONVERT_ARG_CHECKED(Object, value, 2); |
| 81 | object->set(index, value); |
| 82 | return isolate->heap()->undefined_value(); |
| 83 | } |
| 84 | |
| 85 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 86 | RUNTIME_FUNCTION(Runtime_TransitionElementsKind) { |
| 87 | HandleScope scope(isolate); |
| 88 | RUNTIME_ASSERT(args.length() == 2); |
| 89 | CONVERT_ARG_HANDLE_CHECKED(JSArray, array, 0); |
| 90 | CONVERT_ARG_HANDLE_CHECKED(Map, map, 1); |
| 91 | JSObject::TransitionElementsKind(array, map->elements_kind()); |
| 92 | return *array; |
| 93 | } |
| 94 | |
| 95 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 96 | // Moves all own elements of an object, that are below a limit, to positions |
| 97 | // starting at zero. All undefined values are placed after non-undefined values, |
| 98 | // and are followed by non-existing element. Does not change the length |
| 99 | // property. |
| 100 | // Returns the number of non-undefined elements collected. |
| 101 | // Returns -1 if hole removal is not supported by this method. |
| 102 | RUNTIME_FUNCTION(Runtime_RemoveArrayHoles) { |
| 103 | HandleScope scope(isolate); |
| 104 | DCHECK(args.length() == 2); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 105 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, object, 0); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 106 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[1]); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 107 | if (object->IsJSProxy()) return Smi::FromInt(-1); |
| 108 | return *JSObject::PrepareElementsForSort(Handle<JSObject>::cast(object), |
| 109 | limit); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
| 113 | // Move contents of argument 0 (an array) to argument 1 (an array) |
| 114 | RUNTIME_FUNCTION(Runtime_MoveArrayContents) { |
| 115 | HandleScope scope(isolate); |
| 116 | DCHECK(args.length() == 2); |
| 117 | CONVERT_ARG_HANDLE_CHECKED(JSArray, from, 0); |
| 118 | CONVERT_ARG_HANDLE_CHECKED(JSArray, to, 1); |
| 119 | JSObject::ValidateElements(from); |
| 120 | JSObject::ValidateElements(to); |
| 121 | |
| 122 | Handle<FixedArrayBase> new_elements(from->elements()); |
| 123 | ElementsKind from_kind = from->GetElementsKind(); |
| 124 | Handle<Map> new_map = JSObject::GetElementsTransitionMap(to, from_kind); |
| 125 | JSObject::SetMapAndElements(to, new_map, new_elements); |
| 126 | to->set_length(from->length()); |
| 127 | |
| 128 | JSObject::ResetElements(from); |
| 129 | from->set_length(Smi::FromInt(0)); |
| 130 | |
| 131 | JSObject::ValidateElements(to); |
| 132 | return *to; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | // How many elements does this object/array have? |
| 137 | RUNTIME_FUNCTION(Runtime_EstimateNumberOfElements) { |
| 138 | HandleScope scope(isolate); |
| 139 | DCHECK(args.length() == 1); |
| 140 | CONVERT_ARG_HANDLE_CHECKED(JSArray, array, 0); |
| 141 | Handle<FixedArrayBase> elements(array->elements(), isolate); |
| 142 | SealHandleScope shs(isolate); |
| 143 | if (elements->IsDictionary()) { |
| 144 | int result = |
| 145 | Handle<SeededNumberDictionary>::cast(elements)->NumberOfElements(); |
| 146 | return Smi::FromInt(result); |
| 147 | } else { |
| 148 | DCHECK(array->length()->IsSmi()); |
| 149 | // For packed elements, we know the exact number of elements |
| 150 | int length = elements->length(); |
| 151 | ElementsKind kind = array->GetElementsKind(); |
| 152 | if (IsFastPackedElementsKind(kind)) { |
| 153 | return Smi::FromInt(length); |
| 154 | } |
| 155 | // For holey elements, take samples from the buffer checking for holes |
| 156 | // to generate the estimate. |
| 157 | const int kNumberOfHoleCheckSamples = 97; |
| 158 | int increment = (length < kNumberOfHoleCheckSamples) |
| 159 | ? 1 |
| 160 | : static_cast<int>(length / kNumberOfHoleCheckSamples); |
| 161 | ElementsAccessor* accessor = array->GetElementsAccessor(); |
| 162 | int holes = 0; |
| 163 | for (int i = 0; i < length; i += increment) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 164 | if (!accessor->HasElement(array, i, elements)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 165 | ++holes; |
| 166 | } |
| 167 | } |
| 168 | int estimate = static_cast<int>((kNumberOfHoleCheckSamples - holes) / |
| 169 | kNumberOfHoleCheckSamples * length); |
| 170 | return Smi::FromInt(estimate); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | |
| 175 | // Returns an array that tells you where in the [0, length) interval an array |
| 176 | // might have elements. Can either return an array of keys (positive integers |
| 177 | // or undefined) or a number representing the positive length of an interval |
| 178 | // starting at index 0. |
| 179 | // Intervals can span over some keys that are not in the object. |
| 180 | RUNTIME_FUNCTION(Runtime_GetArrayKeys) { |
| 181 | HandleScope scope(isolate); |
| 182 | DCHECK(args.length() == 2); |
| 183 | CONVERT_ARG_HANDLE_CHECKED(JSObject, array, 0); |
| 184 | CONVERT_NUMBER_CHECKED(uint32_t, length, Uint32, args[1]); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 185 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 186 | if (array->HasFastStringWrapperElements()) { |
| 187 | int string_length = |
| 188 | String::cast(Handle<JSValue>::cast(array)->value())->length(); |
| 189 | int backing_store_length = array->elements()->length(); |
| 190 | return *isolate->factory()->NewNumberFromUint( |
| 191 | Min(length, |
| 192 | static_cast<uint32_t>(Max(string_length, backing_store_length)))); |
| 193 | } |
| 194 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 195 | if (!array->elements()->IsDictionary()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 196 | RUNTIME_ASSERT(array->HasFastSmiOrObjectElements() || |
| 197 | array->HasFastDoubleElements()); |
| 198 | uint32_t actual_length = static_cast<uint32_t>(array->elements()->length()); |
| 199 | return *isolate->factory()->NewNumberFromUint(Min(actual_length, length)); |
| 200 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 201 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 202 | KeyAccumulator accumulator(isolate, OWN_ONLY, ALL_PROPERTIES); |
| 203 | // No need to separate prototype levels since we only get element keys. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 204 | for (PrototypeIterator iter(isolate, array, |
| 205 | PrototypeIterator::START_AT_RECEIVER); |
| 206 | !iter.IsAtEnd(); iter.Advance()) { |
| 207 | if (PrototypeIterator::GetCurrent(iter)->IsJSProxy() || |
| 208 | PrototypeIterator::GetCurrent<JSObject>(iter) |
| 209 | ->HasIndexedInterceptor()) { |
| 210 | // Bail out if we find a proxy or interceptor, likely not worth |
| 211 | // collecting keys in that case. |
| 212 | return *isolate->factory()->NewNumberFromUint(length); |
| 213 | } |
| 214 | accumulator.NextPrototype(); |
| 215 | Handle<JSObject> current = PrototypeIterator::GetCurrent<JSObject>(iter); |
| 216 | JSObject::CollectOwnElementKeys(current, &accumulator, ALL_PROPERTIES); |
| 217 | } |
| 218 | // Erase any keys >= length. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 219 | Handle<FixedArray> keys = accumulator.GetKeys(KEEP_NUMBERS); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 220 | int j = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 221 | for (int i = 0; i < keys->length(); i++) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 222 | if (NumberToUint32(keys->get(i)) >= length) continue; |
| 223 | if (i != j) keys->set(j, keys->get(i)); |
| 224 | j++; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 225 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 226 | |
| 227 | if (j != keys->length()) { |
| 228 | isolate->heap()->RightTrimFixedArray<Heap::CONCURRENT_TO_SWEEPER>( |
| 229 | *keys, keys->length() - j); |
| 230 | } |
| 231 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 232 | return *isolate->factory()->NewJSArrayWithElements(keys); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 236 | namespace { |
| 237 | |
| 238 | Object* ArrayConstructorCommon(Isolate* isolate, Handle<JSFunction> constructor, |
| 239 | Handle<JSReceiver> new_target, |
| 240 | Handle<AllocationSite> site, |
| 241 | Arguments* caller_args) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 242 | Factory* factory = isolate->factory(); |
| 243 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 244 | // If called through new, new.target can be: |
| 245 | // - a subclass of constructor, |
| 246 | // - a proxy wrapper around constructor, or |
| 247 | // - the constructor itself. |
| 248 | // If called through Reflect.construct, it's guaranteed to be a constructor by |
| 249 | // REFLECT_CONSTRUCT_PREPARE. |
| 250 | DCHECK(new_target->IsConstructor()); |
| 251 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 252 | bool holey = false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 253 | bool can_use_type_feedback = !site.is_null(); |
| 254 | bool can_inline_array_constructor = true; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 255 | if (caller_args->length() == 1) { |
| 256 | Handle<Object> argument_one = caller_args->at<Object>(0); |
| 257 | if (argument_one->IsSmi()) { |
| 258 | int value = Handle<Smi>::cast(argument_one)->value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 259 | if (value < 0 || |
| 260 | JSArray::SetLengthWouldNormalize(isolate->heap(), value)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 261 | // the array is a dictionary in this case. |
| 262 | can_use_type_feedback = false; |
| 263 | } else if (value != 0) { |
| 264 | holey = true; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 265 | if (value >= JSArray::kInitialMaxFastElementArray) { |
| 266 | can_inline_array_constructor = false; |
| 267 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 268 | } |
| 269 | } else { |
| 270 | // Non-smi length argument produces a dictionary |
| 271 | can_use_type_feedback = false; |
| 272 | } |
| 273 | } |
| 274 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 275 | Handle<Map> initial_map; |
| 276 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 277 | isolate, initial_map, |
| 278 | JSFunction::GetDerivedMap(isolate, constructor, new_target)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 279 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 280 | ElementsKind to_kind = can_use_type_feedback ? site->GetElementsKind() |
| 281 | : initial_map->elements_kind(); |
| 282 | if (holey && !IsFastHoleyElementsKind(to_kind)) { |
| 283 | to_kind = GetHoleyElementsKind(to_kind); |
| 284 | // Update the allocation site info to reflect the advice alteration. |
| 285 | if (!site.is_null()) site->SetElementsKind(to_kind); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 286 | } |
| 287 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 288 | // We should allocate with an initial map that reflects the allocation site |
| 289 | // advice. Therefore we use AllocateJSObjectFromMap instead of passing |
| 290 | // the constructor. |
| 291 | if (to_kind != initial_map->elements_kind()) { |
| 292 | initial_map = Map::AsElementsKind(initial_map, to_kind); |
| 293 | } |
| 294 | |
| 295 | // If we don't care to track arrays of to_kind ElementsKind, then |
| 296 | // don't emit a memento for them. |
| 297 | Handle<AllocationSite> allocation_site; |
| 298 | if (AllocationSite::GetMode(to_kind) == TRACK_ALLOCATION_SITE) { |
| 299 | allocation_site = site; |
| 300 | } |
| 301 | |
| 302 | Handle<JSArray> array = Handle<JSArray>::cast( |
| 303 | factory->NewJSObjectFromMap(initial_map, NOT_TENURED, allocation_site)); |
| 304 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 305 | factory->NewJSArrayStorage(array, 0, 0, DONT_INITIALIZE_ARRAY_ELEMENTS); |
| 306 | |
| 307 | ElementsKind old_kind = array->GetElementsKind(); |
| 308 | RETURN_FAILURE_ON_EXCEPTION( |
| 309 | isolate, ArrayConstructInitializeElements(array, caller_args)); |
| 310 | if (!site.is_null() && |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 311 | (old_kind != array->GetElementsKind() || !can_use_type_feedback || |
| 312 | !can_inline_array_constructor)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 313 | // The arguments passed in caused a transition. This kind of complexity |
| 314 | // can't be dealt with in the inlined hydrogen array constructor case. |
| 315 | // We must mark the allocationsite as un-inlinable. |
| 316 | site->SetDoNotInlineCall(); |
| 317 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 318 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 319 | return *array; |
| 320 | } |
| 321 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 322 | } // namespace |
| 323 | |
| 324 | |
| 325 | RUNTIME_FUNCTION(Runtime_NewArray) { |
| 326 | HandleScope scope(isolate); |
| 327 | DCHECK_LE(3, args.length()); |
| 328 | int const argc = args.length() - 3; |
| 329 | // TODO(bmeurer): Remove this Arguments nonsense. |
| 330 | Arguments argv(argc, args.arguments() - 1); |
| 331 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, constructor, 0); |
| 332 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, new_target, argc + 1); |
| 333 | CONVERT_ARG_HANDLE_CHECKED(HeapObject, type_info, argc + 2); |
| 334 | // TODO(bmeurer): Use MaybeHandle to pass around the AllocationSite. |
| 335 | Handle<AllocationSite> site = type_info->IsAllocationSite() |
| 336 | ? Handle<AllocationSite>::cast(type_info) |
| 337 | : Handle<AllocationSite>::null(); |
| 338 | return ArrayConstructorCommon(isolate, constructor, new_target, site, &argv); |
| 339 | } |
| 340 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 341 | |
| 342 | RUNTIME_FUNCTION(Runtime_ArrayConstructor) { |
| 343 | HandleScope scope(isolate); |
| 344 | // If we get 2 arguments then they are the stub parameters (constructor, type |
| 345 | // info). If we get 4, then the first one is a pointer to the arguments |
| 346 | // passed by the caller, and the last one is the length of the arguments |
| 347 | // passed to the caller (redundant, but useful to check on the deoptimizer |
| 348 | // with an assert). |
| 349 | Arguments empty_args(0, NULL); |
| 350 | bool no_caller_args = args.length() == 2; |
| 351 | DCHECK(no_caller_args || args.length() == 4); |
| 352 | int parameters_start = no_caller_args ? 0 : 1; |
| 353 | Arguments* caller_args = |
| 354 | no_caller_args ? &empty_args : reinterpret_cast<Arguments*>(args[0]); |
| 355 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, constructor, parameters_start); |
| 356 | CONVERT_ARG_HANDLE_CHECKED(Object, type_info, parameters_start + 1); |
| 357 | #ifdef DEBUG |
| 358 | if (!no_caller_args) { |
| 359 | CONVERT_SMI_ARG_CHECKED(arg_count, parameters_start + 2); |
| 360 | DCHECK(arg_count == caller_args->length()); |
| 361 | } |
| 362 | #endif |
| 363 | |
| 364 | Handle<AllocationSite> site; |
| 365 | if (!type_info.is_null() && |
| 366 | *type_info != isolate->heap()->undefined_value()) { |
| 367 | site = Handle<AllocationSite>::cast(type_info); |
| 368 | DCHECK(!site->SitePointsToLiteral()); |
| 369 | } |
| 370 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 371 | return ArrayConstructorCommon(isolate, constructor, constructor, site, |
| 372 | caller_args); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 373 | } |
| 374 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 375 | RUNTIME_FUNCTION(Runtime_InternalArrayConstructor) { |
| 376 | HandleScope scope(isolate); |
| 377 | Arguments empty_args(0, NULL); |
| 378 | bool no_caller_args = args.length() == 1; |
| 379 | DCHECK(no_caller_args || args.length() == 3); |
| 380 | int parameters_start = no_caller_args ? 0 : 1; |
| 381 | Arguments* caller_args = |
| 382 | no_caller_args ? &empty_args : reinterpret_cast<Arguments*>(args[0]); |
| 383 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, constructor, parameters_start); |
| 384 | #ifdef DEBUG |
| 385 | if (!no_caller_args) { |
| 386 | CONVERT_SMI_ARG_CHECKED(arg_count, parameters_start + 1); |
| 387 | DCHECK(arg_count == caller_args->length()); |
| 388 | } |
| 389 | #endif |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 390 | return ArrayConstructorCommon(isolate, constructor, constructor, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 391 | Handle<AllocationSite>::null(), caller_args); |
| 392 | } |
| 393 | |
| 394 | |
| 395 | RUNTIME_FUNCTION(Runtime_NormalizeElements) { |
| 396 | HandleScope scope(isolate); |
| 397 | DCHECK(args.length() == 1); |
| 398 | CONVERT_ARG_HANDLE_CHECKED(JSObject, array, 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 399 | RUNTIME_ASSERT(!array->HasFixedTypedArrayElements() && |
| 400 | !array->IsJSGlobalProxy()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 401 | JSObject::NormalizeElements(array); |
| 402 | return *array; |
| 403 | } |
| 404 | |
| 405 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 406 | // GrowArrayElements returns a sentinel Smi if the object was normalized. |
| 407 | RUNTIME_FUNCTION(Runtime_GrowArrayElements) { |
| 408 | HandleScope scope(isolate); |
| 409 | DCHECK(args.length() == 2); |
| 410 | CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0); |
| 411 | CONVERT_NUMBER_CHECKED(int, key, Int32, args[1]); |
| 412 | |
| 413 | if (key < 0) { |
| 414 | return object->elements(); |
| 415 | } |
| 416 | |
| 417 | uint32_t capacity = static_cast<uint32_t>(object->elements()->length()); |
| 418 | uint32_t index = static_cast<uint32_t>(key); |
| 419 | |
| 420 | if (index >= capacity) { |
| 421 | if (object->WouldConvertToSlowElements(index)) { |
| 422 | // We don't want to allow operations that cause lazy deopt. Return a Smi |
| 423 | // as a signal that optimized code should eagerly deoptimize. |
| 424 | return Smi::FromInt(0); |
| 425 | } |
| 426 | |
| 427 | uint32_t new_capacity = JSObject::NewElementsCapacity(index + 1); |
| 428 | object->GetElementsAccessor()->GrowCapacityAndConvert(object, new_capacity); |
| 429 | } |
| 430 | |
| 431 | // On success, return the fixed array elements. |
| 432 | return object->elements(); |
| 433 | } |
| 434 | |
| 435 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 436 | RUNTIME_FUNCTION(Runtime_HasComplexElements) { |
| 437 | HandleScope scope(isolate); |
| 438 | DCHECK(args.length() == 1); |
| 439 | CONVERT_ARG_HANDLE_CHECKED(JSObject, array, 0); |
| 440 | for (PrototypeIterator iter(isolate, array, |
| 441 | PrototypeIterator::START_AT_RECEIVER); |
| 442 | !iter.IsAtEnd(); iter.Advance()) { |
| 443 | if (PrototypeIterator::GetCurrent(iter)->IsJSProxy()) { |
| 444 | return isolate->heap()->true_value(); |
| 445 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 446 | Handle<JSObject> current = PrototypeIterator::GetCurrent<JSObject>(iter); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 447 | if (current->HasIndexedInterceptor()) { |
| 448 | return isolate->heap()->true_value(); |
| 449 | } |
| 450 | if (!current->HasDictionaryElements()) continue; |
| 451 | if (current->element_dictionary()->HasComplexElements()) { |
| 452 | return isolate->heap()->true_value(); |
| 453 | } |
| 454 | } |
| 455 | return isolate->heap()->false_value(); |
| 456 | } |
| 457 | |
| 458 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 459 | RUNTIME_FUNCTION(Runtime_IsArray) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 460 | SealHandleScope shs(isolate); |
| 461 | DCHECK(args.length() == 1); |
| 462 | CONVERT_ARG_CHECKED(Object, obj, 0); |
| 463 | return isolate->heap()->ToBoolean(obj->IsJSArray()); |
| 464 | } |
| 465 | |
| 466 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 467 | RUNTIME_FUNCTION(Runtime_HasCachedArrayIndex) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 468 | SealHandleScope shs(isolate); |
| 469 | DCHECK(args.length() == 1); |
| 470 | return isolate->heap()->false_value(); |
| 471 | } |
| 472 | |
| 473 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 474 | RUNTIME_FUNCTION(Runtime_GetCachedArrayIndex) { |
| 475 | // This can never be reached, because Runtime_HasCachedArrayIndex always |
| 476 | // returns false. |
| 477 | UNIMPLEMENTED(); |
| 478 | return nullptr; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 482 | RUNTIME_FUNCTION(Runtime_ArraySpeciesConstructor) { |
| 483 | HandleScope scope(isolate); |
| 484 | DCHECK(args.length() == 1); |
| 485 | CONVERT_ARG_HANDLE_CHECKED(Object, original_array, 0); |
| 486 | Handle<Object> constructor; |
| 487 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 488 | isolate, constructor, |
| 489 | Object::ArraySpeciesConstructor(isolate, original_array)); |
| 490 | return *constructor; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 491 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 492 | |
| 493 | } // namespace internal |
| 494 | } // namespace v8 |