blob: a64bf4ecec8643c3964f08c7f2c42f72bb7bc0c9 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "api.h"
31#include "arguments.h"
32#include "bootstrapper.h"
33#include "builtins.h"
34#include "ic-inl.h"
35
36namespace v8 {
37namespace internal {
38
Leon Clarkee46be812010-01-19 14:06:41 +000039namespace {
40
41// Arguments object passed to C++ builtins.
42template <BuiltinExtraArguments extra_args>
43class BuiltinArguments : public Arguments {
44 public:
45 BuiltinArguments(int length, Object** arguments)
46 : Arguments(length, arguments) { }
47
48 Object*& operator[] (int index) {
49 ASSERT(index < length());
50 return Arguments::operator[](index);
51 }
52
53 template <class S> Handle<S> at(int index) {
54 ASSERT(index < length());
55 return Arguments::at<S>(index);
56 }
57
58 Handle<Object> receiver() {
59 return Arguments::at<Object>(0);
60 }
61
62 Handle<JSFunction> called_function() {
63 STATIC_ASSERT(extra_args == NEEDS_CALLED_FUNCTION);
64 return Arguments::at<JSFunction>(Arguments::length() - 1);
65 }
66
67 // Gets the total number of arguments including the receiver (but
68 // excluding extra arguments).
69 int length() const {
70 STATIC_ASSERT(extra_args == NO_EXTRA_ARGUMENTS);
71 return Arguments::length();
72 }
73
74#ifdef DEBUG
75 void Verify() {
76 // Check we have at least the receiver.
77 ASSERT(Arguments::length() >= 1);
78 }
79#endif
80};
81
82
83// Specialize BuiltinArguments for the called function extra argument.
84
85template <>
86int BuiltinArguments<NEEDS_CALLED_FUNCTION>::length() const {
87 return Arguments::length() - 1;
88}
89
90#ifdef DEBUG
91template <>
92void BuiltinArguments<NEEDS_CALLED_FUNCTION>::Verify() {
93 // Check we have at least the receiver and the called function.
94 ASSERT(Arguments::length() >= 2);
95 // Make sure cast to JSFunction succeeds.
96 called_function();
97}
98#endif
99
100
101#define DEF_ARG_TYPE(name, spec) \
102 typedef BuiltinArguments<spec> name##ArgumentsType;
103BUILTIN_LIST_C(DEF_ARG_TYPE)
104#undef DEF_ARG_TYPE
105
106} // namespace
107
108
Steve Blocka7e24c12009-10-30 11:49:00 +0000109// ----------------------------------------------------------------------------
Leon Clarkee46be812010-01-19 14:06:41 +0000110// Support macro for defining builtins in C++.
Steve Blocka7e24c12009-10-30 11:49:00 +0000111// ----------------------------------------------------------------------------
112//
113// A builtin function is defined by writing:
114//
115// BUILTIN(name) {
116// ...
117// }
Steve Blocka7e24c12009-10-30 11:49:00 +0000118//
Leon Clarkee46be812010-01-19 14:06:41 +0000119// In the body of the builtin function the arguments can be accessed
120// through the BuiltinArguments object args.
Steve Blocka7e24c12009-10-30 11:49:00 +0000121
Leon Clarkee46be812010-01-19 14:06:41 +0000122#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000123
Leon Clarkee46be812010-01-19 14:06:41 +0000124#define BUILTIN(name) \
125 static Object* Builtin_Impl_##name(name##ArgumentsType args); \
126 static Object* Builtin_##name(name##ArgumentsType args) { \
127 args.Verify(); \
128 return Builtin_Impl_##name(args); \
129 } \
130 static Object* Builtin_Impl_##name(name##ArgumentsType args)
Steve Blocka7e24c12009-10-30 11:49:00 +0000131
Leon Clarkee46be812010-01-19 14:06:41 +0000132#else // For release mode.
Steve Blocka7e24c12009-10-30 11:49:00 +0000133
Leon Clarkee46be812010-01-19 14:06:41 +0000134#define BUILTIN(name) \
135 static Object* Builtin_##name(name##ArgumentsType args)
136
137#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000138
139
140static inline bool CalledAsConstructor() {
141#ifdef DEBUG
142 // Calculate the result using a full stack frame iterator and check
143 // that the state of the stack is as we assume it to be in the
144 // code below.
145 StackFrameIterator it;
146 ASSERT(it.frame()->is_exit());
147 it.Advance();
148 StackFrame* frame = it.frame();
149 bool reference_result = frame->is_construct();
150#endif
151 Address fp = Top::c_entry_fp(Top::GetCurrentThread());
152 // Because we know fp points to an exit frame we can use the relevant
153 // part of ExitFrame::ComputeCallerState directly.
154 const int kCallerOffset = ExitFrameConstants::kCallerFPOffset;
155 Address caller_fp = Memory::Address_at(fp + kCallerOffset);
156 // This inlines the part of StackFrame::ComputeType that grabs the
157 // type of the current frame. Note that StackFrame::ComputeType
158 // has been specialized for each architecture so if any one of them
159 // changes this code has to be changed as well.
160 const int kMarkerOffset = StandardFrameConstants::kMarkerOffset;
161 const Smi* kConstructMarker = Smi::FromInt(StackFrame::CONSTRUCT);
162 Object* marker = Memory::Object_at(caller_fp + kMarkerOffset);
163 bool result = (marker == kConstructMarker);
164 ASSERT_EQ(result, reference_result);
165 return result;
166}
167
168// ----------------------------------------------------------------------------
169
170
Steve Blocka7e24c12009-10-30 11:49:00 +0000171BUILTIN(Illegal) {
172 UNREACHABLE();
Leon Clarkee46be812010-01-19 14:06:41 +0000173 return Heap::undefined_value(); // Make compiler happy.
Steve Blocka7e24c12009-10-30 11:49:00 +0000174}
Steve Blocka7e24c12009-10-30 11:49:00 +0000175
176
177BUILTIN(EmptyFunction) {
Leon Clarkee46be812010-01-19 14:06:41 +0000178 return Heap::undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000179}
Steve Blocka7e24c12009-10-30 11:49:00 +0000180
181
182BUILTIN(ArrayCodeGeneric) {
183 Counters::array_function_runtime.Increment();
184
185 JSArray* array;
186 if (CalledAsConstructor()) {
Leon Clarkee46be812010-01-19 14:06:41 +0000187 array = JSArray::cast(*args.receiver());
Steve Blocka7e24c12009-10-30 11:49:00 +0000188 } else {
189 // Allocate the JS Array
190 JSFunction* constructor =
191 Top::context()->global_context()->array_function();
192 Object* obj = Heap::AllocateJSObject(constructor);
193 if (obj->IsFailure()) return obj;
194 array = JSArray::cast(obj);
195 }
196
197 // 'array' now contains the JSArray we should initialize.
Steve Block8defd9f2010-07-08 12:39:36 +0100198 ASSERT(array->HasFastElements());
Steve Blocka7e24c12009-10-30 11:49:00 +0000199
200 // Optimize the case where there is one argument and the argument is a
201 // small smi.
202 if (args.length() == 2) {
203 Object* obj = args[1];
204 if (obj->IsSmi()) {
205 int len = Smi::cast(obj)->value();
206 if (len >= 0 && len < JSObject::kInitialMaxFastElementArray) {
207 Object* obj = Heap::AllocateFixedArrayWithHoles(len);
208 if (obj->IsFailure()) return obj;
209 array->SetContent(FixedArray::cast(obj));
210 return array;
211 }
212 }
213 // Take the argument as the length.
214 obj = array->Initialize(0);
215 if (obj->IsFailure()) return obj;
216 return array->SetElementsLength(args[1]);
217 }
218
219 // Optimize the case where there are no parameters passed.
220 if (args.length() == 1) {
221 return array->Initialize(JSArray::kPreallocatedArrayElements);
222 }
223
224 // Take the arguments as elements.
225 int number_of_elements = args.length() - 1;
226 Smi* len = Smi::FromInt(number_of_elements);
227 Object* obj = Heap::AllocateFixedArrayWithHoles(len->value());
228 if (obj->IsFailure()) return obj;
Leon Clarke4515c472010-02-03 11:58:03 +0000229
230 AssertNoAllocation no_gc;
Steve Blocka7e24c12009-10-30 11:49:00 +0000231 FixedArray* elms = FixedArray::cast(obj);
Leon Clarke4515c472010-02-03 11:58:03 +0000232 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000233 // Fill in the content
234 for (int index = 0; index < number_of_elements; index++) {
235 elms->set(index, args[index+1], mode);
236 }
237
238 // Set length and elements on the array.
239 array->set_elements(FixedArray::cast(obj));
Leon Clarke4515c472010-02-03 11:58:03 +0000240 array->set_length(len);
Steve Blocka7e24c12009-10-30 11:49:00 +0000241
242 return array;
243}
Steve Blocka7e24c12009-10-30 11:49:00 +0000244
245
Steve Block6ded16b2010-05-10 14:33:55 +0100246static Object* AllocateJSArray() {
247 JSFunction* array_function =
248 Top::context()->global_context()->array_function();
249 Object* result = Heap::AllocateJSObject(array_function);
250 if (result->IsFailure()) return result;
251 return result;
252}
253
254
255static Object* AllocateEmptyJSArray() {
256 Object* result = AllocateJSArray();
257 if (result->IsFailure()) return result;
258 JSArray* result_array = JSArray::cast(result);
259 result_array->set_length(Smi::FromInt(0));
260 result_array->set_elements(Heap::empty_fixed_array());
261 return result_array;
262}
263
264
265static void CopyElements(AssertNoAllocation* no_gc,
266 FixedArray* dst,
267 int dst_index,
268 FixedArray* src,
269 int src_index,
270 int len) {
271 ASSERT(dst != src); // Use MoveElements instead.
Iain Merrick75681382010-08-19 15:07:18 +0100272 ASSERT(dst->map() != Heap::fixed_cow_array_map());
Steve Block6ded16b2010-05-10 14:33:55 +0100273 ASSERT(len > 0);
274 CopyWords(dst->data_start() + dst_index,
275 src->data_start() + src_index,
276 len);
277 WriteBarrierMode mode = dst->GetWriteBarrierMode(*no_gc);
278 if (mode == UPDATE_WRITE_BARRIER) {
279 Heap::RecordWrites(dst->address(), dst->OffsetOfElementAt(dst_index), len);
280 }
281}
282
283
284static void MoveElements(AssertNoAllocation* no_gc,
285 FixedArray* dst,
286 int dst_index,
287 FixedArray* src,
288 int src_index,
289 int len) {
Iain Merrick75681382010-08-19 15:07:18 +0100290 ASSERT(dst->map() != Heap::fixed_cow_array_map());
Steve Block6ded16b2010-05-10 14:33:55 +0100291 memmove(dst->data_start() + dst_index,
292 src->data_start() + src_index,
293 len * kPointerSize);
294 WriteBarrierMode mode = dst->GetWriteBarrierMode(*no_gc);
295 if (mode == UPDATE_WRITE_BARRIER) {
296 Heap::RecordWrites(dst->address(), dst->OffsetOfElementAt(dst_index), len);
297 }
298}
299
300
301static void FillWithHoles(FixedArray* dst, int from, int to) {
Iain Merrick75681382010-08-19 15:07:18 +0100302 ASSERT(dst->map() != Heap::fixed_cow_array_map());
Steve Block6ded16b2010-05-10 14:33:55 +0100303 MemsetPointer(dst->data_start() + from, Heap::the_hole_value(), to - from);
304}
305
306
307static FixedArray* LeftTrimFixedArray(FixedArray* elms, int to_trim) {
Iain Merrick75681382010-08-19 15:07:18 +0100308 ASSERT(elms->map() != Heap::fixed_cow_array_map());
Steve Block791712a2010-08-27 10:21:07 +0100309 // For now this trick is only applied to fixed arrays in new and paged space.
Steve Block6ded16b2010-05-10 14:33:55 +0100310 // In large object space the object's start must coincide with chunk
311 // and thus the trick is just not applicable.
Steve Block791712a2010-08-27 10:21:07 +0100312 ASSERT(!Heap::lo_space()->Contains(elms));
Steve Block6ded16b2010-05-10 14:33:55 +0100313
314 STATIC_ASSERT(FixedArray::kMapOffset == 0);
315 STATIC_ASSERT(FixedArray::kLengthOffset == kPointerSize);
316 STATIC_ASSERT(FixedArray::kHeaderSize == 2 * kPointerSize);
317
318 Object** former_start = HeapObject::RawField(elms, 0);
319
320 const int len = elms->length();
321
Steve Block791712a2010-08-27 10:21:07 +0100322 if (to_trim > FixedArray::kHeaderSize / kPointerSize &&
323 !Heap::new_space()->Contains(elms)) {
324 // If we are doing a big trim in old space then we zap the space that was
325 // formerly part of the array so that the GC (aided by the card-based
326 // remembered set) won't find pointers to new-space there.
327 Object** zap = reinterpret_cast<Object**>(elms->address());
328 zap++; // Header of filler must be at least one word so skip that.
329 for (int i = 1; i < to_trim; i++) {
330 *zap++ = Smi::FromInt(0);
331 }
332 }
Steve Block6ded16b2010-05-10 14:33:55 +0100333 // Technically in new space this write might be omitted (except for
334 // debug mode which iterates through the heap), but to play safer
335 // we still do it.
336 Heap::CreateFillerObjectAt(elms->address(), to_trim * kPointerSize);
337
338 former_start[to_trim] = Heap::fixed_array_map();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100339 former_start[to_trim + 1] = Smi::FromInt(len - to_trim);
Steve Block6ded16b2010-05-10 14:33:55 +0100340
Steve Block791712a2010-08-27 10:21:07 +0100341 return FixedArray::cast(HeapObject::FromAddress(
342 elms->address() + to_trim * kPointerSize));
Steve Block6ded16b2010-05-10 14:33:55 +0100343}
344
345
Kristian Monsen25f61362010-05-21 11:50:48 +0100346static bool ArrayPrototypeHasNoElements(Context* global_context,
347 JSObject* array_proto) {
Steve Block6ded16b2010-05-10 14:33:55 +0100348 // This method depends on non writability of Object and Array prototype
349 // fields.
Kristian Monsen25f61362010-05-21 11:50:48 +0100350 if (array_proto->elements() != Heap::empty_fixed_array()) return false;
Steve Block6ded16b2010-05-10 14:33:55 +0100351 // Hidden prototype
Kristian Monsen25f61362010-05-21 11:50:48 +0100352 array_proto = JSObject::cast(array_proto->GetPrototype());
353 ASSERT(array_proto->elements() == Heap::empty_fixed_array());
Steve Block6ded16b2010-05-10 14:33:55 +0100354 // Object.prototype
Kristian Monsen25f61362010-05-21 11:50:48 +0100355 array_proto = JSObject::cast(array_proto->GetPrototype());
356 if (array_proto != global_context->initial_object_prototype()) return false;
357 if (array_proto->elements() != Heap::empty_fixed_array()) return false;
358 ASSERT(array_proto->GetPrototype()->IsNull());
Steve Block6ded16b2010-05-10 14:33:55 +0100359 return true;
360}
361
362
Iain Merrick75681382010-08-19 15:07:18 +0100363static inline Object* EnsureJSArrayWithWritableFastElements(Object* receiver) {
364 if (!receiver->IsJSArray()) return NULL;
Steve Block6ded16b2010-05-10 14:33:55 +0100365 JSArray* array = JSArray::cast(receiver);
Steve Block6ded16b2010-05-10 14:33:55 +0100366 HeapObject* elms = HeapObject::cast(array->elements());
Iain Merrick75681382010-08-19 15:07:18 +0100367 if (elms->map() == Heap::fixed_array_map()) return elms;
368 if (elms->map() == Heap::fixed_cow_array_map()) {
369 return array->EnsureWritableFastElements();
Steve Block6ded16b2010-05-10 14:33:55 +0100370 }
Iain Merrick75681382010-08-19 15:07:18 +0100371 return NULL;
Steve Block6ded16b2010-05-10 14:33:55 +0100372}
373
374
Iain Merrick75681382010-08-19 15:07:18 +0100375static inline bool IsJSArrayFastElementMovingAllowed(JSArray* receiver) {
Kristian Monsen25f61362010-05-21 11:50:48 +0100376 Context* global_context = Top::context()->global_context();
377 JSObject* array_proto =
378 JSObject::cast(global_context->array_function()->prototype());
Iain Merrick75681382010-08-19 15:07:18 +0100379 return receiver->GetPrototype() == array_proto &&
380 ArrayPrototypeHasNoElements(global_context, array_proto);
Kristian Monsen25f61362010-05-21 11:50:48 +0100381}
382
383
Steve Block6ded16b2010-05-10 14:33:55 +0100384static Object* CallJsBuiltin(const char* name,
385 BuiltinArguments<NO_EXTRA_ARGUMENTS> args) {
386 HandleScope handleScope;
387
388 Handle<Object> js_builtin =
389 GetProperty(Handle<JSObject>(Top::global_context()->builtins()),
390 name);
391 ASSERT(js_builtin->IsJSFunction());
392 Handle<JSFunction> function(Handle<JSFunction>::cast(js_builtin));
Kristian Monsen25f61362010-05-21 11:50:48 +0100393 ScopedVector<Object**> argv(args.length() - 1);
Steve Block6ded16b2010-05-10 14:33:55 +0100394 int n_args = args.length() - 1;
395 for (int i = 0; i < n_args; i++) {
396 argv[i] = args.at<Object>(i + 1).location();
397 }
398 bool pending_exception = false;
399 Handle<Object> result = Execution::Call(function,
400 args.receiver(),
401 n_args,
402 argv.start(),
403 &pending_exception);
Steve Block6ded16b2010-05-10 14:33:55 +0100404 if (pending_exception) return Failure::Exception();
405 return *result;
406}
407
408
Steve Blocka7e24c12009-10-30 11:49:00 +0000409BUILTIN(ArrayPush) {
Steve Block6ded16b2010-05-10 14:33:55 +0100410 Object* receiver = *args.receiver();
Iain Merrick75681382010-08-19 15:07:18 +0100411 Object* elms_obj = EnsureJSArrayWithWritableFastElements(receiver);
412 if (elms_obj == NULL) return CallJsBuiltin("ArrayPush", args);
413 if (elms_obj->IsFailure()) return elms_obj;
414 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100415 JSArray* array = JSArray::cast(receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +0000416
Steve Blocka7e24c12009-10-30 11:49:00 +0000417 int len = Smi::cast(array->length())->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000418 int to_add = args.length() - 1;
419 if (to_add == 0) {
420 return Smi::FromInt(len);
421 }
422 // Currently fixed arrays cannot grow too big, so
423 // we should never hit this case.
424 ASSERT(to_add <= (Smi::kMaxValue - len));
Steve Blocka7e24c12009-10-30 11:49:00 +0000425
Andrei Popescu402d9372010-02-26 13:31:12 +0000426 int new_length = len + to_add;
Steve Blocka7e24c12009-10-30 11:49:00 +0000427
Andrei Popescu402d9372010-02-26 13:31:12 +0000428 if (new_length > elms->length()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000429 // New backing storage is needed.
430 int capacity = new_length + (new_length >> 1) + 16;
Steve Block6ded16b2010-05-10 14:33:55 +0100431 Object* obj = Heap::AllocateUninitializedFixedArray(capacity);
Steve Blocka7e24c12009-10-30 11:49:00 +0000432 if (obj->IsFailure()) return obj;
Steve Block6ded16b2010-05-10 14:33:55 +0100433 FixedArray* new_elms = FixedArray::cast(obj);
Leon Clarke4515c472010-02-03 11:58:03 +0000434
435 AssertNoAllocation no_gc;
Steve Block6ded16b2010-05-10 14:33:55 +0100436 if (len > 0) {
437 CopyElements(&no_gc, new_elms, 0, elms, 0, len);
438 }
439 FillWithHoles(new_elms, new_length, capacity);
440
Andrei Popescu402d9372010-02-26 13:31:12 +0000441 elms = new_elms;
442 array->set_elements(elms);
Steve Blocka7e24c12009-10-30 11:49:00 +0000443 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000444
Steve Block6ded16b2010-05-10 14:33:55 +0100445 // Add the provided values.
Andrei Popescu402d9372010-02-26 13:31:12 +0000446 AssertNoAllocation no_gc;
447 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
Andrei Popescu402d9372010-02-26 13:31:12 +0000448 for (int index = 0; index < to_add; index++) {
449 elms->set(index + len, args[index + 1], mode);
450 }
451
Steve Blocka7e24c12009-10-30 11:49:00 +0000452 // Set the length.
Leon Clarke4515c472010-02-03 11:58:03 +0000453 array->set_length(Smi::FromInt(new_length));
Andrei Popescu402d9372010-02-26 13:31:12 +0000454 return Smi::FromInt(new_length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000455}
Steve Blocka7e24c12009-10-30 11:49:00 +0000456
457
458BUILTIN(ArrayPop) {
Steve Block6ded16b2010-05-10 14:33:55 +0100459 Object* receiver = *args.receiver();
Iain Merrick75681382010-08-19 15:07:18 +0100460 Object* elms_obj = EnsureJSArrayWithWritableFastElements(receiver);
461 if (elms_obj == NULL) return CallJsBuiltin("ArrayPop", args);
462 if (elms_obj->IsFailure()) return elms_obj;
463 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100464 JSArray* array = JSArray::cast(receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +0000465
466 int len = Smi::cast(array->length())->value();
Steve Block6ded16b2010-05-10 14:33:55 +0100467 if (len == 0) return Heap::undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000468
469 // Get top element
Steve Blocka7e24c12009-10-30 11:49:00 +0000470 Object* top = elms->get(len - 1);
471
472 // Set the length.
Leon Clarke4515c472010-02-03 11:58:03 +0000473 array->set_length(Smi::FromInt(len - 1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000474
475 if (!top->IsTheHole()) {
476 // Delete the top element.
477 elms->set_the_hole(len - 1);
478 return top;
479 }
480
Kristian Monsen25f61362010-05-21 11:50:48 +0100481 top = array->GetPrototype()->GetElement(len - 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000482
483 return top;
484}
Steve Blocka7e24c12009-10-30 11:49:00 +0000485
486
Andrei Popescu402d9372010-02-26 13:31:12 +0000487BUILTIN(ArrayShift) {
Steve Block6ded16b2010-05-10 14:33:55 +0100488 Object* receiver = *args.receiver();
Iain Merrick75681382010-08-19 15:07:18 +0100489 Object* elms_obj = EnsureJSArrayWithWritableFastElements(receiver);
490 if (elms_obj->IsFailure()) return elms_obj;
491 if (elms_obj == NULL ||
492 !IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) {
Steve Block6ded16b2010-05-10 14:33:55 +0100493 return CallJsBuiltin("ArrayShift", args);
494 }
Iain Merrick75681382010-08-19 15:07:18 +0100495 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100496 JSArray* array = JSArray::cast(receiver);
Andrei Popescu402d9372010-02-26 13:31:12 +0000497 ASSERT(array->HasFastElements());
498
499 int len = Smi::cast(array->length())->value();
500 if (len == 0) return Heap::undefined_value();
501
Andrei Popescu402d9372010-02-26 13:31:12 +0000502 // Get first element
503 Object* first = elms->get(0);
504 if (first->IsTheHole()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100505 first = Heap::undefined_value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000506 }
507
Steve Block791712a2010-08-27 10:21:07 +0100508 if (!Heap::lo_space()->Contains(elms)) {
509 // As elms still in the same space they used to be,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100510 // there is no need to update region dirty mark.
Steve Block6ded16b2010-05-10 14:33:55 +0100511 array->set_elements(LeftTrimFixedArray(elms, 1), SKIP_WRITE_BARRIER);
512 } else {
513 // Shift the elements.
514 AssertNoAllocation no_gc;
515 MoveElements(&no_gc, elms, 0, elms, 1, len - 1);
516 elms->set(len - 1, Heap::the_hole_value());
Andrei Popescu402d9372010-02-26 13:31:12 +0000517 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000518
519 // Set the length.
520 array->set_length(Smi::FromInt(len - 1));
521
522 return first;
523}
524
525
526BUILTIN(ArrayUnshift) {
Steve Block6ded16b2010-05-10 14:33:55 +0100527 Object* receiver = *args.receiver();
Iain Merrick75681382010-08-19 15:07:18 +0100528 Object* elms_obj = EnsureJSArrayWithWritableFastElements(receiver);
529 if (elms_obj->IsFailure()) return elms_obj;
530 if (elms_obj == NULL ||
531 !IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) {
Steve Block6ded16b2010-05-10 14:33:55 +0100532 return CallJsBuiltin("ArrayUnshift", args);
533 }
Iain Merrick75681382010-08-19 15:07:18 +0100534 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100535 JSArray* array = JSArray::cast(receiver);
Andrei Popescu402d9372010-02-26 13:31:12 +0000536 ASSERT(array->HasFastElements());
537
538 int len = Smi::cast(array->length())->value();
539 int to_add = args.length() - 1;
Andrei Popescu402d9372010-02-26 13:31:12 +0000540 int new_length = len + to_add;
541 // Currently fixed arrays cannot grow too big, so
542 // we should never hit this case.
543 ASSERT(to_add <= (Smi::kMaxValue - len));
544
Andrei Popescu402d9372010-02-26 13:31:12 +0000545 if (new_length > elms->length()) {
546 // New backing storage is needed.
547 int capacity = new_length + (new_length >> 1) + 16;
Steve Block6ded16b2010-05-10 14:33:55 +0100548 Object* obj = Heap::AllocateUninitializedFixedArray(capacity);
Andrei Popescu402d9372010-02-26 13:31:12 +0000549 if (obj->IsFailure()) return obj;
Steve Block6ded16b2010-05-10 14:33:55 +0100550 FixedArray* new_elms = FixedArray::cast(obj);
Andrei Popescu402d9372010-02-26 13:31:12 +0000551
552 AssertNoAllocation no_gc;
Steve Block6ded16b2010-05-10 14:33:55 +0100553 if (len > 0) {
554 CopyElements(&no_gc, new_elms, to_add, elms, 0, len);
555 }
556 FillWithHoles(new_elms, new_length, capacity);
Andrei Popescu402d9372010-02-26 13:31:12 +0000557
558 elms = new_elms;
559 array->set_elements(elms);
560 } else {
561 AssertNoAllocation no_gc;
Steve Block6ded16b2010-05-10 14:33:55 +0100562 MoveElements(&no_gc, elms, to_add, elms, 0, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000563 }
564
565 // Add the provided values.
566 AssertNoAllocation no_gc;
567 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
568 for (int i = 0; i < to_add; i++) {
569 elms->set(i, args[i + 1], mode);
570 }
571
572 // Set the length.
573 array->set_length(Smi::FromInt(new_length));
574 return Smi::FromInt(new_length);
575}
576
577
Andrei Popescu402d9372010-02-26 13:31:12 +0000578BUILTIN(ArraySlice) {
Steve Block6ded16b2010-05-10 14:33:55 +0100579 Object* receiver = *args.receiver();
Iain Merrick75681382010-08-19 15:07:18 +0100580 Object* elms_obj = EnsureJSArrayWithWritableFastElements(receiver);
581 if (elms_obj->IsFailure()) return elms_obj;
582 if (elms_obj == NULL ||
583 !IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) {
Steve Block6ded16b2010-05-10 14:33:55 +0100584 return CallJsBuiltin("ArraySlice", args);
585 }
Iain Merrick75681382010-08-19 15:07:18 +0100586 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100587 JSArray* array = JSArray::cast(receiver);
Andrei Popescu402d9372010-02-26 13:31:12 +0000588 ASSERT(array->HasFastElements());
589
590 int len = Smi::cast(array->length())->value();
591
592 int n_arguments = args.length() - 1;
593
594 // Note carefully choosen defaults---if argument is missing,
Steve Block6ded16b2010-05-10 14:33:55 +0100595 // it's undefined which gets converted to 0 for relative_start
596 // and to len for relative_end.
597 int relative_start = 0;
598 int relative_end = len;
Andrei Popescu402d9372010-02-26 13:31:12 +0000599 if (n_arguments > 0) {
600 Object* arg1 = args[1];
601 if (arg1->IsSmi()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100602 relative_start = Smi::cast(arg1)->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000603 } else if (!arg1->IsUndefined()) {
604 return CallJsBuiltin("ArraySlice", args);
605 }
606 if (n_arguments > 1) {
607 Object* arg2 = args[2];
608 if (arg2->IsSmi()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100609 relative_end = Smi::cast(arg2)->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000610 } else if (!arg2->IsUndefined()) {
611 return CallJsBuiltin("ArraySlice", args);
612 }
613 }
614 }
615
616 // ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 6.
Steve Block6ded16b2010-05-10 14:33:55 +0100617 int k = (relative_start < 0) ? Max(len + relative_start, 0)
618 : Min(relative_start, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000619
620 // ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 8.
Steve Block6ded16b2010-05-10 14:33:55 +0100621 int final = (relative_end < 0) ? Max(len + relative_end, 0)
622 : Min(relative_end, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000623
624 // Calculate the length of result array.
625 int result_len = final - k;
Steve Block6ded16b2010-05-10 14:33:55 +0100626 if (result_len <= 0) {
627 return AllocateEmptyJSArray();
Andrei Popescu402d9372010-02-26 13:31:12 +0000628 }
629
Steve Block6ded16b2010-05-10 14:33:55 +0100630 Object* result = AllocateJSArray();
Andrei Popescu402d9372010-02-26 13:31:12 +0000631 if (result->IsFailure()) return result;
632 JSArray* result_array = JSArray::cast(result);
633
Steve Block6ded16b2010-05-10 14:33:55 +0100634 result = Heap::AllocateUninitializedFixedArray(result_len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000635 if (result->IsFailure()) return result;
636 FixedArray* result_elms = FixedArray::cast(result);
637
Andrei Popescu402d9372010-02-26 13:31:12 +0000638 AssertNoAllocation no_gc;
Steve Block6ded16b2010-05-10 14:33:55 +0100639 CopyElements(&no_gc, result_elms, 0, elms, k, result_len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000640
641 // Set elements.
642 result_array->set_elements(result_elms);
643
644 // Set the length.
645 result_array->set_length(Smi::FromInt(result_len));
646 return result_array;
647}
648
649
650BUILTIN(ArraySplice) {
Steve Block6ded16b2010-05-10 14:33:55 +0100651 Object* receiver = *args.receiver();
Iain Merrick75681382010-08-19 15:07:18 +0100652 Object* elms_obj = EnsureJSArrayWithWritableFastElements(receiver);
653 if (elms_obj->IsFailure()) return elms_obj;
654 if (elms_obj == NULL ||
655 !IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) {
Steve Block6ded16b2010-05-10 14:33:55 +0100656 return CallJsBuiltin("ArraySplice", args);
657 }
Iain Merrick75681382010-08-19 15:07:18 +0100658 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100659 JSArray* array = JSArray::cast(receiver);
Andrei Popescu402d9372010-02-26 13:31:12 +0000660 ASSERT(array->HasFastElements());
661
662 int len = Smi::cast(array->length())->value();
663
664 int n_arguments = args.length() - 1;
665
666 // SpiderMonkey and JSC return undefined in the case where no
667 // arguments are given instead of using the implicit undefined
668 // arguments. This does not follow ECMA-262, but we do the same for
669 // compatibility.
670 // TraceMonkey follows ECMA-262 though.
671 if (n_arguments == 0) {
672 return Heap::undefined_value();
673 }
674
Steve Block6ded16b2010-05-10 14:33:55 +0100675 int relative_start = 0;
Andrei Popescu402d9372010-02-26 13:31:12 +0000676 Object* arg1 = args[1];
677 if (arg1->IsSmi()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100678 relative_start = Smi::cast(arg1)->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000679 } else if (!arg1->IsUndefined()) {
680 return CallJsBuiltin("ArraySplice", args);
681 }
Steve Block6ded16b2010-05-10 14:33:55 +0100682 int actual_start = (relative_start < 0) ? Max(len + relative_start, 0)
683 : Min(relative_start, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000684
685 // SpiderMonkey, TraceMonkey and JSC treat the case where no delete count is
686 // given differently from when an undefined delete count is given.
687 // This does not follow ECMA-262, but we do the same for
688 // compatibility.
Steve Block6ded16b2010-05-10 14:33:55 +0100689 int delete_count = len;
Andrei Popescu402d9372010-02-26 13:31:12 +0000690 if (n_arguments > 1) {
691 Object* arg2 = args[2];
692 if (arg2->IsSmi()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100693 delete_count = Smi::cast(arg2)->value();
Andrei Popescu402d9372010-02-26 13:31:12 +0000694 } else {
695 return CallJsBuiltin("ArraySplice", args);
696 }
697 }
Steve Block6ded16b2010-05-10 14:33:55 +0100698 int actual_delete_count = Min(Max(delete_count, 0), len - actual_start);
Andrei Popescu402d9372010-02-26 13:31:12 +0000699
Steve Block6ded16b2010-05-10 14:33:55 +0100700 JSArray* result_array = NULL;
701 if (actual_delete_count == 0) {
702 Object* result = AllocateEmptyJSArray();
703 if (result->IsFailure()) return result;
704 result_array = JSArray::cast(result);
705 } else {
706 // Allocate result array.
707 Object* result = AllocateJSArray();
708 if (result->IsFailure()) return result;
709 result_array = JSArray::cast(result);
Andrei Popescu402d9372010-02-26 13:31:12 +0000710
Steve Block6ded16b2010-05-10 14:33:55 +0100711 result = Heap::AllocateUninitializedFixedArray(actual_delete_count);
712 if (result->IsFailure()) return result;
713 FixedArray* result_elms = FixedArray::cast(result);
Andrei Popescu402d9372010-02-26 13:31:12 +0000714
Steve Block6ded16b2010-05-10 14:33:55 +0100715 AssertNoAllocation no_gc;
716 // Fill newly created array.
717 CopyElements(&no_gc,
718 result_elms, 0,
719 elms, actual_start,
720 actual_delete_count);
Andrei Popescu402d9372010-02-26 13:31:12 +0000721
Steve Block6ded16b2010-05-10 14:33:55 +0100722 // Set elements.
723 result_array->set_elements(result_elms);
Andrei Popescu402d9372010-02-26 13:31:12 +0000724
Steve Block6ded16b2010-05-10 14:33:55 +0100725 // Set the length.
726 result_array->set_length(Smi::FromInt(actual_delete_count));
Andrei Popescu402d9372010-02-26 13:31:12 +0000727 }
728
Steve Block6ded16b2010-05-10 14:33:55 +0100729 int item_count = (n_arguments > 1) ? (n_arguments - 2) : 0;
Andrei Popescu402d9372010-02-26 13:31:12 +0000730
Steve Block6ded16b2010-05-10 14:33:55 +0100731 int new_length = len - actual_delete_count + item_count;
Andrei Popescu402d9372010-02-26 13:31:12 +0000732
Steve Block6ded16b2010-05-10 14:33:55 +0100733 if (item_count < actual_delete_count) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000734 // Shrink the array.
Steve Block791712a2010-08-27 10:21:07 +0100735 const bool trim_array = !Heap::lo_space()->Contains(elms) &&
Steve Block6ded16b2010-05-10 14:33:55 +0100736 ((actual_start + item_count) <
737 (len - actual_delete_count - actual_start));
738 if (trim_array) {
739 const int delta = actual_delete_count - item_count;
Andrei Popescu402d9372010-02-26 13:31:12 +0000740
Steve Block6ded16b2010-05-10 14:33:55 +0100741 if (actual_start > 0) {
742 Object** start = elms->data_start();
743 memmove(start + delta, start, actual_start * kPointerSize);
744 }
745
746 elms = LeftTrimFixedArray(elms, delta);
747 array->set_elements(elms, SKIP_WRITE_BARRIER);
748 } else {
749 AssertNoAllocation no_gc;
750 MoveElements(&no_gc,
751 elms, actual_start + item_count,
752 elms, actual_start + actual_delete_count,
753 (len - actual_delete_count - actual_start));
754 FillWithHoles(elms, new_length, len);
Andrei Popescu402d9372010-02-26 13:31:12 +0000755 }
Steve Block6ded16b2010-05-10 14:33:55 +0100756 } else if (item_count > actual_delete_count) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000757 // Currently fixed arrays cannot grow too big, so
758 // we should never hit this case.
Steve Block6ded16b2010-05-10 14:33:55 +0100759 ASSERT((item_count - actual_delete_count) <= (Smi::kMaxValue - len));
Andrei Popescu402d9372010-02-26 13:31:12 +0000760
761 // Check if array need to grow.
762 if (new_length > elms->length()) {
763 // New backing storage is needed.
764 int capacity = new_length + (new_length >> 1) + 16;
Steve Block6ded16b2010-05-10 14:33:55 +0100765 Object* obj = Heap::AllocateUninitializedFixedArray(capacity);
Andrei Popescu402d9372010-02-26 13:31:12 +0000766 if (obj->IsFailure()) return obj;
Andrei Popescu402d9372010-02-26 13:31:12 +0000767 FixedArray* new_elms = FixedArray::cast(obj);
Andrei Popescu402d9372010-02-26 13:31:12 +0000768
Steve Block6ded16b2010-05-10 14:33:55 +0100769 AssertNoAllocation no_gc;
770 // Copy the part before actual_start as is.
771 if (actual_start > 0) {
772 CopyElements(&no_gc, new_elms, 0, elms, 0, actual_start);
Andrei Popescu402d9372010-02-26 13:31:12 +0000773 }
Steve Block6ded16b2010-05-10 14:33:55 +0100774 const int to_copy = len - actual_delete_count - actual_start;
775 if (to_copy > 0) {
776 CopyElements(&no_gc,
777 new_elms, actual_start + item_count,
778 elms, actual_start + actual_delete_count,
779 to_copy);
780 }
781 FillWithHoles(new_elms, new_length, capacity);
Andrei Popescu402d9372010-02-26 13:31:12 +0000782
Andrei Popescu402d9372010-02-26 13:31:12 +0000783 elms = new_elms;
784 array->set_elements(elms);
Steve Block6ded16b2010-05-10 14:33:55 +0100785 } else {
786 AssertNoAllocation no_gc;
787 MoveElements(&no_gc,
788 elms, actual_start + item_count,
789 elms, actual_start + actual_delete_count,
790 (len - actual_delete_count - actual_start));
Andrei Popescu402d9372010-02-26 13:31:12 +0000791 }
792 }
793
Steve Block6ded16b2010-05-10 14:33:55 +0100794 AssertNoAllocation no_gc;
795 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
796 for (int k = actual_start; k < actual_start + item_count; k++) {
797 elms->set(k, args[3 + k - actual_start], mode);
Andrei Popescu402d9372010-02-26 13:31:12 +0000798 }
799
800 // Set the length.
801 array->set_length(Smi::FromInt(new_length));
802
803 return result_array;
804}
805
806
Steve Block6ded16b2010-05-10 14:33:55 +0100807BUILTIN(ArrayConcat) {
Kristian Monsen25f61362010-05-21 11:50:48 +0100808 Context* global_context = Top::context()->global_context();
809 JSObject* array_proto =
810 JSObject::cast(global_context->array_function()->prototype());
811 if (!ArrayPrototypeHasNoElements(global_context, array_proto)) {
Steve Block6ded16b2010-05-10 14:33:55 +0100812 return CallJsBuiltin("ArrayConcat", args);
813 }
814
815 // Iterate through all the arguments performing checks
816 // and calculating total length.
817 int n_arguments = args.length();
818 int result_len = 0;
819 for (int i = 0; i < n_arguments; i++) {
820 Object* arg = args[i];
Kristian Monsen25f61362010-05-21 11:50:48 +0100821 if (!arg->IsJSArray() || !JSArray::cast(arg)->HasFastElements()
822 || JSArray::cast(arg)->GetPrototype() != array_proto) {
Steve Block6ded16b2010-05-10 14:33:55 +0100823 return CallJsBuiltin("ArrayConcat", args);
824 }
825
826 int len = Smi::cast(JSArray::cast(arg)->length())->value();
827
828 // We shouldn't overflow when adding another len.
829 const int kHalfOfMaxInt = 1 << (kBitsPerInt - 2);
830 STATIC_ASSERT(FixedArray::kMaxLength < kHalfOfMaxInt);
831 USE(kHalfOfMaxInt);
832 result_len += len;
833 ASSERT(result_len >= 0);
834
835 if (result_len > FixedArray::kMaxLength) {
836 return CallJsBuiltin("ArrayConcat", args);
837 }
838 }
839
840 if (result_len == 0) {
841 return AllocateEmptyJSArray();
842 }
843
844 // Allocate result.
845 Object* result = AllocateJSArray();
846 if (result->IsFailure()) return result;
847 JSArray* result_array = JSArray::cast(result);
848
849 result = Heap::AllocateUninitializedFixedArray(result_len);
850 if (result->IsFailure()) return result;
851 FixedArray* result_elms = FixedArray::cast(result);
852
853 // Copy data.
854 AssertNoAllocation no_gc;
855 int start_pos = 0;
856 for (int i = 0; i < n_arguments; i++) {
857 JSArray* array = JSArray::cast(args[i]);
858 int len = Smi::cast(array->length())->value();
859 if (len > 0) {
860 FixedArray* elms = FixedArray::cast(array->elements());
861 CopyElements(&no_gc, result_elms, start_pos, elms, 0, len);
862 start_pos += len;
863 }
864 }
865 ASSERT(start_pos == result_len);
866
867 // Set the length and elements.
868 result_array->set_length(Smi::FromInt(result_len));
869 result_array->set_elements(result_elms);
870
871 return result_array;
872}
873
874
Steve Blocka7e24c12009-10-30 11:49:00 +0000875// -----------------------------------------------------------------------------
876//
877
878
879// Returns the holder JSObject if the function can legally be called
880// with this receiver. Returns Heap::null_value() if the call is
881// illegal. Any arguments that don't fit the expected type is
882// overwritten with undefined. Arguments that do fit the expected
883// type is overwritten with the object in the prototype chain that
884// actually has that type.
885static inline Object* TypeCheck(int argc,
886 Object** argv,
887 FunctionTemplateInfo* info) {
888 Object* recv = argv[0];
889 Object* sig_obj = info->signature();
890 if (sig_obj->IsUndefined()) return recv;
891 SignatureInfo* sig = SignatureInfo::cast(sig_obj);
892 // If necessary, check the receiver
893 Object* recv_type = sig->receiver();
894
895 Object* holder = recv;
896 if (!recv_type->IsUndefined()) {
897 for (; holder != Heap::null_value(); holder = holder->GetPrototype()) {
898 if (holder->IsInstanceOf(FunctionTemplateInfo::cast(recv_type))) {
899 break;
900 }
901 }
902 if (holder == Heap::null_value()) return holder;
903 }
904 Object* args_obj = sig->args();
905 // If there is no argument signature we're done
906 if (args_obj->IsUndefined()) return holder;
907 FixedArray* args = FixedArray::cast(args_obj);
908 int length = args->length();
909 if (argc <= length) length = argc - 1;
910 for (int i = 0; i < length; i++) {
911 Object* argtype = args->get(i);
912 if (argtype->IsUndefined()) continue;
913 Object** arg = &argv[-1 - i];
914 Object* current = *arg;
915 for (; current != Heap::null_value(); current = current->GetPrototype()) {
916 if (current->IsInstanceOf(FunctionTemplateInfo::cast(argtype))) {
917 *arg = current;
918 break;
919 }
920 }
921 if (current == Heap::null_value()) *arg = Heap::undefined_value();
922 }
923 return holder;
924}
925
926
Leon Clarkee46be812010-01-19 14:06:41 +0000927template <bool is_construct>
928static Object* HandleApiCallHelper(
929 BuiltinArguments<NEEDS_CALLED_FUNCTION> args) {
930 ASSERT(is_construct == CalledAsConstructor());
Steve Blocka7e24c12009-10-30 11:49:00 +0000931
Leon Clarkee46be812010-01-19 14:06:41 +0000932 HandleScope scope;
933 Handle<JSFunction> function = args.called_function();
Steve Block6ded16b2010-05-10 14:33:55 +0100934 ASSERT(function->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +0000935
Steve Block6ded16b2010-05-10 14:33:55 +0100936 FunctionTemplateInfo* fun_data = function->shared()->get_api_func_data();
Steve Blocka7e24c12009-10-30 11:49:00 +0000937 if (is_construct) {
Steve Block6ded16b2010-05-10 14:33:55 +0100938 Handle<FunctionTemplateInfo> desc(fun_data);
Steve Blocka7e24c12009-10-30 11:49:00 +0000939 bool pending_exception = false;
Leon Clarkee46be812010-01-19 14:06:41 +0000940 Factory::ConfigureInstance(desc, Handle<JSObject>::cast(args.receiver()),
Steve Blocka7e24c12009-10-30 11:49:00 +0000941 &pending_exception);
942 ASSERT(Top::has_pending_exception() == pending_exception);
943 if (pending_exception) return Failure::Exception();
Steve Block6ded16b2010-05-10 14:33:55 +0100944 fun_data = *desc;
Steve Blocka7e24c12009-10-30 11:49:00 +0000945 }
946
Steve Blocka7e24c12009-10-30 11:49:00 +0000947 Object* raw_holder = TypeCheck(args.length(), &args[0], fun_data);
948
949 if (raw_holder->IsNull()) {
950 // This function cannot be called with the given receiver. Abort!
951 Handle<Object> obj =
952 Factory::NewTypeError("illegal_invocation", HandleVector(&function, 1));
953 return Top::Throw(*obj);
954 }
955
956 Object* raw_call_data = fun_data->call_code();
957 if (!raw_call_data->IsUndefined()) {
958 CallHandlerInfo* call_data = CallHandlerInfo::cast(raw_call_data);
959 Object* callback_obj = call_data->callback();
960 v8::InvocationCallback callback =
961 v8::ToCData<v8::InvocationCallback>(callback_obj);
962 Object* data_obj = call_data->data();
963 Object* result;
964
Steve Blocka7e24c12009-10-30 11:49:00 +0000965 Handle<Object> data_handle(data_obj);
966 v8::Local<v8::Value> data = v8::Utils::ToLocal(data_handle);
967 ASSERT(raw_holder->IsJSObject());
968 v8::Local<v8::Function> callee = v8::Utils::ToLocal(function);
969 Handle<JSObject> holder_handle(JSObject::cast(raw_holder));
970 v8::Local<v8::Object> holder = v8::Utils::ToLocal(holder_handle);
Leon Clarkee46be812010-01-19 14:06:41 +0000971 LOG(ApiObjectAccess("call", JSObject::cast(*args.receiver())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000972 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments(
973 data,
974 holder,
975 callee,
976 is_construct,
977 reinterpret_cast<void**>(&args[0] - 1),
978 args.length() - 1);
979
980 v8::Handle<v8::Value> value;
981 {
982 // Leaving JavaScript.
983 VMState state(EXTERNAL);
Steve Blockd0582a62009-12-15 09:54:21 +0000984#ifdef ENABLE_LOGGING_AND_PROFILING
985 state.set_external_callback(v8::ToCData<Address>(callback_obj));
986#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000987 value = callback(new_args);
988 }
989 if (value.IsEmpty()) {
990 result = Heap::undefined_value();
991 } else {
992 result = *reinterpret_cast<Object**>(*value);
993 }
994
995 RETURN_IF_SCHEDULED_EXCEPTION();
996 if (!is_construct || result->IsJSObject()) return result;
997 }
998
Leon Clarkee46be812010-01-19 14:06:41 +0000999 return *args.receiver();
Steve Blocka7e24c12009-10-30 11:49:00 +00001000}
Leon Clarkee46be812010-01-19 14:06:41 +00001001
1002
1003BUILTIN(HandleApiCall) {
1004 return HandleApiCallHelper<false>(args);
1005}
1006
1007
1008BUILTIN(HandleApiCallConstruct) {
1009 return HandleApiCallHelper<true>(args);
1010}
Steve Blocka7e24c12009-10-30 11:49:00 +00001011
1012
Andrei Popescu402d9372010-02-26 13:31:12 +00001013#ifdef DEBUG
1014
1015static void VerifyTypeCheck(Handle<JSObject> object,
1016 Handle<JSFunction> function) {
Steve Block6ded16b2010-05-10 14:33:55 +01001017 ASSERT(function->shared()->IsApiFunction());
1018 FunctionTemplateInfo* info = function->shared()->get_api_func_data();
Andrei Popescu402d9372010-02-26 13:31:12 +00001019 if (info->signature()->IsUndefined()) return;
1020 SignatureInfo* signature = SignatureInfo::cast(info->signature());
1021 Object* receiver_type = signature->receiver();
1022 if (receiver_type->IsUndefined()) return;
1023 FunctionTemplateInfo* type = FunctionTemplateInfo::cast(receiver_type);
1024 ASSERT(object->IsInstanceOf(type));
1025}
1026
1027#endif
1028
1029
1030BUILTIN(FastHandleApiCall) {
1031 ASSERT(!CalledAsConstructor());
1032 const bool is_construct = false;
1033
1034 // We expect four more arguments: function, callback, call data, and holder.
1035 const int args_length = args.length() - 4;
1036 ASSERT(args_length >= 0);
1037
1038 Handle<JSFunction> function = args.at<JSFunction>(args_length);
1039 Object* callback_obj = args[args_length + 1];
1040 Handle<Object> data_handle = args.at<Object>(args_length + 2);
1041 Handle<JSObject> checked_holder = args.at<JSObject>(args_length + 3);
1042
1043#ifdef DEBUG
1044 VerifyTypeCheck(checked_holder, function);
1045#endif
1046
1047 v8::Local<v8::Object> holder = v8::Utils::ToLocal(checked_holder);
1048 v8::Local<v8::Function> callee = v8::Utils::ToLocal(function);
1049 v8::InvocationCallback callback =
1050 v8::ToCData<v8::InvocationCallback>(callback_obj);
1051 v8::Local<v8::Value> data = v8::Utils::ToLocal(data_handle);
1052
1053 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments(
1054 data,
1055 holder,
1056 callee,
1057 is_construct,
1058 reinterpret_cast<void**>(&args[0] - 1),
1059 args_length - 1);
1060
1061 HandleScope scope;
1062 Object* result;
1063 v8::Handle<v8::Value> value;
1064 {
1065 // Leaving JavaScript.
1066 VMState state(EXTERNAL);
1067#ifdef ENABLE_LOGGING_AND_PROFILING
1068 state.set_external_callback(v8::ToCData<Address>(callback_obj));
1069#endif
1070 value = callback(new_args);
1071 }
1072 if (value.IsEmpty()) {
1073 result = Heap::undefined_value();
1074 } else {
1075 result = *reinterpret_cast<Object**>(*value);
1076 }
1077
1078 RETURN_IF_SCHEDULED_EXCEPTION();
1079 return result;
1080}
1081
1082
Steve Blocka7e24c12009-10-30 11:49:00 +00001083// Helper function to handle calls to non-function objects created through the
1084// API. The object can be called as either a constructor (using new) or just as
1085// a function (without new).
Leon Clarkee46be812010-01-19 14:06:41 +00001086static Object* HandleApiCallAsFunctionOrConstructor(
1087 bool is_construct_call,
1088 BuiltinArguments<NO_EXTRA_ARGUMENTS> args) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001089 // Non-functions are never called as constructors. Even if this is an object
1090 // called as a constructor the delegate call is not a construct call.
1091 ASSERT(!CalledAsConstructor());
1092
1093 Handle<Object> receiver = args.at<Object>(0);
1094
1095 // Get the object called.
Leon Clarkee46be812010-01-19 14:06:41 +00001096 JSObject* obj = JSObject::cast(*args.receiver());
Steve Blocka7e24c12009-10-30 11:49:00 +00001097
1098 // Get the invocation callback from the function descriptor that was
1099 // used to create the called object.
1100 ASSERT(obj->map()->has_instance_call_handler());
1101 JSFunction* constructor = JSFunction::cast(obj->map()->constructor());
Steve Block6ded16b2010-05-10 14:33:55 +01001102 ASSERT(constructor->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +00001103 Object* handler =
Steve Block6ded16b2010-05-10 14:33:55 +01001104 constructor->shared()->get_api_func_data()->instance_call_handler();
Steve Blocka7e24c12009-10-30 11:49:00 +00001105 ASSERT(!handler->IsUndefined());
1106 CallHandlerInfo* call_data = CallHandlerInfo::cast(handler);
1107 Object* callback_obj = call_data->callback();
1108 v8::InvocationCallback callback =
1109 v8::ToCData<v8::InvocationCallback>(callback_obj);
1110
1111 // Get the data for the call and perform the callback.
1112 Object* data_obj = call_data->data();
1113 Object* result;
1114 { HandleScope scope;
1115 v8::Local<v8::Object> self =
Leon Clarkee46be812010-01-19 14:06:41 +00001116 v8::Utils::ToLocal(Handle<JSObject>::cast(args.receiver()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001117 Handle<Object> data_handle(data_obj);
1118 v8::Local<v8::Value> data = v8::Utils::ToLocal(data_handle);
1119 Handle<JSFunction> callee_handle(constructor);
1120 v8::Local<v8::Function> callee = v8::Utils::ToLocal(callee_handle);
Leon Clarkee46be812010-01-19 14:06:41 +00001121 LOG(ApiObjectAccess("call non-function", JSObject::cast(*args.receiver())));
Steve Blocka7e24c12009-10-30 11:49:00 +00001122 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments(
1123 data,
1124 self,
1125 callee,
1126 is_construct_call,
1127 reinterpret_cast<void**>(&args[0] - 1),
1128 args.length() - 1);
1129 v8::Handle<v8::Value> value;
1130 {
1131 // Leaving JavaScript.
1132 VMState state(EXTERNAL);
Steve Blockd0582a62009-12-15 09:54:21 +00001133#ifdef ENABLE_LOGGING_AND_PROFILING
1134 state.set_external_callback(v8::ToCData<Address>(callback_obj));
1135#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001136 value = callback(new_args);
1137 }
1138 if (value.IsEmpty()) {
1139 result = Heap::undefined_value();
1140 } else {
1141 result = *reinterpret_cast<Object**>(*value);
1142 }
1143 }
1144 // Check for exceptions and return result.
1145 RETURN_IF_SCHEDULED_EXCEPTION();
1146 return result;
1147}
1148
1149
1150// Handle calls to non-function objects created through the API. This delegate
1151// function is used when the call is a normal function call.
1152BUILTIN(HandleApiCallAsFunction) {
1153 return HandleApiCallAsFunctionOrConstructor(false, args);
1154}
Steve Blocka7e24c12009-10-30 11:49:00 +00001155
1156
1157// Handle calls to non-function objects created through the API. This delegate
1158// function is used when the call is a construct call.
1159BUILTIN(HandleApiCallAsConstructor) {
1160 return HandleApiCallAsFunctionOrConstructor(true, args);
1161}
Steve Blocka7e24c12009-10-30 11:49:00 +00001162
1163
1164static void Generate_LoadIC_ArrayLength(MacroAssembler* masm) {
1165 LoadIC::GenerateArrayLength(masm);
1166}
1167
1168
1169static void Generate_LoadIC_StringLength(MacroAssembler* masm) {
1170 LoadIC::GenerateStringLength(masm);
1171}
1172
1173
1174static void Generate_LoadIC_FunctionPrototype(MacroAssembler* masm) {
1175 LoadIC::GenerateFunctionPrototype(masm);
1176}
1177
1178
1179static void Generate_LoadIC_Initialize(MacroAssembler* masm) {
1180 LoadIC::GenerateInitialize(masm);
1181}
1182
1183
1184static void Generate_LoadIC_PreMonomorphic(MacroAssembler* masm) {
1185 LoadIC::GeneratePreMonomorphic(masm);
1186}
1187
1188
1189static void Generate_LoadIC_Miss(MacroAssembler* masm) {
1190 LoadIC::GenerateMiss(masm);
1191}
1192
1193
1194static void Generate_LoadIC_Megamorphic(MacroAssembler* masm) {
1195 LoadIC::GenerateMegamorphic(masm);
1196}
1197
1198
1199static void Generate_LoadIC_Normal(MacroAssembler* masm) {
1200 LoadIC::GenerateNormal(masm);
1201}
1202
1203
1204static void Generate_KeyedLoadIC_Initialize(MacroAssembler* masm) {
1205 KeyedLoadIC::GenerateInitialize(masm);
1206}
1207
1208
1209static void Generate_KeyedLoadIC_Miss(MacroAssembler* masm) {
1210 KeyedLoadIC::GenerateMiss(masm);
1211}
1212
1213
1214static void Generate_KeyedLoadIC_Generic(MacroAssembler* masm) {
1215 KeyedLoadIC::GenerateGeneric(masm);
1216}
1217
1218
Leon Clarkee46be812010-01-19 14:06:41 +00001219static void Generate_KeyedLoadIC_String(MacroAssembler* masm) {
1220 KeyedLoadIC::GenerateString(masm);
1221}
1222
1223
Steve Block3ce2e202009-11-05 08:53:23 +00001224static void Generate_KeyedLoadIC_ExternalByteArray(MacroAssembler* masm) {
1225 KeyedLoadIC::GenerateExternalArray(masm, kExternalByteArray);
1226}
1227
1228
1229static void Generate_KeyedLoadIC_ExternalUnsignedByteArray(
1230 MacroAssembler* masm) {
1231 KeyedLoadIC::GenerateExternalArray(masm, kExternalUnsignedByteArray);
1232}
1233
1234
1235static void Generate_KeyedLoadIC_ExternalShortArray(MacroAssembler* masm) {
1236 KeyedLoadIC::GenerateExternalArray(masm, kExternalShortArray);
1237}
1238
1239
1240static void Generate_KeyedLoadIC_ExternalUnsignedShortArray(
1241 MacroAssembler* masm) {
1242 KeyedLoadIC::GenerateExternalArray(masm, kExternalUnsignedShortArray);
1243}
1244
1245
1246static void Generate_KeyedLoadIC_ExternalIntArray(MacroAssembler* masm) {
1247 KeyedLoadIC::GenerateExternalArray(masm, kExternalIntArray);
1248}
1249
1250
1251static void Generate_KeyedLoadIC_ExternalUnsignedIntArray(
1252 MacroAssembler* masm) {
1253 KeyedLoadIC::GenerateExternalArray(masm, kExternalUnsignedIntArray);
1254}
1255
1256
1257static void Generate_KeyedLoadIC_ExternalFloatArray(MacroAssembler* masm) {
1258 KeyedLoadIC::GenerateExternalArray(masm, kExternalFloatArray);
1259}
1260
1261
Steve Blocka7e24c12009-10-30 11:49:00 +00001262static void Generate_KeyedLoadIC_PreMonomorphic(MacroAssembler* masm) {
1263 KeyedLoadIC::GeneratePreMonomorphic(masm);
1264}
1265
Andrei Popescu402d9372010-02-26 13:31:12 +00001266static void Generate_KeyedLoadIC_IndexedInterceptor(MacroAssembler* masm) {
1267 KeyedLoadIC::GenerateIndexedInterceptor(masm);
1268}
1269
Steve Blocka7e24c12009-10-30 11:49:00 +00001270
1271static void Generate_StoreIC_Initialize(MacroAssembler* masm) {
1272 StoreIC::GenerateInitialize(masm);
1273}
1274
1275
1276static void Generate_StoreIC_Miss(MacroAssembler* masm) {
1277 StoreIC::GenerateMiss(masm);
1278}
1279
1280
Steve Block8defd9f2010-07-08 12:39:36 +01001281static void Generate_StoreIC_Normal(MacroAssembler* masm) {
1282 StoreIC::GenerateNormal(masm);
1283}
1284
1285
Steve Blocka7e24c12009-10-30 11:49:00 +00001286static void Generate_StoreIC_Megamorphic(MacroAssembler* masm) {
1287 StoreIC::GenerateMegamorphic(masm);
1288}
1289
1290
Steve Block6ded16b2010-05-10 14:33:55 +01001291static void Generate_StoreIC_ArrayLength(MacroAssembler* masm) {
1292 StoreIC::GenerateArrayLength(masm);
1293}
1294
1295
Steve Blocka7e24c12009-10-30 11:49:00 +00001296static void Generate_KeyedStoreIC_Generic(MacroAssembler* masm) {
1297 KeyedStoreIC::GenerateGeneric(masm);
1298}
1299
1300
Steve Block3ce2e202009-11-05 08:53:23 +00001301static void Generate_KeyedStoreIC_ExternalByteArray(MacroAssembler* masm) {
1302 KeyedStoreIC::GenerateExternalArray(masm, kExternalByteArray);
1303}
1304
1305
1306static void Generate_KeyedStoreIC_ExternalUnsignedByteArray(
1307 MacroAssembler* masm) {
1308 KeyedStoreIC::GenerateExternalArray(masm, kExternalUnsignedByteArray);
1309}
1310
1311
1312static void Generate_KeyedStoreIC_ExternalShortArray(MacroAssembler* masm) {
1313 KeyedStoreIC::GenerateExternalArray(masm, kExternalShortArray);
1314}
1315
1316
1317static void Generate_KeyedStoreIC_ExternalUnsignedShortArray(
1318 MacroAssembler* masm) {
1319 KeyedStoreIC::GenerateExternalArray(masm, kExternalUnsignedShortArray);
1320}
1321
1322
1323static void Generate_KeyedStoreIC_ExternalIntArray(MacroAssembler* masm) {
1324 KeyedStoreIC::GenerateExternalArray(masm, kExternalIntArray);
1325}
1326
1327
1328static void Generate_KeyedStoreIC_ExternalUnsignedIntArray(
1329 MacroAssembler* masm) {
1330 KeyedStoreIC::GenerateExternalArray(masm, kExternalUnsignedIntArray);
1331}
1332
1333
1334static void Generate_KeyedStoreIC_ExternalFloatArray(MacroAssembler* masm) {
1335 KeyedStoreIC::GenerateExternalArray(masm, kExternalFloatArray);
1336}
1337
1338
Steve Blocka7e24c12009-10-30 11:49:00 +00001339static void Generate_KeyedStoreIC_Miss(MacroAssembler* masm) {
1340 KeyedStoreIC::GenerateMiss(masm);
1341}
1342
1343
1344static void Generate_KeyedStoreIC_Initialize(MacroAssembler* masm) {
1345 KeyedStoreIC::GenerateInitialize(masm);
1346}
1347
1348
1349#ifdef ENABLE_DEBUGGER_SUPPORT
1350static void Generate_LoadIC_DebugBreak(MacroAssembler* masm) {
1351 Debug::GenerateLoadICDebugBreak(masm);
1352}
1353
1354
1355static void Generate_StoreIC_DebugBreak(MacroAssembler* masm) {
1356 Debug::GenerateStoreICDebugBreak(masm);
1357}
1358
1359
1360static void Generate_KeyedLoadIC_DebugBreak(MacroAssembler* masm) {
1361 Debug::GenerateKeyedLoadICDebugBreak(masm);
1362}
1363
1364
1365static void Generate_KeyedStoreIC_DebugBreak(MacroAssembler* masm) {
1366 Debug::GenerateKeyedStoreICDebugBreak(masm);
1367}
1368
1369
1370static void Generate_ConstructCall_DebugBreak(MacroAssembler* masm) {
1371 Debug::GenerateConstructCallDebugBreak(masm);
1372}
1373
1374
1375static void Generate_Return_DebugBreak(MacroAssembler* masm) {
1376 Debug::GenerateReturnDebugBreak(masm);
1377}
1378
1379
1380static void Generate_StubNoRegisters_DebugBreak(MacroAssembler* masm) {
1381 Debug::GenerateStubNoRegistersDebugBreak(masm);
1382}
Steve Block6ded16b2010-05-10 14:33:55 +01001383
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001384
1385static void Generate_Slot_DebugBreak(MacroAssembler* masm) {
1386 Debug::GenerateSlotDebugBreak(masm);
1387}
1388
1389
Steve Block6ded16b2010-05-10 14:33:55 +01001390static void Generate_PlainReturn_LiveEdit(MacroAssembler* masm) {
1391 Debug::GeneratePlainReturnLiveEdit(masm);
1392}
1393
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001394
Steve Block6ded16b2010-05-10 14:33:55 +01001395static void Generate_FrameDropper_LiveEdit(MacroAssembler* masm) {
1396 Debug::GenerateFrameDropperLiveEdit(masm);
1397}
Steve Blocka7e24c12009-10-30 11:49:00 +00001398#endif
1399
1400Object* Builtins::builtins_[builtin_count] = { NULL, };
1401const char* Builtins::names_[builtin_count] = { NULL, };
1402
Leon Clarkee46be812010-01-19 14:06:41 +00001403#define DEF_ENUM_C(name, ignore) FUNCTION_ADDR(Builtin_##name),
Steve Blocka7e24c12009-10-30 11:49:00 +00001404 Address Builtins::c_functions_[cfunction_count] = {
1405 BUILTIN_LIST_C(DEF_ENUM_C)
1406 };
1407#undef DEF_ENUM_C
1408
1409#define DEF_JS_NAME(name, ignore) #name,
1410#define DEF_JS_ARGC(ignore, argc) argc,
1411const char* Builtins::javascript_names_[id_count] = {
1412 BUILTINS_LIST_JS(DEF_JS_NAME)
1413};
1414
1415int Builtins::javascript_argc_[id_count] = {
1416 BUILTINS_LIST_JS(DEF_JS_ARGC)
1417};
1418#undef DEF_JS_NAME
1419#undef DEF_JS_ARGC
1420
1421static bool is_initialized = false;
1422void Builtins::Setup(bool create_heap_objects) {
1423 ASSERT(!is_initialized);
1424
1425 // Create a scope for the handles in the builtins.
1426 HandleScope scope;
1427
1428 struct BuiltinDesc {
1429 byte* generator;
1430 byte* c_code;
1431 const char* s_name; // name is only used for generating log information.
1432 int name;
1433 Code::Flags flags;
Leon Clarkee46be812010-01-19 14:06:41 +00001434 BuiltinExtraArguments extra_args;
Steve Blocka7e24c12009-10-30 11:49:00 +00001435 };
1436
Leon Clarkee46be812010-01-19 14:06:41 +00001437#define DEF_FUNCTION_PTR_C(name, extra_args) \
1438 { FUNCTION_ADDR(Generate_Adaptor), \
1439 FUNCTION_ADDR(Builtin_##name), \
1440 #name, \
1441 c_##name, \
1442 Code::ComputeFlags(Code::BUILTIN), \
1443 extra_args \
Steve Blocka7e24c12009-10-30 11:49:00 +00001444 },
1445
1446#define DEF_FUNCTION_PTR_A(name, kind, state) \
1447 { FUNCTION_ADDR(Generate_##name), \
1448 NULL, \
1449 #name, \
1450 name, \
Leon Clarkee46be812010-01-19 14:06:41 +00001451 Code::ComputeFlags(Code::kind, NOT_IN_LOOP, state), \
1452 NO_EXTRA_ARGUMENTS \
Steve Blocka7e24c12009-10-30 11:49:00 +00001453 },
1454
1455 // Define array of pointers to generators and C builtin functions.
1456 static BuiltinDesc functions[] = {
1457 BUILTIN_LIST_C(DEF_FUNCTION_PTR_C)
1458 BUILTIN_LIST_A(DEF_FUNCTION_PTR_A)
1459 BUILTIN_LIST_DEBUG_A(DEF_FUNCTION_PTR_A)
1460 // Terminator:
Leon Clarkee46be812010-01-19 14:06:41 +00001461 { NULL, NULL, NULL, builtin_count, static_cast<Code::Flags>(0),
1462 NO_EXTRA_ARGUMENTS }
Steve Blocka7e24c12009-10-30 11:49:00 +00001463 };
1464
1465#undef DEF_FUNCTION_PTR_C
1466#undef DEF_FUNCTION_PTR_A
1467
1468 // For now we generate builtin adaptor code into a stack-allocated
1469 // buffer, before copying it into individual code objects.
1470 byte buffer[4*KB];
1471
1472 // Traverse the list of builtins and generate an adaptor in a
1473 // separate code object for each one.
1474 for (int i = 0; i < builtin_count; i++) {
1475 if (create_heap_objects) {
1476 MacroAssembler masm(buffer, sizeof buffer);
1477 // Generate the code/adaptor.
Leon Clarkee46be812010-01-19 14:06:41 +00001478 typedef void (*Generator)(MacroAssembler*, int, BuiltinExtraArguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00001479 Generator g = FUNCTION_CAST<Generator>(functions[i].generator);
1480 // We pass all arguments to the generator, but it may not use all of
1481 // them. This works because the first arguments are on top of the
1482 // stack.
Leon Clarkee46be812010-01-19 14:06:41 +00001483 g(&masm, functions[i].name, functions[i].extra_args);
Steve Blocka7e24c12009-10-30 11:49:00 +00001484 // Move the code into the object heap.
1485 CodeDesc desc;
1486 masm.GetCode(&desc);
1487 Code::Flags flags = functions[i].flags;
1488 Object* code;
1489 {
1490 // During startup it's OK to always allocate and defer GC to later.
1491 // This simplifies things because we don't need to retry.
1492 AlwaysAllocateScope __scope__;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001493 code = Heap::CreateCode(desc, flags, masm.CodeObject());
Steve Blocka7e24c12009-10-30 11:49:00 +00001494 if (code->IsFailure()) {
1495 v8::internal::V8::FatalProcessOutOfMemory("CreateCode");
1496 }
1497 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001498 // Log the event and add the code to the builtins array.
Steve Block6ded16b2010-05-10 14:33:55 +01001499 PROFILE(CodeCreateEvent(Logger::BUILTIN_TAG,
1500 Code::cast(code), functions[i].s_name));
Steve Blocka7e24c12009-10-30 11:49:00 +00001501 builtins_[i] = code;
1502#ifdef ENABLE_DISASSEMBLER
1503 if (FLAG_print_builtin_code) {
1504 PrintF("Builtin: %s\n", functions[i].s_name);
1505 Code::cast(code)->Disassemble(functions[i].s_name);
1506 PrintF("\n");
1507 }
1508#endif
1509 } else {
1510 // Deserializing. The values will be filled in during IterateBuiltins.
1511 builtins_[i] = NULL;
1512 }
1513 names_[i] = functions[i].s_name;
1514 }
1515
1516 // Mark as initialized.
1517 is_initialized = true;
1518}
1519
1520
1521void Builtins::TearDown() {
1522 is_initialized = false;
1523}
1524
1525
1526void Builtins::IterateBuiltins(ObjectVisitor* v) {
1527 v->VisitPointers(&builtins_[0], &builtins_[0] + builtin_count);
1528}
1529
1530
1531const char* Builtins::Lookup(byte* pc) {
1532 if (is_initialized) { // may be called during initialization (disassembler!)
1533 for (int i = 0; i < builtin_count; i++) {
1534 Code* entry = Code::cast(builtins_[i]);
1535 if (entry->contains(pc)) {
1536 return names_[i];
1537 }
1538 }
1539 }
1540 return NULL;
1541}
1542
1543
1544} } // namespace v8::internal