blob: 52d5530cec658866f6e66051c2ea8ce972fdac3b [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"
35
36namespace v8 {
37namespace internal {
38
Leon Clarkee46be812010-01-19 14:06:41 +000039namespace {
40
41// Arguments object passed to C++ builtins.
42template <BuiltinExtraArguments extra_args>
43class BuiltinArguments : public Arguments {
44 public:
45 BuiltinArguments(int length, Object** arguments)
46 : Arguments(length, arguments) { }
47
48 Object*& operator[] (int index) {
49 ASSERT(index < length());
50 return Arguments::operator[](index);
51 }
52
53 template <class S> Handle<S> at(int index) {
54 ASSERT(index < length());
55 return Arguments::at<S>(index);
56 }
57
58 Handle<Object> receiver() {
59 return Arguments::at<Object>(0);
60 }
61
62 Handle<JSFunction> called_function() {
63 STATIC_ASSERT(extra_args == NEEDS_CALLED_FUNCTION);
64 return Arguments::at<JSFunction>(Arguments::length() - 1);
65 }
66
67 // Gets the total number of arguments including the receiver (but
68 // excluding extra arguments).
69 int length() const {
70 STATIC_ASSERT(extra_args == NO_EXTRA_ARGUMENTS);
71 return Arguments::length();
72 }
73
74#ifdef DEBUG
75 void Verify() {
76 // Check we have at least the receiver.
77 ASSERT(Arguments::length() >= 1);
78 }
79#endif
80};
81
82
83// Specialize BuiltinArguments for the called function extra argument.
84
85template <>
86int BuiltinArguments<NEEDS_CALLED_FUNCTION>::length() const {
87 return Arguments::length() - 1;
88}
89
90#ifdef DEBUG
91template <>
92void BuiltinArguments<NEEDS_CALLED_FUNCTION>::Verify() {
93 // Check we have at least the receiver and the called function.
94 ASSERT(Arguments::length() >= 2);
95 // Make sure cast to JSFunction succeeds.
96 called_function();
97}
98#endif
99
100
101#define DEF_ARG_TYPE(name, spec) \
102 typedef BuiltinArguments<spec> name##ArgumentsType;
103BUILTIN_LIST_C(DEF_ARG_TYPE)
104#undef DEF_ARG_TYPE
105
106} // namespace
107
108
Steve Blocka7e24c12009-10-30 11:49:00 +0000109// ----------------------------------------------------------------------------
Leon Clarkee46be812010-01-19 14:06:41 +0000110// Support macro for defining builtins in C++.
Steve Blocka7e24c12009-10-30 11:49:00 +0000111// ----------------------------------------------------------------------------
112//
113// A builtin function is defined by writing:
114//
115// BUILTIN(name) {
116// ...
117// }
Steve Blocka7e24c12009-10-30 11:49:00 +0000118//
Leon Clarkee46be812010-01-19 14:06:41 +0000119// In the body of the builtin function the arguments can be accessed
120// through the BuiltinArguments object args.
Steve Blocka7e24c12009-10-30 11:49:00 +0000121
Leon Clarkee46be812010-01-19 14:06:41 +0000122#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000123
John Reck59135872010-11-02 12:39:01 -0700124#define BUILTIN(name) \
125 MUST_USE_RESULT static MaybeObject* Builtin_Impl_##name( \
126 name##ArgumentsType args); \
127 MUST_USE_RESULT static MaybeObject* Builtin_##name( \
128 name##ArgumentsType args) { \
129 args.Verify(); \
130 return Builtin_Impl_##name(args); \
131 } \
132 MUST_USE_RESULT static MaybeObject* Builtin_Impl_##name( \
133 name##ArgumentsType args)
Steve Blocka7e24c12009-10-30 11:49:00 +0000134
Leon Clarkee46be812010-01-19 14:06:41 +0000135#else // For release mode.
Steve Blocka7e24c12009-10-30 11:49:00 +0000136
John Reck59135872010-11-02 12:39:01 -0700137#define BUILTIN(name) \
138 static MaybeObject* Builtin_##name(name##ArgumentsType args)
Leon Clarkee46be812010-01-19 14:06:41 +0000139
140#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000141
142
143static inline bool CalledAsConstructor() {
144#ifdef DEBUG
145 // Calculate the result using a full stack frame iterator and check
146 // that the state of the stack is as we assume it to be in the
147 // code below.
148 StackFrameIterator it;
149 ASSERT(it.frame()->is_exit());
150 it.Advance();
151 StackFrame* frame = it.frame();
152 bool reference_result = frame->is_construct();
153#endif
154 Address fp = Top::c_entry_fp(Top::GetCurrentThread());
155 // Because we know fp points to an exit frame we can use the relevant
156 // part of ExitFrame::ComputeCallerState directly.
157 const int kCallerOffset = ExitFrameConstants::kCallerFPOffset;
158 Address caller_fp = Memory::Address_at(fp + kCallerOffset);
159 // This inlines the part of StackFrame::ComputeType that grabs the
160 // type of the current frame. Note that StackFrame::ComputeType
161 // has been specialized for each architecture so if any one of them
162 // changes this code has to be changed as well.
163 const int kMarkerOffset = StandardFrameConstants::kMarkerOffset;
164 const Smi* kConstructMarker = Smi::FromInt(StackFrame::CONSTRUCT);
165 Object* marker = Memory::Object_at(caller_fp + kMarkerOffset);
166 bool result = (marker == kConstructMarker);
167 ASSERT_EQ(result, reference_result);
168 return result;
169}
170
171// ----------------------------------------------------------------------------
172
173
Steve Blocka7e24c12009-10-30 11:49:00 +0000174BUILTIN(Illegal) {
175 UNREACHABLE();
Leon Clarkee46be812010-01-19 14:06:41 +0000176 return Heap::undefined_value(); // Make compiler happy.
Steve Blocka7e24c12009-10-30 11:49:00 +0000177}
Steve Blocka7e24c12009-10-30 11:49:00 +0000178
179
180BUILTIN(EmptyFunction) {
Leon Clarkee46be812010-01-19 14:06:41 +0000181 return Heap::undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000182}
Steve Blocka7e24c12009-10-30 11:49:00 +0000183
184
185BUILTIN(ArrayCodeGeneric) {
186 Counters::array_function_runtime.Increment();
187
188 JSArray* array;
189 if (CalledAsConstructor()) {
Leon Clarkee46be812010-01-19 14:06:41 +0000190 array = JSArray::cast(*args.receiver());
Steve Blocka7e24c12009-10-30 11:49:00 +0000191 } else {
192 // Allocate the JS Array
193 JSFunction* constructor =
194 Top::context()->global_context()->array_function();
John Reck59135872010-11-02 12:39:01 -0700195 Object* obj;
196 { MaybeObject* maybe_obj = Heap::AllocateJSObject(constructor);
197 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
198 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000199 array = JSArray::cast(obj);
200 }
201
202 // 'array' now contains the JSArray we should initialize.
Steve Block8defd9f2010-07-08 12:39:36 +0100203 ASSERT(array->HasFastElements());
Steve Blocka7e24c12009-10-30 11:49:00 +0000204
205 // Optimize the case where there is one argument and the argument is a
206 // small smi.
207 if (args.length() == 2) {
208 Object* obj = args[1];
209 if (obj->IsSmi()) {
210 int len = Smi::cast(obj)->value();
211 if (len >= 0 && len < JSObject::kInitialMaxFastElementArray) {
John Reck59135872010-11-02 12:39:01 -0700212 Object* obj;
213 { MaybeObject* maybe_obj = Heap::AllocateFixedArrayWithHoles(len);
214 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
215 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000216 array->SetContent(FixedArray::cast(obj));
217 return array;
218 }
219 }
220 // Take the argument as the length.
John Reck59135872010-11-02 12:39:01 -0700221 { MaybeObject* maybe_obj = array->Initialize(0);
222 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
223 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000224 return array->SetElementsLength(args[1]);
225 }
226
227 // Optimize the case where there are no parameters passed.
228 if (args.length() == 1) {
229 return array->Initialize(JSArray::kPreallocatedArrayElements);
230 }
231
232 // Take the arguments as elements.
233 int number_of_elements = args.length() - 1;
234 Smi* len = Smi::FromInt(number_of_elements);
John Reck59135872010-11-02 12:39:01 -0700235 Object* obj;
236 { MaybeObject* maybe_obj = Heap::AllocateFixedArrayWithHoles(len->value());
237 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
238 }
Leon Clarke4515c472010-02-03 11:58:03 +0000239
240 AssertNoAllocation no_gc;
Steve Blocka7e24c12009-10-30 11:49:00 +0000241 FixedArray* elms = FixedArray::cast(obj);
Leon Clarke4515c472010-02-03 11:58:03 +0000242 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000243 // Fill in the content
244 for (int index = 0; index < number_of_elements; index++) {
245 elms->set(index, args[index+1], mode);
246 }
247
248 // Set length and elements on the array.
249 array->set_elements(FixedArray::cast(obj));
Leon Clarke4515c472010-02-03 11:58:03 +0000250 array->set_length(len);
Steve Blocka7e24c12009-10-30 11:49:00 +0000251
252 return array;
253}
Steve Blocka7e24c12009-10-30 11:49:00 +0000254
255
John Reck59135872010-11-02 12:39:01 -0700256MUST_USE_RESULT static MaybeObject* AllocateJSArray() {
Steve Block6ded16b2010-05-10 14:33:55 +0100257 JSFunction* array_function =
258 Top::context()->global_context()->array_function();
John Reck59135872010-11-02 12:39:01 -0700259 Object* result;
260 { MaybeObject* maybe_result = Heap::AllocateJSObject(array_function);
261 if (!maybe_result->ToObject(&result)) return maybe_result;
262 }
Steve Block6ded16b2010-05-10 14:33:55 +0100263 return result;
264}
265
266
John Reck59135872010-11-02 12:39:01 -0700267MUST_USE_RESULT static MaybeObject* AllocateEmptyJSArray() {
268 Object* result;
269 { MaybeObject* maybe_result = AllocateJSArray();
270 if (!maybe_result->ToObject(&result)) return maybe_result;
271 }
Steve Block6ded16b2010-05-10 14:33:55 +0100272 JSArray* result_array = JSArray::cast(result);
273 result_array->set_length(Smi::FromInt(0));
274 result_array->set_elements(Heap::empty_fixed_array());
275 return result_array;
276}
277
278
279static void CopyElements(AssertNoAllocation* no_gc,
280 FixedArray* dst,
281 int dst_index,
282 FixedArray* src,
283 int src_index,
284 int len) {
285 ASSERT(dst != src); // Use MoveElements instead.
Iain Merrick75681382010-08-19 15:07:18 +0100286 ASSERT(dst->map() != Heap::fixed_cow_array_map());
Steve Block6ded16b2010-05-10 14:33:55 +0100287 ASSERT(len > 0);
288 CopyWords(dst->data_start() + dst_index,
289 src->data_start() + src_index,
290 len);
291 WriteBarrierMode mode = dst->GetWriteBarrierMode(*no_gc);
292 if (mode == UPDATE_WRITE_BARRIER) {
293 Heap::RecordWrites(dst->address(), dst->OffsetOfElementAt(dst_index), len);
294 }
295}
296
297
298static void MoveElements(AssertNoAllocation* no_gc,
299 FixedArray* dst,
300 int dst_index,
301 FixedArray* src,
302 int src_index,
303 int len) {
Iain Merrick75681382010-08-19 15:07:18 +0100304 ASSERT(dst->map() != Heap::fixed_cow_array_map());
Steve Block6ded16b2010-05-10 14:33:55 +0100305 memmove(dst->data_start() + dst_index,
306 src->data_start() + src_index,
307 len * kPointerSize);
308 WriteBarrierMode mode = dst->GetWriteBarrierMode(*no_gc);
309 if (mode == UPDATE_WRITE_BARRIER) {
310 Heap::RecordWrites(dst->address(), dst->OffsetOfElementAt(dst_index), len);
311 }
312}
313
314
315static void FillWithHoles(FixedArray* dst, int from, int to) {
Iain Merrick75681382010-08-19 15:07:18 +0100316 ASSERT(dst->map() != Heap::fixed_cow_array_map());
Steve Block6ded16b2010-05-10 14:33:55 +0100317 MemsetPointer(dst->data_start() + from, Heap::the_hole_value(), to - from);
318}
319
320
321static FixedArray* LeftTrimFixedArray(FixedArray* elms, int to_trim) {
Iain Merrick75681382010-08-19 15:07:18 +0100322 ASSERT(elms->map() != Heap::fixed_cow_array_map());
Steve Block791712a2010-08-27 10:21:07 +0100323 // For now this trick is only applied to fixed arrays in new and paged space.
Steve Block6ded16b2010-05-10 14:33:55 +0100324 // In large object space the object's start must coincide with chunk
325 // and thus the trick is just not applicable.
Steve Block791712a2010-08-27 10:21:07 +0100326 ASSERT(!Heap::lo_space()->Contains(elms));
Steve Block6ded16b2010-05-10 14:33:55 +0100327
328 STATIC_ASSERT(FixedArray::kMapOffset == 0);
329 STATIC_ASSERT(FixedArray::kLengthOffset == kPointerSize);
330 STATIC_ASSERT(FixedArray::kHeaderSize == 2 * kPointerSize);
331
332 Object** former_start = HeapObject::RawField(elms, 0);
333
334 const int len = elms->length();
335
Steve Block791712a2010-08-27 10:21:07 +0100336 if (to_trim > FixedArray::kHeaderSize / kPointerSize &&
337 !Heap::new_space()->Contains(elms)) {
338 // If we are doing a big trim in old space then we zap the space that was
339 // formerly part of the array so that the GC (aided by the card-based
340 // remembered set) won't find pointers to new-space there.
341 Object** zap = reinterpret_cast<Object**>(elms->address());
342 zap++; // Header of filler must be at least one word so skip that.
343 for (int i = 1; i < to_trim; i++) {
344 *zap++ = Smi::FromInt(0);
345 }
346 }
Steve Block6ded16b2010-05-10 14:33:55 +0100347 // Technically in new space this write might be omitted (except for
348 // debug mode which iterates through the heap), but to play safer
349 // we still do it.
350 Heap::CreateFillerObjectAt(elms->address(), to_trim * kPointerSize);
351
352 former_start[to_trim] = Heap::fixed_array_map();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100353 former_start[to_trim + 1] = Smi::FromInt(len - to_trim);
Steve Block6ded16b2010-05-10 14:33:55 +0100354
Steve Block791712a2010-08-27 10:21:07 +0100355 return FixedArray::cast(HeapObject::FromAddress(
356 elms->address() + to_trim * kPointerSize));
Steve Block6ded16b2010-05-10 14:33:55 +0100357}
358
359
Kristian Monsen25f61362010-05-21 11:50:48 +0100360static bool ArrayPrototypeHasNoElements(Context* global_context,
361 JSObject* array_proto) {
Steve Block6ded16b2010-05-10 14:33:55 +0100362 // This method depends on non writability of Object and Array prototype
363 // fields.
Kristian Monsen25f61362010-05-21 11:50:48 +0100364 if (array_proto->elements() != Heap::empty_fixed_array()) return false;
Steve Block6ded16b2010-05-10 14:33:55 +0100365 // Hidden prototype
Kristian Monsen25f61362010-05-21 11:50:48 +0100366 array_proto = JSObject::cast(array_proto->GetPrototype());
367 ASSERT(array_proto->elements() == Heap::empty_fixed_array());
Steve Block6ded16b2010-05-10 14:33:55 +0100368 // Object.prototype
Kristian Monsen25f61362010-05-21 11:50:48 +0100369 array_proto = JSObject::cast(array_proto->GetPrototype());
370 if (array_proto != global_context->initial_object_prototype()) return false;
371 if (array_proto->elements() != Heap::empty_fixed_array()) return false;
372 ASSERT(array_proto->GetPrototype()->IsNull());
Steve Block6ded16b2010-05-10 14:33:55 +0100373 return true;
374}
375
376
John Reck59135872010-11-02 12:39:01 -0700377MUST_USE_RESULT
378static inline MaybeObject* EnsureJSArrayWithWritableFastElements(
379 Object* receiver) {
Iain Merrick75681382010-08-19 15:07:18 +0100380 if (!receiver->IsJSArray()) return NULL;
Steve Block6ded16b2010-05-10 14:33:55 +0100381 JSArray* array = JSArray::cast(receiver);
Steve Block6ded16b2010-05-10 14:33:55 +0100382 HeapObject* elms = HeapObject::cast(array->elements());
Iain Merrick75681382010-08-19 15:07:18 +0100383 if (elms->map() == Heap::fixed_array_map()) return elms;
384 if (elms->map() == Heap::fixed_cow_array_map()) {
385 return array->EnsureWritableFastElements();
Steve Block6ded16b2010-05-10 14:33:55 +0100386 }
Iain Merrick75681382010-08-19 15:07:18 +0100387 return NULL;
Steve Block6ded16b2010-05-10 14:33:55 +0100388}
389
390
Iain Merrick75681382010-08-19 15:07:18 +0100391static inline bool IsJSArrayFastElementMovingAllowed(JSArray* receiver) {
Kristian Monsen25f61362010-05-21 11:50:48 +0100392 Context* global_context = Top::context()->global_context();
393 JSObject* array_proto =
394 JSObject::cast(global_context->array_function()->prototype());
Iain Merrick75681382010-08-19 15:07:18 +0100395 return receiver->GetPrototype() == array_proto &&
396 ArrayPrototypeHasNoElements(global_context, array_proto);
Kristian Monsen25f61362010-05-21 11:50:48 +0100397}
398
399
John Reck59135872010-11-02 12:39:01 -0700400MUST_USE_RESULT static MaybeObject* CallJsBuiltin(
401 const char* name,
402 BuiltinArguments<NO_EXTRA_ARGUMENTS> args) {
Steve Block6ded16b2010-05-10 14:33:55 +0100403 HandleScope handleScope;
404
405 Handle<Object> js_builtin =
406 GetProperty(Handle<JSObject>(Top::global_context()->builtins()),
407 name);
408 ASSERT(js_builtin->IsJSFunction());
409 Handle<JSFunction> function(Handle<JSFunction>::cast(js_builtin));
Kristian Monsen25f61362010-05-21 11:50:48 +0100410 ScopedVector<Object**> argv(args.length() - 1);
Steve Block6ded16b2010-05-10 14:33:55 +0100411 int n_args = args.length() - 1;
412 for (int i = 0; i < n_args; i++) {
413 argv[i] = args.at<Object>(i + 1).location();
414 }
415 bool pending_exception = false;
416 Handle<Object> result = Execution::Call(function,
417 args.receiver(),
418 n_args,
419 argv.start(),
420 &pending_exception);
Steve Block6ded16b2010-05-10 14:33:55 +0100421 if (pending_exception) return Failure::Exception();
422 return *result;
423}
424
425
Steve Blocka7e24c12009-10-30 11:49:00 +0000426BUILTIN(ArrayPush) {
Steve Block6ded16b2010-05-10 14:33:55 +0100427 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700428 Object* elms_obj;
429 { MaybeObject* maybe_elms_obj =
430 EnsureJSArrayWithWritableFastElements(receiver);
431 if (maybe_elms_obj == NULL) return CallJsBuiltin("ArrayPush", args);
432 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
433 }
Iain Merrick75681382010-08-19 15:07:18 +0100434 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100435 JSArray* array = JSArray::cast(receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +0000436
Steve Blocka7e24c12009-10-30 11:49:00 +0000437 int len = Smi::cast(array->length())->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000438 int to_add = args.length() - 1;
439 if (to_add == 0) {
440 return Smi::FromInt(len);
441 }
442 // Currently fixed arrays cannot grow too big, so
443 // we should never hit this case.
444 ASSERT(to_add <= (Smi::kMaxValue - len));
Steve Blocka7e24c12009-10-30 11:49:00 +0000445
Andrei Popescu402d9372010-02-26 13:31:12 +0000446 int new_length = len + to_add;
Steve Blocka7e24c12009-10-30 11:49:00 +0000447
Andrei Popescu402d9372010-02-26 13:31:12 +0000448 if (new_length > elms->length()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000449 // New backing storage is needed.
450 int capacity = new_length + (new_length >> 1) + 16;
John Reck59135872010-11-02 12:39:01 -0700451 Object* obj;
452 { MaybeObject* maybe_obj = Heap::AllocateUninitializedFixedArray(capacity);
453 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
454 }
Steve Block6ded16b2010-05-10 14:33:55 +0100455 FixedArray* new_elms = FixedArray::cast(obj);
Leon Clarke4515c472010-02-03 11:58:03 +0000456
457 AssertNoAllocation no_gc;
Steve Block6ded16b2010-05-10 14:33:55 +0100458 if (len > 0) {
459 CopyElements(&no_gc, new_elms, 0, elms, 0, len);
460 }
461 FillWithHoles(new_elms, new_length, capacity);
462
Andrei Popescu402d9372010-02-26 13:31:12 +0000463 elms = new_elms;
464 array->set_elements(elms);
Steve Blocka7e24c12009-10-30 11:49:00 +0000465 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000466
Steve Block6ded16b2010-05-10 14:33:55 +0100467 // Add the provided values.
Andrei Popescu402d9372010-02-26 13:31:12 +0000468 AssertNoAllocation no_gc;
469 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
Andrei Popescu402d9372010-02-26 13:31:12 +0000470 for (int index = 0; index < to_add; index++) {
471 elms->set(index + len, args[index + 1], mode);
472 }
473
Steve Blocka7e24c12009-10-30 11:49:00 +0000474 // Set the length.
Leon Clarke4515c472010-02-03 11:58:03 +0000475 array->set_length(Smi::FromInt(new_length));
Andrei Popescu402d9372010-02-26 13:31:12 +0000476 return Smi::FromInt(new_length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000477}
Steve Blocka7e24c12009-10-30 11:49:00 +0000478
479
480BUILTIN(ArrayPop) {
Steve Block6ded16b2010-05-10 14:33:55 +0100481 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700482 Object* elms_obj;
483 { MaybeObject* maybe_elms_obj =
484 EnsureJSArrayWithWritableFastElements(receiver);
485 if (maybe_elms_obj == NULL) return CallJsBuiltin("ArrayPop", args);
486 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
487 }
Iain Merrick75681382010-08-19 15:07:18 +0100488 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100489 JSArray* array = JSArray::cast(receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +0000490
491 int len = Smi::cast(array->length())->value();
Steve Block6ded16b2010-05-10 14:33:55 +0100492 if (len == 0) return Heap::undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000493
494 // Get top element
John Reck59135872010-11-02 12:39:01 -0700495 MaybeObject* top = elms->get(len - 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000496
497 // Set the length.
Leon Clarke4515c472010-02-03 11:58:03 +0000498 array->set_length(Smi::FromInt(len - 1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000499
500 if (!top->IsTheHole()) {
501 // Delete the top element.
502 elms->set_the_hole(len - 1);
503 return top;
504 }
505
Kristian Monsen25f61362010-05-21 11:50:48 +0100506 top = array->GetPrototype()->GetElement(len - 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000507
508 return top;
509}
Steve Blocka7e24c12009-10-30 11:49:00 +0000510
511
Andrei Popescu402d9372010-02-26 13:31:12 +0000512BUILTIN(ArrayShift) {
Steve Block6ded16b2010-05-10 14:33:55 +0100513 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700514 Object* elms_obj;
515 { MaybeObject* maybe_elms_obj =
516 EnsureJSArrayWithWritableFastElements(receiver);
517 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
518 }
Iain Merrick75681382010-08-19 15:07:18 +0100519 if (elms_obj == NULL ||
520 !IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) {
Steve Block6ded16b2010-05-10 14:33:55 +0100521 return CallJsBuiltin("ArrayShift", args);
522 }
Iain Merrick75681382010-08-19 15:07:18 +0100523 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100524 JSArray* array = JSArray::cast(receiver);
Andrei Popescu402d9372010-02-26 13:31:12 +0000525 ASSERT(array->HasFastElements());
526
527 int len = Smi::cast(array->length())->value();
528 if (len == 0) return Heap::undefined_value();
529
Andrei Popescu402d9372010-02-26 13:31:12 +0000530 // Get first element
531 Object* first = elms->get(0);
532 if (first->IsTheHole()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100533 first = Heap::undefined_value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000534 }
535
Steve Block791712a2010-08-27 10:21:07 +0100536 if (!Heap::lo_space()->Contains(elms)) {
537 // As elms still in the same space they used to be,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100538 // there is no need to update region dirty mark.
Steve Block6ded16b2010-05-10 14:33:55 +0100539 array->set_elements(LeftTrimFixedArray(elms, 1), SKIP_WRITE_BARRIER);
540 } else {
541 // Shift the elements.
542 AssertNoAllocation no_gc;
543 MoveElements(&no_gc, elms, 0, elms, 1, len - 1);
544 elms->set(len - 1, Heap::the_hole_value());
Andrei Popescu402d9372010-02-26 13:31:12 +0000545 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000546
547 // Set the length.
548 array->set_length(Smi::FromInt(len - 1));
549
550 return first;
551}
552
553
554BUILTIN(ArrayUnshift) {
Steve Block6ded16b2010-05-10 14:33:55 +0100555 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700556 Object* elms_obj;
557 { MaybeObject* maybe_elms_obj =
558 EnsureJSArrayWithWritableFastElements(receiver);
559 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
560 }
Iain Merrick75681382010-08-19 15:07:18 +0100561 if (elms_obj == NULL ||
562 !IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) {
Steve Block6ded16b2010-05-10 14:33:55 +0100563 return CallJsBuiltin("ArrayUnshift", args);
564 }
Iain Merrick75681382010-08-19 15:07:18 +0100565 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100566 JSArray* array = JSArray::cast(receiver);
Andrei Popescu402d9372010-02-26 13:31:12 +0000567 ASSERT(array->HasFastElements());
568
569 int len = Smi::cast(array->length())->value();
570 int to_add = args.length() - 1;
Andrei Popescu402d9372010-02-26 13:31:12 +0000571 int new_length = len + to_add;
572 // Currently fixed arrays cannot grow too big, so
573 // we should never hit this case.
574 ASSERT(to_add <= (Smi::kMaxValue - len));
575
Andrei Popescu402d9372010-02-26 13:31:12 +0000576 if (new_length > elms->length()) {
577 // New backing storage is needed.
578 int capacity = new_length + (new_length >> 1) + 16;
John Reck59135872010-11-02 12:39:01 -0700579 Object* obj;
580 { MaybeObject* maybe_obj = Heap::AllocateUninitializedFixedArray(capacity);
581 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
582 }
Steve Block6ded16b2010-05-10 14:33:55 +0100583 FixedArray* new_elms = FixedArray::cast(obj);
Andrei Popescu402d9372010-02-26 13:31:12 +0000584
585 AssertNoAllocation no_gc;
Steve Block6ded16b2010-05-10 14:33:55 +0100586 if (len > 0) {
587 CopyElements(&no_gc, new_elms, to_add, elms, 0, len);
588 }
589 FillWithHoles(new_elms, new_length, capacity);
Andrei Popescu402d9372010-02-26 13:31:12 +0000590
591 elms = new_elms;
592 array->set_elements(elms);
593 } else {
594 AssertNoAllocation no_gc;
Steve Block6ded16b2010-05-10 14:33:55 +0100595 MoveElements(&no_gc, elms, to_add, elms, 0, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000596 }
597
598 // Add the provided values.
599 AssertNoAllocation no_gc;
600 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
601 for (int i = 0; i < to_add; i++) {
602 elms->set(i, args[i + 1], mode);
603 }
604
605 // Set the length.
606 array->set_length(Smi::FromInt(new_length));
607 return Smi::FromInt(new_length);
608}
609
610
Andrei Popescu402d9372010-02-26 13:31:12 +0000611BUILTIN(ArraySlice) {
Steve Block6ded16b2010-05-10 14:33:55 +0100612 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700613 Object* elms_obj;
614 { MaybeObject* maybe_elms_obj =
615 EnsureJSArrayWithWritableFastElements(receiver);
616 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
617 }
Iain Merrick75681382010-08-19 15:07:18 +0100618 if (elms_obj == NULL ||
619 !IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) {
Steve Block6ded16b2010-05-10 14:33:55 +0100620 return CallJsBuiltin("ArraySlice", args);
621 }
Iain Merrick75681382010-08-19 15:07:18 +0100622 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100623 JSArray* array = JSArray::cast(receiver);
Andrei Popescu402d9372010-02-26 13:31:12 +0000624 ASSERT(array->HasFastElements());
625
626 int len = Smi::cast(array->length())->value();
627
628 int n_arguments = args.length() - 1;
629
630 // Note carefully choosen defaults---if argument is missing,
Steve Block6ded16b2010-05-10 14:33:55 +0100631 // it's undefined which gets converted to 0 for relative_start
632 // and to len for relative_end.
633 int relative_start = 0;
634 int relative_end = len;
Andrei Popescu402d9372010-02-26 13:31:12 +0000635 if (n_arguments > 0) {
636 Object* arg1 = args[1];
637 if (arg1->IsSmi()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100638 relative_start = Smi::cast(arg1)->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000639 } else if (!arg1->IsUndefined()) {
640 return CallJsBuiltin("ArraySlice", args);
641 }
642 if (n_arguments > 1) {
643 Object* arg2 = args[2];
644 if (arg2->IsSmi()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100645 relative_end = Smi::cast(arg2)->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000646 } else if (!arg2->IsUndefined()) {
647 return CallJsBuiltin("ArraySlice", args);
648 }
649 }
650 }
651
652 // ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 6.
Steve Block6ded16b2010-05-10 14:33:55 +0100653 int k = (relative_start < 0) ? Max(len + relative_start, 0)
654 : Min(relative_start, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000655
656 // ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 8.
Steve Block6ded16b2010-05-10 14:33:55 +0100657 int final = (relative_end < 0) ? Max(len + relative_end, 0)
658 : Min(relative_end, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000659
660 // Calculate the length of result array.
661 int result_len = final - k;
Steve Block6ded16b2010-05-10 14:33:55 +0100662 if (result_len <= 0) {
663 return AllocateEmptyJSArray();
Andrei Popescu402d9372010-02-26 13:31:12 +0000664 }
665
John Reck59135872010-11-02 12:39:01 -0700666 Object* result;
667 { MaybeObject* maybe_result = AllocateJSArray();
668 if (!maybe_result->ToObject(&result)) return maybe_result;
669 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000670 JSArray* result_array = JSArray::cast(result);
671
John Reck59135872010-11-02 12:39:01 -0700672 { MaybeObject* maybe_result =
673 Heap::AllocateUninitializedFixedArray(result_len);
674 if (!maybe_result->ToObject(&result)) return maybe_result;
675 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000676 FixedArray* result_elms = FixedArray::cast(result);
677
Andrei Popescu402d9372010-02-26 13:31:12 +0000678 AssertNoAllocation no_gc;
Steve Block6ded16b2010-05-10 14:33:55 +0100679 CopyElements(&no_gc, result_elms, 0, elms, k, result_len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000680
681 // Set elements.
682 result_array->set_elements(result_elms);
683
684 // Set the length.
685 result_array->set_length(Smi::FromInt(result_len));
686 return result_array;
687}
688
689
690BUILTIN(ArraySplice) {
Steve Block6ded16b2010-05-10 14:33:55 +0100691 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700692 Object* elms_obj;
693 { MaybeObject* maybe_elms_obj =
694 EnsureJSArrayWithWritableFastElements(receiver);
695 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
696 }
Iain Merrick75681382010-08-19 15:07:18 +0100697 if (elms_obj == NULL ||
698 !IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) {
Steve Block6ded16b2010-05-10 14:33:55 +0100699 return CallJsBuiltin("ArraySplice", args);
700 }
Iain Merrick75681382010-08-19 15:07:18 +0100701 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100702 JSArray* array = JSArray::cast(receiver);
Andrei Popescu402d9372010-02-26 13:31:12 +0000703 ASSERT(array->HasFastElements());
704
705 int len = Smi::cast(array->length())->value();
706
707 int n_arguments = args.length() - 1;
708
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100709 // Return empty array when no arguments are supplied.
Andrei Popescu402d9372010-02-26 13:31:12 +0000710 if (n_arguments == 0) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100711 return AllocateEmptyJSArray();
Andrei Popescu402d9372010-02-26 13:31:12 +0000712 }
713
Steve Block6ded16b2010-05-10 14:33:55 +0100714 int relative_start = 0;
Andrei Popescu402d9372010-02-26 13:31:12 +0000715 Object* arg1 = args[1];
716 if (arg1->IsSmi()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100717 relative_start = Smi::cast(arg1)->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000718 } else if (!arg1->IsUndefined()) {
719 return CallJsBuiltin("ArraySplice", args);
720 }
Steve Block6ded16b2010-05-10 14:33:55 +0100721 int actual_start = (relative_start < 0) ? Max(len + relative_start, 0)
722 : Min(relative_start, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000723
724 // SpiderMonkey, TraceMonkey and JSC treat the case where no delete count is
725 // given differently from when an undefined delete count is given.
726 // This does not follow ECMA-262, but we do the same for
727 // compatibility.
Steve Block6ded16b2010-05-10 14:33:55 +0100728 int delete_count = len;
Andrei Popescu402d9372010-02-26 13:31:12 +0000729 if (n_arguments > 1) {
730 Object* arg2 = args[2];
731 if (arg2->IsSmi()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100732 delete_count = Smi::cast(arg2)->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000733 } else {
734 return CallJsBuiltin("ArraySplice", args);
735 }
736 }
Steve Block6ded16b2010-05-10 14:33:55 +0100737 int actual_delete_count = Min(Max(delete_count, 0), len - actual_start);
Andrei Popescu402d9372010-02-26 13:31:12 +0000738
Steve Block6ded16b2010-05-10 14:33:55 +0100739 JSArray* result_array = NULL;
740 if (actual_delete_count == 0) {
John Reck59135872010-11-02 12:39:01 -0700741 Object* result;
742 { MaybeObject* maybe_result = AllocateEmptyJSArray();
743 if (!maybe_result->ToObject(&result)) return maybe_result;
744 }
Steve Block6ded16b2010-05-10 14:33:55 +0100745 result_array = JSArray::cast(result);
746 } else {
747 // Allocate result array.
John Reck59135872010-11-02 12:39:01 -0700748 Object* result;
749 { MaybeObject* maybe_result = AllocateJSArray();
750 if (!maybe_result->ToObject(&result)) return maybe_result;
751 }
Steve Block6ded16b2010-05-10 14:33:55 +0100752 result_array = JSArray::cast(result);
Andrei Popescu402d9372010-02-26 13:31:12 +0000753
John Reck59135872010-11-02 12:39:01 -0700754 { MaybeObject* maybe_result =
755 Heap::AllocateUninitializedFixedArray(actual_delete_count);
756 if (!maybe_result->ToObject(&result)) return maybe_result;
757 }
Steve Block6ded16b2010-05-10 14:33:55 +0100758 FixedArray* result_elms = FixedArray::cast(result);
Andrei Popescu402d9372010-02-26 13:31:12 +0000759
Steve Block6ded16b2010-05-10 14:33:55 +0100760 AssertNoAllocation no_gc;
761 // Fill newly created array.
762 CopyElements(&no_gc,
763 result_elms, 0,
764 elms, actual_start,
765 actual_delete_count);
Andrei Popescu402d9372010-02-26 13:31:12 +0000766
Steve Block6ded16b2010-05-10 14:33:55 +0100767 // Set elements.
768 result_array->set_elements(result_elms);
Andrei Popescu402d9372010-02-26 13:31:12 +0000769
Steve Block6ded16b2010-05-10 14:33:55 +0100770 // Set the length.
771 result_array->set_length(Smi::FromInt(actual_delete_count));
Andrei Popescu402d9372010-02-26 13:31:12 +0000772 }
773
Steve Block6ded16b2010-05-10 14:33:55 +0100774 int item_count = (n_arguments > 1) ? (n_arguments - 2) : 0;
Andrei Popescu402d9372010-02-26 13:31:12 +0000775
Steve Block6ded16b2010-05-10 14:33:55 +0100776 int new_length = len - actual_delete_count + item_count;
Andrei Popescu402d9372010-02-26 13:31:12 +0000777
Steve Block6ded16b2010-05-10 14:33:55 +0100778 if (item_count < actual_delete_count) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000779 // Shrink the array.
Steve Block791712a2010-08-27 10:21:07 +0100780 const bool trim_array = !Heap::lo_space()->Contains(elms) &&
Steve Block6ded16b2010-05-10 14:33:55 +0100781 ((actual_start + item_count) <
782 (len - actual_delete_count - actual_start));
783 if (trim_array) {
784 const int delta = actual_delete_count - item_count;
Andrei Popescu402d9372010-02-26 13:31:12 +0000785
Steve Block6ded16b2010-05-10 14:33:55 +0100786 if (actual_start > 0) {
787 Object** start = elms->data_start();
788 memmove(start + delta, start, actual_start * kPointerSize);
789 }
790
791 elms = LeftTrimFixedArray(elms, delta);
792 array->set_elements(elms, SKIP_WRITE_BARRIER);
793 } else {
794 AssertNoAllocation no_gc;
795 MoveElements(&no_gc,
796 elms, actual_start + item_count,
797 elms, actual_start + actual_delete_count,
798 (len - actual_delete_count - actual_start));
799 FillWithHoles(elms, new_length, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000800 }
Steve Block6ded16b2010-05-10 14:33:55 +0100801 } else if (item_count > actual_delete_count) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000802 // Currently fixed arrays cannot grow too big, so
803 // we should never hit this case.
Steve Block6ded16b2010-05-10 14:33:55 +0100804 ASSERT((item_count - actual_delete_count) <= (Smi::kMaxValue - len));
Andrei Popescu402d9372010-02-26 13:31:12 +0000805
806 // Check if array need to grow.
807 if (new_length > elms->length()) {
808 // New backing storage is needed.
809 int capacity = new_length + (new_length >> 1) + 16;
John Reck59135872010-11-02 12:39:01 -0700810 Object* obj;
811 { MaybeObject* maybe_obj =
812 Heap::AllocateUninitializedFixedArray(capacity);
813 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
814 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000815 FixedArray* new_elms = FixedArray::cast(obj);
Andrei Popescu402d9372010-02-26 13:31:12 +0000816
Steve Block6ded16b2010-05-10 14:33:55 +0100817 AssertNoAllocation no_gc;
818 // Copy the part before actual_start as is.
819 if (actual_start > 0) {
820 CopyElements(&no_gc, new_elms, 0, elms, 0, actual_start);
Andrei Popescu402d9372010-02-26 13:31:12 +0000821 }
Steve Block6ded16b2010-05-10 14:33:55 +0100822 const int to_copy = len - actual_delete_count - actual_start;
823 if (to_copy > 0) {
824 CopyElements(&no_gc,
825 new_elms, actual_start + item_count,
826 elms, actual_start + actual_delete_count,
827 to_copy);
828 }
829 FillWithHoles(new_elms, new_length, capacity);
Andrei Popescu402d9372010-02-26 13:31:12 +0000830
Andrei Popescu402d9372010-02-26 13:31:12 +0000831 elms = new_elms;
832 array->set_elements(elms);
Steve Block6ded16b2010-05-10 14:33:55 +0100833 } else {
834 AssertNoAllocation no_gc;
835 MoveElements(&no_gc,
836 elms, actual_start + item_count,
837 elms, actual_start + actual_delete_count,
838 (len - actual_delete_count - actual_start));
Andrei Popescu402d9372010-02-26 13:31:12 +0000839 }
840 }
841
Steve Block6ded16b2010-05-10 14:33:55 +0100842 AssertNoAllocation no_gc;
843 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
844 for (int k = actual_start; k < actual_start + item_count; k++) {
845 elms->set(k, args[3 + k - actual_start], mode);
Andrei Popescu402d9372010-02-26 13:31:12 +0000846 }
847
848 // Set the length.
849 array->set_length(Smi::FromInt(new_length));
850
851 return result_array;
852}
853
854
Steve Block6ded16b2010-05-10 14:33:55 +0100855BUILTIN(ArrayConcat) {
Kristian Monsen25f61362010-05-21 11:50:48 +0100856 Context* global_context = Top::context()->global_context();
857 JSObject* array_proto =
858 JSObject::cast(global_context->array_function()->prototype());
859 if (!ArrayPrototypeHasNoElements(global_context, array_proto)) {
Steve Block6ded16b2010-05-10 14:33:55 +0100860 return CallJsBuiltin("ArrayConcat", args);
861 }
862
863 // Iterate through all the arguments performing checks
864 // and calculating total length.
865 int n_arguments = args.length();
866 int result_len = 0;
867 for (int i = 0; i < n_arguments; i++) {
868 Object* arg = args[i];
Kristian Monsen25f61362010-05-21 11:50:48 +0100869 if (!arg->IsJSArray() || !JSArray::cast(arg)->HasFastElements()
870 || JSArray::cast(arg)->GetPrototype() != array_proto) {
Steve Block6ded16b2010-05-10 14:33:55 +0100871 return CallJsBuiltin("ArrayConcat", args);
872 }
873
874 int len = Smi::cast(JSArray::cast(arg)->length())->value();
875
876 // We shouldn't overflow when adding another len.
877 const int kHalfOfMaxInt = 1 << (kBitsPerInt - 2);
878 STATIC_ASSERT(FixedArray::kMaxLength < kHalfOfMaxInt);
879 USE(kHalfOfMaxInt);
880 result_len += len;
881 ASSERT(result_len >= 0);
882
883 if (result_len > FixedArray::kMaxLength) {
884 return CallJsBuiltin("ArrayConcat", args);
885 }
886 }
887
888 if (result_len == 0) {
889 return AllocateEmptyJSArray();
890 }
891
892 // Allocate result.
John Reck59135872010-11-02 12:39:01 -0700893 Object* result;
894 { MaybeObject* maybe_result = AllocateJSArray();
895 if (!maybe_result->ToObject(&result)) return maybe_result;
896 }
Steve Block6ded16b2010-05-10 14:33:55 +0100897 JSArray* result_array = JSArray::cast(result);
898
John Reck59135872010-11-02 12:39:01 -0700899 { MaybeObject* maybe_result =
900 Heap::AllocateUninitializedFixedArray(result_len);
901 if (!maybe_result->ToObject(&result)) return maybe_result;
902 }
Steve Block6ded16b2010-05-10 14:33:55 +0100903 FixedArray* result_elms = FixedArray::cast(result);
904
905 // Copy data.
906 AssertNoAllocation no_gc;
907 int start_pos = 0;
908 for (int i = 0; i < n_arguments; i++) {
909 JSArray* array = JSArray::cast(args[i]);
910 int len = Smi::cast(array->length())->value();
911 if (len > 0) {
912 FixedArray* elms = FixedArray::cast(array->elements());
913 CopyElements(&no_gc, result_elms, start_pos, elms, 0, len);
914 start_pos += len;
915 }
916 }
917 ASSERT(start_pos == result_len);
918
919 // Set the length and elements.
920 result_array->set_length(Smi::FromInt(result_len));
921 result_array->set_elements(result_elms);
922
923 return result_array;
924}
925
926
Steve Blocka7e24c12009-10-30 11:49:00 +0000927// -----------------------------------------------------------------------------
928//
929
930
931// Returns the holder JSObject if the function can legally be called
932// with this receiver. Returns Heap::null_value() if the call is
933// illegal. Any arguments that don't fit the expected type is
934// overwritten with undefined. Arguments that do fit the expected
935// type is overwritten with the object in the prototype chain that
936// actually has that type.
937static inline Object* TypeCheck(int argc,
938 Object** argv,
939 FunctionTemplateInfo* info) {
940 Object* recv = argv[0];
941 Object* sig_obj = info->signature();
942 if (sig_obj->IsUndefined()) return recv;
943 SignatureInfo* sig = SignatureInfo::cast(sig_obj);
944 // If necessary, check the receiver
945 Object* recv_type = sig->receiver();
946
947 Object* holder = recv;
948 if (!recv_type->IsUndefined()) {
949 for (; holder != Heap::null_value(); holder = holder->GetPrototype()) {
950 if (holder->IsInstanceOf(FunctionTemplateInfo::cast(recv_type))) {
951 break;
952 }
953 }
954 if (holder == Heap::null_value()) return holder;
955 }
956 Object* args_obj = sig->args();
957 // If there is no argument signature we're done
958 if (args_obj->IsUndefined()) return holder;
959 FixedArray* args = FixedArray::cast(args_obj);
960 int length = args->length();
961 if (argc <= length) length = argc - 1;
962 for (int i = 0; i < length; i++) {
963 Object* argtype = args->get(i);
964 if (argtype->IsUndefined()) continue;
965 Object** arg = &argv[-1 - i];
966 Object* current = *arg;
967 for (; current != Heap::null_value(); current = current->GetPrototype()) {
968 if (current->IsInstanceOf(FunctionTemplateInfo::cast(argtype))) {
969 *arg = current;
970 break;
971 }
972 }
973 if (current == Heap::null_value()) *arg = Heap::undefined_value();
974 }
975 return holder;
976}
977
978
Leon Clarkee46be812010-01-19 14:06:41 +0000979template <bool is_construct>
John Reck59135872010-11-02 12:39:01 -0700980MUST_USE_RESULT static MaybeObject* HandleApiCallHelper(
Leon Clarkee46be812010-01-19 14:06:41 +0000981 BuiltinArguments<NEEDS_CALLED_FUNCTION> args) {
982 ASSERT(is_construct == CalledAsConstructor());
Steve Blocka7e24c12009-10-30 11:49:00 +0000983
Leon Clarkee46be812010-01-19 14:06:41 +0000984 HandleScope scope;
985 Handle<JSFunction> function = args.called_function();
Steve Block6ded16b2010-05-10 14:33:55 +0100986 ASSERT(function->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +0000987
Steve Block6ded16b2010-05-10 14:33:55 +0100988 FunctionTemplateInfo* fun_data = function->shared()->get_api_func_data();
Steve Blocka7e24c12009-10-30 11:49:00 +0000989 if (is_construct) {
Steve Block6ded16b2010-05-10 14:33:55 +0100990 Handle<FunctionTemplateInfo> desc(fun_data);
Steve Blocka7e24c12009-10-30 11:49:00 +0000991 bool pending_exception = false;
Leon Clarkee46be812010-01-19 14:06:41 +0000992 Factory::ConfigureInstance(desc, Handle<JSObject>::cast(args.receiver()),
Steve Blocka7e24c12009-10-30 11:49:00 +0000993 &pending_exception);
994 ASSERT(Top::has_pending_exception() == pending_exception);
995 if (pending_exception) return Failure::Exception();
Steve Block6ded16b2010-05-10 14:33:55 +0100996 fun_data = *desc;
Steve Blocka7e24c12009-10-30 11:49:00 +0000997 }
998
Steve Blocka7e24c12009-10-30 11:49:00 +0000999 Object* raw_holder = TypeCheck(args.length(), &args[0], fun_data);
1000
1001 if (raw_holder->IsNull()) {
1002 // This function cannot be called with the given receiver. Abort!
1003 Handle<Object> obj =
1004 Factory::NewTypeError("illegal_invocation", HandleVector(&function, 1));
1005 return Top::Throw(*obj);
1006 }
1007
1008 Object* raw_call_data = fun_data->call_code();
1009 if (!raw_call_data->IsUndefined()) {
1010 CallHandlerInfo* call_data = CallHandlerInfo::cast(raw_call_data);
1011 Object* callback_obj = call_data->callback();
1012 v8::InvocationCallback callback =
1013 v8::ToCData<v8::InvocationCallback>(callback_obj);
1014 Object* data_obj = call_data->data();
1015 Object* result;
1016
Steve Blocka7e24c12009-10-30 11:49:00 +00001017 Handle<Object> data_handle(data_obj);
1018 v8::Local<v8::Value> data = v8::Utils::ToLocal(data_handle);
1019 ASSERT(raw_holder->IsJSObject());
1020 v8::Local<v8::Function> callee = v8::Utils::ToLocal(function);
1021 Handle<JSObject> holder_handle(JSObject::cast(raw_holder));
1022 v8::Local<v8::Object> holder = v8::Utils::ToLocal(holder_handle);
Leon Clarkee46be812010-01-19 14:06:41 +00001023 LOG(ApiObjectAccess("call", JSObject::cast(*args.receiver())));
Steve Blocka7e24c12009-10-30 11:49:00 +00001024 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments(
1025 data,
1026 holder,
1027 callee,
1028 is_construct,
1029 reinterpret_cast<void**>(&args[0] - 1),
1030 args.length() - 1);
1031
1032 v8::Handle<v8::Value> value;
1033 {
1034 // Leaving JavaScript.
1035 VMState state(EXTERNAL);
Steve Blockd0582a62009-12-15 09:54:21 +00001036#ifdef ENABLE_LOGGING_AND_PROFILING
1037 state.set_external_callback(v8::ToCData<Address>(callback_obj));
1038#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001039 value = callback(new_args);
1040 }
1041 if (value.IsEmpty()) {
1042 result = Heap::undefined_value();
1043 } else {
1044 result = *reinterpret_cast<Object**>(*value);
1045 }
1046
1047 RETURN_IF_SCHEDULED_EXCEPTION();
1048 if (!is_construct || result->IsJSObject()) return result;
1049 }
1050
Leon Clarkee46be812010-01-19 14:06:41 +00001051 return *args.receiver();
Steve Blocka7e24c12009-10-30 11:49:00 +00001052}
Leon Clarkee46be812010-01-19 14:06:41 +00001053
1054
1055BUILTIN(HandleApiCall) {
1056 return HandleApiCallHelper<false>(args);
1057}
1058
1059
1060BUILTIN(HandleApiCallConstruct) {
1061 return HandleApiCallHelper<true>(args);
1062}
Steve Blocka7e24c12009-10-30 11:49:00 +00001063
1064
Andrei Popescu402d9372010-02-26 13:31:12 +00001065#ifdef DEBUG
1066
1067static void VerifyTypeCheck(Handle<JSObject> object,
1068 Handle<JSFunction> function) {
Steve Block6ded16b2010-05-10 14:33:55 +01001069 ASSERT(function->shared()->IsApiFunction());
1070 FunctionTemplateInfo* info = function->shared()->get_api_func_data();
Andrei Popescu402d9372010-02-26 13:31:12 +00001071 if (info->signature()->IsUndefined()) return;
1072 SignatureInfo* signature = SignatureInfo::cast(info->signature());
1073 Object* receiver_type = signature->receiver();
1074 if (receiver_type->IsUndefined()) return;
1075 FunctionTemplateInfo* type = FunctionTemplateInfo::cast(receiver_type);
1076 ASSERT(object->IsInstanceOf(type));
1077}
1078
1079#endif
1080
1081
1082BUILTIN(FastHandleApiCall) {
1083 ASSERT(!CalledAsConstructor());
1084 const bool is_construct = false;
1085
1086 // We expect four more arguments: function, callback, call data, and holder.
1087 const int args_length = args.length() - 4;
1088 ASSERT(args_length >= 0);
1089
1090 Handle<JSFunction> function = args.at<JSFunction>(args_length);
1091 Object* callback_obj = args[args_length + 1];
1092 Handle<Object> data_handle = args.at<Object>(args_length + 2);
1093 Handle<JSObject> checked_holder = args.at<JSObject>(args_length + 3);
1094
1095#ifdef DEBUG
1096 VerifyTypeCheck(checked_holder, function);
1097#endif
1098
1099 v8::Local<v8::Object> holder = v8::Utils::ToLocal(checked_holder);
1100 v8::Local<v8::Function> callee = v8::Utils::ToLocal(function);
1101 v8::InvocationCallback callback =
1102 v8::ToCData<v8::InvocationCallback>(callback_obj);
1103 v8::Local<v8::Value> data = v8::Utils::ToLocal(data_handle);
1104
1105 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments(
1106 data,
1107 holder,
1108 callee,
1109 is_construct,
1110 reinterpret_cast<void**>(&args[0] - 1),
1111 args_length - 1);
1112
1113 HandleScope scope;
1114 Object* result;
1115 v8::Handle<v8::Value> value;
1116 {
1117 // Leaving JavaScript.
1118 VMState state(EXTERNAL);
1119#ifdef ENABLE_LOGGING_AND_PROFILING
1120 state.set_external_callback(v8::ToCData<Address>(callback_obj));
1121#endif
1122 value = callback(new_args);
1123 }
1124 if (value.IsEmpty()) {
1125 result = Heap::undefined_value();
1126 } else {
1127 result = *reinterpret_cast<Object**>(*value);
1128 }
1129
1130 RETURN_IF_SCHEDULED_EXCEPTION();
1131 return result;
1132}
1133
1134
Steve Blocka7e24c12009-10-30 11:49:00 +00001135// Helper function to handle calls to non-function objects created through the
1136// API. The object can be called as either a constructor (using new) or just as
1137// a function (without new).
John Reck59135872010-11-02 12:39:01 -07001138MUST_USE_RESULT static MaybeObject* HandleApiCallAsFunctionOrConstructor(
Leon Clarkee46be812010-01-19 14:06:41 +00001139 bool is_construct_call,
1140 BuiltinArguments<NO_EXTRA_ARGUMENTS> args) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001141 // Non-functions are never called as constructors. Even if this is an object
1142 // called as a constructor the delegate call is not a construct call.
1143 ASSERT(!CalledAsConstructor());
1144
1145 Handle<Object> receiver = args.at<Object>(0);
1146
1147 // Get the object called.
Leon Clarkee46be812010-01-19 14:06:41 +00001148 JSObject* obj = JSObject::cast(*args.receiver());
Steve Blocka7e24c12009-10-30 11:49:00 +00001149
1150 // Get the invocation callback from the function descriptor that was
1151 // used to create the called object.
1152 ASSERT(obj->map()->has_instance_call_handler());
1153 JSFunction* constructor = JSFunction::cast(obj->map()->constructor());
Steve Block6ded16b2010-05-10 14:33:55 +01001154 ASSERT(constructor->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +00001155 Object* handler =
Steve Block6ded16b2010-05-10 14:33:55 +01001156 constructor->shared()->get_api_func_data()->instance_call_handler();
Steve Blocka7e24c12009-10-30 11:49:00 +00001157 ASSERT(!handler->IsUndefined());
1158 CallHandlerInfo* call_data = CallHandlerInfo::cast(handler);
1159 Object* callback_obj = call_data->callback();
1160 v8::InvocationCallback callback =
1161 v8::ToCData<v8::InvocationCallback>(callback_obj);
1162
1163 // Get the data for the call and perform the callback.
1164 Object* data_obj = call_data->data();
1165 Object* result;
1166 { HandleScope scope;
1167 v8::Local<v8::Object> self =
Leon Clarkee46be812010-01-19 14:06:41 +00001168 v8::Utils::ToLocal(Handle<JSObject>::cast(args.receiver()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001169 Handle<Object> data_handle(data_obj);
1170 v8::Local<v8::Value> data = v8::Utils::ToLocal(data_handle);
1171 Handle<JSFunction> callee_handle(constructor);
1172 v8::Local<v8::Function> callee = v8::Utils::ToLocal(callee_handle);
Leon Clarkee46be812010-01-19 14:06:41 +00001173 LOG(ApiObjectAccess("call non-function", JSObject::cast(*args.receiver())));
Steve Blocka7e24c12009-10-30 11:49:00 +00001174 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments(
1175 data,
1176 self,
1177 callee,
1178 is_construct_call,
1179 reinterpret_cast<void**>(&args[0] - 1),
1180 args.length() - 1);
1181 v8::Handle<v8::Value> value;
1182 {
1183 // Leaving JavaScript.
1184 VMState state(EXTERNAL);
Steve Blockd0582a62009-12-15 09:54:21 +00001185#ifdef ENABLE_LOGGING_AND_PROFILING
1186 state.set_external_callback(v8::ToCData<Address>(callback_obj));
1187#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001188 value = callback(new_args);
1189 }
1190 if (value.IsEmpty()) {
1191 result = Heap::undefined_value();
1192 } else {
1193 result = *reinterpret_cast<Object**>(*value);
1194 }
1195 }
1196 // Check for exceptions and return result.
1197 RETURN_IF_SCHEDULED_EXCEPTION();
1198 return result;
1199}
1200
1201
1202// Handle calls to non-function objects created through the API. This delegate
1203// function is used when the call is a normal function call.
1204BUILTIN(HandleApiCallAsFunction) {
1205 return HandleApiCallAsFunctionOrConstructor(false, args);
1206}
Steve Blocka7e24c12009-10-30 11:49:00 +00001207
1208
1209// Handle calls to non-function objects created through the API. This delegate
1210// function is used when the call is a construct call.
1211BUILTIN(HandleApiCallAsConstructor) {
1212 return HandleApiCallAsFunctionOrConstructor(true, args);
1213}
Steve Blocka7e24c12009-10-30 11:49:00 +00001214
1215
1216static void Generate_LoadIC_ArrayLength(MacroAssembler* masm) {
1217 LoadIC::GenerateArrayLength(masm);
1218}
1219
1220
1221static void Generate_LoadIC_StringLength(MacroAssembler* masm) {
1222 LoadIC::GenerateStringLength(masm);
1223}
1224
1225
1226static void Generate_LoadIC_FunctionPrototype(MacroAssembler* masm) {
1227 LoadIC::GenerateFunctionPrototype(masm);
1228}
1229
1230
1231static void Generate_LoadIC_Initialize(MacroAssembler* masm) {
1232 LoadIC::GenerateInitialize(masm);
1233}
1234
1235
1236static void Generate_LoadIC_PreMonomorphic(MacroAssembler* masm) {
1237 LoadIC::GeneratePreMonomorphic(masm);
1238}
1239
1240
1241static void Generate_LoadIC_Miss(MacroAssembler* masm) {
1242 LoadIC::GenerateMiss(masm);
1243}
1244
1245
1246static void Generate_LoadIC_Megamorphic(MacroAssembler* masm) {
1247 LoadIC::GenerateMegamorphic(masm);
1248}
1249
1250
1251static void Generate_LoadIC_Normal(MacroAssembler* masm) {
1252 LoadIC::GenerateNormal(masm);
1253}
1254
1255
1256static void Generate_KeyedLoadIC_Initialize(MacroAssembler* masm) {
1257 KeyedLoadIC::GenerateInitialize(masm);
1258}
1259
1260
1261static void Generate_KeyedLoadIC_Miss(MacroAssembler* masm) {
1262 KeyedLoadIC::GenerateMiss(masm);
1263}
1264
1265
1266static void Generate_KeyedLoadIC_Generic(MacroAssembler* masm) {
1267 KeyedLoadIC::GenerateGeneric(masm);
1268}
1269
1270
Leon Clarkee46be812010-01-19 14:06:41 +00001271static void Generate_KeyedLoadIC_String(MacroAssembler* masm) {
1272 KeyedLoadIC::GenerateString(masm);
1273}
1274
1275
Steve Block3ce2e202009-11-05 08:53:23 +00001276static void Generate_KeyedLoadIC_ExternalByteArray(MacroAssembler* masm) {
1277 KeyedLoadIC::GenerateExternalArray(masm, kExternalByteArray);
1278}
1279
1280
1281static void Generate_KeyedLoadIC_ExternalUnsignedByteArray(
1282 MacroAssembler* masm) {
1283 KeyedLoadIC::GenerateExternalArray(masm, kExternalUnsignedByteArray);
1284}
1285
1286
1287static void Generate_KeyedLoadIC_ExternalShortArray(MacroAssembler* masm) {
1288 KeyedLoadIC::GenerateExternalArray(masm, kExternalShortArray);
1289}
1290
1291
1292static void Generate_KeyedLoadIC_ExternalUnsignedShortArray(
1293 MacroAssembler* masm) {
1294 KeyedLoadIC::GenerateExternalArray(masm, kExternalUnsignedShortArray);
1295}
1296
1297
1298static void Generate_KeyedLoadIC_ExternalIntArray(MacroAssembler* masm) {
1299 KeyedLoadIC::GenerateExternalArray(masm, kExternalIntArray);
1300}
1301
1302
1303static void Generate_KeyedLoadIC_ExternalUnsignedIntArray(
1304 MacroAssembler* masm) {
1305 KeyedLoadIC::GenerateExternalArray(masm, kExternalUnsignedIntArray);
1306}
1307
1308
1309static void Generate_KeyedLoadIC_ExternalFloatArray(MacroAssembler* masm) {
1310 KeyedLoadIC::GenerateExternalArray(masm, kExternalFloatArray);
1311}
1312
1313
Steve Blocka7e24c12009-10-30 11:49:00 +00001314static void Generate_KeyedLoadIC_PreMonomorphic(MacroAssembler* masm) {
1315 KeyedLoadIC::GeneratePreMonomorphic(masm);
1316}
1317
Andrei Popescu402d9372010-02-26 13:31:12 +00001318static void Generate_KeyedLoadIC_IndexedInterceptor(MacroAssembler* masm) {
1319 KeyedLoadIC::GenerateIndexedInterceptor(masm);
1320}
1321
Steve Blocka7e24c12009-10-30 11:49:00 +00001322
1323static void Generate_StoreIC_Initialize(MacroAssembler* masm) {
1324 StoreIC::GenerateInitialize(masm);
1325}
1326
1327
1328static void Generate_StoreIC_Miss(MacroAssembler* masm) {
1329 StoreIC::GenerateMiss(masm);
1330}
1331
1332
Steve Block8defd9f2010-07-08 12:39:36 +01001333static void Generate_StoreIC_Normal(MacroAssembler* masm) {
1334 StoreIC::GenerateNormal(masm);
1335}
1336
1337
Steve Blocka7e24c12009-10-30 11:49:00 +00001338static void Generate_StoreIC_Megamorphic(MacroAssembler* masm) {
1339 StoreIC::GenerateMegamorphic(masm);
1340}
1341
1342
Steve Block6ded16b2010-05-10 14:33:55 +01001343static void Generate_StoreIC_ArrayLength(MacroAssembler* masm) {
1344 StoreIC::GenerateArrayLength(masm);
1345}
1346
1347
Steve Blocka7e24c12009-10-30 11:49:00 +00001348static void Generate_KeyedStoreIC_Generic(MacroAssembler* masm) {
1349 KeyedStoreIC::GenerateGeneric(masm);
1350}
1351
1352
Steve Block3ce2e202009-11-05 08:53:23 +00001353static void Generate_KeyedStoreIC_ExternalByteArray(MacroAssembler* masm) {
1354 KeyedStoreIC::GenerateExternalArray(masm, kExternalByteArray);
1355}
1356
1357
1358static void Generate_KeyedStoreIC_ExternalUnsignedByteArray(
1359 MacroAssembler* masm) {
1360 KeyedStoreIC::GenerateExternalArray(masm, kExternalUnsignedByteArray);
1361}
1362
1363
1364static void Generate_KeyedStoreIC_ExternalShortArray(MacroAssembler* masm) {
1365 KeyedStoreIC::GenerateExternalArray(masm, kExternalShortArray);
1366}
1367
1368
1369static void Generate_KeyedStoreIC_ExternalUnsignedShortArray(
1370 MacroAssembler* masm) {
1371 KeyedStoreIC::GenerateExternalArray(masm, kExternalUnsignedShortArray);
1372}
1373
1374
1375static void Generate_KeyedStoreIC_ExternalIntArray(MacroAssembler* masm) {
1376 KeyedStoreIC::GenerateExternalArray(masm, kExternalIntArray);
1377}
1378
1379
1380static void Generate_KeyedStoreIC_ExternalUnsignedIntArray(
1381 MacroAssembler* masm) {
1382 KeyedStoreIC::GenerateExternalArray(masm, kExternalUnsignedIntArray);
1383}
1384
1385
1386static void Generate_KeyedStoreIC_ExternalFloatArray(MacroAssembler* masm) {
1387 KeyedStoreIC::GenerateExternalArray(masm, kExternalFloatArray);
1388}
1389
1390
Steve Blocka7e24c12009-10-30 11:49:00 +00001391static void Generate_KeyedStoreIC_Miss(MacroAssembler* masm) {
1392 KeyedStoreIC::GenerateMiss(masm);
1393}
1394
1395
1396static void Generate_KeyedStoreIC_Initialize(MacroAssembler* masm) {
1397 KeyedStoreIC::GenerateInitialize(masm);
1398}
1399
1400
1401#ifdef ENABLE_DEBUGGER_SUPPORT
1402static void Generate_LoadIC_DebugBreak(MacroAssembler* masm) {
1403 Debug::GenerateLoadICDebugBreak(masm);
1404}
1405
1406
1407static void Generate_StoreIC_DebugBreak(MacroAssembler* masm) {
1408 Debug::GenerateStoreICDebugBreak(masm);
1409}
1410
1411
1412static void Generate_KeyedLoadIC_DebugBreak(MacroAssembler* masm) {
1413 Debug::GenerateKeyedLoadICDebugBreak(masm);
1414}
1415
1416
1417static void Generate_KeyedStoreIC_DebugBreak(MacroAssembler* masm) {
1418 Debug::GenerateKeyedStoreICDebugBreak(masm);
1419}
1420
1421
1422static void Generate_ConstructCall_DebugBreak(MacroAssembler* masm) {
1423 Debug::GenerateConstructCallDebugBreak(masm);
1424}
1425
1426
1427static void Generate_Return_DebugBreak(MacroAssembler* masm) {
1428 Debug::GenerateReturnDebugBreak(masm);
1429}
1430
1431
1432static void Generate_StubNoRegisters_DebugBreak(MacroAssembler* masm) {
1433 Debug::GenerateStubNoRegistersDebugBreak(masm);
1434}
Steve Block6ded16b2010-05-10 14:33:55 +01001435
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001436
1437static void Generate_Slot_DebugBreak(MacroAssembler* masm) {
1438 Debug::GenerateSlotDebugBreak(masm);
1439}
1440
1441
Steve Block6ded16b2010-05-10 14:33:55 +01001442static void Generate_PlainReturn_LiveEdit(MacroAssembler* masm) {
1443 Debug::GeneratePlainReturnLiveEdit(masm);
1444}
1445
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001446
Steve Block6ded16b2010-05-10 14:33:55 +01001447static void Generate_FrameDropper_LiveEdit(MacroAssembler* masm) {
1448 Debug::GenerateFrameDropperLiveEdit(masm);
1449}
Steve Blocka7e24c12009-10-30 11:49:00 +00001450#endif
1451
1452Object* Builtins::builtins_[builtin_count] = { NULL, };
1453const char* Builtins::names_[builtin_count] = { NULL, };
1454
Leon Clarkee46be812010-01-19 14:06:41 +00001455#define DEF_ENUM_C(name, ignore) FUNCTION_ADDR(Builtin_##name),
Steve Blocka7e24c12009-10-30 11:49:00 +00001456 Address Builtins::c_functions_[cfunction_count] = {
1457 BUILTIN_LIST_C(DEF_ENUM_C)
1458 };
1459#undef DEF_ENUM_C
1460
1461#define DEF_JS_NAME(name, ignore) #name,
1462#define DEF_JS_ARGC(ignore, argc) argc,
1463const char* Builtins::javascript_names_[id_count] = {
1464 BUILTINS_LIST_JS(DEF_JS_NAME)
1465};
1466
1467int Builtins::javascript_argc_[id_count] = {
1468 BUILTINS_LIST_JS(DEF_JS_ARGC)
1469};
1470#undef DEF_JS_NAME
1471#undef DEF_JS_ARGC
1472
1473static bool is_initialized = false;
1474void Builtins::Setup(bool create_heap_objects) {
1475 ASSERT(!is_initialized);
1476
1477 // Create a scope for the handles in the builtins.
1478 HandleScope scope;
1479
1480 struct BuiltinDesc {
1481 byte* generator;
1482 byte* c_code;
1483 const char* s_name; // name is only used for generating log information.
1484 int name;
1485 Code::Flags flags;
Leon Clarkee46be812010-01-19 14:06:41 +00001486 BuiltinExtraArguments extra_args;
Steve Blocka7e24c12009-10-30 11:49:00 +00001487 };
1488
Leon Clarkee46be812010-01-19 14:06:41 +00001489#define DEF_FUNCTION_PTR_C(name, extra_args) \
1490 { FUNCTION_ADDR(Generate_Adaptor), \
1491 FUNCTION_ADDR(Builtin_##name), \
1492 #name, \
1493 c_##name, \
1494 Code::ComputeFlags(Code::BUILTIN), \
1495 extra_args \
Steve Blocka7e24c12009-10-30 11:49:00 +00001496 },
1497
1498#define DEF_FUNCTION_PTR_A(name, kind, state) \
1499 { FUNCTION_ADDR(Generate_##name), \
1500 NULL, \
1501 #name, \
1502 name, \
Leon Clarkee46be812010-01-19 14:06:41 +00001503 Code::ComputeFlags(Code::kind, NOT_IN_LOOP, state), \
1504 NO_EXTRA_ARGUMENTS \
Steve Blocka7e24c12009-10-30 11:49:00 +00001505 },
1506
1507 // Define array of pointers to generators and C builtin functions.
1508 static BuiltinDesc functions[] = {
1509 BUILTIN_LIST_C(DEF_FUNCTION_PTR_C)
1510 BUILTIN_LIST_A(DEF_FUNCTION_PTR_A)
1511 BUILTIN_LIST_DEBUG_A(DEF_FUNCTION_PTR_A)
1512 // Terminator:
Leon Clarkee46be812010-01-19 14:06:41 +00001513 { NULL, NULL, NULL, builtin_count, static_cast<Code::Flags>(0),
1514 NO_EXTRA_ARGUMENTS }
Steve Blocka7e24c12009-10-30 11:49:00 +00001515 };
1516
1517#undef DEF_FUNCTION_PTR_C
1518#undef DEF_FUNCTION_PTR_A
1519
1520 // For now we generate builtin adaptor code into a stack-allocated
1521 // buffer, before copying it into individual code objects.
1522 byte buffer[4*KB];
1523
1524 // Traverse the list of builtins and generate an adaptor in a
1525 // separate code object for each one.
1526 for (int i = 0; i < builtin_count; i++) {
1527 if (create_heap_objects) {
1528 MacroAssembler masm(buffer, sizeof buffer);
1529 // Generate the code/adaptor.
Leon Clarkee46be812010-01-19 14:06:41 +00001530 typedef void (*Generator)(MacroAssembler*, int, BuiltinExtraArguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00001531 Generator g = FUNCTION_CAST<Generator>(functions[i].generator);
1532 // We pass all arguments to the generator, but it may not use all of
1533 // them. This works because the first arguments are on top of the
1534 // stack.
Leon Clarkee46be812010-01-19 14:06:41 +00001535 g(&masm, functions[i].name, functions[i].extra_args);
Steve Blocka7e24c12009-10-30 11:49:00 +00001536 // Move the code into the object heap.
1537 CodeDesc desc;
1538 masm.GetCode(&desc);
1539 Code::Flags flags = functions[i].flags;
John Reck59135872010-11-02 12:39:01 -07001540 Object* code = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00001541 {
1542 // During startup it's OK to always allocate and defer GC to later.
1543 // This simplifies things because we don't need to retry.
1544 AlwaysAllocateScope __scope__;
John Reck59135872010-11-02 12:39:01 -07001545 { MaybeObject* maybe_code =
1546 Heap::CreateCode(desc, flags, masm.CodeObject());
1547 if (!maybe_code->ToObject(&code)) {
1548 v8::internal::V8::FatalProcessOutOfMemory("CreateCode");
1549 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001550 }
1551 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001552 // Log the event and add the code to the builtins array.
Steve Block6ded16b2010-05-10 14:33:55 +01001553 PROFILE(CodeCreateEvent(Logger::BUILTIN_TAG,
1554 Code::cast(code), functions[i].s_name));
Steve Blocka7e24c12009-10-30 11:49:00 +00001555 builtins_[i] = code;
1556#ifdef ENABLE_DISASSEMBLER
1557 if (FLAG_print_builtin_code) {
1558 PrintF("Builtin: %s\n", functions[i].s_name);
1559 Code::cast(code)->Disassemble(functions[i].s_name);
1560 PrintF("\n");
1561 }
1562#endif
1563 } else {
1564 // Deserializing. The values will be filled in during IterateBuiltins.
1565 builtins_[i] = NULL;
1566 }
1567 names_[i] = functions[i].s_name;
1568 }
1569
1570 // Mark as initialized.
1571 is_initialized = true;
1572}
1573
1574
1575void Builtins::TearDown() {
1576 is_initialized = false;
1577}
1578
1579
1580void Builtins::IterateBuiltins(ObjectVisitor* v) {
1581 v->VisitPointers(&builtins_[0], &builtins_[0] + builtin_count);
1582}
1583
1584
1585const char* Builtins::Lookup(byte* pc) {
1586 if (is_initialized) { // may be called during initialization (disassembler!)
1587 for (int i = 0; i < builtin_count; i++) {
1588 Code* entry = Code::cast(builtins_[i]);
1589 if (entry->contains(pc)) {
1590 return names_[i];
1591 }
1592 }
1593 }
1594 return NULL;
1595}
1596
Steve Blocka7e24c12009-10-30 11:49:00 +00001597} } // namespace v8::internal