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