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