Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "api.h" |
| 31 | #include "arguments.h" |
| 32 | #include "bootstrapper.h" |
| 33 | #include "builtins.h" |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 34 | #include "gdb-jit.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 35 | #include "ic-inl.h" |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 36 | #include "heap-profiler.h" |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 37 | #include "mark-compact.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 38 | #include "vm-state-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 39 | |
| 40 | namespace v8 { |
| 41 | namespace internal { |
| 42 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 43 | namespace { |
| 44 | |
| 45 | // Arguments object passed to C++ builtins. |
| 46 | template <BuiltinExtraArguments extra_args> |
| 47 | class BuiltinArguments : public Arguments { |
| 48 | public: |
| 49 | BuiltinArguments(int length, Object** arguments) |
| 50 | : Arguments(length, arguments) { } |
| 51 | |
| 52 | Object*& operator[] (int index) { |
| 53 | ASSERT(index < length()); |
| 54 | return Arguments::operator[](index); |
| 55 | } |
| 56 | |
| 57 | template <class S> Handle<S> at(int index) { |
| 58 | ASSERT(index < length()); |
| 59 | return Arguments::at<S>(index); |
| 60 | } |
| 61 | |
| 62 | Handle<Object> receiver() { |
| 63 | return Arguments::at<Object>(0); |
| 64 | } |
| 65 | |
| 66 | Handle<JSFunction> called_function() { |
| 67 | STATIC_ASSERT(extra_args == NEEDS_CALLED_FUNCTION); |
| 68 | return Arguments::at<JSFunction>(Arguments::length() - 1); |
| 69 | } |
| 70 | |
| 71 | // Gets the total number of arguments including the receiver (but |
| 72 | // excluding extra arguments). |
| 73 | int length() const { |
| 74 | STATIC_ASSERT(extra_args == NO_EXTRA_ARGUMENTS); |
| 75 | return Arguments::length(); |
| 76 | } |
| 77 | |
| 78 | #ifdef DEBUG |
| 79 | void Verify() { |
| 80 | // Check we have at least the receiver. |
| 81 | ASSERT(Arguments::length() >= 1); |
| 82 | } |
| 83 | #endif |
| 84 | }; |
| 85 | |
| 86 | |
| 87 | // Specialize BuiltinArguments for the called function extra argument. |
| 88 | |
| 89 | template <> |
| 90 | int BuiltinArguments<NEEDS_CALLED_FUNCTION>::length() const { |
| 91 | return Arguments::length() - 1; |
| 92 | } |
| 93 | |
| 94 | #ifdef DEBUG |
| 95 | template <> |
| 96 | void BuiltinArguments<NEEDS_CALLED_FUNCTION>::Verify() { |
| 97 | // Check we have at least the receiver and the called function. |
| 98 | ASSERT(Arguments::length() >= 2); |
| 99 | // Make sure cast to JSFunction succeeds. |
| 100 | called_function(); |
| 101 | } |
| 102 | #endif |
| 103 | |
| 104 | |
| 105 | #define DEF_ARG_TYPE(name, spec) \ |
| 106 | typedef BuiltinArguments<spec> name##ArgumentsType; |
| 107 | BUILTIN_LIST_C(DEF_ARG_TYPE) |
| 108 | #undef DEF_ARG_TYPE |
| 109 | |
| 110 | } // namespace |
| 111 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 112 | // ---------------------------------------------------------------------------- |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 113 | // Support macro for defining builtins in C++. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 114 | // ---------------------------------------------------------------------------- |
| 115 | // |
| 116 | // A builtin function is defined by writing: |
| 117 | // |
| 118 | // BUILTIN(name) { |
| 119 | // ... |
| 120 | // } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 121 | // |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 122 | // In the body of the builtin function the arguments can be accessed |
| 123 | // through the BuiltinArguments object args. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 124 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 125 | #ifdef DEBUG |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 126 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 127 | #define BUILTIN(name) \ |
| 128 | MUST_USE_RESULT static MaybeObject* Builtin_Impl_##name( \ |
| 129 | name##ArgumentsType args, Isolate* isolate); \ |
| 130 | MUST_USE_RESULT static MaybeObject* Builtin_##name( \ |
| 131 | name##ArgumentsType args, Isolate* isolate) { \ |
| 132 | ASSERT(isolate == Isolate::Current()); \ |
| 133 | args.Verify(); \ |
| 134 | return Builtin_Impl_##name(args, isolate); \ |
| 135 | } \ |
| 136 | MUST_USE_RESULT static MaybeObject* Builtin_Impl_##name( \ |
| 137 | name##ArgumentsType args, Isolate* isolate) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 138 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 139 | #else // For release mode. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 140 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 141 | #define BUILTIN(name) \ |
| 142 | static MaybeObject* Builtin_##name(name##ArgumentsType args, Isolate* isolate) |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 143 | |
| 144 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 145 | |
| 146 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 147 | static inline bool CalledAsConstructor(Isolate* isolate) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 148 | #ifdef DEBUG |
| 149 | // Calculate the result using a full stack frame iterator and check |
| 150 | // that the state of the stack is as we assume it to be in the |
| 151 | // code below. |
| 152 | StackFrameIterator it; |
| 153 | ASSERT(it.frame()->is_exit()); |
| 154 | it.Advance(); |
| 155 | StackFrame* frame = it.frame(); |
| 156 | bool reference_result = frame->is_construct(); |
| 157 | #endif |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 158 | Address fp = Isolate::c_entry_fp(isolate->thread_local_top()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 159 | // Because we know fp points to an exit frame we can use the relevant |
| 160 | // part of ExitFrame::ComputeCallerState directly. |
| 161 | const int kCallerOffset = ExitFrameConstants::kCallerFPOffset; |
| 162 | Address caller_fp = Memory::Address_at(fp + kCallerOffset); |
| 163 | // This inlines the part of StackFrame::ComputeType that grabs the |
| 164 | // type of the current frame. Note that StackFrame::ComputeType |
| 165 | // has been specialized for each architecture so if any one of them |
| 166 | // changes this code has to be changed as well. |
| 167 | const int kMarkerOffset = StandardFrameConstants::kMarkerOffset; |
| 168 | const Smi* kConstructMarker = Smi::FromInt(StackFrame::CONSTRUCT); |
| 169 | Object* marker = Memory::Object_at(caller_fp + kMarkerOffset); |
| 170 | bool result = (marker == kConstructMarker); |
| 171 | ASSERT_EQ(result, reference_result); |
| 172 | return result; |
| 173 | } |
| 174 | |
| 175 | // ---------------------------------------------------------------------------- |
| 176 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 177 | BUILTIN(Illegal) { |
| 178 | UNREACHABLE(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 179 | return isolate->heap()->undefined_value(); // Make compiler happy. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 180 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 181 | |
| 182 | |
| 183 | BUILTIN(EmptyFunction) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 184 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 185 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 186 | |
| 187 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 188 | static MaybeObject* ArrayCodeGenericCommon(Arguments* args, |
| 189 | Isolate* isolate, |
| 190 | JSFunction* constructor) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 191 | Heap* heap = isolate->heap(); |
| 192 | isolate->counters()->array_function_runtime()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 193 | |
| 194 | JSArray* array; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 195 | if (CalledAsConstructor(isolate)) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 196 | array = JSArray::cast((*args)[0]); |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 197 | // Initialize elements and length in case later allocations fail so that the |
| 198 | // array object is initialized in a valid state. |
| 199 | array->set_length(Smi::FromInt(0)); |
| 200 | array->set_elements(heap->empty_fixed_array()); |
| 201 | if (!FLAG_smi_only_arrays) { |
| 202 | Context* global_context = isolate->context()->global_context(); |
| 203 | if (array->GetElementsKind() == FAST_SMI_ONLY_ELEMENTS && |
| 204 | !global_context->object_js_array_map()->IsUndefined()) { |
| 205 | array->set_map(Map::cast(global_context->object_js_array_map())); |
| 206 | } |
| 207 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 208 | } else { |
| 209 | // Allocate the JS Array |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 210 | MaybeObject* maybe_obj = heap->AllocateJSObject(constructor); |
| 211 | if (!maybe_obj->To(&array)) return maybe_obj; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 214 | // Optimize the case where there is one argument and the argument is a |
| 215 | // small smi. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 216 | if (args->length() == 2) { |
| 217 | Object* obj = (*args)[1]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 218 | if (obj->IsSmi()) { |
| 219 | int len = Smi::cast(obj)->value(); |
| 220 | if (len >= 0 && len < JSObject::kInitialMaxFastElementArray) { |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 221 | Object* fixed_array; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 222 | { MaybeObject* maybe_obj = heap->AllocateFixedArrayWithHoles(len); |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 223 | if (!maybe_obj->ToObject(&fixed_array)) return maybe_obj; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 224 | } |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 225 | // We do not use SetContent to skip the unnecessary elements type check. |
| 226 | array->set_elements(FixedArray::cast(fixed_array)); |
| 227 | array->set_length(Smi::cast(obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 228 | return array; |
| 229 | } |
| 230 | } |
| 231 | // Take the argument as the length. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 232 | { MaybeObject* maybe_obj = array->Initialize(0); |
| 233 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 234 | } |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 235 | return array->SetElementsLength((*args)[1]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // Optimize the case where there are no parameters passed. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 239 | if (args->length() == 1) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 240 | return array->Initialize(JSArray::kPreallocatedArrayElements); |
| 241 | } |
| 242 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 243 | // Set length and elements on the array. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 244 | int number_of_elements = args->length() - 1; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 245 | MaybeObject* maybe_object = |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 246 | array->EnsureCanContainElements(args, 1, number_of_elements, |
| 247 | ALLOW_CONVERTED_DOUBLE_ELEMENTS); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 248 | if (maybe_object->IsFailure()) return maybe_object; |
| 249 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 250 | // Allocate an appropriately typed elements array. |
| 251 | MaybeObject* maybe_elms; |
| 252 | ElementsKind elements_kind = array->GetElementsKind(); |
| 253 | if (elements_kind == FAST_DOUBLE_ELEMENTS) { |
| 254 | maybe_elms = heap->AllocateUninitializedFixedDoubleArray( |
| 255 | number_of_elements); |
| 256 | } else { |
| 257 | maybe_elms = heap->AllocateFixedArrayWithHoles(number_of_elements); |
| 258 | } |
| 259 | FixedArrayBase* elms; |
| 260 | if (!maybe_elms->To<FixedArrayBase>(&elms)) return maybe_elms; |
| 261 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 262 | // Fill in the content |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 263 | switch (array->GetElementsKind()) { |
| 264 | case FAST_SMI_ONLY_ELEMENTS: { |
| 265 | FixedArray* smi_elms = FixedArray::cast(elms); |
| 266 | for (int index = 0; index < number_of_elements; index++) { |
| 267 | smi_elms->set(index, (*args)[index+1], SKIP_WRITE_BARRIER); |
| 268 | } |
| 269 | break; |
| 270 | } |
| 271 | case FAST_ELEMENTS: { |
| 272 | AssertNoAllocation no_gc; |
| 273 | WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc); |
| 274 | FixedArray* object_elms = FixedArray::cast(elms); |
| 275 | for (int index = 0; index < number_of_elements; index++) { |
| 276 | object_elms->set(index, (*args)[index+1], mode); |
| 277 | } |
| 278 | break; |
| 279 | } |
| 280 | case FAST_DOUBLE_ELEMENTS: { |
| 281 | FixedDoubleArray* double_elms = FixedDoubleArray::cast(elms); |
| 282 | for (int index = 0; index < number_of_elements; index++) { |
| 283 | double_elms->set(index, (*args)[index+1]->Number()); |
| 284 | } |
| 285 | break; |
| 286 | } |
| 287 | default: |
| 288 | UNREACHABLE(); |
| 289 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 292 | array->set_elements(elms); |
| 293 | array->set_length(Smi::FromInt(number_of_elements)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 294 | return array; |
| 295 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 296 | |
| 297 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 298 | BUILTIN(InternalArrayCodeGeneric) { |
| 299 | return ArrayCodeGenericCommon( |
| 300 | &args, |
| 301 | isolate, |
| 302 | isolate->context()->global_context()->internal_array_function()); |
| 303 | } |
| 304 | |
| 305 | |
| 306 | BUILTIN(ArrayCodeGeneric) { |
| 307 | return ArrayCodeGenericCommon( |
| 308 | &args, |
| 309 | isolate, |
| 310 | isolate->context()->global_context()->array_function()); |
| 311 | } |
| 312 | |
| 313 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 314 | static void MoveElements(Heap* heap, |
| 315 | AssertNoAllocation* no_gc, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 316 | FixedArray* dst, |
| 317 | int dst_index, |
| 318 | FixedArray* src, |
| 319 | int src_index, |
| 320 | int len) { |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 321 | if (len == 0) return; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 322 | ASSERT(dst->map() != HEAP->fixed_cow_array_map()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 323 | memmove(dst->data_start() + dst_index, |
| 324 | src->data_start() + src_index, |
| 325 | len * kPointerSize); |
| 326 | WriteBarrierMode mode = dst->GetWriteBarrierMode(*no_gc); |
| 327 | if (mode == UPDATE_WRITE_BARRIER) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 328 | heap->RecordWrites(dst->address(), dst->OffsetOfElementAt(dst_index), len); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 329 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 330 | heap->incremental_marking()->RecordWrites(dst); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 334 | static void FillWithHoles(Heap* heap, FixedArray* dst, int from, int to) { |
| 335 | ASSERT(dst->map() != heap->fixed_cow_array_map()); |
| 336 | MemsetPointer(dst->data_start() + from, heap->the_hole_value(), to - from); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 340 | static FixedArray* LeftTrimFixedArray(Heap* heap, |
| 341 | FixedArray* elms, |
| 342 | int to_trim) { |
| 343 | ASSERT(elms->map() != HEAP->fixed_cow_array_map()); |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 344 | // For now this trick is only applied to fixed arrays in new and paged space. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 345 | // In large object space the object's start must coincide with chunk |
| 346 | // and thus the trick is just not applicable. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 347 | ASSERT(!HEAP->lo_space()->Contains(elms)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 348 | |
| 349 | STATIC_ASSERT(FixedArray::kMapOffset == 0); |
| 350 | STATIC_ASSERT(FixedArray::kLengthOffset == kPointerSize); |
| 351 | STATIC_ASSERT(FixedArray::kHeaderSize == 2 * kPointerSize); |
| 352 | |
| 353 | Object** former_start = HeapObject::RawField(elms, 0); |
| 354 | |
| 355 | const int len = elms->length(); |
| 356 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 357 | if (to_trim > FixedArray::kHeaderSize / kPointerSize && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 358 | !heap->new_space()->Contains(elms)) { |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 359 | // If we are doing a big trim in old space then we zap the space that was |
| 360 | // formerly part of the array so that the GC (aided by the card-based |
| 361 | // remembered set) won't find pointers to new-space there. |
| 362 | Object** zap = reinterpret_cast<Object**>(elms->address()); |
| 363 | zap++; // Header of filler must be at least one word so skip that. |
| 364 | for (int i = 1; i < to_trim; i++) { |
| 365 | *zap++ = Smi::FromInt(0); |
| 366 | } |
| 367 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 368 | // Technically in new space this write might be omitted (except for |
| 369 | // debug mode which iterates through the heap), but to play safer |
| 370 | // we still do it. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 371 | heap->CreateFillerObjectAt(elms->address(), to_trim * kPointerSize); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 372 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 373 | former_start[to_trim] = heap->fixed_array_map(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 374 | former_start[to_trim + 1] = Smi::FromInt(len - to_trim); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 375 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 376 | // Maintain marking consistency for HeapObjectIterator and |
| 377 | // IncrementalMarking. |
| 378 | int size_delta = to_trim * kPointerSize; |
| 379 | if (heap->marking()->TransferMark(elms->address(), |
| 380 | elms->address() + size_delta)) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 381 | MemoryChunk::IncrementLiveBytesFromMutator(elms->address(), -size_delta); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 384 | HEAP_PROFILE(heap, ObjectMoveEvent(elms->address(), |
| 385 | elms->address() + size_delta)); |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 386 | return FixedArray::cast(HeapObject::FromAddress( |
| 387 | elms->address() + to_trim * kPointerSize)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 391 | static bool ArrayPrototypeHasNoElements(Heap* heap, |
| 392 | Context* global_context, |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 393 | JSObject* array_proto) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 394 | // This method depends on non writability of Object and Array prototype |
| 395 | // fields. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 396 | if (array_proto->elements() != heap->empty_fixed_array()) return false; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 397 | // Object.prototype |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 398 | Object* proto = array_proto->GetPrototype(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 399 | if (proto == heap->null_value()) return false; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 400 | array_proto = JSObject::cast(proto); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 401 | if (array_proto != global_context->initial_object_prototype()) return false; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 402 | if (array_proto->elements() != heap->empty_fixed_array()) return false; |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 403 | return array_proto->GetPrototype()->IsNull(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 407 | MUST_USE_RESULT |
| 408 | static inline MaybeObject* EnsureJSArrayWithWritableFastElements( |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 409 | Heap* heap, Object* receiver, Arguments* args, int first_added_arg) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 410 | if (!receiver->IsJSArray()) return NULL; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 411 | JSArray* array = JSArray::cast(receiver); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 412 | HeapObject* elms = array->elements(); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 413 | Map* map = elms->map(); |
| 414 | if (map == heap->fixed_array_map()) { |
| 415 | if (args == NULL || !array->HasFastSmiOnlyElements()) { |
| 416 | return elms; |
| 417 | } |
| 418 | } else if (map == heap->fixed_cow_array_map()) { |
| 419 | MaybeObject* maybe_writable_result = array->EnsureWritableFastElements(); |
| 420 | if (args == NULL || !array->HasFastSmiOnlyElements() || |
| 421 | maybe_writable_result->IsFailure()) { |
| 422 | return maybe_writable_result; |
| 423 | } |
| 424 | } else { |
| 425 | return NULL; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 426 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 427 | |
| 428 | // Need to ensure that the arguments passed in args can be contained in |
| 429 | // the array. |
| 430 | int args_length = args->length(); |
| 431 | if (first_added_arg >= args_length) return array->elements(); |
| 432 | |
| 433 | MaybeObject* maybe_array = array->EnsureCanContainElements( |
| 434 | args, |
| 435 | first_added_arg, |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 436 | args_length - first_added_arg, |
| 437 | DONT_ALLOW_DOUBLE_ELEMENTS); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 438 | if (maybe_array->IsFailure()) return maybe_array; |
| 439 | return array->elements(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 443 | static inline bool IsJSArrayFastElementMovingAllowed(Heap* heap, |
| 444 | JSArray* receiver) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 445 | if (!FLAG_clever_optimizations) return false; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 446 | Context* global_context = heap->isolate()->context()->global_context(); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 447 | JSObject* array_proto = |
| 448 | JSObject::cast(global_context->array_function()->prototype()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 449 | return receiver->GetPrototype() == array_proto && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 450 | ArrayPrototypeHasNoElements(heap, global_context, array_proto); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 454 | MUST_USE_RESULT static MaybeObject* CallJsBuiltin( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 455 | Isolate* isolate, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 456 | const char* name, |
| 457 | BuiltinArguments<NO_EXTRA_ARGUMENTS> args) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 458 | HandleScope handleScope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 459 | |
| 460 | Handle<Object> js_builtin = |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 461 | GetProperty(Handle<JSObject>(isolate->global_context()->builtins()), |
| 462 | name); |
| 463 | Handle<JSFunction> function = Handle<JSFunction>::cast(js_builtin); |
| 464 | int argc = args.length() - 1; |
| 465 | ScopedVector<Handle<Object> > argv(argc); |
| 466 | for (int i = 0; i < argc; ++i) { |
| 467 | argv[i] = args.at<Object>(i + 1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 468 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 469 | bool pending_exception; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 470 | Handle<Object> result = Execution::Call(function, |
| 471 | args.receiver(), |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 472 | argc, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 473 | argv.start(), |
| 474 | &pending_exception); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 475 | if (pending_exception) return Failure::Exception(); |
| 476 | return *result; |
| 477 | } |
| 478 | |
| 479 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 480 | BUILTIN(ArrayPush) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 481 | Heap* heap = isolate->heap(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 482 | Object* receiver = *args.receiver(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 483 | Object* elms_obj; |
| 484 | { MaybeObject* maybe_elms_obj = |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 485 | EnsureJSArrayWithWritableFastElements(heap, receiver, &args, 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 486 | if (maybe_elms_obj == NULL) { |
| 487 | return CallJsBuiltin(isolate, "ArrayPush", args); |
| 488 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 489 | if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj; |
| 490 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 491 | FixedArray* elms = FixedArray::cast(elms_obj); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 492 | JSArray* array = JSArray::cast(receiver); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 493 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 494 | int len = Smi::cast(array->length())->value(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 495 | int to_add = args.length() - 1; |
| 496 | if (to_add == 0) { |
| 497 | return Smi::FromInt(len); |
| 498 | } |
| 499 | // Currently fixed arrays cannot grow too big, so |
| 500 | // we should never hit this case. |
| 501 | ASSERT(to_add <= (Smi::kMaxValue - len)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 502 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 503 | int new_length = len + to_add; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 504 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 505 | if (new_length > elms->length()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 506 | // New backing storage is needed. |
| 507 | int capacity = new_length + (new_length >> 1) + 16; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 508 | Object* obj; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 509 | { MaybeObject* maybe_obj = heap->AllocateUninitializedFixedArray(capacity); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 510 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 511 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 512 | FixedArray* new_elms = FixedArray::cast(obj); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 513 | |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 514 | CopyObjectToObjectElements(elms, FAST_ELEMENTS, 0, |
| 515 | new_elms, FAST_ELEMENTS, 0, len); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 516 | FillWithHoles(heap, new_elms, new_length, capacity); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 517 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 518 | elms = new_elms; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 519 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 520 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 521 | // Add the provided values. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 522 | AssertNoAllocation no_gc; |
| 523 | WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 524 | for (int index = 0; index < to_add; index++) { |
| 525 | elms->set(index + len, args[index + 1], mode); |
| 526 | } |
| 527 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 528 | if (elms != array->elements()) { |
| 529 | array->set_elements(elms); |
| 530 | } |
| 531 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 532 | // Set the length. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 533 | array->set_length(Smi::FromInt(new_length)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 534 | return Smi::FromInt(new_length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 535 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 536 | |
| 537 | |
| 538 | BUILTIN(ArrayPop) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 539 | Heap* heap = isolate->heap(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 540 | Object* receiver = *args.receiver(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 541 | Object* elms_obj; |
| 542 | { MaybeObject* maybe_elms_obj = |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 543 | EnsureJSArrayWithWritableFastElements(heap, receiver, NULL, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 544 | if (maybe_elms_obj == NULL) return CallJsBuiltin(isolate, "ArrayPop", args); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 545 | if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj; |
| 546 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 547 | FixedArray* elms = FixedArray::cast(elms_obj); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 548 | JSArray* array = JSArray::cast(receiver); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 549 | |
| 550 | int len = Smi::cast(array->length())->value(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 551 | if (len == 0) return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 552 | |
| 553 | // Get top element |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 554 | MaybeObject* top = elms->get(len - 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 555 | |
| 556 | // Set the length. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 557 | array->set_length(Smi::FromInt(len - 1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 558 | |
| 559 | if (!top->IsTheHole()) { |
| 560 | // Delete the top element. |
| 561 | elms->set_the_hole(len - 1); |
| 562 | return top; |
| 563 | } |
| 564 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 565 | top = array->GetPrototype()->GetElement(len - 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 566 | |
| 567 | return top; |
| 568 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 569 | |
| 570 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 571 | BUILTIN(ArrayShift) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 572 | Heap* heap = isolate->heap(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 573 | Object* receiver = *args.receiver(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 574 | Object* elms_obj; |
| 575 | { MaybeObject* maybe_elms_obj = |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 576 | EnsureJSArrayWithWritableFastElements(heap, receiver, NULL, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 577 | if (maybe_elms_obj == NULL) |
| 578 | return CallJsBuiltin(isolate, "ArrayShift", args); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 579 | if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj; |
| 580 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 581 | if (!IsJSArrayFastElementMovingAllowed(heap, JSArray::cast(receiver))) { |
| 582 | return CallJsBuiltin(isolate, "ArrayShift", args); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 583 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 584 | FixedArray* elms = FixedArray::cast(elms_obj); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 585 | JSArray* array = JSArray::cast(receiver); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 586 | ASSERT(array->HasFastTypeElements()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 587 | |
| 588 | int len = Smi::cast(array->length())->value(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 589 | if (len == 0) return heap->undefined_value(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 590 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 591 | // Get first element |
| 592 | Object* first = elms->get(0); |
| 593 | if (first->IsTheHole()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 594 | first = heap->undefined_value(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 597 | if (!heap->lo_space()->Contains(elms)) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 598 | array->set_elements(LeftTrimFixedArray(heap, elms, 1)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 599 | } else { |
| 600 | // Shift the elements. |
| 601 | AssertNoAllocation no_gc; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 602 | MoveElements(heap, &no_gc, elms, 0, elms, 1, len - 1); |
| 603 | elms->set(len - 1, heap->the_hole_value()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 604 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 605 | |
| 606 | // Set the length. |
| 607 | array->set_length(Smi::FromInt(len - 1)); |
| 608 | |
| 609 | return first; |
| 610 | } |
| 611 | |
| 612 | |
| 613 | BUILTIN(ArrayUnshift) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 614 | Heap* heap = isolate->heap(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 615 | Object* receiver = *args.receiver(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 616 | Object* elms_obj; |
| 617 | { MaybeObject* maybe_elms_obj = |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 618 | EnsureJSArrayWithWritableFastElements(heap, receiver, NULL, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 619 | if (maybe_elms_obj == NULL) |
| 620 | return CallJsBuiltin(isolate, "ArrayUnshift", args); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 621 | if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj; |
| 622 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 623 | if (!IsJSArrayFastElementMovingAllowed(heap, JSArray::cast(receiver))) { |
| 624 | return CallJsBuiltin(isolate, "ArrayUnshift", args); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 625 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 626 | FixedArray* elms = FixedArray::cast(elms_obj); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 627 | JSArray* array = JSArray::cast(receiver); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 628 | ASSERT(array->HasFastTypeElements()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 629 | |
| 630 | int len = Smi::cast(array->length())->value(); |
| 631 | int to_add = args.length() - 1; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 632 | int new_length = len + to_add; |
| 633 | // Currently fixed arrays cannot grow too big, so |
| 634 | // we should never hit this case. |
| 635 | ASSERT(to_add <= (Smi::kMaxValue - len)); |
| 636 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 637 | MaybeObject* maybe_object = |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 638 | array->EnsureCanContainElements(&args, 1, to_add, |
| 639 | DONT_ALLOW_DOUBLE_ELEMENTS); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 640 | if (maybe_object->IsFailure()) return maybe_object; |
| 641 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 642 | if (new_length > elms->length()) { |
| 643 | // New backing storage is needed. |
| 644 | int capacity = new_length + (new_length >> 1) + 16; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 645 | Object* obj; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 646 | { MaybeObject* maybe_obj = heap->AllocateUninitializedFixedArray(capacity); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 647 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 648 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 649 | FixedArray* new_elms = FixedArray::cast(obj); |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 650 | CopyObjectToObjectElements(elms, FAST_ELEMENTS, 0, |
| 651 | new_elms, FAST_ELEMENTS, to_add, len); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 652 | FillWithHoles(heap, new_elms, new_length, capacity); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 653 | elms = new_elms; |
| 654 | array->set_elements(elms); |
| 655 | } else { |
| 656 | AssertNoAllocation no_gc; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 657 | MoveElements(heap, &no_gc, elms, to_add, elms, 0, len); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | // Add the provided values. |
| 661 | AssertNoAllocation no_gc; |
| 662 | WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc); |
| 663 | for (int i = 0; i < to_add; i++) { |
| 664 | elms->set(i, args[i + 1], mode); |
| 665 | } |
| 666 | |
| 667 | // Set the length. |
| 668 | array->set_length(Smi::FromInt(new_length)); |
| 669 | return Smi::FromInt(new_length); |
| 670 | } |
| 671 | |
| 672 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 673 | BUILTIN(ArraySlice) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 674 | Heap* heap = isolate->heap(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 675 | Object* receiver = *args.receiver(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 676 | FixedArray* elms; |
| 677 | int len = -1; |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 678 | if (receiver->IsJSArray()) { |
| 679 | JSArray* array = JSArray::cast(receiver); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 680 | if (!array->HasFastTypeElements() || |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 681 | !IsJSArrayFastElementMovingAllowed(heap, array)) { |
| 682 | return CallJsBuiltin(isolate, "ArraySlice", args); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 683 | } |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 684 | |
| 685 | elms = FixedArray::cast(array->elements()); |
| 686 | len = Smi::cast(array->length())->value(); |
| 687 | } else { |
| 688 | // Array.slice(arguments, ...) is quite a common idiom (notably more |
| 689 | // than 50% of invocations in Web apps). Treat it in C++ as well. |
| 690 | Map* arguments_map = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 691 | isolate->context()->global_context()->arguments_boilerplate()->map(); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 692 | |
| 693 | bool is_arguments_object_with_fast_elements = |
| 694 | receiver->IsJSObject() |
| 695 | && JSObject::cast(receiver)->map() == arguments_map |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 696 | && JSObject::cast(receiver)->HasFastTypeElements(); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 697 | if (!is_arguments_object_with_fast_elements) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 698 | return CallJsBuiltin(isolate, "ArraySlice", args); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 699 | } |
| 700 | elms = FixedArray::cast(JSObject::cast(receiver)->elements()); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 701 | Object* len_obj = JSObject::cast(receiver) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 702 | ->InObjectPropertyAt(Heap::kArgumentsLengthIndex); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 703 | if (!len_obj->IsSmi()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 704 | return CallJsBuiltin(isolate, "ArraySlice", args); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 705 | } |
| 706 | len = Smi::cast(len_obj)->value(); |
| 707 | if (len > elms->length()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 708 | return CallJsBuiltin(isolate, "ArraySlice", args); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 709 | } |
| 710 | for (int i = 0; i < len; i++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 711 | if (elms->get(i) == heap->the_hole_value()) { |
| 712 | return CallJsBuiltin(isolate, "ArraySlice", args); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 713 | } |
| 714 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 715 | } |
| 716 | ASSERT(len >= 0); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 717 | int n_arguments = args.length() - 1; |
| 718 | |
| 719 | // Note carefully choosen defaults---if argument is missing, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 720 | // it's undefined which gets converted to 0 for relative_start |
| 721 | // and to len for relative_end. |
| 722 | int relative_start = 0; |
| 723 | int relative_end = len; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 724 | if (n_arguments > 0) { |
| 725 | Object* arg1 = args[1]; |
| 726 | if (arg1->IsSmi()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 727 | relative_start = Smi::cast(arg1)->value(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 728 | } else if (!arg1->IsUndefined()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 729 | return CallJsBuiltin(isolate, "ArraySlice", args); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 730 | } |
| 731 | if (n_arguments > 1) { |
| 732 | Object* arg2 = args[2]; |
| 733 | if (arg2->IsSmi()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 734 | relative_end = Smi::cast(arg2)->value(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 735 | } else if (!arg2->IsUndefined()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 736 | return CallJsBuiltin(isolate, "ArraySlice", args); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | // ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 6. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 742 | int k = (relative_start < 0) ? Max(len + relative_start, 0) |
| 743 | : Min(relative_start, len); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 744 | |
| 745 | // ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 8. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 746 | int final = (relative_end < 0) ? Max(len + relative_end, 0) |
| 747 | : Min(relative_end, len); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 748 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 749 | ElementsKind elements_kind = JSObject::cast(receiver)->GetElementsKind(); |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 750 | |
| 751 | // Calculate the length of result array. |
| 752 | int result_len = Max(final - k, 0); |
| 753 | |
| 754 | MaybeObject* maybe_array = |
| 755 | heap->AllocateJSArrayAndStorage(elements_kind, |
| 756 | result_len, |
| 757 | result_len); |
| 758 | JSArray* result_array; |
| 759 | if (!maybe_array->To(&result_array)) return maybe_array; |
| 760 | |
| 761 | CopyObjectToObjectElements(elms, FAST_ELEMENTS, k, |
| 762 | FixedArray::cast(result_array->elements()), |
| 763 | FAST_ELEMENTS, 0, result_len); |
| 764 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 765 | return result_array; |
| 766 | } |
| 767 | |
| 768 | |
| 769 | BUILTIN(ArraySplice) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 770 | Heap* heap = isolate->heap(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 771 | Object* receiver = *args.receiver(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 772 | Object* elms_obj; |
| 773 | { MaybeObject* maybe_elms_obj = |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 774 | EnsureJSArrayWithWritableFastElements(heap, receiver, &args, 3); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 775 | if (maybe_elms_obj == NULL) |
| 776 | return CallJsBuiltin(isolate, "ArraySplice", args); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 777 | if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj; |
| 778 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 779 | if (!IsJSArrayFastElementMovingAllowed(heap, JSArray::cast(receiver))) { |
| 780 | return CallJsBuiltin(isolate, "ArraySplice", args); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 781 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 782 | FixedArray* elms = FixedArray::cast(elms_obj); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 783 | JSArray* array = JSArray::cast(receiver); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 784 | ASSERT(array->HasFastTypeElements()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 785 | |
| 786 | int len = Smi::cast(array->length())->value(); |
| 787 | |
| 788 | int n_arguments = args.length() - 1; |
| 789 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 790 | int relative_start = 0; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 791 | if (n_arguments > 0) { |
| 792 | Object* arg1 = args[1]; |
| 793 | if (arg1->IsSmi()) { |
| 794 | relative_start = Smi::cast(arg1)->value(); |
| 795 | } else if (!arg1->IsUndefined()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 796 | return CallJsBuiltin(isolate, "ArraySplice", args); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 797 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 798 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 799 | int actual_start = (relative_start < 0) ? Max(len + relative_start, 0) |
| 800 | : Min(relative_start, len); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 801 | |
| 802 | // SpiderMonkey, TraceMonkey and JSC treat the case where no delete count is |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 803 | // given as a request to delete all the elements from the start. |
| 804 | // And it differs from the case of undefined delete count. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 805 | // This does not follow ECMA-262, but we do the same for |
| 806 | // compatibility. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 807 | int actual_delete_count; |
| 808 | if (n_arguments == 1) { |
| 809 | ASSERT(len - actual_start >= 0); |
| 810 | actual_delete_count = len - actual_start; |
| 811 | } else { |
| 812 | int value = 0; // ToInteger(undefined) == 0 |
| 813 | if (n_arguments > 1) { |
| 814 | Object* arg2 = args[2]; |
| 815 | if (arg2->IsSmi()) { |
| 816 | value = Smi::cast(arg2)->value(); |
| 817 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 818 | return CallJsBuiltin(isolate, "ArraySplice", args); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 819 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 820 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 821 | actual_delete_count = Min(Max(value, 0), len - actual_start); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 822 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 823 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 824 | JSArray* result_array = NULL; |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 825 | ElementsKind elements_kind = |
| 826 | JSObject::cast(receiver)->GetElementsKind(); |
| 827 | MaybeObject* maybe_array = |
| 828 | heap->AllocateJSArrayAndStorage(elements_kind, |
| 829 | actual_delete_count, |
| 830 | actual_delete_count); |
| 831 | if (!maybe_array->To(&result_array)) return maybe_array; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 832 | |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 833 | { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 834 | // Fill newly created array. |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 835 | CopyObjectToObjectElements(elms, FAST_ELEMENTS, actual_start, |
| 836 | FixedArray::cast(result_array->elements()), |
| 837 | FAST_ELEMENTS, 0, actual_delete_count); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 840 | int item_count = (n_arguments > 1) ? (n_arguments - 2) : 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 841 | int new_length = len - actual_delete_count + item_count; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 842 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 843 | bool elms_changed = false; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 844 | if (item_count < actual_delete_count) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 845 | // Shrink the array. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 846 | const bool trim_array = !heap->lo_space()->Contains(elms) && |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 847 | ((actual_start + item_count) < |
| 848 | (len - actual_delete_count - actual_start)); |
| 849 | if (trim_array) { |
| 850 | const int delta = actual_delete_count - item_count; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 851 | |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 852 | { |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 853 | AssertNoAllocation no_gc; |
| 854 | MoveElements(heap, &no_gc, elms, delta, elms, 0, actual_start); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 855 | } |
| 856 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 857 | elms = LeftTrimFixedArray(heap, elms, delta); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 858 | |
| 859 | elms_changed = true; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 860 | } else { |
| 861 | AssertNoAllocation no_gc; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 862 | MoveElements(heap, &no_gc, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 863 | elms, actual_start + item_count, |
| 864 | elms, actual_start + actual_delete_count, |
| 865 | (len - actual_delete_count - actual_start)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 866 | FillWithHoles(heap, elms, new_length, len); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 867 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 868 | } else if (item_count > actual_delete_count) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 869 | // Currently fixed arrays cannot grow too big, so |
| 870 | // we should never hit this case. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 871 | ASSERT((item_count - actual_delete_count) <= (Smi::kMaxValue - len)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 872 | |
| 873 | // Check if array need to grow. |
| 874 | if (new_length > elms->length()) { |
| 875 | // New backing storage is needed. |
| 876 | int capacity = new_length + (new_length >> 1) + 16; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 877 | Object* obj; |
| 878 | { MaybeObject* maybe_obj = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 879 | heap->AllocateUninitializedFixedArray(capacity); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 880 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 881 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 882 | FixedArray* new_elms = FixedArray::cast(obj); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 883 | |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 884 | { |
| 885 | // Copy the part before actual_start as is. |
| 886 | CopyObjectToObjectElements(elms, FAST_ELEMENTS, 0, |
| 887 | new_elms, FAST_ELEMENTS, 0, actual_start); |
| 888 | const int to_copy = len - actual_delete_count - actual_start; |
| 889 | CopyObjectToObjectElements(elms, FAST_ELEMENTS, |
| 890 | actual_start + actual_delete_count, |
| 891 | new_elms, FAST_ELEMENTS, |
| 892 | actual_start + item_count, to_copy); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 893 | } |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 894 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 895 | FillWithHoles(heap, new_elms, new_length, capacity); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 896 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 897 | elms = new_elms; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 898 | elms_changed = true; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 899 | } else { |
| 900 | AssertNoAllocation no_gc; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 901 | MoveElements(heap, &no_gc, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 902 | elms, actual_start + item_count, |
| 903 | elms, actual_start + actual_delete_count, |
| 904 | (len - actual_delete_count - actual_start)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 908 | AssertNoAllocation no_gc; |
| 909 | WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc); |
| 910 | for (int k = actual_start; k < actual_start + item_count; k++) { |
| 911 | elms->set(k, args[3 + k - actual_start], mode); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 914 | if (elms_changed) { |
| 915 | array->set_elements(elms); |
| 916 | } |
| 917 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 918 | // Set the length. |
| 919 | array->set_length(Smi::FromInt(new_length)); |
| 920 | |
| 921 | return result_array; |
| 922 | } |
| 923 | |
| 924 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 925 | BUILTIN(ArrayConcat) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 926 | Heap* heap = isolate->heap(); |
| 927 | Context* global_context = isolate->context()->global_context(); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 928 | JSObject* array_proto = |
| 929 | JSObject::cast(global_context->array_function()->prototype()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 930 | if (!ArrayPrototypeHasNoElements(heap, global_context, array_proto)) { |
| 931 | return CallJsBuiltin(isolate, "ArrayConcat", args); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | // Iterate through all the arguments performing checks |
| 935 | // and calculating total length. |
| 936 | int n_arguments = args.length(); |
| 937 | int result_len = 0; |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 938 | ElementsKind elements_kind = FAST_SMI_ONLY_ELEMENTS; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 939 | for (int i = 0; i < n_arguments; i++) { |
| 940 | Object* arg = args[i]; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 941 | if (!arg->IsJSArray() || !JSArray::cast(arg)->HasFastTypeElements() |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 942 | || JSArray::cast(arg)->GetPrototype() != array_proto) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 943 | return CallJsBuiltin(isolate, "ArrayConcat", args); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | int len = Smi::cast(JSArray::cast(arg)->length())->value(); |
| 947 | |
| 948 | // We shouldn't overflow when adding another len. |
| 949 | const int kHalfOfMaxInt = 1 << (kBitsPerInt - 2); |
| 950 | STATIC_ASSERT(FixedArray::kMaxLength < kHalfOfMaxInt); |
| 951 | USE(kHalfOfMaxInt); |
| 952 | result_len += len; |
| 953 | ASSERT(result_len >= 0); |
| 954 | |
| 955 | if (result_len > FixedArray::kMaxLength) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 956 | return CallJsBuiltin(isolate, "ArrayConcat", args); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 957 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 958 | |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 959 | if (!JSArray::cast(arg)->HasFastSmiOnlyElements()) { |
| 960 | elements_kind = FAST_ELEMENTS; |
| 961 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | // Allocate result. |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 965 | JSArray* result_array; |
| 966 | MaybeObject* maybe_array = |
| 967 | heap->AllocateJSArrayAndStorage(elements_kind, |
| 968 | result_len, |
| 969 | result_len); |
| 970 | if (!maybe_array->To(&result_array)) return maybe_array; |
| 971 | if (result_len == 0) return result_array; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 972 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 973 | // Copy data. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 974 | int start_pos = 0; |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 975 | FixedArray* result_elms(FixedArray::cast(result_array->elements())); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 976 | for (int i = 0; i < n_arguments; i++) { |
| 977 | JSArray* array = JSArray::cast(args[i]); |
| 978 | int len = Smi::cast(array->length())->value(); |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 979 | FixedArray* elms = FixedArray::cast(array->elements()); |
| 980 | CopyObjectToObjectElements(elms, FAST_ELEMENTS, 0, |
| 981 | result_elms, FAST_ELEMENTS, |
| 982 | start_pos, len); |
| 983 | start_pos += len; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 984 | } |
| 985 | ASSERT(start_pos == result_len); |
| 986 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 987 | return result_array; |
| 988 | } |
| 989 | |
| 990 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 991 | // ----------------------------------------------------------------------------- |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 992 | // Strict mode poison pills |
| 993 | |
| 994 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 995 | BUILTIN(StrictModePoisonPill) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 996 | HandleScope scope; |
| 997 | return isolate->Throw(*isolate->factory()->NewTypeError( |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 998 | "strict_poison_pill", HandleVector<Object>(NULL, 0))); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 999 | } |
| 1000 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1001 | // ----------------------------------------------------------------------------- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1002 | // |
| 1003 | |
| 1004 | |
| 1005 | // Returns the holder JSObject if the function can legally be called |
| 1006 | // with this receiver. Returns Heap::null_value() if the call is |
| 1007 | // illegal. Any arguments that don't fit the expected type is |
| 1008 | // overwritten with undefined. Arguments that do fit the expected |
| 1009 | // type is overwritten with the object in the prototype chain that |
| 1010 | // actually has that type. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1011 | static inline Object* TypeCheck(Heap* heap, |
| 1012 | int argc, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1013 | Object** argv, |
| 1014 | FunctionTemplateInfo* info) { |
| 1015 | Object* recv = argv[0]; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1016 | // API calls are only supported with JSObject receivers. |
| 1017 | if (!recv->IsJSObject()) return heap->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1018 | Object* sig_obj = info->signature(); |
| 1019 | if (sig_obj->IsUndefined()) return recv; |
| 1020 | SignatureInfo* sig = SignatureInfo::cast(sig_obj); |
| 1021 | // If necessary, check the receiver |
| 1022 | Object* recv_type = sig->receiver(); |
| 1023 | |
| 1024 | Object* holder = recv; |
| 1025 | if (!recv_type->IsUndefined()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1026 | for (; holder != heap->null_value(); holder = holder->GetPrototype()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1027 | if (holder->IsInstanceOf(FunctionTemplateInfo::cast(recv_type))) { |
| 1028 | break; |
| 1029 | } |
| 1030 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1031 | if (holder == heap->null_value()) return holder; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1032 | } |
| 1033 | Object* args_obj = sig->args(); |
| 1034 | // If there is no argument signature we're done |
| 1035 | if (args_obj->IsUndefined()) return holder; |
| 1036 | FixedArray* args = FixedArray::cast(args_obj); |
| 1037 | int length = args->length(); |
| 1038 | if (argc <= length) length = argc - 1; |
| 1039 | for (int i = 0; i < length; i++) { |
| 1040 | Object* argtype = args->get(i); |
| 1041 | if (argtype->IsUndefined()) continue; |
| 1042 | Object** arg = &argv[-1 - i]; |
| 1043 | Object* current = *arg; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1044 | for (; current != heap->null_value(); current = current->GetPrototype()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1045 | if (current->IsInstanceOf(FunctionTemplateInfo::cast(argtype))) { |
| 1046 | *arg = current; |
| 1047 | break; |
| 1048 | } |
| 1049 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1050 | if (current == heap->null_value()) *arg = heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1051 | } |
| 1052 | return holder; |
| 1053 | } |
| 1054 | |
| 1055 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1056 | template <bool is_construct> |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1057 | MUST_USE_RESULT static MaybeObject* HandleApiCallHelper( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1058 | BuiltinArguments<NEEDS_CALLED_FUNCTION> args, Isolate* isolate) { |
| 1059 | ASSERT(is_construct == CalledAsConstructor(isolate)); |
| 1060 | Heap* heap = isolate->heap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1061 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1062 | HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1063 | Handle<JSFunction> function = args.called_function(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1064 | ASSERT(function->shared()->IsApiFunction()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1065 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1066 | FunctionTemplateInfo* fun_data = function->shared()->get_api_func_data(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1067 | if (is_construct) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1068 | Handle<FunctionTemplateInfo> desc(fun_data, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1069 | bool pending_exception = false; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1070 | isolate->factory()->ConfigureInstance( |
| 1071 | desc, Handle<JSObject>::cast(args.receiver()), &pending_exception); |
| 1072 | ASSERT(isolate->has_pending_exception() == pending_exception); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1073 | if (pending_exception) return Failure::Exception(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1074 | fun_data = *desc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1077 | Object* raw_holder = TypeCheck(heap, args.length(), &args[0], fun_data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1078 | |
| 1079 | if (raw_holder->IsNull()) { |
| 1080 | // This function cannot be called with the given receiver. Abort! |
| 1081 | Handle<Object> obj = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1082 | isolate->factory()->NewTypeError( |
| 1083 | "illegal_invocation", HandleVector(&function, 1)); |
| 1084 | return isolate->Throw(*obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | Object* raw_call_data = fun_data->call_code(); |
| 1088 | if (!raw_call_data->IsUndefined()) { |
| 1089 | CallHandlerInfo* call_data = CallHandlerInfo::cast(raw_call_data); |
| 1090 | Object* callback_obj = call_data->callback(); |
| 1091 | v8::InvocationCallback callback = |
| 1092 | v8::ToCData<v8::InvocationCallback>(callback_obj); |
| 1093 | Object* data_obj = call_data->data(); |
| 1094 | Object* result; |
| 1095 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1096 | LOG(isolate, ApiObjectAccess("call", JSObject::cast(*args.receiver()))); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1097 | ASSERT(raw_holder->IsJSObject()); |
| 1098 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1099 | CustomArguments custom(isolate); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1100 | v8::ImplementationUtilities::PrepareArgumentsData(custom.end(), |
| 1101 | data_obj, *function, raw_holder); |
| 1102 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1103 | v8::Arguments new_args = v8::ImplementationUtilities::NewArguments( |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1104 | custom.end(), |
| 1105 | &args[0] - 1, |
| 1106 | args.length() - 1, |
| 1107 | is_construct); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1108 | |
| 1109 | v8::Handle<v8::Value> value; |
| 1110 | { |
| 1111 | // Leaving JavaScript. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1112 | VMState state(isolate, EXTERNAL); |
| 1113 | ExternalCallbackScope call_scope(isolate, |
| 1114 | v8::ToCData<Address>(callback_obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1115 | value = callback(new_args); |
| 1116 | } |
| 1117 | if (value.IsEmpty()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1118 | result = heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1119 | } else { |
| 1120 | result = *reinterpret_cast<Object**>(*value); |
| 1121 | } |
| 1122 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1123 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1124 | if (!is_construct || result->IsJSObject()) return result; |
| 1125 | } |
| 1126 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1127 | return *args.receiver(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1128 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1129 | |
| 1130 | |
| 1131 | BUILTIN(HandleApiCall) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1132 | return HandleApiCallHelper<false>(args, isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | |
| 1136 | BUILTIN(HandleApiCallConstruct) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1137 | return HandleApiCallHelper<true>(args, isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1138 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1139 | |
| 1140 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1141 | #ifdef DEBUG |
| 1142 | |
| 1143 | static void VerifyTypeCheck(Handle<JSObject> object, |
| 1144 | Handle<JSFunction> function) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1145 | ASSERT(function->shared()->IsApiFunction()); |
| 1146 | FunctionTemplateInfo* info = function->shared()->get_api_func_data(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1147 | if (info->signature()->IsUndefined()) return; |
| 1148 | SignatureInfo* signature = SignatureInfo::cast(info->signature()); |
| 1149 | Object* receiver_type = signature->receiver(); |
| 1150 | if (receiver_type->IsUndefined()) return; |
| 1151 | FunctionTemplateInfo* type = FunctionTemplateInfo::cast(receiver_type); |
| 1152 | ASSERT(object->IsInstanceOf(type)); |
| 1153 | } |
| 1154 | |
| 1155 | #endif |
| 1156 | |
| 1157 | |
| 1158 | BUILTIN(FastHandleApiCall) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1159 | ASSERT(!CalledAsConstructor(isolate)); |
| 1160 | Heap* heap = isolate->heap(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1161 | const bool is_construct = false; |
| 1162 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1163 | // We expect four more arguments: callback, function, call data, and holder. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1164 | const int args_length = args.length() - 4; |
| 1165 | ASSERT(args_length >= 0); |
| 1166 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1167 | Object* callback_obj = args[args_length]; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1168 | |
| 1169 | v8::Arguments new_args = v8::ImplementationUtilities::NewArguments( |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1170 | &args[args_length + 1], |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1171 | &args[0] - 1, |
| 1172 | args_length - 1, |
| 1173 | is_construct); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1174 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1175 | #ifdef DEBUG |
| 1176 | VerifyTypeCheck(Utils::OpenHandle(*new_args.Holder()), |
| 1177 | Utils::OpenHandle(*new_args.Callee())); |
| 1178 | #endif |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1179 | HandleScope scope(isolate); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1180 | Object* result; |
| 1181 | v8::Handle<v8::Value> value; |
| 1182 | { |
| 1183 | // Leaving JavaScript. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1184 | VMState state(isolate, EXTERNAL); |
| 1185 | ExternalCallbackScope call_scope(isolate, |
| 1186 | v8::ToCData<Address>(callback_obj)); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1187 | v8::InvocationCallback callback = |
| 1188 | v8::ToCData<v8::InvocationCallback>(callback_obj); |
| 1189 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1190 | value = callback(new_args); |
| 1191 | } |
| 1192 | if (value.IsEmpty()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1193 | result = heap->undefined_value(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1194 | } else { |
| 1195 | result = *reinterpret_cast<Object**>(*value); |
| 1196 | } |
| 1197 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1198 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1199 | return result; |
| 1200 | } |
| 1201 | |
| 1202 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1203 | // Helper function to handle calls to non-function objects created through the |
| 1204 | // API. The object can be called as either a constructor (using new) or just as |
| 1205 | // a function (without new). |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1206 | MUST_USE_RESULT static MaybeObject* HandleApiCallAsFunctionOrConstructor( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1207 | Isolate* isolate, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1208 | bool is_construct_call, |
| 1209 | BuiltinArguments<NO_EXTRA_ARGUMENTS> args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1210 | // Non-functions are never called as constructors. Even if this is an object |
| 1211 | // called as a constructor the delegate call is not a construct call. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1212 | ASSERT(!CalledAsConstructor(isolate)); |
| 1213 | Heap* heap = isolate->heap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1214 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1215 | Handle<Object> receiver = args.receiver(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1216 | |
| 1217 | // Get the object called. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1218 | JSObject* obj = JSObject::cast(*receiver); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1219 | |
| 1220 | // Get the invocation callback from the function descriptor that was |
| 1221 | // used to create the called object. |
| 1222 | ASSERT(obj->map()->has_instance_call_handler()); |
| 1223 | JSFunction* constructor = JSFunction::cast(obj->map()->constructor()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1224 | ASSERT(constructor->shared()->IsApiFunction()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1225 | Object* handler = |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1226 | constructor->shared()->get_api_func_data()->instance_call_handler(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1227 | ASSERT(!handler->IsUndefined()); |
| 1228 | CallHandlerInfo* call_data = CallHandlerInfo::cast(handler); |
| 1229 | Object* callback_obj = call_data->callback(); |
| 1230 | v8::InvocationCallback callback = |
| 1231 | v8::ToCData<v8::InvocationCallback>(callback_obj); |
| 1232 | |
| 1233 | // Get the data for the call and perform the callback. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1234 | Object* result; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1235 | { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1236 | HandleScope scope(isolate); |
| 1237 | LOG(isolate, ApiObjectAccess("call non-function", obj)); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1238 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1239 | CustomArguments custom(isolate); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1240 | v8::ImplementationUtilities::PrepareArgumentsData(custom.end(), |
| 1241 | call_data->data(), constructor, obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1242 | v8::Arguments new_args = v8::ImplementationUtilities::NewArguments( |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1243 | custom.end(), |
| 1244 | &args[0] - 1, |
| 1245 | args.length() - 1, |
| 1246 | is_construct_call); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1247 | v8::Handle<v8::Value> value; |
| 1248 | { |
| 1249 | // Leaving JavaScript. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1250 | VMState state(isolate, EXTERNAL); |
| 1251 | ExternalCallbackScope call_scope(isolate, |
| 1252 | v8::ToCData<Address>(callback_obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1253 | value = callback(new_args); |
| 1254 | } |
| 1255 | if (value.IsEmpty()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1256 | result = heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1257 | } else { |
| 1258 | result = *reinterpret_cast<Object**>(*value); |
| 1259 | } |
| 1260 | } |
| 1261 | // Check for exceptions and return result. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1262 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1263 | return result; |
| 1264 | } |
| 1265 | |
| 1266 | |
| 1267 | // Handle calls to non-function objects created through the API. This delegate |
| 1268 | // function is used when the call is a normal function call. |
| 1269 | BUILTIN(HandleApiCallAsFunction) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1270 | return HandleApiCallAsFunctionOrConstructor(isolate, false, args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1271 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1272 | |
| 1273 | |
| 1274 | // Handle calls to non-function objects created through the API. This delegate |
| 1275 | // function is used when the call is a construct call. |
| 1276 | BUILTIN(HandleApiCallAsConstructor) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1277 | return HandleApiCallAsFunctionOrConstructor(isolate, true, args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1278 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1279 | |
| 1280 | |
| 1281 | static void Generate_LoadIC_ArrayLength(MacroAssembler* masm) { |
| 1282 | LoadIC::GenerateArrayLength(masm); |
| 1283 | } |
| 1284 | |
| 1285 | |
| 1286 | static void Generate_LoadIC_StringLength(MacroAssembler* masm) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1287 | LoadIC::GenerateStringLength(masm, false); |
| 1288 | } |
| 1289 | |
| 1290 | |
| 1291 | static void Generate_LoadIC_StringWrapperLength(MacroAssembler* masm) { |
| 1292 | LoadIC::GenerateStringLength(masm, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | |
| 1296 | static void Generate_LoadIC_FunctionPrototype(MacroAssembler* masm) { |
| 1297 | LoadIC::GenerateFunctionPrototype(masm); |
| 1298 | } |
| 1299 | |
| 1300 | |
| 1301 | static void Generate_LoadIC_Initialize(MacroAssembler* masm) { |
| 1302 | LoadIC::GenerateInitialize(masm); |
| 1303 | } |
| 1304 | |
| 1305 | |
| 1306 | static void Generate_LoadIC_PreMonomorphic(MacroAssembler* masm) { |
| 1307 | LoadIC::GeneratePreMonomorphic(masm); |
| 1308 | } |
| 1309 | |
| 1310 | |
| 1311 | static void Generate_LoadIC_Miss(MacroAssembler* masm) { |
| 1312 | LoadIC::GenerateMiss(masm); |
| 1313 | } |
| 1314 | |
| 1315 | |
| 1316 | static void Generate_LoadIC_Megamorphic(MacroAssembler* masm) { |
| 1317 | LoadIC::GenerateMegamorphic(masm); |
| 1318 | } |
| 1319 | |
| 1320 | |
| 1321 | static void Generate_LoadIC_Normal(MacroAssembler* masm) { |
| 1322 | LoadIC::GenerateNormal(masm); |
| 1323 | } |
| 1324 | |
| 1325 | |
| 1326 | static void Generate_KeyedLoadIC_Initialize(MacroAssembler* masm) { |
| 1327 | KeyedLoadIC::GenerateInitialize(masm); |
| 1328 | } |
| 1329 | |
| 1330 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1331 | static void Generate_KeyedLoadIC_Slow(MacroAssembler* masm) { |
| 1332 | KeyedLoadIC::GenerateRuntimeGetProperty(masm); |
| 1333 | } |
| 1334 | |
| 1335 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1336 | static void Generate_KeyedLoadIC_Miss(MacroAssembler* masm) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1337 | KeyedLoadIC::GenerateMiss(masm, false); |
| 1338 | } |
| 1339 | |
| 1340 | |
| 1341 | static void Generate_KeyedLoadIC_MissForceGeneric(MacroAssembler* masm) { |
| 1342 | KeyedLoadIC::GenerateMiss(masm, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | |
| 1346 | static void Generate_KeyedLoadIC_Generic(MacroAssembler* masm) { |
| 1347 | KeyedLoadIC::GenerateGeneric(masm); |
| 1348 | } |
| 1349 | |
| 1350 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1351 | static void Generate_KeyedLoadIC_String(MacroAssembler* masm) { |
| 1352 | KeyedLoadIC::GenerateString(masm); |
| 1353 | } |
| 1354 | |
| 1355 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1356 | static void Generate_KeyedLoadIC_PreMonomorphic(MacroAssembler* masm) { |
| 1357 | KeyedLoadIC::GeneratePreMonomorphic(masm); |
| 1358 | } |
| 1359 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1360 | static void Generate_KeyedLoadIC_IndexedInterceptor(MacroAssembler* masm) { |
| 1361 | KeyedLoadIC::GenerateIndexedInterceptor(masm); |
| 1362 | } |
| 1363 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1364 | static void Generate_KeyedLoadIC_NonStrictArguments(MacroAssembler* masm) { |
| 1365 | KeyedLoadIC::GenerateNonStrictArguments(masm); |
| 1366 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1367 | |
| 1368 | static void Generate_StoreIC_Initialize(MacroAssembler* masm) { |
| 1369 | StoreIC::GenerateInitialize(masm); |
| 1370 | } |
| 1371 | |
| 1372 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1373 | static void Generate_StoreIC_Initialize_Strict(MacroAssembler* masm) { |
| 1374 | StoreIC::GenerateInitialize(masm); |
| 1375 | } |
| 1376 | |
| 1377 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1378 | static void Generate_StoreIC_Miss(MacroAssembler* masm) { |
| 1379 | StoreIC::GenerateMiss(masm); |
| 1380 | } |
| 1381 | |
| 1382 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1383 | static void Generate_StoreIC_Normal(MacroAssembler* masm) { |
| 1384 | StoreIC::GenerateNormal(masm); |
| 1385 | } |
| 1386 | |
| 1387 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1388 | static void Generate_StoreIC_Normal_Strict(MacroAssembler* masm) { |
| 1389 | StoreIC::GenerateNormal(masm); |
| 1390 | } |
| 1391 | |
| 1392 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1393 | static void Generate_StoreIC_Megamorphic(MacroAssembler* masm) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1394 | StoreIC::GenerateMegamorphic(masm, kNonStrictMode); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | |
| 1398 | static void Generate_StoreIC_Megamorphic_Strict(MacroAssembler* masm) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1399 | StoreIC::GenerateMegamorphic(masm, kStrictMode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1403 | static void Generate_StoreIC_ArrayLength(MacroAssembler* masm) { |
| 1404 | StoreIC::GenerateArrayLength(masm); |
| 1405 | } |
| 1406 | |
| 1407 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1408 | static void Generate_StoreIC_ArrayLength_Strict(MacroAssembler* masm) { |
| 1409 | StoreIC::GenerateArrayLength(masm); |
| 1410 | } |
| 1411 | |
| 1412 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1413 | static void Generate_StoreIC_GlobalProxy(MacroAssembler* masm) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1414 | StoreIC::GenerateGlobalProxy(masm, kNonStrictMode); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1418 | static void Generate_StoreIC_GlobalProxy_Strict(MacroAssembler* masm) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1419 | StoreIC::GenerateGlobalProxy(masm, kStrictMode); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1423 | static void Generate_KeyedStoreIC_Generic(MacroAssembler* masm) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1424 | KeyedStoreIC::GenerateGeneric(masm, kNonStrictMode); |
| 1425 | } |
| 1426 | |
| 1427 | |
| 1428 | static void Generate_KeyedStoreIC_Generic_Strict(MacroAssembler* masm) { |
| 1429 | KeyedStoreIC::GenerateGeneric(masm, kStrictMode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1433 | static void Generate_KeyedStoreIC_Miss(MacroAssembler* masm) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1434 | KeyedStoreIC::GenerateMiss(masm, false); |
| 1435 | } |
| 1436 | |
| 1437 | |
| 1438 | static void Generate_KeyedStoreIC_MissForceGeneric(MacroAssembler* masm) { |
| 1439 | KeyedStoreIC::GenerateMiss(masm, true); |
| 1440 | } |
| 1441 | |
| 1442 | |
| 1443 | static void Generate_KeyedStoreIC_Slow(MacroAssembler* masm) { |
| 1444 | KeyedStoreIC::GenerateSlow(masm); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | |
| 1448 | static void Generate_KeyedStoreIC_Initialize(MacroAssembler* masm) { |
| 1449 | KeyedStoreIC::GenerateInitialize(masm); |
| 1450 | } |
| 1451 | |
| 1452 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1453 | static void Generate_KeyedStoreIC_Initialize_Strict(MacroAssembler* masm) { |
| 1454 | KeyedStoreIC::GenerateInitialize(masm); |
| 1455 | } |
| 1456 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1457 | static void Generate_KeyedStoreIC_NonStrictArguments(MacroAssembler* masm) { |
| 1458 | KeyedStoreIC::GenerateNonStrictArguments(masm); |
| 1459 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1460 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1461 | static void Generate_TransitionElementsSmiToDouble(MacroAssembler* masm) { |
| 1462 | KeyedStoreIC::GenerateTransitionElementsSmiToDouble(masm); |
| 1463 | } |
| 1464 | |
| 1465 | static void Generate_TransitionElementsDoubleToObject(MacroAssembler* masm) { |
| 1466 | KeyedStoreIC::GenerateTransitionElementsDoubleToObject(masm); |
| 1467 | } |
| 1468 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1469 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 1470 | static void Generate_LoadIC_DebugBreak(MacroAssembler* masm) { |
| 1471 | Debug::GenerateLoadICDebugBreak(masm); |
| 1472 | } |
| 1473 | |
| 1474 | |
| 1475 | static void Generate_StoreIC_DebugBreak(MacroAssembler* masm) { |
| 1476 | Debug::GenerateStoreICDebugBreak(masm); |
| 1477 | } |
| 1478 | |
| 1479 | |
| 1480 | static void Generate_KeyedLoadIC_DebugBreak(MacroAssembler* masm) { |
| 1481 | Debug::GenerateKeyedLoadICDebugBreak(masm); |
| 1482 | } |
| 1483 | |
| 1484 | |
| 1485 | static void Generate_KeyedStoreIC_DebugBreak(MacroAssembler* masm) { |
| 1486 | Debug::GenerateKeyedStoreICDebugBreak(masm); |
| 1487 | } |
| 1488 | |
| 1489 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1490 | static void Generate_Return_DebugBreak(MacroAssembler* masm) { |
| 1491 | Debug::GenerateReturnDebugBreak(masm); |
| 1492 | } |
| 1493 | |
| 1494 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1495 | static void Generate_CallFunctionStub_DebugBreak(MacroAssembler* masm) { |
| 1496 | Debug::GenerateCallFunctionStubDebugBreak(masm); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1497 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1498 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1499 | |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 1500 | static void Generate_CallFunctionStub_Recording_DebugBreak( |
| 1501 | MacroAssembler* masm) { |
| 1502 | Debug::GenerateCallFunctionStubRecordDebugBreak(masm); |
| 1503 | } |
| 1504 | |
| 1505 | |
| 1506 | static void Generate_CallConstructStub_DebugBreak(MacroAssembler* masm) { |
| 1507 | Debug::GenerateCallConstructStubDebugBreak(masm); |
| 1508 | } |
| 1509 | |
| 1510 | |
| 1511 | static void Generate_CallConstructStub_Recording_DebugBreak( |
| 1512 | MacroAssembler* masm) { |
| 1513 | Debug::GenerateCallConstructStubRecordDebugBreak(masm); |
| 1514 | } |
| 1515 | |
| 1516 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1517 | static void Generate_Slot_DebugBreak(MacroAssembler* masm) { |
| 1518 | Debug::GenerateSlotDebugBreak(masm); |
| 1519 | } |
| 1520 | |
| 1521 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1522 | static void Generate_PlainReturn_LiveEdit(MacroAssembler* masm) { |
| 1523 | Debug::GeneratePlainReturnLiveEdit(masm); |
| 1524 | } |
| 1525 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1526 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1527 | static void Generate_FrameDropper_LiveEdit(MacroAssembler* masm) { |
| 1528 | Debug::GenerateFrameDropperLiveEdit(masm); |
| 1529 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1530 | #endif |
| 1531 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1532 | |
| 1533 | Builtins::Builtins() : initialized_(false) { |
| 1534 | memset(builtins_, 0, sizeof(builtins_[0]) * builtin_count); |
| 1535 | memset(names_, 0, sizeof(names_[0]) * builtin_count); |
| 1536 | } |
| 1537 | |
| 1538 | |
| 1539 | Builtins::~Builtins() { |
| 1540 | } |
| 1541 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1542 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1543 | #define DEF_ENUM_C(name, ignore) FUNCTION_ADDR(Builtin_##name), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1544 | Address const Builtins::c_functions_[cfunction_count] = { |
| 1545 | BUILTIN_LIST_C(DEF_ENUM_C) |
| 1546 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1547 | #undef DEF_ENUM_C |
| 1548 | |
| 1549 | #define DEF_JS_NAME(name, ignore) #name, |
| 1550 | #define DEF_JS_ARGC(ignore, argc) argc, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1551 | const char* const Builtins::javascript_names_[id_count] = { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1552 | BUILTINS_LIST_JS(DEF_JS_NAME) |
| 1553 | }; |
| 1554 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1555 | int const Builtins::javascript_argc_[id_count] = { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1556 | BUILTINS_LIST_JS(DEF_JS_ARGC) |
| 1557 | }; |
| 1558 | #undef DEF_JS_NAME |
| 1559 | #undef DEF_JS_ARGC |
| 1560 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1561 | struct BuiltinDesc { |
| 1562 | byte* generator; |
| 1563 | byte* c_code; |
| 1564 | const char* s_name; // name is only used for generating log information. |
| 1565 | int name; |
| 1566 | Code::Flags flags; |
| 1567 | BuiltinExtraArguments extra_args; |
| 1568 | }; |
| 1569 | |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 1570 | #define BUILTIN_FUNCTION_TABLE_INIT { V8_ONCE_INIT, {} } |
| 1571 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1572 | class BuiltinFunctionTable { |
| 1573 | public: |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 1574 | BuiltinDesc* functions() { |
| 1575 | CallOnce(&once_, &Builtins::InitBuiltinFunctionTable); |
| 1576 | return functions_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1577 | } |
| 1578 | |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 1579 | OnceType once_; |
| 1580 | BuiltinDesc functions_[Builtins::builtin_count + 1]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1581 | |
| 1582 | friend class Builtins; |
| 1583 | }; |
| 1584 | |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 1585 | static BuiltinFunctionTable builtin_function_table = |
| 1586 | BUILTIN_FUNCTION_TABLE_INIT; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1587 | |
| 1588 | // Define array of pointers to generators and C builtin functions. |
| 1589 | // We do this in a sort of roundabout way so that we can do the initialization |
| 1590 | // within the lexical scope of Builtins:: and within a context where |
| 1591 | // Code::Flags names a non-abstract type. |
| 1592 | void Builtins::InitBuiltinFunctionTable() { |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 1593 | BuiltinDesc* functions = builtin_function_table.functions_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1594 | functions[builtin_count].generator = NULL; |
| 1595 | functions[builtin_count].c_code = NULL; |
| 1596 | functions[builtin_count].s_name = NULL; |
| 1597 | functions[builtin_count].name = builtin_count; |
| 1598 | functions[builtin_count].flags = static_cast<Code::Flags>(0); |
| 1599 | functions[builtin_count].extra_args = NO_EXTRA_ARGUMENTS; |
| 1600 | |
| 1601 | #define DEF_FUNCTION_PTR_C(aname, aextra_args) \ |
| 1602 | functions->generator = FUNCTION_ADDR(Generate_Adaptor); \ |
| 1603 | functions->c_code = FUNCTION_ADDR(Builtin_##aname); \ |
| 1604 | functions->s_name = #aname; \ |
| 1605 | functions->name = c_##aname; \ |
| 1606 | functions->flags = Code::ComputeFlags(Code::BUILTIN); \ |
| 1607 | functions->extra_args = aextra_args; \ |
| 1608 | ++functions; |
| 1609 | |
| 1610 | #define DEF_FUNCTION_PTR_A(aname, kind, state, extra) \ |
| 1611 | functions->generator = FUNCTION_ADDR(Generate_##aname); \ |
| 1612 | functions->c_code = NULL; \ |
| 1613 | functions->s_name = #aname; \ |
| 1614 | functions->name = k##aname; \ |
| 1615 | functions->flags = Code::ComputeFlags(Code::kind, \ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1616 | state, \ |
| 1617 | extra); \ |
| 1618 | functions->extra_args = NO_EXTRA_ARGUMENTS; \ |
| 1619 | ++functions; |
| 1620 | |
| 1621 | BUILTIN_LIST_C(DEF_FUNCTION_PTR_C) |
| 1622 | BUILTIN_LIST_A(DEF_FUNCTION_PTR_A) |
| 1623 | BUILTIN_LIST_DEBUG_A(DEF_FUNCTION_PTR_A) |
| 1624 | |
| 1625 | #undef DEF_FUNCTION_PTR_C |
| 1626 | #undef DEF_FUNCTION_PTR_A |
| 1627 | } |
| 1628 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 1629 | void Builtins::SetUp(bool create_heap_objects) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1630 | ASSERT(!initialized_); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1631 | Isolate* isolate = Isolate::Current(); |
| 1632 | Heap* heap = isolate->heap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1633 | |
| 1634 | // Create a scope for the handles in the builtins. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1635 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1636 | |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 1637 | const BuiltinDesc* functions = builtin_function_table.functions(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1638 | |
| 1639 | // For now we generate builtin adaptor code into a stack-allocated |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1640 | // buffer, before copying it into individual code objects. Be careful |
| 1641 | // with alignment, some platforms don't like unaligned code. |
| 1642 | union { int force_alignment; byte buffer[4*KB]; } u; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1643 | |
| 1644 | // Traverse the list of builtins and generate an adaptor in a |
| 1645 | // separate code object for each one. |
| 1646 | for (int i = 0; i < builtin_count; i++) { |
| 1647 | if (create_heap_objects) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1648 | MacroAssembler masm(isolate, u.buffer, sizeof u.buffer); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1649 | // Generate the code/adaptor. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1650 | typedef void (*Generator)(MacroAssembler*, int, BuiltinExtraArguments); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1651 | Generator g = FUNCTION_CAST<Generator>(functions[i].generator); |
| 1652 | // We pass all arguments to the generator, but it may not use all of |
| 1653 | // them. This works because the first arguments are on top of the |
| 1654 | // stack. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1655 | ASSERT(!masm.has_frame()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1656 | g(&masm, functions[i].name, functions[i].extra_args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1657 | // Move the code into the object heap. |
| 1658 | CodeDesc desc; |
| 1659 | masm.GetCode(&desc); |
| 1660 | Code::Flags flags = functions[i].flags; |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 1661 | Object* code = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1662 | { |
| 1663 | // During startup it's OK to always allocate and defer GC to later. |
| 1664 | // This simplifies things because we don't need to retry. |
| 1665 | AlwaysAllocateScope __scope__; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1666 | { MaybeObject* maybe_code = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1667 | heap->CreateCode(desc, flags, masm.CodeObject()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1668 | if (!maybe_code->ToObject(&code)) { |
| 1669 | v8::internal::V8::FatalProcessOutOfMemory("CreateCode"); |
| 1670 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1671 | } |
| 1672 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1673 | // Log the event and add the code to the builtins array. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1674 | PROFILE(isolate, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1675 | CodeCreateEvent(Logger::BUILTIN_TAG, |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 1676 | Code::cast(code), |
| 1677 | functions[i].s_name)); |
| 1678 | GDBJIT(AddCode(GDBJITInterface::BUILTIN, |
| 1679 | functions[i].s_name, |
| 1680 | Code::cast(code))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1681 | builtins_[i] = code; |
| 1682 | #ifdef ENABLE_DISASSEMBLER |
| 1683 | if (FLAG_print_builtin_code) { |
| 1684 | PrintF("Builtin: %s\n", functions[i].s_name); |
| 1685 | Code::cast(code)->Disassemble(functions[i].s_name); |
| 1686 | PrintF("\n"); |
| 1687 | } |
| 1688 | #endif |
| 1689 | } else { |
| 1690 | // Deserializing. The values will be filled in during IterateBuiltins. |
| 1691 | builtins_[i] = NULL; |
| 1692 | } |
| 1693 | names_[i] = functions[i].s_name; |
| 1694 | } |
| 1695 | |
| 1696 | // Mark as initialized. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1697 | initialized_ = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | |
| 1701 | void Builtins::TearDown() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1702 | initialized_ = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | |
| 1706 | void Builtins::IterateBuiltins(ObjectVisitor* v) { |
| 1707 | v->VisitPointers(&builtins_[0], &builtins_[0] + builtin_count); |
| 1708 | } |
| 1709 | |
| 1710 | |
| 1711 | const char* Builtins::Lookup(byte* pc) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1712 | // may be called during initialization (disassembler!) |
| 1713 | if (initialized_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1714 | for (int i = 0; i < builtin_count; i++) { |
| 1715 | Code* entry = Code::cast(builtins_[i]); |
| 1716 | if (entry->contains(pc)) { |
| 1717 | return names_[i]; |
| 1718 | } |
| 1719 | } |
| 1720 | } |
| 1721 | return NULL; |
| 1722 | } |
| 1723 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1724 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1725 | #define DEFINE_BUILTIN_ACCESSOR_C(name, ignore) \ |
| 1726 | Handle<Code> Builtins::name() { \ |
| 1727 | Code** code_address = \ |
| 1728 | reinterpret_cast<Code**>(builtin_address(k##name)); \ |
| 1729 | return Handle<Code>(code_address); \ |
| 1730 | } |
| 1731 | #define DEFINE_BUILTIN_ACCESSOR_A(name, kind, state, extra) \ |
| 1732 | Handle<Code> Builtins::name() { \ |
| 1733 | Code** code_address = \ |
| 1734 | reinterpret_cast<Code**>(builtin_address(k##name)); \ |
| 1735 | return Handle<Code>(code_address); \ |
| 1736 | } |
| 1737 | BUILTIN_LIST_C(DEFINE_BUILTIN_ACCESSOR_C) |
| 1738 | BUILTIN_LIST_A(DEFINE_BUILTIN_ACCESSOR_A) |
| 1739 | BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) |
| 1740 | #undef DEFINE_BUILTIN_ACCESSOR_C |
| 1741 | #undef DEFINE_BUILTIN_ACCESSOR_A |
| 1742 | |
| 1743 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1744 | } } // namespace v8::internal |