blob: 15ddb5ff438c66ee4045ac3dd2facd7ffbf426a6 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/factory.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/allocation-site-scopes.h"
8#include "src/base/bits.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009#include "src/bootstrapper.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010#include "src/conversions.h"
11#include "src/isolate-inl.h"
12#include "src/macro-assembler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000013
14namespace v8 {
15namespace internal {
16
17
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018// Calls the FUNCTION_CALL function and retries it up to three times
19// to guarantee that any allocations performed during the call will
20// succeed if there's enough memory.
21//
22// Warning: Do not use the identifiers __object__, __maybe_object__,
23// __allocation__ or __scope__ in a call to this macro.
24
25#define RETURN_OBJECT_UNLESS_RETRY(ISOLATE, TYPE) \
26 if (__allocation__.To(&__object__)) { \
27 DCHECK(__object__ != (ISOLATE)->heap()->exception()); \
28 return Handle<TYPE>(TYPE::cast(__object__), ISOLATE); \
29 }
30
31#define CALL_HEAP_FUNCTION(ISOLATE, FUNCTION_CALL, TYPE) \
32 do { \
33 AllocationResult __allocation__ = FUNCTION_CALL; \
34 Object* __object__ = NULL; \
35 RETURN_OBJECT_UNLESS_RETRY(ISOLATE, TYPE) \
36 /* Two GCs before panicking. In newspace will almost always succeed. */ \
37 for (int __i__ = 0; __i__ < 2; __i__++) { \
38 (ISOLATE)->heap()->CollectGarbage(__allocation__.RetrySpace(), \
39 "allocation failure"); \
40 __allocation__ = FUNCTION_CALL; \
41 RETURN_OBJECT_UNLESS_RETRY(ISOLATE, TYPE) \
42 } \
43 (ISOLATE)->counters()->gc_last_resort_from_handles()->Increment(); \
44 (ISOLATE)->heap()->CollectAllAvailableGarbage("last resort gc"); \
45 { \
46 AlwaysAllocateScope __scope__(ISOLATE); \
47 __allocation__ = FUNCTION_CALL; \
48 } \
49 RETURN_OBJECT_UNLESS_RETRY(ISOLATE, TYPE) \
50 /* TODO(1181417): Fix this. */ \
51 v8::internal::Heap::FatalProcessOutOfMemory("CALL_AND_RETRY_LAST", true); \
52 return Handle<TYPE>(); \
53 } while (false)
54
55
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056template<typename T>
57Handle<T> Factory::New(Handle<Map> map, AllocationSpace space) {
58 CALL_HEAP_FUNCTION(
59 isolate(),
60 isolate()->heap()->Allocate(*map, space),
61 T);
62}
63
64
65template<typename T>
66Handle<T> Factory::New(Handle<Map> map,
67 AllocationSpace space,
68 Handle<AllocationSite> allocation_site) {
69 CALL_HEAP_FUNCTION(
70 isolate(),
71 isolate()->heap()->Allocate(*map, space, *allocation_site),
72 T);
73}
74
75
76Handle<HeapObject> Factory::NewFillerObject(int size,
77 bool double_align,
78 AllocationSpace space) {
79 CALL_HEAP_FUNCTION(
80 isolate(),
81 isolate()->heap()->AllocateFillerObject(size, double_align, space),
82 HeapObject);
83}
84
85
86Handle<Box> Factory::NewBox(Handle<Object> value) {
87 Handle<Box> result = Handle<Box>::cast(NewStruct(BOX_TYPE));
88 result->set_value(*value);
89 return result;
90}
91
92
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000093Handle<PrototypeInfo> Factory::NewPrototypeInfo() {
94 Handle<PrototypeInfo> result =
95 Handle<PrototypeInfo>::cast(NewStruct(PROTOTYPE_INFO_TYPE));
96 result->set_prototype_users(WeakFixedArray::Empty());
97 result->set_registry_slot(PrototypeInfo::UNREGISTERED);
98 result->set_validity_cell(Smi::FromInt(0));
99 return result;
100}
101
102
103Handle<SloppyBlockWithEvalContextExtension>
104Factory::NewSloppyBlockWithEvalContextExtension(
105 Handle<ScopeInfo> scope_info, Handle<JSObject> extension) {
106 DCHECK(scope_info->is_declaration_scope());
107 Handle<SloppyBlockWithEvalContextExtension> result =
108 Handle<SloppyBlockWithEvalContextExtension>::cast(
109 NewStruct(SLOPPY_BLOCK_WITH_EVAL_CONTEXT_EXTENSION_TYPE));
110 result->set_scope_info(*scope_info);
111 result->set_extension(*extension);
112 return result;
113}
114
115
116Handle<Oddball> Factory::NewOddball(Handle<Map> map, const char* to_string,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000117 Handle<Object> to_number,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000118 const char* type_of, byte kind) {
119 Handle<Oddball> oddball = New<Oddball>(map, OLD_SPACE);
120 Oddball::Initialize(isolate(), oddball, to_string, to_number, type_of, kind);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000121 return oddball;
122}
123
124
Steve Blocka7e24c12009-10-30 11:49:00 +0000125Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000126 DCHECK(0 <= size);
Steve Block44f0eee2011-05-26 01:26:41 +0100127 CALL_HEAP_FUNCTION(
128 isolate(),
129 isolate()->heap()->AllocateFixedArray(size, pretenure),
130 FixedArray);
Steve Blocka7e24c12009-10-30 11:49:00 +0000131}
132
133
Steve Block6ded16b2010-05-10 14:33:55 +0100134Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size,
135 PretenureFlag pretenure) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000136 DCHECK(0 <= size);
Steve Block44f0eee2011-05-26 01:26:41 +0100137 CALL_HEAP_FUNCTION(
138 isolate(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000139 isolate()->heap()->AllocateFixedArrayWithFiller(size,
140 pretenure,
141 *the_hole_value()),
Steve Block44f0eee2011-05-26 01:26:41 +0100142 FixedArray);
Steve Blocka7e24c12009-10-30 11:49:00 +0000143}
144
145
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000146Handle<FixedArray> Factory::NewUninitializedFixedArray(int size) {
147 CALL_HEAP_FUNCTION(
148 isolate(),
149 isolate()->heap()->AllocateUninitializedFixedArray(size),
150 FixedArray);
151}
152
153
154Handle<FixedArrayBase> Factory::NewFixedDoubleArray(int size,
155 PretenureFlag pretenure) {
156 DCHECK(0 <= size);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000157 CALL_HEAP_FUNCTION(
158 isolate(),
159 isolate()->heap()->AllocateUninitializedFixedDoubleArray(size, pretenure),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000160 FixedArrayBase);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000161}
162
163
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000164Handle<FixedArrayBase> Factory::NewFixedDoubleArrayWithHoles(
165 int size,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100166 PretenureFlag pretenure) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000167 DCHECK(0 <= size);
168 Handle<FixedArrayBase> array = NewFixedDoubleArray(size, pretenure);
169 if (size > 0) {
170 Handle<FixedDoubleArray> double_array =
171 Handle<FixedDoubleArray>::cast(array);
172 for (int i = 0; i < size; ++i) {
173 double_array->set_the_hole(i);
174 }
175 }
176 return array;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100177}
178
179
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000180Handle<OrderedHashSet> Factory::NewOrderedHashSet() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400181 return OrderedHashSet::Allocate(isolate(), OrderedHashSet::kMinCapacity);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000182}
183
184
185Handle<OrderedHashMap> Factory::NewOrderedHashMap() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400186 return OrderedHashMap::Allocate(isolate(), OrderedHashMap::kMinCapacity);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100187}
188
189
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100190Handle<AccessorPair> Factory::NewAccessorPair() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000191 Handle<AccessorPair> accessors =
192 Handle<AccessorPair>::cast(NewStruct(ACCESSOR_PAIR_TYPE));
193 accessors->set_getter(*the_hole_value(), SKIP_WRITE_BARRIER);
194 accessors->set_setter(*the_hole_value(), SKIP_WRITE_BARRIER);
195 return accessors;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100196}
197
198
199Handle<TypeFeedbackInfo> Factory::NewTypeFeedbackInfo() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000200 Handle<TypeFeedbackInfo> info =
201 Handle<TypeFeedbackInfo>::cast(NewStruct(TYPE_FEEDBACK_INFO_TYPE));
202 info->initialize_storage();
203 return info;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100204}
205
206
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000207// Internalized strings are created in the old generation (data space).
208Handle<String> Factory::InternalizeUtf8String(Vector<const char> string) {
209 Utf8StringKey key(string, isolate()->heap()->HashSeed());
210 return InternalizeStringWithKey(&key);
Steve Block9fac8402011-05-12 15:51:54 +0100211}
212
Ben Murdoch257744e2011-11-30 15:57:28 +0000213
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000214Handle<String> Factory::InternalizeOneByteString(Vector<const uint8_t> string) {
215 OneByteStringKey key(string, isolate()->heap()->HashSeed());
216 return InternalizeStringWithKey(&key);
Steve Block9fac8402011-05-12 15:51:54 +0100217}
218
Steve Blocka7e24c12009-10-30 11:49:00 +0000219
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000220Handle<String> Factory::InternalizeOneByteString(
221 Handle<SeqOneByteString> string, int from, int length) {
222 SeqOneByteSubStringKey key(string, from, length);
223 return InternalizeStringWithKey(&key);
Steve Blocka7e24c12009-10-30 11:49:00 +0000224}
225
226
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000227Handle<String> Factory::InternalizeTwoByteString(Vector<const uc16> string) {
228 TwoByteStringKey key(string, isolate()->heap()->HashSeed());
229 return InternalizeStringWithKey(&key);
Steve Blocka7e24c12009-10-30 11:49:00 +0000230}
231
232
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000233template<class StringTableKey>
234Handle<String> Factory::InternalizeStringWithKey(StringTableKey* key) {
235 return StringTable::LookupKey(isolate(), key);
236}
237
238
239MaybeHandle<String> Factory::NewStringFromOneByte(Vector<const uint8_t> string,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000240 PretenureFlag pretenure) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000241 int length = string.length();
242 if (length == 1) return LookupSingleCharacterStringFromCode(string[0]);
243 Handle<SeqOneByteString> result;
244 ASSIGN_RETURN_ON_EXCEPTION(
Steve Block44f0eee2011-05-26 01:26:41 +0100245 isolate(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000246 result,
247 NewRawOneByteString(string.length(), pretenure),
248 String);
249
250 DisallowHeapAllocation no_gc;
251 // Copy the characters into the new object.
252 CopyChars(SeqOneByteString::cast(*result)->GetChars(),
253 string.start(),
254 length);
255 return result;
256}
257
258MaybeHandle<String> Factory::NewStringFromUtf8(Vector<const char> string,
259 PretenureFlag pretenure) {
260 // Check for ASCII first since this is the common case.
261 const char* start = string.start();
262 int length = string.length();
263 int non_ascii_start = String::NonAsciiStart(start, length);
264 if (non_ascii_start >= length) {
265 // If the string is ASCII, we do not need to convert the characters
266 // since UTF8 is backwards compatible with ASCII.
267 return NewStringFromOneByte(Vector<const uint8_t>::cast(string), pretenure);
268 }
269
270 // Non-ASCII and we need to decode.
271 Access<UnicodeCache::Utf8Decoder>
272 decoder(isolate()->unicode_cache()->utf8_decoder());
273 decoder->Reset(string.start() + non_ascii_start,
274 length - non_ascii_start);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000275 int utf16_length = static_cast<int>(decoder->Utf16Length());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000276 DCHECK(utf16_length > 0);
277 // Allocate string.
278 Handle<SeqTwoByteString> result;
279 ASSIGN_RETURN_ON_EXCEPTION(
280 isolate(), result,
281 NewRawTwoByteString(non_ascii_start + utf16_length, pretenure),
282 String);
283 // Copy ASCII portion.
284 uint16_t* data = result->GetChars();
285 const char* ascii_data = string.start();
286 for (int i = 0; i < non_ascii_start; i++) {
287 *data++ = *ascii_data++;
288 }
289 // Now write the remainder.
290 decoder->WriteUtf16(data, utf16_length);
291 return result;
Leon Clarkeac952652010-07-15 11:15:24 +0100292}
293
294
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000295MaybeHandle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
296 PretenureFlag pretenure) {
297 int length = string.length();
298 const uc16* start = string.start();
299 if (String::IsOneByte(start, length)) {
300 if (length == 1) return LookupSingleCharacterStringFromCode(string[0]);
301 Handle<SeqOneByteString> result;
302 ASSIGN_RETURN_ON_EXCEPTION(
303 isolate(),
304 result,
305 NewRawOneByteString(length, pretenure),
306 String);
307 CopyChars(result->GetChars(), start, length);
308 return result;
309 } else {
310 Handle<SeqTwoByteString> result;
311 ASSIGN_RETURN_ON_EXCEPTION(
312 isolate(),
313 result,
314 NewRawTwoByteString(length, pretenure),
315 String);
316 CopyChars(result->GetChars(), start, length);
317 return result;
318 }
319}
320
321
322Handle<String> Factory::NewInternalizedStringFromUtf8(Vector<const char> str,
323 int chars,
324 uint32_t hash_field) {
325 CALL_HEAP_FUNCTION(
326 isolate(),
327 isolate()->heap()->AllocateInternalizedStringFromUtf8(
328 str, chars, hash_field),
329 String);
330}
331
332
333MUST_USE_RESULT Handle<String> Factory::NewOneByteInternalizedString(
334 Vector<const uint8_t> str,
335 uint32_t hash_field) {
336 CALL_HEAP_FUNCTION(
337 isolate(),
338 isolate()->heap()->AllocateOneByteInternalizedString(str, hash_field),
339 String);
340}
341
342
343MUST_USE_RESULT Handle<String> Factory::NewOneByteInternalizedSubString(
344 Handle<SeqOneByteString> string, int offset, int length,
345 uint32_t hash_field) {
346 CALL_HEAP_FUNCTION(
347 isolate(), isolate()->heap()->AllocateOneByteInternalizedString(
348 Vector<const uint8_t>(string->GetChars() + offset, length),
349 hash_field),
350 String);
351}
352
353
354MUST_USE_RESULT Handle<String> Factory::NewTwoByteInternalizedString(
355 Vector<const uc16> str,
356 uint32_t hash_field) {
357 CALL_HEAP_FUNCTION(
358 isolate(),
359 isolate()->heap()->AllocateTwoByteInternalizedString(str, hash_field),
360 String);
361}
362
363
364Handle<String> Factory::NewInternalizedStringImpl(
365 Handle<String> string, int chars, uint32_t hash_field) {
366 CALL_HEAP_FUNCTION(
367 isolate(),
368 isolate()->heap()->AllocateInternalizedStringImpl(
369 *string, chars, hash_field),
370 String);
371}
372
373
374MaybeHandle<Map> Factory::InternalizedStringMapForString(
375 Handle<String> string) {
376 // If the string is in new space it cannot be used as internalized.
377 if (isolate()->heap()->InNewSpace(*string)) return MaybeHandle<Map>();
378
379 // Find the corresponding internalized string map for strings.
380 switch (string->map()->instance_type()) {
381 case STRING_TYPE: return internalized_string_map();
382 case ONE_BYTE_STRING_TYPE:
383 return one_byte_internalized_string_map();
384 case EXTERNAL_STRING_TYPE: return external_internalized_string_map();
385 case EXTERNAL_ONE_BYTE_STRING_TYPE:
386 return external_one_byte_internalized_string_map();
387 case EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE:
388 return external_internalized_string_with_one_byte_data_map();
389 case SHORT_EXTERNAL_STRING_TYPE:
390 return short_external_internalized_string_map();
391 case SHORT_EXTERNAL_ONE_BYTE_STRING_TYPE:
392 return short_external_one_byte_internalized_string_map();
393 case SHORT_EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE:
394 return short_external_internalized_string_with_one_byte_data_map();
395 default: return MaybeHandle<Map>(); // No match found.
396 }
397}
398
399
400MaybeHandle<SeqOneByteString> Factory::NewRawOneByteString(
401 int length, PretenureFlag pretenure) {
402 if (length > String::kMaxLength || length < 0) {
403 THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), SeqOneByteString);
404 }
405 CALL_HEAP_FUNCTION(
406 isolate(),
407 isolate()->heap()->AllocateRawOneByteString(length, pretenure),
408 SeqOneByteString);
409}
410
411
412MaybeHandle<SeqTwoByteString> Factory::NewRawTwoByteString(
413 int length, PretenureFlag pretenure) {
414 if (length > String::kMaxLength || length < 0) {
415 THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), SeqTwoByteString);
416 }
Steve Block44f0eee2011-05-26 01:26:41 +0100417 CALL_HEAP_FUNCTION(
418 isolate(),
419 isolate()->heap()->AllocateRawTwoByteString(length, pretenure),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000420 SeqTwoByteString);
Steve Blocka7e24c12009-10-30 11:49:00 +0000421}
422
423
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000424Handle<String> Factory::LookupSingleCharacterStringFromCode(uint32_t code) {
425 if (code <= String::kMaxOneByteCharCodeU) {
426 {
427 DisallowHeapAllocation no_allocation;
428 Object* value = single_character_string_cache()->get(code);
429 if (value != *undefined_value()) {
430 return handle(String::cast(value), isolate());
431 }
432 }
433 uint8_t buffer[1];
434 buffer[0] = static_cast<uint8_t>(code);
435 Handle<String> result =
436 InternalizeOneByteString(Vector<const uint8_t>(buffer, 1));
437 single_character_string_cache()->set(code, *result);
438 return result;
439 }
440 DCHECK(code <= String::kMaxUtf16CodeUnitU);
441
442 Handle<SeqTwoByteString> result = NewRawTwoByteString(1).ToHandleChecked();
443 result->SeqTwoByteStringSet(0, static_cast<uint16_t>(code));
444 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000445}
446
447
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000448// Returns true for a character in a range. Both limits are inclusive.
449static inline bool Between(uint32_t character, uint32_t from, uint32_t to) {
450 // This makes uses of the the unsigned wraparound.
451 return character - from <= to - from;
452}
453
454
455static inline Handle<String> MakeOrFindTwoCharacterString(Isolate* isolate,
456 uint16_t c1,
457 uint16_t c2) {
458 // Numeric strings have a different hash algorithm not known by
459 // LookupTwoCharsStringIfExists, so we skip this step for such strings.
460 if (!Between(c1, '0', '9') || !Between(c2, '0', '9')) {
461 Handle<String> result;
462 if (StringTable::LookupTwoCharsStringIfExists(isolate, c1, c2).
463 ToHandle(&result)) {
464 return result;
465 }
466 }
467
468 // Now we know the length is 2, we might as well make use of that fact
469 // when building the new string.
470 if (static_cast<unsigned>(c1 | c2) <= String::kMaxOneByteCharCodeU) {
471 // We can do this.
472 DCHECK(base::bits::IsPowerOfTwo32(String::kMaxOneByteCharCodeU +
473 1)); // because of this.
474 Handle<SeqOneByteString> str =
475 isolate->factory()->NewRawOneByteString(2).ToHandleChecked();
476 uint8_t* dest = str->GetChars();
477 dest[0] = static_cast<uint8_t>(c1);
478 dest[1] = static_cast<uint8_t>(c2);
479 return str;
480 } else {
481 Handle<SeqTwoByteString> str =
482 isolate->factory()->NewRawTwoByteString(2).ToHandleChecked();
483 uc16* dest = str->GetChars();
484 dest[0] = c1;
485 dest[1] = c2;
486 return str;
487 }
488}
489
490
491template<typename SinkChar, typename StringType>
492Handle<String> ConcatStringContent(Handle<StringType> result,
493 Handle<String> first,
494 Handle<String> second) {
495 DisallowHeapAllocation pointer_stays_valid;
496 SinkChar* sink = result->GetChars();
497 String::WriteToFlat(*first, sink, 0, first->length());
498 String::WriteToFlat(*second, sink + first->length(), 0, second->length());
499 return result;
500}
501
502
503MaybeHandle<String> Factory::NewConsString(Handle<String> left,
504 Handle<String> right) {
505 int left_length = left->length();
506 if (left_length == 0) return right;
507 int right_length = right->length();
508 if (right_length == 0) return left;
509
510 int length = left_length + right_length;
511
512 if (length == 2) {
513 uint16_t c1 = left->Get(0);
514 uint16_t c2 = right->Get(0);
515 return MakeOrFindTwoCharacterString(isolate(), c1, c2);
516 }
517
518 // Make sure that an out of memory exception is thrown if the length
519 // of the new cons string is too large.
520 if (length > String::kMaxLength || length < 0) {
521 THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), String);
522 }
523
524 bool left_is_one_byte = left->IsOneByteRepresentation();
525 bool right_is_one_byte = right->IsOneByteRepresentation();
526 bool is_one_byte = left_is_one_byte && right_is_one_byte;
527 bool is_one_byte_data_in_two_byte_string = false;
528 if (!is_one_byte) {
529 // At least one of the strings uses two-byte representation so we
530 // can't use the fast case code for short one-byte strings below, but
531 // we can try to save memory if all chars actually fit in one-byte.
532 is_one_byte_data_in_two_byte_string =
533 left->HasOnlyOneByteChars() && right->HasOnlyOneByteChars();
534 if (is_one_byte_data_in_two_byte_string) {
535 isolate()->counters()->string_add_runtime_ext_to_one_byte()->Increment();
536 }
537 }
538
539 // If the resulting string is small make a flat string.
540 if (length < ConsString::kMinLength) {
541 // Note that neither of the two inputs can be a slice because:
542 STATIC_ASSERT(ConsString::kMinLength <= SlicedString::kMinLength);
543 DCHECK(left->IsFlat());
544 DCHECK(right->IsFlat());
545
546 STATIC_ASSERT(ConsString::kMinLength <= String::kMaxLength);
547 if (is_one_byte) {
548 Handle<SeqOneByteString> result =
549 NewRawOneByteString(length).ToHandleChecked();
550 DisallowHeapAllocation no_gc;
551 uint8_t* dest = result->GetChars();
552 // Copy left part.
553 const uint8_t* src =
554 left->IsExternalString()
555 ? Handle<ExternalOneByteString>::cast(left)->GetChars()
556 : Handle<SeqOneByteString>::cast(left)->GetChars();
557 for (int i = 0; i < left_length; i++) *dest++ = src[i];
558 // Copy right part.
559 src = right->IsExternalString()
560 ? Handle<ExternalOneByteString>::cast(right)->GetChars()
561 : Handle<SeqOneByteString>::cast(right)->GetChars();
562 for (int i = 0; i < right_length; i++) *dest++ = src[i];
563 return result;
564 }
565
566 return (is_one_byte_data_in_two_byte_string)
567 ? ConcatStringContent<uint8_t>(
568 NewRawOneByteString(length).ToHandleChecked(), left, right)
569 : ConcatStringContent<uc16>(
570 NewRawTwoByteString(length).ToHandleChecked(), left, right);
571 }
572
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000573 Handle<ConsString> result =
574 (is_one_byte || is_one_byte_data_in_two_byte_string)
575 ? New<ConsString>(cons_one_byte_string_map(), NEW_SPACE)
576 : New<ConsString>(cons_string_map(), NEW_SPACE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000577
578 DisallowHeapAllocation no_gc;
579 WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc);
580
581 result->set_hash_field(String::kEmptyHashField);
582 result->set_length(length);
583 result->set_first(*left, mode);
584 result->set_second(*right, mode);
585 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000586}
587
588
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000589Handle<String> Factory::NewProperSubString(Handle<String> str,
590 int begin,
591 int end) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000592#if VERIFY_HEAP
593 if (FLAG_verify_heap) str->StringVerify();
594#endif
595 DCHECK(begin > 0 || end < str->length());
596
597 str = String::Flatten(str);
598
599 int length = end - begin;
600 if (length <= 0) return empty_string();
601 if (length == 1) {
602 return LookupSingleCharacterStringFromCode(str->Get(begin));
603 }
604 if (length == 2) {
605 // Optimization for 2-byte strings often used as keys in a decompression
606 // dictionary. Check whether we already have the string in the string
607 // table to prevent creation of many unnecessary strings.
608 uint16_t c1 = str->Get(begin);
609 uint16_t c2 = str->Get(begin + 1);
610 return MakeOrFindTwoCharacterString(isolate(), c1, c2);
611 }
612
613 if (!FLAG_string_slices || length < SlicedString::kMinLength) {
614 if (str->IsOneByteRepresentation()) {
615 Handle<SeqOneByteString> result =
616 NewRawOneByteString(length).ToHandleChecked();
617 uint8_t* dest = result->GetChars();
618 DisallowHeapAllocation no_gc;
619 String::WriteToFlat(*str, dest, begin, end);
620 return result;
621 } else {
622 Handle<SeqTwoByteString> result =
623 NewRawTwoByteString(length).ToHandleChecked();
624 uc16* dest = result->GetChars();
625 DisallowHeapAllocation no_gc;
626 String::WriteToFlat(*str, dest, begin, end);
627 return result;
628 }
629 }
630
631 int offset = begin;
632
633 if (str->IsSlicedString()) {
634 Handle<SlicedString> slice = Handle<SlicedString>::cast(str);
635 str = Handle<String>(slice->parent(), isolate());
636 offset += slice->offset();
637 }
638
639 DCHECK(str->IsSeqString() || str->IsExternalString());
640 Handle<Map> map = str->IsOneByteRepresentation()
641 ? sliced_one_byte_string_map()
642 : sliced_string_map();
643 Handle<SlicedString> slice = New<SlicedString>(map, NEW_SPACE);
644
645 slice->set_hash_field(String::kEmptyHashField);
646 slice->set_length(length);
647 slice->set_parent(*str);
648 slice->set_offset(offset);
649 return slice;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000650}
651
652
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000653MaybeHandle<String> Factory::NewExternalStringFromOneByte(
654 const ExternalOneByteString::Resource* resource) {
655 size_t length = resource->length();
656 if (length > static_cast<size_t>(String::kMaxLength)) {
657 THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), String);
658 }
659
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000660 Handle<Map> map;
661 if (resource->IsCompressible()) {
662 // TODO(hajimehoshi): Rename this to 'uncached_external_one_byte_string_map'
663 map = short_external_one_byte_string_map();
664 } else {
665 map = external_one_byte_string_map();
666 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000667 Handle<ExternalOneByteString> external_string =
668 New<ExternalOneByteString>(map, NEW_SPACE);
669 external_string->set_length(static_cast<int>(length));
670 external_string->set_hash_field(String::kEmptyHashField);
671 external_string->set_resource(resource);
672
673 return external_string;
Steve Blocka7e24c12009-10-30 11:49:00 +0000674}
675
676
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000677MaybeHandle<String> Factory::NewExternalStringFromTwoByte(
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100678 const ExternalTwoByteString::Resource* resource) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000679 size_t length = resource->length();
680 if (length > static_cast<size_t>(String::kMaxLength)) {
681 THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), String);
682 }
683
684 // For small strings we check whether the resource contains only
685 // one byte characters. If yes, we use a different string map.
686 static const size_t kOneByteCheckLengthLimit = 32;
687 bool is_one_byte = length <= kOneByteCheckLengthLimit &&
688 String::IsOneByte(resource->data(), static_cast<int>(length));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000689 Handle<Map> map;
690 if (resource->IsCompressible()) {
691 // TODO(hajimehoshi): Rename these to 'uncached_external_string_...'.
692 map = is_one_byte ? short_external_string_with_one_byte_data_map()
693 : short_external_string_map();
694 } else {
695 map = is_one_byte ? external_string_with_one_byte_data_map()
696 : external_string_map();
697 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000698 Handle<ExternalTwoByteString> external_string =
699 New<ExternalTwoByteString>(map, NEW_SPACE);
700 external_string->set_length(static_cast<int>(length));
701 external_string->set_hash_field(String::kEmptyHashField);
702 external_string->set_resource(resource);
703
704 return external_string;
Steve Blocka7e24c12009-10-30 11:49:00 +0000705}
706
707
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000708Handle<Symbol> Factory::NewSymbol() {
Steve Block44f0eee2011-05-26 01:26:41 +0100709 CALL_HEAP_FUNCTION(
710 isolate(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000711 isolate()->heap()->AllocateSymbol(),
712 Symbol);
713}
714
715
716Handle<Symbol> Factory::NewPrivateSymbol() {
717 Handle<Symbol> symbol = NewSymbol();
718 symbol->set_is_private(true);
719 return symbol;
720}
721
722
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000723Handle<Context> Factory::NewNativeContext() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000724 Handle<FixedArray> array =
725 NewFixedArray(Context::NATIVE_CONTEXT_SLOTS, TENURED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000726 array->set_map_no_write_barrier(*native_context_map());
727 Handle<Context> context = Handle<Context>::cast(array);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000728 context->set_native_context(*context);
729 context->set_errors_thrown(Smi::FromInt(0));
730 Handle<WeakCell> weak_cell = NewWeakCell(context);
731 context->set_self_weak_cell(*weak_cell);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000732 DCHECK(context->IsNativeContext());
733 return context;
734}
735
736
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400737Handle<Context> Factory::NewScriptContext(Handle<JSFunction> function,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000738 Handle<ScopeInfo> scope_info) {
739 Handle<FixedArray> array =
740 NewFixedArray(scope_info->ContextLength(), TENURED);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400741 array->set_map_no_write_barrier(*script_context_map());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000742 Handle<Context> context = Handle<Context>::cast(array);
743 context->set_closure(*function);
744 context->set_previous(function->context());
745 context->set_extension(*scope_info);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000746 context->set_native_context(function->native_context());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400747 DCHECK(context->IsScriptContext());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000748 return context;
749}
750
751
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400752Handle<ScriptContextTable> Factory::NewScriptContextTable() {
753 Handle<FixedArray> array = NewFixedArray(1);
754 array->set_map_no_write_barrier(*script_context_table_map());
755 Handle<ScriptContextTable> context_table =
756 Handle<ScriptContextTable>::cast(array);
757 context_table->set_used(0);
758 return context_table;
759}
760
761
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000762Handle<Context> Factory::NewModuleContext(Handle<ScopeInfo> scope_info) {
763 Handle<FixedArray> array =
764 NewFixedArray(scope_info->ContextLength(), TENURED);
765 array->set_map_no_write_barrier(*module_context_map());
766 // Instance link will be set later.
767 Handle<Context> context = Handle<Context>::cast(array);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000768 context->set_extension(*the_hole_value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000769 return context;
Steve Blocka7e24c12009-10-30 11:49:00 +0000770}
771
772
773Handle<Context> Factory::NewFunctionContext(int length,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000774 Handle<JSFunction> function) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000775 DCHECK(length >= Context::MIN_CONTEXT_SLOTS);
776 Handle<FixedArray> array = NewFixedArray(length);
777 array->set_map_no_write_barrier(*function_context_map());
778 Handle<Context> context = Handle<Context>::cast(array);
779 context->set_closure(*function);
780 context->set_previous(function->context());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000781 context->set_extension(*the_hole_value());
782 context->set_native_context(function->native_context());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000783 return context;
Steve Blocka7e24c12009-10-30 11:49:00 +0000784}
785
786
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000787Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function,
788 Handle<Context> previous,
789 Handle<String> name,
790 Handle<Object> thrown_object) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000791 STATIC_ASSERT(Context::MIN_CONTEXT_SLOTS == Context::THROWN_OBJECT_INDEX);
792 Handle<FixedArray> array = NewFixedArray(Context::MIN_CONTEXT_SLOTS + 1);
793 array->set_map_no_write_barrier(*catch_context_map());
794 Handle<Context> context = Handle<Context>::cast(array);
795 context->set_closure(*function);
796 context->set_previous(*previous);
797 context->set_extension(*name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000798 context->set_native_context(previous->native_context());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000799 context->set(Context::THROWN_OBJECT_INDEX, *thrown_object);
800 return context;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000801}
802
803
804Handle<Context> Factory::NewWithContext(Handle<JSFunction> function,
805 Handle<Context> previous,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000806 Handle<JSReceiver> extension) {
807 Handle<FixedArray> array = NewFixedArray(Context::MIN_CONTEXT_SLOTS);
808 array->set_map_no_write_barrier(*with_context_map());
809 Handle<Context> context = Handle<Context>::cast(array);
810 context->set_closure(*function);
811 context->set_previous(*previous);
812 context->set_extension(*extension);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000813 context->set_native_context(previous->native_context());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000814 return context;
Steve Blocka7e24c12009-10-30 11:49:00 +0000815}
816
817
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000818Handle<Context> Factory::NewBlockContext(Handle<JSFunction> function,
819 Handle<Context> previous,
820 Handle<ScopeInfo> scope_info) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000821 Handle<FixedArray> array = NewFixedArray(scope_info->ContextLength());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000822 array->set_map_no_write_barrier(*block_context_map());
823 Handle<Context> context = Handle<Context>::cast(array);
824 context->set_closure(*function);
825 context->set_previous(*previous);
826 context->set_extension(*scope_info);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000827 context->set_native_context(previous->native_context());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000828 return context;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000829}
830
831
Steve Blocka7e24c12009-10-30 11:49:00 +0000832Handle<Struct> Factory::NewStruct(InstanceType type) {
Steve Block44f0eee2011-05-26 01:26:41 +0100833 CALL_HEAP_FUNCTION(
834 isolate(),
835 isolate()->heap()->AllocateStruct(type),
836 Struct);
Steve Blocka7e24c12009-10-30 11:49:00 +0000837}
838
839
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000840Handle<CodeCache> Factory::NewCodeCache() {
841 Handle<CodeCache> code_cache =
842 Handle<CodeCache>::cast(NewStruct(CODE_CACHE_TYPE));
843 code_cache->set_default_cache(*empty_fixed_array(), SKIP_WRITE_BARRIER);
844 code_cache->set_normal_type_cache(*undefined_value(), SKIP_WRITE_BARRIER);
845 return code_cache;
846}
847
848
849Handle<AliasedArgumentsEntry> Factory::NewAliasedArgumentsEntry(
850 int aliased_context_slot) {
851 Handle<AliasedArgumentsEntry> entry = Handle<AliasedArgumentsEntry>::cast(
852 NewStruct(ALIASED_ARGUMENTS_ENTRY_TYPE));
853 entry->set_aliased_context_slot(aliased_context_slot);
854 return entry;
855}
856
857
Ben Murdoch097c5b22016-05-18 11:27:45 +0100858Handle<AccessorInfo> Factory::NewAccessorInfo() {
859 Handle<AccessorInfo> info =
860 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
Steve Blocka7e24c12009-10-30 11:49:00 +0000861 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
862 return info;
863}
864
865
866Handle<Script> Factory::NewScript(Handle<String> source) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000867 // Create and initialize script object.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000868 Heap* heap = isolate()->heap();
Steve Blocka7e24c12009-10-30 11:49:00 +0000869 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
870 script->set_source(*source);
Steve Block44f0eee2011-05-26 01:26:41 +0100871 script->set_name(heap->undefined_value());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000872 script->set_id(isolate()->heap()->NextScriptId());
873 script->set_line_offset(0);
874 script->set_column_offset(0);
Steve Block44f0eee2011-05-26 01:26:41 +0100875 script->set_context_data(heap->undefined_value());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000876 script->set_type(Script::TYPE_NORMAL);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400877 script->set_wrapper(heap->undefined_value());
Steve Block44f0eee2011-05-26 01:26:41 +0100878 script->set_line_ends(heap->undefined_value());
879 script->set_eval_from_shared(heap->undefined_value());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000880 script->set_eval_from_instructions_offset(0);
881 script->set_shared_function_infos(Smi::FromInt(0));
882 script->set_flags(0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000883
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000884 heap->set_script_list(*WeakFixedArray::Add(script_list(), script));
Steve Blocka7e24c12009-10-30 11:49:00 +0000885 return script;
886}
887
888
Ben Murdoch257744e2011-11-30 15:57:28 +0000889Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100890 CALL_HEAP_FUNCTION(isolate(),
Ben Murdoch257744e2011-11-30 15:57:28 +0000891 isolate()->heap()->AllocateForeign(addr, pretenure),
892 Foreign);
Steve Blocka7e24c12009-10-30 11:49:00 +0000893}
894
895
Ben Murdoch257744e2011-11-30 15:57:28 +0000896Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
897 return NewForeign((Address) desc, TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000898}
899
900
901Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000902 DCHECK(0 <= length);
Steve Block44f0eee2011-05-26 01:26:41 +0100903 CALL_HEAP_FUNCTION(
904 isolate(),
905 isolate()->heap()->AllocateByteArray(length, pretenure),
906 ByteArray);
Steve Blocka7e24c12009-10-30 11:49:00 +0000907}
908
909
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000910Handle<BytecodeArray> Factory::NewBytecodeArray(
911 int length, const byte* raw_bytecodes, int frame_size, int parameter_count,
912 Handle<FixedArray> constant_pool) {
913 DCHECK(0 <= length);
914 CALL_HEAP_FUNCTION(isolate(), isolate()->heap()->AllocateBytecodeArray(
915 length, raw_bytecodes, frame_size,
916 parameter_count, *constant_pool),
917 BytecodeArray);
918}
919
920
921Handle<FixedTypedArrayBase> Factory::NewFixedTypedArrayWithExternalPointer(
922 int length, ExternalArrayType array_type, void* external_pointer,
923 PretenureFlag pretenure) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000924 DCHECK(0 <= length && length <= Smi::kMaxValue);
Steve Block44f0eee2011-05-26 01:26:41 +0100925 CALL_HEAP_FUNCTION(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000926 isolate(), isolate()->heap()->AllocateFixedTypedArrayWithExternalPointer(
927 length, array_type, external_pointer, pretenure),
928 FixedTypedArrayBase);
Steve Block3ce2e202009-11-05 08:53:23 +0000929}
930
931
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000932Handle<FixedTypedArrayBase> Factory::NewFixedTypedArray(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000933 int length, ExternalArrayType array_type, bool initialize,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000934 PretenureFlag pretenure) {
935 DCHECK(0 <= length && length <= Smi::kMaxValue);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000936 CALL_HEAP_FUNCTION(isolate(), isolate()->heap()->AllocateFixedTypedArray(
937 length, array_type, initialize, pretenure),
938 FixedTypedArrayBase);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000939}
940
941
942Handle<Cell> Factory::NewCell(Handle<Object> value) {
943 AllowDeferredHandleDereference convert_to_cell;
944 CALL_HEAP_FUNCTION(
945 isolate(),
946 isolate()->heap()->AllocateCell(*value),
947 Cell);
948}
949
950
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000951Handle<PropertyCell> Factory::NewPropertyCell() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000952 CALL_HEAP_FUNCTION(
953 isolate(),
954 isolate()->heap()->AllocatePropertyCell(),
955 PropertyCell);
956}
957
958
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400959Handle<WeakCell> Factory::NewWeakCell(Handle<HeapObject> value) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000960 // It is safe to dereference the value because we are embedding it
961 // in cell and not inspecting its fields.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400962 AllowDeferredHandleDereference convert_to_cell;
963 CALL_HEAP_FUNCTION(isolate(), isolate()->heap()->AllocateWeakCell(*value),
964 WeakCell);
965}
966
967
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000968Handle<TransitionArray> Factory::NewTransitionArray(int capacity) {
969 CALL_HEAP_FUNCTION(isolate(),
970 isolate()->heap()->AllocateTransitionArray(capacity),
971 TransitionArray);
972}
973
974
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000975Handle<AllocationSite> Factory::NewAllocationSite() {
976 Handle<Map> map = allocation_site_map();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000977 Handle<AllocationSite> site = New<AllocationSite>(map, OLD_SPACE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000978 site->Initialize();
979
980 // Link the site
981 site->set_weak_next(isolate()->heap()->allocation_sites_list());
982 isolate()->heap()->set_allocation_sites_list(*site);
983 return site;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100984}
985
986
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100987Handle<Map> Factory::NewMap(InstanceType type,
988 int instance_size,
989 ElementsKind elements_kind) {
Steve Block44f0eee2011-05-26 01:26:41 +0100990 CALL_HEAP_FUNCTION(
991 isolate(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100992 isolate()->heap()->AllocateMap(type, instance_size, elements_kind),
Steve Block44f0eee2011-05-26 01:26:41 +0100993 Map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000994}
995
996
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000997Handle<JSObject> Factory::CopyJSObject(Handle<JSObject> object) {
998 CALL_HEAP_FUNCTION(isolate(),
999 isolate()->heap()->CopyJSObject(*object, NULL),
1000 JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +00001001}
1002
1003
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001004Handle<JSObject> Factory::CopyJSObjectWithAllocationSite(
1005 Handle<JSObject> object,
1006 Handle<AllocationSite> site) {
1007 CALL_HEAP_FUNCTION(isolate(),
1008 isolate()->heap()->CopyJSObject(
1009 *object,
1010 site.is_null() ? NULL : *site),
1011 JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +00001012}
1013
1014
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001015Handle<FixedArray> Factory::CopyFixedArrayWithMap(Handle<FixedArray> array,
1016 Handle<Map> map) {
1017 CALL_HEAP_FUNCTION(isolate(),
1018 isolate()->heap()->CopyFixedArrayWithMap(*array, *map),
1019 FixedArray);
Steve Block1e0659c2011-05-24 12:43:12 +01001020}
1021
1022
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001023Handle<FixedArray> Factory::CopyFixedArrayAndGrow(Handle<FixedArray> array,
1024 int grow_by,
1025 PretenureFlag pretenure) {
1026 CALL_HEAP_FUNCTION(isolate(), isolate()->heap()->CopyFixedArrayAndGrow(
1027 *array, grow_by, pretenure),
1028 FixedArray);
1029}
1030
Ben Murdoch097c5b22016-05-18 11:27:45 +01001031Handle<FixedArray> Factory::CopyFixedArrayUpTo(Handle<FixedArray> array,
1032 int new_len,
1033 PretenureFlag pretenure) {
1034 CALL_HEAP_FUNCTION(isolate(), isolate()->heap()->CopyFixedArrayUpTo(
1035 *array, new_len, pretenure),
1036 FixedArray);
1037}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001038
Steve Blocka7e24c12009-10-30 11:49:00 +00001039Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001040 CALL_HEAP_FUNCTION(isolate(),
1041 isolate()->heap()->CopyFixedArray(*array),
1042 FixedArray);
1043}
1044
1045
1046Handle<FixedArray> Factory::CopyAndTenureFixedCOWArray(
1047 Handle<FixedArray> array) {
1048 DCHECK(isolate()->heap()->InNewSpace(*array));
1049 CALL_HEAP_FUNCTION(isolate(),
1050 isolate()->heap()->CopyAndTenureFixedCOWArray(*array),
1051 FixedArray);
Steve Blocka7e24c12009-10-30 11:49:00 +00001052}
1053
1054
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001055Handle<FixedDoubleArray> Factory::CopyFixedDoubleArray(
1056 Handle<FixedDoubleArray> array) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001057 CALL_HEAP_FUNCTION(isolate(),
1058 isolate()->heap()->CopyFixedDoubleArray(*array),
1059 FixedDoubleArray);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001060}
1061
1062
Steve Blocka7e24c12009-10-30 11:49:00 +00001063Handle<Object> Factory::NewNumber(double value,
1064 PretenureFlag pretenure) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001065 // We need to distinguish the minus zero value and this cannot be
1066 // done after conversion to int. Doing this by comparing bit
1067 // patterns is faster than using fpclassify() et al.
1068 if (IsMinusZero(value)) return NewHeapNumber(-0.0, IMMUTABLE, pretenure);
1069
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001070 int int_value = FastD2IChecked(value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001071 if (value == int_value && Smi::IsValid(int_value)) {
1072 return handle(Smi::FromInt(int_value), isolate());
1073 }
1074
1075 // Materialize the value in the heap.
1076 return NewHeapNumber(value, IMMUTABLE, pretenure);
Steve Blocka7e24c12009-10-30 11:49:00 +00001077}
1078
1079
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001080Handle<Object> Factory::NewNumberFromInt(int32_t value,
1081 PretenureFlag pretenure) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001082 if (Smi::IsValid(value)) return handle(Smi::FromInt(value), isolate());
1083 // Bypass NewNumber to avoid various redundant checks.
1084 return NewHeapNumber(FastI2D(value), IMMUTABLE, pretenure);
Steve Blocka7e24c12009-10-30 11:49:00 +00001085}
1086
1087
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001088Handle<Object> Factory::NewNumberFromUint(uint32_t value,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001089 PretenureFlag pretenure) {
1090 int32_t int32v = static_cast<int32_t>(value);
1091 if (int32v >= 0 && Smi::IsValid(int32v)) {
1092 return handle(Smi::FromInt(int32v), isolate());
1093 }
1094 return NewHeapNumber(FastUI2D(value), IMMUTABLE, pretenure);
1095}
1096
1097
1098Handle<HeapNumber> Factory::NewHeapNumber(double value,
1099 MutableMode mode,
1100 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +01001101 CALL_HEAP_FUNCTION(
1102 isolate(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001103 isolate()->heap()->AllocateHeapNumber(value, mode, pretenure),
1104 HeapNumber);
Steve Blocka7e24c12009-10-30 11:49:00 +00001105}
1106
1107
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001108#define SIMD128_NEW_DEF(TYPE, Type, type, lane_count, lane_type) \
1109 Handle<Type> Factory::New##Type(lane_type lanes[lane_count], \
1110 PretenureFlag pretenure) { \
1111 CALL_HEAP_FUNCTION( \
1112 isolate(), isolate()->heap()->Allocate##Type(lanes, pretenure), Type); \
Steve Blocka7e24c12009-10-30 11:49:00 +00001113 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001114SIMD128_TYPES(SIMD128_NEW_DEF)
1115#undef SIMD128_NEW_DEF
Steve Blocka7e24c12009-10-30 11:49:00 +00001116
1117
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001118Handle<Object> Factory::NewError(Handle<JSFunction> constructor,
1119 MessageTemplate::Template template_index,
1120 Handle<Object> arg0, Handle<Object> arg1,
1121 Handle<Object> arg2) {
1122 HandleScope scope(isolate());
1123 if (isolate()->bootstrapper()->IsActive()) {
1124 // During bootstrapping we cannot construct error objects.
1125 return scope.CloseAndEscape(NewStringFromAsciiChecked(
1126 MessageTemplate::TemplateString(template_index)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001127 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001128
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001129 Handle<JSFunction> fun = isolate()->make_error_function();
1130 Handle<Object> message_type(Smi::FromInt(template_index), isolate());
1131 if (arg0.is_null()) arg0 = undefined_value();
1132 if (arg1.is_null()) arg1 = undefined_value();
1133 if (arg2.is_null()) arg2 = undefined_value();
1134 Handle<Object> argv[] = {constructor, message_type, arg0, arg1, arg2};
Steve Blocka7e24c12009-10-30 11:49:00 +00001135
1136 // Invoke the JavaScript factory method. If an exception is thrown while
1137 // running the factory method, use the exception as the result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001138 Handle<Object> result;
1139 MaybeHandle<Object> exception;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001140 if (!Execution::TryCall(isolate(), fun, undefined_value(), arraysize(argv),
1141 argv, &exception)
1142 .ToHandle(&result)) {
1143 Handle<Object> exception_obj;
1144 if (exception.ToHandle(&exception_obj)) {
1145 result = exception_obj;
1146 } else {
1147 result = undefined_value();
1148 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001149 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001150 return scope.CloseAndEscape(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00001151}
1152
1153
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001154Handle<Object> Factory::NewError(Handle<JSFunction> constructor,
1155 Handle<String> message) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001156 Handle<Object> argv[] = { message };
Steve Blocka7e24c12009-10-30 11:49:00 +00001157
1158 // Invoke the JavaScript factory method. If an exception is thrown while
1159 // running the factory method, use the exception as the result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001160 Handle<Object> result;
1161 MaybeHandle<Object> exception;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001162 if (!Execution::TryCall(isolate(), constructor, undefined_value(),
1163 arraysize(argv), argv, &exception)
1164 .ToHandle(&result)) {
1165 Handle<Object> exception_obj;
1166 if (exception.ToHandle(&exception_obj)) return exception_obj;
1167 return undefined_value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001168 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001169 return result;
1170}
1171
1172
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001173#define DEFINE_ERROR(NAME, name) \
1174 Handle<Object> Factory::New##NAME(MessageTemplate::Template template_index, \
1175 Handle<Object> arg0, Handle<Object> arg1, \
1176 Handle<Object> arg2) { \
1177 return NewError(isolate()->name##_function(), template_index, arg0, arg1, \
1178 arg2); \
1179 }
1180DEFINE_ERROR(Error, error)
1181DEFINE_ERROR(EvalError, eval_error)
1182DEFINE_ERROR(RangeError, range_error)
1183DEFINE_ERROR(ReferenceError, reference_error)
1184DEFINE_ERROR(SyntaxError, syntax_error)
1185DEFINE_ERROR(TypeError, type_error)
1186#undef DEFINE_ERROR
Steve Blocka7e24c12009-10-30 11:49:00 +00001187
1188
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001189Handle<JSFunction> Factory::NewFunction(Handle<Map> map,
1190 Handle<SharedFunctionInfo> info,
1191 Handle<Context> context,
1192 PretenureFlag pretenure) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001193 AllocationSpace space = pretenure == TENURED ? OLD_SPACE : NEW_SPACE;
1194 Handle<JSFunction> function = New<JSFunction>(map, space);
1195
1196 function->initialize_properties();
1197 function->initialize_elements();
1198 function->set_shared(*info);
1199 function->set_code(info->code());
1200 function->set_context(*context);
1201 function->set_prototype_or_initial_map(*the_hole_value());
1202 function->set_literals(LiteralsArray::cast(*empty_fixed_array()));
1203 function->set_next_function_link(*undefined_value(), SKIP_WRITE_BARRIER);
1204 isolate()->heap()->InitializeJSObjectBody(*function, *map, JSFunction::kSize);
1205 return function;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001206}
Steve Blocka7e24c12009-10-30 11:49:00 +00001207
Steve Blocka7e24c12009-10-30 11:49:00 +00001208
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001209Handle<JSFunction> Factory::NewFunction(Handle<Map> map,
1210 Handle<String> name,
1211 MaybeHandle<Code> code) {
1212 Handle<Context> context(isolate()->native_context());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001213 Handle<SharedFunctionInfo> info =
1214 NewSharedFunctionInfo(name, code, map->is_constructor());
1215 DCHECK(is_sloppy(info->language_mode()));
1216 DCHECK(!map->IsUndefined());
1217 DCHECK(
1218 map.is_identical_to(isolate()->sloppy_function_map()) ||
1219 map.is_identical_to(isolate()->sloppy_function_without_prototype_map()) ||
1220 map.is_identical_to(
1221 isolate()->sloppy_function_with_readonly_prototype_map()) ||
1222 map.is_identical_to(isolate()->strict_function_map()) ||
1223 // TODO(titzer): wasm_function_map() could be undefined here. ugly.
1224 (*map == context->get(Context::WASM_FUNCTION_MAP_INDEX)) ||
1225 map.is_identical_to(isolate()->proxy_function_map()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001226 return NewFunction(map, info, context);
1227}
Steve Blocka7e24c12009-10-30 11:49:00 +00001228
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001229
1230Handle<JSFunction> Factory::NewFunction(Handle<String> name) {
1231 return NewFunction(
1232 isolate()->sloppy_function_map(), name, MaybeHandle<Code>());
Steve Blocka7e24c12009-10-30 11:49:00 +00001233}
1234
1235
Steve Block6ded16b2010-05-10 14:33:55 +01001236Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001237 Handle<Code> code,
1238 bool is_strict) {
1239 Handle<Map> map = is_strict
1240 ? isolate()->strict_function_without_prototype_map()
1241 : isolate()->sloppy_function_without_prototype_map();
1242 return NewFunction(map, name, code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001243}
1244
1245
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001246Handle<JSFunction> Factory::NewFunction(Handle<String> name, Handle<Code> code,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001247 Handle<Object> prototype,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001248 bool read_only_prototype,
1249 bool is_strict) {
1250 // In strict mode, readonly strict map is only available during bootstrap
1251 DCHECK(!is_strict || !read_only_prototype ||
1252 isolate()->bootstrapper()->IsActive());
1253 Handle<Map> map =
1254 is_strict ? isolate()->strict_function_map()
1255 : read_only_prototype
1256 ? isolate()->sloppy_function_with_readonly_prototype_map()
1257 : isolate()->sloppy_function_map();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001258 Handle<JSFunction> result = NewFunction(map, name, code);
1259 result->set_prototype_or_initial_map(*prototype);
1260 return result;
1261}
1262
1263
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001264Handle<JSFunction> Factory::NewFunction(Handle<String> name, Handle<Code> code,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001265 Handle<Object> prototype,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001266 InstanceType type, int instance_size,
1267 bool read_only_prototype,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001268 bool install_constructor,
1269 bool is_strict) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001270 // Allocate the function
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001271 Handle<JSFunction> function =
1272 NewFunction(name, code, prototype, read_only_prototype, is_strict);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001273
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001274 ElementsKind elements_kind =
1275 type == JS_ARRAY_TYPE ? FAST_SMI_ELEMENTS : FAST_HOLEY_SMI_ELEMENTS;
1276 Handle<Map> initial_map = NewMap(type, instance_size, elements_kind);
1277 if (!function->shared()->is_generator()) {
1278 if (prototype->IsTheHole()) {
1279 prototype = NewFunctionPrototype(function);
1280 } else if (install_constructor) {
1281 JSObject::AddProperty(Handle<JSObject>::cast(prototype),
1282 constructor_string(), function, DONT_ENUM);
1283 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001284 }
1285
1286 JSFunction::SetInitialMap(function, initial_map,
1287 Handle<JSReceiver>::cast(prototype));
1288
Steve Block6ded16b2010-05-10 14:33:55 +01001289 return function;
1290}
1291
1292
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001293Handle<JSFunction> Factory::NewFunction(Handle<String> name,
1294 Handle<Code> code,
1295 InstanceType type,
1296 int instance_size) {
1297 return NewFunction(name, code, the_hole_value(), type, instance_size);
1298}
1299
1300
1301Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
1302 // Make sure to use globals from the function's context, since the function
1303 // can be from a different context.
1304 Handle<Context> native_context(function->context()->native_context());
1305 Handle<Map> new_map;
1306 if (function->shared()->is_generator()) {
1307 // Generator prototypes can share maps since they don't have "constructor"
1308 // properties.
1309 new_map = handle(native_context->generator_object_prototype_map());
1310 } else {
1311 // Each function prototype gets a fresh map to avoid unwanted sharing of
1312 // maps between prototypes of different constructors.
1313 Handle<JSFunction> object_function(native_context->object_function());
1314 DCHECK(object_function->has_initial_map());
1315 new_map = handle(object_function->initial_map());
1316 }
1317
1318 DCHECK(!new_map->is_prototype_map());
1319 Handle<JSObject> prototype = NewJSObjectFromMap(new_map);
1320
1321 if (!function->shared()->is_generator()) {
1322 JSObject::AddProperty(prototype, constructor_string(), function, DONT_ENUM);
1323 }
1324
1325 return prototype;
1326}
1327
1328
1329Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
1330 Handle<SharedFunctionInfo> info,
1331 Handle<Context> context,
1332 PretenureFlag pretenure) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001333 int map_index =
1334 Context::FunctionMapIndex(info->language_mode(), info->kind());
1335 Handle<Map> initial_map(Map::cast(context->native_context()->get(map_index)));
1336
1337 return NewFunctionFromSharedFunctionInfo(initial_map, info, context,
1338 pretenure);
1339}
1340
1341
1342Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
1343 Handle<Map> initial_map, Handle<SharedFunctionInfo> info,
1344 Handle<Context> context, PretenureFlag pretenure) {
1345 DCHECK_EQ(JS_FUNCTION_TYPE, initial_map->instance_type());
1346 Handle<JSFunction> result =
1347 NewFunction(initial_map, info, context, pretenure);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001348
1349 if (info->ic_age() != isolate()->heap()->global_ic_age()) {
1350 info->ResetForNewContext(isolate()->heap()->global_ic_age());
1351 }
1352
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001353 if (FLAG_always_opt && info->allows_lazy_compilation()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001354 result->MarkForOptimization();
1355 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001356
1357 CodeAndLiterals cached = info->SearchOptimizedCodeMap(
1358 context->native_context(), BailoutId::None());
1359 if (cached.code != nullptr) {
1360 // Caching of optimized code enabled and optimized code found.
1361 DCHECK(!cached.code->marked_for_deoptimization());
1362 DCHECK(result->shared()->is_compiled());
1363 result->ReplaceCode(cached.code);
1364 }
1365
1366 if (cached.literals != nullptr) {
1367 result->set_literals(cached.literals);
1368 } else {
1369 int number_of_literals = info->num_literals();
1370 Handle<LiteralsArray> literals =
1371 LiteralsArray::New(isolate(), handle(info->feedback_vector()),
1372 number_of_literals, pretenure);
1373 result->set_literals(*literals);
1374
1375 // Cache context-specific literals.
1376 Handle<Context> native_context(context->native_context());
1377 SharedFunctionInfo::AddLiteralsToOptimizedCodeMap(info, native_context,
1378 literals);
1379 }
1380
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001381 return result;
1382}
1383
1384
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001385Handle<ScopeInfo> Factory::NewScopeInfo(int length) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001386 Handle<FixedArray> array = NewFixedArray(length, TENURED);
1387 array->set_map_no_write_barrier(*scope_info_map());
1388 Handle<ScopeInfo> scope_info = Handle<ScopeInfo>::cast(array);
1389 return scope_info;
1390}
1391
1392
1393Handle<JSObject> Factory::NewExternal(void* value) {
1394 Handle<Foreign> foreign = NewForeign(static_cast<Address>(value));
1395 Handle<JSObject> external = NewJSObjectFromMap(external_map());
1396 external->SetInternalField(0, *foreign);
1397 return external;
1398}
1399
1400
1401Handle<Code> Factory::NewCodeRaw(int object_size, bool immovable) {
1402 CALL_HEAP_FUNCTION(isolate(),
1403 isolate()->heap()->AllocateCode(object_size, immovable),
1404 Code);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001405}
1406
1407
Steve Blocka7e24c12009-10-30 11:49:00 +00001408Handle<Code> Factory::NewCode(const CodeDesc& desc,
Steve Blocka7e24c12009-10-30 11:49:00 +00001409 Code::Flags flags,
Steve Block44f0eee2011-05-26 01:26:41 +01001410 Handle<Object> self_ref,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001411 bool immovable,
1412 bool crankshafted,
1413 int prologue_offset,
1414 bool is_debug) {
1415 Handle<ByteArray> reloc_info = NewByteArray(desc.reloc_size, TENURED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001416
1417 // Compute size.
1418 int body_size = RoundUp(desc.instr_size, kObjectAlignment);
1419 int obj_size = Code::SizeFor(body_size);
1420
1421 Handle<Code> code = NewCodeRaw(obj_size, immovable);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001422 DCHECK(isolate()->code_range() == NULL || !isolate()->code_range()->valid() ||
1423 isolate()->code_range()->contains(code->address()) ||
1424 obj_size <= isolate()->heap()->code_space()->AreaSize());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001425
1426 // The code object has not been fully initialized yet. We rely on the
1427 // fact that no allocation will happen from this point on.
1428 DisallowHeapAllocation no_gc;
1429 code->set_gc_metadata(Smi::FromInt(0));
1430 code->set_ic_age(isolate()->heap()->global_ic_age());
1431 code->set_instruction_size(desc.instr_size);
1432 code->set_relocation_info(*reloc_info);
1433 code->set_flags(flags);
1434 code->set_raw_kind_specific_flags1(0);
1435 code->set_raw_kind_specific_flags2(0);
1436 code->set_is_crankshafted(crankshafted);
1437 code->set_deoptimization_data(*empty_fixed_array(), SKIP_WRITE_BARRIER);
1438 code->set_raw_type_feedback_info(Smi::FromInt(0));
1439 code->set_next_code_link(*undefined_value());
1440 code->set_handler_table(*empty_fixed_array(), SKIP_WRITE_BARRIER);
1441 code->set_prologue_offset(prologue_offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001442 code->set_constant_pool_offset(desc.instr_size - desc.constant_pool_size);
1443
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001444 if (code->kind() == Code::OPTIMIZED_FUNCTION) {
1445 code->set_marked_for_deoptimization(false);
1446 }
1447
1448 if (is_debug) {
1449 DCHECK(code->kind() == Code::FUNCTION);
1450 code->set_has_debug_break_slots(true);
1451 }
1452
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001453 // Allow self references to created code object by patching the handle to
1454 // point to the newly allocated Code object.
1455 if (!self_ref.is_null()) *(self_ref.location()) = *code;
1456
1457 // Migrate generated code.
1458 // The generated code can contain Object** values (typically from handles)
1459 // that are dereferenced during the copy to point directly to the actual heap
1460 // objects. These pointers can include references to the code object itself,
1461 // through the self_reference parameter.
1462 code->CopyFrom(desc);
1463
1464#ifdef VERIFY_HEAP
1465 if (FLAG_verify_heap) code->ObjectVerify();
1466#endif
1467 return code;
Steve Blocka7e24c12009-10-30 11:49:00 +00001468}
1469
1470
1471Handle<Code> Factory::CopyCode(Handle<Code> code) {
Steve Block44f0eee2011-05-26 01:26:41 +01001472 CALL_HEAP_FUNCTION(isolate(),
1473 isolate()->heap()->CopyCode(*code),
1474 Code);
Steve Blocka7e24c12009-10-30 11:49:00 +00001475}
1476
1477
Steve Block6ded16b2010-05-10 14:33:55 +01001478Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
Steve Block44f0eee2011-05-26 01:26:41 +01001479 CALL_HEAP_FUNCTION(isolate(),
1480 isolate()->heap()->CopyCode(*code, reloc_info),
1481 Code);
Steve Block6ded16b2010-05-10 14:33:55 +01001482}
1483
Ben Murdoch097c5b22016-05-18 11:27:45 +01001484Handle<BytecodeArray> Factory::CopyBytecodeArray(
1485 Handle<BytecodeArray> bytecode_array) {
1486 CALL_HEAP_FUNCTION(isolate(),
1487 isolate()->heap()->CopyBytecodeArray(*bytecode_array),
1488 BytecodeArray);
1489}
Steve Block6ded16b2010-05-10 14:33:55 +01001490
Steve Blocka7e24c12009-10-30 11:49:00 +00001491Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
1492 PretenureFlag pretenure) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001493 JSFunction::EnsureHasInitialMap(constructor);
Steve Block44f0eee2011-05-26 01:26:41 +01001494 CALL_HEAP_FUNCTION(
1495 isolate(),
1496 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +00001497}
1498
1499
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001500Handle<JSObject> Factory::NewJSObjectWithMemento(
1501 Handle<JSFunction> constructor,
1502 Handle<AllocationSite> site) {
1503 JSFunction::EnsureHasInitialMap(constructor);
Steve Block44f0eee2011-05-26 01:26:41 +01001504 CALL_HEAP_FUNCTION(
1505 isolate(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001506 isolate()->heap()->AllocateJSObject(*constructor, NOT_TENURED, *site),
Steve Block44f0eee2011-05-26 01:26:41 +01001507 JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +00001508}
1509
1510
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001511Handle<JSModule> Factory::NewJSModule(Handle<Context> context,
1512 Handle<ScopeInfo> scope_info) {
1513 // Allocate a fresh map. Modules do not have a prototype.
1514 Handle<Map> map = NewMap(JS_MODULE_TYPE, JSModule::kSize);
1515 // Allocate the object based on the map.
1516 Handle<JSModule> module =
1517 Handle<JSModule>::cast(NewJSObjectFromMap(map, TENURED));
1518 module->set_context(*context);
1519 module->set_scope_info(*scope_info);
1520 return module;
1521}
1522
1523
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001524Handle<JSGlobalObject> Factory::NewJSGlobalObject(
1525 Handle<JSFunction> constructor) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001526 DCHECK(constructor->has_initial_map());
1527 Handle<Map> map(constructor->initial_map());
1528 DCHECK(map->is_dictionary_map());
1529
1530 // Make sure no field properties are described in the initial map.
1531 // This guarantees us that normalizing the properties does not
1532 // require us to change property values to PropertyCells.
1533 DCHECK(map->NextFreePropertyIndex() == 0);
1534
1535 // Make sure we don't have a ton of pre-allocated slots in the
1536 // global objects. They will be unused once we normalize the object.
1537 DCHECK(map->unused_property_fields() == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001538 DCHECK(map->GetInObjectProperties() == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001539
1540 // Initial size of the backing store to avoid resize of the storage during
1541 // bootstrapping. The size differs between the JS global object ad the
1542 // builtins object.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001543 int initial_size = 64;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001544
1545 // Allocate a dictionary object for backing storage.
1546 int at_least_space_for = map->NumberOfOwnDescriptors() * 2 + initial_size;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001547 Handle<GlobalDictionary> dictionary =
1548 GlobalDictionary::New(isolate(), at_least_space_for);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001549
1550 // The global object might be created from an object template with accessors.
1551 // Fill these accessors into the dictionary.
1552 Handle<DescriptorArray> descs(map->instance_descriptors());
1553 for (int i = 0; i < map->NumberOfOwnDescriptors(); i++) {
1554 PropertyDetails details = descs->GetDetails(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001555 // Only accessors are expected.
1556 DCHECK_EQ(ACCESSOR_CONSTANT, details.type());
1557 PropertyDetails d(details.attributes(), ACCESSOR_CONSTANT, i + 1,
1558 PropertyCellType::kMutable);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001559 Handle<Name> name(descs->GetKey(i));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001560 Handle<PropertyCell> cell = NewPropertyCell();
1561 cell->set_value(descs->GetCallbacksObject(i));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001562 // |dictionary| already contains enough space for all properties.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001563 USE(GlobalDictionary::Add(dictionary, name, cell, d));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001564 }
1565
1566 // Allocate the global object and initialize it with the backing store.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001567 Handle<JSGlobalObject> global = New<JSGlobalObject>(map, OLD_SPACE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001568 isolate()->heap()->InitializeJSObjectFromMap(*global, *dictionary, *map);
1569
1570 // Create a new map for the global object.
1571 Handle<Map> new_map = Map::CopyDropDescriptors(map);
1572 new_map->set_dictionary_map(true);
1573
1574 // Set up the global object as a normalized object.
1575 global->set_map(*new_map);
1576 global->set_properties(*dictionary);
1577
1578 // Make sure result is a global object with properties in dictionary.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001579 DCHECK(global->IsJSGlobalObject() && !global->HasFastProperties());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001580 return global;
1581}
1582
1583
1584Handle<JSObject> Factory::NewJSObjectFromMap(
1585 Handle<Map> map,
1586 PretenureFlag pretenure,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001587 Handle<AllocationSite> allocation_site) {
1588 CALL_HEAP_FUNCTION(
1589 isolate(),
1590 isolate()->heap()->AllocateJSObjectFromMap(
1591 *map,
1592 pretenure,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001593 allocation_site.is_null() ? NULL : *allocation_site),
1594 JSObject);
1595}
1596
1597
1598Handle<JSArray> Factory::NewJSArray(ElementsKind elements_kind,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001599 Strength strength,
Steve Blocka7e24c12009-10-30 11:49:00 +00001600 PretenureFlag pretenure) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001601 Map* map = isolate()->get_initial_js_array_map(elements_kind, strength);
1602 if (map == nullptr) {
1603 DCHECK(strength == Strength::WEAK);
1604 Context* native_context = isolate()->context()->native_context();
1605 JSFunction* array_function = native_context->array_function();
1606 map = array_function->initial_map();
1607 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001608 return Handle<JSArray>::cast(NewJSObjectFromMap(handle(map), pretenure));
1609}
1610
1611
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001612Handle<JSArray> Factory::NewJSArray(ElementsKind elements_kind, int length,
1613 int capacity, Strength strength,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001614 ArrayStorageAllocationMode mode,
1615 PretenureFlag pretenure) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001616 Handle<JSArray> array = NewJSArray(elements_kind, strength, pretenure);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001617 NewJSArrayStorage(array, length, capacity, mode);
1618 return array;
Steve Blocka7e24c12009-10-30 11:49:00 +00001619}
1620
1621
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001622Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements,
1623 ElementsKind elements_kind,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001624 int length, Strength strength,
Steve Blocka7e24c12009-10-30 11:49:00 +00001625 PretenureFlag pretenure) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001626 DCHECK(length <= elements->length());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001627 Handle<JSArray> array = NewJSArray(elements_kind, strength, pretenure);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001628
1629 array->set_elements(*elements);
1630 array->set_length(Smi::FromInt(length));
1631 JSObject::ValidateElements(array);
1632 return array;
1633}
1634
1635
1636void Factory::NewJSArrayStorage(Handle<JSArray> array,
1637 int length,
1638 int capacity,
1639 ArrayStorageAllocationMode mode) {
1640 DCHECK(capacity >= length);
1641
1642 if (capacity == 0) {
1643 array->set_length(Smi::FromInt(0));
1644 array->set_elements(*empty_fixed_array());
1645 return;
1646 }
1647
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001648 HandleScope inner_scope(isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001649 Handle<FixedArrayBase> elms;
1650 ElementsKind elements_kind = array->GetElementsKind();
1651 if (IsFastDoubleElementsKind(elements_kind)) {
1652 if (mode == DONT_INITIALIZE_ARRAY_ELEMENTS) {
1653 elms = NewFixedDoubleArray(capacity);
1654 } else {
1655 DCHECK(mode == INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE);
1656 elms = NewFixedDoubleArrayWithHoles(capacity);
1657 }
1658 } else {
1659 DCHECK(IsFastSmiOrObjectElementsKind(elements_kind));
1660 if (mode == DONT_INITIALIZE_ARRAY_ELEMENTS) {
1661 elms = NewUninitializedFixedArray(capacity);
1662 } else {
1663 DCHECK(mode == INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE);
1664 elms = NewFixedArrayWithHoles(capacity);
1665 }
1666 }
1667
1668 array->set_elements(*elms);
1669 array->set_length(Smi::FromInt(length));
1670}
1671
1672
1673Handle<JSGeneratorObject> Factory::NewJSGeneratorObject(
1674 Handle<JSFunction> function) {
1675 DCHECK(function->shared()->is_generator());
1676 JSFunction::EnsureHasInitialMap(function);
1677 Handle<Map> map(function->initial_map());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001678 DCHECK_EQ(JS_GENERATOR_OBJECT_TYPE, map->instance_type());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001679 CALL_HEAP_FUNCTION(
1680 isolate(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001681 isolate()->heap()->AllocateJSObjectFromMap(*map),
1682 JSGeneratorObject);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001683}
1684
1685
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001686Handle<JSArrayBuffer> Factory::NewJSArrayBuffer(SharedFlag shared,
1687 PretenureFlag pretenure) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001688 Handle<JSFunction> array_buffer_fun(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001689 shared == SharedFlag::kShared
1690 ? isolate()->native_context()->shared_array_buffer_fun()
1691 : isolate()->native_context()->array_buffer_fun());
1692 CALL_HEAP_FUNCTION(isolate(), isolate()->heap()->AllocateJSObject(
1693 *array_buffer_fun, pretenure),
1694 JSArrayBuffer);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001695}
1696
1697
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001698Handle<JSDataView> Factory::NewJSDataView() {
1699 Handle<JSFunction> data_view_fun(
1700 isolate()->native_context()->data_view_fun());
1701 CALL_HEAP_FUNCTION(
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001702 isolate(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001703 isolate()->heap()->AllocateJSObject(*data_view_fun),
1704 JSDataView);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001705}
1706
1707
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001708Handle<JSMap> Factory::NewJSMap() {
1709 Handle<Map> map(isolate()->native_context()->js_map_map());
1710 Handle<JSMap> js_map = Handle<JSMap>::cast(NewJSObjectFromMap(map));
1711 JSMap::Initialize(js_map, isolate());
1712 return js_map;
1713}
1714
1715
1716Handle<JSSet> Factory::NewJSSet() {
1717 Handle<Map> map(isolate()->native_context()->js_set_map());
1718 Handle<JSSet> js_set = Handle<JSSet>::cast(NewJSObjectFromMap(map));
1719 JSSet::Initialize(js_set, isolate());
1720 return js_set;
1721}
1722
1723
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001724Handle<JSMapIterator> Factory::NewJSMapIterator() {
1725 Handle<Map> map(isolate()->native_context()->map_iterator_map());
1726 CALL_HEAP_FUNCTION(isolate(),
1727 isolate()->heap()->AllocateJSObjectFromMap(*map),
1728 JSMapIterator);
1729}
1730
1731
1732Handle<JSSetIterator> Factory::NewJSSetIterator() {
1733 Handle<Map> map(isolate()->native_context()->set_iterator_map());
1734 CALL_HEAP_FUNCTION(isolate(),
1735 isolate()->heap()->AllocateJSObjectFromMap(*map),
1736 JSSetIterator);
1737}
1738
1739
1740namespace {
1741
1742ElementsKind GetExternalArrayElementsKind(ExternalArrayType type) {
1743 switch (type) {
1744#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
1745 case kExternal##Type##Array: \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001746 return TYPE##_ELEMENTS;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001747 TYPED_ARRAYS(TYPED_ARRAY_CASE)
1748 }
1749 UNREACHABLE();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001750 return FIRST_FIXED_TYPED_ARRAY_ELEMENTS_KIND;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001751#undef TYPED_ARRAY_CASE
1752}
1753
1754
1755size_t GetExternalArrayElementSize(ExternalArrayType type) {
1756 switch (type) {
1757#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
1758 case kExternal##Type##Array: \
1759 return size;
1760 TYPED_ARRAYS(TYPED_ARRAY_CASE)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001761 default:
1762 UNREACHABLE();
1763 return 0;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001764 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001765#undef TYPED_ARRAY_CASE
1766}
1767
1768
1769size_t GetFixedTypedArraysElementSize(ElementsKind kind) {
1770 switch (kind) {
1771#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
1772 case TYPE##_ELEMENTS: \
1773 return size;
1774 TYPED_ARRAYS(TYPED_ARRAY_CASE)
1775 default:
1776 UNREACHABLE();
1777 return 0;
1778 }
1779#undef TYPED_ARRAY_CASE
1780}
1781
1782
1783ExternalArrayType GetArrayTypeFromElementsKind(ElementsKind kind) {
1784 switch (kind) {
1785#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
1786 case TYPE##_ELEMENTS: \
1787 return kExternal##Type##Array;
1788 TYPED_ARRAYS(TYPED_ARRAY_CASE)
1789 default:
1790 UNREACHABLE();
1791 return kExternalInt8Array;
1792 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001793#undef TYPED_ARRAY_CASE
1794}
1795
1796
1797JSFunction* GetTypedArrayFun(ExternalArrayType type, Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001798 Context* native_context = isolate->context()->native_context();
1799 switch (type) {
1800#define TYPED_ARRAY_FUN(Type, type, TYPE, ctype, size) \
1801 case kExternal##Type##Array: \
1802 return native_context->type##_array_fun();
1803
1804 TYPED_ARRAYS(TYPED_ARRAY_FUN)
1805#undef TYPED_ARRAY_FUN
1806
1807 default:
1808 UNREACHABLE();
1809 return NULL;
1810 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001811}
1812
1813
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001814JSFunction* GetTypedArrayFun(ElementsKind elements_kind, Isolate* isolate) {
1815 Context* native_context = isolate->context()->native_context();
1816 switch (elements_kind) {
1817#define TYPED_ARRAY_FUN(Type, type, TYPE, ctype, size) \
1818 case TYPE##_ELEMENTS: \
1819 return native_context->type##_array_fun();
1820
1821 TYPED_ARRAYS(TYPED_ARRAY_FUN)
1822#undef TYPED_ARRAY_FUN
1823
1824 default:
1825 UNREACHABLE();
1826 return NULL;
1827 }
1828}
1829
1830
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001831void SetupArrayBufferView(i::Isolate* isolate,
1832 i::Handle<i::JSArrayBufferView> obj,
1833 i::Handle<i::JSArrayBuffer> buffer,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001834 size_t byte_offset, size_t byte_length,
1835 PretenureFlag pretenure = NOT_TENURED) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001836 DCHECK(byte_offset + byte_length <=
1837 static_cast<size_t>(buffer->byte_length()->Number()));
1838
1839 obj->set_buffer(*buffer);
1840
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001841 i::Handle<i::Object> byte_offset_object =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001842 isolate->factory()->NewNumberFromSize(byte_offset, pretenure);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001843 obj->set_byte_offset(*byte_offset_object);
1844
1845 i::Handle<i::Object> byte_length_object =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001846 isolate->factory()->NewNumberFromSize(byte_length, pretenure);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001847 obj->set_byte_length(*byte_length_object);
1848}
1849
1850
1851} // namespace
1852
1853
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001854Handle<JSTypedArray> Factory::NewJSTypedArray(ExternalArrayType type,
1855 PretenureFlag pretenure) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001856 Handle<JSFunction> typed_array_fun_handle(GetTypedArrayFun(type, isolate()));
1857
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001858 CALL_HEAP_FUNCTION(isolate(), isolate()->heap()->AllocateJSObject(
1859 *typed_array_fun_handle, pretenure),
1860 JSTypedArray);
1861}
1862
1863
1864Handle<JSTypedArray> Factory::NewJSTypedArray(ElementsKind elements_kind,
1865 PretenureFlag pretenure) {
1866 Handle<JSFunction> typed_array_fun_handle(
1867 GetTypedArrayFun(elements_kind, isolate()));
1868
1869 CALL_HEAP_FUNCTION(isolate(), isolate()->heap()->AllocateJSObject(
1870 *typed_array_fun_handle, pretenure),
1871 JSTypedArray);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001872}
1873
1874
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001875Handle<JSTypedArray> Factory::NewJSTypedArray(ExternalArrayType type,
1876 Handle<JSArrayBuffer> buffer,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001877 size_t byte_offset, size_t length,
1878 PretenureFlag pretenure) {
1879 Handle<JSTypedArray> obj = NewJSTypedArray(type, pretenure);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001880
1881 size_t element_size = GetExternalArrayElementSize(type);
1882 ElementsKind elements_kind = GetExternalArrayElementsKind(type);
1883
1884 CHECK(byte_offset % element_size == 0);
1885
1886 CHECK(length <= (std::numeric_limits<size_t>::max() / element_size));
1887 CHECK(length <= static_cast<size_t>(Smi::kMaxValue));
1888 size_t byte_length = length * element_size;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001889 SetupArrayBufferView(isolate(), obj, buffer, byte_offset, byte_length,
1890 pretenure);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001891
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001892 Handle<Object> length_object = NewNumberFromSize(length, pretenure);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001893 obj->set_length(*length_object);
1894
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001895 Handle<FixedTypedArrayBase> elements = NewFixedTypedArrayWithExternalPointer(
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001896 static_cast<int>(length), type,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001897 static_cast<uint8_t*>(buffer->backing_store()) + byte_offset, pretenure);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001898 Handle<Map> map = JSObject::GetElementsTransitionMap(obj, elements_kind);
1899 JSObject::SetMapAndElements(obj, map, elements);
1900 return obj;
1901}
1902
1903
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001904Handle<JSTypedArray> Factory::NewJSTypedArray(ElementsKind elements_kind,
1905 size_t number_of_elements,
1906 PretenureFlag pretenure) {
1907 Handle<JSTypedArray> obj = NewJSTypedArray(elements_kind, pretenure);
1908
1909 size_t element_size = GetFixedTypedArraysElementSize(elements_kind);
1910 ExternalArrayType array_type = GetArrayTypeFromElementsKind(elements_kind);
1911
1912 CHECK(number_of_elements <=
1913 (std::numeric_limits<size_t>::max() / element_size));
1914 CHECK(number_of_elements <= static_cast<size_t>(Smi::kMaxValue));
1915 size_t byte_length = number_of_elements * element_size;
1916
1917 obj->set_byte_offset(Smi::FromInt(0));
1918 i::Handle<i::Object> byte_length_object =
1919 NewNumberFromSize(byte_length, pretenure);
1920 obj->set_byte_length(*byte_length_object);
1921 Handle<Object> length_object =
1922 NewNumberFromSize(number_of_elements, pretenure);
1923 obj->set_length(*length_object);
1924
1925 Handle<JSArrayBuffer> buffer =
1926 NewJSArrayBuffer(SharedFlag::kNotShared, pretenure);
1927 JSArrayBuffer::Setup(buffer, isolate(), true, NULL, byte_length,
1928 SharedFlag::kNotShared);
1929 obj->set_buffer(*buffer);
1930 Handle<FixedTypedArrayBase> elements = NewFixedTypedArray(
1931 static_cast<int>(number_of_elements), array_type, true, pretenure);
1932 obj->set_elements(*elements);
1933 return obj;
1934}
1935
1936
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001937Handle<JSDataView> Factory::NewJSDataView(Handle<JSArrayBuffer> buffer,
1938 size_t byte_offset,
1939 size_t byte_length) {
1940 Handle<JSDataView> obj = NewJSDataView();
1941 SetupArrayBufferView(isolate(), obj, buffer, byte_offset, byte_length);
1942 return obj;
1943}
1944
1945
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001946MaybeHandle<JSBoundFunction> Factory::NewJSBoundFunction(
1947 Handle<JSReceiver> target_function, Handle<Object> bound_this,
1948 Vector<Handle<Object>> bound_args) {
1949 DCHECK(target_function->IsCallable());
1950 STATIC_ASSERT(Code::kMaxArguments <= FixedArray::kMaxLength);
1951 if (bound_args.length() >= Code::kMaxArguments) {
1952 THROW_NEW_ERROR(isolate(),
1953 NewRangeError(MessageTemplate::kTooManyArguments),
1954 JSBoundFunction);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001955 }
1956
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001957 // Determine the prototype of the {target_function}.
1958 Handle<Object> prototype;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001959 ASSIGN_RETURN_ON_EXCEPTION(
1960 isolate(), prototype,
1961 JSReceiver::GetPrototype(isolate(), target_function), JSBoundFunction);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001962
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001963 // Create the [[BoundArguments]] for the result.
1964 Handle<FixedArray> bound_arguments;
1965 if (bound_args.length() == 0) {
1966 bound_arguments = empty_fixed_array();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001967 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001968 bound_arguments = NewFixedArray(bound_args.length());
1969 for (int i = 0; i < bound_args.length(); ++i) {
1970 bound_arguments->set(i, *bound_args[i]);
1971 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001972 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001973
1974 // Setup the map for the JSBoundFunction instance.
1975 Handle<Map> map = handle(
1976 target_function->IsConstructor()
1977 ? isolate()->native_context()->bound_function_with_constructor_map()
1978 : isolate()
1979 ->native_context()
1980 ->bound_function_without_constructor_map(),
1981 isolate());
1982 if (map->prototype() != *prototype) {
1983 map = Map::TransitionToPrototype(map, prototype, REGULAR_PROTOTYPE);
1984 }
1985 DCHECK_EQ(target_function->IsConstructor(), map->is_constructor());
1986
1987 // Setup the JSBoundFunction instance.
1988 Handle<JSBoundFunction> result =
1989 Handle<JSBoundFunction>::cast(NewJSObjectFromMap(map));
1990 result->set_bound_target_function(*target_function);
1991 result->set_bound_this(*bound_this);
1992 result->set_bound_arguments(*bound_arguments);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001993 result->set_length(Smi::FromInt(0));
1994 result->set_name(*undefined_value(), SKIP_WRITE_BARRIER);
1995 return result;
1996}
1997
1998
1999// ES6 section 9.5.15 ProxyCreate (target, handler)
2000Handle<JSProxy> Factory::NewJSProxy(Handle<JSReceiver> target,
2001 Handle<JSReceiver> handler) {
2002 // Allocate the proxy object.
2003 Handle<Map> map;
2004 if (target->IsCallable()) {
2005 if (target->IsConstructor()) {
2006 map = Handle<Map>(isolate()->proxy_constructor_map());
2007 } else {
2008 map = Handle<Map>(isolate()->proxy_callable_map());
2009 }
2010 } else {
2011 map = Handle<Map>(isolate()->proxy_map());
2012 }
2013 DCHECK(map->prototype()->IsNull());
2014 Handle<JSProxy> result = New<JSProxy>(map, NEW_SPACE);
2015 result->initialize_properties();
2016 result->set_target(*target);
2017 result->set_handler(*handler);
2018 result->set_hash(*undefined_value(), SKIP_WRITE_BARRIER);
2019 return result;
2020}
2021
2022
2023Handle<JSGlobalProxy> Factory::NewUninitializedJSGlobalProxy() {
2024 // Create an empty shell of a JSGlobalProxy that needs to be reinitialized
2025 // via ReinitializeJSGlobalProxy later.
2026 Handle<Map> map = NewMap(JS_GLOBAL_PROXY_TYPE, JSGlobalProxy::kSize);
2027 // Maintain invariant expected from any JSGlobalProxy.
2028 map->set_is_access_check_needed(true);
2029 CALL_HEAP_FUNCTION(
2030 isolate(), isolate()->heap()->AllocateJSObjectFromMap(*map, NOT_TENURED),
2031 JSGlobalProxy);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002032}
2033
2034
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002035void Factory::ReinitializeJSGlobalProxy(Handle<JSGlobalProxy> object,
2036 Handle<JSFunction> constructor) {
2037 DCHECK(constructor->has_initial_map());
2038 Handle<Map> map(constructor->initial_map(), isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002039 Handle<Map> old_map(object->map(), isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002040
2041 // The proxy's hash should be retained across reinitialization.
2042 Handle<Object> hash(object->hash(), isolate());
2043
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002044 JSObject::InvalidatePrototypeChains(*old_map);
2045 if (old_map->is_prototype_map()) {
2046 map = Map::Copy(map, "CopyAsPrototypeForJSGlobalProxy");
2047 map->set_is_prototype_map(true);
2048 }
2049 JSObject::UpdatePrototypeUserRegistration(old_map, map, isolate());
2050
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002051 // Check that the already allocated object has the same size and type as
2052 // objects allocated using the constructor.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002053 DCHECK(map->instance_size() == old_map->instance_size());
2054 DCHECK(map->instance_type() == old_map->instance_type());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002055
2056 // Allocate the backing storage for the properties.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002057 Handle<FixedArray> properties = empty_fixed_array();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002058
2059 // In order to keep heap in consistent state there must be no allocations
2060 // before object re-initialization is finished.
2061 DisallowHeapAllocation no_allocation;
2062
2063 // Reset the map for the object.
2064 object->synchronized_set_map(*map);
2065
2066 Heap* heap = isolate()->heap();
2067 // Reinitialize the object from the constructor map.
2068 heap->InitializeJSObjectFromMap(*object, *properties, *map);
2069
2070 // Restore the saved hash.
2071 object->set_hash(*hash);
2072}
2073
2074
Steve Block6ded16b2010-05-10 14:33:55 +01002075Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002076 Handle<String> name, int number_of_literals, FunctionKind kind,
2077 Handle<Code> code, Handle<ScopeInfo> scope_info,
2078 Handle<TypeFeedbackVector> feedback_vector) {
2079 DCHECK(IsValidFunctionKind(kind));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002080 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(
2081 name, code, IsConstructable(kind, scope_info->language_mode()));
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002082 shared->set_scope_info(*scope_info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002083 shared->set_feedback_vector(*feedback_vector);
2084 shared->set_kind(kind);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002085 shared->set_num_literals(number_of_literals);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002086 if (IsGeneratorFunction(kind)) {
2087 shared->set_instance_class_name(isolate()->heap()->Generator_string());
2088 shared->DisableOptimization(kGenerator);
2089 }
Steve Block6ded16b2010-05-10 14:33:55 +01002090 return shared;
2091}
2092
2093
Steve Block1e0659c2011-05-24 12:43:12 +01002094Handle<JSMessageObject> Factory::NewJSMessageObject(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002095 MessageTemplate::Template message, Handle<Object> argument,
2096 int start_position, int end_position, Handle<Object> script,
Steve Block1e0659c2011-05-24 12:43:12 +01002097 Handle<Object> stack_frames) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002098 Handle<Map> map = message_object_map();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002099 Handle<JSMessageObject> message_obj = New<JSMessageObject>(map, NEW_SPACE);
2100 message_obj->set_properties(*empty_fixed_array(), SKIP_WRITE_BARRIER);
2101 message_obj->initialize_elements();
2102 message_obj->set_elements(*empty_fixed_array(), SKIP_WRITE_BARRIER);
2103 message_obj->set_type(message);
2104 message_obj->set_argument(*argument);
2105 message_obj->set_start_position(start_position);
2106 message_obj->set_end_position(end_position);
2107 message_obj->set_script(*script);
2108 message_obj->set_stack_frames(*stack_frames);
2109 return message_obj;
Steve Blocka7e24c12009-10-30 11:49:00 +00002110}
2111
2112
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002113Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002114 Handle<String> name, MaybeHandle<Code> maybe_code, bool is_constructor) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002115 // Function names are assumed to be flat elsewhere. Must flatten before
2116 // allocating SharedFunctionInfo to avoid GC seeing the uninitialized SFI.
2117 name = String::Flatten(name, TENURED);
2118
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002119 Handle<Map> map = shared_function_info_map();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002120 Handle<SharedFunctionInfo> share = New<SharedFunctionInfo>(map, OLD_SPACE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002121
2122 // Set pointer fields.
2123 share->set_name(*name);
2124 Handle<Code> code;
2125 if (!maybe_code.ToHandle(&code)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002126 code = isolate()->builtins()->Illegal();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002127 }
2128 share->set_code(*code);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002129 share->set_optimized_code_map(*cleared_optimized_code_map());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002130 share->set_scope_info(ScopeInfo::Empty(isolate()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002131 Handle<Code> construct_stub =
2132 is_constructor ? isolate()->builtins()->JSConstructStubGeneric()
2133 : isolate()->builtins()->ConstructedNonConstructable();
2134 share->set_construct_stub(*construct_stub);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002135 share->set_instance_class_name(*Object_string());
2136 share->set_function_data(*undefined_value(), SKIP_WRITE_BARRIER);
2137 share->set_script(*undefined_value(), SKIP_WRITE_BARRIER);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002138 share->set_debug_info(DebugInfo::uninitialized(), SKIP_WRITE_BARRIER);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002139 share->set_inferred_name(*empty_string(), SKIP_WRITE_BARRIER);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002140 StaticFeedbackVectorSpec empty_spec;
2141 Handle<TypeFeedbackMetadata> feedback_metadata =
2142 TypeFeedbackMetadata::New(isolate(), &empty_spec);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002143 Handle<TypeFeedbackVector> feedback_vector =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002144 TypeFeedbackVector::New(isolate(), feedback_metadata);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002145 share->set_feedback_vector(*feedback_vector, SKIP_WRITE_BARRIER);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002146#if TRACE_MAPS
2147 share->set_unique_id(isolate()->GetNextUniqueSharedFunctionInfoId());
2148#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002149 share->set_profiler_ticks(0);
2150 share->set_ast_node_count(0);
2151 share->set_counters(0);
2152
2153 // Set integer fields (smi or int, depending on the architecture).
2154 share->set_length(0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002155 share->set_internal_formal_parameter_count(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002156 share->set_expected_nof_properties(0);
2157 share->set_num_literals(0);
2158 share->set_start_position_and_type(0);
2159 share->set_end_position(0);
2160 share->set_function_token_position(0);
2161 // All compiler hints default to false or 0.
2162 share->set_compiler_hints(0);
2163 share->set_opt_count_and_bailout_reason(0);
2164
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002165 // Link into the list.
2166 Handle<Object> new_noscript_list =
2167 WeakFixedArray::Add(noscript_shared_function_infos(), share);
2168 isolate()->heap()->set_noscript_shared_function_infos(*new_noscript_list);
2169
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002170 return share;
Steve Block6ded16b2010-05-10 14:33:55 +01002171}
2172
2173
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002174static inline int NumberCacheHash(Handle<FixedArray> cache,
2175 Handle<Object> number) {
2176 int mask = (cache->length() >> 1) - 1;
2177 if (number->IsSmi()) {
2178 return Handle<Smi>::cast(number)->value() & mask;
2179 } else {
2180 DoubleRepresentation rep(number->Number());
2181 return
2182 (static_cast<int>(rep.bits) ^ static_cast<int>(rep.bits >> 32)) & mask;
2183 }
Steve Block6ded16b2010-05-10 14:33:55 +01002184}
2185
2186
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002187Handle<Object> Factory::GetNumberStringCache(Handle<Object> number) {
2188 DisallowHeapAllocation no_gc;
2189 int hash = NumberCacheHash(number_string_cache(), number);
2190 Object* key = number_string_cache()->get(hash * 2);
2191 if (key == *number || (key->IsHeapNumber() && number->IsHeapNumber() &&
2192 key->Number() == number->Number())) {
2193 return Handle<String>(
2194 String::cast(number_string_cache()->get(hash * 2 + 1)), isolate());
2195 }
2196 return undefined_value();
Leon Clarkee46be812010-01-19 14:06:41 +00002197}
2198
2199
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002200void Factory::SetNumberStringCache(Handle<Object> number,
2201 Handle<String> string) {
2202 int hash = NumberCacheHash(number_string_cache(), number);
2203 if (number_string_cache()->get(hash * 2) != *undefined_value()) {
2204 int full_size = isolate()->heap()->FullSizeNumberStringCacheLength();
2205 if (number_string_cache()->length() != full_size) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002206 Handle<FixedArray> new_cache = NewFixedArray(full_size, TENURED);
2207 isolate()->heap()->set_number_string_cache(*new_cache);
2208 return;
2209 }
2210 }
2211 number_string_cache()->set(hash * 2, *number);
2212 number_string_cache()->set(hash * 2 + 1, *string);
Steve Blocka7e24c12009-10-30 11:49:00 +00002213}
2214
2215
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002216Handle<String> Factory::NumberToString(Handle<Object> number,
2217 bool check_number_string_cache) {
2218 isolate()->counters()->number_to_string_runtime()->Increment();
2219 if (check_number_string_cache) {
2220 Handle<Object> cached = GetNumberStringCache(number);
2221 if (!cached->IsUndefined()) return Handle<String>::cast(cached);
2222 }
2223
2224 char arr[100];
2225 Vector<char> buffer(arr, arraysize(arr));
2226 const char* str;
2227 if (number->IsSmi()) {
2228 int num = Handle<Smi>::cast(number)->value();
2229 str = IntToCString(num, buffer);
2230 } else {
2231 double num = Handle<HeapNumber>::cast(number)->value();
2232 str = DoubleToCString(num, buffer);
2233 }
2234
2235 // We tenure the allocated string since it is referenced from the
2236 // number-string cache which lives in the old space.
2237 Handle<String> js_string = NewStringFromAsciiChecked(str, TENURED);
2238 SetNumberStringCache(number, js_string);
2239 return js_string;
2240}
2241
2242
Steve Blocka7e24c12009-10-30 11:49:00 +00002243Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002244 // Allocate initial fixed array for active break points before allocating the
2245 // debug info object to avoid allocation while setting up the debug info
2246 // object.
2247 Handle<FixedArray> break_points(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002248 NewFixedArray(DebugInfo::kEstimatedNofBreakPointsInFunction));
Steve Blocka7e24c12009-10-30 11:49:00 +00002249
2250 // Create and set up the debug info object. Debug info contains function, a
2251 // copy of the original code, the executing code and initial fixed array for
2252 // active break points.
2253 Handle<DebugInfo> debug_info =
Steve Block44f0eee2011-05-26 01:26:41 +01002254 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
Steve Blocka7e24c12009-10-30 11:49:00 +00002255 debug_info->set_shared(*shared);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002256 if (shared->HasBytecodeArray()) {
2257 // Create a copy for debugging.
2258 Handle<BytecodeArray> original(shared->bytecode_array(), isolate());
2259 Handle<BytecodeArray> copy = CopyBytecodeArray(original);
2260 debug_info->set_abstract_code(AbstractCode::cast(*copy));
2261 } else {
2262 debug_info->set_abstract_code(AbstractCode::cast(shared->code()));
2263 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002264 debug_info->set_break_points(*break_points);
2265
2266 // Link debug info to function.
2267 shared->set_debug_info(*debug_info);
2268
2269 return debug_info;
2270}
Steve Blocka7e24c12009-10-30 11:49:00 +00002271
2272
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002273Handle<JSObject> Factory::NewArgumentsObject(Handle<JSFunction> callee,
Steve Blocka7e24c12009-10-30 11:49:00 +00002274 int length) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002275 bool strict_mode_callee = is_strict(callee->shared()->language_mode()) ||
2276 !callee->shared()->has_simple_parameters();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002277 Handle<Map> map = strict_mode_callee ? isolate()->strict_arguments_map()
2278 : isolate()->sloppy_arguments_map();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002279 AllocationSiteUsageContext context(isolate(), Handle<AllocationSite>(),
2280 false);
2281 DCHECK(!isolate()->has_pending_exception());
2282 Handle<JSObject> result = NewJSObjectFromMap(map);
2283 Handle<Smi> value(Smi::FromInt(length), isolate());
2284 Object::SetProperty(result, length_string(), value, STRICT).Assert();
2285 if (!strict_mode_callee) {
2286 Object::SetProperty(result, callee_string(), callee, STRICT).Assert();
2287 }
2288 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00002289}
2290
2291
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002292Handle<JSWeakMap> Factory::NewJSWeakMap() {
2293 // TODO(adamk): Currently the map is only created three times per
2294 // isolate. If it's created more often, the map should be moved into the
2295 // strong root list.
2296 Handle<Map> map = NewMap(JS_WEAK_MAP_TYPE, JSWeakMap::kSize);
2297 return Handle<JSWeakMap>::cast(NewJSObjectFromMap(map));
Steve Blocka7e24c12009-10-30 11:49:00 +00002298}
2299
2300
Steve Blocka7e24c12009-10-30 11:49:00 +00002301Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002302 int number_of_properties,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002303 bool is_strong,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002304 bool* is_result_from_cache) {
2305 const int kMapCacheSize = 128;
2306
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002307 // We do not cache maps for too many properties or when running builtin code.
2308 if (number_of_properties > kMapCacheSize ||
2309 isolate()->bootstrapper()->IsActive()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002310 *is_result_from_cache = false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002311 Handle<Map> map = Map::Create(isolate(), number_of_properties);
2312 if (is_strong) map->set_is_strong();
2313 return map;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002314 }
2315 *is_result_from_cache = true;
2316 if (number_of_properties == 0) {
2317 // Reuse the initial map of the Object function if the literal has no
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002318 // predeclared properties, or the strong map if strong.
2319 return handle(is_strong
2320 ? context->js_object_strong_map()
2321 : context->object_function()->initial_map(), isolate());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002322 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002323
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002324 int cache_index = number_of_properties - 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002325 Handle<Object> maybe_cache(is_strong ? context->strong_map_cache()
2326 : context->map_cache(), isolate());
2327 if (maybe_cache->IsUndefined()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002328 // Allocate the new map cache for the native context.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002329 maybe_cache = NewFixedArray(kMapCacheSize, TENURED);
2330 if (is_strong) {
2331 context->set_strong_map_cache(*maybe_cache);
2332 } else {
2333 context->set_map_cache(*maybe_cache);
2334 }
2335 } else {
2336 // Check to see whether there is a matching element in the cache.
2337 Handle<FixedArray> cache = Handle<FixedArray>::cast(maybe_cache);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002338 Object* result = cache->get(cache_index);
2339 if (result->IsWeakCell()) {
2340 WeakCell* cell = WeakCell::cast(result);
2341 if (!cell->cleared()) {
2342 return handle(Map::cast(cell->value()), isolate());
2343 }
2344 }
2345 }
2346 // Create a new map and add it to the cache.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002347 Handle<FixedArray> cache = Handle<FixedArray>::cast(maybe_cache);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002348 Handle<Map> map = Map::Create(isolate(), number_of_properties);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002349 if (is_strong) map->set_is_strong();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002350 Handle<WeakCell> cell = NewWeakCell(map);
2351 cache->set(cache_index, *cell);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002352 return map;
Steve Blocka7e24c12009-10-30 11:49:00 +00002353}
2354
2355
2356void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
2357 JSRegExp::Type type,
2358 Handle<String> source,
2359 JSRegExp::Flags flags,
2360 Handle<Object> data) {
2361 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
2362
2363 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
2364 store->set(JSRegExp::kSourceIndex, *source);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002365 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags));
Steve Blocka7e24c12009-10-30 11:49:00 +00002366 store->set(JSRegExp::kAtomPatternIndex, *data);
2367 regexp->set_data(*store);
2368}
2369
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002370
Steve Blocka7e24c12009-10-30 11:49:00 +00002371void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
2372 JSRegExp::Type type,
2373 Handle<String> source,
2374 JSRegExp::Flags flags,
2375 int capture_count) {
2376 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00002377 Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
Steve Blocka7e24c12009-10-30 11:49:00 +00002378 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
2379 store->set(JSRegExp::kSourceIndex, *source);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002380 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002381 store->set(JSRegExp::kIrregexpLatin1CodeIndex, uninitialized);
Ben Murdoch257744e2011-11-30 15:57:28 +00002382 store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002383 store->set(JSRegExp::kIrregexpLatin1CodeSavedIndex, uninitialized);
Ben Murdoch257744e2011-11-30 15:57:28 +00002384 store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
Steve Blocka7e24c12009-10-30 11:49:00 +00002385 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
2386 store->set(JSRegExp::kIrregexpCaptureCountIndex,
2387 Smi::FromInt(capture_count));
2388 regexp->set_data(*store);
2389}
2390
2391
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002392Handle<Object> Factory::GlobalConstantFor(Handle<Name> name) {
2393 if (Name::Equals(name, undefined_string())) return undefined_value();
2394 if (Name::Equals(name, nan_string())) return nan_value();
2395 if (Name::Equals(name, infinity_string())) return infinity_value();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002396 return Handle<Object>::null();
2397}
2398
2399
2400Handle<Object> Factory::ToBoolean(bool value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002401 return value ? true_value() : false_value();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002402}
2403
2404
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002405} // namespace internal
2406} // namespace v8