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