Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(264)

Side by Side Diff: src/builtins.cc

Issue 6880010: Merge (7265, 7271] from bleeding_edge to experimental/gc branch.... (Closed) Base URL: http://v8.googlecode.com/svn/branches/experimental/gc/
Patch Set: '' Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/builtins.h ('k') | src/checks.cc » ('j') | src/heap.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 #endif 101 #endif
102 102
103 103
104 #define DEF_ARG_TYPE(name, spec) \ 104 #define DEF_ARG_TYPE(name, spec) \
105 typedef BuiltinArguments<spec> name##ArgumentsType; 105 typedef BuiltinArguments<spec> name##ArgumentsType;
106 BUILTIN_LIST_C(DEF_ARG_TYPE) 106 BUILTIN_LIST_C(DEF_ARG_TYPE)
107 #undef DEF_ARG_TYPE 107 #undef DEF_ARG_TYPE
108 108
109 } // namespace 109 } // namespace
110 110
111
112 // ---------------------------------------------------------------------------- 111 // ----------------------------------------------------------------------------
113 // Support macro for defining builtins in C++. 112 // Support macro for defining builtins in C++.
114 // ---------------------------------------------------------------------------- 113 // ----------------------------------------------------------------------------
115 // 114 //
116 // A builtin function is defined by writing: 115 // A builtin function is defined by writing:
117 // 116 //
118 // BUILTIN(name) { 117 // BUILTIN(name) {
119 // ... 118 // ...
120 // } 119 // }
121 // 120 //
122 // In the body of the builtin function the arguments can be accessed 121 // In the body of the builtin function the arguments can be accessed
123 // through the BuiltinArguments object args. 122 // through the BuiltinArguments object args.
124 123
125 #ifdef DEBUG 124 #ifdef DEBUG
126 125
127 #define BUILTIN(name) \ 126 #define BUILTIN(name) \
128 MUST_USE_RESULT static MaybeObject* Builtin_Impl_##name( \ 127 MUST_USE_RESULT static MaybeObject* Builtin_Impl_##name( \
129 name##ArgumentsType args); \ 128 name##ArgumentsType args, Isolate* isolate); \
130 MUST_USE_RESULT static MaybeObject* Builtin_##name( \ 129 MUST_USE_RESULT static MaybeObject* Builtin_##name( \
131 name##ArgumentsType args) { \ 130 name##ArgumentsType args, Isolate* isolate) { \
132 args.Verify(); \ 131 ASSERT(isolate == Isolate::Current()); \
133 return Builtin_Impl_##name(args); \ 132 args.Verify(); \
134 } \ 133 return Builtin_Impl_##name(args, isolate); \
135 MUST_USE_RESULT static MaybeObject* Builtin_Impl_##name( \ 134 } \
136 name##ArgumentsType args) 135 MUST_USE_RESULT static MaybeObject* Builtin_Impl_##name( \
136 name##ArgumentsType args, Isolate* isolate)
137 137
138 #else // For release mode. 138 #else // For release mode.
139 139
140 #define BUILTIN(name) \ 140 #define BUILTIN(name) \
141 static MaybeObject* Builtin_##name(name##ArgumentsType args) 141 static MaybeObject* Builtin_##name(name##ArgumentsType args, Isolate* isolate)
142 142
143 #endif 143 #endif
144 144
145 145
146 static inline bool CalledAsConstructor() { 146 static inline bool CalledAsConstructor(Isolate* isolate) {
147 #ifdef DEBUG 147 #ifdef DEBUG
148 // Calculate the result using a full stack frame iterator and check 148 // Calculate the result using a full stack frame iterator and check
149 // that the state of the stack is as we assume it to be in the 149 // that the state of the stack is as we assume it to be in the
150 // code below. 150 // code below.
151 StackFrameIterator it; 151 StackFrameIterator it;
152 ASSERT(it.frame()->is_exit()); 152 ASSERT(it.frame()->is_exit());
153 it.Advance(); 153 it.Advance();
154 StackFrame* frame = it.frame(); 154 StackFrame* frame = it.frame();
155 bool reference_result = frame->is_construct(); 155 bool reference_result = frame->is_construct();
156 #endif 156 #endif
157 Address fp = Top::c_entry_fp(Top::GetCurrentThread()); 157 Address fp = Isolate::c_entry_fp(isolate->thread_local_top());
158 // Because we know fp points to an exit frame we can use the relevant 158 // Because we know fp points to an exit frame we can use the relevant
159 // part of ExitFrame::ComputeCallerState directly. 159 // part of ExitFrame::ComputeCallerState directly.
160 const int kCallerOffset = ExitFrameConstants::kCallerFPOffset; 160 const int kCallerOffset = ExitFrameConstants::kCallerFPOffset;
161 Address caller_fp = Memory::Address_at(fp + kCallerOffset); 161 Address caller_fp = Memory::Address_at(fp + kCallerOffset);
162 // This inlines the part of StackFrame::ComputeType that grabs the 162 // This inlines the part of StackFrame::ComputeType that grabs the
163 // type of the current frame. Note that StackFrame::ComputeType 163 // type of the current frame. Note that StackFrame::ComputeType
164 // has been specialized for each architecture so if any one of them 164 // has been specialized for each architecture so if any one of them
165 // changes this code has to be changed as well. 165 // changes this code has to be changed as well.
166 const int kMarkerOffset = StandardFrameConstants::kMarkerOffset; 166 const int kMarkerOffset = StandardFrameConstants::kMarkerOffset;
167 const Smi* kConstructMarker = Smi::FromInt(StackFrame::CONSTRUCT); 167 const Smi* kConstructMarker = Smi::FromInt(StackFrame::CONSTRUCT);
168 Object* marker = Memory::Object_at(caller_fp + kMarkerOffset); 168 Object* marker = Memory::Object_at(caller_fp + kMarkerOffset);
169 bool result = (marker == kConstructMarker); 169 bool result = (marker == kConstructMarker);
170 ASSERT_EQ(result, reference_result); 170 ASSERT_EQ(result, reference_result);
171 return result; 171 return result;
172 } 172 }
173 173
174 // ---------------------------------------------------------------------------- 174 // ----------------------------------------------------------------------------
175 175
176
177 BUILTIN(Illegal) { 176 BUILTIN(Illegal) {
178 UNREACHABLE(); 177 UNREACHABLE();
179 return Heap::undefined_value(); // Make compiler happy. 178 return isolate->heap()->undefined_value(); // Make compiler happy.
180 } 179 }
181 180
182 181
183 BUILTIN(EmptyFunction) { 182 BUILTIN(EmptyFunction) {
184 return Heap::undefined_value(); 183 return isolate->heap()->undefined_value();
185 } 184 }
186 185
187 186
188 BUILTIN(ArrayCodeGeneric) { 187 BUILTIN(ArrayCodeGeneric) {
189 Counters::array_function_runtime.Increment(); 188 Heap* heap = isolate->heap();
189 isolate->counters()->array_function_runtime()->Increment();
190 190
191 JSArray* array; 191 JSArray* array;
192 if (CalledAsConstructor()) { 192 if (CalledAsConstructor(isolate)) {
193 array = JSArray::cast(*args.receiver()); 193 array = JSArray::cast(*args.receiver());
194 } else { 194 } else {
195 // Allocate the JS Array 195 // Allocate the JS Array
196 JSFunction* constructor = 196 JSFunction* constructor =
197 Top::context()->global_context()->array_function(); 197 isolate->context()->global_context()->array_function();
198 Object* obj; 198 Object* obj;
199 { MaybeObject* maybe_obj = Heap::AllocateJSObject(constructor); 199 { MaybeObject* maybe_obj = heap->AllocateJSObject(constructor);
200 if (!maybe_obj->ToObject(&obj)) return maybe_obj; 200 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
201 } 201 }
202 array = JSArray::cast(obj); 202 array = JSArray::cast(obj);
203 } 203 }
204 204
205 // 'array' now contains the JSArray we should initialize. 205 // 'array' now contains the JSArray we should initialize.
206 ASSERT(array->HasFastElements()); 206 ASSERT(array->HasFastElements());
207 207
208 // Optimize the case where there is one argument and the argument is a 208 // Optimize the case where there is one argument and the argument is a
209 // small smi. 209 // small smi.
210 if (args.length() == 2) { 210 if (args.length() == 2) {
211 Object* obj = args[1]; 211 Object* obj = args[1];
212 if (obj->IsSmi()) { 212 if (obj->IsSmi()) {
213 int len = Smi::cast(obj)->value(); 213 int len = Smi::cast(obj)->value();
214 if (len >= 0 && len < JSObject::kInitialMaxFastElementArray) { 214 if (len >= 0 && len < JSObject::kInitialMaxFastElementArray) {
215 Object* obj; 215 Object* obj;
216 { MaybeObject* maybe_obj = Heap::AllocateFixedArrayWithHoles(len); 216 { MaybeObject* maybe_obj = heap->AllocateFixedArrayWithHoles(len);
217 if (!maybe_obj->ToObject(&obj)) return maybe_obj; 217 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
218 } 218 }
219 array->SetContent(FixedArray::cast(obj)); 219 array->SetContent(FixedArray::cast(obj));
220 return array; 220 return array;
221 } 221 }
222 } 222 }
223 // Take the argument as the length. 223 // Take the argument as the length.
224 { MaybeObject* maybe_obj = array->Initialize(0); 224 { MaybeObject* maybe_obj = array->Initialize(0);
225 if (!maybe_obj->ToObject(&obj)) return maybe_obj; 225 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
226 } 226 }
227 return array->SetElementsLength(args[1]); 227 return array->SetElementsLength(args[1]);
228 } 228 }
229 229
230 // Optimize the case where there are no parameters passed. 230 // Optimize the case where there are no parameters passed.
231 if (args.length() == 1) { 231 if (args.length() == 1) {
232 return array->Initialize(JSArray::kPreallocatedArrayElements); 232 return array->Initialize(JSArray::kPreallocatedArrayElements);
233 } 233 }
234 234
235 // Take the arguments as elements. 235 // Take the arguments as elements.
236 int number_of_elements = args.length() - 1; 236 int number_of_elements = args.length() - 1;
237 Smi* len = Smi::FromInt(number_of_elements); 237 Smi* len = Smi::FromInt(number_of_elements);
238 Object* obj; 238 Object* obj;
239 { MaybeObject* maybe_obj = Heap::AllocateFixedArrayWithHoles(len->value()); 239 { MaybeObject* maybe_obj = heap->AllocateFixedArrayWithHoles(len->value());
240 if (!maybe_obj->ToObject(&obj)) return maybe_obj; 240 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
241 } 241 }
242 242
243 AssertNoAllocation no_gc; 243 AssertNoAllocation no_gc;
244 FixedArray* elms = FixedArray::cast(obj); 244 FixedArray* elms = FixedArray::cast(obj);
245 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc); 245 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
246 // Fill in the content 246 // Fill in the content
247 for (int index = 0; index < number_of_elements; index++) { 247 for (int index = 0; index < number_of_elements; index++) {
248 elms->set(index, args[index+1], mode); 248 elms->set(index, args[index+1], mode);
249 } 249 }
250 250
251 // Set length and elements on the array. 251 // Set length and elements on the array.
252 array->set_elements(FixedArray::cast(obj)); 252 array->set_elements(FixedArray::cast(obj));
253 array->set_length(len); 253 array->set_length(len);
254 254
255 return array; 255 return array;
256 } 256 }
257 257
258 258
259 MUST_USE_RESULT static MaybeObject* AllocateJSArray() { 259 MUST_USE_RESULT static MaybeObject* AllocateJSArray(Heap* heap) {
260 JSFunction* array_function = 260 JSFunction* array_function =
261 Top::context()->global_context()->array_function(); 261 heap->isolate()->context()->global_context()->array_function();
262 Object* result; 262 Object* result;
263 { MaybeObject* maybe_result = Heap::AllocateJSObject(array_function); 263 { MaybeObject* maybe_result = heap->AllocateJSObject(array_function);
264 if (!maybe_result->ToObject(&result)) return maybe_result; 264 if (!maybe_result->ToObject(&result)) return maybe_result;
265 } 265 }
266 return result; 266 return result;
267 } 267 }
268 268
269 269
270 MUST_USE_RESULT static MaybeObject* AllocateEmptyJSArray() { 270 MUST_USE_RESULT static MaybeObject* AllocateEmptyJSArray(Heap* heap) {
271 Object* result; 271 Object* result;
272 { MaybeObject* maybe_result = AllocateJSArray(); 272 { MaybeObject* maybe_result = AllocateJSArray(heap);
273 if (!maybe_result->ToObject(&result)) return maybe_result; 273 if (!maybe_result->ToObject(&result)) return maybe_result;
274 } 274 }
275 JSArray* result_array = JSArray::cast(result); 275 JSArray* result_array = JSArray::cast(result);
276 result_array->set_length(Smi::FromInt(0)); 276 result_array->set_length(Smi::FromInt(0));
277 result_array->set_elements(Heap::empty_fixed_array()); 277 result_array->set_elements(heap->empty_fixed_array());
278 return result_array; 278 return result_array;
279 } 279 }
280 280
281 281
282 static void CopyElements(AssertNoAllocation* no_gc, 282 static void CopyElements(Heap* heap,
283 AssertNoAllocation* no_gc,
283 FixedArray* dst, 284 FixedArray* dst,
284 int dst_index, 285 int dst_index,
285 FixedArray* src, 286 FixedArray* src,
286 int src_index, 287 int src_index,
287 int len) { 288 int len) {
288 ASSERT(dst != src); // Use MoveElements instead. 289 ASSERT(dst != src); // Use MoveElements instead.
289 ASSERT(dst->map() != Heap::fixed_cow_array_map()); 290 ASSERT(dst->map() != HEAP->fixed_cow_array_map());
290 ASSERT(len > 0); 291 ASSERT(len > 0);
291 CopyWords(dst->data_start() + dst_index, 292 CopyWords(dst->data_start() + dst_index,
292 src->data_start() + src_index, 293 src->data_start() + src_index,
293 len); 294 len);
294 WriteBarrierMode mode = dst->GetWriteBarrierMode(*no_gc); 295 WriteBarrierMode mode = dst->GetWriteBarrierMode(*no_gc);
295 if (mode == UPDATE_WRITE_BARRIER) { 296 if (mode == UPDATE_WRITE_BARRIER) {
296 Heap::RecordWrites(dst->address(), dst->OffsetOfElementAt(dst_index), len); 297 heap->RecordWrites(dst->address(), dst->OffsetOfElementAt(dst_index), len);
297 } 298 }
298 IncrementalMarking::RecordWrites(dst); 299 heap->incremental_marking()->RecordWrites(dst);
299 } 300 }
300 301
301 302
302 static void MoveElements(AssertNoAllocation* no_gc, 303 static void MoveElements(Heap* heap,
304 AssertNoAllocation* no_gc,
303 FixedArray* dst, 305 FixedArray* dst,
304 int dst_index, 306 int dst_index,
305 FixedArray* src, 307 FixedArray* src,
306 int src_index, 308 int src_index,
307 int len) { 309 int len) {
308 ASSERT(dst->map() != Heap::fixed_cow_array_map()); 310 ASSERT(dst->map() != HEAP->fixed_cow_array_map());
309 memmove(dst->data_start() + dst_index, 311 memmove(dst->data_start() + dst_index,
310 src->data_start() + src_index, 312 src->data_start() + src_index,
311 len * kPointerSize); 313 len * kPointerSize);
312 WriteBarrierMode mode = dst->GetWriteBarrierMode(*no_gc); 314 WriteBarrierMode mode = dst->GetWriteBarrierMode(*no_gc);
313 if (mode == UPDATE_WRITE_BARRIER) { 315 if (mode == UPDATE_WRITE_BARRIER) {
314 Heap::RecordWrites(dst->address(), dst->OffsetOfElementAt(dst_index), len); 316 heap->RecordWrites(dst->address(), dst->OffsetOfElementAt(dst_index), len);
315 } 317 }
316 IncrementalMarking::RecordWrites(dst); 318 heap->incremental_marking()->RecordWrites(dst);
317 } 319 }
318 320
319 321
320 static void FillWithHoles(FixedArray* dst, int from, int to) { 322 static void FillWithHoles(Heap* heap, FixedArray* dst, int from, int to) {
321 ASSERT(dst->map() != Heap::fixed_cow_array_map()); 323 ASSERT(dst->map() != heap->fixed_cow_array_map());
322 MemsetPointer(dst->data_start() + from, Heap::the_hole_value(), to - from); 324 MemsetPointer(dst->data_start() + from, heap->the_hole_value(), to - from);
323 } 325 }
324 326
325 327
326 static FixedArray* LeftTrimFixedArray(FixedArray* elms, int to_trim) { 328 static FixedArray* LeftTrimFixedArray(Heap* heap,
327 ASSERT(elms->map() != Heap::fixed_cow_array_map()); 329 FixedArray* elms,
330 int to_trim) {
331 ASSERT(elms->map() != HEAP->fixed_cow_array_map());
328 // For now this trick is only applied to fixed arrays in new and paged space. 332 // For now this trick is only applied to fixed arrays in new and paged space.
329 // In large object space the object's start must coincide with chunk 333 // In large object space the object's start must coincide with chunk
330 // and thus the trick is just not applicable. 334 // and thus the trick is just not applicable.
331 ASSERT(!Heap::lo_space()->Contains(elms)); 335 ASSERT(!HEAP->lo_space()->Contains(elms));
332 336
333 STATIC_ASSERT(FixedArray::kMapOffset == 0); 337 STATIC_ASSERT(FixedArray::kMapOffset == 0);
334 STATIC_ASSERT(FixedArray::kLengthOffset == kPointerSize); 338 STATIC_ASSERT(FixedArray::kLengthOffset == kPointerSize);
335 STATIC_ASSERT(FixedArray::kHeaderSize == 2 * kPointerSize); 339 STATIC_ASSERT(FixedArray::kHeaderSize == 2 * kPointerSize);
336 340
337 Object** former_start = HeapObject::RawField(elms, 0); 341 Object** former_start = HeapObject::RawField(elms, 0);
338 342
339 const int len = elms->length(); 343 const int len = elms->length();
340 344
341 if (to_trim > FixedArray::kHeaderSize / kPointerSize && 345 if (to_trim > FixedArray::kHeaderSize / kPointerSize &&
342 !Heap::new_space()->Contains(elms)) { 346 !heap->new_space()->Contains(elms)) {
343 // If we are doing a big trim in old space then we zap the space that was 347 // If we are doing a big trim in old space then we zap the space that was
344 // formerly part of the array so that the GC (aided by the card-based 348 // formerly part of the array so that the GC (aided by the card-based
345 // remembered set) won't find pointers to new-space there. 349 // remembered set) won't find pointers to new-space there.
346 Object** zap = reinterpret_cast<Object**>(elms->address()); 350 Object** zap = reinterpret_cast<Object**>(elms->address());
347 zap++; // Header of filler must be at least one word so skip that. 351 zap++; // Header of filler must be at least one word so skip that.
348 for (int i = 1; i < to_trim; i++) { 352 for (int i = 1; i < to_trim; i++) {
349 *zap++ = Smi::FromInt(0); 353 *zap++ = Smi::FromInt(0);
350 } 354 }
351 } 355 }
352 // Technically in new space this write might be omitted (except for 356 // Technically in new space this write might be omitted (except for
353 // debug mode which iterates through the heap), but to play safer 357 // debug mode which iterates through the heap), but to play safer
354 // we still do it. 358 // we still do it.
355 Heap::CreateFillerObjectAt(elms->address(), to_trim * kPointerSize); 359 heap->CreateFillerObjectAt(elms->address(), to_trim * kPointerSize);
356 360
357 former_start[to_trim] = Heap::fixed_array_map(); 361 former_start[to_trim] = heap->fixed_array_map();
358 former_start[to_trim + 1] = Smi::FromInt(len - to_trim); 362 former_start[to_trim + 1] = Smi::FromInt(len - to_trim);
359 363
360 // Maintain marking consistency for HeapObjectIterator and 364 // Maintain marking consistency for HeapObjectIterator and
361 // IncrementalMarking. 365 // IncrementalMarking.
362 Marking::TransferMark(elms->address(), 366 heap->marking()->TransferMark(elms->address(),
363 elms->address() + to_trim * kPointerSize); 367 elms->address() + to_trim * kPointerSize);
364 368
365 return FixedArray::cast(HeapObject::FromAddress( 369 return FixedArray::cast(HeapObject::FromAddress(
366 elms->address() + to_trim * kPointerSize)); 370 elms->address() + to_trim * kPointerSize));
367 } 371 }
368 372
369 373
370 static bool ArrayPrototypeHasNoElements(Context* global_context, 374 static bool ArrayPrototypeHasNoElements(Heap* heap,
375 Context* global_context,
371 JSObject* array_proto) { 376 JSObject* array_proto) {
372 // This method depends on non writability of Object and Array prototype 377 // This method depends on non writability of Object and Array prototype
373 // fields. 378 // fields.
374 if (array_proto->elements() != Heap::empty_fixed_array()) return false; 379 if (array_proto->elements() != heap->empty_fixed_array()) return false;
375 // Hidden prototype 380 // Hidden prototype
376 array_proto = JSObject::cast(array_proto->GetPrototype()); 381 array_proto = JSObject::cast(array_proto->GetPrototype());
377 ASSERT(array_proto->elements() == Heap::empty_fixed_array()); 382 ASSERT(array_proto->elements() == heap->empty_fixed_array());
378 // Object.prototype 383 // Object.prototype
379 Object* proto = array_proto->GetPrototype(); 384 Object* proto = array_proto->GetPrototype();
380 if (proto == Heap::null_value()) return false; 385 if (proto == heap->null_value()) return false;
381 array_proto = JSObject::cast(proto); 386 array_proto = JSObject::cast(proto);
382 if (array_proto != global_context->initial_object_prototype()) return false; 387 if (array_proto != global_context->initial_object_prototype()) return false;
383 if (array_proto->elements() != Heap::empty_fixed_array()) return false; 388 if (array_proto->elements() != heap->empty_fixed_array()) return false;
384 ASSERT(array_proto->GetPrototype()->IsNull()); 389 ASSERT(array_proto->GetPrototype()->IsNull());
385 return true; 390 return true;
386 } 391 }
387 392
388 393
389 MUST_USE_RESULT 394 MUST_USE_RESULT
390 static inline MaybeObject* EnsureJSArrayWithWritableFastElements( 395 static inline MaybeObject* EnsureJSArrayWithWritableFastElements(
391 Object* receiver) { 396 Heap* heap, Object* receiver) {
392 if (!receiver->IsJSArray()) return NULL; 397 if (!receiver->IsJSArray()) return NULL;
393 JSArray* array = JSArray::cast(receiver); 398 JSArray* array = JSArray::cast(receiver);
394 HeapObject* elms = array->elements(); 399 HeapObject* elms = array->elements();
395 if (elms->map() == Heap::fixed_array_map()) return elms; 400 if (elms->map() == heap->fixed_array_map()) return elms;
396 if (elms->map() == Heap::fixed_cow_array_map()) { 401 if (elms->map() == heap->fixed_cow_array_map()) {
397 return array->EnsureWritableFastElements(); 402 return array->EnsureWritableFastElements();
398 } 403 }
399 return NULL; 404 return NULL;
400 } 405 }
401 406
402 407
403 static inline bool IsJSArrayFastElementMovingAllowed(JSArray* receiver) { 408 static inline bool IsJSArrayFastElementMovingAllowed(Heap* heap,
404 Context* global_context = Top::context()->global_context(); 409 JSArray* receiver) {
410 Context* global_context = heap->isolate()->context()->global_context();
405 JSObject* array_proto = 411 JSObject* array_proto =
406 JSObject::cast(global_context->array_function()->prototype()); 412 JSObject::cast(global_context->array_function()->prototype());
407 return receiver->GetPrototype() == array_proto && 413 return receiver->GetPrototype() == array_proto &&
408 ArrayPrototypeHasNoElements(global_context, array_proto); 414 ArrayPrototypeHasNoElements(heap, global_context, array_proto);
409 } 415 }
410 416
411 417
412 MUST_USE_RESULT static MaybeObject* CallJsBuiltin( 418 MUST_USE_RESULT static MaybeObject* CallJsBuiltin(
419 Isolate* isolate,
413 const char* name, 420 const char* name,
414 BuiltinArguments<NO_EXTRA_ARGUMENTS> args) { 421 BuiltinArguments<NO_EXTRA_ARGUMENTS> args) {
415 HandleScope handleScope; 422 HandleScope handleScope(isolate);
416 423
417 Handle<Object> js_builtin = 424 Handle<Object> js_builtin =
418 GetProperty(Handle<JSObject>(Top::global_context()->builtins()), 425 GetProperty(Handle<JSObject>(
419 name); 426 isolate->global_context()->builtins()),
427 name);
420 ASSERT(js_builtin->IsJSFunction()); 428 ASSERT(js_builtin->IsJSFunction());
421 Handle<JSFunction> function(Handle<JSFunction>::cast(js_builtin)); 429 Handle<JSFunction> function(Handle<JSFunction>::cast(js_builtin));
422 ScopedVector<Object**> argv(args.length() - 1); 430 ScopedVector<Object**> argv(args.length() - 1);
423 int n_args = args.length() - 1; 431 int n_args = args.length() - 1;
424 for (int i = 0; i < n_args; i++) { 432 for (int i = 0; i < n_args; i++) {
425 argv[i] = args.at<Object>(i + 1).location(); 433 argv[i] = args.at<Object>(i + 1).location();
426 } 434 }
427 bool pending_exception = false; 435 bool pending_exception = false;
428 Handle<Object> result = Execution::Call(function, 436 Handle<Object> result = Execution::Call(function,
429 args.receiver(), 437 args.receiver(),
430 n_args, 438 n_args,
431 argv.start(), 439 argv.start(),
432 &pending_exception); 440 &pending_exception);
433 if (pending_exception) return Failure::Exception(); 441 if (pending_exception) return Failure::Exception();
434 return *result; 442 return *result;
435 } 443 }
436 444
437 445
438 BUILTIN(ArrayPush) { 446 BUILTIN(ArrayPush) {
447 Heap* heap = isolate->heap();
439 Object* receiver = *args.receiver(); 448 Object* receiver = *args.receiver();
440 Object* elms_obj; 449 Object* elms_obj;
441 { MaybeObject* maybe_elms_obj = 450 { MaybeObject* maybe_elms_obj =
442 EnsureJSArrayWithWritableFastElements(receiver); 451 EnsureJSArrayWithWritableFastElements(heap, receiver);
443 if (maybe_elms_obj == NULL) return CallJsBuiltin("ArrayPush", args); 452 if (maybe_elms_obj == NULL) {
453 return CallJsBuiltin(isolate, "ArrayPush", args);
454 }
444 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj; 455 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
445 } 456 }
446 FixedArray* elms = FixedArray::cast(elms_obj); 457 FixedArray* elms = FixedArray::cast(elms_obj);
447 JSArray* array = JSArray::cast(receiver); 458 JSArray* array = JSArray::cast(receiver);
448 459
449 int len = Smi::cast(array->length())->value(); 460 int len = Smi::cast(array->length())->value();
450 int to_add = args.length() - 1; 461 int to_add = args.length() - 1;
451 if (to_add == 0) { 462 if (to_add == 0) {
452 return Smi::FromInt(len); 463 return Smi::FromInt(len);
453 } 464 }
454 // Currently fixed arrays cannot grow too big, so 465 // Currently fixed arrays cannot grow too big, so
455 // we should never hit this case. 466 // we should never hit this case.
456 ASSERT(to_add <= (Smi::kMaxValue - len)); 467 ASSERT(to_add <= (Smi::kMaxValue - len));
457 468
458 int new_length = len + to_add; 469 int new_length = len + to_add;
459 470
460 if (new_length > elms->length()) { 471 if (new_length > elms->length()) {
461 // New backing storage is needed. 472 // New backing storage is needed.
462 int capacity = new_length + (new_length >> 1) + 16; 473 int capacity = new_length + (new_length >> 1) + 16;
463 Object* obj; 474 Object* obj;
464 { MaybeObject* maybe_obj = Heap::AllocateUninitializedFixedArray(capacity); 475 { MaybeObject* maybe_obj = heap->AllocateUninitializedFixedArray(capacity);
465 if (!maybe_obj->ToObject(&obj)) return maybe_obj; 476 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
466 } 477 }
467 FixedArray* new_elms = FixedArray::cast(obj); 478 FixedArray* new_elms = FixedArray::cast(obj);
468 479
469 AssertNoAllocation no_gc; 480 AssertNoAllocation no_gc;
470 if (len > 0) { 481 if (len > 0) {
471 CopyElements(&no_gc, new_elms, 0, elms, 0, len); 482 CopyElements(heap, &no_gc, new_elms, 0, elms, 0, len);
472 } 483 }
473 FillWithHoles(new_elms, new_length, capacity); 484 FillWithHoles(heap, new_elms, new_length, capacity);
474 485
475 elms = new_elms; 486 elms = new_elms;
476 array->set_elements(elms); 487 array->set_elements(elms);
477 } 488 }
478 489
479 // Add the provided values. 490 // Add the provided values.
480 AssertNoAllocation no_gc; 491 AssertNoAllocation no_gc;
481 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc); 492 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
482 for (int index = 0; index < to_add; index++) { 493 for (int index = 0; index < to_add; index++) {
483 elms->set(index + len, args[index + 1], mode); 494 elms->set(index + len, args[index + 1], mode);
484 } 495 }
485 496
486 // Set the length. 497 // Set the length.
487 array->set_length(Smi::FromInt(new_length)); 498 array->set_length(Smi::FromInt(new_length));
488 return Smi::FromInt(new_length); 499 return Smi::FromInt(new_length);
489 } 500 }
490 501
491 502
492 BUILTIN(ArrayPop) { 503 BUILTIN(ArrayPop) {
504 Heap* heap = isolate->heap();
493 Object* receiver = *args.receiver(); 505 Object* receiver = *args.receiver();
494 Object* elms_obj; 506 Object* elms_obj;
495 { MaybeObject* maybe_elms_obj = 507 { MaybeObject* maybe_elms_obj =
496 EnsureJSArrayWithWritableFastElements(receiver); 508 EnsureJSArrayWithWritableFastElements(heap, receiver);
497 if (maybe_elms_obj == NULL) return CallJsBuiltin("ArrayPop", args); 509 if (maybe_elms_obj == NULL) return CallJsBuiltin(isolate, "ArrayPop", args);
498 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj; 510 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
499 } 511 }
500 FixedArray* elms = FixedArray::cast(elms_obj); 512 FixedArray* elms = FixedArray::cast(elms_obj);
501 JSArray* array = JSArray::cast(receiver); 513 JSArray* array = JSArray::cast(receiver);
502 514
503 int len = Smi::cast(array->length())->value(); 515 int len = Smi::cast(array->length())->value();
504 if (len == 0) return Heap::undefined_value(); 516 if (len == 0) return heap->undefined_value();
505 517
506 // Get top element 518 // Get top element
507 MaybeObject* top = elms->get(len - 1); 519 MaybeObject* top = elms->get(len - 1);
508 520
509 // Set the length. 521 // Set the length.
510 array->set_length(Smi::FromInt(len - 1)); 522 array->set_length(Smi::FromInt(len - 1));
511 523
512 if (!top->IsTheHole()) { 524 if (!top->IsTheHole()) {
513 // Delete the top element. 525 // Delete the top element.
514 elms->set_the_hole(len - 1); 526 elms->set_the_hole(len - 1);
515 return top; 527 return top;
516 } 528 }
517 529
518 top = array->GetPrototype()->GetElement(len - 1); 530 top = array->GetPrototype()->GetElement(len - 1);
519 531
520 return top; 532 return top;
521 } 533 }
522 534
523 535
524 BUILTIN(ArrayShift) { 536 BUILTIN(ArrayShift) {
537 Heap* heap = isolate->heap();
525 Object* receiver = *args.receiver(); 538 Object* receiver = *args.receiver();
526 Object* elms_obj; 539 Object* elms_obj;
527 { MaybeObject* maybe_elms_obj = 540 { MaybeObject* maybe_elms_obj =
528 EnsureJSArrayWithWritableFastElements(receiver); 541 EnsureJSArrayWithWritableFastElements(heap, receiver);
529 if (maybe_elms_obj == NULL) return CallJsBuiltin("ArrayShift", args); 542 if (maybe_elms_obj == NULL)
543 return CallJsBuiltin(isolate, "ArrayShift", args);
530 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj; 544 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
531 } 545 }
532 if (!IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) { 546 if (!IsJSArrayFastElementMovingAllowed(heap, JSArray::cast(receiver))) {
533 return CallJsBuiltin("ArrayShift", args); 547 return CallJsBuiltin(isolate, "ArrayShift", args);
534 } 548 }
535 FixedArray* elms = FixedArray::cast(elms_obj); 549 FixedArray* elms = FixedArray::cast(elms_obj);
536 JSArray* array = JSArray::cast(receiver); 550 JSArray* array = JSArray::cast(receiver);
537 ASSERT(array->HasFastElements()); 551 ASSERT(array->HasFastElements());
538 552
539 int len = Smi::cast(array->length())->value(); 553 int len = Smi::cast(array->length())->value();
540 if (len == 0) return Heap::undefined_value(); 554 if (len == 0) return heap->undefined_value();
541 555
542 // Get first element 556 // Get first element
543 Object* first = elms->get(0); 557 Object* first = elms->get(0);
544 if (first->IsTheHole()) { 558 if (first->IsTheHole()) {
545 first = Heap::undefined_value(); 559 first = heap->undefined_value();
546 } 560 }
547 561
548 if (!Heap::lo_space()->Contains(elms)) { 562 if (!heap->lo_space()->Contains(elms)) {
549 // As elms still in the same space they used to be, 563 // As elms still in the same space they used to be,
550 // there is no need to update region dirty mark. 564 // there is no need to update region dirty mark.
551 array->set_elements(LeftTrimFixedArray(elms, 1), SKIP_WRITE_BARRIER); 565 array->set_elements(LeftTrimFixedArray(heap, elms, 1), SKIP_WRITE_BARRIER);
552 } else { 566 } else {
553 // Shift the elements. 567 // Shift the elements.
554 AssertNoAllocation no_gc; 568 AssertNoAllocation no_gc;
555 MoveElements(&no_gc, elms, 0, elms, 1, len - 1); 569 MoveElements(heap, &no_gc, elms, 0, elms, 1, len - 1);
556 elms->set(len - 1, Heap::the_hole_value()); 570 elms->set(len - 1, heap->the_hole_value());
557 } 571 }
558 572
559 // Set the length. 573 // Set the length.
560 array->set_length(Smi::FromInt(len - 1)); 574 array->set_length(Smi::FromInt(len - 1));
561 575
562 return first; 576 return first;
563 } 577 }
564 578
565 579
566 BUILTIN(ArrayUnshift) { 580 BUILTIN(ArrayUnshift) {
581 Heap* heap = isolate->heap();
567 Object* receiver = *args.receiver(); 582 Object* receiver = *args.receiver();
568 Object* elms_obj; 583 Object* elms_obj;
569 { MaybeObject* maybe_elms_obj = 584 { MaybeObject* maybe_elms_obj =
570 EnsureJSArrayWithWritableFastElements(receiver); 585 EnsureJSArrayWithWritableFastElements(heap, receiver);
571 if (maybe_elms_obj == NULL) return CallJsBuiltin("ArrayUnshift", args); 586 if (maybe_elms_obj == NULL)
587 return CallJsBuiltin(isolate, "ArrayUnshift", args);
572 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj; 588 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
573 } 589 }
574 if (!IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) { 590 if (!IsJSArrayFastElementMovingAllowed(heap, JSArray::cast(receiver))) {
575 return CallJsBuiltin("ArrayUnshift", args); 591 return CallJsBuiltin(isolate, "ArrayUnshift", args);
576 } 592 }
577 FixedArray* elms = FixedArray::cast(elms_obj); 593 FixedArray* elms = FixedArray::cast(elms_obj);
578 JSArray* array = JSArray::cast(receiver); 594 JSArray* array = JSArray::cast(receiver);
579 ASSERT(array->HasFastElements()); 595 ASSERT(array->HasFastElements());
580 596
581 int len = Smi::cast(array->length())->value(); 597 int len = Smi::cast(array->length())->value();
582 int to_add = args.length() - 1; 598 int to_add = args.length() - 1;
583 int new_length = len + to_add; 599 int new_length = len + to_add;
584 // Currently fixed arrays cannot grow too big, so 600 // Currently fixed arrays cannot grow too big, so
585 // we should never hit this case. 601 // we should never hit this case.
586 ASSERT(to_add <= (Smi::kMaxValue - len)); 602 ASSERT(to_add <= (Smi::kMaxValue - len));
587 603
588 if (new_length > elms->length()) { 604 if (new_length > elms->length()) {
589 // New backing storage is needed. 605 // New backing storage is needed.
590 int capacity = new_length + (new_length >> 1) + 16; 606 int capacity = new_length + (new_length >> 1) + 16;
591 Object* obj; 607 Object* obj;
592 { MaybeObject* maybe_obj = Heap::AllocateUninitializedFixedArray(capacity); 608 { MaybeObject* maybe_obj = heap->AllocateUninitializedFixedArray(capacity);
593 if (!maybe_obj->ToObject(&obj)) return maybe_obj; 609 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
594 } 610 }
595 FixedArray* new_elms = FixedArray::cast(obj); 611 FixedArray* new_elms = FixedArray::cast(obj);
596 612
597 AssertNoAllocation no_gc; 613 AssertNoAllocation no_gc;
598 if (len > 0) { 614 if (len > 0) {
599 CopyElements(&no_gc, new_elms, to_add, elms, 0, len); 615 CopyElements(heap, &no_gc, new_elms, to_add, elms, 0, len);
600 } 616 }
601 FillWithHoles(new_elms, new_length, capacity); 617 FillWithHoles(heap, new_elms, new_length, capacity);
602 618
603 elms = new_elms; 619 elms = new_elms;
604 array->set_elements(elms); 620 array->set_elements(elms);
605 } else { 621 } else {
606 AssertNoAllocation no_gc; 622 AssertNoAllocation no_gc;
607 MoveElements(&no_gc, elms, to_add, elms, 0, len); 623 MoveElements(heap, &no_gc, elms, to_add, elms, 0, len);
608 } 624 }
609 625
610 // Add the provided values. 626 // Add the provided values.
611 AssertNoAllocation no_gc; 627 AssertNoAllocation no_gc;
612 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc); 628 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
613 for (int i = 0; i < to_add; i++) { 629 for (int i = 0; i < to_add; i++) {
614 elms->set(i, args[i + 1], mode); 630 elms->set(i, args[i + 1], mode);
615 } 631 }
616 632
617 // Set the length. 633 // Set the length.
618 array->set_length(Smi::FromInt(new_length)); 634 array->set_length(Smi::FromInt(new_length));
619 return Smi::FromInt(new_length); 635 return Smi::FromInt(new_length);
620 } 636 }
621 637
622 638
623 BUILTIN(ArraySlice) { 639 BUILTIN(ArraySlice) {
640 Heap* heap = isolate->heap();
624 Object* receiver = *args.receiver(); 641 Object* receiver = *args.receiver();
625 FixedArray* elms; 642 FixedArray* elms;
626 int len = -1; 643 int len = -1;
627 if (receiver->IsJSArray()) { 644 if (receiver->IsJSArray()) {
628 JSArray* array = JSArray::cast(receiver); 645 JSArray* array = JSArray::cast(receiver);
629 if (!array->HasFastElements() || 646 if (!array->HasFastElements() ||
630 !IsJSArrayFastElementMovingAllowed(array)) { 647 !IsJSArrayFastElementMovingAllowed(heap, array)) {
631 return CallJsBuiltin("ArraySlice", args); 648 return CallJsBuiltin(isolate, "ArraySlice", args);
632 } 649 }
633 650
634 elms = FixedArray::cast(array->elements()); 651 elms = FixedArray::cast(array->elements());
635 len = Smi::cast(array->length())->value(); 652 len = Smi::cast(array->length())->value();
636 } else { 653 } else {
637 // Array.slice(arguments, ...) is quite a common idiom (notably more 654 // Array.slice(arguments, ...) is quite a common idiom (notably more
638 // than 50% of invocations in Web apps). Treat it in C++ as well. 655 // than 50% of invocations in Web apps). Treat it in C++ as well.
639 Map* arguments_map = 656 Map* arguments_map =
640 Top::context()->global_context()->arguments_boilerplate()->map(); 657 isolate->context()->global_context()->arguments_boilerplate()->map();
641 658
642 bool is_arguments_object_with_fast_elements = 659 bool is_arguments_object_with_fast_elements =
643 receiver->IsJSObject() 660 receiver->IsJSObject()
644 && JSObject::cast(receiver)->map() == arguments_map 661 && JSObject::cast(receiver)->map() == arguments_map
645 && JSObject::cast(receiver)->HasFastElements(); 662 && JSObject::cast(receiver)->HasFastElements();
646 if (!is_arguments_object_with_fast_elements) { 663 if (!is_arguments_object_with_fast_elements) {
647 return CallJsBuiltin("ArraySlice", args); 664 return CallJsBuiltin(isolate, "ArraySlice", args);
648 } 665 }
649 elms = FixedArray::cast(JSObject::cast(receiver)->elements()); 666 elms = FixedArray::cast(JSObject::cast(receiver)->elements());
650 Object* len_obj = JSObject::cast(receiver) 667 Object* len_obj = JSObject::cast(receiver)
651 ->InObjectPropertyAt(Heap::kArgumentsLengthIndex); 668 ->InObjectPropertyAt(Heap::kArgumentsLengthIndex);
652 if (!len_obj->IsSmi()) { 669 if (!len_obj->IsSmi()) {
653 return CallJsBuiltin("ArraySlice", args); 670 return CallJsBuiltin(isolate, "ArraySlice", args);
654 } 671 }
655 len = Smi::cast(len_obj)->value(); 672 len = Smi::cast(len_obj)->value();
656 if (len > elms->length()) { 673 if (len > elms->length()) {
657 return CallJsBuiltin("ArraySlice", args); 674 return CallJsBuiltin(isolate, "ArraySlice", args);
658 } 675 }
659 for (int i = 0; i < len; i++) { 676 for (int i = 0; i < len; i++) {
660 if (elms->get(i) == Heap::the_hole_value()) { 677 if (elms->get(i) == heap->the_hole_value()) {
661 return CallJsBuiltin("ArraySlice", args); 678 return CallJsBuiltin(isolate, "ArraySlice", args);
662 } 679 }
663 } 680 }
664 } 681 }
665 ASSERT(len >= 0); 682 ASSERT(len >= 0);
666 int n_arguments = args.length() - 1; 683 int n_arguments = args.length() - 1;
667 684
668 // Note carefully choosen defaults---if argument is missing, 685 // Note carefully choosen defaults---if argument is missing,
669 // it's undefined which gets converted to 0 for relative_start 686 // it's undefined which gets converted to 0 for relative_start
670 // and to len for relative_end. 687 // and to len for relative_end.
671 int relative_start = 0; 688 int relative_start = 0;
672 int relative_end = len; 689 int relative_end = len;
673 if (n_arguments > 0) { 690 if (n_arguments > 0) {
674 Object* arg1 = args[1]; 691 Object* arg1 = args[1];
675 if (arg1->IsSmi()) { 692 if (arg1->IsSmi()) {
676 relative_start = Smi::cast(arg1)->value(); 693 relative_start = Smi::cast(arg1)->value();
677 } else if (!arg1->IsUndefined()) { 694 } else if (!arg1->IsUndefined()) {
678 return CallJsBuiltin("ArraySlice", args); 695 return CallJsBuiltin(isolate, "ArraySlice", args);
679 } 696 }
680 if (n_arguments > 1) { 697 if (n_arguments > 1) {
681 Object* arg2 = args[2]; 698 Object* arg2 = args[2];
682 if (arg2->IsSmi()) { 699 if (arg2->IsSmi()) {
683 relative_end = Smi::cast(arg2)->value(); 700 relative_end = Smi::cast(arg2)->value();
684 } else if (!arg2->IsUndefined()) { 701 } else if (!arg2->IsUndefined()) {
685 return CallJsBuiltin("ArraySlice", args); 702 return CallJsBuiltin(isolate, "ArraySlice", args);
686 } 703 }
687 } 704 }
688 } 705 }
689 706
690 // ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 6. 707 // ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 6.
691 int k = (relative_start < 0) ? Max(len + relative_start, 0) 708 int k = (relative_start < 0) ? Max(len + relative_start, 0)
692 : Min(relative_start, len); 709 : Min(relative_start, len);
693 710
694 // ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 8. 711 // ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 8.
695 int final = (relative_end < 0) ? Max(len + relative_end, 0) 712 int final = (relative_end < 0) ? Max(len + relative_end, 0)
696 : Min(relative_end, len); 713 : Min(relative_end, len);
697 714
698 // Calculate the length of result array. 715 // Calculate the length of result array.
699 int result_len = final - k; 716 int result_len = final - k;
700 if (result_len <= 0) { 717 if (result_len <= 0) {
701 return AllocateEmptyJSArray(); 718 return AllocateEmptyJSArray(heap);
702 } 719 }
703 720
704 Object* result; 721 Object* result;
705 { MaybeObject* maybe_result = AllocateJSArray(); 722 { MaybeObject* maybe_result = AllocateJSArray(heap);
706 if (!maybe_result->ToObject(&result)) return maybe_result; 723 if (!maybe_result->ToObject(&result)) return maybe_result;
707 } 724 }
708 JSArray* result_array = JSArray::cast(result); 725 JSArray* result_array = JSArray::cast(result);
709 726
710 { MaybeObject* maybe_result = 727 { MaybeObject* maybe_result =
711 Heap::AllocateUninitializedFixedArray(result_len); 728 heap->AllocateUninitializedFixedArray(result_len);
712 if (!maybe_result->ToObject(&result)) return maybe_result; 729 if (!maybe_result->ToObject(&result)) return maybe_result;
713 } 730 }
714 FixedArray* result_elms = FixedArray::cast(result); 731 FixedArray* result_elms = FixedArray::cast(result);
715 732
716 AssertNoAllocation no_gc; 733 AssertNoAllocation no_gc;
717 CopyElements(&no_gc, result_elms, 0, elms, k, result_len); 734 CopyElements(heap, &no_gc, result_elms, 0, elms, k, result_len);
718 735
719 // Set elements. 736 // Set elements.
720 result_array->set_elements(result_elms); 737 result_array->set_elements(result_elms);
721 738
722 // Set the length. 739 // Set the length.
723 result_array->set_length(Smi::FromInt(result_len)); 740 result_array->set_length(Smi::FromInt(result_len));
724 return result_array; 741 return result_array;
725 } 742 }
726 743
727 744
728 BUILTIN(ArraySplice) { 745 BUILTIN(ArraySplice) {
746 Heap* heap = isolate->heap();
729 Object* receiver = *args.receiver(); 747 Object* receiver = *args.receiver();
730 Object* elms_obj; 748 Object* elms_obj;
731 { MaybeObject* maybe_elms_obj = 749 { MaybeObject* maybe_elms_obj =
732 EnsureJSArrayWithWritableFastElements(receiver); 750 EnsureJSArrayWithWritableFastElements(heap, receiver);
733 if (maybe_elms_obj == NULL) return CallJsBuiltin("ArraySplice", args); 751 if (maybe_elms_obj == NULL)
752 return CallJsBuiltin(isolate, "ArraySplice", args);
734 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj; 753 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
735 } 754 }
736 if (!IsJSArrayFastElementMovingAllowed(JSArray::cast(receiver))) { 755 if (!IsJSArrayFastElementMovingAllowed(heap, JSArray::cast(receiver))) {
737 return CallJsBuiltin("ArraySplice", args); 756 return CallJsBuiltin(isolate, "ArraySplice", args);
738 } 757 }
739 FixedArray* elms = FixedArray::cast(elms_obj); 758 FixedArray* elms = FixedArray::cast(elms_obj);
740 JSArray* array = JSArray::cast(receiver); 759 JSArray* array = JSArray::cast(receiver);
741 ASSERT(array->HasFastElements()); 760 ASSERT(array->HasFastElements());
742 761
743 int len = Smi::cast(array->length())->value(); 762 int len = Smi::cast(array->length())->value();
744 763
745 int n_arguments = args.length() - 1; 764 int n_arguments = args.length() - 1;
746 765
747 int relative_start = 0; 766 int relative_start = 0;
748 if (n_arguments > 0) { 767 if (n_arguments > 0) {
749 Object* arg1 = args[1]; 768 Object* arg1 = args[1];
750 if (arg1->IsSmi()) { 769 if (arg1->IsSmi()) {
751 relative_start = Smi::cast(arg1)->value(); 770 relative_start = Smi::cast(arg1)->value();
752 } else if (!arg1->IsUndefined()) { 771 } else if (!arg1->IsUndefined()) {
753 return CallJsBuiltin("ArraySplice", args); 772 return CallJsBuiltin(isolate, "ArraySplice", args);
754 } 773 }
755 } 774 }
756 int actual_start = (relative_start < 0) ? Max(len + relative_start, 0) 775 int actual_start = (relative_start < 0) ? Max(len + relative_start, 0)
757 : Min(relative_start, len); 776 : Min(relative_start, len);
758 777
759 // SpiderMonkey, TraceMonkey and JSC treat the case where no delete count is 778 // SpiderMonkey, TraceMonkey and JSC treat the case where no delete count is
760 // given as a request to delete all the elements from the start. 779 // given as a request to delete all the elements from the start.
761 // And it differs from the case of undefined delete count. 780 // And it differs from the case of undefined delete count.
762 // This does not follow ECMA-262, but we do the same for 781 // This does not follow ECMA-262, but we do the same for
763 // compatibility. 782 // compatibility.
764 int actual_delete_count; 783 int actual_delete_count;
765 if (n_arguments == 1) { 784 if (n_arguments == 1) {
766 ASSERT(len - actual_start >= 0); 785 ASSERT(len - actual_start >= 0);
767 actual_delete_count = len - actual_start; 786 actual_delete_count = len - actual_start;
768 } else { 787 } else {
769 int value = 0; // ToInteger(undefined) == 0 788 int value = 0; // ToInteger(undefined) == 0
770 if (n_arguments > 1) { 789 if (n_arguments > 1) {
771 Object* arg2 = args[2]; 790 Object* arg2 = args[2];
772 if (arg2->IsSmi()) { 791 if (arg2->IsSmi()) {
773 value = Smi::cast(arg2)->value(); 792 value = Smi::cast(arg2)->value();
774 } else { 793 } else {
775 return CallJsBuiltin("ArraySplice", args); 794 return CallJsBuiltin(isolate, "ArraySplice", args);
776 } 795 }
777 } 796 }
778 actual_delete_count = Min(Max(value, 0), len - actual_start); 797 actual_delete_count = Min(Max(value, 0), len - actual_start);
779 } 798 }
780 799
781 JSArray* result_array = NULL; 800 JSArray* result_array = NULL;
782 if (actual_delete_count == 0) { 801 if (actual_delete_count == 0) {
783 Object* result; 802 Object* result;
784 { MaybeObject* maybe_result = AllocateEmptyJSArray(); 803 { MaybeObject* maybe_result = AllocateEmptyJSArray(heap);
785 if (!maybe_result->ToObject(&result)) return maybe_result; 804 if (!maybe_result->ToObject(&result)) return maybe_result;
786 } 805 }
787 result_array = JSArray::cast(result); 806 result_array = JSArray::cast(result);
788 } else { 807 } else {
789 // Allocate result array. 808 // Allocate result array.
790 Object* result; 809 Object* result;
791 { MaybeObject* maybe_result = AllocateJSArray(); 810 { MaybeObject* maybe_result = AllocateJSArray(heap);
792 if (!maybe_result->ToObject(&result)) return maybe_result; 811 if (!maybe_result->ToObject(&result)) return maybe_result;
793 } 812 }
794 result_array = JSArray::cast(result); 813 result_array = JSArray::cast(result);
795 814
796 { MaybeObject* maybe_result = 815 { MaybeObject* maybe_result =
797 Heap::AllocateUninitializedFixedArray(actual_delete_count); 816 heap->AllocateUninitializedFixedArray(actual_delete_count);
798 if (!maybe_result->ToObject(&result)) return maybe_result; 817 if (!maybe_result->ToObject(&result)) return maybe_result;
799 } 818 }
800 FixedArray* result_elms = FixedArray::cast(result); 819 FixedArray* result_elms = FixedArray::cast(result);
801 820
802 AssertNoAllocation no_gc; 821 AssertNoAllocation no_gc;
803 // Fill newly created array. 822 // Fill newly created array.
804 CopyElements(&no_gc, 823 CopyElements(heap,
824 &no_gc,
805 result_elms, 0, 825 result_elms, 0,
806 elms, actual_start, 826 elms, actual_start,
807 actual_delete_count); 827 actual_delete_count);
808 828
809 // Set elements. 829 // Set elements.
810 result_array->set_elements(result_elms); 830 result_array->set_elements(result_elms);
811 831
812 // Set the length. 832 // Set the length.
813 result_array->set_length(Smi::FromInt(actual_delete_count)); 833 result_array->set_length(Smi::FromInt(actual_delete_count));
814 } 834 }
815 835
816 int item_count = (n_arguments > 1) ? (n_arguments - 2) : 0; 836 int item_count = (n_arguments > 1) ? (n_arguments - 2) : 0;
817 837
818 int new_length = len - actual_delete_count + item_count; 838 int new_length = len - actual_delete_count + item_count;
819 839
820 if (item_count < actual_delete_count) { 840 if (item_count < actual_delete_count) {
821 // Shrink the array. 841 // Shrink the array.
822 const bool trim_array = !Heap::lo_space()->Contains(elms) && 842 const bool trim_array = !heap->lo_space()->Contains(elms) &&
823 ((actual_start + item_count) < 843 ((actual_start + item_count) <
824 (len - actual_delete_count - actual_start)); 844 (len - actual_delete_count - actual_start));
825 if (trim_array) { 845 if (trim_array) {
826 const int delta = actual_delete_count - item_count; 846 const int delta = actual_delete_count - item_count;
827 847
828 if (actual_start > 0) { 848 if (actual_start > 0) {
829 Object** start = elms->data_start(); 849 Object** start = elms->data_start();
830 memmove(start + delta, start, actual_start * kPointerSize); 850 memmove(start + delta, start, actual_start * kPointerSize);
831 } 851 }
832 852
833 elms = LeftTrimFixedArray(elms, delta); 853 elms = LeftTrimFixedArray(heap, elms, delta);
834 array->set_elements(elms, SKIP_WRITE_BARRIER); 854 array->set_elements(elms, SKIP_WRITE_BARRIER);
835 } else { 855 } else {
836 AssertNoAllocation no_gc; 856 AssertNoAllocation no_gc;
837 MoveElements(&no_gc, 857 MoveElements(heap, &no_gc,
838 elms, actual_start + item_count, 858 elms, actual_start + item_count,
839 elms, actual_start + actual_delete_count, 859 elms, actual_start + actual_delete_count,
840 (len - actual_delete_count - actual_start)); 860 (len - actual_delete_count - actual_start));
841 FillWithHoles(elms, new_length, len); 861 FillWithHoles(heap, elms, new_length, len);
842 } 862 }
843 } else if (item_count > actual_delete_count) { 863 } else if (item_count > actual_delete_count) {
844 // Currently fixed arrays cannot grow too big, so 864 // Currently fixed arrays cannot grow too big, so
845 // we should never hit this case. 865 // we should never hit this case.
846 ASSERT((item_count - actual_delete_count) <= (Smi::kMaxValue - len)); 866 ASSERT((item_count - actual_delete_count) <= (Smi::kMaxValue - len));
847 867
848 // Check if array need to grow. 868 // Check if array need to grow.
849 if (new_length > elms->length()) { 869 if (new_length > elms->length()) {
850 // New backing storage is needed. 870 // New backing storage is needed.
851 int capacity = new_length + (new_length >> 1) + 16; 871 int capacity = new_length + (new_length >> 1) + 16;
852 Object* obj; 872 Object* obj;
853 { MaybeObject* maybe_obj = 873 { MaybeObject* maybe_obj =
854 Heap::AllocateUninitializedFixedArray(capacity); 874 heap->AllocateUninitializedFixedArray(capacity);
855 if (!maybe_obj->ToObject(&obj)) return maybe_obj; 875 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
856 } 876 }
857 FixedArray* new_elms = FixedArray::cast(obj); 877 FixedArray* new_elms = FixedArray::cast(obj);
858 878
859 AssertNoAllocation no_gc; 879 AssertNoAllocation no_gc;
860 // Copy the part before actual_start as is. 880 // Copy the part before actual_start as is.
861 if (actual_start > 0) { 881 if (actual_start > 0) {
862 CopyElements(&no_gc, new_elms, 0, elms, 0, actual_start); 882 CopyElements(heap, &no_gc, new_elms, 0, elms, 0, actual_start);
863 } 883 }
864 const int to_copy = len - actual_delete_count - actual_start; 884 const int to_copy = len - actual_delete_count - actual_start;
865 if (to_copy > 0) { 885 if (to_copy > 0) {
866 CopyElements(&no_gc, 886 CopyElements(heap, &no_gc,
867 new_elms, actual_start + item_count, 887 new_elms, actual_start + item_count,
868 elms, actual_start + actual_delete_count, 888 elms, actual_start + actual_delete_count,
869 to_copy); 889 to_copy);
870 } 890 }
871 FillWithHoles(new_elms, new_length, capacity); 891 FillWithHoles(heap, new_elms, new_length, capacity);
872 892
873 elms = new_elms; 893 elms = new_elms;
874 array->set_elements(elms); 894 array->set_elements(elms);
875 } else { 895 } else {
876 AssertNoAllocation no_gc; 896 AssertNoAllocation no_gc;
877 MoveElements(&no_gc, 897 MoveElements(heap, &no_gc,
878 elms, actual_start + item_count, 898 elms, actual_start + item_count,
879 elms, actual_start + actual_delete_count, 899 elms, actual_start + actual_delete_count,
880 (len - actual_delete_count - actual_start)); 900 (len - actual_delete_count - actual_start));
881 } 901 }
882 } 902 }
883 903
884 AssertNoAllocation no_gc; 904 AssertNoAllocation no_gc;
885 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc); 905 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
886 for (int k = actual_start; k < actual_start + item_count; k++) { 906 for (int k = actual_start; k < actual_start + item_count; k++) {
887 elms->set(k, args[3 + k - actual_start], mode); 907 elms->set(k, args[3 + k - actual_start], mode);
888 } 908 }
889 909
890 // Set the length. 910 // Set the length.
891 array->set_length(Smi::FromInt(new_length)); 911 array->set_length(Smi::FromInt(new_length));
892 912
893 return result_array; 913 return result_array;
894 } 914 }
895 915
896 916
897 BUILTIN(ArrayConcat) { 917 BUILTIN(ArrayConcat) {
898 Context* global_context = Top::context()->global_context(); 918 Heap* heap = isolate->heap();
919 Context* global_context = isolate->context()->global_context();
899 JSObject* array_proto = 920 JSObject* array_proto =
900 JSObject::cast(global_context->array_function()->prototype()); 921 JSObject::cast(global_context->array_function()->prototype());
901 if (!ArrayPrototypeHasNoElements(global_context, array_proto)) { 922 if (!ArrayPrototypeHasNoElements(heap, global_context, array_proto)) {
902 return CallJsBuiltin("ArrayConcat", args); 923 return CallJsBuiltin(isolate, "ArrayConcat", args);
903 } 924 }
904 925
905 // Iterate through all the arguments performing checks 926 // Iterate through all the arguments performing checks
906 // and calculating total length. 927 // and calculating total length.
907 int n_arguments = args.length(); 928 int n_arguments = args.length();
908 int result_len = 0; 929 int result_len = 0;
909 for (int i = 0; i < n_arguments; i++) { 930 for (int i = 0; i < n_arguments; i++) {
910 Object* arg = args[i]; 931 Object* arg = args[i];
911 if (!arg->IsJSArray() || !JSArray::cast(arg)->HasFastElements() 932 if (!arg->IsJSArray() || !JSArray::cast(arg)->HasFastElements()
912 || JSArray::cast(arg)->GetPrototype() != array_proto) { 933 || JSArray::cast(arg)->GetPrototype() != array_proto) {
913 return CallJsBuiltin("ArrayConcat", args); 934 return CallJsBuiltin(isolate, "ArrayConcat", args);
914 } 935 }
915 936
916 int len = Smi::cast(JSArray::cast(arg)->length())->value(); 937 int len = Smi::cast(JSArray::cast(arg)->length())->value();
917 938
918 // We shouldn't overflow when adding another len. 939 // We shouldn't overflow when adding another len.
919 const int kHalfOfMaxInt = 1 << (kBitsPerInt - 2); 940 const int kHalfOfMaxInt = 1 << (kBitsPerInt - 2);
920 STATIC_ASSERT(FixedArray::kMaxLength < kHalfOfMaxInt); 941 STATIC_ASSERT(FixedArray::kMaxLength < kHalfOfMaxInt);
921 USE(kHalfOfMaxInt); 942 USE(kHalfOfMaxInt);
922 result_len += len; 943 result_len += len;
923 ASSERT(result_len >= 0); 944 ASSERT(result_len >= 0);
924 945
925 if (result_len > FixedArray::kMaxLength) { 946 if (result_len > FixedArray::kMaxLength) {
926 return CallJsBuiltin("ArrayConcat", args); 947 return CallJsBuiltin(isolate, "ArrayConcat", args);
927 } 948 }
928 } 949 }
929 950
930 if (result_len == 0) { 951 if (result_len == 0) {
931 return AllocateEmptyJSArray(); 952 return AllocateEmptyJSArray(heap);
932 } 953 }
933 954
934 // Allocate result. 955 // Allocate result.
935 Object* result; 956 Object* result;
936 { MaybeObject* maybe_result = AllocateJSArray(); 957 { MaybeObject* maybe_result = AllocateJSArray(heap);
937 if (!maybe_result->ToObject(&result)) return maybe_result; 958 if (!maybe_result->ToObject(&result)) return maybe_result;
938 } 959 }
939 JSArray* result_array = JSArray::cast(result); 960 JSArray* result_array = JSArray::cast(result);
940 961
941 { MaybeObject* maybe_result = 962 { MaybeObject* maybe_result =
942 Heap::AllocateUninitializedFixedArray(result_len); 963 heap->AllocateUninitializedFixedArray(result_len);
943 if (!maybe_result->ToObject(&result)) return maybe_result; 964 if (!maybe_result->ToObject(&result)) return maybe_result;
944 } 965 }
945 FixedArray* result_elms = FixedArray::cast(result); 966 FixedArray* result_elms = FixedArray::cast(result);
946 967
947 // Copy data. 968 // Copy data.
948 AssertNoAllocation no_gc; 969 AssertNoAllocation no_gc;
949 int start_pos = 0; 970 int start_pos = 0;
950 for (int i = 0; i < n_arguments; i++) { 971 for (int i = 0; i < n_arguments; i++) {
951 JSArray* array = JSArray::cast(args[i]); 972 JSArray* array = JSArray::cast(args[i]);
952 int len = Smi::cast(array->length())->value(); 973 int len = Smi::cast(array->length())->value();
953 if (len > 0) { 974 if (len > 0) {
954 FixedArray* elms = FixedArray::cast(array->elements()); 975 FixedArray* elms = FixedArray::cast(array->elements());
955 CopyElements(&no_gc, result_elms, start_pos, elms, 0, len); 976 CopyElements(heap, &no_gc, result_elms, start_pos, elms, 0, len);
956 start_pos += len; 977 start_pos += len;
957 } 978 }
958 } 979 }
959 ASSERT(start_pos == result_len); 980 ASSERT(start_pos == result_len);
960 981
961 // Set the length and elements. 982 // Set the length and elements.
962 result_array->set_length(Smi::FromInt(result_len)); 983 result_array->set_length(Smi::FromInt(result_len));
963 result_array->set_elements(result_elms); 984 result_array->set_elements(result_elms);
964 985
965 return result_array; 986 return result_array;
966 } 987 }
967 988
968 989
969 // ----------------------------------------------------------------------------- 990 // -----------------------------------------------------------------------------
970 // Strict mode poison pills 991 // Strict mode poison pills
971 992
972 993
973 BUILTIN(StrictArgumentsCallee) { 994 BUILTIN(StrictArgumentsCallee) {
974 HandleScope scope; 995 HandleScope scope;
975 return Top::Throw(*Factory::NewTypeError("strict_arguments_callee", 996 return isolate->Throw(*isolate->factory()->NewTypeError(
976 HandleVector<Object>(NULL, 0))); 997 "strict_arguments_callee", HandleVector<Object>(NULL, 0)));
977 } 998 }
978 999
979 1000
980 BUILTIN(StrictArgumentsCaller) { 1001 BUILTIN(StrictArgumentsCaller) {
981 HandleScope scope; 1002 HandleScope scope;
982 return Top::Throw(*Factory::NewTypeError("strict_arguments_caller", 1003 return isolate->Throw(*isolate->factory()->NewTypeError(
983 HandleVector<Object>(NULL, 0))); 1004 "strict_arguments_caller", HandleVector<Object>(NULL, 0)));
984 } 1005 }
985 1006
986 1007
987 BUILTIN(StrictFunctionCaller) { 1008 BUILTIN(StrictFunctionCaller) {
988 HandleScope scope; 1009 HandleScope scope;
989 return Top::Throw(*Factory::NewTypeError("strict_function_caller", 1010 return isolate->Throw(*isolate->factory()->NewTypeError(
990 HandleVector<Object>(NULL, 0))); 1011 "strict_function_caller", HandleVector<Object>(NULL, 0)));
991 } 1012 }
992 1013
993 1014
994 BUILTIN(StrictFunctionArguments) { 1015 BUILTIN(StrictFunctionArguments) {
995 HandleScope scope; 1016 HandleScope scope;
996 return Top::Throw(*Factory::NewTypeError("strict_function_arguments", 1017 return isolate->Throw(*isolate->factory()->NewTypeError(
997 HandleVector<Object>(NULL, 0))); 1018 "strict_function_arguments", HandleVector<Object>(NULL, 0)));
998 } 1019 }
999 1020
1000 1021
1001 // ----------------------------------------------------------------------------- 1022 // -----------------------------------------------------------------------------
1002 // 1023 //
1003 1024
1004 1025
1005 // Returns the holder JSObject if the function can legally be called 1026 // Returns the holder JSObject if the function can legally be called
1006 // with this receiver. Returns Heap::null_value() if the call is 1027 // with this receiver. Returns Heap::null_value() if the call is
1007 // illegal. Any arguments that don't fit the expected type is 1028 // illegal. Any arguments that don't fit the expected type is
1008 // overwritten with undefined. Arguments that do fit the expected 1029 // overwritten with undefined. Arguments that do fit the expected
1009 // type is overwritten with the object in the prototype chain that 1030 // type is overwritten with the object in the prototype chain that
1010 // actually has that type. 1031 // actually has that type.
1011 static inline Object* TypeCheck(int argc, 1032 static inline Object* TypeCheck(Heap* heap,
1033 int argc,
1012 Object** argv, 1034 Object** argv,
1013 FunctionTemplateInfo* info) { 1035 FunctionTemplateInfo* info) {
1014 Object* recv = argv[0]; 1036 Object* recv = argv[0];
1015 Object* sig_obj = info->signature(); 1037 Object* sig_obj = info->signature();
1016 if (sig_obj->IsUndefined()) return recv; 1038 if (sig_obj->IsUndefined()) return recv;
1017 SignatureInfo* sig = SignatureInfo::cast(sig_obj); 1039 SignatureInfo* sig = SignatureInfo::cast(sig_obj);
1018 // If necessary, check the receiver 1040 // If necessary, check the receiver
1019 Object* recv_type = sig->receiver(); 1041 Object* recv_type = sig->receiver();
1020 1042
1021 Object* holder = recv; 1043 Object* holder = recv;
1022 if (!recv_type->IsUndefined()) { 1044 if (!recv_type->IsUndefined()) {
1023 for (; holder != Heap::null_value(); holder = holder->GetPrototype()) { 1045 for (; holder != heap->null_value(); holder = holder->GetPrototype()) {
1024 if (holder->IsInstanceOf(FunctionTemplateInfo::cast(recv_type))) { 1046 if (holder->IsInstanceOf(FunctionTemplateInfo::cast(recv_type))) {
1025 break; 1047 break;
1026 } 1048 }
1027 } 1049 }
1028 if (holder == Heap::null_value()) return holder; 1050 if (holder == heap->null_value()) return holder;
1029 } 1051 }
1030 Object* args_obj = sig->args(); 1052 Object* args_obj = sig->args();
1031 // If there is no argument signature we're done 1053 // If there is no argument signature we're done
1032 if (args_obj->IsUndefined()) return holder; 1054 if (args_obj->IsUndefined()) return holder;
1033 FixedArray* args = FixedArray::cast(args_obj); 1055 FixedArray* args = FixedArray::cast(args_obj);
1034 int length = args->length(); 1056 int length = args->length();
1035 if (argc <= length) length = argc - 1; 1057 if (argc <= length) length = argc - 1;
1036 for (int i = 0; i < length; i++) { 1058 for (int i = 0; i < length; i++) {
1037 Object* argtype = args->get(i); 1059 Object* argtype = args->get(i);
1038 if (argtype->IsUndefined()) continue; 1060 if (argtype->IsUndefined()) continue;
1039 Object** arg = &argv[-1 - i]; 1061 Object** arg = &argv[-1 - i];
1040 Object* current = *arg; 1062 Object* current = *arg;
1041 for (; current != Heap::null_value(); current = current->GetPrototype()) { 1063 for (; current != heap->null_value(); current = current->GetPrototype()) {
1042 if (current->IsInstanceOf(FunctionTemplateInfo::cast(argtype))) { 1064 if (current->IsInstanceOf(FunctionTemplateInfo::cast(argtype))) {
1043 *arg = current; 1065 *arg = current;
1044 break; 1066 break;
1045 } 1067 }
1046 } 1068 }
1047 if (current == Heap::null_value()) *arg = Heap::undefined_value(); 1069 if (current == heap->null_value()) *arg = heap->undefined_value();
1048 } 1070 }
1049 return holder; 1071 return holder;
1050 } 1072 }
1051 1073
1052 1074
1053 template <bool is_construct> 1075 template <bool is_construct>
1054 MUST_USE_RESULT static MaybeObject* HandleApiCallHelper( 1076 MUST_USE_RESULT static MaybeObject* HandleApiCallHelper(
1055 BuiltinArguments<NEEDS_CALLED_FUNCTION> args) { 1077 BuiltinArguments<NEEDS_CALLED_FUNCTION> args, Isolate* isolate) {
1056 ASSERT(is_construct == CalledAsConstructor()); 1078 ASSERT(is_construct == CalledAsConstructor(isolate));
1079 Heap* heap = isolate->heap();
1057 1080
1058 HandleScope scope; 1081 HandleScope scope(isolate);
1059 Handle<JSFunction> function = args.called_function(); 1082 Handle<JSFunction> function = args.called_function();
1060 ASSERT(function->shared()->IsApiFunction()); 1083 ASSERT(function->shared()->IsApiFunction());
1061 1084
1062 FunctionTemplateInfo* fun_data = function->shared()->get_api_func_data(); 1085 FunctionTemplateInfo* fun_data = function->shared()->get_api_func_data();
1063 if (is_construct) { 1086 if (is_construct) {
1064 Handle<FunctionTemplateInfo> desc(fun_data); 1087 Handle<FunctionTemplateInfo> desc(fun_data, isolate);
1065 bool pending_exception = false; 1088 bool pending_exception = false;
1066 Factory::ConfigureInstance(desc, Handle<JSObject>::cast(args.receiver()), 1089 isolate->factory()->ConfigureInstance(
1067 &pending_exception); 1090 desc, Handle<JSObject>::cast(args.receiver()), &pending_exception);
1068 ASSERT(Top::has_pending_exception() == pending_exception); 1091 ASSERT(isolate->has_pending_exception() == pending_exception);
1069 if (pending_exception) return Failure::Exception(); 1092 if (pending_exception) return Failure::Exception();
1070 fun_data = *desc; 1093 fun_data = *desc;
1071 } 1094 }
1072 1095
1073 Object* raw_holder = TypeCheck(args.length(), &args[0], fun_data); 1096 Object* raw_holder = TypeCheck(heap, args.length(), &args[0], fun_data);
1074 1097
1075 if (raw_holder->IsNull()) { 1098 if (raw_holder->IsNull()) {
1076 // This function cannot be called with the given receiver. Abort! 1099 // This function cannot be called with the given receiver. Abort!
1077 Handle<Object> obj = 1100 Handle<Object> obj =
1078 Factory::NewTypeError("illegal_invocation", HandleVector(&function, 1)); 1101 isolate->factory()->NewTypeError(
1079 return Top::Throw(*obj); 1102 "illegal_invocation", HandleVector(&function, 1));
1103 return isolate->Throw(*obj);
1080 } 1104 }
1081 1105
1082 Object* raw_call_data = fun_data->call_code(); 1106 Object* raw_call_data = fun_data->call_code();
1083 if (!raw_call_data->IsUndefined()) { 1107 if (!raw_call_data->IsUndefined()) {
1084 CallHandlerInfo* call_data = CallHandlerInfo::cast(raw_call_data); 1108 CallHandlerInfo* call_data = CallHandlerInfo::cast(raw_call_data);
1085 Object* callback_obj = call_data->callback(); 1109 Object* callback_obj = call_data->callback();
1086 v8::InvocationCallback callback = 1110 v8::InvocationCallback callback =
1087 v8::ToCData<v8::InvocationCallback>(callback_obj); 1111 v8::ToCData<v8::InvocationCallback>(callback_obj);
1088 Object* data_obj = call_data->data(); 1112 Object* data_obj = call_data->data();
1089 Object* result; 1113 Object* result;
1090 1114
1091 LOG(ApiObjectAccess("call", JSObject::cast(*args.receiver()))); 1115 LOG(isolate, ApiObjectAccess("call", JSObject::cast(*args.receiver())));
1092 ASSERT(raw_holder->IsJSObject()); 1116 ASSERT(raw_holder->IsJSObject());
1093 1117
1094 CustomArguments custom; 1118 CustomArguments custom(isolate);
1095 v8::ImplementationUtilities::PrepareArgumentsData(custom.end(), 1119 v8::ImplementationUtilities::PrepareArgumentsData(custom.end(),
1096 data_obj, *function, raw_holder); 1120 data_obj, *function, raw_holder);
1097 1121
1098 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments( 1122 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments(
1099 custom.end(), 1123 custom.end(),
1100 &args[0] - 1, 1124 &args[0] - 1,
1101 args.length() - 1, 1125 args.length() - 1,
1102 is_construct); 1126 is_construct);
1103 1127
1104 v8::Handle<v8::Value> value; 1128 v8::Handle<v8::Value> value;
1105 { 1129 {
1106 // Leaving JavaScript. 1130 // Leaving JavaScript.
1107 VMState state(EXTERNAL); 1131 VMState state(isolate, EXTERNAL);
1108 ExternalCallbackScope call_scope(v8::ToCData<Address>(callback_obj)); 1132 ExternalCallbackScope call_scope(isolate,
1133 v8::ToCData<Address>(callback_obj));
1109 value = callback(new_args); 1134 value = callback(new_args);
1110 } 1135 }
1111 if (value.IsEmpty()) { 1136 if (value.IsEmpty()) {
1112 result = Heap::undefined_value(); 1137 result = heap->undefined_value();
1113 } else { 1138 } else {
1114 result = *reinterpret_cast<Object**>(*value); 1139 result = *reinterpret_cast<Object**>(*value);
1115 } 1140 }
1116 1141
1117 RETURN_IF_SCHEDULED_EXCEPTION(); 1142 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
1118 if (!is_construct || result->IsJSObject()) return result; 1143 if (!is_construct || result->IsJSObject()) return result;
1119 } 1144 }
1120 1145
1121 return *args.receiver(); 1146 return *args.receiver();
1122 } 1147 }
1123 1148
1124 1149
1125 BUILTIN(HandleApiCall) { 1150 BUILTIN(HandleApiCall) {
1126 return HandleApiCallHelper<false>(args); 1151 return HandleApiCallHelper<false>(args, isolate);
1127 } 1152 }
1128 1153
1129 1154
1130 BUILTIN(HandleApiCallConstruct) { 1155 BUILTIN(HandleApiCallConstruct) {
1131 return HandleApiCallHelper<true>(args); 1156 return HandleApiCallHelper<true>(args, isolate);
1132 } 1157 }
1133 1158
1134 1159
1135 #ifdef DEBUG 1160 #ifdef DEBUG
1136 1161
1137 static void VerifyTypeCheck(Handle<JSObject> object, 1162 static void VerifyTypeCheck(Handle<JSObject> object,
1138 Handle<JSFunction> function) { 1163 Handle<JSFunction> function) {
1139 ASSERT(function->shared()->IsApiFunction()); 1164 ASSERT(function->shared()->IsApiFunction());
1140 FunctionTemplateInfo* info = function->shared()->get_api_func_data(); 1165 FunctionTemplateInfo* info = function->shared()->get_api_func_data();
1141 if (info->signature()->IsUndefined()) return; 1166 if (info->signature()->IsUndefined()) return;
1142 SignatureInfo* signature = SignatureInfo::cast(info->signature()); 1167 SignatureInfo* signature = SignatureInfo::cast(info->signature());
1143 Object* receiver_type = signature->receiver(); 1168 Object* receiver_type = signature->receiver();
1144 if (receiver_type->IsUndefined()) return; 1169 if (receiver_type->IsUndefined()) return;
1145 FunctionTemplateInfo* type = FunctionTemplateInfo::cast(receiver_type); 1170 FunctionTemplateInfo* type = FunctionTemplateInfo::cast(receiver_type);
1146 ASSERT(object->IsInstanceOf(type)); 1171 ASSERT(object->IsInstanceOf(type));
1147 } 1172 }
1148 1173
1149 #endif 1174 #endif
1150 1175
1151 1176
1152 BUILTIN(FastHandleApiCall) { 1177 BUILTIN(FastHandleApiCall) {
1153 ASSERT(!CalledAsConstructor()); 1178 ASSERT(!CalledAsConstructor(isolate));
1179 Heap* heap = isolate->heap();
1154 const bool is_construct = false; 1180 const bool is_construct = false;
1155 1181
1156 // We expect four more arguments: callback, function, call data, and holder. 1182 // We expect four more arguments: callback, function, call data, and holder.
1157 const int args_length = args.length() - 4; 1183 const int args_length = args.length() - 4;
1158 ASSERT(args_length >= 0); 1184 ASSERT(args_length >= 0);
1159 1185
1160 Object* callback_obj = args[args_length]; 1186 Object* callback_obj = args[args_length];
1161 1187
1162 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments( 1188 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments(
1163 &args[args_length + 1], 1189 &args[args_length + 1],
1164 &args[0] - 1, 1190 &args[0] - 1,
1165 args_length - 1, 1191 args_length - 1,
1166 is_construct); 1192 is_construct);
1167 1193
1168 #ifdef DEBUG 1194 #ifdef DEBUG
1169 VerifyTypeCheck(Utils::OpenHandle(*new_args.Holder()), 1195 VerifyTypeCheck(Utils::OpenHandle(*new_args.Holder()),
1170 Utils::OpenHandle(*new_args.Callee())); 1196 Utils::OpenHandle(*new_args.Callee()));
1171 #endif 1197 #endif
1172 HandleScope scope; 1198 HandleScope scope(isolate);
1173 Object* result; 1199 Object* result;
1174 v8::Handle<v8::Value> value; 1200 v8::Handle<v8::Value> value;
1175 { 1201 {
1176 // Leaving JavaScript. 1202 // Leaving JavaScript.
1177 VMState state(EXTERNAL); 1203 VMState state(isolate, EXTERNAL);
1178 ExternalCallbackScope call_scope(v8::ToCData<Address>(callback_obj)); 1204 ExternalCallbackScope call_scope(isolate,
1205 v8::ToCData<Address>(callback_obj));
1179 v8::InvocationCallback callback = 1206 v8::InvocationCallback callback =
1180 v8::ToCData<v8::InvocationCallback>(callback_obj); 1207 v8::ToCData<v8::InvocationCallback>(callback_obj);
1181 1208
1182 value = callback(new_args); 1209 value = callback(new_args);
1183 } 1210 }
1184 if (value.IsEmpty()) { 1211 if (value.IsEmpty()) {
1185 result = Heap::undefined_value(); 1212 result = heap->undefined_value();
1186 } else { 1213 } else {
1187 result = *reinterpret_cast<Object**>(*value); 1214 result = *reinterpret_cast<Object**>(*value);
1188 } 1215 }
1189 1216
1190 RETURN_IF_SCHEDULED_EXCEPTION(); 1217 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
1191 return result; 1218 return result;
1192 } 1219 }
1193 1220
1194 1221
1195 // Helper function to handle calls to non-function objects created through the 1222 // Helper function to handle calls to non-function objects created through the
1196 // API. The object can be called as either a constructor (using new) or just as 1223 // API. The object can be called as either a constructor (using new) or just as
1197 // a function (without new). 1224 // a function (without new).
1198 MUST_USE_RESULT static MaybeObject* HandleApiCallAsFunctionOrConstructor( 1225 MUST_USE_RESULT static MaybeObject* HandleApiCallAsFunctionOrConstructor(
1226 Isolate* isolate,
1199 bool is_construct_call, 1227 bool is_construct_call,
1200 BuiltinArguments<NO_EXTRA_ARGUMENTS> args) { 1228 BuiltinArguments<NO_EXTRA_ARGUMENTS> args) {
1201 // Non-functions are never called as constructors. Even if this is an object 1229 // Non-functions are never called as constructors. Even if this is an object
1202 // called as a constructor the delegate call is not a construct call. 1230 // called as a constructor the delegate call is not a construct call.
1203 ASSERT(!CalledAsConstructor()); 1231 ASSERT(!CalledAsConstructor(isolate));
1232 Heap* heap = isolate->heap();
1204 1233
1205 Handle<Object> receiver = args.at<Object>(0); 1234 Handle<Object> receiver = args.at<Object>(0);
1206 1235
1207 // Get the object called. 1236 // Get the object called.
1208 JSObject* obj = JSObject::cast(*args.receiver()); 1237 JSObject* obj = JSObject::cast(*args.receiver());
1209 1238
1210 // Get the invocation callback from the function descriptor that was 1239 // Get the invocation callback from the function descriptor that was
1211 // used to create the called object. 1240 // used to create the called object.
1212 ASSERT(obj->map()->has_instance_call_handler()); 1241 ASSERT(obj->map()->has_instance_call_handler());
1213 JSFunction* constructor = JSFunction::cast(obj->map()->constructor()); 1242 JSFunction* constructor = JSFunction::cast(obj->map()->constructor());
1214 ASSERT(constructor->shared()->IsApiFunction()); 1243 ASSERT(constructor->shared()->IsApiFunction());
1215 Object* handler = 1244 Object* handler =
1216 constructor->shared()->get_api_func_data()->instance_call_handler(); 1245 constructor->shared()->get_api_func_data()->instance_call_handler();
1217 ASSERT(!handler->IsUndefined()); 1246 ASSERT(!handler->IsUndefined());
1218 CallHandlerInfo* call_data = CallHandlerInfo::cast(handler); 1247 CallHandlerInfo* call_data = CallHandlerInfo::cast(handler);
1219 Object* callback_obj = call_data->callback(); 1248 Object* callback_obj = call_data->callback();
1220 v8::InvocationCallback callback = 1249 v8::InvocationCallback callback =
1221 v8::ToCData<v8::InvocationCallback>(callback_obj); 1250 v8::ToCData<v8::InvocationCallback>(callback_obj);
1222 1251
1223 // Get the data for the call and perform the callback. 1252 // Get the data for the call and perform the callback.
1224 Object* result; 1253 Object* result;
1225 { 1254 {
1226 HandleScope scope; 1255 HandleScope scope(isolate);
1256 LOG(isolate, ApiObjectAccess("call non-function", obj));
1227 1257
1228 LOG(ApiObjectAccess("call non-function", obj)); 1258 CustomArguments custom(isolate);
1229
1230 CustomArguments custom;
1231 v8::ImplementationUtilities::PrepareArgumentsData(custom.end(), 1259 v8::ImplementationUtilities::PrepareArgumentsData(custom.end(),
1232 call_data->data(), constructor, obj); 1260 call_data->data(), constructor, obj);
1233 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments( 1261 v8::Arguments new_args = v8::ImplementationUtilities::NewArguments(
1234 custom.end(), 1262 custom.end(),
1235 &args[0] - 1, 1263 &args[0] - 1,
1236 args.length() - 1, 1264 args.length() - 1,
1237 is_construct_call); 1265 is_construct_call);
1238 v8::Handle<v8::Value> value; 1266 v8::Handle<v8::Value> value;
1239 { 1267 {
1240 // Leaving JavaScript. 1268 // Leaving JavaScript.
1241 VMState state(EXTERNAL); 1269 VMState state(isolate, EXTERNAL);
1242 ExternalCallbackScope call_scope(v8::ToCData<Address>(callback_obj)); 1270 ExternalCallbackScope call_scope(isolate,
1271 v8::ToCData<Address>(callback_obj));
1243 value = callback(new_args); 1272 value = callback(new_args);
1244 } 1273 }
1245 if (value.IsEmpty()) { 1274 if (value.IsEmpty()) {
1246 result = Heap::undefined_value(); 1275 result = heap->undefined_value();
1247 } else { 1276 } else {
1248 result = *reinterpret_cast<Object**>(*value); 1277 result = *reinterpret_cast<Object**>(*value);
1249 } 1278 }
1250 } 1279 }
1251 // Check for exceptions and return result. 1280 // Check for exceptions and return result.
1252 RETURN_IF_SCHEDULED_EXCEPTION(); 1281 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
1253 return result; 1282 return result;
1254 } 1283 }
1255 1284
1256 1285
1257 // Handle calls to non-function objects created through the API. This delegate 1286 // Handle calls to non-function objects created through the API. This delegate
1258 // function is used when the call is a normal function call. 1287 // function is used when the call is a normal function call.
1259 BUILTIN(HandleApiCallAsFunction) { 1288 BUILTIN(HandleApiCallAsFunction) {
1260 return HandleApiCallAsFunctionOrConstructor(false, args); 1289 return HandleApiCallAsFunctionOrConstructor(isolate, false, args);
1261 } 1290 }
1262 1291
1263 1292
1264 // Handle calls to non-function objects created through the API. This delegate 1293 // Handle calls to non-function objects created through the API. This delegate
1265 // function is used when the call is a construct call. 1294 // function is used when the call is a construct call.
1266 BUILTIN(HandleApiCallAsConstructor) { 1295 BUILTIN(HandleApiCallAsConstructor) {
1267 return HandleApiCallAsFunctionOrConstructor(true, args); 1296 return HandleApiCallAsFunctionOrConstructor(isolate, true, args);
1268 } 1297 }
1269 1298
1270 1299
1271 static void Generate_LoadIC_ArrayLength(MacroAssembler* masm) { 1300 static void Generate_LoadIC_ArrayLength(MacroAssembler* masm) {
1272 LoadIC::GenerateArrayLength(masm); 1301 LoadIC::GenerateArrayLength(masm);
1273 } 1302 }
1274 1303
1275 1304
1276 static void Generate_LoadIC_StringLength(MacroAssembler* masm) { 1305 static void Generate_LoadIC_StringLength(MacroAssembler* masm) {
1277 LoadIC::GenerateStringLength(masm, false); 1306 LoadIC::GenerateStringLength(masm, false);
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
1466 static void Generate_PlainReturn_LiveEdit(MacroAssembler* masm) { 1495 static void Generate_PlainReturn_LiveEdit(MacroAssembler* masm) {
1467 Debug::GeneratePlainReturnLiveEdit(masm); 1496 Debug::GeneratePlainReturnLiveEdit(masm);
1468 } 1497 }
1469 1498
1470 1499
1471 static void Generate_FrameDropper_LiveEdit(MacroAssembler* masm) { 1500 static void Generate_FrameDropper_LiveEdit(MacroAssembler* masm) {
1472 Debug::GenerateFrameDropperLiveEdit(masm); 1501 Debug::GenerateFrameDropperLiveEdit(masm);
1473 } 1502 }
1474 #endif 1503 #endif
1475 1504
1476 Object* Builtins::builtins_[builtin_count] = { NULL, }; 1505
1477 const char* Builtins::names_[builtin_count] = { NULL, }; 1506 Builtins::Builtins() : initialized_(false) {
1507 memset(builtins_, 0, sizeof(builtins_[0]) * builtin_count);
1508 memset(names_, 0, sizeof(names_[0]) * builtin_count);
1509 }
1510
1511
1512 Builtins::~Builtins() {
1513 }
1514
1478 1515
1479 #define DEF_ENUM_C(name, ignore) FUNCTION_ADDR(Builtin_##name), 1516 #define DEF_ENUM_C(name, ignore) FUNCTION_ADDR(Builtin_##name),
1480 Address Builtins::c_functions_[cfunction_count] = { 1517 Address const Builtins::c_functions_[cfunction_count] = {
1481 BUILTIN_LIST_C(DEF_ENUM_C) 1518 BUILTIN_LIST_C(DEF_ENUM_C)
1482 }; 1519 };
1483 #undef DEF_ENUM_C 1520 #undef DEF_ENUM_C
1484 1521
1485 #define DEF_JS_NAME(name, ignore) #name, 1522 #define DEF_JS_NAME(name, ignore) #name,
1486 #define DEF_JS_ARGC(ignore, argc) argc, 1523 #define DEF_JS_ARGC(ignore, argc) argc,
1487 const char* Builtins::javascript_names_[id_count] = { 1524 const char* const Builtins::javascript_names_[id_count] = {
1488 BUILTINS_LIST_JS(DEF_JS_NAME) 1525 BUILTINS_LIST_JS(DEF_JS_NAME)
1489 }; 1526 };
1490 1527
1491 int Builtins::javascript_argc_[id_count] = { 1528 int const Builtins::javascript_argc_[id_count] = {
1492 BUILTINS_LIST_JS(DEF_JS_ARGC) 1529 BUILTINS_LIST_JS(DEF_JS_ARGC)
1493 }; 1530 };
1494 #undef DEF_JS_NAME 1531 #undef DEF_JS_NAME
1495 #undef DEF_JS_ARGC 1532 #undef DEF_JS_ARGC
1496 1533
1497 static bool is_initialized = false; 1534 struct BuiltinDesc {
1535 byte* generator;
1536 byte* c_code;
1537 const char* s_name; // name is only used for generating log information.
1538 int name;
1539 Code::Flags flags;
1540 BuiltinExtraArguments extra_args;
1541 };
1542
1543 class BuiltinFunctionTable {
1544 public:
1545 BuiltinFunctionTable() {
1546 Builtins::InitBuiltinFunctionTable();
1547 }
1548
1549 static const BuiltinDesc* functions() { return functions_; }
1550
1551 private:
1552 static BuiltinDesc functions_[Builtins::builtin_count + 1];
1553
1554 friend class Builtins;
1555 };
1556
1557 BuiltinDesc BuiltinFunctionTable::functions_[Builtins::builtin_count + 1];
1558
1559 static const BuiltinFunctionTable builtin_function_table_init;
1560
1561 // Define array of pointers to generators and C builtin functions.
1562 // We do this in a sort of roundabout way so that we can do the initialization
1563 // within the lexical scope of Builtins:: and within a context where
1564 // Code::Flags names a non-abstract type.
1565 void Builtins::InitBuiltinFunctionTable() {
1566 BuiltinDesc* functions = BuiltinFunctionTable::functions_;
1567 functions[builtin_count].generator = NULL;
1568 functions[builtin_count].c_code = NULL;
1569 functions[builtin_count].s_name = NULL;
1570 functions[builtin_count].name = builtin_count;
1571 functions[builtin_count].flags = static_cast<Code::Flags>(0);
1572 functions[builtin_count].extra_args = NO_EXTRA_ARGUMENTS;
1573
1574 #define DEF_FUNCTION_PTR_C(aname, aextra_args) \
1575 functions->generator = FUNCTION_ADDR(Generate_Adaptor); \
1576 functions->c_code = FUNCTION_ADDR(Builtin_##aname); \
1577 functions->s_name = #aname; \
1578 functions->name = c_##aname; \
1579 functions->flags = Code::ComputeFlags(Code::BUILTIN); \
1580 functions->extra_args = aextra_args; \
1581 ++functions;
1582
1583 #define DEF_FUNCTION_PTR_A(aname, kind, state, extra) \
1584 functions->generator = FUNCTION_ADDR(Generate_##aname); \
1585 functions->c_code = NULL; \
1586 functions->s_name = #aname; \
1587 functions->name = aname; \
1588 functions->flags = Code::ComputeFlags(Code::kind, \
1589 NOT_IN_LOOP, \
1590 state, \
1591 extra); \
1592 functions->extra_args = NO_EXTRA_ARGUMENTS; \
1593 ++functions;
1594
1595 BUILTIN_LIST_C(DEF_FUNCTION_PTR_C)
1596 BUILTIN_LIST_A(DEF_FUNCTION_PTR_A)
1597 BUILTIN_LIST_DEBUG_A(DEF_FUNCTION_PTR_A)
1598
1599 #undef DEF_FUNCTION_PTR_C
1600 #undef DEF_FUNCTION_PTR_A
1601 }
1602
1498 void Builtins::Setup(bool create_heap_objects) { 1603 void Builtins::Setup(bool create_heap_objects) {
1499 ASSERT(!is_initialized); 1604 ASSERT(!initialized_);
1605 Heap* heap = Isolate::Current()->heap();
1500 1606
1501 // Create a scope for the handles in the builtins. 1607 // Create a scope for the handles in the builtins.
1502 HandleScope scope; 1608 HandleScope scope;
1503 1609
1504 struct BuiltinDesc { 1610 const BuiltinDesc* functions = BuiltinFunctionTable::functions();
1505 byte* generator;
1506 byte* c_code;
1507 const char* s_name; // name is only used for generating log information.
1508 int name;
1509 Code::Flags flags;
1510 BuiltinExtraArguments extra_args;
1511 };
1512
1513 #define DEF_FUNCTION_PTR_C(name, extra_args) \
1514 { FUNCTION_ADDR(Generate_Adaptor), \
1515 FUNCTION_ADDR(Builtin_##name), \
1516 #name, \
1517 c_##name, \
1518 Code::ComputeFlags(Code::BUILTIN), \
1519 extra_args \
1520 },
1521
1522 #define DEF_FUNCTION_PTR_A(name, kind, state, extra) \
1523 { FUNCTION_ADDR(Generate_##name), \
1524 NULL, \
1525 #name, \
1526 name, \
1527 Code::ComputeFlags(Code::kind, NOT_IN_LOOP, state, extra), \
1528 NO_EXTRA_ARGUMENTS \
1529 },
1530
1531 // Define array of pointers to generators and C builtin functions.
1532 static BuiltinDesc functions[] = {
1533 BUILTIN_LIST_C(DEF_FUNCTION_PTR_C)
1534 BUILTIN_LIST_A(DEF_FUNCTION_PTR_A)
1535 BUILTIN_LIST_DEBUG_A(DEF_FUNCTION_PTR_A)
1536 // Terminator:
1537 { NULL, NULL, NULL, builtin_count, static_cast<Code::Flags>(0),
1538 NO_EXTRA_ARGUMENTS }
1539 };
1540
1541 #undef DEF_FUNCTION_PTR_C
1542 #undef DEF_FUNCTION_PTR_A
1543 1611
1544 // For now we generate builtin adaptor code into a stack-allocated 1612 // For now we generate builtin adaptor code into a stack-allocated
1545 // buffer, before copying it into individual code objects. 1613 // buffer, before copying it into individual code objects.
1546 byte buffer[4*KB]; 1614 byte buffer[4*KB];
1547 1615
1548 // Traverse the list of builtins and generate an adaptor in a 1616 // Traverse the list of builtins and generate an adaptor in a
1549 // separate code object for each one. 1617 // separate code object for each one.
1550 for (int i = 0; i < builtin_count; i++) { 1618 for (int i = 0; i < builtin_count; i++) {
1551 if (create_heap_objects) { 1619 if (create_heap_objects) {
1552 MacroAssembler masm(buffer, sizeof buffer); 1620 MacroAssembler masm(buffer, sizeof buffer);
1553 // Generate the code/adaptor. 1621 // Generate the code/adaptor.
1554 typedef void (*Generator)(MacroAssembler*, int, BuiltinExtraArguments); 1622 typedef void (*Generator)(MacroAssembler*, int, BuiltinExtraArguments);
1555 Generator g = FUNCTION_CAST<Generator>(functions[i].generator); 1623 Generator g = FUNCTION_CAST<Generator>(functions[i].generator);
1556 // We pass all arguments to the generator, but it may not use all of 1624 // We pass all arguments to the generator, but it may not use all of
1557 // them. This works because the first arguments are on top of the 1625 // them. This works because the first arguments are on top of the
1558 // stack. 1626 // stack.
1559 g(&masm, functions[i].name, functions[i].extra_args); 1627 g(&masm, functions[i].name, functions[i].extra_args);
1560 // Move the code into the object heap. 1628 // Move the code into the object heap.
1561 CodeDesc desc; 1629 CodeDesc desc;
1562 masm.GetCode(&desc); 1630 masm.GetCode(&desc);
1563 Code::Flags flags = functions[i].flags; 1631 Code::Flags flags = functions[i].flags;
1564 Object* code = NULL; 1632 Object* code = NULL;
1565 { 1633 {
1566 // During startup it's OK to always allocate and defer GC to later. 1634 // During startup it's OK to always allocate and defer GC to later.
1567 // This simplifies things because we don't need to retry. 1635 // This simplifies things because we don't need to retry.
1568 AlwaysAllocateScope __scope__; 1636 AlwaysAllocateScope __scope__;
1569 { MaybeObject* maybe_code = 1637 { MaybeObject* maybe_code =
1570 Heap::CreateCode(desc, flags, masm.CodeObject()); 1638 heap->CreateCode(desc, flags, masm.CodeObject());
1571 if (!maybe_code->ToObject(&code)) { 1639 if (!maybe_code->ToObject(&code)) {
1572 v8::internal::V8::FatalProcessOutOfMemory("CreateCode"); 1640 v8::internal::V8::FatalProcessOutOfMemory("CreateCode");
1573 } 1641 }
1574 } 1642 }
1575 } 1643 }
1576 // Log the event and add the code to the builtins array. 1644 // Log the event and add the code to the builtins array.
1577 PROFILE(CodeCreateEvent(Logger::BUILTIN_TAG, 1645 PROFILE(ISOLATE,
1646 CodeCreateEvent(Logger::BUILTIN_TAG,
1578 Code::cast(code), 1647 Code::cast(code),
1579 functions[i].s_name)); 1648 functions[i].s_name));
1580 GDBJIT(AddCode(GDBJITInterface::BUILTIN, 1649 GDBJIT(AddCode(GDBJITInterface::BUILTIN,
1581 functions[i].s_name, 1650 functions[i].s_name,
1582 Code::cast(code))); 1651 Code::cast(code)));
1583 builtins_[i] = code; 1652 builtins_[i] = code;
1584 #ifdef ENABLE_DISASSEMBLER 1653 #ifdef ENABLE_DISASSEMBLER
1585 if (FLAG_print_builtin_code) { 1654 if (FLAG_print_builtin_code) {
1586 PrintF("Builtin: %s\n", functions[i].s_name); 1655 PrintF("Builtin: %s\n", functions[i].s_name);
1587 Code::cast(code)->Disassemble(functions[i].s_name); 1656 Code::cast(code)->Disassemble(functions[i].s_name);
1588 PrintF("\n"); 1657 PrintF("\n");
1589 } 1658 }
1590 #endif 1659 #endif
1591 } else { 1660 } else {
1592 // Deserializing. The values will be filled in during IterateBuiltins. 1661 // Deserializing. The values will be filled in during IterateBuiltins.
1593 builtins_[i] = NULL; 1662 builtins_[i] = NULL;
1594 } 1663 }
1595 names_[i] = functions[i].s_name; 1664 names_[i] = functions[i].s_name;
1596 } 1665 }
1597 1666
1598 // Mark as initialized. 1667 // Mark as initialized.
1599 is_initialized = true; 1668 initialized_ = true;
1600 } 1669 }
1601 1670
1602 1671
1603 void Builtins::TearDown() { 1672 void Builtins::TearDown() {
1604 is_initialized = false; 1673 initialized_ = false;
1605 } 1674 }
1606 1675
1607 1676
1608 void Builtins::IterateBuiltins(ObjectVisitor* v) { 1677 void Builtins::IterateBuiltins(ObjectVisitor* v) {
1609 v->VisitPointers(&builtins_[0], &builtins_[0] + builtin_count); 1678 v->VisitPointers(&builtins_[0], &builtins_[0] + builtin_count);
1610 } 1679 }
1611 1680
1612 1681
1613 const char* Builtins::Lookup(byte* pc) { 1682 const char* Builtins::Lookup(byte* pc) {
1614 if (is_initialized) { // may be called during initialization (disassembler!) 1683 // may be called during initialization (disassembler!)
1684 if (initialized_) {
1615 for (int i = 0; i < builtin_count; i++) { 1685 for (int i = 0; i < builtin_count; i++) {
1616 Code* entry = Code::cast(builtins_[i]); 1686 Code* entry = Code::cast(builtins_[i]);
1617 if (entry->contains(pc)) { 1687 if (entry->contains(pc)) {
1618 return names_[i]; 1688 return names_[i];
1619 } 1689 }
1620 } 1690 }
1621 } 1691 }
1622 return NULL; 1692 return NULL;
1623 } 1693 }
1624 1694
1625 1695
1626 } } // namespace v8::internal 1696 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/builtins.h ('k') | src/checks.cc » ('j') | src/heap.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698