blob: 01e88f5593adb2ad9ec24cc975e30c2492a8c852 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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"
Ben Murdochb8e0da22011-05-16 14:20:40 +010034#include "gdb-jit.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000035#include "ic-inl.h"
Ben Murdoch3ef787d2012-04-12 10:51:47 +010036#include "heap-profiler.h"
37#include "mark-compact.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010038#include "vm-state-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000039
40namespace v8 {
41namespace internal {
42
Leon Clarkee46be812010-01-19 14:06:41 +000043namespace {
44
45// Arguments object passed to C++ builtins.
46template <BuiltinExtraArguments extra_args>
47class BuiltinArguments : public Arguments {
48 public:
49 BuiltinArguments(int length, Object** arguments)
50 : Arguments(length, arguments) { }
51
52 Object*& operator[] (int index) {
53 ASSERT(index < length());
54 return Arguments::operator[](index);
55 }
56
57 template <class S> Handle<S> at(int index) {
58 ASSERT(index < length());
59 return Arguments::at<S>(index);
60 }
61
62 Handle<Object> receiver() {
63 return Arguments::at<Object>(0);
64 }
65
66 Handle<JSFunction> called_function() {
67 STATIC_ASSERT(extra_args == NEEDS_CALLED_FUNCTION);
68 return Arguments::at<JSFunction>(Arguments::length() - 1);
69 }
70
71 // Gets the total number of arguments including the receiver (but
72 // excluding extra arguments).
73 int length() const {
74 STATIC_ASSERT(extra_args == NO_EXTRA_ARGUMENTS);
75 return Arguments::length();
76 }
77
78#ifdef DEBUG
79 void Verify() {
80 // Check we have at least the receiver.
81 ASSERT(Arguments::length() >= 1);
82 }
83#endif
84};
85
86
87// Specialize BuiltinArguments for the called function extra argument.
88
89template <>
90int BuiltinArguments<NEEDS_CALLED_FUNCTION>::length() const {
91 return Arguments::length() - 1;
92}
93
94#ifdef DEBUG
95template <>
96void BuiltinArguments<NEEDS_CALLED_FUNCTION>::Verify() {
97 // Check we have at least the receiver and the called function.
98 ASSERT(Arguments::length() >= 2);
99 // Make sure cast to JSFunction succeeds.
100 called_function();
101}
102#endif
103
104
105#define DEF_ARG_TYPE(name, spec) \
106 typedef BuiltinArguments<spec> name##ArgumentsType;
107BUILTIN_LIST_C(DEF_ARG_TYPE)
108#undef DEF_ARG_TYPE
109
110} // namespace
111
Steve Blocka7e24c12009-10-30 11:49:00 +0000112// ----------------------------------------------------------------------------
Leon Clarkee46be812010-01-19 14:06:41 +0000113// Support macro for defining builtins in C++.
Steve Blocka7e24c12009-10-30 11:49:00 +0000114// ----------------------------------------------------------------------------
115//
116// A builtin function is defined by writing:
117//
118// BUILTIN(name) {
119// ...
120// }
Steve Blocka7e24c12009-10-30 11:49:00 +0000121//
Leon Clarkee46be812010-01-19 14:06:41 +0000122// In the body of the builtin function the arguments can be accessed
123// through the BuiltinArguments object args.
Steve Blocka7e24c12009-10-30 11:49:00 +0000124
Leon Clarkee46be812010-01-19 14:06:41 +0000125#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000126
Steve Block44f0eee2011-05-26 01:26:41 +0100127#define BUILTIN(name) \
128 MUST_USE_RESULT static MaybeObject* Builtin_Impl_##name( \
129 name##ArgumentsType args, Isolate* isolate); \
130 MUST_USE_RESULT static MaybeObject* Builtin_##name( \
131 name##ArgumentsType args, Isolate* isolate) { \
132 ASSERT(isolate == Isolate::Current()); \
133 args.Verify(); \
134 return Builtin_Impl_##name(args, isolate); \
135 } \
136 MUST_USE_RESULT static MaybeObject* Builtin_Impl_##name( \
137 name##ArgumentsType args, Isolate* isolate)
Steve Blocka7e24c12009-10-30 11:49:00 +0000138
Leon Clarkee46be812010-01-19 14:06:41 +0000139#else // For release mode.
Steve Blocka7e24c12009-10-30 11:49:00 +0000140
Steve Block44f0eee2011-05-26 01:26:41 +0100141#define BUILTIN(name) \
142 static MaybeObject* Builtin_##name(name##ArgumentsType args, Isolate* isolate)
Leon Clarkee46be812010-01-19 14:06:41 +0000143
144#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000145
146
Steve Block44f0eee2011-05-26 01:26:41 +0100147static inline bool CalledAsConstructor(Isolate* isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000148#ifdef DEBUG
149 // Calculate the result using a full stack frame iterator and check
150 // that the state of the stack is as we assume it to be in the
151 // code below.
152 StackFrameIterator it;
153 ASSERT(it.frame()->is_exit());
154 it.Advance();
155 StackFrame* frame = it.frame();
156 bool reference_result = frame->is_construct();
157#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100158 Address fp = Isolate::c_entry_fp(isolate->thread_local_top());
Steve Blocka7e24c12009-10-30 11:49:00 +0000159 // Because we know fp points to an exit frame we can use the relevant
160 // part of ExitFrame::ComputeCallerState directly.
161 const int kCallerOffset = ExitFrameConstants::kCallerFPOffset;
162 Address caller_fp = Memory::Address_at(fp + kCallerOffset);
163 // This inlines the part of StackFrame::ComputeType that grabs the
164 // type of the current frame. Note that StackFrame::ComputeType
165 // has been specialized for each architecture so if any one of them
166 // changes this code has to be changed as well.
167 const int kMarkerOffset = StandardFrameConstants::kMarkerOffset;
168 const Smi* kConstructMarker = Smi::FromInt(StackFrame::CONSTRUCT);
169 Object* marker = Memory::Object_at(caller_fp + kMarkerOffset);
170 bool result = (marker == kConstructMarker);
171 ASSERT_EQ(result, reference_result);
172 return result;
173}
174
175// ----------------------------------------------------------------------------
176
Steve Blocka7e24c12009-10-30 11:49:00 +0000177BUILTIN(Illegal) {
178 UNREACHABLE();
Steve Block44f0eee2011-05-26 01:26:41 +0100179 return isolate->heap()->undefined_value(); // Make compiler happy.
Steve Blocka7e24c12009-10-30 11:49:00 +0000180}
Steve Blocka7e24c12009-10-30 11:49:00 +0000181
182
183BUILTIN(EmptyFunction) {
Steve Block44f0eee2011-05-26 01:26:41 +0100184 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000185}
Steve Blocka7e24c12009-10-30 11:49:00 +0000186
187
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100188static MaybeObject* ArrayCodeGenericCommon(Arguments* args,
189 Isolate* isolate,
190 JSFunction* constructor) {
Steve Block44f0eee2011-05-26 01:26:41 +0100191 Heap* heap = isolate->heap();
192 isolate->counters()->array_function_runtime()->Increment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000193
194 JSArray* array;
Steve Block44f0eee2011-05-26 01:26:41 +0100195 if (CalledAsConstructor(isolate)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100196 array = JSArray::cast((*args)[0]);
197 // Initialize elements and length in case later allocations fail so that the
198 // array object is initialized in a valid state.
199 array->set_length(Smi::FromInt(0));
200 array->set_elements(heap->empty_fixed_array());
201 if (!FLAG_smi_only_arrays) {
202 Context* global_context = isolate->context()->global_context();
203 if (array->GetElementsKind() == FAST_SMI_ONLY_ELEMENTS &&
204 !global_context->object_js_array_map()->IsUndefined()) {
205 array->set_map(Map::cast(global_context->object_js_array_map()));
206 }
207 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000208 } else {
209 // Allocate the JS Array
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100210 MaybeObject* maybe_obj = heap->AllocateJSObject(constructor);
211 if (!maybe_obj->To(&array)) return maybe_obj;
Steve Blocka7e24c12009-10-30 11:49:00 +0000212 }
213
Steve Blocka7e24c12009-10-30 11:49:00 +0000214 // Optimize the case where there is one argument and the argument is a
215 // small smi.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100216 if (args->length() == 2) {
217 Object* obj = (*args)[1];
Steve Blocka7e24c12009-10-30 11:49:00 +0000218 if (obj->IsSmi()) {
219 int len = Smi::cast(obj)->value();
220 if (len >= 0 && len < JSObject::kInitialMaxFastElementArray) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100221 Object* fixed_array;
Steve Block44f0eee2011-05-26 01:26:41 +0100222 { MaybeObject* maybe_obj = heap->AllocateFixedArrayWithHoles(len);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100223 if (!maybe_obj->ToObject(&fixed_array)) return maybe_obj;
John Reck59135872010-11-02 12:39:01 -0700224 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100225 // We do not use SetContent to skip the unnecessary elements type check.
226 array->set_elements(FixedArray::cast(fixed_array));
227 array->set_length(Smi::cast(obj));
Steve Blocka7e24c12009-10-30 11:49:00 +0000228 return array;
229 }
230 }
231 // Take the argument as the length.
John Reck59135872010-11-02 12:39:01 -0700232 { MaybeObject* maybe_obj = array->Initialize(0);
233 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
234 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100235 return array->SetElementsLength((*args)[1]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000236 }
237
238 // Optimize the case where there are no parameters passed.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100239 if (args->length() == 1) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000240 return array->Initialize(JSArray::kPreallocatedArrayElements);
241 }
242
Ben Murdoch85b71792012-04-11 18:30:58 +0100243 // Set length and elements on the array.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100244 int number_of_elements = args->length() - 1;
245 MaybeObject* maybe_object =
246 array->EnsureCanContainElements(args, 1, number_of_elements,
247 ALLOW_CONVERTED_DOUBLE_ELEMENTS);
248 if (maybe_object->IsFailure()) return maybe_object;
Ben Murdoch85b71792012-04-11 18:30:58 +0100249
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100250 // Allocate an appropriately typed elements array.
251 MaybeObject* maybe_elms;
252 ElementsKind elements_kind = array->GetElementsKind();
253 if (elements_kind == FAST_DOUBLE_ELEMENTS) {
254 maybe_elms = heap->AllocateUninitializedFixedDoubleArray(
255 number_of_elements);
256 } else {
257 maybe_elms = heap->AllocateFixedArrayWithHoles(number_of_elements);
258 }
259 FixedArrayBase* elms;
260 if (!maybe_elms->To<FixedArrayBase>(&elms)) return maybe_elms;
261
262 // Fill in the content
263 switch (array->GetElementsKind()) {
264 case FAST_SMI_ONLY_ELEMENTS: {
265 FixedArray* smi_elms = FixedArray::cast(elms);
266 for (int index = 0; index < number_of_elements; index++) {
267 smi_elms->set(index, (*args)[index+1], SKIP_WRITE_BARRIER);
268 }
269 break;
270 }
271 case FAST_ELEMENTS: {
272 AssertNoAllocation no_gc;
273 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
274 FixedArray* object_elms = FixedArray::cast(elms);
275 for (int index = 0; index < number_of_elements; index++) {
276 object_elms->set(index, (*args)[index+1], mode);
277 }
278 break;
279 }
280 case FAST_DOUBLE_ELEMENTS: {
281 FixedDoubleArray* double_elms = FixedDoubleArray::cast(elms);
282 for (int index = 0; index < number_of_elements; index++) {
283 double_elms->set(index, (*args)[index+1]->Number());
284 }
285 break;
286 }
287 default:
288 UNREACHABLE();
289 break;
290 }
291
292 array->set_elements(elms);
293 array->set_length(Smi::FromInt(number_of_elements));
Steve Blocka7e24c12009-10-30 11:49:00 +0000294 return array;
295}
Steve Blocka7e24c12009-10-30 11:49:00 +0000296
297
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100298BUILTIN(InternalArrayCodeGeneric) {
299 return ArrayCodeGenericCommon(
300 &args,
301 isolate,
302 isolate->context()->global_context()->internal_array_function());
Ben Murdochc7cc0282012-03-05 14:35:55 +0000303}
304
305
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100306BUILTIN(ArrayCodeGeneric) {
307 return ArrayCodeGenericCommon(
308 &args,
309 isolate,
310 isolate->context()->global_context()->array_function());
Ben Murdochc7cc0282012-03-05 14:35:55 +0000311}
312
313
Steve Block44f0eee2011-05-26 01:26:41 +0100314static void MoveElements(Heap* heap,
315 AssertNoAllocation* no_gc,
Steve Block6ded16b2010-05-10 14:33:55 +0100316 FixedArray* dst,
317 int dst_index,
318 FixedArray* src,
319 int src_index,
320 int len) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100321 if (len == 0) return;
Steve Block44f0eee2011-05-26 01:26:41 +0100322 ASSERT(dst->map() != HEAP->fixed_cow_array_map());
Steve Block6ded16b2010-05-10 14:33:55 +0100323 memmove(dst->data_start() + dst_index,
324 src->data_start() + src_index,
325 len * kPointerSize);
326 WriteBarrierMode mode = dst->GetWriteBarrierMode(*no_gc);
327 if (mode == UPDATE_WRITE_BARRIER) {
Steve Block44f0eee2011-05-26 01:26:41 +0100328 heap->RecordWrites(dst->address(), dst->OffsetOfElementAt(dst_index), len);
Steve Block6ded16b2010-05-10 14:33:55 +0100329 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100330 heap->incremental_marking()->RecordWrites(dst);
Steve Block6ded16b2010-05-10 14:33:55 +0100331}
332
333
Steve Block44f0eee2011-05-26 01:26:41 +0100334static void FillWithHoles(Heap* heap, FixedArray* dst, int from, int to) {
335 ASSERT(dst->map() != heap->fixed_cow_array_map());
336 MemsetPointer(dst->data_start() + from, heap->the_hole_value(), to - from);
Steve Block6ded16b2010-05-10 14:33:55 +0100337}
338
339
Steve Block44f0eee2011-05-26 01:26:41 +0100340static FixedArray* LeftTrimFixedArray(Heap* heap,
341 FixedArray* elms,
342 int to_trim) {
343 ASSERT(elms->map() != HEAP->fixed_cow_array_map());
Steve Block791712a2010-08-27 10:21:07 +0100344 // For now this trick is only applied to fixed arrays in new and paged space.
Steve Block6ded16b2010-05-10 14:33:55 +0100345 // In large object space the object's start must coincide with chunk
346 // and thus the trick is just not applicable.
Steve Block44f0eee2011-05-26 01:26:41 +0100347 ASSERT(!HEAP->lo_space()->Contains(elms));
Steve Block6ded16b2010-05-10 14:33:55 +0100348
349 STATIC_ASSERT(FixedArray::kMapOffset == 0);
350 STATIC_ASSERT(FixedArray::kLengthOffset == kPointerSize);
351 STATIC_ASSERT(FixedArray::kHeaderSize == 2 * kPointerSize);
352
353 Object** former_start = HeapObject::RawField(elms, 0);
354
355 const int len = elms->length();
356
Steve Block791712a2010-08-27 10:21:07 +0100357 if (to_trim > FixedArray::kHeaderSize / kPointerSize &&
Steve Block44f0eee2011-05-26 01:26:41 +0100358 !heap->new_space()->Contains(elms)) {
Steve Block791712a2010-08-27 10:21:07 +0100359 // If we are doing a big trim in old space then we zap the space that was
360 // formerly part of the array so that the GC (aided by the card-based
361 // remembered set) won't find pointers to new-space there.
362 Object** zap = reinterpret_cast<Object**>(elms->address());
363 zap++; // Header of filler must be at least one word so skip that.
364 for (int i = 1; i < to_trim; i++) {
365 *zap++ = Smi::FromInt(0);
366 }
367 }
Steve Block6ded16b2010-05-10 14:33:55 +0100368 // Technically in new space this write might be omitted (except for
369 // debug mode which iterates through the heap), but to play safer
370 // we still do it.
Steve Block44f0eee2011-05-26 01:26:41 +0100371 heap->CreateFillerObjectAt(elms->address(), to_trim * kPointerSize);
Steve Block6ded16b2010-05-10 14:33:55 +0100372
Steve Block44f0eee2011-05-26 01:26:41 +0100373 former_start[to_trim] = heap->fixed_array_map();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100374 former_start[to_trim + 1] = Smi::FromInt(len - to_trim);
Steve Block6ded16b2010-05-10 14:33:55 +0100375
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100376 // Maintain marking consistency for HeapObjectIterator and
377 // IncrementalMarking.
378 int size_delta = to_trim * kPointerSize;
379 if (heap->marking()->TransferMark(elms->address(),
380 elms->address() + size_delta)) {
381 MemoryChunk::IncrementLiveBytesFromMutator(elms->address(), -size_delta);
382 }
383
384 HEAP_PROFILE(heap, ObjectMoveEvent(elms->address(),
385 elms->address() + size_delta));
Steve Block791712a2010-08-27 10:21:07 +0100386 return FixedArray::cast(HeapObject::FromAddress(
387 elms->address() + to_trim * kPointerSize));
Steve Block6ded16b2010-05-10 14:33:55 +0100388}
389
390
Steve Block44f0eee2011-05-26 01:26:41 +0100391static bool ArrayPrototypeHasNoElements(Heap* heap,
392 Context* global_context,
Kristian Monsen25f61362010-05-21 11:50:48 +0100393 JSObject* array_proto) {
Steve Block6ded16b2010-05-10 14:33:55 +0100394 // This method depends on non writability of Object and Array prototype
395 // fields.
Steve Block44f0eee2011-05-26 01:26:41 +0100396 if (array_proto->elements() != heap->empty_fixed_array()) return false;
Steve Block6ded16b2010-05-10 14:33:55 +0100397 // Object.prototype
Steve Block1e0659c2011-05-24 12:43:12 +0100398 Object* proto = array_proto->GetPrototype();
Steve Block44f0eee2011-05-26 01:26:41 +0100399 if (proto == heap->null_value()) return false;
Steve Block1e0659c2011-05-24 12:43:12 +0100400 array_proto = JSObject::cast(proto);
Kristian Monsen25f61362010-05-21 11:50:48 +0100401 if (array_proto != global_context->initial_object_prototype()) return false;
Steve Block44f0eee2011-05-26 01:26:41 +0100402 if (array_proto->elements() != heap->empty_fixed_array()) return false;
Steve Block053d10c2011-06-13 19:13:29 +0100403 return array_proto->GetPrototype()->IsNull();
Steve Block6ded16b2010-05-10 14:33:55 +0100404}
405
406
John Reck59135872010-11-02 12:39:01 -0700407MUST_USE_RESULT
408static inline MaybeObject* EnsureJSArrayWithWritableFastElements(
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100409 Heap* heap, Object* receiver, Arguments* args, int first_added_arg) {
Iain Merrick75681382010-08-19 15:07:18 +0100410 if (!receiver->IsJSArray()) return NULL;
Steve Block6ded16b2010-05-10 14:33:55 +0100411 JSArray* array = JSArray::cast(receiver);
Steve Block9fac8402011-05-12 15:51:54 +0100412 HeapObject* elms = array->elements();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100413 Map* map = elms->map();
414 if (map == heap->fixed_array_map()) {
Ben Murdoch5710cea2012-05-21 14:52:42 +0100415 if (args == NULL || array->HasFastElements()) return elms;
416 if (array->HasFastDoubleElements()) {
417 ASSERT(elms == heap->empty_fixed_array());
418 MaybeObject* maybe_transition =
419 array->TransitionElementsKind(FAST_ELEMENTS);
420 if (maybe_transition->IsFailure()) return maybe_transition;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100421 return elms;
422 }
423 } else if (map == heap->fixed_cow_array_map()) {
424 MaybeObject* maybe_writable_result = array->EnsureWritableFastElements();
Ben Murdoch5710cea2012-05-21 14:52:42 +0100425 if (args == NULL || array->HasFastElements() ||
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100426 maybe_writable_result->IsFailure()) {
427 return maybe_writable_result;
428 }
429 } else {
430 return NULL;
Steve Block6ded16b2010-05-10 14:33:55 +0100431 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100432
433 // Need to ensure that the arguments passed in args can be contained in
434 // the array.
435 int args_length = args->length();
436 if (first_added_arg >= args_length) return array->elements();
437
438 MaybeObject* maybe_array = array->EnsureCanContainElements(
439 args,
440 first_added_arg,
441 args_length - first_added_arg,
442 DONT_ALLOW_DOUBLE_ELEMENTS);
443 if (maybe_array->IsFailure()) return maybe_array;
444 return array->elements();
Steve Block6ded16b2010-05-10 14:33:55 +0100445}
446
447
Steve Block44f0eee2011-05-26 01:26:41 +0100448static inline bool IsJSArrayFastElementMovingAllowed(Heap* heap,
449 JSArray* receiver) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100450 if (!FLAG_clever_optimizations) return false;
Steve Block44f0eee2011-05-26 01:26:41 +0100451 Context* global_context = heap->isolate()->context()->global_context();
Kristian Monsen25f61362010-05-21 11:50:48 +0100452 JSObject* array_proto =
453 JSObject::cast(global_context->array_function()->prototype());
Iain Merrick75681382010-08-19 15:07:18 +0100454 return receiver->GetPrototype() == array_proto &&
Steve Block44f0eee2011-05-26 01:26:41 +0100455 ArrayPrototypeHasNoElements(heap, global_context, array_proto);
Kristian Monsen25f61362010-05-21 11:50:48 +0100456}
457
458
John Reck59135872010-11-02 12:39:01 -0700459MUST_USE_RESULT static MaybeObject* CallJsBuiltin(
Steve Block44f0eee2011-05-26 01:26:41 +0100460 Isolate* isolate,
John Reck59135872010-11-02 12:39:01 -0700461 const char* name,
462 BuiltinArguments<NO_EXTRA_ARGUMENTS> args) {
Steve Block44f0eee2011-05-26 01:26:41 +0100463 HandleScope handleScope(isolate);
Steve Block6ded16b2010-05-10 14:33:55 +0100464
465 Handle<Object> js_builtin =
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100466 GetProperty(Handle<JSObject>(isolate->global_context()->builtins()),
467 name);
468 Handle<JSFunction> function = Handle<JSFunction>::cast(js_builtin);
469 int argc = args.length() - 1;
470 ScopedVector<Handle<Object> > argv(argc);
471 for (int i = 0; i < argc; ++i) {
472 argv[i] = args.at<Object>(i + 1);
Steve Block6ded16b2010-05-10 14:33:55 +0100473 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100474 bool pending_exception;
Steve Block6ded16b2010-05-10 14:33:55 +0100475 Handle<Object> result = Execution::Call(function,
476 args.receiver(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100477 argc,
Steve Block6ded16b2010-05-10 14:33:55 +0100478 argv.start(),
479 &pending_exception);
Steve Block6ded16b2010-05-10 14:33:55 +0100480 if (pending_exception) return Failure::Exception();
481 return *result;
482}
483
484
Steve Blocka7e24c12009-10-30 11:49:00 +0000485BUILTIN(ArrayPush) {
Steve Block44f0eee2011-05-26 01:26:41 +0100486 Heap* heap = isolate->heap();
Steve Block6ded16b2010-05-10 14:33:55 +0100487 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700488 Object* elms_obj;
489 { MaybeObject* maybe_elms_obj =
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100490 EnsureJSArrayWithWritableFastElements(heap, receiver, &args, 1);
Steve Block44f0eee2011-05-26 01:26:41 +0100491 if (maybe_elms_obj == NULL) {
492 return CallJsBuiltin(isolate, "ArrayPush", args);
493 }
John Reck59135872010-11-02 12:39:01 -0700494 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
495 }
Iain Merrick75681382010-08-19 15:07:18 +0100496 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100497 JSArray* array = JSArray::cast(receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +0000498
Steve Blocka7e24c12009-10-30 11:49:00 +0000499 int len = Smi::cast(array->length())->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000500 int to_add = args.length() - 1;
501 if (to_add == 0) {
502 return Smi::FromInt(len);
503 }
504 // Currently fixed arrays cannot grow too big, so
505 // we should never hit this case.
506 ASSERT(to_add <= (Smi::kMaxValue - len));
Steve Blocka7e24c12009-10-30 11:49:00 +0000507
Andrei Popescu402d9372010-02-26 13:31:12 +0000508 int new_length = len + to_add;
Steve Blocka7e24c12009-10-30 11:49:00 +0000509
Andrei Popescu402d9372010-02-26 13:31:12 +0000510 if (new_length > elms->length()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000511 // New backing storage is needed.
512 int capacity = new_length + (new_length >> 1) + 16;
John Reck59135872010-11-02 12:39:01 -0700513 Object* obj;
Steve Block44f0eee2011-05-26 01:26:41 +0100514 { MaybeObject* maybe_obj = heap->AllocateUninitializedFixedArray(capacity);
John Reck59135872010-11-02 12:39:01 -0700515 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
516 }
Steve Block6ded16b2010-05-10 14:33:55 +0100517 FixedArray* new_elms = FixedArray::cast(obj);
Leon Clarke4515c472010-02-03 11:58:03 +0000518
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100519 CopyObjectToObjectElements(elms, FAST_ELEMENTS, 0,
520 new_elms, FAST_ELEMENTS, 0, len);
Steve Block44f0eee2011-05-26 01:26:41 +0100521 FillWithHoles(heap, new_elms, new_length, capacity);
Steve Block6ded16b2010-05-10 14:33:55 +0100522
Andrei Popescu402d9372010-02-26 13:31:12 +0000523 elms = new_elms;
Steve Blocka7e24c12009-10-30 11:49:00 +0000524 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000525
Steve Block6ded16b2010-05-10 14:33:55 +0100526 // Add the provided values.
Andrei Popescu402d9372010-02-26 13:31:12 +0000527 AssertNoAllocation no_gc;
528 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
Andrei Popescu402d9372010-02-26 13:31:12 +0000529 for (int index = 0; index < to_add; index++) {
530 elms->set(index + len, args[index + 1], mode);
531 }
532
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100533 if (elms != array->elements()) {
534 array->set_elements(elms);
535 }
536
Steve Blocka7e24c12009-10-30 11:49:00 +0000537 // Set the length.
Leon Clarke4515c472010-02-03 11:58:03 +0000538 array->set_length(Smi::FromInt(new_length));
Andrei Popescu402d9372010-02-26 13:31:12 +0000539 return Smi::FromInt(new_length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000540}
Steve Blocka7e24c12009-10-30 11:49:00 +0000541
542
543BUILTIN(ArrayPop) {
Steve Block44f0eee2011-05-26 01:26:41 +0100544 Heap* heap = isolate->heap();
Steve Block6ded16b2010-05-10 14:33:55 +0100545 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700546 Object* elms_obj;
547 { MaybeObject* maybe_elms_obj =
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100548 EnsureJSArrayWithWritableFastElements(heap, receiver, NULL, 0);
Steve Block44f0eee2011-05-26 01:26:41 +0100549 if (maybe_elms_obj == NULL) return CallJsBuiltin(isolate, "ArrayPop", args);
John Reck59135872010-11-02 12:39:01 -0700550 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
551 }
Iain Merrick75681382010-08-19 15:07:18 +0100552 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100553 JSArray* array = JSArray::cast(receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +0000554
555 int len = Smi::cast(array->length())->value();
Steve Block44f0eee2011-05-26 01:26:41 +0100556 if (len == 0) return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000557
558 // Get top element
John Reck59135872010-11-02 12:39:01 -0700559 MaybeObject* top = elms->get(len - 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000560
561 // Set the length.
Leon Clarke4515c472010-02-03 11:58:03 +0000562 array->set_length(Smi::FromInt(len - 1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000563
564 if (!top->IsTheHole()) {
565 // Delete the top element.
566 elms->set_the_hole(len - 1);
567 return top;
568 }
569
Kristian Monsen25f61362010-05-21 11:50:48 +0100570 top = array->GetPrototype()->GetElement(len - 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000571
572 return top;
573}
Steve Blocka7e24c12009-10-30 11:49:00 +0000574
575
Andrei Popescu402d9372010-02-26 13:31:12 +0000576BUILTIN(ArrayShift) {
Steve Block44f0eee2011-05-26 01:26:41 +0100577 Heap* heap = isolate->heap();
Steve Block6ded16b2010-05-10 14:33:55 +0100578 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700579 Object* elms_obj;
580 { MaybeObject* maybe_elms_obj =
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100581 EnsureJSArrayWithWritableFastElements(heap, receiver, NULL, 0);
Steve Block44f0eee2011-05-26 01:26:41 +0100582 if (maybe_elms_obj == NULL)
583 return CallJsBuiltin(isolate, "ArrayShift", args);
John Reck59135872010-11-02 12:39:01 -0700584 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
585 }
Steve Block44f0eee2011-05-26 01:26:41 +0100586 if (!IsJSArrayFastElementMovingAllowed(heap, JSArray::cast(receiver))) {
587 return CallJsBuiltin(isolate, "ArrayShift", args);
Steve Block6ded16b2010-05-10 14:33:55 +0100588 }
Iain Merrick75681382010-08-19 15:07:18 +0100589 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100590 JSArray* array = JSArray::cast(receiver);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100591 ASSERT(array->HasFastTypeElements());
Andrei Popescu402d9372010-02-26 13:31:12 +0000592
593 int len = Smi::cast(array->length())->value();
Steve Block44f0eee2011-05-26 01:26:41 +0100594 if (len == 0) return heap->undefined_value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000595
Andrei Popescu402d9372010-02-26 13:31:12 +0000596 // Get first element
597 Object* first = elms->get(0);
598 if (first->IsTheHole()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100599 first = heap->undefined_value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000600 }
601
Steve Block44f0eee2011-05-26 01:26:41 +0100602 if (!heap->lo_space()->Contains(elms)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100603 array->set_elements(LeftTrimFixedArray(heap, elms, 1));
Steve Block6ded16b2010-05-10 14:33:55 +0100604 } else {
605 // Shift the elements.
606 AssertNoAllocation no_gc;
Steve Block44f0eee2011-05-26 01:26:41 +0100607 MoveElements(heap, &no_gc, elms, 0, elms, 1, len - 1);
608 elms->set(len - 1, heap->the_hole_value());
Andrei Popescu402d9372010-02-26 13:31:12 +0000609 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000610
611 // Set the length.
612 array->set_length(Smi::FromInt(len - 1));
613
614 return first;
615}
616
617
618BUILTIN(ArrayUnshift) {
Steve Block44f0eee2011-05-26 01:26:41 +0100619 Heap* heap = isolate->heap();
Steve Block6ded16b2010-05-10 14:33:55 +0100620 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700621 Object* elms_obj;
622 { MaybeObject* maybe_elms_obj =
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100623 EnsureJSArrayWithWritableFastElements(heap, receiver, NULL, 0);
Steve Block44f0eee2011-05-26 01:26:41 +0100624 if (maybe_elms_obj == NULL)
625 return CallJsBuiltin(isolate, "ArrayUnshift", args);
John Reck59135872010-11-02 12:39:01 -0700626 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
627 }
Steve Block44f0eee2011-05-26 01:26:41 +0100628 if (!IsJSArrayFastElementMovingAllowed(heap, JSArray::cast(receiver))) {
629 return CallJsBuiltin(isolate, "ArrayUnshift", args);
Steve Block6ded16b2010-05-10 14:33:55 +0100630 }
Iain Merrick75681382010-08-19 15:07:18 +0100631 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100632 JSArray* array = JSArray::cast(receiver);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100633 ASSERT(array->HasFastTypeElements());
Andrei Popescu402d9372010-02-26 13:31:12 +0000634
635 int len = Smi::cast(array->length())->value();
636 int to_add = args.length() - 1;
Andrei Popescu402d9372010-02-26 13:31:12 +0000637 int new_length = len + to_add;
638 // Currently fixed arrays cannot grow too big, so
639 // we should never hit this case.
640 ASSERT(to_add <= (Smi::kMaxValue - len));
641
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100642 MaybeObject* maybe_object =
643 array->EnsureCanContainElements(&args, 1, to_add,
644 DONT_ALLOW_DOUBLE_ELEMENTS);
645 if (maybe_object->IsFailure()) return maybe_object;
646
Andrei Popescu402d9372010-02-26 13:31:12 +0000647 if (new_length > elms->length()) {
648 // New backing storage is needed.
649 int capacity = new_length + (new_length >> 1) + 16;
John Reck59135872010-11-02 12:39:01 -0700650 Object* obj;
Steve Block44f0eee2011-05-26 01:26:41 +0100651 { MaybeObject* maybe_obj = heap->AllocateUninitializedFixedArray(capacity);
John Reck59135872010-11-02 12:39:01 -0700652 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
653 }
Steve Block6ded16b2010-05-10 14:33:55 +0100654 FixedArray* new_elms = FixedArray::cast(obj);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100655 CopyObjectToObjectElements(elms, FAST_ELEMENTS, 0,
656 new_elms, FAST_ELEMENTS, to_add, len);
Steve Block44f0eee2011-05-26 01:26:41 +0100657 FillWithHoles(heap, new_elms, new_length, capacity);
Andrei Popescu402d9372010-02-26 13:31:12 +0000658 elms = new_elms;
659 array->set_elements(elms);
660 } else {
661 AssertNoAllocation no_gc;
Steve Block44f0eee2011-05-26 01:26:41 +0100662 MoveElements(heap, &no_gc, elms, to_add, elms, 0, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000663 }
664
665 // Add the provided values.
666 AssertNoAllocation no_gc;
667 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
668 for (int i = 0; i < to_add; i++) {
669 elms->set(i, args[i + 1], mode);
670 }
671
672 // Set the length.
673 array->set_length(Smi::FromInt(new_length));
674 return Smi::FromInt(new_length);
675}
676
677
Andrei Popescu402d9372010-02-26 13:31:12 +0000678BUILTIN(ArraySlice) {
Steve Block44f0eee2011-05-26 01:26:41 +0100679 Heap* heap = isolate->heap();
Steve Block6ded16b2010-05-10 14:33:55 +0100680 Object* receiver = *args.receiver();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100681 FixedArray* elms;
682 int len = -1;
Steve Block9fac8402011-05-12 15:51:54 +0100683 if (receiver->IsJSArray()) {
684 JSArray* array = JSArray::cast(receiver);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100685 if (!array->HasFastTypeElements() ||
Steve Block44f0eee2011-05-26 01:26:41 +0100686 !IsJSArrayFastElementMovingAllowed(heap, array)) {
687 return CallJsBuiltin(isolate, "ArraySlice", args);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100688 }
Steve Block9fac8402011-05-12 15:51:54 +0100689
690 elms = FixedArray::cast(array->elements());
691 len = Smi::cast(array->length())->value();
692 } else {
693 // Array.slice(arguments, ...) is quite a common idiom (notably more
694 // than 50% of invocations in Web apps). Treat it in C++ as well.
695 Map* arguments_map =
Steve Block44f0eee2011-05-26 01:26:41 +0100696 isolate->context()->global_context()->arguments_boilerplate()->map();
Steve Block9fac8402011-05-12 15:51:54 +0100697
698 bool is_arguments_object_with_fast_elements =
699 receiver->IsJSObject()
700 && JSObject::cast(receiver)->map() == arguments_map
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100701 && JSObject::cast(receiver)->HasFastTypeElements();
Steve Block9fac8402011-05-12 15:51:54 +0100702 if (!is_arguments_object_with_fast_elements) {
Steve Block44f0eee2011-05-26 01:26:41 +0100703 return CallJsBuiltin(isolate, "ArraySlice", args);
Steve Block9fac8402011-05-12 15:51:54 +0100704 }
705 elms = FixedArray::cast(JSObject::cast(receiver)->elements());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100706 Object* len_obj = JSObject::cast(receiver)
Steve Block44f0eee2011-05-26 01:26:41 +0100707 ->InObjectPropertyAt(Heap::kArgumentsLengthIndex);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100708 if (!len_obj->IsSmi()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100709 return CallJsBuiltin(isolate, "ArraySlice", args);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100710 }
711 len = Smi::cast(len_obj)->value();
712 if (len > elms->length()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100713 return CallJsBuiltin(isolate, "ArraySlice", args);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100714 }
715 for (int i = 0; i < len; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100716 if (elms->get(i) == heap->the_hole_value()) {
717 return CallJsBuiltin(isolate, "ArraySlice", args);
Steve Block9fac8402011-05-12 15:51:54 +0100718 }
719 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100720 }
721 ASSERT(len >= 0);
Andrei Popescu402d9372010-02-26 13:31:12 +0000722 int n_arguments = args.length() - 1;
723
724 // Note carefully choosen defaults---if argument is missing,
Steve Block6ded16b2010-05-10 14:33:55 +0100725 // it's undefined which gets converted to 0 for relative_start
726 // and to len for relative_end.
727 int relative_start = 0;
728 int relative_end = len;
Andrei Popescu402d9372010-02-26 13:31:12 +0000729 if (n_arguments > 0) {
730 Object* arg1 = args[1];
731 if (arg1->IsSmi()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100732 relative_start = Smi::cast(arg1)->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000733 } else if (!arg1->IsUndefined()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100734 return CallJsBuiltin(isolate, "ArraySlice", args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000735 }
736 if (n_arguments > 1) {
737 Object* arg2 = args[2];
738 if (arg2->IsSmi()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100739 relative_end = Smi::cast(arg2)->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000740 } else if (!arg2->IsUndefined()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100741 return CallJsBuiltin(isolate, "ArraySlice", args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000742 }
743 }
744 }
745
746 // ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 6.
Steve Block6ded16b2010-05-10 14:33:55 +0100747 int k = (relative_start < 0) ? Max(len + relative_start, 0)
748 : Min(relative_start, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000749
750 // ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 8.
Steve Block6ded16b2010-05-10 14:33:55 +0100751 int final = (relative_end < 0) ? Max(len + relative_end, 0)
752 : Min(relative_end, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000753
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100754 ElementsKind elements_kind = JSObject::cast(receiver)->GetElementsKind();
755
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100756 // Calculate the length of result array.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100757 int result_len = Max(final - k, 0);
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100758
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100759 MaybeObject* maybe_array =
760 heap->AllocateJSArrayAndStorage(elements_kind,
761 result_len,
762 result_len);
763 JSArray* result_array;
764 if (!maybe_array->To(&result_array)) return maybe_array;
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100765
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100766 CopyObjectToObjectElements(elms, FAST_ELEMENTS, k,
767 FixedArray::cast(result_array->elements()),
768 FAST_ELEMENTS, 0, result_len);
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100769
Andrei Popescu402d9372010-02-26 13:31:12 +0000770 return result_array;
771}
772
773
774BUILTIN(ArraySplice) {
Steve Block44f0eee2011-05-26 01:26:41 +0100775 Heap* heap = isolate->heap();
Steve Block6ded16b2010-05-10 14:33:55 +0100776 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700777 Object* elms_obj;
778 { MaybeObject* maybe_elms_obj =
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100779 EnsureJSArrayWithWritableFastElements(heap, receiver, &args, 3);
Steve Block44f0eee2011-05-26 01:26:41 +0100780 if (maybe_elms_obj == NULL)
781 return CallJsBuiltin(isolate, "ArraySplice", args);
John Reck59135872010-11-02 12:39:01 -0700782 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
783 }
Steve Block44f0eee2011-05-26 01:26:41 +0100784 if (!IsJSArrayFastElementMovingAllowed(heap, JSArray::cast(receiver))) {
785 return CallJsBuiltin(isolate, "ArraySplice", args);
Steve Block6ded16b2010-05-10 14:33:55 +0100786 }
Iain Merrick75681382010-08-19 15:07:18 +0100787 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100788 JSArray* array = JSArray::cast(receiver);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100789 ASSERT(array->HasFastTypeElements());
Andrei Popescu402d9372010-02-26 13:31:12 +0000790
791 int len = Smi::cast(array->length())->value();
792
793 int n_arguments = args.length() - 1;
794
Steve Block6ded16b2010-05-10 14:33:55 +0100795 int relative_start = 0;
Steve Block1e0659c2011-05-24 12:43:12 +0100796 if (n_arguments > 0) {
797 Object* arg1 = args[1];
798 if (arg1->IsSmi()) {
799 relative_start = Smi::cast(arg1)->value();
800 } else if (!arg1->IsUndefined()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100801 return CallJsBuiltin(isolate, "ArraySplice", args);
Steve Block1e0659c2011-05-24 12:43:12 +0100802 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000803 }
Steve Block6ded16b2010-05-10 14:33:55 +0100804 int actual_start = (relative_start < 0) ? Max(len + relative_start, 0)
805 : Min(relative_start, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000806
807 // SpiderMonkey, TraceMonkey and JSC treat the case where no delete count is
Steve Block1e0659c2011-05-24 12:43:12 +0100808 // given as a request to delete all the elements from the start.
809 // And it differs from the case of undefined delete count.
Andrei Popescu402d9372010-02-26 13:31:12 +0000810 // This does not follow ECMA-262, but we do the same for
811 // compatibility.
Steve Block1e0659c2011-05-24 12:43:12 +0100812 int actual_delete_count;
813 if (n_arguments == 1) {
814 ASSERT(len - actual_start >= 0);
815 actual_delete_count = len - actual_start;
816 } else {
817 int value = 0; // ToInteger(undefined) == 0
818 if (n_arguments > 1) {
819 Object* arg2 = args[2];
820 if (arg2->IsSmi()) {
821 value = Smi::cast(arg2)->value();
822 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100823 return CallJsBuiltin(isolate, "ArraySplice", args);
Steve Block1e0659c2011-05-24 12:43:12 +0100824 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000825 }
Steve Block1e0659c2011-05-24 12:43:12 +0100826 actual_delete_count = Min(Max(value, 0), len - actual_start);
Andrei Popescu402d9372010-02-26 13:31:12 +0000827 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000828
Steve Block6ded16b2010-05-10 14:33:55 +0100829 JSArray* result_array = NULL;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100830 ElementsKind elements_kind =
831 JSObject::cast(receiver)->GetElementsKind();
832 MaybeObject* maybe_array =
833 heap->AllocateJSArrayAndStorage(elements_kind,
834 actual_delete_count,
835 actual_delete_count);
836 if (!maybe_array->To(&result_array)) return maybe_array;
Andrei Popescu402d9372010-02-26 13:31:12 +0000837
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100838 {
Steve Block6ded16b2010-05-10 14:33:55 +0100839 // Fill newly created array.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100840 CopyObjectToObjectElements(elms, FAST_ELEMENTS, actual_start,
841 FixedArray::cast(result_array->elements()),
842 FAST_ELEMENTS, 0, actual_delete_count);
Andrei Popescu402d9372010-02-26 13:31:12 +0000843 }
844
Steve Block6ded16b2010-05-10 14:33:55 +0100845 int item_count = (n_arguments > 1) ? (n_arguments - 2) : 0;
Steve Block6ded16b2010-05-10 14:33:55 +0100846 int new_length = len - actual_delete_count + item_count;
Andrei Popescu402d9372010-02-26 13:31:12 +0000847
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100848 bool elms_changed = false;
Steve Block6ded16b2010-05-10 14:33:55 +0100849 if (item_count < actual_delete_count) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000850 // Shrink the array.
Steve Block44f0eee2011-05-26 01:26:41 +0100851 const bool trim_array = !heap->lo_space()->Contains(elms) &&
Steve Block6ded16b2010-05-10 14:33:55 +0100852 ((actual_start + item_count) <
853 (len - actual_delete_count - actual_start));
854 if (trim_array) {
855 const int delta = actual_delete_count - item_count;
Andrei Popescu402d9372010-02-26 13:31:12 +0000856
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100857 {
Steve Block053d10c2011-06-13 19:13:29 +0100858 AssertNoAllocation no_gc;
859 MoveElements(heap, &no_gc, elms, delta, elms, 0, actual_start);
Steve Block6ded16b2010-05-10 14:33:55 +0100860 }
861
Steve Block44f0eee2011-05-26 01:26:41 +0100862 elms = LeftTrimFixedArray(heap, elms, delta);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100863
864 elms_changed = true;
Steve Block6ded16b2010-05-10 14:33:55 +0100865 } else {
866 AssertNoAllocation no_gc;
Steve Block44f0eee2011-05-26 01:26:41 +0100867 MoveElements(heap, &no_gc,
Steve Block6ded16b2010-05-10 14:33:55 +0100868 elms, actual_start + item_count,
869 elms, actual_start + actual_delete_count,
870 (len - actual_delete_count - actual_start));
Steve Block44f0eee2011-05-26 01:26:41 +0100871 FillWithHoles(heap, elms, new_length, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000872 }
Steve Block6ded16b2010-05-10 14:33:55 +0100873 } else if (item_count > actual_delete_count) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000874 // Currently fixed arrays cannot grow too big, so
875 // we should never hit this case.
Steve Block6ded16b2010-05-10 14:33:55 +0100876 ASSERT((item_count - actual_delete_count) <= (Smi::kMaxValue - len));
Andrei Popescu402d9372010-02-26 13:31:12 +0000877
878 // Check if array need to grow.
879 if (new_length > elms->length()) {
880 // New backing storage is needed.
881 int capacity = new_length + (new_length >> 1) + 16;
John Reck59135872010-11-02 12:39:01 -0700882 Object* obj;
883 { MaybeObject* maybe_obj =
Steve Block44f0eee2011-05-26 01:26:41 +0100884 heap->AllocateUninitializedFixedArray(capacity);
John Reck59135872010-11-02 12:39:01 -0700885 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
886 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000887 FixedArray* new_elms = FixedArray::cast(obj);
Andrei Popescu402d9372010-02-26 13:31:12 +0000888
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100889 {
890 // Copy the part before actual_start as is.
891 CopyObjectToObjectElements(elms, FAST_ELEMENTS, 0,
892 new_elms, FAST_ELEMENTS, 0, actual_start);
893 const int to_copy = len - actual_delete_count - actual_start;
894 CopyObjectToObjectElements(elms, FAST_ELEMENTS,
895 actual_start + actual_delete_count,
896 new_elms, FAST_ELEMENTS,
897 actual_start + item_count, to_copy);
Andrei Popescu402d9372010-02-26 13:31:12 +0000898 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100899
Steve Block44f0eee2011-05-26 01:26:41 +0100900 FillWithHoles(heap, new_elms, new_length, capacity);
Andrei Popescu402d9372010-02-26 13:31:12 +0000901
Andrei Popescu402d9372010-02-26 13:31:12 +0000902 elms = new_elms;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100903 elms_changed = true;
Steve Block6ded16b2010-05-10 14:33:55 +0100904 } else {
905 AssertNoAllocation no_gc;
Steve Block44f0eee2011-05-26 01:26:41 +0100906 MoveElements(heap, &no_gc,
Steve Block6ded16b2010-05-10 14:33:55 +0100907 elms, actual_start + item_count,
908 elms, actual_start + actual_delete_count,
909 (len - actual_delete_count - actual_start));
Andrei Popescu402d9372010-02-26 13:31:12 +0000910 }
911 }
912
Steve Block6ded16b2010-05-10 14:33:55 +0100913 AssertNoAllocation no_gc;
914 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
915 for (int k = actual_start; k < actual_start + item_count; k++) {
916 elms->set(k, args[3 + k - actual_start], mode);
Andrei Popescu402d9372010-02-26 13:31:12 +0000917 }
918
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100919 if (elms_changed) {
920 array->set_elements(elms);
921 }
922
Andrei Popescu402d9372010-02-26 13:31:12 +0000923 // Set the length.
924 array->set_length(Smi::FromInt(new_length));
925
926 return result_array;
927}
928
929
Steve Block6ded16b2010-05-10 14:33:55 +0100930BUILTIN(ArrayConcat) {
Steve Block44f0eee2011-05-26 01:26:41 +0100931 Heap* heap = isolate->heap();
932 Context* global_context = isolate->context()->global_context();
Kristian Monsen25f61362010-05-21 11:50:48 +0100933 JSObject* array_proto =
934 JSObject::cast(global_context->array_function()->prototype());
Steve Block44f0eee2011-05-26 01:26:41 +0100935 if (!ArrayPrototypeHasNoElements(heap, global_context, array_proto)) {
936 return CallJsBuiltin(isolate, "ArrayConcat", args);
Steve Block6ded16b2010-05-10 14:33:55 +0100937 }
938
939 // Iterate through all the arguments performing checks
940 // and calculating total length.
941 int n_arguments = args.length();
942 int result_len = 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100943 ElementsKind elements_kind = FAST_SMI_ONLY_ELEMENTS;
Steve Block6ded16b2010-05-10 14:33:55 +0100944 for (int i = 0; i < n_arguments; i++) {
945 Object* arg = args[i];
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100946 if (!arg->IsJSArray() || !JSArray::cast(arg)->HasFastTypeElements()
Kristian Monsen25f61362010-05-21 11:50:48 +0100947 || JSArray::cast(arg)->GetPrototype() != array_proto) {
Steve Block44f0eee2011-05-26 01:26:41 +0100948 return CallJsBuiltin(isolate, "ArrayConcat", args);
Steve Block6ded16b2010-05-10 14:33:55 +0100949 }
950
951 int len = Smi::cast(JSArray::cast(arg)->length())->value();
952
953 // We shouldn't overflow when adding another len.
954 const int kHalfOfMaxInt = 1 << (kBitsPerInt - 2);
955 STATIC_ASSERT(FixedArray::kMaxLength < kHalfOfMaxInt);
956 USE(kHalfOfMaxInt);
957 result_len += len;
958 ASSERT(result_len >= 0);
959
960 if (result_len > FixedArray::kMaxLength) {
Steve Block44f0eee2011-05-26 01:26:41 +0100961 return CallJsBuiltin(isolate, "ArrayConcat", args);
Steve Block6ded16b2010-05-10 14:33:55 +0100962 }
Steve Block6ded16b2010-05-10 14:33:55 +0100963
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100964 if (!JSArray::cast(arg)->HasFastSmiOnlyElements()) {
965 elements_kind = FAST_ELEMENTS;
966 }
Steve Block6ded16b2010-05-10 14:33:55 +0100967 }
968
969 // Allocate result.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100970 JSArray* result_array;
971 MaybeObject* maybe_array =
972 heap->AllocateJSArrayAndStorage(elements_kind,
973 result_len,
974 result_len);
975 if (!maybe_array->To(&result_array)) return maybe_array;
976 if (result_len == 0) return result_array;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000977
Steve Block6ded16b2010-05-10 14:33:55 +0100978 // Copy data.
Steve Block6ded16b2010-05-10 14:33:55 +0100979 int start_pos = 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100980 FixedArray* result_elms(FixedArray::cast(result_array->elements()));
Steve Block6ded16b2010-05-10 14:33:55 +0100981 for (int i = 0; i < n_arguments; i++) {
982 JSArray* array = JSArray::cast(args[i]);
983 int len = Smi::cast(array->length())->value();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100984 FixedArray* elms = FixedArray::cast(array->elements());
985 CopyObjectToObjectElements(elms, FAST_ELEMENTS, 0,
986 result_elms, FAST_ELEMENTS,
987 start_pos, len);
988 start_pos += len;
Steve Block6ded16b2010-05-10 14:33:55 +0100989 }
990 ASSERT(start_pos == result_len);
991
Steve Block6ded16b2010-05-10 14:33:55 +0100992 return result_array;
993}
994
995
Steve Blocka7e24c12009-10-30 11:49:00 +0000996// -----------------------------------------------------------------------------
Steve Block44f0eee2011-05-26 01:26:41 +0100997// Strict mode poison pills
998
999
Ben Murdoch257744e2011-11-30 15:57:28 +00001000BUILTIN(StrictModePoisonPill) {
Steve Block44f0eee2011-05-26 01:26:41 +01001001 HandleScope scope;
1002 return isolate->Throw(*isolate->factory()->NewTypeError(
Ben Murdoch257744e2011-11-30 15:57:28 +00001003 "strict_poison_pill", HandleVector<Object>(NULL, 0)));
Steve Block44f0eee2011-05-26 01:26:41 +01001004}
1005
Steve Block44f0eee2011-05-26 01:26:41 +01001006// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +00001007//
1008
1009
1010// Returns the holder JSObject if the function can legally be called
1011// with this receiver. Returns Heap::null_value() if the call is
1012// illegal. Any arguments that don't fit the expected type is
1013// overwritten with undefined. Arguments that do fit the expected
1014// type is overwritten with the object in the prototype chain that
1015// actually has that type.
Steve Block44f0eee2011-05-26 01:26:41 +01001016static inline Object* TypeCheck(Heap* heap,
1017 int argc,
Steve Blocka7e24c12009-10-30 11:49:00 +00001018 Object** argv,
1019 FunctionTemplateInfo* info) {
1020 Object* recv = argv[0];
Ben Murdoch257744e2011-11-30 15:57:28 +00001021 // API calls are only supported with JSObject receivers.
1022 if (!recv->IsJSObject()) return heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00001023 Object* sig_obj = info->signature();
1024 if (sig_obj->IsUndefined()) return recv;
1025 SignatureInfo* sig = SignatureInfo::cast(sig_obj);
1026 // If necessary, check the receiver
1027 Object* recv_type = sig->receiver();
1028
1029 Object* holder = recv;
1030 if (!recv_type->IsUndefined()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001031 for (; holder != heap->null_value(); holder = holder->GetPrototype()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001032 if (holder->IsInstanceOf(FunctionTemplateInfo::cast(recv_type))) {
1033 break;
1034 }
1035 }
Steve Block44f0eee2011-05-26 01:26:41 +01001036 if (holder == heap->null_value()) return holder;
Steve Blocka7e24c12009-10-30 11:49:00 +00001037 }
1038 Object* args_obj = sig->args();
1039 // If there is no argument signature we're done
1040 if (args_obj->IsUndefined()) return holder;
1041 FixedArray* args = FixedArray::cast(args_obj);
1042 int length = args->length();
1043 if (argc <= length) length = argc - 1;
1044 for (int i = 0; i < length; i++) {
1045 Object* argtype = args->get(i);
1046 if (argtype->IsUndefined()) continue;
1047 Object** arg = &argv[-1 - i];
1048 Object* current = *arg;
Steve Block44f0eee2011-05-26 01:26:41 +01001049 for (; current != heap->null_value(); current = current->GetPrototype()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001050 if (current->IsInstanceOf(FunctionTemplateInfo::cast(argtype))) {
1051 *arg = current;
1052 break;
1053 }
1054 }
Steve Block44f0eee2011-05-26 01:26:41 +01001055 if (current == heap->null_value()) *arg = heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00001056 }
1057 return holder;
1058}
1059
1060
Leon Clarkee46be812010-01-19 14:06:41 +00001061template <bool is_construct>
John Reck59135872010-11-02 12:39:01 -07001062MUST_USE_RESULT static MaybeObject* HandleApiCallHelper(
Steve Block44f0eee2011-05-26 01:26:41 +01001063 BuiltinArguments<NEEDS_CALLED_FUNCTION> args, Isolate* isolate) {
1064 ASSERT(is_construct == CalledAsConstructor(isolate));
1065 Heap* heap = isolate->heap();
Steve Blocka7e24c12009-10-30 11:49:00 +00001066
Steve Block44f0eee2011-05-26 01:26:41 +01001067 HandleScope scope(isolate);
Leon Clarkee46be812010-01-19 14:06:41 +00001068 Handle<JSFunction> function = args.called_function();
Steve Block6ded16b2010-05-10 14:33:55 +01001069 ASSERT(function->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +00001070
Steve Block6ded16b2010-05-10 14:33:55 +01001071 FunctionTemplateInfo* fun_data = function->shared()->get_api_func_data();
Steve Blocka7e24c12009-10-30 11:49:00 +00001072 if (is_construct) {
Steve Block44f0eee2011-05-26 01:26:41 +01001073 Handle<FunctionTemplateInfo> desc(fun_data, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001074 bool pending_exception = false;
Steve Block44f0eee2011-05-26 01:26:41 +01001075 isolate->factory()->ConfigureInstance(
1076 desc, Handle<JSObject>::cast(args.receiver()), &pending_exception);
1077 ASSERT(isolate->has_pending_exception() == pending_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +00001078 if (pending_exception) return Failure::Exception();
Steve Block6ded16b2010-05-10 14:33:55 +01001079 fun_data = *desc;
Steve Blocka7e24c12009-10-30 11:49:00 +00001080 }
1081
Steve Block44f0eee2011-05-26 01:26:41 +01001082 Object* raw_holder = TypeCheck(heap, args.length(), &args[0], fun_data);
Steve Blocka7e24c12009-10-30 11:49:00 +00001083
1084 if (raw_holder->IsNull()) {
1085 // This function cannot be called with the given receiver. Abort!
1086 Handle<Object> obj =
Steve Block44f0eee2011-05-26 01:26:41 +01001087 isolate->factory()->NewTypeError(
1088 "illegal_invocation", HandleVector(&function, 1));
1089 return isolate->Throw(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001090 }
1091
1092 Object* raw_call_data = fun_data->call_code();
1093 if (!raw_call_data->IsUndefined()) {
1094 CallHandlerInfo* call_data = CallHandlerInfo::cast(raw_call_data);
1095 Object* callback_obj = call_data->callback();
1096 v8::InvocationCallback callback =
1097 v8::ToCData<v8::InvocationCallback>(callback_obj);
1098 Object* data_obj = call_data->data();
1099 Object* result;
1100
Steve Block44f0eee2011-05-26 01:26:41 +01001101 LOG(isolate, ApiObjectAccess("call", JSObject::cast(*args.receiver())));
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001102 ASSERT(raw_holder->IsJSObject());
1103
Steve Block44f0eee2011-05-26 01:26:41 +01001104 CustomArguments custom(isolate);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001105 v8::ImplementationUtilities::PrepareArgumentsData(custom.end(),
1106 data_obj, *function, raw_holder);
1107
Steve Blocka7e24c12009-10-30 11:49:00 +00001108 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments(
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001109 custom.end(),
1110 &args[0] - 1,
1111 args.length() - 1,
1112 is_construct);
Steve Blocka7e24c12009-10-30 11:49:00 +00001113
1114 v8::Handle<v8::Value> value;
1115 {
1116 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01001117 VMState state(isolate, EXTERNAL);
1118 ExternalCallbackScope call_scope(isolate,
1119 v8::ToCData<Address>(callback_obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00001120 value = callback(new_args);
1121 }
1122 if (value.IsEmpty()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001123 result = heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00001124 } else {
1125 result = *reinterpret_cast<Object**>(*value);
1126 }
1127
Steve Block44f0eee2011-05-26 01:26:41 +01001128 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001129 if (!is_construct || result->IsJSObject()) return result;
1130 }
1131
Leon Clarkee46be812010-01-19 14:06:41 +00001132 return *args.receiver();
Steve Blocka7e24c12009-10-30 11:49:00 +00001133}
Leon Clarkee46be812010-01-19 14:06:41 +00001134
1135
1136BUILTIN(HandleApiCall) {
Steve Block44f0eee2011-05-26 01:26:41 +01001137 return HandleApiCallHelper<false>(args, isolate);
Leon Clarkee46be812010-01-19 14:06:41 +00001138}
1139
1140
1141BUILTIN(HandleApiCallConstruct) {
Steve Block44f0eee2011-05-26 01:26:41 +01001142 return HandleApiCallHelper<true>(args, isolate);
Leon Clarkee46be812010-01-19 14:06:41 +00001143}
Steve Blocka7e24c12009-10-30 11:49:00 +00001144
1145
Andrei Popescu402d9372010-02-26 13:31:12 +00001146#ifdef DEBUG
1147
1148static void VerifyTypeCheck(Handle<JSObject> object,
1149 Handle<JSFunction> function) {
Steve Block6ded16b2010-05-10 14:33:55 +01001150 ASSERT(function->shared()->IsApiFunction());
1151 FunctionTemplateInfo* info = function->shared()->get_api_func_data();
Andrei Popescu402d9372010-02-26 13:31:12 +00001152 if (info->signature()->IsUndefined()) return;
1153 SignatureInfo* signature = SignatureInfo::cast(info->signature());
1154 Object* receiver_type = signature->receiver();
1155 if (receiver_type->IsUndefined()) return;
1156 FunctionTemplateInfo* type = FunctionTemplateInfo::cast(receiver_type);
1157 ASSERT(object->IsInstanceOf(type));
1158}
1159
1160#endif
1161
1162
1163BUILTIN(FastHandleApiCall) {
Steve Block44f0eee2011-05-26 01:26:41 +01001164 ASSERT(!CalledAsConstructor(isolate));
1165 Heap* heap = isolate->heap();
Andrei Popescu402d9372010-02-26 13:31:12 +00001166 const bool is_construct = false;
1167
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001168 // We expect four more arguments: callback, function, call data, and holder.
Andrei Popescu402d9372010-02-26 13:31:12 +00001169 const int args_length = args.length() - 4;
1170 ASSERT(args_length >= 0);
1171
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001172 Object* callback_obj = args[args_length];
Andrei Popescu402d9372010-02-26 13:31:12 +00001173
1174 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments(
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001175 &args[args_length + 1],
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001176 &args[0] - 1,
1177 args_length - 1,
1178 is_construct);
Andrei Popescu402d9372010-02-26 13:31:12 +00001179
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001180#ifdef DEBUG
1181 VerifyTypeCheck(Utils::OpenHandle(*new_args.Holder()),
1182 Utils::OpenHandle(*new_args.Callee()));
1183#endif
Steve Block44f0eee2011-05-26 01:26:41 +01001184 HandleScope scope(isolate);
Andrei Popescu402d9372010-02-26 13:31:12 +00001185 Object* result;
1186 v8::Handle<v8::Value> value;
1187 {
1188 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01001189 VMState state(isolate, EXTERNAL);
1190 ExternalCallbackScope call_scope(isolate,
1191 v8::ToCData<Address>(callback_obj));
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001192 v8::InvocationCallback callback =
1193 v8::ToCData<v8::InvocationCallback>(callback_obj);
1194
Andrei Popescu402d9372010-02-26 13:31:12 +00001195 value = callback(new_args);
1196 }
1197 if (value.IsEmpty()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001198 result = heap->undefined_value();
Andrei Popescu402d9372010-02-26 13:31:12 +00001199 } else {
1200 result = *reinterpret_cast<Object**>(*value);
1201 }
1202
Steve Block44f0eee2011-05-26 01:26:41 +01001203 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Andrei Popescu402d9372010-02-26 13:31:12 +00001204 return result;
1205}
1206
1207
Steve Blocka7e24c12009-10-30 11:49:00 +00001208// Helper function to handle calls to non-function objects created through the
1209// API. The object can be called as either a constructor (using new) or just as
1210// a function (without new).
John Reck59135872010-11-02 12:39:01 -07001211MUST_USE_RESULT static MaybeObject* HandleApiCallAsFunctionOrConstructor(
Steve Block44f0eee2011-05-26 01:26:41 +01001212 Isolate* isolate,
Leon Clarkee46be812010-01-19 14:06:41 +00001213 bool is_construct_call,
1214 BuiltinArguments<NO_EXTRA_ARGUMENTS> args) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001215 // Non-functions are never called as constructors. Even if this is an object
1216 // called as a constructor the delegate call is not a construct call.
Steve Block44f0eee2011-05-26 01:26:41 +01001217 ASSERT(!CalledAsConstructor(isolate));
1218 Heap* heap = isolate->heap();
Steve Blocka7e24c12009-10-30 11:49:00 +00001219
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001220 Handle<Object> receiver = args.receiver();
Steve Blocka7e24c12009-10-30 11:49:00 +00001221
1222 // Get the object called.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001223 JSObject* obj = JSObject::cast(*receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +00001224
1225 // Get the invocation callback from the function descriptor that was
1226 // used to create the called object.
1227 ASSERT(obj->map()->has_instance_call_handler());
1228 JSFunction* constructor = JSFunction::cast(obj->map()->constructor());
Steve Block6ded16b2010-05-10 14:33:55 +01001229 ASSERT(constructor->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +00001230 Object* handler =
Steve Block6ded16b2010-05-10 14:33:55 +01001231 constructor->shared()->get_api_func_data()->instance_call_handler();
Steve Blocka7e24c12009-10-30 11:49:00 +00001232 ASSERT(!handler->IsUndefined());
1233 CallHandlerInfo* call_data = CallHandlerInfo::cast(handler);
1234 Object* callback_obj = call_data->callback();
1235 v8::InvocationCallback callback =
1236 v8::ToCData<v8::InvocationCallback>(callback_obj);
1237
1238 // Get the data for the call and perform the callback.
Steve Blocka7e24c12009-10-30 11:49:00 +00001239 Object* result;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001240 {
Steve Block44f0eee2011-05-26 01:26:41 +01001241 HandleScope scope(isolate);
1242 LOG(isolate, ApiObjectAccess("call non-function", obj));
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001243
Steve Block44f0eee2011-05-26 01:26:41 +01001244 CustomArguments custom(isolate);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001245 v8::ImplementationUtilities::PrepareArgumentsData(custom.end(),
1246 call_data->data(), constructor, obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001247 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments(
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001248 custom.end(),
1249 &args[0] - 1,
1250 args.length() - 1,
1251 is_construct_call);
Steve Blocka7e24c12009-10-30 11:49:00 +00001252 v8::Handle<v8::Value> value;
1253 {
1254 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01001255 VMState state(isolate, EXTERNAL);
1256 ExternalCallbackScope call_scope(isolate,
1257 v8::ToCData<Address>(callback_obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00001258 value = callback(new_args);
1259 }
1260 if (value.IsEmpty()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001261 result = heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00001262 } else {
1263 result = *reinterpret_cast<Object**>(*value);
1264 }
1265 }
1266 // Check for exceptions and return result.
Steve Block44f0eee2011-05-26 01:26:41 +01001267 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001268 return result;
1269}
1270
1271
1272// Handle calls to non-function objects created through the API. This delegate
1273// function is used when the call is a normal function call.
1274BUILTIN(HandleApiCallAsFunction) {
Steve Block44f0eee2011-05-26 01:26:41 +01001275 return HandleApiCallAsFunctionOrConstructor(isolate, false, args);
Steve Blocka7e24c12009-10-30 11:49:00 +00001276}
Steve Blocka7e24c12009-10-30 11:49:00 +00001277
1278
1279// Handle calls to non-function objects created through the API. This delegate
1280// function is used when the call is a construct call.
1281BUILTIN(HandleApiCallAsConstructor) {
Steve Block44f0eee2011-05-26 01:26:41 +01001282 return HandleApiCallAsFunctionOrConstructor(isolate, true, args);
Steve Blocka7e24c12009-10-30 11:49:00 +00001283}
Steve Blocka7e24c12009-10-30 11:49:00 +00001284
1285
1286static void Generate_LoadIC_ArrayLength(MacroAssembler* masm) {
1287 LoadIC::GenerateArrayLength(masm);
1288}
1289
1290
1291static void Generate_LoadIC_StringLength(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01001292 LoadIC::GenerateStringLength(masm, false);
1293}
1294
1295
1296static void Generate_LoadIC_StringWrapperLength(MacroAssembler* masm) {
1297 LoadIC::GenerateStringLength(masm, true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001298}
1299
1300
1301static void Generate_LoadIC_FunctionPrototype(MacroAssembler* masm) {
1302 LoadIC::GenerateFunctionPrototype(masm);
1303}
1304
1305
1306static void Generate_LoadIC_Initialize(MacroAssembler* masm) {
1307 LoadIC::GenerateInitialize(masm);
1308}
1309
1310
1311static void Generate_LoadIC_PreMonomorphic(MacroAssembler* masm) {
1312 LoadIC::GeneratePreMonomorphic(masm);
1313}
1314
1315
1316static void Generate_LoadIC_Miss(MacroAssembler* masm) {
1317 LoadIC::GenerateMiss(masm);
1318}
1319
1320
1321static void Generate_LoadIC_Megamorphic(MacroAssembler* masm) {
1322 LoadIC::GenerateMegamorphic(masm);
1323}
1324
1325
1326static void Generate_LoadIC_Normal(MacroAssembler* masm) {
1327 LoadIC::GenerateNormal(masm);
1328}
1329
1330
1331static void Generate_KeyedLoadIC_Initialize(MacroAssembler* masm) {
1332 KeyedLoadIC::GenerateInitialize(masm);
1333}
1334
1335
Ben Murdoch257744e2011-11-30 15:57:28 +00001336static void Generate_KeyedLoadIC_Slow(MacroAssembler* masm) {
1337 KeyedLoadIC::GenerateRuntimeGetProperty(masm);
1338}
1339
1340
Steve Blocka7e24c12009-10-30 11:49:00 +00001341static void Generate_KeyedLoadIC_Miss(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001342 KeyedLoadIC::GenerateMiss(masm, false);
1343}
1344
1345
1346static void Generate_KeyedLoadIC_MissForceGeneric(MacroAssembler* masm) {
1347 KeyedLoadIC::GenerateMiss(masm, true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001348}
1349
1350
1351static void Generate_KeyedLoadIC_Generic(MacroAssembler* masm) {
1352 KeyedLoadIC::GenerateGeneric(masm);
1353}
1354
1355
Leon Clarkee46be812010-01-19 14:06:41 +00001356static void Generate_KeyedLoadIC_String(MacroAssembler* masm) {
1357 KeyedLoadIC::GenerateString(masm);
1358}
1359
1360
Steve Blocka7e24c12009-10-30 11:49:00 +00001361static void Generate_KeyedLoadIC_PreMonomorphic(MacroAssembler* masm) {
1362 KeyedLoadIC::GeneratePreMonomorphic(masm);
1363}
1364
Andrei Popescu402d9372010-02-26 13:31:12 +00001365static void Generate_KeyedLoadIC_IndexedInterceptor(MacroAssembler* masm) {
1366 KeyedLoadIC::GenerateIndexedInterceptor(masm);
1367}
1368
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001369static void Generate_KeyedLoadIC_NonStrictArguments(MacroAssembler* masm) {
1370 KeyedLoadIC::GenerateNonStrictArguments(masm);
1371}
Steve Blocka7e24c12009-10-30 11:49:00 +00001372
1373static void Generate_StoreIC_Initialize(MacroAssembler* masm) {
1374 StoreIC::GenerateInitialize(masm);
1375}
1376
1377
Steve Block1e0659c2011-05-24 12:43:12 +01001378static void Generate_StoreIC_Initialize_Strict(MacroAssembler* masm) {
1379 StoreIC::GenerateInitialize(masm);
1380}
1381
1382
Steve Blocka7e24c12009-10-30 11:49:00 +00001383static void Generate_StoreIC_Miss(MacroAssembler* masm) {
1384 StoreIC::GenerateMiss(masm);
1385}
1386
1387
Steve Block8defd9f2010-07-08 12:39:36 +01001388static void Generate_StoreIC_Normal(MacroAssembler* masm) {
1389 StoreIC::GenerateNormal(masm);
1390}
1391
1392
Steve Block1e0659c2011-05-24 12:43:12 +01001393static void Generate_StoreIC_Normal_Strict(MacroAssembler* masm) {
1394 StoreIC::GenerateNormal(masm);
1395}
1396
1397
Steve Blocka7e24c12009-10-30 11:49:00 +00001398static void Generate_StoreIC_Megamorphic(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001399 StoreIC::GenerateMegamorphic(masm, kNonStrictMode);
Steve Block1e0659c2011-05-24 12:43:12 +01001400}
1401
1402
1403static void Generate_StoreIC_Megamorphic_Strict(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001404 StoreIC::GenerateMegamorphic(masm, kStrictMode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001405}
1406
1407
Steve Block6ded16b2010-05-10 14:33:55 +01001408static void Generate_StoreIC_ArrayLength(MacroAssembler* masm) {
1409 StoreIC::GenerateArrayLength(masm);
1410}
1411
1412
Steve Block1e0659c2011-05-24 12:43:12 +01001413static void Generate_StoreIC_ArrayLength_Strict(MacroAssembler* masm) {
1414 StoreIC::GenerateArrayLength(masm);
1415}
1416
1417
Ben Murdochb0fe1622011-05-05 13:52:32 +01001418static void Generate_StoreIC_GlobalProxy(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001419 StoreIC::GenerateGlobalProxy(masm, kNonStrictMode);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001420}
1421
1422
Steve Block1e0659c2011-05-24 12:43:12 +01001423static void Generate_StoreIC_GlobalProxy_Strict(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001424 StoreIC::GenerateGlobalProxy(masm, kStrictMode);
Steve Block1e0659c2011-05-24 12:43:12 +01001425}
1426
1427
Steve Blocka7e24c12009-10-30 11:49:00 +00001428static void Generate_KeyedStoreIC_Generic(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001429 KeyedStoreIC::GenerateGeneric(masm, kNonStrictMode);
1430}
1431
1432
1433static void Generate_KeyedStoreIC_Generic_Strict(MacroAssembler* masm) {
1434 KeyedStoreIC::GenerateGeneric(masm, kStrictMode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001435}
1436
1437
Steve Blocka7e24c12009-10-30 11:49:00 +00001438static void Generate_KeyedStoreIC_Miss(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001439 KeyedStoreIC::GenerateMiss(masm, false);
1440}
1441
1442
1443static void Generate_KeyedStoreIC_MissForceGeneric(MacroAssembler* masm) {
1444 KeyedStoreIC::GenerateMiss(masm, true);
1445}
1446
1447
1448static void Generate_KeyedStoreIC_Slow(MacroAssembler* masm) {
1449 KeyedStoreIC::GenerateSlow(masm);
Steve Blocka7e24c12009-10-30 11:49:00 +00001450}
1451
1452
1453static void Generate_KeyedStoreIC_Initialize(MacroAssembler* masm) {
1454 KeyedStoreIC::GenerateInitialize(masm);
1455}
1456
1457
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001458static void Generate_KeyedStoreIC_Initialize_Strict(MacroAssembler* masm) {
1459 KeyedStoreIC::GenerateInitialize(masm);
1460}
1461
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001462static void Generate_KeyedStoreIC_NonStrictArguments(MacroAssembler* masm) {
1463 KeyedStoreIC::GenerateNonStrictArguments(masm);
1464}
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001465
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001466static void Generate_TransitionElementsSmiToDouble(MacroAssembler* masm) {
1467 KeyedStoreIC::GenerateTransitionElementsSmiToDouble(masm);
1468}
1469
1470static void Generate_TransitionElementsDoubleToObject(MacroAssembler* masm) {
1471 KeyedStoreIC::GenerateTransitionElementsDoubleToObject(masm);
1472}
1473
Steve Blocka7e24c12009-10-30 11:49:00 +00001474#ifdef ENABLE_DEBUGGER_SUPPORT
1475static void Generate_LoadIC_DebugBreak(MacroAssembler* masm) {
1476 Debug::GenerateLoadICDebugBreak(masm);
1477}
1478
1479
1480static void Generate_StoreIC_DebugBreak(MacroAssembler* masm) {
1481 Debug::GenerateStoreICDebugBreak(masm);
1482}
1483
1484
1485static void Generate_KeyedLoadIC_DebugBreak(MacroAssembler* masm) {
1486 Debug::GenerateKeyedLoadICDebugBreak(masm);
1487}
1488
1489
1490static void Generate_KeyedStoreIC_DebugBreak(MacroAssembler* masm) {
1491 Debug::GenerateKeyedStoreICDebugBreak(masm);
1492}
1493
1494
Steve Blocka7e24c12009-10-30 11:49:00 +00001495static void Generate_Return_DebugBreak(MacroAssembler* masm) {
1496 Debug::GenerateReturnDebugBreak(masm);
1497}
1498
1499
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001500static void Generate_CallFunctionStub_DebugBreak(MacroAssembler* masm) {
1501 Debug::GenerateCallFunctionStubDebugBreak(masm);
1502}
1503
1504
1505static void Generate_CallFunctionStub_Recording_DebugBreak(
1506 MacroAssembler* masm) {
1507 Debug::GenerateCallFunctionStubRecordDebugBreak(masm);
1508}
1509
1510
1511static void Generate_CallConstructStub_DebugBreak(MacroAssembler* masm) {
1512 Debug::GenerateCallConstructStubDebugBreak(masm);
1513}
1514
1515
1516static void Generate_CallConstructStub_Recording_DebugBreak(
1517 MacroAssembler* masm) {
1518 Debug::GenerateCallConstructStubRecordDebugBreak(masm);
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001519}
1520
1521
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001522static void Generate_Slot_DebugBreak(MacroAssembler* masm) {
1523 Debug::GenerateSlotDebugBreak(masm);
1524}
1525
1526
Steve Block6ded16b2010-05-10 14:33:55 +01001527static void Generate_PlainReturn_LiveEdit(MacroAssembler* masm) {
1528 Debug::GeneratePlainReturnLiveEdit(masm);
1529}
1530
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001531
Steve Block6ded16b2010-05-10 14:33:55 +01001532static void Generate_FrameDropper_LiveEdit(MacroAssembler* masm) {
1533 Debug::GenerateFrameDropperLiveEdit(masm);
1534}
Steve Blocka7e24c12009-10-30 11:49:00 +00001535#endif
1536
Steve Block44f0eee2011-05-26 01:26:41 +01001537
1538Builtins::Builtins() : initialized_(false) {
1539 memset(builtins_, 0, sizeof(builtins_[0]) * builtin_count);
1540 memset(names_, 0, sizeof(names_[0]) * builtin_count);
1541}
1542
1543
1544Builtins::~Builtins() {
1545}
1546
Steve Blocka7e24c12009-10-30 11:49:00 +00001547
Leon Clarkee46be812010-01-19 14:06:41 +00001548#define DEF_ENUM_C(name, ignore) FUNCTION_ADDR(Builtin_##name),
Steve Block44f0eee2011-05-26 01:26:41 +01001549Address const Builtins::c_functions_[cfunction_count] = {
1550 BUILTIN_LIST_C(DEF_ENUM_C)
1551};
Steve Blocka7e24c12009-10-30 11:49:00 +00001552#undef DEF_ENUM_C
1553
1554#define DEF_JS_NAME(name, ignore) #name,
1555#define DEF_JS_ARGC(ignore, argc) argc,
Steve Block44f0eee2011-05-26 01:26:41 +01001556const char* const Builtins::javascript_names_[id_count] = {
Steve Blocka7e24c12009-10-30 11:49:00 +00001557 BUILTINS_LIST_JS(DEF_JS_NAME)
1558};
1559
Steve Block44f0eee2011-05-26 01:26:41 +01001560int const Builtins::javascript_argc_[id_count] = {
Steve Blocka7e24c12009-10-30 11:49:00 +00001561 BUILTINS_LIST_JS(DEF_JS_ARGC)
1562};
1563#undef DEF_JS_NAME
1564#undef DEF_JS_ARGC
1565
Steve Block44f0eee2011-05-26 01:26:41 +01001566struct BuiltinDesc {
1567 byte* generator;
1568 byte* c_code;
1569 const char* s_name; // name is only used for generating log information.
1570 int name;
1571 Code::Flags flags;
1572 BuiltinExtraArguments extra_args;
1573};
1574
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001575#define BUILTIN_FUNCTION_TABLE_INIT { V8_ONCE_INIT, {} }
1576
Steve Block44f0eee2011-05-26 01:26:41 +01001577class BuiltinFunctionTable {
1578 public:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001579 BuiltinDesc* functions() {
1580 CallOnce(&once_, &Builtins::InitBuiltinFunctionTable);
1581 return functions_;
Steve Block44f0eee2011-05-26 01:26:41 +01001582 }
1583
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001584 OnceType once_;
1585 BuiltinDesc functions_[Builtins::builtin_count + 1];
Steve Block44f0eee2011-05-26 01:26:41 +01001586
1587 friend class Builtins;
1588};
1589
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001590static BuiltinFunctionTable builtin_function_table =
1591 BUILTIN_FUNCTION_TABLE_INIT;
Steve Block44f0eee2011-05-26 01:26:41 +01001592
1593// Define array of pointers to generators and C builtin functions.
1594// We do this in a sort of roundabout way so that we can do the initialization
1595// within the lexical scope of Builtins:: and within a context where
1596// Code::Flags names a non-abstract type.
1597void Builtins::InitBuiltinFunctionTable() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001598 BuiltinDesc* functions = builtin_function_table.functions_;
Steve Block44f0eee2011-05-26 01:26:41 +01001599 functions[builtin_count].generator = NULL;
1600 functions[builtin_count].c_code = NULL;
1601 functions[builtin_count].s_name = NULL;
1602 functions[builtin_count].name = builtin_count;
1603 functions[builtin_count].flags = static_cast<Code::Flags>(0);
1604 functions[builtin_count].extra_args = NO_EXTRA_ARGUMENTS;
1605
1606#define DEF_FUNCTION_PTR_C(aname, aextra_args) \
1607 functions->generator = FUNCTION_ADDR(Generate_Adaptor); \
1608 functions->c_code = FUNCTION_ADDR(Builtin_##aname); \
1609 functions->s_name = #aname; \
1610 functions->name = c_##aname; \
1611 functions->flags = Code::ComputeFlags(Code::BUILTIN); \
1612 functions->extra_args = aextra_args; \
1613 ++functions;
1614
1615#define DEF_FUNCTION_PTR_A(aname, kind, state, extra) \
1616 functions->generator = FUNCTION_ADDR(Generate_##aname); \
1617 functions->c_code = NULL; \
1618 functions->s_name = #aname; \
1619 functions->name = k##aname; \
1620 functions->flags = Code::ComputeFlags(Code::kind, \
Steve Block44f0eee2011-05-26 01:26:41 +01001621 state, \
1622 extra); \
1623 functions->extra_args = NO_EXTRA_ARGUMENTS; \
1624 ++functions;
1625
1626 BUILTIN_LIST_C(DEF_FUNCTION_PTR_C)
1627 BUILTIN_LIST_A(DEF_FUNCTION_PTR_A)
1628 BUILTIN_LIST_DEBUG_A(DEF_FUNCTION_PTR_A)
1629
1630#undef DEF_FUNCTION_PTR_C
1631#undef DEF_FUNCTION_PTR_A
1632}
1633
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001634void Builtins::SetUp(bool create_heap_objects) {
Steve Block44f0eee2011-05-26 01:26:41 +01001635 ASSERT(!initialized_);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001636 Isolate* isolate = Isolate::Current();
1637 Heap* heap = isolate->heap();
Steve Blocka7e24c12009-10-30 11:49:00 +00001638
1639 // Create a scope for the handles in the builtins.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001640 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001641
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001642 const BuiltinDesc* functions = builtin_function_table.functions();
Steve Blocka7e24c12009-10-30 11:49:00 +00001643
1644 // For now we generate builtin adaptor code into a stack-allocated
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001645 // buffer, before copying it into individual code objects. Be careful
1646 // with alignment, some platforms don't like unaligned code.
1647 union { int force_alignment; byte buffer[4*KB]; } u;
Steve Blocka7e24c12009-10-30 11:49:00 +00001648
1649 // Traverse the list of builtins and generate an adaptor in a
1650 // separate code object for each one.
1651 for (int i = 0; i < builtin_count; i++) {
1652 if (create_heap_objects) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001653 MacroAssembler masm(isolate, u.buffer, sizeof u.buffer);
Steve Blocka7e24c12009-10-30 11:49:00 +00001654 // Generate the code/adaptor.
Leon Clarkee46be812010-01-19 14:06:41 +00001655 typedef void (*Generator)(MacroAssembler*, int, BuiltinExtraArguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00001656 Generator g = FUNCTION_CAST<Generator>(functions[i].generator);
1657 // We pass all arguments to the generator, but it may not use all of
1658 // them. This works because the first arguments are on top of the
1659 // stack.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001660 ASSERT(!masm.has_frame());
Leon Clarkee46be812010-01-19 14:06:41 +00001661 g(&masm, functions[i].name, functions[i].extra_args);
Steve Blocka7e24c12009-10-30 11:49:00 +00001662 // Move the code into the object heap.
1663 CodeDesc desc;
1664 masm.GetCode(&desc);
1665 Code::Flags flags = functions[i].flags;
Ben Murdochb8e0da22011-05-16 14:20:40 +01001666 Object* code = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00001667 {
1668 // During startup it's OK to always allocate and defer GC to later.
1669 // This simplifies things because we don't need to retry.
1670 AlwaysAllocateScope __scope__;
John Reck59135872010-11-02 12:39:01 -07001671 { MaybeObject* maybe_code =
Steve Block44f0eee2011-05-26 01:26:41 +01001672 heap->CreateCode(desc, flags, masm.CodeObject());
John Reck59135872010-11-02 12:39:01 -07001673 if (!maybe_code->ToObject(&code)) {
1674 v8::internal::V8::FatalProcessOutOfMemory("CreateCode");
1675 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001676 }
1677 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001678 // Log the event and add the code to the builtins array.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001679 PROFILE(isolate,
Steve Block44f0eee2011-05-26 01:26:41 +01001680 CodeCreateEvent(Logger::BUILTIN_TAG,
Ben Murdochb8e0da22011-05-16 14:20:40 +01001681 Code::cast(code),
1682 functions[i].s_name));
1683 GDBJIT(AddCode(GDBJITInterface::BUILTIN,
1684 functions[i].s_name,
1685 Code::cast(code)));
Steve Blocka7e24c12009-10-30 11:49:00 +00001686 builtins_[i] = code;
1687#ifdef ENABLE_DISASSEMBLER
1688 if (FLAG_print_builtin_code) {
1689 PrintF("Builtin: %s\n", functions[i].s_name);
1690 Code::cast(code)->Disassemble(functions[i].s_name);
1691 PrintF("\n");
1692 }
1693#endif
1694 } else {
1695 // Deserializing. The values will be filled in during IterateBuiltins.
1696 builtins_[i] = NULL;
1697 }
1698 names_[i] = functions[i].s_name;
1699 }
1700
1701 // Mark as initialized.
Steve Block44f0eee2011-05-26 01:26:41 +01001702 initialized_ = true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001703}
1704
1705
1706void Builtins::TearDown() {
Steve Block44f0eee2011-05-26 01:26:41 +01001707 initialized_ = false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001708}
1709
1710
1711void Builtins::IterateBuiltins(ObjectVisitor* v) {
1712 v->VisitPointers(&builtins_[0], &builtins_[0] + builtin_count);
1713}
1714
1715
1716const char* Builtins::Lookup(byte* pc) {
Steve Block44f0eee2011-05-26 01:26:41 +01001717 // may be called during initialization (disassembler!)
1718 if (initialized_) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001719 for (int i = 0; i < builtin_count; i++) {
1720 Code* entry = Code::cast(builtins_[i]);
1721 if (entry->contains(pc)) {
1722 return names_[i];
1723 }
1724 }
1725 }
1726 return NULL;
1727}
1728
Ben Murdochb0fe1622011-05-05 13:52:32 +01001729
Steve Block44f0eee2011-05-26 01:26:41 +01001730#define DEFINE_BUILTIN_ACCESSOR_C(name, ignore) \
1731Handle<Code> Builtins::name() { \
1732 Code** code_address = \
1733 reinterpret_cast<Code**>(builtin_address(k##name)); \
1734 return Handle<Code>(code_address); \
1735}
1736#define DEFINE_BUILTIN_ACCESSOR_A(name, kind, state, extra) \
1737Handle<Code> Builtins::name() { \
1738 Code** code_address = \
1739 reinterpret_cast<Code**>(builtin_address(k##name)); \
1740 return Handle<Code>(code_address); \
1741}
1742BUILTIN_LIST_C(DEFINE_BUILTIN_ACCESSOR_C)
1743BUILTIN_LIST_A(DEFINE_BUILTIN_ACCESSOR_A)
1744BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A)
1745#undef DEFINE_BUILTIN_ACCESSOR_C
1746#undef DEFINE_BUILTIN_ACCESSOR_A
1747
1748
Steve Blocka7e24c12009-10-30 11:49:00 +00001749} } // namespace v8::internal