blob: 0c76f6944f86684b31e360d5fe1d2f6d40ba8b78 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "api.h"
31#include "arguments.h"
32#include "bootstrapper.h"
33#include "builtins.h"
34#include "ic-inl.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010035#include "vm-state-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000036
37namespace v8 {
38namespace internal {
39
Leon Clarkee46be812010-01-19 14:06:41 +000040namespace {
41
42// Arguments object passed to C++ builtins.
43template <BuiltinExtraArguments extra_args>
44class BuiltinArguments : public Arguments {
45 public:
46 BuiltinArguments(int length, Object** arguments)
47 : Arguments(length, arguments) { }
48
49 Object*& operator[] (int index) {
50 ASSERT(index < length());
51 return Arguments::operator[](index);
52 }
53
54 template <class S> Handle<S> at(int index) {
55 ASSERT(index < length());
56 return Arguments::at<S>(index);
57 }
58
59 Handle<Object> receiver() {
60 return Arguments::at<Object>(0);
61 }
62
63 Handle<JSFunction> called_function() {
64 STATIC_ASSERT(extra_args == NEEDS_CALLED_FUNCTION);
65 return Arguments::at<JSFunction>(Arguments::length() - 1);
66 }
67
68 // Gets the total number of arguments including the receiver (but
69 // excluding extra arguments).
70 int length() const {
71 STATIC_ASSERT(extra_args == NO_EXTRA_ARGUMENTS);
72 return Arguments::length();
73 }
74
75#ifdef DEBUG
76 void Verify() {
77 // Check we have at least the receiver.
78 ASSERT(Arguments::length() >= 1);
79 }
80#endif
81};
82
83
84// Specialize BuiltinArguments for the called function extra argument.
85
86template <>
87int BuiltinArguments<NEEDS_CALLED_FUNCTION>::length() const {
88 return Arguments::length() - 1;
89}
90
91#ifdef DEBUG
92template <>
93void BuiltinArguments<NEEDS_CALLED_FUNCTION>::Verify() {
94 // Check we have at least the receiver and the called function.
95 ASSERT(Arguments::length() >= 2);
96 // Make sure cast to JSFunction succeeds.
97 called_function();
98}
99#endif
100
101
102#define DEF_ARG_TYPE(name, spec) \
103 typedef BuiltinArguments<spec> name##ArgumentsType;
104BUILTIN_LIST_C(DEF_ARG_TYPE)
105#undef DEF_ARG_TYPE
106
107} // namespace
108
109
Steve Blocka7e24c12009-10-30 11:49:00 +0000110// ----------------------------------------------------------------------------
Leon Clarkee46be812010-01-19 14:06:41 +0000111// Support macro for defining builtins in C++.
Steve Blocka7e24c12009-10-30 11:49:00 +0000112// ----------------------------------------------------------------------------
113//
114// A builtin function is defined by writing:
115//
116// BUILTIN(name) {
117// ...
118// }
Steve Blocka7e24c12009-10-30 11:49:00 +0000119//
Leon Clarkee46be812010-01-19 14:06:41 +0000120// In the body of the builtin function the arguments can be accessed
121// through the BuiltinArguments object args.
Steve Blocka7e24c12009-10-30 11:49:00 +0000122
Leon Clarkee46be812010-01-19 14:06:41 +0000123#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000124
John Reck59135872010-11-02 12:39:01 -0700125#define BUILTIN(name) \
126 MUST_USE_RESULT static MaybeObject* Builtin_Impl_##name( \
127 name##ArgumentsType args); \
128 MUST_USE_RESULT static MaybeObject* Builtin_##name( \
129 name##ArgumentsType args) { \
130 args.Verify(); \
131 return Builtin_Impl_##name(args); \
132 } \
133 MUST_USE_RESULT static MaybeObject* Builtin_Impl_##name( \
134 name##ArgumentsType args)
Steve Blocka7e24c12009-10-30 11:49:00 +0000135
Leon Clarkee46be812010-01-19 14:06:41 +0000136#else // For release mode.
Steve Blocka7e24c12009-10-30 11:49:00 +0000137
John Reck59135872010-11-02 12:39:01 -0700138#define BUILTIN(name) \
139 static MaybeObject* Builtin_##name(name##ArgumentsType args)
Leon Clarkee46be812010-01-19 14:06:41 +0000140
141#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000142
143
144static inline bool CalledAsConstructor() {
145#ifdef DEBUG
146 // Calculate the result using a full stack frame iterator and check
147 // that the state of the stack is as we assume it to be in the
148 // code below.
149 StackFrameIterator it;
150 ASSERT(it.frame()->is_exit());
151 it.Advance();
152 StackFrame* frame = it.frame();
153 bool reference_result = frame->is_construct();
154#endif
155 Address fp = Top::c_entry_fp(Top::GetCurrentThread());
156 // Because we know fp points to an exit frame we can use the relevant
157 // part of ExitFrame::ComputeCallerState directly.
158 const int kCallerOffset = ExitFrameConstants::kCallerFPOffset;
159 Address caller_fp = Memory::Address_at(fp + kCallerOffset);
160 // This inlines the part of StackFrame::ComputeType that grabs the
161 // type of the current frame. Note that StackFrame::ComputeType
162 // has been specialized for each architecture so if any one of them
163 // changes this code has to be changed as well.
164 const int kMarkerOffset = StandardFrameConstants::kMarkerOffset;
165 const Smi* kConstructMarker = Smi::FromInt(StackFrame::CONSTRUCT);
166 Object* marker = Memory::Object_at(caller_fp + kMarkerOffset);
167 bool result = (marker == kConstructMarker);
168 ASSERT_EQ(result, reference_result);
169 return result;
170}
171
172// ----------------------------------------------------------------------------
173
174
Steve Blocka7e24c12009-10-30 11:49:00 +0000175BUILTIN(Illegal) {
176 UNREACHABLE();
Leon Clarkee46be812010-01-19 14:06:41 +0000177 return Heap::undefined_value(); // Make compiler happy.
Steve Blocka7e24c12009-10-30 11:49:00 +0000178}
Steve Blocka7e24c12009-10-30 11:49:00 +0000179
180
181BUILTIN(EmptyFunction) {
Leon Clarkee46be812010-01-19 14:06:41 +0000182 return Heap::undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000183}
Steve Blocka7e24c12009-10-30 11:49:00 +0000184
185
186BUILTIN(ArrayCodeGeneric) {
187 Counters::array_function_runtime.Increment();
188
189 JSArray* array;
190 if (CalledAsConstructor()) {
Leon Clarkee46be812010-01-19 14:06:41 +0000191 array = JSArray::cast(*args.receiver());
Steve Blocka7e24c12009-10-30 11:49:00 +0000192 } else {
193 // Allocate the JS Array
194 JSFunction* constructor =
195 Top::context()->global_context()->array_function();
John Reck59135872010-11-02 12:39:01 -0700196 Object* obj;
197 { MaybeObject* maybe_obj = Heap::AllocateJSObject(constructor);
198 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
199 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000200 array = JSArray::cast(obj);
201 }
202
203 // 'array' now contains the JSArray we should initialize.
Steve Block8defd9f2010-07-08 12:39:36 +0100204 ASSERT(array->HasFastElements());
Steve Blocka7e24c12009-10-30 11:49:00 +0000205
206 // Optimize the case where there is one argument and the argument is a
207 // small smi.
208 if (args.length() == 2) {
209 Object* obj = args[1];
210 if (obj->IsSmi()) {
211 int len = Smi::cast(obj)->value();
212 if (len >= 0 && len < JSObject::kInitialMaxFastElementArray) {
John Reck59135872010-11-02 12:39:01 -0700213 Object* obj;
214 { MaybeObject* maybe_obj = Heap::AllocateFixedArrayWithHoles(len);
215 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
216 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000217 array->SetContent(FixedArray::cast(obj));
218 return array;
219 }
220 }
221 // Take the argument as the length.
John Reck59135872010-11-02 12:39:01 -0700222 { MaybeObject* maybe_obj = array->Initialize(0);
223 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
224 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000225 return array->SetElementsLength(args[1]);
226 }
227
228 // Optimize the case where there are no parameters passed.
229 if (args.length() == 1) {
230 return array->Initialize(JSArray::kPreallocatedArrayElements);
231 }
232
233 // Take the arguments as elements.
234 int number_of_elements = args.length() - 1;
235 Smi* len = Smi::FromInt(number_of_elements);
John Reck59135872010-11-02 12:39:01 -0700236 Object* obj;
237 { MaybeObject* maybe_obj = Heap::AllocateFixedArrayWithHoles(len->value());
238 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
239 }
Leon Clarke4515c472010-02-03 11:58:03 +0000240
241 AssertNoAllocation no_gc;
Steve Blocka7e24c12009-10-30 11:49:00 +0000242 FixedArray* elms = FixedArray::cast(obj);
Leon Clarke4515c472010-02-03 11:58:03 +0000243 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000244 // Fill in the content
245 for (int index = 0; index < number_of_elements; index++) {
246 elms->set(index, args[index+1], mode);
247 }
248
249 // Set length and elements on the array.
250 array->set_elements(FixedArray::cast(obj));
Leon Clarke4515c472010-02-03 11:58:03 +0000251 array->set_length(len);
Steve Blocka7e24c12009-10-30 11:49:00 +0000252
253 return array;
254}
Steve Blocka7e24c12009-10-30 11:49:00 +0000255
256
John Reck59135872010-11-02 12:39:01 -0700257MUST_USE_RESULT static MaybeObject* AllocateJSArray() {
Steve Block6ded16b2010-05-10 14:33:55 +0100258 JSFunction* array_function =
259 Top::context()->global_context()->array_function();
John Reck59135872010-11-02 12:39:01 -0700260 Object* result;
261 { MaybeObject* maybe_result = Heap::AllocateJSObject(array_function);
262 if (!maybe_result->ToObject(&result)) return maybe_result;
263 }
Steve Block6ded16b2010-05-10 14:33:55 +0100264 return result;
265}
266
267
John Reck59135872010-11-02 12:39:01 -0700268MUST_USE_RESULT static MaybeObject* AllocateEmptyJSArray() {
269 Object* result;
270 { MaybeObject* maybe_result = AllocateJSArray();
271 if (!maybe_result->ToObject(&result)) return maybe_result;
272 }
Steve Block6ded16b2010-05-10 14:33:55 +0100273 JSArray* result_array = JSArray::cast(result);
274 result_array->set_length(Smi::FromInt(0));
275 result_array->set_elements(Heap::empty_fixed_array());
276 return result_array;
277}
278
279
280static void CopyElements(AssertNoAllocation* no_gc,
281 FixedArray* dst,
282 int dst_index,
283 FixedArray* src,
284 int src_index,
285 int len) {
286 ASSERT(dst != src); // Use MoveElements instead.
Iain Merrick75681382010-08-19 15:07:18 +0100287 ASSERT(dst->map() != Heap::fixed_cow_array_map());
Steve Block6ded16b2010-05-10 14:33:55 +0100288 ASSERT(len > 0);
289 CopyWords(dst->data_start() + dst_index,
290 src->data_start() + src_index,
291 len);
292 WriteBarrierMode mode = dst->GetWriteBarrierMode(*no_gc);
293 if (mode == UPDATE_WRITE_BARRIER) {
294 Heap::RecordWrites(dst->address(), dst->OffsetOfElementAt(dst_index), len);
295 }
296}
297
298
299static void MoveElements(AssertNoAllocation* no_gc,
300 FixedArray* dst,
301 int dst_index,
302 FixedArray* src,
303 int src_index,
304 int len) {
Iain Merrick75681382010-08-19 15:07:18 +0100305 ASSERT(dst->map() != Heap::fixed_cow_array_map());
Steve Block6ded16b2010-05-10 14:33:55 +0100306 memmove(dst->data_start() + dst_index,
307 src->data_start() + src_index,
308 len * kPointerSize);
309 WriteBarrierMode mode = dst->GetWriteBarrierMode(*no_gc);
310 if (mode == UPDATE_WRITE_BARRIER) {
311 Heap::RecordWrites(dst->address(), dst->OffsetOfElementAt(dst_index), len);
312 }
313}
314
315
316static void FillWithHoles(FixedArray* dst, int from, int to) {
Iain Merrick75681382010-08-19 15:07:18 +0100317 ASSERT(dst->map() != Heap::fixed_cow_array_map());
Steve Block6ded16b2010-05-10 14:33:55 +0100318 MemsetPointer(dst->data_start() + from, Heap::the_hole_value(), to - from);
319}
320
321
322static FixedArray* LeftTrimFixedArray(FixedArray* elms, int to_trim) {
Iain Merrick75681382010-08-19 15:07:18 +0100323 ASSERT(elms->map() != Heap::fixed_cow_array_map());
Steve Block791712a2010-08-27 10:21:07 +0100324 // For now this trick is only applied to fixed arrays in new and paged space.
Steve Block6ded16b2010-05-10 14:33:55 +0100325 // In large object space the object's start must coincide with chunk
326 // and thus the trick is just not applicable.
Steve Block791712a2010-08-27 10:21:07 +0100327 ASSERT(!Heap::lo_space()->Contains(elms));
Steve Block6ded16b2010-05-10 14:33:55 +0100328
329 STATIC_ASSERT(FixedArray::kMapOffset == 0);
330 STATIC_ASSERT(FixedArray::kLengthOffset == kPointerSize);
331 STATIC_ASSERT(FixedArray::kHeaderSize == 2 * kPointerSize);
332
333 Object** former_start = HeapObject::RawField(elms, 0);
334
335 const int len = elms->length();
336
Steve Block791712a2010-08-27 10:21:07 +0100337 if (to_trim > FixedArray::kHeaderSize / kPointerSize &&
338 !Heap::new_space()->Contains(elms)) {
339 // If we are doing a big trim in old space then we zap the space that was
340 // formerly part of the array so that the GC (aided by the card-based
341 // remembered set) won't find pointers to new-space there.
342 Object** zap = reinterpret_cast<Object**>(elms->address());
343 zap++; // Header of filler must be at least one word so skip that.
344 for (int i = 1; i < to_trim; i++) {
345 *zap++ = Smi::FromInt(0);
346 }
347 }
Steve Block6ded16b2010-05-10 14:33:55 +0100348 // Technically in new space this write might be omitted (except for
349 // debug mode which iterates through the heap), but to play safer
350 // we still do it.
351 Heap::CreateFillerObjectAt(elms->address(), to_trim * kPointerSize);
352
353 former_start[to_trim] = Heap::fixed_array_map();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100354 former_start[to_trim + 1] = Smi::FromInt(len - to_trim);
Steve Block6ded16b2010-05-10 14:33:55 +0100355
Steve Block791712a2010-08-27 10:21:07 +0100356 return FixedArray::cast(HeapObject::FromAddress(
357 elms->address() + to_trim * kPointerSize));
Steve Block6ded16b2010-05-10 14:33:55 +0100358}
359
360
Kristian Monsen25f61362010-05-21 11:50:48 +0100361static bool ArrayPrototypeHasNoElements(Context* global_context,
362 JSObject* array_proto) {
Steve Block6ded16b2010-05-10 14:33:55 +0100363 // This method depends on non writability of Object and Array prototype
364 // fields.
Kristian Monsen25f61362010-05-21 11:50:48 +0100365 if (array_proto->elements() != Heap::empty_fixed_array()) return false;
Steve Block6ded16b2010-05-10 14:33:55 +0100366 // Hidden prototype
Kristian Monsen25f61362010-05-21 11:50:48 +0100367 array_proto = JSObject::cast(array_proto->GetPrototype());
368 ASSERT(array_proto->elements() == Heap::empty_fixed_array());
Steve Block6ded16b2010-05-10 14:33:55 +0100369 // Object.prototype
Kristian Monsen25f61362010-05-21 11:50:48 +0100370 array_proto = JSObject::cast(array_proto->GetPrototype());
371 if (array_proto != global_context->initial_object_prototype()) return false;
372 if (array_proto->elements() != Heap::empty_fixed_array()) return false;
373 ASSERT(array_proto->GetPrototype()->IsNull());
Steve Block6ded16b2010-05-10 14:33:55 +0100374 return true;
375}
376
377
John Reck59135872010-11-02 12:39:01 -0700378MUST_USE_RESULT
379static inline MaybeObject* EnsureJSArrayWithWritableFastElements(
380 Object* receiver) {
Iain Merrick75681382010-08-19 15:07:18 +0100381 if (!receiver->IsJSArray()) return NULL;
Steve Block6ded16b2010-05-10 14:33:55 +0100382 JSArray* array = JSArray::cast(receiver);
Steve Block9fac8402011-05-12 15:51:54 +0100383 HeapObject* elms = array->elements();
Iain Merrick75681382010-08-19 15:07:18 +0100384 if (elms->map() == Heap::fixed_array_map()) return elms;
385 if (elms->map() == Heap::fixed_cow_array_map()) {
386 return array->EnsureWritableFastElements();
Steve Block6ded16b2010-05-10 14:33:55 +0100387 }
Iain Merrick75681382010-08-19 15:07:18 +0100388 return NULL;
Steve Block6ded16b2010-05-10 14:33:55 +0100389}
390
391
Iain Merrick75681382010-08-19 15:07:18 +0100392static inline bool IsJSArrayFastElementMovingAllowed(JSArray* receiver) {
Kristian Monsen25f61362010-05-21 11:50:48 +0100393 Context* global_context = Top::context()->global_context();
394 JSObject* array_proto =
395 JSObject::cast(global_context->array_function()->prototype());
Iain Merrick75681382010-08-19 15:07:18 +0100396 return receiver->GetPrototype() == array_proto &&
397 ArrayPrototypeHasNoElements(global_context, array_proto);
Kristian Monsen25f61362010-05-21 11:50:48 +0100398}
399
400
John Reck59135872010-11-02 12:39:01 -0700401MUST_USE_RESULT static MaybeObject* CallJsBuiltin(
402 const char* name,
403 BuiltinArguments<NO_EXTRA_ARGUMENTS> args) {
Steve Block6ded16b2010-05-10 14:33:55 +0100404 HandleScope handleScope;
405
406 Handle<Object> js_builtin =
407 GetProperty(Handle<JSObject>(Top::global_context()->builtins()),
408 name);
409 ASSERT(js_builtin->IsJSFunction());
410 Handle<JSFunction> function(Handle<JSFunction>::cast(js_builtin));
Kristian Monsen25f61362010-05-21 11:50:48 +0100411 ScopedVector<Object**> argv(args.length() - 1);
Steve Block6ded16b2010-05-10 14:33:55 +0100412 int n_args = args.length() - 1;
413 for (int i = 0; i < n_args; i++) {
414 argv[i] = args.at<Object>(i + 1).location();
415 }
416 bool pending_exception = false;
417 Handle<Object> result = Execution::Call(function,
418 args.receiver(),
419 n_args,
420 argv.start(),
421 &pending_exception);
Steve Block6ded16b2010-05-10 14:33:55 +0100422 if (pending_exception) return Failure::Exception();
423 return *result;
424}
425
426
Steve Blocka7e24c12009-10-30 11:49:00 +0000427BUILTIN(ArrayPush) {
Steve Block6ded16b2010-05-10 14:33:55 +0100428 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700429 Object* elms_obj;
430 { MaybeObject* maybe_elms_obj =
431 EnsureJSArrayWithWritableFastElements(receiver);
432 if (maybe_elms_obj == NULL) return CallJsBuiltin("ArrayPush", args);
433 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
434 }
Iain Merrick75681382010-08-19 15:07:18 +0100435 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100436 JSArray* array = JSArray::cast(receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +0000437
Steve Blocka7e24c12009-10-30 11:49:00 +0000438 int len = Smi::cast(array->length())->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000439 int to_add = args.length() - 1;
440 if (to_add == 0) {
441 return Smi::FromInt(len);
442 }
443 // Currently fixed arrays cannot grow too big, so
444 // we should never hit this case.
445 ASSERT(to_add <= (Smi::kMaxValue - len));
Steve Blocka7e24c12009-10-30 11:49:00 +0000446
Andrei Popescu402d9372010-02-26 13:31:12 +0000447 int new_length = len + to_add;
Steve Blocka7e24c12009-10-30 11:49:00 +0000448
Andrei Popescu402d9372010-02-26 13:31:12 +0000449 if (new_length > elms->length()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000450 // New backing storage is needed.
451 int capacity = new_length + (new_length >> 1) + 16;
John Reck59135872010-11-02 12:39:01 -0700452 Object* obj;
453 { MaybeObject* maybe_obj = Heap::AllocateUninitializedFixedArray(capacity);
454 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
455 }
Steve Block6ded16b2010-05-10 14:33:55 +0100456 FixedArray* new_elms = FixedArray::cast(obj);
Leon Clarke4515c472010-02-03 11:58:03 +0000457
458 AssertNoAllocation no_gc;
Steve Block6ded16b2010-05-10 14:33:55 +0100459 if (len > 0) {
460 CopyElements(&no_gc, new_elms, 0, elms, 0, len);
461 }
462 FillWithHoles(new_elms, new_length, capacity);
463
Andrei Popescu402d9372010-02-26 13:31:12 +0000464 elms = new_elms;
465 array->set_elements(elms);
Steve Blocka7e24c12009-10-30 11:49:00 +0000466 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000467
Steve Block6ded16b2010-05-10 14:33:55 +0100468 // Add the provided values.
Andrei Popescu402d9372010-02-26 13:31:12 +0000469 AssertNoAllocation no_gc;
470 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
Andrei Popescu402d9372010-02-26 13:31:12 +0000471 for (int index = 0; index < to_add; index++) {
472 elms->set(index + len, args[index + 1], mode);
473 }
474
Steve Blocka7e24c12009-10-30 11:49:00 +0000475 // Set the length.
Leon Clarke4515c472010-02-03 11:58:03 +0000476 array->set_length(Smi::FromInt(new_length));
Andrei Popescu402d9372010-02-26 13:31:12 +0000477 return Smi::FromInt(new_length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000478}
Steve Blocka7e24c12009-10-30 11:49:00 +0000479
480
481BUILTIN(ArrayPop) {
Steve Block6ded16b2010-05-10 14:33:55 +0100482 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700483 Object* elms_obj;
484 { MaybeObject* maybe_elms_obj =
485 EnsureJSArrayWithWritableFastElements(receiver);
486 if (maybe_elms_obj == NULL) return CallJsBuiltin("ArrayPop", args);
487 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
488 }
Iain Merrick75681382010-08-19 15:07:18 +0100489 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100490 JSArray* array = JSArray::cast(receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +0000491
492 int len = Smi::cast(array->length())->value();
Steve Block6ded16b2010-05-10 14:33:55 +0100493 if (len == 0) return Heap::undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000494
495 // Get top element
John Reck59135872010-11-02 12:39:01 -0700496 MaybeObject* top = elms->get(len - 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000497
498 // Set the length.
Leon Clarke4515c472010-02-03 11:58:03 +0000499 array->set_length(Smi::FromInt(len - 1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000500
501 if (!top->IsTheHole()) {
502 // Delete the top element.
503 elms->set_the_hole(len - 1);
504 return top;
505 }
506
Kristian Monsen25f61362010-05-21 11:50:48 +0100507 top = array->GetPrototype()->GetElement(len - 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000508
509 return top;
510}
Steve Blocka7e24c12009-10-30 11:49:00 +0000511
512
Andrei Popescu402d9372010-02-26 13:31:12 +0000513BUILTIN(ArrayShift) {
Steve Block6ded16b2010-05-10 14:33:55 +0100514 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700515 Object* elms_obj;
516 { MaybeObject* maybe_elms_obj =
517 EnsureJSArrayWithWritableFastElements(receiver);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100518 if (maybe_elms_obj == NULL) return CallJsBuiltin("ArrayShift", args);
John Reck59135872010-11-02 12:39:01 -0700519 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
520 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100521 if (!IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) {
Steve Block6ded16b2010-05-10 14:33:55 +0100522 return CallJsBuiltin("ArrayShift", args);
523 }
Iain Merrick75681382010-08-19 15:07:18 +0100524 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100525 JSArray* array = JSArray::cast(receiver);
Andrei Popescu402d9372010-02-26 13:31:12 +0000526 ASSERT(array->HasFastElements());
527
528 int len = Smi::cast(array->length())->value();
529 if (len == 0) return Heap::undefined_value();
530
Andrei Popescu402d9372010-02-26 13:31:12 +0000531 // Get first element
532 Object* first = elms->get(0);
533 if (first->IsTheHole()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100534 first = Heap::undefined_value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000535 }
536
Steve Block791712a2010-08-27 10:21:07 +0100537 if (!Heap::lo_space()->Contains(elms)) {
538 // As elms still in the same space they used to be,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100539 // there is no need to update region dirty mark.
Steve Block6ded16b2010-05-10 14:33:55 +0100540 array->set_elements(LeftTrimFixedArray(elms, 1), SKIP_WRITE_BARRIER);
541 } else {
542 // Shift the elements.
543 AssertNoAllocation no_gc;
544 MoveElements(&no_gc, elms, 0, elms, 1, len - 1);
545 elms->set(len - 1, Heap::the_hole_value());
Andrei Popescu402d9372010-02-26 13:31:12 +0000546 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000547
548 // Set the length.
549 array->set_length(Smi::FromInt(len - 1));
550
551 return first;
552}
553
554
555BUILTIN(ArrayUnshift) {
Steve Block6ded16b2010-05-10 14:33:55 +0100556 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700557 Object* elms_obj;
558 { MaybeObject* maybe_elms_obj =
559 EnsureJSArrayWithWritableFastElements(receiver);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100560 if (maybe_elms_obj == NULL) return CallJsBuiltin("ArrayUnshift", args);
John Reck59135872010-11-02 12:39:01 -0700561 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
562 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100563 if (!IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) {
Steve Block6ded16b2010-05-10 14:33:55 +0100564 return CallJsBuiltin("ArrayUnshift", args);
565 }
Iain Merrick75681382010-08-19 15:07:18 +0100566 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100567 JSArray* array = JSArray::cast(receiver);
Andrei Popescu402d9372010-02-26 13:31:12 +0000568 ASSERT(array->HasFastElements());
569
570 int len = Smi::cast(array->length())->value();
571 int to_add = args.length() - 1;
Andrei Popescu402d9372010-02-26 13:31:12 +0000572 int new_length = len + to_add;
573 // Currently fixed arrays cannot grow too big, so
574 // we should never hit this case.
575 ASSERT(to_add <= (Smi::kMaxValue - len));
576
Andrei Popescu402d9372010-02-26 13:31:12 +0000577 if (new_length > elms->length()) {
578 // New backing storage is needed.
579 int capacity = new_length + (new_length >> 1) + 16;
John Reck59135872010-11-02 12:39:01 -0700580 Object* obj;
581 { MaybeObject* maybe_obj = Heap::AllocateUninitializedFixedArray(capacity);
582 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
583 }
Steve Block6ded16b2010-05-10 14:33:55 +0100584 FixedArray* new_elms = FixedArray::cast(obj);
Andrei Popescu402d9372010-02-26 13:31:12 +0000585
586 AssertNoAllocation no_gc;
Steve Block6ded16b2010-05-10 14:33:55 +0100587 if (len > 0) {
588 CopyElements(&no_gc, new_elms, to_add, elms, 0, len);
589 }
590 FillWithHoles(new_elms, new_length, capacity);
Andrei Popescu402d9372010-02-26 13:31:12 +0000591
592 elms = new_elms;
593 array->set_elements(elms);
594 } else {
595 AssertNoAllocation no_gc;
Steve Block6ded16b2010-05-10 14:33:55 +0100596 MoveElements(&no_gc, elms, to_add, elms, 0, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000597 }
598
599 // Add the provided values.
600 AssertNoAllocation no_gc;
601 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
602 for (int i = 0; i < to_add; i++) {
603 elms->set(i, args[i + 1], mode);
604 }
605
606 // Set the length.
607 array->set_length(Smi::FromInt(new_length));
608 return Smi::FromInt(new_length);
609}
610
611
Andrei Popescu402d9372010-02-26 13:31:12 +0000612BUILTIN(ArraySlice) {
Steve Block6ded16b2010-05-10 14:33:55 +0100613 Object* receiver = *args.receiver();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100614 FixedArray* elms;
615 int len = -1;
Steve Block9fac8402011-05-12 15:51:54 +0100616 if (receiver->IsJSArray()) {
617 JSArray* array = JSArray::cast(receiver);
618 if (!array->HasFastElements() ||
619 !IsJSArrayFastElementMovingAllowed(array)) {
620 return CallJsBuiltin("ArraySlice", args);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100621 }
Steve Block9fac8402011-05-12 15:51:54 +0100622
623 elms = FixedArray::cast(array->elements());
624 len = Smi::cast(array->length())->value();
625 } else {
626 // Array.slice(arguments, ...) is quite a common idiom (notably more
627 // than 50% of invocations in Web apps). Treat it in C++ as well.
628 Map* arguments_map =
629 Top::context()->global_context()->arguments_boilerplate()->map();
630
631 bool is_arguments_object_with_fast_elements =
632 receiver->IsJSObject()
633 && JSObject::cast(receiver)->map() == arguments_map
634 && JSObject::cast(receiver)->HasFastElements();
635 if (!is_arguments_object_with_fast_elements) {
636 return CallJsBuiltin("ArraySlice", args);
637 }
638 elms = FixedArray::cast(JSObject::cast(receiver)->elements());
639 len = elms->length();
640#ifdef DEBUG
641 // Arguments object by construction should have no holes, check it.
642 if (FLAG_enable_slow_asserts) {
643 for (int i = 0; i < len; i++) {
644 ASSERT(elms->get(i) != Heap::the_hole_value());
645 }
646 }
647#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +0100648 }
649 ASSERT(len >= 0);
Andrei Popescu402d9372010-02-26 13:31:12 +0000650 int n_arguments = args.length() - 1;
651
652 // Note carefully choosen defaults---if argument is missing,
Steve Block6ded16b2010-05-10 14:33:55 +0100653 // it's undefined which gets converted to 0 for relative_start
654 // and to len for relative_end.
655 int relative_start = 0;
656 int relative_end = len;
Andrei Popescu402d9372010-02-26 13:31:12 +0000657 if (n_arguments > 0) {
658 Object* arg1 = args[1];
659 if (arg1->IsSmi()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100660 relative_start = Smi::cast(arg1)->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000661 } else if (!arg1->IsUndefined()) {
662 return CallJsBuiltin("ArraySlice", args);
663 }
664 if (n_arguments > 1) {
665 Object* arg2 = args[2];
666 if (arg2->IsSmi()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100667 relative_end = Smi::cast(arg2)->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000668 } else if (!arg2->IsUndefined()) {
669 return CallJsBuiltin("ArraySlice", args);
670 }
671 }
672 }
673
674 // ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 6.
Steve Block6ded16b2010-05-10 14:33:55 +0100675 int k = (relative_start < 0) ? Max(len + relative_start, 0)
676 : Min(relative_start, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000677
678 // ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 8.
Steve Block6ded16b2010-05-10 14:33:55 +0100679 int final = (relative_end < 0) ? Max(len + relative_end, 0)
680 : Min(relative_end, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000681
682 // Calculate the length of result array.
683 int result_len = final - k;
Steve Block6ded16b2010-05-10 14:33:55 +0100684 if (result_len <= 0) {
685 return AllocateEmptyJSArray();
Andrei Popescu402d9372010-02-26 13:31:12 +0000686 }
687
John Reck59135872010-11-02 12:39:01 -0700688 Object* result;
689 { MaybeObject* maybe_result = AllocateJSArray();
690 if (!maybe_result->ToObject(&result)) return maybe_result;
691 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000692 JSArray* result_array = JSArray::cast(result);
693
John Reck59135872010-11-02 12:39:01 -0700694 { MaybeObject* maybe_result =
695 Heap::AllocateUninitializedFixedArray(result_len);
696 if (!maybe_result->ToObject(&result)) return maybe_result;
697 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000698 FixedArray* result_elms = FixedArray::cast(result);
699
Andrei Popescu402d9372010-02-26 13:31:12 +0000700 AssertNoAllocation no_gc;
Steve Block6ded16b2010-05-10 14:33:55 +0100701 CopyElements(&no_gc, result_elms, 0, elms, k, result_len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000702
703 // Set elements.
704 result_array->set_elements(result_elms);
705
706 // Set the length.
707 result_array->set_length(Smi::FromInt(result_len));
708 return result_array;
709}
710
711
712BUILTIN(ArraySplice) {
Steve Block6ded16b2010-05-10 14:33:55 +0100713 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700714 Object* elms_obj;
715 { MaybeObject* maybe_elms_obj =
716 EnsureJSArrayWithWritableFastElements(receiver);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100717 if (maybe_elms_obj == NULL) return CallJsBuiltin("ArraySplice", args);
John Reck59135872010-11-02 12:39:01 -0700718 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
719 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100720 if (!IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) {
Steve Block6ded16b2010-05-10 14:33:55 +0100721 return CallJsBuiltin("ArraySplice", args);
722 }
Iain Merrick75681382010-08-19 15:07:18 +0100723 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100724 JSArray* array = JSArray::cast(receiver);
Andrei Popescu402d9372010-02-26 13:31:12 +0000725 ASSERT(array->HasFastElements());
726
727 int len = Smi::cast(array->length())->value();
728
729 int n_arguments = args.length() - 1;
730
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100731 // Return empty array when no arguments are supplied.
Andrei Popescu402d9372010-02-26 13:31:12 +0000732 if (n_arguments == 0) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100733 return AllocateEmptyJSArray();
Andrei Popescu402d9372010-02-26 13:31:12 +0000734 }
735
Steve Block6ded16b2010-05-10 14:33:55 +0100736 int relative_start = 0;
Andrei Popescu402d9372010-02-26 13:31:12 +0000737 Object* arg1 = args[1];
738 if (arg1->IsSmi()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100739 relative_start = Smi::cast(arg1)->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000740 } else if (!arg1->IsUndefined()) {
741 return CallJsBuiltin("ArraySplice", args);
742 }
Steve Block6ded16b2010-05-10 14:33:55 +0100743 int actual_start = (relative_start < 0) ? Max(len + relative_start, 0)
744 : Min(relative_start, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000745
746 // SpiderMonkey, TraceMonkey and JSC treat the case where no delete count is
747 // given differently from when an undefined delete count is given.
748 // This does not follow ECMA-262, but we do the same for
749 // compatibility.
Steve Block6ded16b2010-05-10 14:33:55 +0100750 int delete_count = len;
Andrei Popescu402d9372010-02-26 13:31:12 +0000751 if (n_arguments > 1) {
752 Object* arg2 = args[2];
753 if (arg2->IsSmi()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100754 delete_count = Smi::cast(arg2)->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000755 } else {
756 return CallJsBuiltin("ArraySplice", args);
757 }
758 }
Steve Block6ded16b2010-05-10 14:33:55 +0100759 int actual_delete_count = Min(Max(delete_count, 0), len - actual_start);
Andrei Popescu402d9372010-02-26 13:31:12 +0000760
Steve Block6ded16b2010-05-10 14:33:55 +0100761 JSArray* result_array = NULL;
762 if (actual_delete_count == 0) {
John Reck59135872010-11-02 12:39:01 -0700763 Object* result;
764 { MaybeObject* maybe_result = AllocateEmptyJSArray();
765 if (!maybe_result->ToObject(&result)) return maybe_result;
766 }
Steve Block6ded16b2010-05-10 14:33:55 +0100767 result_array = JSArray::cast(result);
768 } else {
769 // Allocate result array.
John Reck59135872010-11-02 12:39:01 -0700770 Object* result;
771 { MaybeObject* maybe_result = AllocateJSArray();
772 if (!maybe_result->ToObject(&result)) return maybe_result;
773 }
Steve Block6ded16b2010-05-10 14:33:55 +0100774 result_array = JSArray::cast(result);
Andrei Popescu402d9372010-02-26 13:31:12 +0000775
John Reck59135872010-11-02 12:39:01 -0700776 { MaybeObject* maybe_result =
777 Heap::AllocateUninitializedFixedArray(actual_delete_count);
778 if (!maybe_result->ToObject(&result)) return maybe_result;
779 }
Steve Block6ded16b2010-05-10 14:33:55 +0100780 FixedArray* result_elms = FixedArray::cast(result);
Andrei Popescu402d9372010-02-26 13:31:12 +0000781
Steve Block6ded16b2010-05-10 14:33:55 +0100782 AssertNoAllocation no_gc;
783 // Fill newly created array.
784 CopyElements(&no_gc,
785 result_elms, 0,
786 elms, actual_start,
787 actual_delete_count);
Andrei Popescu402d9372010-02-26 13:31:12 +0000788
Steve Block6ded16b2010-05-10 14:33:55 +0100789 // Set elements.
790 result_array->set_elements(result_elms);
Andrei Popescu402d9372010-02-26 13:31:12 +0000791
Steve Block6ded16b2010-05-10 14:33:55 +0100792 // Set the length.
793 result_array->set_length(Smi::FromInt(actual_delete_count));
Andrei Popescu402d9372010-02-26 13:31:12 +0000794 }
795
Steve Block6ded16b2010-05-10 14:33:55 +0100796 int item_count = (n_arguments > 1) ? (n_arguments - 2) : 0;
Andrei Popescu402d9372010-02-26 13:31:12 +0000797
Steve Block6ded16b2010-05-10 14:33:55 +0100798 int new_length = len - actual_delete_count + item_count;
Andrei Popescu402d9372010-02-26 13:31:12 +0000799
Steve Block6ded16b2010-05-10 14:33:55 +0100800 if (item_count < actual_delete_count) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000801 // Shrink the array.
Steve Block791712a2010-08-27 10:21:07 +0100802 const bool trim_array = !Heap::lo_space()->Contains(elms) &&
Steve Block6ded16b2010-05-10 14:33:55 +0100803 ((actual_start + item_count) <
804 (len - actual_delete_count - actual_start));
805 if (trim_array) {
806 const int delta = actual_delete_count - item_count;
Andrei Popescu402d9372010-02-26 13:31:12 +0000807
Steve Block6ded16b2010-05-10 14:33:55 +0100808 if (actual_start > 0) {
809 Object** start = elms->data_start();
810 memmove(start + delta, start, actual_start * kPointerSize);
811 }
812
813 elms = LeftTrimFixedArray(elms, delta);
814 array->set_elements(elms, SKIP_WRITE_BARRIER);
815 } else {
816 AssertNoAllocation no_gc;
817 MoveElements(&no_gc,
818 elms, actual_start + item_count,
819 elms, actual_start + actual_delete_count,
820 (len - actual_delete_count - actual_start));
821 FillWithHoles(elms, new_length, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000822 }
Steve Block6ded16b2010-05-10 14:33:55 +0100823 } else if (item_count > actual_delete_count) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000824 // Currently fixed arrays cannot grow too big, so
825 // we should never hit this case.
Steve Block6ded16b2010-05-10 14:33:55 +0100826 ASSERT((item_count - actual_delete_count) <= (Smi::kMaxValue - len));
Andrei Popescu402d9372010-02-26 13:31:12 +0000827
828 // Check if array need to grow.
829 if (new_length > elms->length()) {
830 // New backing storage is needed.
831 int capacity = new_length + (new_length >> 1) + 16;
John Reck59135872010-11-02 12:39:01 -0700832 Object* obj;
833 { MaybeObject* maybe_obj =
834 Heap::AllocateUninitializedFixedArray(capacity);
835 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
836 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000837 FixedArray* new_elms = FixedArray::cast(obj);
Andrei Popescu402d9372010-02-26 13:31:12 +0000838
Steve Block6ded16b2010-05-10 14:33:55 +0100839 AssertNoAllocation no_gc;
840 // Copy the part before actual_start as is.
841 if (actual_start > 0) {
842 CopyElements(&no_gc, new_elms, 0, elms, 0, actual_start);
Andrei Popescu402d9372010-02-26 13:31:12 +0000843 }
Steve Block6ded16b2010-05-10 14:33:55 +0100844 const int to_copy = len - actual_delete_count - actual_start;
845 if (to_copy > 0) {
846 CopyElements(&no_gc,
847 new_elms, actual_start + item_count,
848 elms, actual_start + actual_delete_count,
849 to_copy);
850 }
851 FillWithHoles(new_elms, new_length, capacity);
Andrei Popescu402d9372010-02-26 13:31:12 +0000852
Andrei Popescu402d9372010-02-26 13:31:12 +0000853 elms = new_elms;
854 array->set_elements(elms);
Steve Block6ded16b2010-05-10 14:33:55 +0100855 } else {
856 AssertNoAllocation no_gc;
857 MoveElements(&no_gc,
858 elms, actual_start + item_count,
859 elms, actual_start + actual_delete_count,
860 (len - actual_delete_count - actual_start));
Andrei Popescu402d9372010-02-26 13:31:12 +0000861 }
862 }
863
Steve Block6ded16b2010-05-10 14:33:55 +0100864 AssertNoAllocation no_gc;
865 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
866 for (int k = actual_start; k < actual_start + item_count; k++) {
867 elms->set(k, args[3 + k - actual_start], mode);
Andrei Popescu402d9372010-02-26 13:31:12 +0000868 }
869
870 // Set the length.
871 array->set_length(Smi::FromInt(new_length));
872
873 return result_array;
874}
875
876
Steve Block6ded16b2010-05-10 14:33:55 +0100877BUILTIN(ArrayConcat) {
Kristian Monsen25f61362010-05-21 11:50:48 +0100878 Context* global_context = Top::context()->global_context();
879 JSObject* array_proto =
880 JSObject::cast(global_context->array_function()->prototype());
881 if (!ArrayPrototypeHasNoElements(global_context, array_proto)) {
Steve Block6ded16b2010-05-10 14:33:55 +0100882 return CallJsBuiltin("ArrayConcat", args);
883 }
884
885 // Iterate through all the arguments performing checks
886 // and calculating total length.
887 int n_arguments = args.length();
888 int result_len = 0;
889 for (int i = 0; i < n_arguments; i++) {
890 Object* arg = args[i];
Kristian Monsen25f61362010-05-21 11:50:48 +0100891 if (!arg->IsJSArray() || !JSArray::cast(arg)->HasFastElements()
892 || JSArray::cast(arg)->GetPrototype() != array_proto) {
Steve Block6ded16b2010-05-10 14:33:55 +0100893 return CallJsBuiltin("ArrayConcat", args);
894 }
895
896 int len = Smi::cast(JSArray::cast(arg)->length())->value();
897
898 // We shouldn't overflow when adding another len.
899 const int kHalfOfMaxInt = 1 << (kBitsPerInt - 2);
900 STATIC_ASSERT(FixedArray::kMaxLength < kHalfOfMaxInt);
901 USE(kHalfOfMaxInt);
902 result_len += len;
903 ASSERT(result_len >= 0);
904
905 if (result_len > FixedArray::kMaxLength) {
906 return CallJsBuiltin("ArrayConcat", args);
907 }
908 }
909
910 if (result_len == 0) {
911 return AllocateEmptyJSArray();
912 }
913
914 // Allocate result.
John Reck59135872010-11-02 12:39:01 -0700915 Object* result;
916 { MaybeObject* maybe_result = AllocateJSArray();
917 if (!maybe_result->ToObject(&result)) return maybe_result;
918 }
Steve Block6ded16b2010-05-10 14:33:55 +0100919 JSArray* result_array = JSArray::cast(result);
920
John Reck59135872010-11-02 12:39:01 -0700921 { MaybeObject* maybe_result =
922 Heap::AllocateUninitializedFixedArray(result_len);
923 if (!maybe_result->ToObject(&result)) return maybe_result;
924 }
Steve Block6ded16b2010-05-10 14:33:55 +0100925 FixedArray* result_elms = FixedArray::cast(result);
926
927 // Copy data.
928 AssertNoAllocation no_gc;
929 int start_pos = 0;
930 for (int i = 0; i < n_arguments; i++) {
931 JSArray* array = JSArray::cast(args[i]);
932 int len = Smi::cast(array->length())->value();
933 if (len > 0) {
934 FixedArray* elms = FixedArray::cast(array->elements());
935 CopyElements(&no_gc, result_elms, start_pos, elms, 0, len);
936 start_pos += len;
937 }
938 }
939 ASSERT(start_pos == result_len);
940
941 // Set the length and elements.
942 result_array->set_length(Smi::FromInt(result_len));
943 result_array->set_elements(result_elms);
944
945 return result_array;
946}
947
948
Steve Blocka7e24c12009-10-30 11:49:00 +0000949// -----------------------------------------------------------------------------
950//
951
952
953// Returns the holder JSObject if the function can legally be called
954// with this receiver. Returns Heap::null_value() if the call is
955// illegal. Any arguments that don't fit the expected type is
956// overwritten with undefined. Arguments that do fit the expected
957// type is overwritten with the object in the prototype chain that
958// actually has that type.
959static inline Object* TypeCheck(int argc,
960 Object** argv,
961 FunctionTemplateInfo* info) {
962 Object* recv = argv[0];
963 Object* sig_obj = info->signature();
964 if (sig_obj->IsUndefined()) return recv;
965 SignatureInfo* sig = SignatureInfo::cast(sig_obj);
966 // If necessary, check the receiver
967 Object* recv_type = sig->receiver();
968
969 Object* holder = recv;
970 if (!recv_type->IsUndefined()) {
971 for (; holder != Heap::null_value(); holder = holder->GetPrototype()) {
972 if (holder->IsInstanceOf(FunctionTemplateInfo::cast(recv_type))) {
973 break;
974 }
975 }
976 if (holder == Heap::null_value()) return holder;
977 }
978 Object* args_obj = sig->args();
979 // If there is no argument signature we're done
980 if (args_obj->IsUndefined()) return holder;
981 FixedArray* args = FixedArray::cast(args_obj);
982 int length = args->length();
983 if (argc <= length) length = argc - 1;
984 for (int i = 0; i < length; i++) {
985 Object* argtype = args->get(i);
986 if (argtype->IsUndefined()) continue;
987 Object** arg = &argv[-1 - i];
988 Object* current = *arg;
989 for (; current != Heap::null_value(); current = current->GetPrototype()) {
990 if (current->IsInstanceOf(FunctionTemplateInfo::cast(argtype))) {
991 *arg = current;
992 break;
993 }
994 }
995 if (current == Heap::null_value()) *arg = Heap::undefined_value();
996 }
997 return holder;
998}
999
1000
Leon Clarkee46be812010-01-19 14:06:41 +00001001template <bool is_construct>
John Reck59135872010-11-02 12:39:01 -07001002MUST_USE_RESULT static MaybeObject* HandleApiCallHelper(
Leon Clarkee46be812010-01-19 14:06:41 +00001003 BuiltinArguments<NEEDS_CALLED_FUNCTION> args) {
1004 ASSERT(is_construct == CalledAsConstructor());
Steve Blocka7e24c12009-10-30 11:49:00 +00001005
Leon Clarkee46be812010-01-19 14:06:41 +00001006 HandleScope scope;
1007 Handle<JSFunction> function = args.called_function();
Steve Block6ded16b2010-05-10 14:33:55 +01001008 ASSERT(function->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +00001009
Steve Block6ded16b2010-05-10 14:33:55 +01001010 FunctionTemplateInfo* fun_data = function->shared()->get_api_func_data();
Steve Blocka7e24c12009-10-30 11:49:00 +00001011 if (is_construct) {
Steve Block6ded16b2010-05-10 14:33:55 +01001012 Handle<FunctionTemplateInfo> desc(fun_data);
Steve Blocka7e24c12009-10-30 11:49:00 +00001013 bool pending_exception = false;
Leon Clarkee46be812010-01-19 14:06:41 +00001014 Factory::ConfigureInstance(desc, Handle<JSObject>::cast(args.receiver()),
Steve Blocka7e24c12009-10-30 11:49:00 +00001015 &pending_exception);
1016 ASSERT(Top::has_pending_exception() == pending_exception);
1017 if (pending_exception) return Failure::Exception();
Steve Block6ded16b2010-05-10 14:33:55 +01001018 fun_data = *desc;
Steve Blocka7e24c12009-10-30 11:49:00 +00001019 }
1020
Steve Blocka7e24c12009-10-30 11:49:00 +00001021 Object* raw_holder = TypeCheck(args.length(), &args[0], fun_data);
1022
1023 if (raw_holder->IsNull()) {
1024 // This function cannot be called with the given receiver. Abort!
1025 Handle<Object> obj =
1026 Factory::NewTypeError("illegal_invocation", HandleVector(&function, 1));
1027 return Top::Throw(*obj);
1028 }
1029
1030 Object* raw_call_data = fun_data->call_code();
1031 if (!raw_call_data->IsUndefined()) {
1032 CallHandlerInfo* call_data = CallHandlerInfo::cast(raw_call_data);
1033 Object* callback_obj = call_data->callback();
1034 v8::InvocationCallback callback =
1035 v8::ToCData<v8::InvocationCallback>(callback_obj);
1036 Object* data_obj = call_data->data();
1037 Object* result;
1038
Leon Clarkee46be812010-01-19 14:06:41 +00001039 LOG(ApiObjectAccess("call", JSObject::cast(*args.receiver())));
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001040 ASSERT(raw_holder->IsJSObject());
1041
1042 CustomArguments custom;
1043 v8::ImplementationUtilities::PrepareArgumentsData(custom.end(),
1044 data_obj, *function, raw_holder);
1045
Steve Blocka7e24c12009-10-30 11:49:00 +00001046 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments(
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001047 custom.end(),
1048 &args[0] - 1,
1049 args.length() - 1,
1050 is_construct);
Steve Blocka7e24c12009-10-30 11:49:00 +00001051
1052 v8::Handle<v8::Value> value;
1053 {
1054 // Leaving JavaScript.
1055 VMState state(EXTERNAL);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001056 ExternalCallbackScope call_scope(v8::ToCData<Address>(callback_obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00001057 value = callback(new_args);
1058 }
1059 if (value.IsEmpty()) {
1060 result = Heap::undefined_value();
1061 } else {
1062 result = *reinterpret_cast<Object**>(*value);
1063 }
1064
1065 RETURN_IF_SCHEDULED_EXCEPTION();
1066 if (!is_construct || result->IsJSObject()) return result;
1067 }
1068
Leon Clarkee46be812010-01-19 14:06:41 +00001069 return *args.receiver();
Steve Blocka7e24c12009-10-30 11:49:00 +00001070}
Leon Clarkee46be812010-01-19 14:06:41 +00001071
1072
1073BUILTIN(HandleApiCall) {
1074 return HandleApiCallHelper<false>(args);
1075}
1076
1077
1078BUILTIN(HandleApiCallConstruct) {
1079 return HandleApiCallHelper<true>(args);
1080}
Steve Blocka7e24c12009-10-30 11:49:00 +00001081
1082
Andrei Popescu402d9372010-02-26 13:31:12 +00001083#ifdef DEBUG
1084
1085static void VerifyTypeCheck(Handle<JSObject> object,
1086 Handle<JSFunction> function) {
Steve Block6ded16b2010-05-10 14:33:55 +01001087 ASSERT(function->shared()->IsApiFunction());
1088 FunctionTemplateInfo* info = function->shared()->get_api_func_data();
Andrei Popescu402d9372010-02-26 13:31:12 +00001089 if (info->signature()->IsUndefined()) return;
1090 SignatureInfo* signature = SignatureInfo::cast(info->signature());
1091 Object* receiver_type = signature->receiver();
1092 if (receiver_type->IsUndefined()) return;
1093 FunctionTemplateInfo* type = FunctionTemplateInfo::cast(receiver_type);
1094 ASSERT(object->IsInstanceOf(type));
1095}
1096
1097#endif
1098
1099
1100BUILTIN(FastHandleApiCall) {
1101 ASSERT(!CalledAsConstructor());
1102 const bool is_construct = false;
1103
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001104 // We expect four more arguments: callback, function, call data, and holder.
Andrei Popescu402d9372010-02-26 13:31:12 +00001105 const int args_length = args.length() - 4;
1106 ASSERT(args_length >= 0);
1107
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001108 Object* callback_obj = args[args_length];
Andrei Popescu402d9372010-02-26 13:31:12 +00001109
1110 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments(
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001111 &args[args_length + 1],
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001112 &args[0] - 1,
1113 args_length - 1,
1114 is_construct);
Andrei Popescu402d9372010-02-26 13:31:12 +00001115
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001116#ifdef DEBUG
1117 VerifyTypeCheck(Utils::OpenHandle(*new_args.Holder()),
1118 Utils::OpenHandle(*new_args.Callee()));
1119#endif
Andrei Popescu402d9372010-02-26 13:31:12 +00001120 HandleScope scope;
1121 Object* result;
1122 v8::Handle<v8::Value> value;
1123 {
1124 // Leaving JavaScript.
1125 VMState state(EXTERNAL);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001126 ExternalCallbackScope call_scope(v8::ToCData<Address>(callback_obj));
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001127 v8::InvocationCallback callback =
1128 v8::ToCData<v8::InvocationCallback>(callback_obj);
1129
Andrei Popescu402d9372010-02-26 13:31:12 +00001130 value = callback(new_args);
1131 }
1132 if (value.IsEmpty()) {
1133 result = Heap::undefined_value();
1134 } else {
1135 result = *reinterpret_cast<Object**>(*value);
1136 }
1137
1138 RETURN_IF_SCHEDULED_EXCEPTION();
1139 return result;
1140}
1141
1142
Steve Blocka7e24c12009-10-30 11:49:00 +00001143// Helper function to handle calls to non-function objects created through the
1144// API. The object can be called as either a constructor (using new) or just as
1145// a function (without new).
John Reck59135872010-11-02 12:39:01 -07001146MUST_USE_RESULT static MaybeObject* HandleApiCallAsFunctionOrConstructor(
Leon Clarkee46be812010-01-19 14:06:41 +00001147 bool is_construct_call,
1148 BuiltinArguments<NO_EXTRA_ARGUMENTS> args) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001149 // Non-functions are never called as constructors. Even if this is an object
1150 // called as a constructor the delegate call is not a construct call.
1151 ASSERT(!CalledAsConstructor());
1152
1153 Handle<Object> receiver = args.at<Object>(0);
1154
1155 // Get the object called.
Leon Clarkee46be812010-01-19 14:06:41 +00001156 JSObject* obj = JSObject::cast(*args.receiver());
Steve Blocka7e24c12009-10-30 11:49:00 +00001157
1158 // Get the invocation callback from the function descriptor that was
1159 // used to create the called object.
1160 ASSERT(obj->map()->has_instance_call_handler());
1161 JSFunction* constructor = JSFunction::cast(obj->map()->constructor());
Steve Block6ded16b2010-05-10 14:33:55 +01001162 ASSERT(constructor->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +00001163 Object* handler =
Steve Block6ded16b2010-05-10 14:33:55 +01001164 constructor->shared()->get_api_func_data()->instance_call_handler();
Steve Blocka7e24c12009-10-30 11:49:00 +00001165 ASSERT(!handler->IsUndefined());
1166 CallHandlerInfo* call_data = CallHandlerInfo::cast(handler);
1167 Object* callback_obj = call_data->callback();
1168 v8::InvocationCallback callback =
1169 v8::ToCData<v8::InvocationCallback>(callback_obj);
1170
1171 // Get the data for the call and perform the callback.
Steve Blocka7e24c12009-10-30 11:49:00 +00001172 Object* result;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001173 {
1174 HandleScope scope;
1175
1176 LOG(ApiObjectAccess("call non-function", obj));
1177
1178 CustomArguments custom;
1179 v8::ImplementationUtilities::PrepareArgumentsData(custom.end(),
1180 call_data->data(), constructor, obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001181 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments(
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001182 custom.end(),
1183 &args[0] - 1,
1184 args.length() - 1,
1185 is_construct_call);
Steve Blocka7e24c12009-10-30 11:49:00 +00001186 v8::Handle<v8::Value> value;
1187 {
1188 // Leaving JavaScript.
1189 VMState state(EXTERNAL);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001190 ExternalCallbackScope call_scope(v8::ToCData<Address>(callback_obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00001191 value = callback(new_args);
1192 }
1193 if (value.IsEmpty()) {
1194 result = Heap::undefined_value();
1195 } else {
1196 result = *reinterpret_cast<Object**>(*value);
1197 }
1198 }
1199 // Check for exceptions and return result.
1200 RETURN_IF_SCHEDULED_EXCEPTION();
1201 return result;
1202}
1203
1204
1205// Handle calls to non-function objects created through the API. This delegate
1206// function is used when the call is a normal function call.
1207BUILTIN(HandleApiCallAsFunction) {
1208 return HandleApiCallAsFunctionOrConstructor(false, args);
1209}
Steve Blocka7e24c12009-10-30 11:49:00 +00001210
1211
1212// Handle calls to non-function objects created through the API. This delegate
1213// function is used when the call is a construct call.
1214BUILTIN(HandleApiCallAsConstructor) {
1215 return HandleApiCallAsFunctionOrConstructor(true, args);
1216}
Steve Blocka7e24c12009-10-30 11:49:00 +00001217
1218
1219static void Generate_LoadIC_ArrayLength(MacroAssembler* masm) {
1220 LoadIC::GenerateArrayLength(masm);
1221}
1222
1223
1224static void Generate_LoadIC_StringLength(MacroAssembler* masm) {
1225 LoadIC::GenerateStringLength(masm);
1226}
1227
1228
1229static void Generate_LoadIC_FunctionPrototype(MacroAssembler* masm) {
1230 LoadIC::GenerateFunctionPrototype(masm);
1231}
1232
1233
1234static void Generate_LoadIC_Initialize(MacroAssembler* masm) {
1235 LoadIC::GenerateInitialize(masm);
1236}
1237
1238
1239static void Generate_LoadIC_PreMonomorphic(MacroAssembler* masm) {
1240 LoadIC::GeneratePreMonomorphic(masm);
1241}
1242
1243
1244static void Generate_LoadIC_Miss(MacroAssembler* masm) {
1245 LoadIC::GenerateMiss(masm);
1246}
1247
1248
1249static void Generate_LoadIC_Megamorphic(MacroAssembler* masm) {
1250 LoadIC::GenerateMegamorphic(masm);
1251}
1252
1253
1254static void Generate_LoadIC_Normal(MacroAssembler* masm) {
1255 LoadIC::GenerateNormal(masm);
1256}
1257
1258
1259static void Generate_KeyedLoadIC_Initialize(MacroAssembler* masm) {
1260 KeyedLoadIC::GenerateInitialize(masm);
1261}
1262
1263
1264static void Generate_KeyedLoadIC_Miss(MacroAssembler* masm) {
1265 KeyedLoadIC::GenerateMiss(masm);
1266}
1267
1268
1269static void Generate_KeyedLoadIC_Generic(MacroAssembler* masm) {
1270 KeyedLoadIC::GenerateGeneric(masm);
1271}
1272
1273
Leon Clarkee46be812010-01-19 14:06:41 +00001274static void Generate_KeyedLoadIC_String(MacroAssembler* masm) {
1275 KeyedLoadIC::GenerateString(masm);
1276}
1277
1278
Steve Block3ce2e202009-11-05 08:53:23 +00001279static void Generate_KeyedLoadIC_ExternalByteArray(MacroAssembler* masm) {
1280 KeyedLoadIC::GenerateExternalArray(masm, kExternalByteArray);
1281}
1282
1283
1284static void Generate_KeyedLoadIC_ExternalUnsignedByteArray(
1285 MacroAssembler* masm) {
1286 KeyedLoadIC::GenerateExternalArray(masm, kExternalUnsignedByteArray);
1287}
1288
1289
1290static void Generate_KeyedLoadIC_ExternalShortArray(MacroAssembler* masm) {
1291 KeyedLoadIC::GenerateExternalArray(masm, kExternalShortArray);
1292}
1293
1294
1295static void Generate_KeyedLoadIC_ExternalUnsignedShortArray(
1296 MacroAssembler* masm) {
1297 KeyedLoadIC::GenerateExternalArray(masm, kExternalUnsignedShortArray);
1298}
1299
1300
1301static void Generate_KeyedLoadIC_ExternalIntArray(MacroAssembler* masm) {
1302 KeyedLoadIC::GenerateExternalArray(masm, kExternalIntArray);
1303}
1304
1305
1306static void Generate_KeyedLoadIC_ExternalUnsignedIntArray(
1307 MacroAssembler* masm) {
1308 KeyedLoadIC::GenerateExternalArray(masm, kExternalUnsignedIntArray);
1309}
1310
1311
1312static void Generate_KeyedLoadIC_ExternalFloatArray(MacroAssembler* masm) {
1313 KeyedLoadIC::GenerateExternalArray(masm, kExternalFloatArray);
1314}
1315
1316
Steve Blocka7e24c12009-10-30 11:49:00 +00001317static void Generate_KeyedLoadIC_PreMonomorphic(MacroAssembler* masm) {
1318 KeyedLoadIC::GeneratePreMonomorphic(masm);
1319}
1320
Andrei Popescu402d9372010-02-26 13:31:12 +00001321static void Generate_KeyedLoadIC_IndexedInterceptor(MacroAssembler* masm) {
1322 KeyedLoadIC::GenerateIndexedInterceptor(masm);
1323}
1324
Steve Blocka7e24c12009-10-30 11:49:00 +00001325
1326static void Generate_StoreIC_Initialize(MacroAssembler* masm) {
1327 StoreIC::GenerateInitialize(masm);
1328}
1329
1330
1331static void Generate_StoreIC_Miss(MacroAssembler* masm) {
1332 StoreIC::GenerateMiss(masm);
1333}
1334
1335
Steve Block8defd9f2010-07-08 12:39:36 +01001336static void Generate_StoreIC_Normal(MacroAssembler* masm) {
1337 StoreIC::GenerateNormal(masm);
1338}
1339
1340
Steve Blocka7e24c12009-10-30 11:49:00 +00001341static void Generate_StoreIC_Megamorphic(MacroAssembler* masm) {
1342 StoreIC::GenerateMegamorphic(masm);
1343}
1344
1345
Steve Block6ded16b2010-05-10 14:33:55 +01001346static void Generate_StoreIC_ArrayLength(MacroAssembler* masm) {
1347 StoreIC::GenerateArrayLength(masm);
1348}
1349
1350
Ben Murdochb0fe1622011-05-05 13:52:32 +01001351static void Generate_StoreIC_GlobalProxy(MacroAssembler* masm) {
1352 StoreIC::GenerateGlobalProxy(masm);
1353}
1354
1355
Steve Blocka7e24c12009-10-30 11:49:00 +00001356static void Generate_KeyedStoreIC_Generic(MacroAssembler* masm) {
1357 KeyedStoreIC::GenerateGeneric(masm);
1358}
1359
1360
Steve Block3ce2e202009-11-05 08:53:23 +00001361static void Generate_KeyedStoreIC_ExternalByteArray(MacroAssembler* masm) {
1362 KeyedStoreIC::GenerateExternalArray(masm, kExternalByteArray);
1363}
1364
1365
1366static void Generate_KeyedStoreIC_ExternalUnsignedByteArray(
1367 MacroAssembler* masm) {
1368 KeyedStoreIC::GenerateExternalArray(masm, kExternalUnsignedByteArray);
1369}
1370
1371
1372static void Generate_KeyedStoreIC_ExternalShortArray(MacroAssembler* masm) {
1373 KeyedStoreIC::GenerateExternalArray(masm, kExternalShortArray);
1374}
1375
1376
1377static void Generate_KeyedStoreIC_ExternalUnsignedShortArray(
1378 MacroAssembler* masm) {
1379 KeyedStoreIC::GenerateExternalArray(masm, kExternalUnsignedShortArray);
1380}
1381
1382
1383static void Generate_KeyedStoreIC_ExternalIntArray(MacroAssembler* masm) {
1384 KeyedStoreIC::GenerateExternalArray(masm, kExternalIntArray);
1385}
1386
1387
1388static void Generate_KeyedStoreIC_ExternalUnsignedIntArray(
1389 MacroAssembler* masm) {
1390 KeyedStoreIC::GenerateExternalArray(masm, kExternalUnsignedIntArray);
1391}
1392
1393
1394static void Generate_KeyedStoreIC_ExternalFloatArray(MacroAssembler* masm) {
1395 KeyedStoreIC::GenerateExternalArray(masm, kExternalFloatArray);
1396}
1397
1398
Steve Blocka7e24c12009-10-30 11:49:00 +00001399static void Generate_KeyedStoreIC_Miss(MacroAssembler* masm) {
1400 KeyedStoreIC::GenerateMiss(masm);
1401}
1402
1403
1404static void Generate_KeyedStoreIC_Initialize(MacroAssembler* masm) {
1405 KeyedStoreIC::GenerateInitialize(masm);
1406}
1407
1408
1409#ifdef ENABLE_DEBUGGER_SUPPORT
1410static void Generate_LoadIC_DebugBreak(MacroAssembler* masm) {
1411 Debug::GenerateLoadICDebugBreak(masm);
1412}
1413
1414
1415static void Generate_StoreIC_DebugBreak(MacroAssembler* masm) {
1416 Debug::GenerateStoreICDebugBreak(masm);
1417}
1418
1419
1420static void Generate_KeyedLoadIC_DebugBreak(MacroAssembler* masm) {
1421 Debug::GenerateKeyedLoadICDebugBreak(masm);
1422}
1423
1424
1425static void Generate_KeyedStoreIC_DebugBreak(MacroAssembler* masm) {
1426 Debug::GenerateKeyedStoreICDebugBreak(masm);
1427}
1428
1429
1430static void Generate_ConstructCall_DebugBreak(MacroAssembler* masm) {
1431 Debug::GenerateConstructCallDebugBreak(masm);
1432}
1433
1434
1435static void Generate_Return_DebugBreak(MacroAssembler* masm) {
1436 Debug::GenerateReturnDebugBreak(masm);
1437}
1438
1439
1440static void Generate_StubNoRegisters_DebugBreak(MacroAssembler* masm) {
1441 Debug::GenerateStubNoRegistersDebugBreak(masm);
1442}
Steve Block6ded16b2010-05-10 14:33:55 +01001443
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001444
1445static void Generate_Slot_DebugBreak(MacroAssembler* masm) {
1446 Debug::GenerateSlotDebugBreak(masm);
1447}
1448
1449
Steve Block6ded16b2010-05-10 14:33:55 +01001450static void Generate_PlainReturn_LiveEdit(MacroAssembler* masm) {
1451 Debug::GeneratePlainReturnLiveEdit(masm);
1452}
1453
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001454
Steve Block6ded16b2010-05-10 14:33:55 +01001455static void Generate_FrameDropper_LiveEdit(MacroAssembler* masm) {
1456 Debug::GenerateFrameDropperLiveEdit(masm);
1457}
Steve Blocka7e24c12009-10-30 11:49:00 +00001458#endif
1459
1460Object* Builtins::builtins_[builtin_count] = { NULL, };
1461const char* Builtins::names_[builtin_count] = { NULL, };
1462
Leon Clarkee46be812010-01-19 14:06:41 +00001463#define DEF_ENUM_C(name, ignore) FUNCTION_ADDR(Builtin_##name),
Steve Blocka7e24c12009-10-30 11:49:00 +00001464 Address Builtins::c_functions_[cfunction_count] = {
1465 BUILTIN_LIST_C(DEF_ENUM_C)
1466 };
1467#undef DEF_ENUM_C
1468
1469#define DEF_JS_NAME(name, ignore) #name,
1470#define DEF_JS_ARGC(ignore, argc) argc,
1471const char* Builtins::javascript_names_[id_count] = {
1472 BUILTINS_LIST_JS(DEF_JS_NAME)
1473};
1474
1475int Builtins::javascript_argc_[id_count] = {
1476 BUILTINS_LIST_JS(DEF_JS_ARGC)
1477};
1478#undef DEF_JS_NAME
1479#undef DEF_JS_ARGC
1480
1481static bool is_initialized = false;
1482void Builtins::Setup(bool create_heap_objects) {
1483 ASSERT(!is_initialized);
1484
1485 // Create a scope for the handles in the builtins.
1486 HandleScope scope;
1487
1488 struct BuiltinDesc {
1489 byte* generator;
1490 byte* c_code;
1491 const char* s_name; // name is only used for generating log information.
1492 int name;
1493 Code::Flags flags;
Leon Clarkee46be812010-01-19 14:06:41 +00001494 BuiltinExtraArguments extra_args;
Steve Blocka7e24c12009-10-30 11:49:00 +00001495 };
1496
Leon Clarkee46be812010-01-19 14:06:41 +00001497#define DEF_FUNCTION_PTR_C(name, extra_args) \
1498 { FUNCTION_ADDR(Generate_Adaptor), \
1499 FUNCTION_ADDR(Builtin_##name), \
1500 #name, \
1501 c_##name, \
1502 Code::ComputeFlags(Code::BUILTIN), \
1503 extra_args \
Steve Blocka7e24c12009-10-30 11:49:00 +00001504 },
1505
1506#define DEF_FUNCTION_PTR_A(name, kind, state) \
1507 { FUNCTION_ADDR(Generate_##name), \
1508 NULL, \
1509 #name, \
1510 name, \
Leon Clarkee46be812010-01-19 14:06:41 +00001511 Code::ComputeFlags(Code::kind, NOT_IN_LOOP, state), \
1512 NO_EXTRA_ARGUMENTS \
Steve Blocka7e24c12009-10-30 11:49:00 +00001513 },
1514
1515 // Define array of pointers to generators and C builtin functions.
1516 static BuiltinDesc functions[] = {
1517 BUILTIN_LIST_C(DEF_FUNCTION_PTR_C)
1518 BUILTIN_LIST_A(DEF_FUNCTION_PTR_A)
1519 BUILTIN_LIST_DEBUG_A(DEF_FUNCTION_PTR_A)
1520 // Terminator:
Leon Clarkee46be812010-01-19 14:06:41 +00001521 { NULL, NULL, NULL, builtin_count, static_cast<Code::Flags>(0),
1522 NO_EXTRA_ARGUMENTS }
Steve Blocka7e24c12009-10-30 11:49:00 +00001523 };
1524
1525#undef DEF_FUNCTION_PTR_C
1526#undef DEF_FUNCTION_PTR_A
1527
1528 // For now we generate builtin adaptor code into a stack-allocated
1529 // buffer, before copying it into individual code objects.
1530 byte buffer[4*KB];
1531
1532 // Traverse the list of builtins and generate an adaptor in a
1533 // separate code object for each one.
1534 for (int i = 0; i < builtin_count; i++) {
1535 if (create_heap_objects) {
1536 MacroAssembler masm(buffer, sizeof buffer);
1537 // Generate the code/adaptor.
Leon Clarkee46be812010-01-19 14:06:41 +00001538 typedef void (*Generator)(MacroAssembler*, int, BuiltinExtraArguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00001539 Generator g = FUNCTION_CAST<Generator>(functions[i].generator);
1540 // We pass all arguments to the generator, but it may not use all of
1541 // them. This works because the first arguments are on top of the
1542 // stack.
Leon Clarkee46be812010-01-19 14:06:41 +00001543 g(&masm, functions[i].name, functions[i].extra_args);
Steve Blocka7e24c12009-10-30 11:49:00 +00001544 // Move the code into the object heap.
1545 CodeDesc desc;
1546 masm.GetCode(&desc);
1547 Code::Flags flags = functions[i].flags;
John Reck59135872010-11-02 12:39:01 -07001548 Object* code = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00001549 {
1550 // During startup it's OK to always allocate and defer GC to later.
1551 // This simplifies things because we don't need to retry.
1552 AlwaysAllocateScope __scope__;
John Reck59135872010-11-02 12:39:01 -07001553 { MaybeObject* maybe_code =
1554 Heap::CreateCode(desc, flags, masm.CodeObject());
1555 if (!maybe_code->ToObject(&code)) {
1556 v8::internal::V8::FatalProcessOutOfMemory("CreateCode");
1557 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001558 }
1559 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001560 // Log the event and add the code to the builtins array.
Steve Block6ded16b2010-05-10 14:33:55 +01001561 PROFILE(CodeCreateEvent(Logger::BUILTIN_TAG,
1562 Code::cast(code), functions[i].s_name));
Steve Blocka7e24c12009-10-30 11:49:00 +00001563 builtins_[i] = code;
1564#ifdef ENABLE_DISASSEMBLER
1565 if (FLAG_print_builtin_code) {
1566 PrintF("Builtin: %s\n", functions[i].s_name);
1567 Code::cast(code)->Disassemble(functions[i].s_name);
1568 PrintF("\n");
1569 }
1570#endif
1571 } else {
1572 // Deserializing. The values will be filled in during IterateBuiltins.
1573 builtins_[i] = NULL;
1574 }
1575 names_[i] = functions[i].s_name;
1576 }
1577
1578 // Mark as initialized.
1579 is_initialized = true;
1580}
1581
1582
1583void Builtins::TearDown() {
1584 is_initialized = false;
1585}
1586
1587
1588void Builtins::IterateBuiltins(ObjectVisitor* v) {
1589 v->VisitPointers(&builtins_[0], &builtins_[0] + builtin_count);
1590}
1591
1592
1593const char* Builtins::Lookup(byte* pc) {
1594 if (is_initialized) { // may be called during initialization (disassembler!)
1595 for (int i = 0; i < builtin_count; i++) {
1596 Code* entry = Code::cast(builtins_[i]);
1597 if (entry->contains(pc)) {
1598 return names_[i];
1599 }
1600 }
1601 }
1602 return NULL;
1603}
1604
Ben Murdochb0fe1622011-05-05 13:52:32 +01001605
Steve Blocka7e24c12009-10-30 11:49:00 +00001606} } // namespace v8::internal