blob: e6a0699f076978ec80a73d061bda8490e04a127e [file] [log] [blame]
Ben Murdoch85b71792012-04-11 18:30:58 +01001// Copyright 2011 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 Murdochb0fe1622011-05-05 13:52:32 +010036#include "vm-state-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000037
38namespace v8 {
39namespace internal {
40
Leon Clarkee46be812010-01-19 14:06:41 +000041namespace {
42
43// Arguments object passed to C++ builtins.
44template <BuiltinExtraArguments extra_args>
45class BuiltinArguments : public Arguments {
46 public:
47 BuiltinArguments(int length, Object** arguments)
48 : Arguments(length, arguments) { }
49
50 Object*& operator[] (int index) {
51 ASSERT(index < length());
52 return Arguments::operator[](index);
53 }
54
55 template <class S> Handle<S> at(int index) {
56 ASSERT(index < length());
57 return Arguments::at<S>(index);
58 }
59
60 Handle<Object> receiver() {
61 return Arguments::at<Object>(0);
62 }
63
64 Handle<JSFunction> called_function() {
65 STATIC_ASSERT(extra_args == NEEDS_CALLED_FUNCTION);
66 return Arguments::at<JSFunction>(Arguments::length() - 1);
67 }
68
69 // Gets the total number of arguments including the receiver (but
70 // excluding extra arguments).
71 int length() const {
72 STATIC_ASSERT(extra_args == NO_EXTRA_ARGUMENTS);
73 return Arguments::length();
74 }
75
76#ifdef DEBUG
77 void Verify() {
78 // Check we have at least the receiver.
79 ASSERT(Arguments::length() >= 1);
80 }
81#endif
82};
83
84
85// Specialize BuiltinArguments for the called function extra argument.
86
87template <>
88int BuiltinArguments<NEEDS_CALLED_FUNCTION>::length() const {
89 return Arguments::length() - 1;
90}
91
92#ifdef DEBUG
93template <>
94void BuiltinArguments<NEEDS_CALLED_FUNCTION>::Verify() {
95 // Check we have at least the receiver and the called function.
96 ASSERT(Arguments::length() >= 2);
97 // Make sure cast to JSFunction succeeds.
98 called_function();
99}
100#endif
101
102
103#define DEF_ARG_TYPE(name, spec) \
104 typedef BuiltinArguments<spec> name##ArgumentsType;
105BUILTIN_LIST_C(DEF_ARG_TYPE)
106#undef DEF_ARG_TYPE
107
108} // namespace
109
Steve Blocka7e24c12009-10-30 11:49:00 +0000110// ----------------------------------------------------------------------------
Leon Clarkee46be812010-01-19 14:06:41 +0000111// Support macro for defining builtins in C++.
Steve Blocka7e24c12009-10-30 11:49:00 +0000112// ----------------------------------------------------------------------------
113//
114// A builtin function is defined by writing:
115//
116// BUILTIN(name) {
117// ...
118// }
Steve Blocka7e24c12009-10-30 11:49:00 +0000119//
Leon Clarkee46be812010-01-19 14:06:41 +0000120// In the body of the builtin function the arguments can be accessed
121// through the BuiltinArguments object args.
Steve Blocka7e24c12009-10-30 11:49:00 +0000122
Leon Clarkee46be812010-01-19 14:06:41 +0000123#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000124
Steve Block44f0eee2011-05-26 01:26:41 +0100125#define BUILTIN(name) \
126 MUST_USE_RESULT static MaybeObject* Builtin_Impl_##name( \
127 name##ArgumentsType args, Isolate* isolate); \
128 MUST_USE_RESULT static MaybeObject* Builtin_##name( \
129 name##ArgumentsType args, Isolate* isolate) { \
130 ASSERT(isolate == Isolate::Current()); \
131 args.Verify(); \
132 return Builtin_Impl_##name(args, isolate); \
133 } \
134 MUST_USE_RESULT static MaybeObject* Builtin_Impl_##name( \
135 name##ArgumentsType args, Isolate* isolate)
Steve Blocka7e24c12009-10-30 11:49:00 +0000136
Leon Clarkee46be812010-01-19 14:06:41 +0000137#else // For release mode.
Steve Blocka7e24c12009-10-30 11:49:00 +0000138
Steve Block44f0eee2011-05-26 01:26:41 +0100139#define BUILTIN(name) \
140 static MaybeObject* Builtin_##name(name##ArgumentsType args, Isolate* isolate)
Leon Clarkee46be812010-01-19 14:06:41 +0000141
142#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000143
144
Steve Block44f0eee2011-05-26 01:26:41 +0100145static inline bool CalledAsConstructor(Isolate* isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000146#ifdef DEBUG
147 // Calculate the result using a full stack frame iterator and check
148 // that the state of the stack is as we assume it to be in the
149 // code below.
150 StackFrameIterator it;
151 ASSERT(it.frame()->is_exit());
152 it.Advance();
153 StackFrame* frame = it.frame();
154 bool reference_result = frame->is_construct();
155#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100156 Address fp = Isolate::c_entry_fp(isolate->thread_local_top());
Steve Blocka7e24c12009-10-30 11:49:00 +0000157 // Because we know fp points to an exit frame we can use the relevant
158 // part of ExitFrame::ComputeCallerState directly.
159 const int kCallerOffset = ExitFrameConstants::kCallerFPOffset;
160 Address caller_fp = Memory::Address_at(fp + kCallerOffset);
161 // This inlines the part of StackFrame::ComputeType that grabs the
162 // type of the current frame. Note that StackFrame::ComputeType
163 // has been specialized for each architecture so if any one of them
164 // changes this code has to be changed as well.
165 const int kMarkerOffset = StandardFrameConstants::kMarkerOffset;
166 const Smi* kConstructMarker = Smi::FromInt(StackFrame::CONSTRUCT);
167 Object* marker = Memory::Object_at(caller_fp + kMarkerOffset);
168 bool result = (marker == kConstructMarker);
169 ASSERT_EQ(result, reference_result);
170 return result;
171}
172
173// ----------------------------------------------------------------------------
174
Steve Blocka7e24c12009-10-30 11:49:00 +0000175BUILTIN(Illegal) {
176 UNREACHABLE();
Steve Block44f0eee2011-05-26 01:26:41 +0100177 return isolate->heap()->undefined_value(); // Make compiler happy.
Steve Blocka7e24c12009-10-30 11:49:00 +0000178}
Steve Blocka7e24c12009-10-30 11:49:00 +0000179
180
181BUILTIN(EmptyFunction) {
Steve Block44f0eee2011-05-26 01:26:41 +0100182 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000183}
Steve Blocka7e24c12009-10-30 11:49:00 +0000184
185
Ben Murdoch85b71792012-04-11 18:30:58 +0100186BUILTIN(ArrayCodeGeneric) {
Steve Block44f0eee2011-05-26 01:26:41 +0100187 Heap* heap = isolate->heap();
188 isolate->counters()->array_function_runtime()->Increment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000189
190 JSArray* array;
Steve Block44f0eee2011-05-26 01:26:41 +0100191 if (CalledAsConstructor(isolate)) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100192 array = JSArray::cast(*args.receiver());
Steve Blocka7e24c12009-10-30 11:49:00 +0000193 } else {
194 // Allocate the JS Array
Ben Murdoch85b71792012-04-11 18:30:58 +0100195 JSFunction* constructor =
196 isolate->context()->global_context()->array_function();
197 Object* obj;
198 { MaybeObject* maybe_obj = heap->AllocateJSObject(constructor);
199 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
200 }
201 array = JSArray::cast(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +0000202 }
203
Ben Murdoch85b71792012-04-11 18:30:58 +0100204 // 'array' now contains the JSArray we should initialize.
205 ASSERT(array->HasFastElements());
206
Steve Blocka7e24c12009-10-30 11:49:00 +0000207 // Optimize the case where there is one argument and the argument is a
208 // small smi.
Ben Murdoch85b71792012-04-11 18:30:58 +0100209 if (args.length() == 2) {
210 Object* obj = args[1];
Steve Blocka7e24c12009-10-30 11:49:00 +0000211 if (obj->IsSmi()) {
212 int len = Smi::cast(obj)->value();
213 if (len >= 0 && len < JSObject::kInitialMaxFastElementArray) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100214 Object* obj;
Steve Block44f0eee2011-05-26 01:26:41 +0100215 { MaybeObject* maybe_obj = heap->AllocateFixedArrayWithHoles(len);
Ben Murdoch85b71792012-04-11 18:30:58 +0100216 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
John Reck59135872010-11-02 12:39:01 -0700217 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100218 array->SetContent(FixedArray::cast(obj));
Steve Blocka7e24c12009-10-30 11:49:00 +0000219 return array;
220 }
221 }
222 // Take the argument as the length.
John Reck59135872010-11-02 12:39:01 -0700223 { MaybeObject* maybe_obj = array->Initialize(0);
224 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
225 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100226 return array->SetElementsLength(args[1]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000227 }
228
229 // Optimize the case where there are no parameters passed.
Ben Murdoch85b71792012-04-11 18:30:58 +0100230 if (args.length() == 1) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000231 return array->Initialize(JSArray::kPreallocatedArrayElements);
232 }
233
Ben Murdoch85b71792012-04-11 18:30:58 +0100234 // Take the arguments as elements.
235 int number_of_elements = args.length() - 1;
236 Smi* len = Smi::FromInt(number_of_elements);
237 Object* obj;
238 { MaybeObject* maybe_obj = heap->AllocateFixedArrayWithHoles(len->value());
239 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
Ben Murdochc7cc0282012-03-05 14:35:55 +0000240 }
Ben Murdochc7cc0282012-03-05 14:35:55 +0000241
Ben Murdoch85b71792012-04-11 18:30:58 +0100242 AssertNoAllocation no_gc;
243 FixedArray* elms = FixedArray::cast(obj);
244 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000245 // Fill in the content
Ben Murdoch85b71792012-04-11 18:30:58 +0100246 for (int index = 0; index < number_of_elements; index++) {
247 elms->set(index, args[index+1], mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000248 }
249
Ben Murdoch85b71792012-04-11 18:30:58 +0100250 // Set length and elements on the array.
251 array->set_elements(FixedArray::cast(obj));
252 array->set_length(len);
253
Steve Blocka7e24c12009-10-30 11:49:00 +0000254 return array;
255}
Steve Blocka7e24c12009-10-30 11:49:00 +0000256
257
Ben Murdoch85b71792012-04-11 18:30:58 +0100258MUST_USE_RESULT static MaybeObject* AllocateJSArray(Heap* heap) {
259 JSFunction* array_function =
260 heap->isolate()->context()->global_context()->array_function();
261 Object* result;
262 { MaybeObject* maybe_result = heap->AllocateJSObject(array_function);
263 if (!maybe_result->ToObject(&result)) return maybe_result;
264 }
265 return result;
Ben Murdochc7cc0282012-03-05 14:35:55 +0000266}
267
268
Ben Murdoch85b71792012-04-11 18:30:58 +0100269MUST_USE_RESULT static MaybeObject* AllocateEmptyJSArray(Heap* heap) {
270 Object* result;
271 { MaybeObject* maybe_result = AllocateJSArray(heap);
272 if (!maybe_result->ToObject(&result)) return maybe_result;
273 }
274 JSArray* result_array = JSArray::cast(result);
275 result_array->set_length(Smi::FromInt(0));
276 result_array->set_elements(heap->empty_fixed_array());
277 return result_array;
278}
279
280
281static void CopyElements(Heap* heap,
282 AssertNoAllocation* no_gc,
283 FixedArray* dst,
284 int dst_index,
285 FixedArray* src,
286 int src_index,
287 int len) {
288 ASSERT(dst != src); // Use MoveElements instead.
289 ASSERT(dst->map() != HEAP->fixed_cow_array_map());
290 ASSERT(len > 0);
291 CopyWords(dst->data_start() + dst_index,
292 src->data_start() + src_index,
293 len);
294 WriteBarrierMode mode = dst->GetWriteBarrierMode(*no_gc);
295 if (mode == UPDATE_WRITE_BARRIER) {
296 heap->RecordWrites(dst->address(), dst->OffsetOfElementAt(dst_index), len);
297 }
Ben Murdochc7cc0282012-03-05 14:35:55 +0000298}
299
300
Steve Block44f0eee2011-05-26 01:26:41 +0100301static void MoveElements(Heap* heap,
302 AssertNoAllocation* no_gc,
Steve Block6ded16b2010-05-10 14:33:55 +0100303 FixedArray* dst,
304 int dst_index,
305 FixedArray* src,
306 int src_index,
307 int len) {
Steve Block44f0eee2011-05-26 01:26:41 +0100308 ASSERT(dst->map() != HEAP->fixed_cow_array_map());
Steve Block6ded16b2010-05-10 14:33:55 +0100309 memmove(dst->data_start() + dst_index,
310 src->data_start() + src_index,
311 len * kPointerSize);
312 WriteBarrierMode mode = dst->GetWriteBarrierMode(*no_gc);
313 if (mode == UPDATE_WRITE_BARRIER) {
Steve Block44f0eee2011-05-26 01:26:41 +0100314 heap->RecordWrites(dst->address(), dst->OffsetOfElementAt(dst_index), len);
Steve Block6ded16b2010-05-10 14:33:55 +0100315 }
316}
317
318
Steve Block44f0eee2011-05-26 01:26:41 +0100319static void FillWithHoles(Heap* heap, FixedArray* dst, int from, int to) {
320 ASSERT(dst->map() != heap->fixed_cow_array_map());
321 MemsetPointer(dst->data_start() + from, heap->the_hole_value(), to - from);
Steve Block6ded16b2010-05-10 14:33:55 +0100322}
323
324
Steve Block44f0eee2011-05-26 01:26:41 +0100325static FixedArray* LeftTrimFixedArray(Heap* heap,
326 FixedArray* elms,
327 int to_trim) {
328 ASSERT(elms->map() != HEAP->fixed_cow_array_map());
Steve Block791712a2010-08-27 10:21:07 +0100329 // For now this trick is only applied to fixed arrays in new and paged space.
Steve Block6ded16b2010-05-10 14:33:55 +0100330 // In large object space the object's start must coincide with chunk
331 // and thus the trick is just not applicable.
Steve Block44f0eee2011-05-26 01:26:41 +0100332 ASSERT(!HEAP->lo_space()->Contains(elms));
Steve Block6ded16b2010-05-10 14:33:55 +0100333
334 STATIC_ASSERT(FixedArray::kMapOffset == 0);
335 STATIC_ASSERT(FixedArray::kLengthOffset == kPointerSize);
336 STATIC_ASSERT(FixedArray::kHeaderSize == 2 * kPointerSize);
337
338 Object** former_start = HeapObject::RawField(elms, 0);
339
340 const int len = elms->length();
341
Steve Block791712a2010-08-27 10:21:07 +0100342 if (to_trim > FixedArray::kHeaderSize / kPointerSize &&
Steve Block44f0eee2011-05-26 01:26:41 +0100343 !heap->new_space()->Contains(elms)) {
Steve Block791712a2010-08-27 10:21:07 +0100344 // If we are doing a big trim in old space then we zap the space that was
345 // formerly part of the array so that the GC (aided by the card-based
346 // remembered set) won't find pointers to new-space there.
347 Object** zap = reinterpret_cast<Object**>(elms->address());
348 zap++; // Header of filler must be at least one word so skip that.
349 for (int i = 1; i < to_trim; i++) {
350 *zap++ = Smi::FromInt(0);
351 }
352 }
Steve Block6ded16b2010-05-10 14:33:55 +0100353 // Technically in new space this write might be omitted (except for
354 // debug mode which iterates through the heap), but to play safer
355 // we still do it.
Steve Block44f0eee2011-05-26 01:26:41 +0100356 heap->CreateFillerObjectAt(elms->address(), to_trim * kPointerSize);
Steve Block6ded16b2010-05-10 14:33:55 +0100357
Steve Block44f0eee2011-05-26 01:26:41 +0100358 former_start[to_trim] = heap->fixed_array_map();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100359 former_start[to_trim + 1] = Smi::FromInt(len - to_trim);
Steve Block6ded16b2010-05-10 14:33:55 +0100360
Steve Block791712a2010-08-27 10:21:07 +0100361 return FixedArray::cast(HeapObject::FromAddress(
362 elms->address() + to_trim * kPointerSize));
Steve Block6ded16b2010-05-10 14:33:55 +0100363}
364
365
Steve Block44f0eee2011-05-26 01:26:41 +0100366static bool ArrayPrototypeHasNoElements(Heap* heap,
367 Context* global_context,
Kristian Monsen25f61362010-05-21 11:50:48 +0100368 JSObject* array_proto) {
Steve Block6ded16b2010-05-10 14:33:55 +0100369 // This method depends on non writability of Object and Array prototype
370 // fields.
Steve Block44f0eee2011-05-26 01:26:41 +0100371 if (array_proto->elements() != heap->empty_fixed_array()) return false;
Ben Murdoch85b71792012-04-11 18:30:58 +0100372 // Hidden prototype
373 array_proto = JSObject::cast(array_proto->GetPrototype());
374 ASSERT(array_proto->elements() == heap->empty_fixed_array());
Steve Block6ded16b2010-05-10 14:33:55 +0100375 // Object.prototype
Steve Block1e0659c2011-05-24 12:43:12 +0100376 Object* proto = array_proto->GetPrototype();
Steve Block44f0eee2011-05-26 01:26:41 +0100377 if (proto == heap->null_value()) return false;
Steve Block1e0659c2011-05-24 12:43:12 +0100378 array_proto = JSObject::cast(proto);
Kristian Monsen25f61362010-05-21 11:50:48 +0100379 if (array_proto != global_context->initial_object_prototype()) return false;
Steve Block44f0eee2011-05-26 01:26:41 +0100380 if (array_proto->elements() != heap->empty_fixed_array()) return false;
Steve Block053d10c2011-06-13 19:13:29 +0100381 return array_proto->GetPrototype()->IsNull();
Steve Block6ded16b2010-05-10 14:33:55 +0100382}
383
384
John Reck59135872010-11-02 12:39:01 -0700385MUST_USE_RESULT
386static inline MaybeObject* EnsureJSArrayWithWritableFastElements(
Ben Murdoch85b71792012-04-11 18:30:58 +0100387 Heap* heap, Object* receiver) {
Iain Merrick75681382010-08-19 15:07:18 +0100388 if (!receiver->IsJSArray()) return NULL;
Steve Block6ded16b2010-05-10 14:33:55 +0100389 JSArray* array = JSArray::cast(receiver);
Steve Block9fac8402011-05-12 15:51:54 +0100390 HeapObject* elms = array->elements();
Ben Murdoch85b71792012-04-11 18:30:58 +0100391 if (elms->map() == heap->fixed_array_map()) return elms;
392 if (elms->map() == heap->fixed_cow_array_map()) {
393 return array->EnsureWritableFastElements();
Steve Block6ded16b2010-05-10 14:33:55 +0100394 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100395 return NULL;
Steve Block6ded16b2010-05-10 14:33:55 +0100396}
397
398
Steve Block44f0eee2011-05-26 01:26:41 +0100399static inline bool IsJSArrayFastElementMovingAllowed(Heap* heap,
400 JSArray* receiver) {
401 Context* global_context = heap->isolate()->context()->global_context();
Kristian Monsen25f61362010-05-21 11:50:48 +0100402 JSObject* array_proto =
403 JSObject::cast(global_context->array_function()->prototype());
Iain Merrick75681382010-08-19 15:07:18 +0100404 return receiver->GetPrototype() == array_proto &&
Steve Block44f0eee2011-05-26 01:26:41 +0100405 ArrayPrototypeHasNoElements(heap, global_context, array_proto);
Kristian Monsen25f61362010-05-21 11:50:48 +0100406}
407
408
John Reck59135872010-11-02 12:39:01 -0700409MUST_USE_RESULT static MaybeObject* CallJsBuiltin(
Steve Block44f0eee2011-05-26 01:26:41 +0100410 Isolate* isolate,
John Reck59135872010-11-02 12:39:01 -0700411 const char* name,
412 BuiltinArguments<NO_EXTRA_ARGUMENTS> args) {
Steve Block44f0eee2011-05-26 01:26:41 +0100413 HandleScope handleScope(isolate);
Steve Block6ded16b2010-05-10 14:33:55 +0100414
415 Handle<Object> js_builtin =
Ben Murdoch85b71792012-04-11 18:30:58 +0100416 GetProperty(Handle<JSObject>(
417 isolate->global_context()->builtins()),
418 name);
419 ASSERT(js_builtin->IsJSFunction());
420 Handle<JSFunction> function(Handle<JSFunction>::cast(js_builtin));
421 ScopedVector<Object**> argv(args.length() - 1);
422 int n_args = args.length() - 1;
423 for (int i = 0; i < n_args; i++) {
424 argv[i] = args.at<Object>(i + 1).location();
Steve Block6ded16b2010-05-10 14:33:55 +0100425 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100426 bool pending_exception = false;
Steve Block6ded16b2010-05-10 14:33:55 +0100427 Handle<Object> result = Execution::Call(function,
428 args.receiver(),
Ben Murdoch85b71792012-04-11 18:30:58 +0100429 n_args,
Steve Block6ded16b2010-05-10 14:33:55 +0100430 argv.start(),
431 &pending_exception);
Steve Block6ded16b2010-05-10 14:33:55 +0100432 if (pending_exception) return Failure::Exception();
433 return *result;
434}
435
436
Steve Blocka7e24c12009-10-30 11:49:00 +0000437BUILTIN(ArrayPush) {
Steve Block44f0eee2011-05-26 01:26:41 +0100438 Heap* heap = isolate->heap();
Steve Block6ded16b2010-05-10 14:33:55 +0100439 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700440 Object* elms_obj;
441 { MaybeObject* maybe_elms_obj =
Ben Murdoch85b71792012-04-11 18:30:58 +0100442 EnsureJSArrayWithWritableFastElements(heap, receiver);
Steve Block44f0eee2011-05-26 01:26:41 +0100443 if (maybe_elms_obj == NULL) {
444 return CallJsBuiltin(isolate, "ArrayPush", args);
445 }
John Reck59135872010-11-02 12:39:01 -0700446 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
447 }
Iain Merrick75681382010-08-19 15:07:18 +0100448 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100449 JSArray* array = JSArray::cast(receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +0000450
Steve Blocka7e24c12009-10-30 11:49:00 +0000451 int len = Smi::cast(array->length())->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000452 int to_add = args.length() - 1;
453 if (to_add == 0) {
454 return Smi::FromInt(len);
455 }
456 // Currently fixed arrays cannot grow too big, so
457 // we should never hit this case.
458 ASSERT(to_add <= (Smi::kMaxValue - len));
Steve Blocka7e24c12009-10-30 11:49:00 +0000459
Andrei Popescu402d9372010-02-26 13:31:12 +0000460 int new_length = len + to_add;
Steve Blocka7e24c12009-10-30 11:49:00 +0000461
Andrei Popescu402d9372010-02-26 13:31:12 +0000462 if (new_length > elms->length()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000463 // New backing storage is needed.
464 int capacity = new_length + (new_length >> 1) + 16;
John Reck59135872010-11-02 12:39:01 -0700465 Object* obj;
Steve Block44f0eee2011-05-26 01:26:41 +0100466 { MaybeObject* maybe_obj = heap->AllocateUninitializedFixedArray(capacity);
John Reck59135872010-11-02 12:39:01 -0700467 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
468 }
Steve Block6ded16b2010-05-10 14:33:55 +0100469 FixedArray* new_elms = FixedArray::cast(obj);
Leon Clarke4515c472010-02-03 11:58:03 +0000470
Ben Murdoch85b71792012-04-11 18:30:58 +0100471 AssertNoAllocation no_gc;
472 if (len > 0) {
473 CopyElements(heap, &no_gc, new_elms, 0, elms, 0, len);
474 }
Steve Block44f0eee2011-05-26 01:26:41 +0100475 FillWithHoles(heap, new_elms, new_length, capacity);
Steve Block6ded16b2010-05-10 14:33:55 +0100476
Andrei Popescu402d9372010-02-26 13:31:12 +0000477 elms = new_elms;
Ben Murdoch85b71792012-04-11 18:30:58 +0100478 array->set_elements(elms);
Steve Blocka7e24c12009-10-30 11:49:00 +0000479 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000480
Steve Block6ded16b2010-05-10 14:33:55 +0100481 // Add the provided values.
Andrei Popescu402d9372010-02-26 13:31:12 +0000482 AssertNoAllocation no_gc;
483 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
Andrei Popescu402d9372010-02-26 13:31:12 +0000484 for (int index = 0; index < to_add; index++) {
485 elms->set(index + len, args[index + 1], mode);
486 }
487
Steve Blocka7e24c12009-10-30 11:49:00 +0000488 // Set the length.
Leon Clarke4515c472010-02-03 11:58:03 +0000489 array->set_length(Smi::FromInt(new_length));
Andrei Popescu402d9372010-02-26 13:31:12 +0000490 return Smi::FromInt(new_length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000491}
Steve Blocka7e24c12009-10-30 11:49:00 +0000492
493
494BUILTIN(ArrayPop) {
Steve Block44f0eee2011-05-26 01:26:41 +0100495 Heap* heap = isolate->heap();
Steve Block6ded16b2010-05-10 14:33:55 +0100496 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700497 Object* elms_obj;
498 { MaybeObject* maybe_elms_obj =
Ben Murdoch85b71792012-04-11 18:30:58 +0100499 EnsureJSArrayWithWritableFastElements(heap, receiver);
Steve Block44f0eee2011-05-26 01:26:41 +0100500 if (maybe_elms_obj == NULL) return CallJsBuiltin(isolate, "ArrayPop", args);
John Reck59135872010-11-02 12:39:01 -0700501 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
502 }
Iain Merrick75681382010-08-19 15:07:18 +0100503 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100504 JSArray* array = JSArray::cast(receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +0000505
506 int len = Smi::cast(array->length())->value();
Steve Block44f0eee2011-05-26 01:26:41 +0100507 if (len == 0) return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000508
509 // Get top element
John Reck59135872010-11-02 12:39:01 -0700510 MaybeObject* top = elms->get(len - 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000511
512 // Set the length.
Leon Clarke4515c472010-02-03 11:58:03 +0000513 array->set_length(Smi::FromInt(len - 1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000514
515 if (!top->IsTheHole()) {
516 // Delete the top element.
517 elms->set_the_hole(len - 1);
518 return top;
519 }
520
Kristian Monsen25f61362010-05-21 11:50:48 +0100521 top = array->GetPrototype()->GetElement(len - 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000522
523 return top;
524}
Steve Blocka7e24c12009-10-30 11:49:00 +0000525
526
Andrei Popescu402d9372010-02-26 13:31:12 +0000527BUILTIN(ArrayShift) {
Steve Block44f0eee2011-05-26 01:26:41 +0100528 Heap* heap = isolate->heap();
Steve Block6ded16b2010-05-10 14:33:55 +0100529 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700530 Object* elms_obj;
531 { MaybeObject* maybe_elms_obj =
Ben Murdoch85b71792012-04-11 18:30:58 +0100532 EnsureJSArrayWithWritableFastElements(heap, receiver);
Steve Block44f0eee2011-05-26 01:26:41 +0100533 if (maybe_elms_obj == NULL)
534 return CallJsBuiltin(isolate, "ArrayShift", args);
John Reck59135872010-11-02 12:39:01 -0700535 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
536 }
Steve Block44f0eee2011-05-26 01:26:41 +0100537 if (!IsJSArrayFastElementMovingAllowed(heap, JSArray::cast(receiver))) {
538 return CallJsBuiltin(isolate, "ArrayShift", args);
Steve Block6ded16b2010-05-10 14:33:55 +0100539 }
Iain Merrick75681382010-08-19 15:07:18 +0100540 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100541 JSArray* array = JSArray::cast(receiver);
Ben Murdoch85b71792012-04-11 18:30:58 +0100542 ASSERT(array->HasFastElements());
Andrei Popescu402d9372010-02-26 13:31:12 +0000543
544 int len = Smi::cast(array->length())->value();
Steve Block44f0eee2011-05-26 01:26:41 +0100545 if (len == 0) return heap->undefined_value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000546
Andrei Popescu402d9372010-02-26 13:31:12 +0000547 // Get first element
548 Object* first = elms->get(0);
549 if (first->IsTheHole()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100550 first = heap->undefined_value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000551 }
552
Steve Block44f0eee2011-05-26 01:26:41 +0100553 if (!heap->lo_space()->Contains(elms)) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100554 // As elms still in the same space they used to be,
555 // there is no need to update region dirty mark.
556 array->set_elements(LeftTrimFixedArray(heap, elms, 1), SKIP_WRITE_BARRIER);
Steve Block6ded16b2010-05-10 14:33:55 +0100557 } else {
558 // Shift the elements.
559 AssertNoAllocation no_gc;
Steve Block44f0eee2011-05-26 01:26:41 +0100560 MoveElements(heap, &no_gc, elms, 0, elms, 1, len - 1);
561 elms->set(len - 1, heap->the_hole_value());
Andrei Popescu402d9372010-02-26 13:31:12 +0000562 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000563
564 // Set the length.
565 array->set_length(Smi::FromInt(len - 1));
566
567 return first;
568}
569
570
571BUILTIN(ArrayUnshift) {
Steve Block44f0eee2011-05-26 01:26:41 +0100572 Heap* heap = isolate->heap();
Steve Block6ded16b2010-05-10 14:33:55 +0100573 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700574 Object* elms_obj;
575 { MaybeObject* maybe_elms_obj =
Ben Murdoch85b71792012-04-11 18:30:58 +0100576 EnsureJSArrayWithWritableFastElements(heap, receiver);
Steve Block44f0eee2011-05-26 01:26:41 +0100577 if (maybe_elms_obj == NULL)
578 return CallJsBuiltin(isolate, "ArrayUnshift", args);
John Reck59135872010-11-02 12:39:01 -0700579 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
580 }
Steve Block44f0eee2011-05-26 01:26:41 +0100581 if (!IsJSArrayFastElementMovingAllowed(heap, JSArray::cast(receiver))) {
582 return CallJsBuiltin(isolate, "ArrayUnshift", args);
Steve Block6ded16b2010-05-10 14:33:55 +0100583 }
Iain Merrick75681382010-08-19 15:07:18 +0100584 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100585 JSArray* array = JSArray::cast(receiver);
Ben Murdoch85b71792012-04-11 18:30:58 +0100586 ASSERT(array->HasFastElements());
Andrei Popescu402d9372010-02-26 13:31:12 +0000587
588 int len = Smi::cast(array->length())->value();
589 int to_add = args.length() - 1;
Andrei Popescu402d9372010-02-26 13:31:12 +0000590 int new_length = len + to_add;
591 // Currently fixed arrays cannot grow too big, so
592 // we should never hit this case.
593 ASSERT(to_add <= (Smi::kMaxValue - len));
594
Andrei Popescu402d9372010-02-26 13:31:12 +0000595 if (new_length > elms->length()) {
596 // New backing storage is needed.
597 int capacity = new_length + (new_length >> 1) + 16;
John Reck59135872010-11-02 12:39:01 -0700598 Object* obj;
Steve Block44f0eee2011-05-26 01:26:41 +0100599 { MaybeObject* maybe_obj = heap->AllocateUninitializedFixedArray(capacity);
John Reck59135872010-11-02 12:39:01 -0700600 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
601 }
Steve Block6ded16b2010-05-10 14:33:55 +0100602 FixedArray* new_elms = FixedArray::cast(obj);
Ben Murdoch85b71792012-04-11 18:30:58 +0100603
604 AssertNoAllocation no_gc;
605 if (len > 0) {
606 CopyElements(heap, &no_gc, new_elms, to_add, elms, 0, len);
607 }
Steve Block44f0eee2011-05-26 01:26:41 +0100608 FillWithHoles(heap, new_elms, new_length, capacity);
Ben Murdoch85b71792012-04-11 18:30:58 +0100609
Andrei Popescu402d9372010-02-26 13:31:12 +0000610 elms = new_elms;
611 array->set_elements(elms);
612 } else {
613 AssertNoAllocation no_gc;
Steve Block44f0eee2011-05-26 01:26:41 +0100614 MoveElements(heap, &no_gc, elms, to_add, elms, 0, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000615 }
616
617 // Add the provided values.
618 AssertNoAllocation no_gc;
619 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
620 for (int i = 0; i < to_add; i++) {
621 elms->set(i, args[i + 1], mode);
622 }
623
624 // Set the length.
625 array->set_length(Smi::FromInt(new_length));
626 return Smi::FromInt(new_length);
627}
628
629
Andrei Popescu402d9372010-02-26 13:31:12 +0000630BUILTIN(ArraySlice) {
Steve Block44f0eee2011-05-26 01:26:41 +0100631 Heap* heap = isolate->heap();
Steve Block6ded16b2010-05-10 14:33:55 +0100632 Object* receiver = *args.receiver();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100633 FixedArray* elms;
634 int len = -1;
Steve Block9fac8402011-05-12 15:51:54 +0100635 if (receiver->IsJSArray()) {
636 JSArray* array = JSArray::cast(receiver);
Ben Murdoch85b71792012-04-11 18:30:58 +0100637 if (!array->HasFastElements() ||
Steve Block44f0eee2011-05-26 01:26:41 +0100638 !IsJSArrayFastElementMovingAllowed(heap, array)) {
639 return CallJsBuiltin(isolate, "ArraySlice", args);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100640 }
Steve Block9fac8402011-05-12 15:51:54 +0100641
642 elms = FixedArray::cast(array->elements());
643 len = Smi::cast(array->length())->value();
644 } else {
645 // Array.slice(arguments, ...) is quite a common idiom (notably more
646 // than 50% of invocations in Web apps). Treat it in C++ as well.
647 Map* arguments_map =
Steve Block44f0eee2011-05-26 01:26:41 +0100648 isolate->context()->global_context()->arguments_boilerplate()->map();
Steve Block9fac8402011-05-12 15:51:54 +0100649
650 bool is_arguments_object_with_fast_elements =
651 receiver->IsJSObject()
652 && JSObject::cast(receiver)->map() == arguments_map
Ben Murdoch85b71792012-04-11 18:30:58 +0100653 && JSObject::cast(receiver)->HasFastElements();
Steve Block9fac8402011-05-12 15:51:54 +0100654 if (!is_arguments_object_with_fast_elements) {
Steve Block44f0eee2011-05-26 01:26:41 +0100655 return CallJsBuiltin(isolate, "ArraySlice", args);
Steve Block9fac8402011-05-12 15:51:54 +0100656 }
657 elms = FixedArray::cast(JSObject::cast(receiver)->elements());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100658 Object* len_obj = JSObject::cast(receiver)
Steve Block44f0eee2011-05-26 01:26:41 +0100659 ->InObjectPropertyAt(Heap::kArgumentsLengthIndex);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100660 if (!len_obj->IsSmi()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100661 return CallJsBuiltin(isolate, "ArraySlice", args);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100662 }
663 len = Smi::cast(len_obj)->value();
664 if (len > elms->length()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100665 return CallJsBuiltin(isolate, "ArraySlice", args);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100666 }
667 for (int i = 0; i < len; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100668 if (elms->get(i) == heap->the_hole_value()) {
669 return CallJsBuiltin(isolate, "ArraySlice", args);
Steve Block9fac8402011-05-12 15:51:54 +0100670 }
671 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100672 }
673 ASSERT(len >= 0);
Andrei Popescu402d9372010-02-26 13:31:12 +0000674 int n_arguments = args.length() - 1;
675
676 // Note carefully choosen defaults---if argument is missing,
Steve Block6ded16b2010-05-10 14:33:55 +0100677 // it's undefined which gets converted to 0 for relative_start
678 // and to len for relative_end.
679 int relative_start = 0;
680 int relative_end = len;
Andrei Popescu402d9372010-02-26 13:31:12 +0000681 if (n_arguments > 0) {
682 Object* arg1 = args[1];
683 if (arg1->IsSmi()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100684 relative_start = Smi::cast(arg1)->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000685 } else if (!arg1->IsUndefined()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100686 return CallJsBuiltin(isolate, "ArraySlice", args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000687 }
688 if (n_arguments > 1) {
689 Object* arg2 = args[2];
690 if (arg2->IsSmi()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100691 relative_end = Smi::cast(arg2)->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000692 } else if (!arg2->IsUndefined()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100693 return CallJsBuiltin(isolate, "ArraySlice", args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000694 }
695 }
696 }
697
698 // ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 6.
Steve Block6ded16b2010-05-10 14:33:55 +0100699 int k = (relative_start < 0) ? Max(len + relative_start, 0)
700 : Min(relative_start, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000701
702 // ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 8.
Steve Block6ded16b2010-05-10 14:33:55 +0100703 int final = (relative_end < 0) ? Max(len + relative_end, 0)
704 : Min(relative_end, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000705
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100706 // Calculate the length of result array.
Ben Murdoch85b71792012-04-11 18:30:58 +0100707 int result_len = final - k;
708 if (result_len <= 0) {
709 return AllocateEmptyJSArray(heap);
710 }
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100711
Ben Murdoch85b71792012-04-11 18:30:58 +0100712 Object* result;
713 { MaybeObject* maybe_result = AllocateJSArray(heap);
714 if (!maybe_result->ToObject(&result)) return maybe_result;
715 }
716 JSArray* result_array = JSArray::cast(result);
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100717
Ben Murdoch85b71792012-04-11 18:30:58 +0100718 { MaybeObject* maybe_result =
719 heap->AllocateUninitializedFixedArray(result_len);
720 if (!maybe_result->ToObject(&result)) return maybe_result;
721 }
722 FixedArray* result_elms = FixedArray::cast(result);
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100723
Ben Murdoch85b71792012-04-11 18:30:58 +0100724 AssertNoAllocation no_gc;
725 CopyElements(heap, &no_gc, result_elms, 0, elms, k, result_len);
726
727 // Set elements.
728 result_array->set_elements(result_elms);
729
730 // Set the length.
731 result_array->set_length(Smi::FromInt(result_len));
Andrei Popescu402d9372010-02-26 13:31:12 +0000732 return result_array;
733}
734
735
736BUILTIN(ArraySplice) {
Steve Block44f0eee2011-05-26 01:26:41 +0100737 Heap* heap = isolate->heap();
Steve Block6ded16b2010-05-10 14:33:55 +0100738 Object* receiver = *args.receiver();
John Reck59135872010-11-02 12:39:01 -0700739 Object* elms_obj;
740 { MaybeObject* maybe_elms_obj =
Ben Murdoch85b71792012-04-11 18:30:58 +0100741 EnsureJSArrayWithWritableFastElements(heap, receiver);
Steve Block44f0eee2011-05-26 01:26:41 +0100742 if (maybe_elms_obj == NULL)
743 return CallJsBuiltin(isolate, "ArraySplice", args);
John Reck59135872010-11-02 12:39:01 -0700744 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
745 }
Steve Block44f0eee2011-05-26 01:26:41 +0100746 if (!IsJSArrayFastElementMovingAllowed(heap, JSArray::cast(receiver))) {
747 return CallJsBuiltin(isolate, "ArraySplice", args);
Steve Block6ded16b2010-05-10 14:33:55 +0100748 }
Iain Merrick75681382010-08-19 15:07:18 +0100749 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100750 JSArray* array = JSArray::cast(receiver);
Ben Murdoch85b71792012-04-11 18:30:58 +0100751 ASSERT(array->HasFastElements());
Andrei Popescu402d9372010-02-26 13:31:12 +0000752
753 int len = Smi::cast(array->length())->value();
754
755 int n_arguments = args.length() - 1;
756
Steve Block6ded16b2010-05-10 14:33:55 +0100757 int relative_start = 0;
Steve Block1e0659c2011-05-24 12:43:12 +0100758 if (n_arguments > 0) {
759 Object* arg1 = args[1];
760 if (arg1->IsSmi()) {
761 relative_start = Smi::cast(arg1)->value();
762 } else if (!arg1->IsUndefined()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100763 return CallJsBuiltin(isolate, "ArraySplice", args);
Steve Block1e0659c2011-05-24 12:43:12 +0100764 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000765 }
Steve Block6ded16b2010-05-10 14:33:55 +0100766 int actual_start = (relative_start < 0) ? Max(len + relative_start, 0)
767 : Min(relative_start, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000768
769 // SpiderMonkey, TraceMonkey and JSC treat the case where no delete count is
Steve Block1e0659c2011-05-24 12:43:12 +0100770 // given as a request to delete all the elements from the start.
771 // And it differs from the case of undefined delete count.
Andrei Popescu402d9372010-02-26 13:31:12 +0000772 // This does not follow ECMA-262, but we do the same for
773 // compatibility.
Steve Block1e0659c2011-05-24 12:43:12 +0100774 int actual_delete_count;
775 if (n_arguments == 1) {
776 ASSERT(len - actual_start >= 0);
777 actual_delete_count = len - actual_start;
778 } else {
779 int value = 0; // ToInteger(undefined) == 0
780 if (n_arguments > 1) {
781 Object* arg2 = args[2];
782 if (arg2->IsSmi()) {
783 value = Smi::cast(arg2)->value();
784 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100785 return CallJsBuiltin(isolate, "ArraySplice", args);
Steve Block1e0659c2011-05-24 12:43:12 +0100786 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000787 }
Steve Block1e0659c2011-05-24 12:43:12 +0100788 actual_delete_count = Min(Max(value, 0), len - actual_start);
Andrei Popescu402d9372010-02-26 13:31:12 +0000789 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000790
Steve Block6ded16b2010-05-10 14:33:55 +0100791 JSArray* result_array = NULL;
Ben Murdoch85b71792012-04-11 18:30:58 +0100792 if (actual_delete_count == 0) {
793 Object* result;
794 { MaybeObject* maybe_result = AllocateEmptyJSArray(heap);
795 if (!maybe_result->ToObject(&result)) return maybe_result;
796 }
797 result_array = JSArray::cast(result);
798 } else {
799 // Allocate result array.
800 Object* result;
801 { MaybeObject* maybe_result = AllocateJSArray(heap);
802 if (!maybe_result->ToObject(&result)) return maybe_result;
803 }
804 result_array = JSArray::cast(result);
Andrei Popescu402d9372010-02-26 13:31:12 +0000805
Ben Murdoch85b71792012-04-11 18:30:58 +0100806 { MaybeObject* maybe_result =
807 heap->AllocateUninitializedFixedArray(actual_delete_count);
808 if (!maybe_result->ToObject(&result)) return maybe_result;
809 }
810 FixedArray* result_elms = FixedArray::cast(result);
811
812 AssertNoAllocation no_gc;
Steve Block6ded16b2010-05-10 14:33:55 +0100813 // Fill newly created array.
Ben Murdoch85b71792012-04-11 18:30:58 +0100814 CopyElements(heap,
815 &no_gc,
816 result_elms, 0,
817 elms, actual_start,
818 actual_delete_count);
819
820 // Set elements.
821 result_array->set_elements(result_elms);
822
823 // Set the length.
824 result_array->set_length(Smi::FromInt(actual_delete_count));
Andrei Popescu402d9372010-02-26 13:31:12 +0000825 }
826
Steve Block6ded16b2010-05-10 14:33:55 +0100827 int item_count = (n_arguments > 1) ? (n_arguments - 2) : 0;
Ben Murdoch85b71792012-04-11 18:30:58 +0100828
Steve Block6ded16b2010-05-10 14:33:55 +0100829 int new_length = len - actual_delete_count + item_count;
Andrei Popescu402d9372010-02-26 13:31:12 +0000830
Steve Block6ded16b2010-05-10 14:33:55 +0100831 if (item_count < actual_delete_count) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000832 // Shrink the array.
Steve Block44f0eee2011-05-26 01:26:41 +0100833 const bool trim_array = !heap->lo_space()->Contains(elms) &&
Steve Block6ded16b2010-05-10 14:33:55 +0100834 ((actual_start + item_count) <
835 (len - actual_delete_count - actual_start));
836 if (trim_array) {
837 const int delta = actual_delete_count - item_count;
Andrei Popescu402d9372010-02-26 13:31:12 +0000838
Ben Murdoch85b71792012-04-11 18:30:58 +0100839 if (actual_start > 0) {
Steve Block053d10c2011-06-13 19:13:29 +0100840 AssertNoAllocation no_gc;
841 MoveElements(heap, &no_gc, elms, delta, elms, 0, actual_start);
Steve Block6ded16b2010-05-10 14:33:55 +0100842 }
843
Steve Block44f0eee2011-05-26 01:26:41 +0100844 elms = LeftTrimFixedArray(heap, elms, delta);
Ben Murdoch85b71792012-04-11 18:30:58 +0100845 array->set_elements(elms, SKIP_WRITE_BARRIER);
Steve Block6ded16b2010-05-10 14:33:55 +0100846 } else {
847 AssertNoAllocation no_gc;
Steve Block44f0eee2011-05-26 01:26:41 +0100848 MoveElements(heap, &no_gc,
Steve Block6ded16b2010-05-10 14:33:55 +0100849 elms, actual_start + item_count,
850 elms, actual_start + actual_delete_count,
851 (len - actual_delete_count - actual_start));
Steve Block44f0eee2011-05-26 01:26:41 +0100852 FillWithHoles(heap, elms, new_length, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000853 }
Steve Block6ded16b2010-05-10 14:33:55 +0100854 } else if (item_count > actual_delete_count) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000855 // Currently fixed arrays cannot grow too big, so
856 // we should never hit this case.
Steve Block6ded16b2010-05-10 14:33:55 +0100857 ASSERT((item_count - actual_delete_count) <= (Smi::kMaxValue - len));
Andrei Popescu402d9372010-02-26 13:31:12 +0000858
859 // Check if array need to grow.
860 if (new_length > elms->length()) {
861 // New backing storage is needed.
862 int capacity = new_length + (new_length >> 1) + 16;
John Reck59135872010-11-02 12:39:01 -0700863 Object* obj;
864 { MaybeObject* maybe_obj =
Steve Block44f0eee2011-05-26 01:26:41 +0100865 heap->AllocateUninitializedFixedArray(capacity);
John Reck59135872010-11-02 12:39:01 -0700866 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
867 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000868 FixedArray* new_elms = FixedArray::cast(obj);
Andrei Popescu402d9372010-02-26 13:31:12 +0000869
Ben Murdoch85b71792012-04-11 18:30:58 +0100870 AssertNoAllocation no_gc;
871 // Copy the part before actual_start as is.
872 if (actual_start > 0) {
873 CopyElements(heap, &no_gc, new_elms, 0, elms, 0, actual_start);
Andrei Popescu402d9372010-02-26 13:31:12 +0000874 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100875 const int to_copy = len - actual_delete_count - actual_start;
876 if (to_copy > 0) {
877 CopyElements(heap, &no_gc,
878 new_elms, actual_start + item_count,
879 elms, actual_start + actual_delete_count,
880 to_copy);
881 }
Steve Block44f0eee2011-05-26 01:26:41 +0100882 FillWithHoles(heap, new_elms, new_length, capacity);
Andrei Popescu402d9372010-02-26 13:31:12 +0000883
Andrei Popescu402d9372010-02-26 13:31:12 +0000884 elms = new_elms;
Ben Murdoch85b71792012-04-11 18:30:58 +0100885 array->set_elements(elms);
Steve Block6ded16b2010-05-10 14:33:55 +0100886 } else {
887 AssertNoAllocation no_gc;
Steve Block44f0eee2011-05-26 01:26:41 +0100888 MoveElements(heap, &no_gc,
Steve Block6ded16b2010-05-10 14:33:55 +0100889 elms, actual_start + item_count,
890 elms, actual_start + actual_delete_count,
891 (len - actual_delete_count - actual_start));
Andrei Popescu402d9372010-02-26 13:31:12 +0000892 }
893 }
894
Steve Block6ded16b2010-05-10 14:33:55 +0100895 AssertNoAllocation no_gc;
896 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
897 for (int k = actual_start; k < actual_start + item_count; k++) {
898 elms->set(k, args[3 + k - actual_start], mode);
Andrei Popescu402d9372010-02-26 13:31:12 +0000899 }
900
901 // Set the length.
902 array->set_length(Smi::FromInt(new_length));
903
904 return result_array;
905}
906
907
Steve Block6ded16b2010-05-10 14:33:55 +0100908BUILTIN(ArrayConcat) {
Steve Block44f0eee2011-05-26 01:26:41 +0100909 Heap* heap = isolate->heap();
910 Context* global_context = isolate->context()->global_context();
Kristian Monsen25f61362010-05-21 11:50:48 +0100911 JSObject* array_proto =
912 JSObject::cast(global_context->array_function()->prototype());
Steve Block44f0eee2011-05-26 01:26:41 +0100913 if (!ArrayPrototypeHasNoElements(heap, global_context, array_proto)) {
914 return CallJsBuiltin(isolate, "ArrayConcat", args);
Steve Block6ded16b2010-05-10 14:33:55 +0100915 }
916
917 // Iterate through all the arguments performing checks
918 // and calculating total length.
919 int n_arguments = args.length();
920 int result_len = 0;
921 for (int i = 0; i < n_arguments; i++) {
922 Object* arg = args[i];
Ben Murdoch85b71792012-04-11 18:30:58 +0100923 if (!arg->IsJSArray() || !JSArray::cast(arg)->HasFastElements()
Kristian Monsen25f61362010-05-21 11:50:48 +0100924 || JSArray::cast(arg)->GetPrototype() != array_proto) {
Steve Block44f0eee2011-05-26 01:26:41 +0100925 return CallJsBuiltin(isolate, "ArrayConcat", args);
Steve Block6ded16b2010-05-10 14:33:55 +0100926 }
927
928 int len = Smi::cast(JSArray::cast(arg)->length())->value();
929
930 // We shouldn't overflow when adding another len.
931 const int kHalfOfMaxInt = 1 << (kBitsPerInt - 2);
932 STATIC_ASSERT(FixedArray::kMaxLength < kHalfOfMaxInt);
933 USE(kHalfOfMaxInt);
934 result_len += len;
935 ASSERT(result_len >= 0);
936
937 if (result_len > FixedArray::kMaxLength) {
Steve Block44f0eee2011-05-26 01:26:41 +0100938 return CallJsBuiltin(isolate, "ArrayConcat", args);
Steve Block6ded16b2010-05-10 14:33:55 +0100939 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100940 }
Steve Block6ded16b2010-05-10 14:33:55 +0100941
Ben Murdoch85b71792012-04-11 18:30:58 +0100942 if (result_len == 0) {
943 return AllocateEmptyJSArray(heap);
Steve Block6ded16b2010-05-10 14:33:55 +0100944 }
945
946 // Allocate result.
Ben Murdoch85b71792012-04-11 18:30:58 +0100947 Object* result;
948 { MaybeObject* maybe_result = AllocateJSArray(heap);
949 if (!maybe_result->ToObject(&result)) return maybe_result;
950 }
951 JSArray* result_array = JSArray::cast(result);
952
953 { MaybeObject* maybe_result =
954 heap->AllocateUninitializedFixedArray(result_len);
955 if (!maybe_result->ToObject(&result)) return maybe_result;
956 }
957 FixedArray* result_elms = FixedArray::cast(result);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000958
Steve Block6ded16b2010-05-10 14:33:55 +0100959 // Copy data.
Ben Murdoch85b71792012-04-11 18:30:58 +0100960 AssertNoAllocation no_gc;
Steve Block6ded16b2010-05-10 14:33:55 +0100961 int start_pos = 0;
962 for (int i = 0; i < n_arguments; i++) {
963 JSArray* array = JSArray::cast(args[i]);
964 int len = Smi::cast(array->length())->value();
Ben Murdoch85b71792012-04-11 18:30:58 +0100965 if (len > 0) {
966 FixedArray* elms = FixedArray::cast(array->elements());
967 CopyElements(heap, &no_gc, result_elms, start_pos, elms, 0, len);
968 start_pos += len;
969 }
Steve Block6ded16b2010-05-10 14:33:55 +0100970 }
971 ASSERT(start_pos == result_len);
972
Ben Murdoch85b71792012-04-11 18:30:58 +0100973 // Set the length and elements.
974 result_array->set_length(Smi::FromInt(result_len));
975 result_array->set_elements(result_elms);
976
Steve Block6ded16b2010-05-10 14:33:55 +0100977 return result_array;
978}
979
980
Steve Blocka7e24c12009-10-30 11:49:00 +0000981// -----------------------------------------------------------------------------
Steve Block44f0eee2011-05-26 01:26:41 +0100982// Strict mode poison pills
983
984
Ben Murdoch257744e2011-11-30 15:57:28 +0000985BUILTIN(StrictModePoisonPill) {
Steve Block44f0eee2011-05-26 01:26:41 +0100986 HandleScope scope;
987 return isolate->Throw(*isolate->factory()->NewTypeError(
Ben Murdoch257744e2011-11-30 15:57:28 +0000988 "strict_poison_pill", HandleVector<Object>(NULL, 0)));
Steve Block44f0eee2011-05-26 01:26:41 +0100989}
990
Steve Block44f0eee2011-05-26 01:26:41 +0100991// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000992//
993
994
995// Returns the holder JSObject if the function can legally be called
996// with this receiver. Returns Heap::null_value() if the call is
997// illegal. Any arguments that don't fit the expected type is
998// overwritten with undefined. Arguments that do fit the expected
999// type is overwritten with the object in the prototype chain that
1000// actually has that type.
Steve Block44f0eee2011-05-26 01:26:41 +01001001static inline Object* TypeCheck(Heap* heap,
1002 int argc,
Steve Blocka7e24c12009-10-30 11:49:00 +00001003 Object** argv,
1004 FunctionTemplateInfo* info) {
1005 Object* recv = argv[0];
Ben Murdoch257744e2011-11-30 15:57:28 +00001006 // API calls are only supported with JSObject receivers.
1007 if (!recv->IsJSObject()) return heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00001008 Object* sig_obj = info->signature();
1009 if (sig_obj->IsUndefined()) return recv;
1010 SignatureInfo* sig = SignatureInfo::cast(sig_obj);
1011 // If necessary, check the receiver
1012 Object* recv_type = sig->receiver();
1013
1014 Object* holder = recv;
1015 if (!recv_type->IsUndefined()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001016 for (; holder != heap->null_value(); holder = holder->GetPrototype()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001017 if (holder->IsInstanceOf(FunctionTemplateInfo::cast(recv_type))) {
1018 break;
1019 }
1020 }
Steve Block44f0eee2011-05-26 01:26:41 +01001021 if (holder == heap->null_value()) return holder;
Steve Blocka7e24c12009-10-30 11:49:00 +00001022 }
1023 Object* args_obj = sig->args();
1024 // If there is no argument signature we're done
1025 if (args_obj->IsUndefined()) return holder;
1026 FixedArray* args = FixedArray::cast(args_obj);
1027 int length = args->length();
1028 if (argc <= length) length = argc - 1;
1029 for (int i = 0; i < length; i++) {
1030 Object* argtype = args->get(i);
1031 if (argtype->IsUndefined()) continue;
1032 Object** arg = &argv[-1 - i];
1033 Object* current = *arg;
Steve Block44f0eee2011-05-26 01:26:41 +01001034 for (; current != heap->null_value(); current = current->GetPrototype()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001035 if (current->IsInstanceOf(FunctionTemplateInfo::cast(argtype))) {
1036 *arg = current;
1037 break;
1038 }
1039 }
Steve Block44f0eee2011-05-26 01:26:41 +01001040 if (current == heap->null_value()) *arg = heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00001041 }
1042 return holder;
1043}
1044
1045
Leon Clarkee46be812010-01-19 14:06:41 +00001046template <bool is_construct>
John Reck59135872010-11-02 12:39:01 -07001047MUST_USE_RESULT static MaybeObject* HandleApiCallHelper(
Steve Block44f0eee2011-05-26 01:26:41 +01001048 BuiltinArguments<NEEDS_CALLED_FUNCTION> args, Isolate* isolate) {
1049 ASSERT(is_construct == CalledAsConstructor(isolate));
1050 Heap* heap = isolate->heap();
Steve Blocka7e24c12009-10-30 11:49:00 +00001051
Steve Block44f0eee2011-05-26 01:26:41 +01001052 HandleScope scope(isolate);
Leon Clarkee46be812010-01-19 14:06:41 +00001053 Handle<JSFunction> function = args.called_function();
Steve Block6ded16b2010-05-10 14:33:55 +01001054 ASSERT(function->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +00001055
Steve Block6ded16b2010-05-10 14:33:55 +01001056 FunctionTemplateInfo* fun_data = function->shared()->get_api_func_data();
Steve Blocka7e24c12009-10-30 11:49:00 +00001057 if (is_construct) {
Steve Block44f0eee2011-05-26 01:26:41 +01001058 Handle<FunctionTemplateInfo> desc(fun_data, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001059 bool pending_exception = false;
Steve Block44f0eee2011-05-26 01:26:41 +01001060 isolate->factory()->ConfigureInstance(
1061 desc, Handle<JSObject>::cast(args.receiver()), &pending_exception);
1062 ASSERT(isolate->has_pending_exception() == pending_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +00001063 if (pending_exception) return Failure::Exception();
Steve Block6ded16b2010-05-10 14:33:55 +01001064 fun_data = *desc;
Steve Blocka7e24c12009-10-30 11:49:00 +00001065 }
1066
Steve Block44f0eee2011-05-26 01:26:41 +01001067 Object* raw_holder = TypeCheck(heap, args.length(), &args[0], fun_data);
Steve Blocka7e24c12009-10-30 11:49:00 +00001068
1069 if (raw_holder->IsNull()) {
1070 // This function cannot be called with the given receiver. Abort!
1071 Handle<Object> obj =
Steve Block44f0eee2011-05-26 01:26:41 +01001072 isolate->factory()->NewTypeError(
1073 "illegal_invocation", HandleVector(&function, 1));
1074 return isolate->Throw(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001075 }
1076
1077 Object* raw_call_data = fun_data->call_code();
1078 if (!raw_call_data->IsUndefined()) {
1079 CallHandlerInfo* call_data = CallHandlerInfo::cast(raw_call_data);
1080 Object* callback_obj = call_data->callback();
1081 v8::InvocationCallback callback =
1082 v8::ToCData<v8::InvocationCallback>(callback_obj);
1083 Object* data_obj = call_data->data();
1084 Object* result;
1085
Steve Block44f0eee2011-05-26 01:26:41 +01001086 LOG(isolate, ApiObjectAccess("call", JSObject::cast(*args.receiver())));
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001087 ASSERT(raw_holder->IsJSObject());
1088
Steve Block44f0eee2011-05-26 01:26:41 +01001089 CustomArguments custom(isolate);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001090 v8::ImplementationUtilities::PrepareArgumentsData(custom.end(),
1091 data_obj, *function, raw_holder);
1092
Steve Blocka7e24c12009-10-30 11:49:00 +00001093 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments(
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001094 custom.end(),
1095 &args[0] - 1,
1096 args.length() - 1,
1097 is_construct);
Steve Blocka7e24c12009-10-30 11:49:00 +00001098
1099 v8::Handle<v8::Value> value;
1100 {
1101 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01001102 VMState state(isolate, EXTERNAL);
1103 ExternalCallbackScope call_scope(isolate,
1104 v8::ToCData<Address>(callback_obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00001105 value = callback(new_args);
1106 }
1107 if (value.IsEmpty()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001108 result = heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00001109 } else {
1110 result = *reinterpret_cast<Object**>(*value);
1111 }
1112
Steve Block44f0eee2011-05-26 01:26:41 +01001113 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001114 if (!is_construct || result->IsJSObject()) return result;
1115 }
1116
Leon Clarkee46be812010-01-19 14:06:41 +00001117 return *args.receiver();
Steve Blocka7e24c12009-10-30 11:49:00 +00001118}
Leon Clarkee46be812010-01-19 14:06:41 +00001119
1120
1121BUILTIN(HandleApiCall) {
Steve Block44f0eee2011-05-26 01:26:41 +01001122 return HandleApiCallHelper<false>(args, isolate);
Leon Clarkee46be812010-01-19 14:06:41 +00001123}
1124
1125
1126BUILTIN(HandleApiCallConstruct) {
Steve Block44f0eee2011-05-26 01:26:41 +01001127 return HandleApiCallHelper<true>(args, isolate);
Leon Clarkee46be812010-01-19 14:06:41 +00001128}
Steve Blocka7e24c12009-10-30 11:49:00 +00001129
1130
Andrei Popescu402d9372010-02-26 13:31:12 +00001131#ifdef DEBUG
1132
1133static void VerifyTypeCheck(Handle<JSObject> object,
1134 Handle<JSFunction> function) {
Steve Block6ded16b2010-05-10 14:33:55 +01001135 ASSERT(function->shared()->IsApiFunction());
1136 FunctionTemplateInfo* info = function->shared()->get_api_func_data();
Andrei Popescu402d9372010-02-26 13:31:12 +00001137 if (info->signature()->IsUndefined()) return;
1138 SignatureInfo* signature = SignatureInfo::cast(info->signature());
1139 Object* receiver_type = signature->receiver();
1140 if (receiver_type->IsUndefined()) return;
1141 FunctionTemplateInfo* type = FunctionTemplateInfo::cast(receiver_type);
1142 ASSERT(object->IsInstanceOf(type));
1143}
1144
1145#endif
1146
1147
1148BUILTIN(FastHandleApiCall) {
Steve Block44f0eee2011-05-26 01:26:41 +01001149 ASSERT(!CalledAsConstructor(isolate));
1150 Heap* heap = isolate->heap();
Andrei Popescu402d9372010-02-26 13:31:12 +00001151 const bool is_construct = false;
1152
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001153 // We expect four more arguments: callback, function, call data, and holder.
Andrei Popescu402d9372010-02-26 13:31:12 +00001154 const int args_length = args.length() - 4;
1155 ASSERT(args_length >= 0);
1156
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001157 Object* callback_obj = args[args_length];
Andrei Popescu402d9372010-02-26 13:31:12 +00001158
1159 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments(
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001160 &args[args_length + 1],
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001161 &args[0] - 1,
1162 args_length - 1,
1163 is_construct);
Andrei Popescu402d9372010-02-26 13:31:12 +00001164
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001165#ifdef DEBUG
1166 VerifyTypeCheck(Utils::OpenHandle(*new_args.Holder()),
1167 Utils::OpenHandle(*new_args.Callee()));
1168#endif
Steve Block44f0eee2011-05-26 01:26:41 +01001169 HandleScope scope(isolate);
Andrei Popescu402d9372010-02-26 13:31:12 +00001170 Object* result;
1171 v8::Handle<v8::Value> value;
1172 {
1173 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01001174 VMState state(isolate, EXTERNAL);
1175 ExternalCallbackScope call_scope(isolate,
1176 v8::ToCData<Address>(callback_obj));
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001177 v8::InvocationCallback callback =
1178 v8::ToCData<v8::InvocationCallback>(callback_obj);
1179
Andrei Popescu402d9372010-02-26 13:31:12 +00001180 value = callback(new_args);
1181 }
1182 if (value.IsEmpty()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001183 result = heap->undefined_value();
Andrei Popescu402d9372010-02-26 13:31:12 +00001184 } else {
1185 result = *reinterpret_cast<Object**>(*value);
1186 }
1187
Steve Block44f0eee2011-05-26 01:26:41 +01001188 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Andrei Popescu402d9372010-02-26 13:31:12 +00001189 return result;
1190}
1191
1192
Steve Blocka7e24c12009-10-30 11:49:00 +00001193// Helper function to handle calls to non-function objects created through the
1194// API. The object can be called as either a constructor (using new) or just as
1195// a function (without new).
John Reck59135872010-11-02 12:39:01 -07001196MUST_USE_RESULT static MaybeObject* HandleApiCallAsFunctionOrConstructor(
Steve Block44f0eee2011-05-26 01:26:41 +01001197 Isolate* isolate,
Leon Clarkee46be812010-01-19 14:06:41 +00001198 bool is_construct_call,
1199 BuiltinArguments<NO_EXTRA_ARGUMENTS> args) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001200 // Non-functions are never called as constructors. Even if this is an object
1201 // called as a constructor the delegate call is not a construct call.
Steve Block44f0eee2011-05-26 01:26:41 +01001202 ASSERT(!CalledAsConstructor(isolate));
1203 Heap* heap = isolate->heap();
Steve Blocka7e24c12009-10-30 11:49:00 +00001204
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001205 Handle<Object> receiver = args.receiver();
Steve Blocka7e24c12009-10-30 11:49:00 +00001206
1207 // Get the object called.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001208 JSObject* obj = JSObject::cast(*receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +00001209
1210 // Get the invocation callback from the function descriptor that was
1211 // used to create the called object.
1212 ASSERT(obj->map()->has_instance_call_handler());
1213 JSFunction* constructor = JSFunction::cast(obj->map()->constructor());
Steve Block6ded16b2010-05-10 14:33:55 +01001214 ASSERT(constructor->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +00001215 Object* handler =
Steve Block6ded16b2010-05-10 14:33:55 +01001216 constructor->shared()->get_api_func_data()->instance_call_handler();
Steve Blocka7e24c12009-10-30 11:49:00 +00001217 ASSERT(!handler->IsUndefined());
1218 CallHandlerInfo* call_data = CallHandlerInfo::cast(handler);
1219 Object* callback_obj = call_data->callback();
1220 v8::InvocationCallback callback =
1221 v8::ToCData<v8::InvocationCallback>(callback_obj);
1222
1223 // Get the data for the call and perform the callback.
Steve Blocka7e24c12009-10-30 11:49:00 +00001224 Object* result;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001225 {
Steve Block44f0eee2011-05-26 01:26:41 +01001226 HandleScope scope(isolate);
1227 LOG(isolate, ApiObjectAccess("call non-function", obj));
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001228
Steve Block44f0eee2011-05-26 01:26:41 +01001229 CustomArguments custom(isolate);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001230 v8::ImplementationUtilities::PrepareArgumentsData(custom.end(),
1231 call_data->data(), constructor, obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001232 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments(
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001233 custom.end(),
1234 &args[0] - 1,
1235 args.length() - 1,
1236 is_construct_call);
Steve Blocka7e24c12009-10-30 11:49:00 +00001237 v8::Handle<v8::Value> value;
1238 {
1239 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01001240 VMState state(isolate, EXTERNAL);
1241 ExternalCallbackScope call_scope(isolate,
1242 v8::ToCData<Address>(callback_obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00001243 value = callback(new_args);
1244 }
1245 if (value.IsEmpty()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001246 result = heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00001247 } else {
1248 result = *reinterpret_cast<Object**>(*value);
1249 }
1250 }
1251 // Check for exceptions and return result.
Steve Block44f0eee2011-05-26 01:26:41 +01001252 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001253 return result;
1254}
1255
1256
1257// Handle calls to non-function objects created through the API. This delegate
1258// function is used when the call is a normal function call.
1259BUILTIN(HandleApiCallAsFunction) {
Steve Block44f0eee2011-05-26 01:26:41 +01001260 return HandleApiCallAsFunctionOrConstructor(isolate, false, args);
Steve Blocka7e24c12009-10-30 11:49:00 +00001261}
Steve Blocka7e24c12009-10-30 11:49:00 +00001262
1263
1264// Handle calls to non-function objects created through the API. This delegate
1265// function is used when the call is a construct call.
1266BUILTIN(HandleApiCallAsConstructor) {
Steve Block44f0eee2011-05-26 01:26:41 +01001267 return HandleApiCallAsFunctionOrConstructor(isolate, true, args);
Steve Blocka7e24c12009-10-30 11:49:00 +00001268}
Steve Blocka7e24c12009-10-30 11:49:00 +00001269
1270
1271static void Generate_LoadIC_ArrayLength(MacroAssembler* masm) {
1272 LoadIC::GenerateArrayLength(masm);
1273}
1274
1275
1276static void Generate_LoadIC_StringLength(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01001277 LoadIC::GenerateStringLength(masm, false);
1278}
1279
1280
1281static void Generate_LoadIC_StringWrapperLength(MacroAssembler* masm) {
1282 LoadIC::GenerateStringLength(masm, true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001283}
1284
1285
1286static void Generate_LoadIC_FunctionPrototype(MacroAssembler* masm) {
1287 LoadIC::GenerateFunctionPrototype(masm);
1288}
1289
1290
1291static void Generate_LoadIC_Initialize(MacroAssembler* masm) {
1292 LoadIC::GenerateInitialize(masm);
1293}
1294
1295
1296static void Generate_LoadIC_PreMonomorphic(MacroAssembler* masm) {
1297 LoadIC::GeneratePreMonomorphic(masm);
1298}
1299
1300
1301static void Generate_LoadIC_Miss(MacroAssembler* masm) {
1302 LoadIC::GenerateMiss(masm);
1303}
1304
1305
1306static void Generate_LoadIC_Megamorphic(MacroAssembler* masm) {
1307 LoadIC::GenerateMegamorphic(masm);
1308}
1309
1310
1311static void Generate_LoadIC_Normal(MacroAssembler* masm) {
1312 LoadIC::GenerateNormal(masm);
1313}
1314
1315
1316static void Generate_KeyedLoadIC_Initialize(MacroAssembler* masm) {
1317 KeyedLoadIC::GenerateInitialize(masm);
1318}
1319
1320
Ben Murdoch257744e2011-11-30 15:57:28 +00001321static void Generate_KeyedLoadIC_Slow(MacroAssembler* masm) {
1322 KeyedLoadIC::GenerateRuntimeGetProperty(masm);
1323}
1324
1325
Steve Blocka7e24c12009-10-30 11:49:00 +00001326static void Generate_KeyedLoadIC_Miss(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001327 KeyedLoadIC::GenerateMiss(masm, false);
1328}
1329
1330
1331static void Generate_KeyedLoadIC_MissForceGeneric(MacroAssembler* masm) {
1332 KeyedLoadIC::GenerateMiss(masm, true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001333}
1334
1335
1336static void Generate_KeyedLoadIC_Generic(MacroAssembler* masm) {
1337 KeyedLoadIC::GenerateGeneric(masm);
1338}
1339
1340
Leon Clarkee46be812010-01-19 14:06:41 +00001341static void Generate_KeyedLoadIC_String(MacroAssembler* masm) {
1342 KeyedLoadIC::GenerateString(masm);
1343}
1344
1345
Steve Blocka7e24c12009-10-30 11:49:00 +00001346static void Generate_KeyedLoadIC_PreMonomorphic(MacroAssembler* masm) {
1347 KeyedLoadIC::GeneratePreMonomorphic(masm);
1348}
1349
Andrei Popescu402d9372010-02-26 13:31:12 +00001350static void Generate_KeyedLoadIC_IndexedInterceptor(MacroAssembler* masm) {
1351 KeyedLoadIC::GenerateIndexedInterceptor(masm);
1352}
1353
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001354static void Generate_KeyedLoadIC_NonStrictArguments(MacroAssembler* masm) {
1355 KeyedLoadIC::GenerateNonStrictArguments(masm);
1356}
Steve Blocka7e24c12009-10-30 11:49:00 +00001357
1358static void Generate_StoreIC_Initialize(MacroAssembler* masm) {
1359 StoreIC::GenerateInitialize(masm);
1360}
1361
1362
Steve Block1e0659c2011-05-24 12:43:12 +01001363static void Generate_StoreIC_Initialize_Strict(MacroAssembler* masm) {
1364 StoreIC::GenerateInitialize(masm);
1365}
1366
1367
Steve Blocka7e24c12009-10-30 11:49:00 +00001368static void Generate_StoreIC_Miss(MacroAssembler* masm) {
1369 StoreIC::GenerateMiss(masm);
1370}
1371
1372
Steve Block8defd9f2010-07-08 12:39:36 +01001373static void Generate_StoreIC_Normal(MacroAssembler* masm) {
1374 StoreIC::GenerateNormal(masm);
1375}
1376
1377
Steve Block1e0659c2011-05-24 12:43:12 +01001378static void Generate_StoreIC_Normal_Strict(MacroAssembler* masm) {
1379 StoreIC::GenerateNormal(masm);
1380}
1381
1382
Steve Blocka7e24c12009-10-30 11:49:00 +00001383static void Generate_StoreIC_Megamorphic(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001384 StoreIC::GenerateMegamorphic(masm, kNonStrictMode);
Steve Block1e0659c2011-05-24 12:43:12 +01001385}
1386
1387
1388static void Generate_StoreIC_Megamorphic_Strict(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001389 StoreIC::GenerateMegamorphic(masm, kStrictMode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001390}
1391
1392
Steve Block6ded16b2010-05-10 14:33:55 +01001393static void Generate_StoreIC_ArrayLength(MacroAssembler* masm) {
1394 StoreIC::GenerateArrayLength(masm);
1395}
1396
1397
Steve Block1e0659c2011-05-24 12:43:12 +01001398static void Generate_StoreIC_ArrayLength_Strict(MacroAssembler* masm) {
1399 StoreIC::GenerateArrayLength(masm);
1400}
1401
1402
Ben Murdochb0fe1622011-05-05 13:52:32 +01001403static void Generate_StoreIC_GlobalProxy(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001404 StoreIC::GenerateGlobalProxy(masm, kNonStrictMode);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001405}
1406
1407
Steve Block1e0659c2011-05-24 12:43:12 +01001408static void Generate_StoreIC_GlobalProxy_Strict(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001409 StoreIC::GenerateGlobalProxy(masm, kStrictMode);
Steve Block1e0659c2011-05-24 12:43:12 +01001410}
1411
1412
Steve Blocka7e24c12009-10-30 11:49:00 +00001413static void Generate_KeyedStoreIC_Generic(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001414 KeyedStoreIC::GenerateGeneric(masm, kNonStrictMode);
1415}
1416
1417
1418static void Generate_KeyedStoreIC_Generic_Strict(MacroAssembler* masm) {
1419 KeyedStoreIC::GenerateGeneric(masm, kStrictMode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001420}
1421
1422
Steve Blocka7e24c12009-10-30 11:49:00 +00001423static void Generate_KeyedStoreIC_Miss(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001424 KeyedStoreIC::GenerateMiss(masm, false);
1425}
1426
1427
1428static void Generate_KeyedStoreIC_MissForceGeneric(MacroAssembler* masm) {
1429 KeyedStoreIC::GenerateMiss(masm, true);
1430}
1431
1432
1433static void Generate_KeyedStoreIC_Slow(MacroAssembler* masm) {
1434 KeyedStoreIC::GenerateSlow(masm);
Steve Blocka7e24c12009-10-30 11:49:00 +00001435}
1436
1437
1438static void Generate_KeyedStoreIC_Initialize(MacroAssembler* masm) {
1439 KeyedStoreIC::GenerateInitialize(masm);
1440}
1441
1442
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001443static void Generate_KeyedStoreIC_Initialize_Strict(MacroAssembler* masm) {
1444 KeyedStoreIC::GenerateInitialize(masm);
1445}
1446
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001447static void Generate_KeyedStoreIC_NonStrictArguments(MacroAssembler* masm) {
1448 KeyedStoreIC::GenerateNonStrictArguments(masm);
1449}
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001450
Steve Blocka7e24c12009-10-30 11:49:00 +00001451#ifdef ENABLE_DEBUGGER_SUPPORT
1452static void Generate_LoadIC_DebugBreak(MacroAssembler* masm) {
1453 Debug::GenerateLoadICDebugBreak(masm);
1454}
1455
1456
1457static void Generate_StoreIC_DebugBreak(MacroAssembler* masm) {
1458 Debug::GenerateStoreICDebugBreak(masm);
1459}
1460
1461
1462static void Generate_KeyedLoadIC_DebugBreak(MacroAssembler* masm) {
1463 Debug::GenerateKeyedLoadICDebugBreak(masm);
1464}
1465
1466
1467static void Generate_KeyedStoreIC_DebugBreak(MacroAssembler* masm) {
1468 Debug::GenerateKeyedStoreICDebugBreak(masm);
1469}
1470
1471
Ben Murdoch85b71792012-04-11 18:30:58 +01001472static void Generate_ConstructCall_DebugBreak(MacroAssembler* masm) {
1473 Debug::GenerateConstructCallDebugBreak(masm);
1474}
1475
1476
Steve Blocka7e24c12009-10-30 11:49:00 +00001477static void Generate_Return_DebugBreak(MacroAssembler* masm) {
1478 Debug::GenerateReturnDebugBreak(masm);
1479}
1480
1481
Ben Murdoch85b71792012-04-11 18:30:58 +01001482static void Generate_StubNoRegisters_DebugBreak(MacroAssembler* masm) {
1483 Debug::GenerateStubNoRegistersDebugBreak(masm);
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001484}
1485
1486
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001487static void Generate_Slot_DebugBreak(MacroAssembler* masm) {
1488 Debug::GenerateSlotDebugBreak(masm);
1489}
1490
1491
Steve Block6ded16b2010-05-10 14:33:55 +01001492static void Generate_PlainReturn_LiveEdit(MacroAssembler* masm) {
1493 Debug::GeneratePlainReturnLiveEdit(masm);
1494}
1495
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001496
Steve Block6ded16b2010-05-10 14:33:55 +01001497static void Generate_FrameDropper_LiveEdit(MacroAssembler* masm) {
1498 Debug::GenerateFrameDropperLiveEdit(masm);
1499}
Steve Blocka7e24c12009-10-30 11:49:00 +00001500#endif
1501
Steve Block44f0eee2011-05-26 01:26:41 +01001502
1503Builtins::Builtins() : initialized_(false) {
1504 memset(builtins_, 0, sizeof(builtins_[0]) * builtin_count);
1505 memset(names_, 0, sizeof(names_[0]) * builtin_count);
1506}
1507
1508
1509Builtins::~Builtins() {
1510}
1511
Steve Blocka7e24c12009-10-30 11:49:00 +00001512
Leon Clarkee46be812010-01-19 14:06:41 +00001513#define DEF_ENUM_C(name, ignore) FUNCTION_ADDR(Builtin_##name),
Steve Block44f0eee2011-05-26 01:26:41 +01001514Address const Builtins::c_functions_[cfunction_count] = {
1515 BUILTIN_LIST_C(DEF_ENUM_C)
1516};
Steve Blocka7e24c12009-10-30 11:49:00 +00001517#undef DEF_ENUM_C
1518
1519#define DEF_JS_NAME(name, ignore) #name,
1520#define DEF_JS_ARGC(ignore, argc) argc,
Steve Block44f0eee2011-05-26 01:26:41 +01001521const char* const Builtins::javascript_names_[id_count] = {
Steve Blocka7e24c12009-10-30 11:49:00 +00001522 BUILTINS_LIST_JS(DEF_JS_NAME)
1523};
1524
Steve Block44f0eee2011-05-26 01:26:41 +01001525int const Builtins::javascript_argc_[id_count] = {
Steve Blocka7e24c12009-10-30 11:49:00 +00001526 BUILTINS_LIST_JS(DEF_JS_ARGC)
1527};
1528#undef DEF_JS_NAME
1529#undef DEF_JS_ARGC
1530
Steve Block44f0eee2011-05-26 01:26:41 +01001531struct BuiltinDesc {
1532 byte* generator;
1533 byte* c_code;
1534 const char* s_name; // name is only used for generating log information.
1535 int name;
1536 Code::Flags flags;
1537 BuiltinExtraArguments extra_args;
1538};
1539
1540class BuiltinFunctionTable {
1541 public:
Ben Murdoch85b71792012-04-11 18:30:58 +01001542 BuiltinFunctionTable() {
1543 Builtins::InitBuiltinFunctionTable();
Steve Block44f0eee2011-05-26 01:26:41 +01001544 }
1545
Ben Murdoch85b71792012-04-11 18:30:58 +01001546 static const BuiltinDesc* functions() { return functions_; }
1547
1548 private:
1549 static BuiltinDesc functions_[Builtins::builtin_count + 1];
Steve Block44f0eee2011-05-26 01:26:41 +01001550
1551 friend class Builtins;
1552};
1553
Ben Murdoch85b71792012-04-11 18:30:58 +01001554BuiltinDesc BuiltinFunctionTable::functions_[Builtins::builtin_count + 1];
1555
1556static const BuiltinFunctionTable builtin_function_table_init;
Steve Block44f0eee2011-05-26 01:26:41 +01001557
1558// Define array of pointers to generators and C builtin functions.
1559// We do this in a sort of roundabout way so that we can do the initialization
1560// within the lexical scope of Builtins:: and within a context where
1561// Code::Flags names a non-abstract type.
1562void Builtins::InitBuiltinFunctionTable() {
Ben Murdoch85b71792012-04-11 18:30:58 +01001563 BuiltinDesc* functions = BuiltinFunctionTable::functions_;
Steve Block44f0eee2011-05-26 01:26:41 +01001564 functions[builtin_count].generator = NULL;
1565 functions[builtin_count].c_code = NULL;
1566 functions[builtin_count].s_name = NULL;
1567 functions[builtin_count].name = builtin_count;
1568 functions[builtin_count].flags = static_cast<Code::Flags>(0);
1569 functions[builtin_count].extra_args = NO_EXTRA_ARGUMENTS;
1570
1571#define DEF_FUNCTION_PTR_C(aname, aextra_args) \
1572 functions->generator = FUNCTION_ADDR(Generate_Adaptor); \
1573 functions->c_code = FUNCTION_ADDR(Builtin_##aname); \
1574 functions->s_name = #aname; \
1575 functions->name = c_##aname; \
1576 functions->flags = Code::ComputeFlags(Code::BUILTIN); \
1577 functions->extra_args = aextra_args; \
1578 ++functions;
1579
1580#define DEF_FUNCTION_PTR_A(aname, kind, state, extra) \
1581 functions->generator = FUNCTION_ADDR(Generate_##aname); \
1582 functions->c_code = NULL; \
1583 functions->s_name = #aname; \
1584 functions->name = k##aname; \
1585 functions->flags = Code::ComputeFlags(Code::kind, \
Steve Block44f0eee2011-05-26 01:26:41 +01001586 state, \
1587 extra); \
1588 functions->extra_args = NO_EXTRA_ARGUMENTS; \
1589 ++functions;
1590
1591 BUILTIN_LIST_C(DEF_FUNCTION_PTR_C)
1592 BUILTIN_LIST_A(DEF_FUNCTION_PTR_A)
1593 BUILTIN_LIST_DEBUG_A(DEF_FUNCTION_PTR_A)
1594
1595#undef DEF_FUNCTION_PTR_C
1596#undef DEF_FUNCTION_PTR_A
1597}
1598
Ben Murdoch85b71792012-04-11 18:30:58 +01001599void Builtins::Setup(bool create_heap_objects) {
Steve Block44f0eee2011-05-26 01:26:41 +01001600 ASSERT(!initialized_);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001601 Isolate* isolate = Isolate::Current();
1602 Heap* heap = isolate->heap();
Steve Blocka7e24c12009-10-30 11:49:00 +00001603
1604 // Create a scope for the handles in the builtins.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001605 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001606
Ben Murdoch85b71792012-04-11 18:30:58 +01001607 const BuiltinDesc* functions = BuiltinFunctionTable::functions();
Steve Blocka7e24c12009-10-30 11:49:00 +00001608
1609 // For now we generate builtin adaptor code into a stack-allocated
Ben Murdoch85b71792012-04-11 18:30:58 +01001610 // buffer, before copying it into individual code objects.
1611 byte buffer[4*KB];
Steve Blocka7e24c12009-10-30 11:49:00 +00001612
1613 // Traverse the list of builtins and generate an adaptor in a
1614 // separate code object for each one.
1615 for (int i = 0; i < builtin_count; i++) {
1616 if (create_heap_objects) {
Ben Murdoch85b71792012-04-11 18:30:58 +01001617 MacroAssembler masm(isolate, buffer, sizeof buffer);
Steve Blocka7e24c12009-10-30 11:49:00 +00001618 // Generate the code/adaptor.
Leon Clarkee46be812010-01-19 14:06:41 +00001619 typedef void (*Generator)(MacroAssembler*, int, BuiltinExtraArguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00001620 Generator g = FUNCTION_CAST<Generator>(functions[i].generator);
1621 // We pass all arguments to the generator, but it may not use all of
1622 // them. This works because the first arguments are on top of the
1623 // stack.
Leon Clarkee46be812010-01-19 14:06:41 +00001624 g(&masm, functions[i].name, functions[i].extra_args);
Steve Blocka7e24c12009-10-30 11:49:00 +00001625 // Move the code into the object heap.
1626 CodeDesc desc;
1627 masm.GetCode(&desc);
1628 Code::Flags flags = functions[i].flags;
Ben Murdochb8e0da22011-05-16 14:20:40 +01001629 Object* code = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00001630 {
1631 // During startup it's OK to always allocate and defer GC to later.
1632 // This simplifies things because we don't need to retry.
1633 AlwaysAllocateScope __scope__;
John Reck59135872010-11-02 12:39:01 -07001634 { MaybeObject* maybe_code =
Steve Block44f0eee2011-05-26 01:26:41 +01001635 heap->CreateCode(desc, flags, masm.CodeObject());
John Reck59135872010-11-02 12:39:01 -07001636 if (!maybe_code->ToObject(&code)) {
1637 v8::internal::V8::FatalProcessOutOfMemory("CreateCode");
1638 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001639 }
1640 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001641 // Log the event and add the code to the builtins array.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001642 PROFILE(isolate,
Steve Block44f0eee2011-05-26 01:26:41 +01001643 CodeCreateEvent(Logger::BUILTIN_TAG,
Ben Murdochb8e0da22011-05-16 14:20:40 +01001644 Code::cast(code),
1645 functions[i].s_name));
1646 GDBJIT(AddCode(GDBJITInterface::BUILTIN,
1647 functions[i].s_name,
1648 Code::cast(code)));
Steve Blocka7e24c12009-10-30 11:49:00 +00001649 builtins_[i] = code;
1650#ifdef ENABLE_DISASSEMBLER
1651 if (FLAG_print_builtin_code) {
1652 PrintF("Builtin: %s\n", functions[i].s_name);
1653 Code::cast(code)->Disassemble(functions[i].s_name);
1654 PrintF("\n");
1655 }
1656#endif
1657 } else {
1658 // Deserializing. The values will be filled in during IterateBuiltins.
1659 builtins_[i] = NULL;
1660 }
1661 names_[i] = functions[i].s_name;
1662 }
1663
1664 // Mark as initialized.
Steve Block44f0eee2011-05-26 01:26:41 +01001665 initialized_ = true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001666}
1667
1668
1669void Builtins::TearDown() {
Steve Block44f0eee2011-05-26 01:26:41 +01001670 initialized_ = false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001671}
1672
1673
1674void Builtins::IterateBuiltins(ObjectVisitor* v) {
1675 v->VisitPointers(&builtins_[0], &builtins_[0] + builtin_count);
1676}
1677
1678
1679const char* Builtins::Lookup(byte* pc) {
Steve Block44f0eee2011-05-26 01:26:41 +01001680 // may be called during initialization (disassembler!)
1681 if (initialized_) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001682 for (int i = 0; i < builtin_count; i++) {
1683 Code* entry = Code::cast(builtins_[i]);
1684 if (entry->contains(pc)) {
1685 return names_[i];
1686 }
1687 }
1688 }
1689 return NULL;
1690}
1691
Ben Murdochb0fe1622011-05-05 13:52:32 +01001692
Steve Block44f0eee2011-05-26 01:26:41 +01001693#define DEFINE_BUILTIN_ACCESSOR_C(name, ignore) \
1694Handle<Code> Builtins::name() { \
1695 Code** code_address = \
1696 reinterpret_cast<Code**>(builtin_address(k##name)); \
1697 return Handle<Code>(code_address); \
1698}
1699#define DEFINE_BUILTIN_ACCESSOR_A(name, kind, state, extra) \
1700Handle<Code> Builtins::name() { \
1701 Code** code_address = \
1702 reinterpret_cast<Code**>(builtin_address(k##name)); \
1703 return Handle<Code>(code_address); \
1704}
1705BUILTIN_LIST_C(DEFINE_BUILTIN_ACCESSOR_C)
1706BUILTIN_LIST_A(DEFINE_BUILTIN_ACCESSOR_A)
1707BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A)
1708#undef DEFINE_BUILTIN_ACCESSOR_C
1709#undef DEFINE_BUILTIN_ACCESSOR_A
1710
1711
Steve Blocka7e24c12009-10-30 11:49:00 +00001712} } // namespace v8::internal