Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5 | #include "src/factory.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "src/allocation-site-scopes.h" |
| 8 | #include "src/base/bits.h" |
| 9 | #include "src/conversions.h" |
| 10 | #include "src/isolate-inl.h" |
| 11 | #include "src/macro-assembler.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12 | |
| 13 | namespace v8 { |
| 14 | namespace internal { |
| 15 | |
| 16 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17 | template<typename T> |
| 18 | Handle<T> Factory::New(Handle<Map> map, AllocationSpace space) { |
| 19 | CALL_HEAP_FUNCTION( |
| 20 | isolate(), |
| 21 | isolate()->heap()->Allocate(*map, space), |
| 22 | T); |
| 23 | } |
| 24 | |
| 25 | |
| 26 | template<typename T> |
| 27 | Handle<T> Factory::New(Handle<Map> map, |
| 28 | AllocationSpace space, |
| 29 | Handle<AllocationSite> allocation_site) { |
| 30 | CALL_HEAP_FUNCTION( |
| 31 | isolate(), |
| 32 | isolate()->heap()->Allocate(*map, space, *allocation_site), |
| 33 | T); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | Handle<HeapObject> Factory::NewFillerObject(int size, |
| 38 | bool double_align, |
| 39 | AllocationSpace space) { |
| 40 | CALL_HEAP_FUNCTION( |
| 41 | isolate(), |
| 42 | isolate()->heap()->AllocateFillerObject(size, double_align, space), |
| 43 | HeapObject); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | Handle<Box> Factory::NewBox(Handle<Object> value) { |
| 48 | Handle<Box> result = Handle<Box>::cast(NewStruct(BOX_TYPE)); |
| 49 | result->set_value(*value); |
| 50 | return result; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | Handle<Oddball> Factory::NewOddball(Handle<Map> map, |
| 55 | const char* to_string, |
| 56 | Handle<Object> to_number, |
| 57 | byte kind) { |
| 58 | Handle<Oddball> oddball = New<Oddball>(map, OLD_POINTER_SPACE); |
| 59 | Oddball::Initialize(isolate(), oddball, to_string, to_number, kind); |
| 60 | return oddball; |
| 61 | } |
| 62 | |
| 63 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 64 | Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 65 | DCHECK(0 <= size); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 66 | CALL_HEAP_FUNCTION( |
| 67 | isolate(), |
| 68 | isolate()->heap()->AllocateFixedArray(size, pretenure), |
| 69 | FixedArray); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 73 | Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size, |
| 74 | PretenureFlag pretenure) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 75 | DCHECK(0 <= size); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 76 | CALL_HEAP_FUNCTION( |
| 77 | isolate(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 78 | isolate()->heap()->AllocateFixedArrayWithFiller(size, |
| 79 | pretenure, |
| 80 | *the_hole_value()), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 81 | FixedArray); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 85 | Handle<FixedArray> Factory::NewUninitializedFixedArray(int size) { |
| 86 | CALL_HEAP_FUNCTION( |
| 87 | isolate(), |
| 88 | isolate()->heap()->AllocateUninitializedFixedArray(size), |
| 89 | FixedArray); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | Handle<FixedArrayBase> Factory::NewFixedDoubleArray(int size, |
| 94 | PretenureFlag pretenure) { |
| 95 | DCHECK(0 <= size); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 96 | CALL_HEAP_FUNCTION( |
| 97 | isolate(), |
| 98 | isolate()->heap()->AllocateUninitializedFixedDoubleArray(size, pretenure), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 99 | FixedArrayBase); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 103 | Handle<FixedArrayBase> Factory::NewFixedDoubleArrayWithHoles( |
| 104 | int size, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 105 | PretenureFlag pretenure) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 106 | DCHECK(0 <= size); |
| 107 | Handle<FixedArrayBase> array = NewFixedDoubleArray(size, pretenure); |
| 108 | if (size > 0) { |
| 109 | Handle<FixedDoubleArray> double_array = |
| 110 | Handle<FixedDoubleArray>::cast(array); |
| 111 | for (int i = 0; i < size; ++i) { |
| 112 | double_array->set_the_hole(i); |
| 113 | } |
| 114 | } |
| 115 | return array; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 119 | Handle<ConstantPoolArray> Factory::NewConstantPoolArray( |
| 120 | const ConstantPoolArray::NumberOfEntries& small) { |
| 121 | DCHECK(small.total_count() > 0); |
| 122 | CALL_HEAP_FUNCTION( |
| 123 | isolate(), |
| 124 | isolate()->heap()->AllocateConstantPoolArray(small), |
| 125 | ConstantPoolArray); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | Handle<ConstantPoolArray> Factory::NewExtendedConstantPoolArray( |
| 130 | const ConstantPoolArray::NumberOfEntries& small, |
| 131 | const ConstantPoolArray::NumberOfEntries& extended) { |
| 132 | DCHECK(small.total_count() > 0); |
| 133 | DCHECK(extended.total_count() > 0); |
| 134 | CALL_HEAP_FUNCTION( |
| 135 | isolate(), |
| 136 | isolate()->heap()->AllocateExtendedConstantPoolArray(small, extended), |
| 137 | ConstantPoolArray); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | Handle<OrderedHashSet> Factory::NewOrderedHashSet() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 142 | return OrderedHashSet::Allocate(isolate(), OrderedHashSet::kMinCapacity); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | |
| 146 | Handle<OrderedHashMap> Factory::NewOrderedHashMap() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 147 | return OrderedHashMap::Allocate(isolate(), OrderedHashMap::kMinCapacity); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 151 | Handle<AccessorPair> Factory::NewAccessorPair() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 152 | Handle<AccessorPair> accessors = |
| 153 | Handle<AccessorPair>::cast(NewStruct(ACCESSOR_PAIR_TYPE)); |
| 154 | accessors->set_getter(*the_hole_value(), SKIP_WRITE_BARRIER); |
| 155 | accessors->set_setter(*the_hole_value(), SKIP_WRITE_BARRIER); |
| 156 | return accessors; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | |
| 160 | Handle<TypeFeedbackInfo> Factory::NewTypeFeedbackInfo() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 161 | Handle<TypeFeedbackInfo> info = |
| 162 | Handle<TypeFeedbackInfo>::cast(NewStruct(TYPE_FEEDBACK_INFO_TYPE)); |
| 163 | info->initialize_storage(); |
| 164 | return info; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 168 | // Internalized strings are created in the old generation (data space). |
| 169 | Handle<String> Factory::InternalizeUtf8String(Vector<const char> string) { |
| 170 | Utf8StringKey key(string, isolate()->heap()->HashSeed()); |
| 171 | return InternalizeStringWithKey(&key); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 174 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 175 | // Internalized strings are created in the old generation (data space). |
| 176 | Handle<String> Factory::InternalizeString(Handle<String> string) { |
| 177 | if (string->IsInternalizedString()) return string; |
| 178 | return StringTable::LookupString(isolate(), string); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 182 | Handle<String> Factory::InternalizeOneByteString(Vector<const uint8_t> string) { |
| 183 | OneByteStringKey key(string, isolate()->heap()->HashSeed()); |
| 184 | return InternalizeStringWithKey(&key); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 187 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 188 | Handle<String> Factory::InternalizeOneByteString( |
| 189 | Handle<SeqOneByteString> string, int from, int length) { |
| 190 | SeqOneByteSubStringKey key(string, from, length); |
| 191 | return InternalizeStringWithKey(&key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 195 | Handle<String> Factory::InternalizeTwoByteString(Vector<const uc16> string) { |
| 196 | TwoByteStringKey key(string, isolate()->heap()->HashSeed()); |
| 197 | return InternalizeStringWithKey(&key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 201 | template<class StringTableKey> |
| 202 | Handle<String> Factory::InternalizeStringWithKey(StringTableKey* key) { |
| 203 | return StringTable::LookupKey(isolate(), key); |
| 204 | } |
| 205 | |
| 206 | |
| 207 | MaybeHandle<String> Factory::NewStringFromOneByte(Vector<const uint8_t> string, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 208 | PretenureFlag pretenure) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 209 | int length = string.length(); |
| 210 | if (length == 1) return LookupSingleCharacterStringFromCode(string[0]); |
| 211 | Handle<SeqOneByteString> result; |
| 212 | ASSIGN_RETURN_ON_EXCEPTION( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 213 | isolate(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 214 | result, |
| 215 | NewRawOneByteString(string.length(), pretenure), |
| 216 | String); |
| 217 | |
| 218 | DisallowHeapAllocation no_gc; |
| 219 | // Copy the characters into the new object. |
| 220 | CopyChars(SeqOneByteString::cast(*result)->GetChars(), |
| 221 | string.start(), |
| 222 | length); |
| 223 | return result; |
| 224 | } |
| 225 | |
| 226 | MaybeHandle<String> Factory::NewStringFromUtf8(Vector<const char> string, |
| 227 | PretenureFlag pretenure) { |
| 228 | // Check for ASCII first since this is the common case. |
| 229 | const char* start = string.start(); |
| 230 | int length = string.length(); |
| 231 | int non_ascii_start = String::NonAsciiStart(start, length); |
| 232 | if (non_ascii_start >= length) { |
| 233 | // If the string is ASCII, we do not need to convert the characters |
| 234 | // since UTF8 is backwards compatible with ASCII. |
| 235 | return NewStringFromOneByte(Vector<const uint8_t>::cast(string), pretenure); |
| 236 | } |
| 237 | |
| 238 | // Non-ASCII and we need to decode. |
| 239 | Access<UnicodeCache::Utf8Decoder> |
| 240 | decoder(isolate()->unicode_cache()->utf8_decoder()); |
| 241 | decoder->Reset(string.start() + non_ascii_start, |
| 242 | length - non_ascii_start); |
| 243 | int utf16_length = decoder->Utf16Length(); |
| 244 | DCHECK(utf16_length > 0); |
| 245 | // Allocate string. |
| 246 | Handle<SeqTwoByteString> result; |
| 247 | ASSIGN_RETURN_ON_EXCEPTION( |
| 248 | isolate(), result, |
| 249 | NewRawTwoByteString(non_ascii_start + utf16_length, pretenure), |
| 250 | String); |
| 251 | // Copy ASCII portion. |
| 252 | uint16_t* data = result->GetChars(); |
| 253 | const char* ascii_data = string.start(); |
| 254 | for (int i = 0; i < non_ascii_start; i++) { |
| 255 | *data++ = *ascii_data++; |
| 256 | } |
| 257 | // Now write the remainder. |
| 258 | decoder->WriteUtf16(data, utf16_length); |
| 259 | return result; |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 263 | MaybeHandle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string, |
| 264 | PretenureFlag pretenure) { |
| 265 | int length = string.length(); |
| 266 | const uc16* start = string.start(); |
| 267 | if (String::IsOneByte(start, length)) { |
| 268 | if (length == 1) return LookupSingleCharacterStringFromCode(string[0]); |
| 269 | Handle<SeqOneByteString> result; |
| 270 | ASSIGN_RETURN_ON_EXCEPTION( |
| 271 | isolate(), |
| 272 | result, |
| 273 | NewRawOneByteString(length, pretenure), |
| 274 | String); |
| 275 | CopyChars(result->GetChars(), start, length); |
| 276 | return result; |
| 277 | } else { |
| 278 | Handle<SeqTwoByteString> result; |
| 279 | ASSIGN_RETURN_ON_EXCEPTION( |
| 280 | isolate(), |
| 281 | result, |
| 282 | NewRawTwoByteString(length, pretenure), |
| 283 | String); |
| 284 | CopyChars(result->GetChars(), start, length); |
| 285 | return result; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | |
| 290 | Handle<String> Factory::NewInternalizedStringFromUtf8(Vector<const char> str, |
| 291 | int chars, |
| 292 | uint32_t hash_field) { |
| 293 | CALL_HEAP_FUNCTION( |
| 294 | isolate(), |
| 295 | isolate()->heap()->AllocateInternalizedStringFromUtf8( |
| 296 | str, chars, hash_field), |
| 297 | String); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | MUST_USE_RESULT Handle<String> Factory::NewOneByteInternalizedString( |
| 302 | Vector<const uint8_t> str, |
| 303 | uint32_t hash_field) { |
| 304 | CALL_HEAP_FUNCTION( |
| 305 | isolate(), |
| 306 | isolate()->heap()->AllocateOneByteInternalizedString(str, hash_field), |
| 307 | String); |
| 308 | } |
| 309 | |
| 310 | |
| 311 | MUST_USE_RESULT Handle<String> Factory::NewOneByteInternalizedSubString( |
| 312 | Handle<SeqOneByteString> string, int offset, int length, |
| 313 | uint32_t hash_field) { |
| 314 | CALL_HEAP_FUNCTION( |
| 315 | isolate(), isolate()->heap()->AllocateOneByteInternalizedString( |
| 316 | Vector<const uint8_t>(string->GetChars() + offset, length), |
| 317 | hash_field), |
| 318 | String); |
| 319 | } |
| 320 | |
| 321 | |
| 322 | MUST_USE_RESULT Handle<String> Factory::NewTwoByteInternalizedString( |
| 323 | Vector<const uc16> str, |
| 324 | uint32_t hash_field) { |
| 325 | CALL_HEAP_FUNCTION( |
| 326 | isolate(), |
| 327 | isolate()->heap()->AllocateTwoByteInternalizedString(str, hash_field), |
| 328 | String); |
| 329 | } |
| 330 | |
| 331 | |
| 332 | Handle<String> Factory::NewInternalizedStringImpl( |
| 333 | Handle<String> string, int chars, uint32_t hash_field) { |
| 334 | CALL_HEAP_FUNCTION( |
| 335 | isolate(), |
| 336 | isolate()->heap()->AllocateInternalizedStringImpl( |
| 337 | *string, chars, hash_field), |
| 338 | String); |
| 339 | } |
| 340 | |
| 341 | |
| 342 | MaybeHandle<Map> Factory::InternalizedStringMapForString( |
| 343 | Handle<String> string) { |
| 344 | // If the string is in new space it cannot be used as internalized. |
| 345 | if (isolate()->heap()->InNewSpace(*string)) return MaybeHandle<Map>(); |
| 346 | |
| 347 | // Find the corresponding internalized string map for strings. |
| 348 | switch (string->map()->instance_type()) { |
| 349 | case STRING_TYPE: return internalized_string_map(); |
| 350 | case ONE_BYTE_STRING_TYPE: |
| 351 | return one_byte_internalized_string_map(); |
| 352 | case EXTERNAL_STRING_TYPE: return external_internalized_string_map(); |
| 353 | case EXTERNAL_ONE_BYTE_STRING_TYPE: |
| 354 | return external_one_byte_internalized_string_map(); |
| 355 | case EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE: |
| 356 | return external_internalized_string_with_one_byte_data_map(); |
| 357 | case SHORT_EXTERNAL_STRING_TYPE: |
| 358 | return short_external_internalized_string_map(); |
| 359 | case SHORT_EXTERNAL_ONE_BYTE_STRING_TYPE: |
| 360 | return short_external_one_byte_internalized_string_map(); |
| 361 | case SHORT_EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE: |
| 362 | return short_external_internalized_string_with_one_byte_data_map(); |
| 363 | default: return MaybeHandle<Map>(); // No match found. |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | |
| 368 | MaybeHandle<SeqOneByteString> Factory::NewRawOneByteString( |
| 369 | int length, PretenureFlag pretenure) { |
| 370 | if (length > String::kMaxLength || length < 0) { |
| 371 | THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), SeqOneByteString); |
| 372 | } |
| 373 | CALL_HEAP_FUNCTION( |
| 374 | isolate(), |
| 375 | isolate()->heap()->AllocateRawOneByteString(length, pretenure), |
| 376 | SeqOneByteString); |
| 377 | } |
| 378 | |
| 379 | |
| 380 | MaybeHandle<SeqTwoByteString> Factory::NewRawTwoByteString( |
| 381 | int length, PretenureFlag pretenure) { |
| 382 | if (length > String::kMaxLength || length < 0) { |
| 383 | THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), SeqTwoByteString); |
| 384 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 385 | CALL_HEAP_FUNCTION( |
| 386 | isolate(), |
| 387 | isolate()->heap()->AllocateRawTwoByteString(length, pretenure), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 388 | SeqTwoByteString); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 392 | Handle<String> Factory::LookupSingleCharacterStringFromCode(uint32_t code) { |
| 393 | if (code <= String::kMaxOneByteCharCodeU) { |
| 394 | { |
| 395 | DisallowHeapAllocation no_allocation; |
| 396 | Object* value = single_character_string_cache()->get(code); |
| 397 | if (value != *undefined_value()) { |
| 398 | return handle(String::cast(value), isolate()); |
| 399 | } |
| 400 | } |
| 401 | uint8_t buffer[1]; |
| 402 | buffer[0] = static_cast<uint8_t>(code); |
| 403 | Handle<String> result = |
| 404 | InternalizeOneByteString(Vector<const uint8_t>(buffer, 1)); |
| 405 | single_character_string_cache()->set(code, *result); |
| 406 | return result; |
| 407 | } |
| 408 | DCHECK(code <= String::kMaxUtf16CodeUnitU); |
| 409 | |
| 410 | Handle<SeqTwoByteString> result = NewRawTwoByteString(1).ToHandleChecked(); |
| 411 | result->SeqTwoByteStringSet(0, static_cast<uint16_t>(code)); |
| 412 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 416 | // Returns true for a character in a range. Both limits are inclusive. |
| 417 | static inline bool Between(uint32_t character, uint32_t from, uint32_t to) { |
| 418 | // This makes uses of the the unsigned wraparound. |
| 419 | return character - from <= to - from; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | static inline Handle<String> MakeOrFindTwoCharacterString(Isolate* isolate, |
| 424 | uint16_t c1, |
| 425 | uint16_t c2) { |
| 426 | // Numeric strings have a different hash algorithm not known by |
| 427 | // LookupTwoCharsStringIfExists, so we skip this step for such strings. |
| 428 | if (!Between(c1, '0', '9') || !Between(c2, '0', '9')) { |
| 429 | Handle<String> result; |
| 430 | if (StringTable::LookupTwoCharsStringIfExists(isolate, c1, c2). |
| 431 | ToHandle(&result)) { |
| 432 | return result; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | // Now we know the length is 2, we might as well make use of that fact |
| 437 | // when building the new string. |
| 438 | if (static_cast<unsigned>(c1 | c2) <= String::kMaxOneByteCharCodeU) { |
| 439 | // We can do this. |
| 440 | DCHECK(base::bits::IsPowerOfTwo32(String::kMaxOneByteCharCodeU + |
| 441 | 1)); // because of this. |
| 442 | Handle<SeqOneByteString> str = |
| 443 | isolate->factory()->NewRawOneByteString(2).ToHandleChecked(); |
| 444 | uint8_t* dest = str->GetChars(); |
| 445 | dest[0] = static_cast<uint8_t>(c1); |
| 446 | dest[1] = static_cast<uint8_t>(c2); |
| 447 | return str; |
| 448 | } else { |
| 449 | Handle<SeqTwoByteString> str = |
| 450 | isolate->factory()->NewRawTwoByteString(2).ToHandleChecked(); |
| 451 | uc16* dest = str->GetChars(); |
| 452 | dest[0] = c1; |
| 453 | dest[1] = c2; |
| 454 | return str; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | |
| 459 | template<typename SinkChar, typename StringType> |
| 460 | Handle<String> ConcatStringContent(Handle<StringType> result, |
| 461 | Handle<String> first, |
| 462 | Handle<String> second) { |
| 463 | DisallowHeapAllocation pointer_stays_valid; |
| 464 | SinkChar* sink = result->GetChars(); |
| 465 | String::WriteToFlat(*first, sink, 0, first->length()); |
| 466 | String::WriteToFlat(*second, sink + first->length(), 0, second->length()); |
| 467 | return result; |
| 468 | } |
| 469 | |
| 470 | |
| 471 | MaybeHandle<String> Factory::NewConsString(Handle<String> left, |
| 472 | Handle<String> right) { |
| 473 | int left_length = left->length(); |
| 474 | if (left_length == 0) return right; |
| 475 | int right_length = right->length(); |
| 476 | if (right_length == 0) return left; |
| 477 | |
| 478 | int length = left_length + right_length; |
| 479 | |
| 480 | if (length == 2) { |
| 481 | uint16_t c1 = left->Get(0); |
| 482 | uint16_t c2 = right->Get(0); |
| 483 | return MakeOrFindTwoCharacterString(isolate(), c1, c2); |
| 484 | } |
| 485 | |
| 486 | // Make sure that an out of memory exception is thrown if the length |
| 487 | // of the new cons string is too large. |
| 488 | if (length > String::kMaxLength || length < 0) { |
| 489 | THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), String); |
| 490 | } |
| 491 | |
| 492 | bool left_is_one_byte = left->IsOneByteRepresentation(); |
| 493 | bool right_is_one_byte = right->IsOneByteRepresentation(); |
| 494 | bool is_one_byte = left_is_one_byte && right_is_one_byte; |
| 495 | bool is_one_byte_data_in_two_byte_string = false; |
| 496 | if (!is_one_byte) { |
| 497 | // At least one of the strings uses two-byte representation so we |
| 498 | // can't use the fast case code for short one-byte strings below, but |
| 499 | // we can try to save memory if all chars actually fit in one-byte. |
| 500 | is_one_byte_data_in_two_byte_string = |
| 501 | left->HasOnlyOneByteChars() && right->HasOnlyOneByteChars(); |
| 502 | if (is_one_byte_data_in_two_byte_string) { |
| 503 | isolate()->counters()->string_add_runtime_ext_to_one_byte()->Increment(); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | // If the resulting string is small make a flat string. |
| 508 | if (length < ConsString::kMinLength) { |
| 509 | // Note that neither of the two inputs can be a slice because: |
| 510 | STATIC_ASSERT(ConsString::kMinLength <= SlicedString::kMinLength); |
| 511 | DCHECK(left->IsFlat()); |
| 512 | DCHECK(right->IsFlat()); |
| 513 | |
| 514 | STATIC_ASSERT(ConsString::kMinLength <= String::kMaxLength); |
| 515 | if (is_one_byte) { |
| 516 | Handle<SeqOneByteString> result = |
| 517 | NewRawOneByteString(length).ToHandleChecked(); |
| 518 | DisallowHeapAllocation no_gc; |
| 519 | uint8_t* dest = result->GetChars(); |
| 520 | // Copy left part. |
| 521 | const uint8_t* src = |
| 522 | left->IsExternalString() |
| 523 | ? Handle<ExternalOneByteString>::cast(left)->GetChars() |
| 524 | : Handle<SeqOneByteString>::cast(left)->GetChars(); |
| 525 | for (int i = 0; i < left_length; i++) *dest++ = src[i]; |
| 526 | // Copy right part. |
| 527 | src = right->IsExternalString() |
| 528 | ? Handle<ExternalOneByteString>::cast(right)->GetChars() |
| 529 | : Handle<SeqOneByteString>::cast(right)->GetChars(); |
| 530 | for (int i = 0; i < right_length; i++) *dest++ = src[i]; |
| 531 | return result; |
| 532 | } |
| 533 | |
| 534 | return (is_one_byte_data_in_two_byte_string) |
| 535 | ? ConcatStringContent<uint8_t>( |
| 536 | NewRawOneByteString(length).ToHandleChecked(), left, right) |
| 537 | : ConcatStringContent<uc16>( |
| 538 | NewRawTwoByteString(length).ToHandleChecked(), left, right); |
| 539 | } |
| 540 | |
| 541 | Handle<Map> map = (is_one_byte || is_one_byte_data_in_two_byte_string) |
| 542 | ? cons_one_byte_string_map() |
| 543 | : cons_string_map(); |
| 544 | Handle<ConsString> result = New<ConsString>(map, NEW_SPACE); |
| 545 | |
| 546 | DisallowHeapAllocation no_gc; |
| 547 | WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc); |
| 548 | |
| 549 | result->set_hash_field(String::kEmptyHashField); |
| 550 | result->set_length(length); |
| 551 | result->set_first(*left, mode); |
| 552 | result->set_second(*right, mode); |
| 553 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 557 | Handle<String> Factory::NewProperSubString(Handle<String> str, |
| 558 | int begin, |
| 559 | int end) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 560 | #if VERIFY_HEAP |
| 561 | if (FLAG_verify_heap) str->StringVerify(); |
| 562 | #endif |
| 563 | DCHECK(begin > 0 || end < str->length()); |
| 564 | |
| 565 | str = String::Flatten(str); |
| 566 | |
| 567 | int length = end - begin; |
| 568 | if (length <= 0) return empty_string(); |
| 569 | if (length == 1) { |
| 570 | return LookupSingleCharacterStringFromCode(str->Get(begin)); |
| 571 | } |
| 572 | if (length == 2) { |
| 573 | // Optimization for 2-byte strings often used as keys in a decompression |
| 574 | // dictionary. Check whether we already have the string in the string |
| 575 | // table to prevent creation of many unnecessary strings. |
| 576 | uint16_t c1 = str->Get(begin); |
| 577 | uint16_t c2 = str->Get(begin + 1); |
| 578 | return MakeOrFindTwoCharacterString(isolate(), c1, c2); |
| 579 | } |
| 580 | |
| 581 | if (!FLAG_string_slices || length < SlicedString::kMinLength) { |
| 582 | if (str->IsOneByteRepresentation()) { |
| 583 | Handle<SeqOneByteString> result = |
| 584 | NewRawOneByteString(length).ToHandleChecked(); |
| 585 | uint8_t* dest = result->GetChars(); |
| 586 | DisallowHeapAllocation no_gc; |
| 587 | String::WriteToFlat(*str, dest, begin, end); |
| 588 | return result; |
| 589 | } else { |
| 590 | Handle<SeqTwoByteString> result = |
| 591 | NewRawTwoByteString(length).ToHandleChecked(); |
| 592 | uc16* dest = result->GetChars(); |
| 593 | DisallowHeapAllocation no_gc; |
| 594 | String::WriteToFlat(*str, dest, begin, end); |
| 595 | return result; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | int offset = begin; |
| 600 | |
| 601 | if (str->IsSlicedString()) { |
| 602 | Handle<SlicedString> slice = Handle<SlicedString>::cast(str); |
| 603 | str = Handle<String>(slice->parent(), isolate()); |
| 604 | offset += slice->offset(); |
| 605 | } |
| 606 | |
| 607 | DCHECK(str->IsSeqString() || str->IsExternalString()); |
| 608 | Handle<Map> map = str->IsOneByteRepresentation() |
| 609 | ? sliced_one_byte_string_map() |
| 610 | : sliced_string_map(); |
| 611 | Handle<SlicedString> slice = New<SlicedString>(map, NEW_SPACE); |
| 612 | |
| 613 | slice->set_hash_field(String::kEmptyHashField); |
| 614 | slice->set_length(length); |
| 615 | slice->set_parent(*str); |
| 616 | slice->set_offset(offset); |
| 617 | return slice; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 621 | MaybeHandle<String> Factory::NewExternalStringFromOneByte( |
| 622 | const ExternalOneByteString::Resource* resource) { |
| 623 | size_t length = resource->length(); |
| 624 | if (length > static_cast<size_t>(String::kMaxLength)) { |
| 625 | THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), String); |
| 626 | } |
| 627 | |
| 628 | Handle<Map> map = external_one_byte_string_map(); |
| 629 | Handle<ExternalOneByteString> external_string = |
| 630 | New<ExternalOneByteString>(map, NEW_SPACE); |
| 631 | external_string->set_length(static_cast<int>(length)); |
| 632 | external_string->set_hash_field(String::kEmptyHashField); |
| 633 | external_string->set_resource(resource); |
| 634 | |
| 635 | return external_string; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 639 | MaybeHandle<String> Factory::NewExternalStringFromTwoByte( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 640 | const ExternalTwoByteString::Resource* resource) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 641 | size_t length = resource->length(); |
| 642 | if (length > static_cast<size_t>(String::kMaxLength)) { |
| 643 | THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), String); |
| 644 | } |
| 645 | |
| 646 | // For small strings we check whether the resource contains only |
| 647 | // one byte characters. If yes, we use a different string map. |
| 648 | static const size_t kOneByteCheckLengthLimit = 32; |
| 649 | bool is_one_byte = length <= kOneByteCheckLengthLimit && |
| 650 | String::IsOneByte(resource->data(), static_cast<int>(length)); |
| 651 | Handle<Map> map = is_one_byte ? |
| 652 | external_string_with_one_byte_data_map() : external_string_map(); |
| 653 | Handle<ExternalTwoByteString> external_string = |
| 654 | New<ExternalTwoByteString>(map, NEW_SPACE); |
| 655 | external_string->set_length(static_cast<int>(length)); |
| 656 | external_string->set_hash_field(String::kEmptyHashField); |
| 657 | external_string->set_resource(resource); |
| 658 | |
| 659 | return external_string; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 663 | Handle<Symbol> Factory::NewSymbol() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 664 | CALL_HEAP_FUNCTION( |
| 665 | isolate(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 666 | isolate()->heap()->AllocateSymbol(), |
| 667 | Symbol); |
| 668 | } |
| 669 | |
| 670 | |
| 671 | Handle<Symbol> Factory::NewPrivateSymbol() { |
| 672 | Handle<Symbol> symbol = NewSymbol(); |
| 673 | symbol->set_is_private(true); |
| 674 | return symbol; |
| 675 | } |
| 676 | |
| 677 | |
| 678 | Handle<Symbol> Factory::NewPrivateOwnSymbol() { |
| 679 | Handle<Symbol> symbol = NewSymbol(); |
| 680 | symbol->set_is_private(true); |
| 681 | symbol->set_is_own(true); |
| 682 | return symbol; |
| 683 | } |
| 684 | |
| 685 | |
| 686 | Handle<Context> Factory::NewNativeContext() { |
| 687 | Handle<FixedArray> array = NewFixedArray(Context::NATIVE_CONTEXT_SLOTS); |
| 688 | array->set_map_no_write_barrier(*native_context_map()); |
| 689 | Handle<Context> context = Handle<Context>::cast(array); |
| 690 | context->set_js_array_maps(*undefined_value()); |
| 691 | DCHECK(context->IsNativeContext()); |
| 692 | return context; |
| 693 | } |
| 694 | |
| 695 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 696 | Handle<Context> Factory::NewScriptContext(Handle<JSFunction> function, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 697 | Handle<ScopeInfo> scope_info) { |
| 698 | Handle<FixedArray> array = |
| 699 | NewFixedArray(scope_info->ContextLength(), TENURED); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 700 | array->set_map_no_write_barrier(*script_context_map()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 701 | Handle<Context> context = Handle<Context>::cast(array); |
| 702 | context->set_closure(*function); |
| 703 | context->set_previous(function->context()); |
| 704 | context->set_extension(*scope_info); |
| 705 | context->set_global_object(function->context()->global_object()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 706 | DCHECK(context->IsScriptContext()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 707 | return context; |
| 708 | } |
| 709 | |
| 710 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 711 | Handle<ScriptContextTable> Factory::NewScriptContextTable() { |
| 712 | Handle<FixedArray> array = NewFixedArray(1); |
| 713 | array->set_map_no_write_barrier(*script_context_table_map()); |
| 714 | Handle<ScriptContextTable> context_table = |
| 715 | Handle<ScriptContextTable>::cast(array); |
| 716 | context_table->set_used(0); |
| 717 | return context_table; |
| 718 | } |
| 719 | |
| 720 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 721 | Handle<Context> Factory::NewModuleContext(Handle<ScopeInfo> scope_info) { |
| 722 | Handle<FixedArray> array = |
| 723 | NewFixedArray(scope_info->ContextLength(), TENURED); |
| 724 | array->set_map_no_write_barrier(*module_context_map()); |
| 725 | // Instance link will be set later. |
| 726 | Handle<Context> context = Handle<Context>::cast(array); |
| 727 | context->set_extension(Smi::FromInt(0)); |
| 728 | return context; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | |
| 732 | Handle<Context> Factory::NewFunctionContext(int length, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 733 | Handle<JSFunction> function) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 734 | DCHECK(length >= Context::MIN_CONTEXT_SLOTS); |
| 735 | Handle<FixedArray> array = NewFixedArray(length); |
| 736 | array->set_map_no_write_barrier(*function_context_map()); |
| 737 | Handle<Context> context = Handle<Context>::cast(array); |
| 738 | context->set_closure(*function); |
| 739 | context->set_previous(function->context()); |
| 740 | context->set_extension(Smi::FromInt(0)); |
| 741 | context->set_global_object(function->context()->global_object()); |
| 742 | return context; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 746 | Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function, |
| 747 | Handle<Context> previous, |
| 748 | Handle<String> name, |
| 749 | Handle<Object> thrown_object) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 750 | STATIC_ASSERT(Context::MIN_CONTEXT_SLOTS == Context::THROWN_OBJECT_INDEX); |
| 751 | Handle<FixedArray> array = NewFixedArray(Context::MIN_CONTEXT_SLOTS + 1); |
| 752 | array->set_map_no_write_barrier(*catch_context_map()); |
| 753 | Handle<Context> context = Handle<Context>::cast(array); |
| 754 | context->set_closure(*function); |
| 755 | context->set_previous(*previous); |
| 756 | context->set_extension(*name); |
| 757 | context->set_global_object(previous->global_object()); |
| 758 | context->set(Context::THROWN_OBJECT_INDEX, *thrown_object); |
| 759 | return context; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | |
| 763 | Handle<Context> Factory::NewWithContext(Handle<JSFunction> function, |
| 764 | Handle<Context> previous, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 765 | Handle<JSReceiver> extension) { |
| 766 | Handle<FixedArray> array = NewFixedArray(Context::MIN_CONTEXT_SLOTS); |
| 767 | array->set_map_no_write_barrier(*with_context_map()); |
| 768 | Handle<Context> context = Handle<Context>::cast(array); |
| 769 | context->set_closure(*function); |
| 770 | context->set_previous(*previous); |
| 771 | context->set_extension(*extension); |
| 772 | context->set_global_object(previous->global_object()); |
| 773 | return context; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 777 | Handle<Context> Factory::NewBlockContext(Handle<JSFunction> function, |
| 778 | Handle<Context> previous, |
| 779 | Handle<ScopeInfo> scope_info) { |
| 780 | Handle<FixedArray> array = |
| 781 | NewFixedArrayWithHoles(scope_info->ContextLength()); |
| 782 | array->set_map_no_write_barrier(*block_context_map()); |
| 783 | Handle<Context> context = Handle<Context>::cast(array); |
| 784 | context->set_closure(*function); |
| 785 | context->set_previous(*previous); |
| 786 | context->set_extension(*scope_info); |
| 787 | context->set_global_object(previous->global_object()); |
| 788 | return context; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 792 | Handle<Struct> Factory::NewStruct(InstanceType type) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 793 | CALL_HEAP_FUNCTION( |
| 794 | isolate(), |
| 795 | isolate()->heap()->AllocateStruct(type), |
| 796 | Struct); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 800 | Handle<CodeCache> Factory::NewCodeCache() { |
| 801 | Handle<CodeCache> code_cache = |
| 802 | Handle<CodeCache>::cast(NewStruct(CODE_CACHE_TYPE)); |
| 803 | code_cache->set_default_cache(*empty_fixed_array(), SKIP_WRITE_BARRIER); |
| 804 | code_cache->set_normal_type_cache(*undefined_value(), SKIP_WRITE_BARRIER); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 805 | code_cache->set_weak_cell_cache(*undefined_value(), SKIP_WRITE_BARRIER); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 806 | return code_cache; |
| 807 | } |
| 808 | |
| 809 | |
| 810 | Handle<AliasedArgumentsEntry> Factory::NewAliasedArgumentsEntry( |
| 811 | int aliased_context_slot) { |
| 812 | Handle<AliasedArgumentsEntry> entry = Handle<AliasedArgumentsEntry>::cast( |
| 813 | NewStruct(ALIASED_ARGUMENTS_ENTRY_TYPE)); |
| 814 | entry->set_aliased_context_slot(aliased_context_slot); |
| 815 | return entry; |
| 816 | } |
| 817 | |
| 818 | |
| 819 | Handle<DeclaredAccessorDescriptor> Factory::NewDeclaredAccessorDescriptor() { |
| 820 | return Handle<DeclaredAccessorDescriptor>::cast( |
| 821 | NewStruct(DECLARED_ACCESSOR_DESCRIPTOR_TYPE)); |
| 822 | } |
| 823 | |
| 824 | |
| 825 | Handle<DeclaredAccessorInfo> Factory::NewDeclaredAccessorInfo() { |
| 826 | Handle<DeclaredAccessorInfo> info = |
| 827 | Handle<DeclaredAccessorInfo>::cast( |
| 828 | NewStruct(DECLARED_ACCESSOR_INFO_TYPE)); |
| 829 | info->set_flag(0); // Must clear the flag, it was initialized as undefined. |
| 830 | return info; |
| 831 | } |
| 832 | |
| 833 | |
| 834 | Handle<ExecutableAccessorInfo> Factory::NewExecutableAccessorInfo() { |
| 835 | Handle<ExecutableAccessorInfo> info = |
| 836 | Handle<ExecutableAccessorInfo>::cast( |
| 837 | NewStruct(EXECUTABLE_ACCESSOR_INFO_TYPE)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 838 | info->set_flag(0); // Must clear the flag, it was initialized as undefined. |
| 839 | return info; |
| 840 | } |
| 841 | |
| 842 | |
| 843 | Handle<Script> Factory::NewScript(Handle<String> source) { |
| 844 | // Generate id for this script. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 845 | Heap* heap = isolate()->heap(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 846 | int id = heap->last_script_id()->value() + 1; |
| 847 | if (!Smi::IsValid(id) || id < 0) id = 1; |
| 848 | heap->set_last_script_id(Smi::FromInt(id)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 849 | |
| 850 | // Create and initialize script object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 851 | Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE)); |
| 852 | script->set_source(*source); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 853 | script->set_name(heap->undefined_value()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 854 | script->set_id(Smi::FromInt(id)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 855 | script->set_line_offset(Smi::FromInt(0)); |
| 856 | script->set_column_offset(Smi::FromInt(0)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 857 | script->set_context_data(heap->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 858 | script->set_type(Smi::FromInt(Script::TYPE_NORMAL)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 859 | script->set_wrapper(heap->undefined_value()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 860 | script->set_line_ends(heap->undefined_value()); |
| 861 | script->set_eval_from_shared(heap->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 862 | script->set_eval_from_instructions_offset(Smi::FromInt(0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 863 | script->set_flags(Smi::FromInt(0)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 864 | |
| 865 | return script; |
| 866 | } |
| 867 | |
| 868 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 869 | Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 870 | CALL_HEAP_FUNCTION(isolate(), |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 871 | isolate()->heap()->AllocateForeign(addr, pretenure), |
| 872 | Foreign); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 876 | Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) { |
| 877 | return NewForeign((Address) desc, TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | |
| 881 | Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 882 | DCHECK(0 <= length); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 883 | CALL_HEAP_FUNCTION( |
| 884 | isolate(), |
| 885 | isolate()->heap()->AllocateByteArray(length, pretenure), |
| 886 | ByteArray); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 890 | Handle<ExternalArray> Factory::NewExternalArray(int length, |
| 891 | ExternalArrayType array_type, |
| 892 | void* external_pointer, |
| 893 | PretenureFlag pretenure) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 894 | DCHECK(0 <= length && length <= Smi::kMaxValue); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 895 | CALL_HEAP_FUNCTION( |
| 896 | isolate(), |
| 897 | isolate()->heap()->AllocateExternalArray(length, |
| 898 | array_type, |
| 899 | external_pointer, |
| 900 | pretenure), |
| 901 | ExternalArray); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 905 | Handle<FixedTypedArrayBase> Factory::NewFixedTypedArray( |
| 906 | int length, |
| 907 | ExternalArrayType array_type, |
| 908 | PretenureFlag pretenure) { |
| 909 | DCHECK(0 <= length && length <= Smi::kMaxValue); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 910 | CALL_HEAP_FUNCTION( |
| 911 | isolate(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 912 | isolate()->heap()->AllocateFixedTypedArray(length, |
| 913 | array_type, |
| 914 | pretenure), |
| 915 | FixedTypedArrayBase); |
| 916 | } |
| 917 | |
| 918 | |
| 919 | Handle<Cell> Factory::NewCell(Handle<Object> value) { |
| 920 | AllowDeferredHandleDereference convert_to_cell; |
| 921 | CALL_HEAP_FUNCTION( |
| 922 | isolate(), |
| 923 | isolate()->heap()->AllocateCell(*value), |
| 924 | Cell); |
| 925 | } |
| 926 | |
| 927 | |
| 928 | Handle<PropertyCell> Factory::NewPropertyCellWithHole() { |
| 929 | CALL_HEAP_FUNCTION( |
| 930 | isolate(), |
| 931 | isolate()->heap()->AllocatePropertyCell(), |
| 932 | PropertyCell); |
| 933 | } |
| 934 | |
| 935 | |
| 936 | Handle<PropertyCell> Factory::NewPropertyCell(Handle<Object> value) { |
| 937 | AllowDeferredHandleDereference convert_to_cell; |
| 938 | Handle<PropertyCell> cell = NewPropertyCellWithHole(); |
| 939 | PropertyCell::SetValueInferType(cell, value); |
| 940 | return cell; |
| 941 | } |
| 942 | |
| 943 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 944 | Handle<WeakCell> Factory::NewWeakCell(Handle<HeapObject> value) { |
| 945 | AllowDeferredHandleDereference convert_to_cell; |
| 946 | CALL_HEAP_FUNCTION(isolate(), isolate()->heap()->AllocateWeakCell(*value), |
| 947 | WeakCell); |
| 948 | } |
| 949 | |
| 950 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 951 | Handle<AllocationSite> Factory::NewAllocationSite() { |
| 952 | Handle<Map> map = allocation_site_map(); |
| 953 | Handle<AllocationSite> site = New<AllocationSite>(map, OLD_POINTER_SPACE); |
| 954 | site->Initialize(); |
| 955 | |
| 956 | // Link the site |
| 957 | site->set_weak_next(isolate()->heap()->allocation_sites_list()); |
| 958 | isolate()->heap()->set_allocation_sites_list(*site); |
| 959 | return site; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 963 | Handle<Map> Factory::NewMap(InstanceType type, |
| 964 | int instance_size, |
| 965 | ElementsKind elements_kind) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 966 | CALL_HEAP_FUNCTION( |
| 967 | isolate(), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 968 | isolate()->heap()->AllocateMap(type, instance_size, elements_kind), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 969 | Map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 973 | Handle<JSObject> Factory::CopyJSObject(Handle<JSObject> object) { |
| 974 | CALL_HEAP_FUNCTION(isolate(), |
| 975 | isolate()->heap()->CopyJSObject(*object, NULL), |
| 976 | JSObject); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 980 | Handle<JSObject> Factory::CopyJSObjectWithAllocationSite( |
| 981 | Handle<JSObject> object, |
| 982 | Handle<AllocationSite> site) { |
| 983 | CALL_HEAP_FUNCTION(isolate(), |
| 984 | isolate()->heap()->CopyJSObject( |
| 985 | *object, |
| 986 | site.is_null() ? NULL : *site), |
| 987 | JSObject); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 991 | Handle<FixedArray> Factory::CopyFixedArrayWithMap(Handle<FixedArray> array, |
| 992 | Handle<Map> map) { |
| 993 | CALL_HEAP_FUNCTION(isolate(), |
| 994 | isolate()->heap()->CopyFixedArrayWithMap(*array, *map), |
| 995 | FixedArray); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 999 | Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1000 | CALL_HEAP_FUNCTION(isolate(), |
| 1001 | isolate()->heap()->CopyFixedArray(*array), |
| 1002 | FixedArray); |
| 1003 | } |
| 1004 | |
| 1005 | |
| 1006 | Handle<FixedArray> Factory::CopyAndTenureFixedCOWArray( |
| 1007 | Handle<FixedArray> array) { |
| 1008 | DCHECK(isolate()->heap()->InNewSpace(*array)); |
| 1009 | CALL_HEAP_FUNCTION(isolate(), |
| 1010 | isolate()->heap()->CopyAndTenureFixedCOWArray(*array), |
| 1011 | FixedArray); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1015 | Handle<FixedDoubleArray> Factory::CopyFixedDoubleArray( |
| 1016 | Handle<FixedDoubleArray> array) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1017 | CALL_HEAP_FUNCTION(isolate(), |
| 1018 | isolate()->heap()->CopyFixedDoubleArray(*array), |
| 1019 | FixedDoubleArray); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1023 | Handle<ConstantPoolArray> Factory::CopyConstantPoolArray( |
| 1024 | Handle<ConstantPoolArray> array) { |
| 1025 | CALL_HEAP_FUNCTION(isolate(), |
| 1026 | isolate()->heap()->CopyConstantPoolArray(*array), |
| 1027 | ConstantPoolArray); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | |
| 1031 | Handle<Object> Factory::NewNumber(double value, |
| 1032 | PretenureFlag pretenure) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1033 | // We need to distinguish the minus zero value and this cannot be |
| 1034 | // done after conversion to int. Doing this by comparing bit |
| 1035 | // patterns is faster than using fpclassify() et al. |
| 1036 | if (IsMinusZero(value)) return NewHeapNumber(-0.0, IMMUTABLE, pretenure); |
| 1037 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1038 | int int_value = FastD2IChecked(value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1039 | if (value == int_value && Smi::IsValid(int_value)) { |
| 1040 | return handle(Smi::FromInt(int_value), isolate()); |
| 1041 | } |
| 1042 | |
| 1043 | // Materialize the value in the heap. |
| 1044 | return NewHeapNumber(value, IMMUTABLE, pretenure); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1048 | Handle<Object> Factory::NewNumberFromInt(int32_t value, |
| 1049 | PretenureFlag pretenure) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1050 | if (Smi::IsValid(value)) return handle(Smi::FromInt(value), isolate()); |
| 1051 | // Bypass NewNumber to avoid various redundant checks. |
| 1052 | return NewHeapNumber(FastI2D(value), IMMUTABLE, pretenure); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1056 | Handle<Object> Factory::NewNumberFromUint(uint32_t value, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1057 | PretenureFlag pretenure) { |
| 1058 | int32_t int32v = static_cast<int32_t>(value); |
| 1059 | if (int32v >= 0 && Smi::IsValid(int32v)) { |
| 1060 | return handle(Smi::FromInt(int32v), isolate()); |
| 1061 | } |
| 1062 | return NewHeapNumber(FastUI2D(value), IMMUTABLE, pretenure); |
| 1063 | } |
| 1064 | |
| 1065 | |
| 1066 | Handle<HeapNumber> Factory::NewHeapNumber(double value, |
| 1067 | MutableMode mode, |
| 1068 | PretenureFlag pretenure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1069 | CALL_HEAP_FUNCTION( |
| 1070 | isolate(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1071 | isolate()->heap()->AllocateHeapNumber(value, mode, pretenure), |
| 1072 | HeapNumber); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1076 | MaybeHandle<Object> Factory::NewTypeError(const char* message, |
| 1077 | Vector<Handle<Object> > args) { |
| 1078 | return NewError("MakeTypeError", message, args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1082 | MaybeHandle<Object> Factory::NewTypeError(Handle<String> message) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1083 | return NewError("$TypeError", message); |
| 1084 | } |
| 1085 | |
| 1086 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1087 | MaybeHandle<Object> Factory::NewRangeError(const char* message, |
| 1088 | Vector<Handle<Object> > args) { |
| 1089 | return NewError("MakeRangeError", message, args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1093 | MaybeHandle<Object> Factory::NewRangeError(Handle<String> message) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1094 | return NewError("$RangeError", message); |
| 1095 | } |
| 1096 | |
| 1097 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1098 | MaybeHandle<Object> Factory::NewSyntaxError(const char* message, |
| 1099 | Handle<JSArray> args) { |
| 1100 | return NewError("MakeSyntaxError", message, args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1104 | MaybeHandle<Object> Factory::NewSyntaxError(Handle<String> message) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1105 | return NewError("$SyntaxError", message); |
| 1106 | } |
| 1107 | |
| 1108 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1109 | MaybeHandle<Object> Factory::NewReferenceError(const char* message, |
| 1110 | Vector<Handle<Object> > args) { |
| 1111 | return NewError("MakeReferenceError", message, args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1115 | MaybeHandle<Object> Factory::NewReferenceError(const char* message, |
| 1116 | Handle<JSArray> args) { |
| 1117 | return NewError("MakeReferenceError", message, args); |
| 1118 | } |
| 1119 | |
| 1120 | |
| 1121 | MaybeHandle<Object> Factory::NewReferenceError(Handle<String> message) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1122 | return NewError("$ReferenceError", message); |
| 1123 | } |
| 1124 | |
| 1125 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1126 | MaybeHandle<Object> Factory::NewError(const char* maker, const char* message, |
| 1127 | Vector<Handle<Object> > args) { |
| 1128 | // Instantiate a closeable HandleScope for EscapeFrom. |
| 1129 | v8::EscapableHandleScope scope(reinterpret_cast<v8::Isolate*>(isolate())); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1130 | Handle<FixedArray> array = NewFixedArray(args.length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1131 | for (int i = 0; i < args.length(); i++) { |
| 1132 | array->set(i, *args[i]); |
| 1133 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1134 | Handle<JSArray> object = NewJSArrayWithElements(array); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1135 | Handle<Object> result; |
| 1136 | ASSIGN_RETURN_ON_EXCEPTION(isolate(), result, |
| 1137 | NewError(maker, message, object), Object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1138 | return result.EscapeFrom(&scope); |
| 1139 | } |
| 1140 | |
| 1141 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1142 | MaybeHandle<Object> Factory::NewEvalError(const char* message, |
| 1143 | Vector<Handle<Object> > args) { |
| 1144 | return NewError("MakeEvalError", message, args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1148 | MaybeHandle<Object> Factory::NewError(const char* message, |
| 1149 | Vector<Handle<Object> > args) { |
| 1150 | return NewError("MakeError", message, args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1154 | Handle<String> Factory::EmergencyNewError(const char* message, |
| 1155 | Handle<JSArray> args) { |
| 1156 | const int kBufferSize = 1000; |
| 1157 | char buffer[kBufferSize]; |
| 1158 | size_t space = kBufferSize; |
| 1159 | char* p = &buffer[0]; |
| 1160 | |
| 1161 | Vector<char> v(buffer, kBufferSize); |
| 1162 | StrNCpy(v, message, space); |
| 1163 | space -= Min(space, strlen(message)); |
| 1164 | p = &buffer[kBufferSize] - space; |
| 1165 | |
| 1166 | for (int i = 0; i < Smi::cast(args->length())->value(); i++) { |
| 1167 | if (space > 0) { |
| 1168 | *p++ = ' '; |
| 1169 | space--; |
| 1170 | if (space > 0) { |
| 1171 | Handle<String> arg_str = Handle<String>::cast( |
| 1172 | Object::GetElement(isolate(), args, i).ToHandleChecked()); |
| 1173 | SmartArrayPointer<char> arg = arg_str->ToCString(); |
| 1174 | Vector<char> v2(p, static_cast<int>(space)); |
| 1175 | StrNCpy(v2, arg.get(), space); |
| 1176 | space -= Min(space, strlen(arg.get())); |
| 1177 | p = &buffer[kBufferSize] - space; |
| 1178 | } |
| 1179 | } |
| 1180 | } |
| 1181 | if (space > 0) { |
| 1182 | *p = '\0'; |
| 1183 | } else { |
| 1184 | buffer[kBufferSize - 1] = '\0'; |
| 1185 | } |
| 1186 | return NewStringFromUtf8(CStrVector(buffer), TENURED).ToHandleChecked(); |
| 1187 | } |
| 1188 | |
| 1189 | |
| 1190 | MaybeHandle<Object> Factory::NewError(const char* maker, const char* message, |
| 1191 | Handle<JSArray> args) { |
| 1192 | Handle<String> make_str = InternalizeUtf8String(maker); |
| 1193 | Handle<Object> fun_obj = Object::GetProperty( |
| 1194 | isolate()->js_builtins_object(), make_str).ToHandleChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1195 | // If the builtins haven't been properly configured yet this error |
| 1196 | // constructor may not have been defined. Bail out. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1197 | if (!fun_obj->IsJSFunction()) { |
| 1198 | return EmergencyNewError(message, args); |
| 1199 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1200 | Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1201 | Handle<Object> message_obj = InternalizeUtf8String(message); |
| 1202 | Handle<Object> argv[] = { message_obj, args }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1203 | |
| 1204 | // Invoke the JavaScript factory method. If an exception is thrown while |
| 1205 | // running the factory method, use the exception as the result. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1206 | Handle<Object> result; |
| 1207 | MaybeHandle<Object> exception; |
| 1208 | if (!Execution::TryCall(fun, |
| 1209 | isolate()->js_builtins_object(), |
| 1210 | arraysize(argv), |
| 1211 | argv, |
| 1212 | &exception).ToHandle(&result)) { |
| 1213 | return exception; |
| 1214 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1215 | return result; |
| 1216 | } |
| 1217 | |
| 1218 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1219 | MaybeHandle<Object> Factory::NewError(Handle<String> message) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1220 | return NewError("$Error", message); |
| 1221 | } |
| 1222 | |
| 1223 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1224 | MaybeHandle<Object> Factory::NewError(const char* constructor, |
| 1225 | Handle<String> message) { |
| 1226 | Handle<String> constr = InternalizeUtf8String(constructor); |
| 1227 | Handle<JSFunction> fun = Handle<JSFunction>::cast(Object::GetProperty( |
| 1228 | isolate()->js_builtins_object(), constr).ToHandleChecked()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1229 | Handle<Object> argv[] = { message }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1230 | |
| 1231 | // Invoke the JavaScript factory method. If an exception is thrown while |
| 1232 | // running the factory method, use the exception as the result. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1233 | Handle<Object> result; |
| 1234 | MaybeHandle<Object> exception; |
| 1235 | if (!Execution::TryCall(fun, |
| 1236 | isolate()->js_builtins_object(), |
| 1237 | arraysize(argv), |
| 1238 | argv, |
| 1239 | &exception).ToHandle(&result)) { |
| 1240 | return exception; |
| 1241 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1242 | return result; |
| 1243 | } |
| 1244 | |
| 1245 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1246 | void Factory::InitializeFunction(Handle<JSFunction> function, |
| 1247 | Handle<SharedFunctionInfo> info, |
| 1248 | Handle<Context> context) { |
| 1249 | function->initialize_properties(); |
| 1250 | function->initialize_elements(); |
| 1251 | function->set_shared(*info); |
| 1252 | function->set_code(info->code()); |
| 1253 | function->set_context(*context); |
| 1254 | function->set_prototype_or_initial_map(*the_hole_value()); |
| 1255 | function->set_literals_or_bindings(*empty_fixed_array()); |
| 1256 | function->set_next_function_link(*undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1260 | Handle<JSFunction> Factory::NewFunction(Handle<Map> map, |
| 1261 | Handle<SharedFunctionInfo> info, |
| 1262 | Handle<Context> context, |
| 1263 | PretenureFlag pretenure) { |
| 1264 | AllocationSpace space = pretenure == TENURED ? OLD_POINTER_SPACE : NEW_SPACE; |
| 1265 | Handle<JSFunction> result = New<JSFunction>(map, space); |
| 1266 | InitializeFunction(result, info, context); |
| 1267 | return result; |
| 1268 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1269 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1270 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1271 | Handle<JSFunction> Factory::NewFunction(Handle<Map> map, |
| 1272 | Handle<String> name, |
| 1273 | MaybeHandle<Code> code) { |
| 1274 | Handle<Context> context(isolate()->native_context()); |
| 1275 | Handle<SharedFunctionInfo> info = NewSharedFunctionInfo(name, code); |
| 1276 | DCHECK((info->strict_mode() == SLOPPY) && |
| 1277 | (map.is_identical_to(isolate()->sloppy_function_map()) || |
| 1278 | map.is_identical_to( |
| 1279 | isolate()->sloppy_function_without_prototype_map()) || |
| 1280 | map.is_identical_to( |
| 1281 | isolate()->sloppy_function_with_readonly_prototype_map()))); |
| 1282 | return NewFunction(map, info, context); |
| 1283 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1284 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1285 | |
| 1286 | Handle<JSFunction> Factory::NewFunction(Handle<String> name) { |
| 1287 | return NewFunction( |
| 1288 | isolate()->sloppy_function_map(), name, MaybeHandle<Code>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1292 | Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name, |
| 1293 | Handle<Code> code) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1294 | return NewFunction( |
| 1295 | isolate()->sloppy_function_without_prototype_map(), name, code); |
| 1296 | } |
| 1297 | |
| 1298 | |
| 1299 | Handle<JSFunction> Factory::NewFunction(Handle<String> name, |
| 1300 | Handle<Code> code, |
| 1301 | Handle<Object> prototype, |
| 1302 | bool read_only_prototype) { |
| 1303 | Handle<Map> map = read_only_prototype |
| 1304 | ? isolate()->sloppy_function_with_readonly_prototype_map() |
| 1305 | : isolate()->sloppy_function_map(); |
| 1306 | Handle<JSFunction> result = NewFunction(map, name, code); |
| 1307 | result->set_prototype_or_initial_map(*prototype); |
| 1308 | return result; |
| 1309 | } |
| 1310 | |
| 1311 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1312 | Handle<JSFunction> Factory::NewFunction(Handle<String> name, Handle<Code> code, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1313 | Handle<Object> prototype, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1314 | InstanceType type, int instance_size, |
| 1315 | bool read_only_prototype, |
| 1316 | bool install_constructor) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1317 | // Allocate the function |
| 1318 | Handle<JSFunction> function = NewFunction( |
| 1319 | name, code, prototype, read_only_prototype); |
| 1320 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1321 | ElementsKind elements_kind = |
| 1322 | type == JS_ARRAY_TYPE ? FAST_SMI_ELEMENTS : FAST_HOLEY_SMI_ELEMENTS; |
| 1323 | Handle<Map> initial_map = NewMap(type, instance_size, elements_kind); |
| 1324 | if (!function->shared()->is_generator()) { |
| 1325 | if (prototype->IsTheHole()) { |
| 1326 | prototype = NewFunctionPrototype(function); |
| 1327 | } else if (install_constructor) { |
| 1328 | JSObject::AddProperty(Handle<JSObject>::cast(prototype), |
| 1329 | constructor_string(), function, DONT_ENUM); |
| 1330 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | JSFunction::SetInitialMap(function, initial_map, |
| 1334 | Handle<JSReceiver>::cast(prototype)); |
| 1335 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1336 | return function; |
| 1337 | } |
| 1338 | |
| 1339 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1340 | Handle<JSFunction> Factory::NewFunction(Handle<String> name, |
| 1341 | Handle<Code> code, |
| 1342 | InstanceType type, |
| 1343 | int instance_size) { |
| 1344 | return NewFunction(name, code, the_hole_value(), type, instance_size); |
| 1345 | } |
| 1346 | |
| 1347 | |
| 1348 | Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) { |
| 1349 | // Make sure to use globals from the function's context, since the function |
| 1350 | // can be from a different context. |
| 1351 | Handle<Context> native_context(function->context()->native_context()); |
| 1352 | Handle<Map> new_map; |
| 1353 | if (function->shared()->is_generator()) { |
| 1354 | // Generator prototypes can share maps since they don't have "constructor" |
| 1355 | // properties. |
| 1356 | new_map = handle(native_context->generator_object_prototype_map()); |
| 1357 | } else { |
| 1358 | // Each function prototype gets a fresh map to avoid unwanted sharing of |
| 1359 | // maps between prototypes of different constructors. |
| 1360 | Handle<JSFunction> object_function(native_context->object_function()); |
| 1361 | DCHECK(object_function->has_initial_map()); |
| 1362 | new_map = handle(object_function->initial_map()); |
| 1363 | } |
| 1364 | |
| 1365 | DCHECK(!new_map->is_prototype_map()); |
| 1366 | Handle<JSObject> prototype = NewJSObjectFromMap(new_map); |
| 1367 | |
| 1368 | if (!function->shared()->is_generator()) { |
| 1369 | JSObject::AddProperty(prototype, constructor_string(), function, DONT_ENUM); |
| 1370 | } |
| 1371 | |
| 1372 | return prototype; |
| 1373 | } |
| 1374 | |
| 1375 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1376 | static bool ShouldOptimizeNewClosure(Isolate* isolate, |
| 1377 | Handle<SharedFunctionInfo> info) { |
| 1378 | return isolate->use_crankshaft() && !info->is_toplevel() && |
| 1379 | info->is_compiled() && info->allows_lazy_compilation() && |
| 1380 | !info->optimization_disabled() && !isolate->DebuggerHasBreakPoints(); |
| 1381 | } |
| 1382 | |
| 1383 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1384 | Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo( |
| 1385 | Handle<SharedFunctionInfo> info, |
| 1386 | Handle<Context> context, |
| 1387 | PretenureFlag pretenure) { |
| 1388 | int map_index = Context::FunctionMapIndex(info->strict_mode(), info->kind()); |
| 1389 | Handle<Map> map(Map::cast(context->native_context()->get(map_index))); |
| 1390 | Handle<JSFunction> result = NewFunction(map, info, context, pretenure); |
| 1391 | |
| 1392 | if (info->ic_age() != isolate()->heap()->global_ic_age()) { |
| 1393 | info->ResetForNewContext(isolate()->heap()->global_ic_age()); |
| 1394 | } |
| 1395 | |
| 1396 | int index = info->SearchOptimizedCodeMap(context->native_context(), |
| 1397 | BailoutId::None()); |
| 1398 | if (!info->bound() && index < 0) { |
| 1399 | int number_of_literals = info->num_literals(); |
| 1400 | Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure); |
| 1401 | if (number_of_literals > 0) { |
| 1402 | // Store the native context in the literals array prefix. This |
| 1403 | // context will be used when creating object, regexp and array |
| 1404 | // literals in this function. |
| 1405 | literals->set(JSFunction::kLiteralNativeContextIndex, |
| 1406 | context->native_context()); |
| 1407 | } |
| 1408 | result->set_literals(*literals); |
| 1409 | } |
| 1410 | |
| 1411 | if (index > 0) { |
| 1412 | // Caching of optimized code enabled and optimized code found. |
| 1413 | FixedArray* literals = info->GetLiteralsFromOptimizedCodeMap(index); |
| 1414 | if (literals != NULL) result->set_literals(literals); |
| 1415 | Code* code = info->GetCodeFromOptimizedCodeMap(index); |
| 1416 | DCHECK(!code->marked_for_deoptimization()); |
| 1417 | result->ReplaceCode(code); |
| 1418 | return result; |
| 1419 | } |
| 1420 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1421 | if (FLAG_always_opt && ShouldOptimizeNewClosure(isolate(), info)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1422 | result->MarkForOptimization(); |
| 1423 | } |
| 1424 | return result; |
| 1425 | } |
| 1426 | |
| 1427 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1428 | Handle<ScopeInfo> Factory::NewScopeInfo(int length) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1429 | Handle<FixedArray> array = NewFixedArray(length, TENURED); |
| 1430 | array->set_map_no_write_barrier(*scope_info_map()); |
| 1431 | Handle<ScopeInfo> scope_info = Handle<ScopeInfo>::cast(array); |
| 1432 | return scope_info; |
| 1433 | } |
| 1434 | |
| 1435 | |
| 1436 | Handle<JSObject> Factory::NewExternal(void* value) { |
| 1437 | Handle<Foreign> foreign = NewForeign(static_cast<Address>(value)); |
| 1438 | Handle<JSObject> external = NewJSObjectFromMap(external_map()); |
| 1439 | external->SetInternalField(0, *foreign); |
| 1440 | return external; |
| 1441 | } |
| 1442 | |
| 1443 | |
| 1444 | Handle<Code> Factory::NewCodeRaw(int object_size, bool immovable) { |
| 1445 | CALL_HEAP_FUNCTION(isolate(), |
| 1446 | isolate()->heap()->AllocateCode(object_size, immovable), |
| 1447 | Code); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1451 | Handle<Code> Factory::NewCode(const CodeDesc& desc, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1452 | Code::Flags flags, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1453 | Handle<Object> self_ref, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1454 | bool immovable, |
| 1455 | bool crankshafted, |
| 1456 | int prologue_offset, |
| 1457 | bool is_debug) { |
| 1458 | Handle<ByteArray> reloc_info = NewByteArray(desc.reloc_size, TENURED); |
| 1459 | Handle<ConstantPoolArray> constant_pool = |
| 1460 | desc.origin->NewConstantPool(isolate()); |
| 1461 | |
| 1462 | // Compute size. |
| 1463 | int body_size = RoundUp(desc.instr_size, kObjectAlignment); |
| 1464 | int obj_size = Code::SizeFor(body_size); |
| 1465 | |
| 1466 | Handle<Code> code = NewCodeRaw(obj_size, immovable); |
| 1467 | DCHECK(isolate()->code_range() == NULL || |
| 1468 | !isolate()->code_range()->valid() || |
| 1469 | isolate()->code_range()->contains(code->address())); |
| 1470 | |
| 1471 | // The code object has not been fully initialized yet. We rely on the |
| 1472 | // fact that no allocation will happen from this point on. |
| 1473 | DisallowHeapAllocation no_gc; |
| 1474 | code->set_gc_metadata(Smi::FromInt(0)); |
| 1475 | code->set_ic_age(isolate()->heap()->global_ic_age()); |
| 1476 | code->set_instruction_size(desc.instr_size); |
| 1477 | code->set_relocation_info(*reloc_info); |
| 1478 | code->set_flags(flags); |
| 1479 | code->set_raw_kind_specific_flags1(0); |
| 1480 | code->set_raw_kind_specific_flags2(0); |
| 1481 | code->set_is_crankshafted(crankshafted); |
| 1482 | code->set_deoptimization_data(*empty_fixed_array(), SKIP_WRITE_BARRIER); |
| 1483 | code->set_raw_type_feedback_info(Smi::FromInt(0)); |
| 1484 | code->set_next_code_link(*undefined_value()); |
| 1485 | code->set_handler_table(*empty_fixed_array(), SKIP_WRITE_BARRIER); |
| 1486 | code->set_prologue_offset(prologue_offset); |
| 1487 | if (code->kind() == Code::OPTIMIZED_FUNCTION) { |
| 1488 | code->set_marked_for_deoptimization(false); |
| 1489 | } |
| 1490 | |
| 1491 | if (is_debug) { |
| 1492 | DCHECK(code->kind() == Code::FUNCTION); |
| 1493 | code->set_has_debug_break_slots(true); |
| 1494 | } |
| 1495 | |
| 1496 | desc.origin->PopulateConstantPool(*constant_pool); |
| 1497 | code->set_constant_pool(*constant_pool); |
| 1498 | |
| 1499 | // Allow self references to created code object by patching the handle to |
| 1500 | // point to the newly allocated Code object. |
| 1501 | if (!self_ref.is_null()) *(self_ref.location()) = *code; |
| 1502 | |
| 1503 | // Migrate generated code. |
| 1504 | // The generated code can contain Object** values (typically from handles) |
| 1505 | // that are dereferenced during the copy to point directly to the actual heap |
| 1506 | // objects. These pointers can include references to the code object itself, |
| 1507 | // through the self_reference parameter. |
| 1508 | code->CopyFrom(desc); |
| 1509 | |
| 1510 | #ifdef VERIFY_HEAP |
| 1511 | if (FLAG_verify_heap) code->ObjectVerify(); |
| 1512 | #endif |
| 1513 | return code; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | |
| 1517 | Handle<Code> Factory::CopyCode(Handle<Code> code) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1518 | CALL_HEAP_FUNCTION(isolate(), |
| 1519 | isolate()->heap()->CopyCode(*code), |
| 1520 | Code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1524 | Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1525 | CALL_HEAP_FUNCTION(isolate(), |
| 1526 | isolate()->heap()->CopyCode(*code, reloc_info), |
| 1527 | Code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1531 | Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor, |
| 1532 | PretenureFlag pretenure) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1533 | JSFunction::EnsureHasInitialMap(constructor); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1534 | CALL_HEAP_FUNCTION( |
| 1535 | isolate(), |
| 1536 | isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1540 | Handle<JSObject> Factory::NewJSObjectWithMemento( |
| 1541 | Handle<JSFunction> constructor, |
| 1542 | Handle<AllocationSite> site) { |
| 1543 | JSFunction::EnsureHasInitialMap(constructor); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1544 | CALL_HEAP_FUNCTION( |
| 1545 | isolate(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1546 | isolate()->heap()->AllocateJSObject(*constructor, NOT_TENURED, *site), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1547 | JSObject); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1551 | Handle<JSModule> Factory::NewJSModule(Handle<Context> context, |
| 1552 | Handle<ScopeInfo> scope_info) { |
| 1553 | // Allocate a fresh map. Modules do not have a prototype. |
| 1554 | Handle<Map> map = NewMap(JS_MODULE_TYPE, JSModule::kSize); |
| 1555 | // Allocate the object based on the map. |
| 1556 | Handle<JSModule> module = |
| 1557 | Handle<JSModule>::cast(NewJSObjectFromMap(map, TENURED)); |
| 1558 | module->set_context(*context); |
| 1559 | module->set_scope_info(*scope_info); |
| 1560 | return module; |
| 1561 | } |
| 1562 | |
| 1563 | |
| 1564 | Handle<GlobalObject> Factory::NewGlobalObject(Handle<JSFunction> constructor) { |
| 1565 | DCHECK(constructor->has_initial_map()); |
| 1566 | Handle<Map> map(constructor->initial_map()); |
| 1567 | DCHECK(map->is_dictionary_map()); |
| 1568 | |
| 1569 | // Make sure no field properties are described in the initial map. |
| 1570 | // This guarantees us that normalizing the properties does not |
| 1571 | // require us to change property values to PropertyCells. |
| 1572 | DCHECK(map->NextFreePropertyIndex() == 0); |
| 1573 | |
| 1574 | // Make sure we don't have a ton of pre-allocated slots in the |
| 1575 | // global objects. They will be unused once we normalize the object. |
| 1576 | DCHECK(map->unused_property_fields() == 0); |
| 1577 | DCHECK(map->inobject_properties() == 0); |
| 1578 | |
| 1579 | // Initial size of the backing store to avoid resize of the storage during |
| 1580 | // bootstrapping. The size differs between the JS global object ad the |
| 1581 | // builtins object. |
| 1582 | int initial_size = map->instance_type() == JS_GLOBAL_OBJECT_TYPE ? 64 : 512; |
| 1583 | |
| 1584 | // Allocate a dictionary object for backing storage. |
| 1585 | int at_least_space_for = map->NumberOfOwnDescriptors() * 2 + initial_size; |
| 1586 | Handle<NameDictionary> dictionary = |
| 1587 | NameDictionary::New(isolate(), at_least_space_for); |
| 1588 | |
| 1589 | // The global object might be created from an object template with accessors. |
| 1590 | // Fill these accessors into the dictionary. |
| 1591 | Handle<DescriptorArray> descs(map->instance_descriptors()); |
| 1592 | for (int i = 0; i < map->NumberOfOwnDescriptors(); i++) { |
| 1593 | PropertyDetails details = descs->GetDetails(i); |
| 1594 | DCHECK(details.type() == CALLBACKS); // Only accessors are expected. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1595 | PropertyDetails d(details.attributes(), CALLBACKS, i + 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1596 | Handle<Name> name(descs->GetKey(i)); |
| 1597 | Handle<Object> value(descs->GetCallbacksObject(i), isolate()); |
| 1598 | Handle<PropertyCell> cell = NewPropertyCell(value); |
| 1599 | // |dictionary| already contains enough space for all properties. |
| 1600 | USE(NameDictionary::Add(dictionary, name, cell, d)); |
| 1601 | } |
| 1602 | |
| 1603 | // Allocate the global object and initialize it with the backing store. |
| 1604 | Handle<GlobalObject> global = New<GlobalObject>(map, OLD_POINTER_SPACE); |
| 1605 | isolate()->heap()->InitializeJSObjectFromMap(*global, *dictionary, *map); |
| 1606 | |
| 1607 | // Create a new map for the global object. |
| 1608 | Handle<Map> new_map = Map::CopyDropDescriptors(map); |
| 1609 | new_map->set_dictionary_map(true); |
| 1610 | |
| 1611 | // Set up the global object as a normalized object. |
| 1612 | global->set_map(*new_map); |
| 1613 | global->set_properties(*dictionary); |
| 1614 | |
| 1615 | // Make sure result is a global object with properties in dictionary. |
| 1616 | DCHECK(global->IsGlobalObject() && !global->HasFastProperties()); |
| 1617 | return global; |
| 1618 | } |
| 1619 | |
| 1620 | |
| 1621 | Handle<JSObject> Factory::NewJSObjectFromMap( |
| 1622 | Handle<Map> map, |
| 1623 | PretenureFlag pretenure, |
| 1624 | bool alloc_props, |
| 1625 | Handle<AllocationSite> allocation_site) { |
| 1626 | CALL_HEAP_FUNCTION( |
| 1627 | isolate(), |
| 1628 | isolate()->heap()->AllocateJSObjectFromMap( |
| 1629 | *map, |
| 1630 | pretenure, |
| 1631 | alloc_props, |
| 1632 | allocation_site.is_null() ? NULL : *allocation_site), |
| 1633 | JSObject); |
| 1634 | } |
| 1635 | |
| 1636 | |
| 1637 | Handle<JSArray> Factory::NewJSArray(ElementsKind elements_kind, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1638 | PretenureFlag pretenure) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1639 | Context* native_context = isolate()->context()->native_context(); |
| 1640 | JSFunction* array_function = native_context->array_function(); |
| 1641 | Map* map = array_function->initial_map(); |
| 1642 | Map* transition_map = isolate()->get_initial_js_array_map(elements_kind); |
| 1643 | if (transition_map != NULL) map = transition_map; |
| 1644 | return Handle<JSArray>::cast(NewJSObjectFromMap(handle(map), pretenure)); |
| 1645 | } |
| 1646 | |
| 1647 | |
| 1648 | Handle<JSArray> Factory::NewJSArray(ElementsKind elements_kind, |
| 1649 | int length, |
| 1650 | int capacity, |
| 1651 | ArrayStorageAllocationMode mode, |
| 1652 | PretenureFlag pretenure) { |
| 1653 | Handle<JSArray> array = NewJSArray(elements_kind, pretenure); |
| 1654 | NewJSArrayStorage(array, length, capacity, mode); |
| 1655 | return array; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1659 | Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements, |
| 1660 | ElementsKind elements_kind, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1661 | int length, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1662 | PretenureFlag pretenure) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1663 | DCHECK(length <= elements->length()); |
| 1664 | Handle<JSArray> array = NewJSArray(elements_kind, pretenure); |
| 1665 | |
| 1666 | array->set_elements(*elements); |
| 1667 | array->set_length(Smi::FromInt(length)); |
| 1668 | JSObject::ValidateElements(array); |
| 1669 | return array; |
| 1670 | } |
| 1671 | |
| 1672 | |
| 1673 | void Factory::NewJSArrayStorage(Handle<JSArray> array, |
| 1674 | int length, |
| 1675 | int capacity, |
| 1676 | ArrayStorageAllocationMode mode) { |
| 1677 | DCHECK(capacity >= length); |
| 1678 | |
| 1679 | if (capacity == 0) { |
| 1680 | array->set_length(Smi::FromInt(0)); |
| 1681 | array->set_elements(*empty_fixed_array()); |
| 1682 | return; |
| 1683 | } |
| 1684 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1685 | HandleScope inner_scope(isolate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1686 | Handle<FixedArrayBase> elms; |
| 1687 | ElementsKind elements_kind = array->GetElementsKind(); |
| 1688 | if (IsFastDoubleElementsKind(elements_kind)) { |
| 1689 | if (mode == DONT_INITIALIZE_ARRAY_ELEMENTS) { |
| 1690 | elms = NewFixedDoubleArray(capacity); |
| 1691 | } else { |
| 1692 | DCHECK(mode == INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE); |
| 1693 | elms = NewFixedDoubleArrayWithHoles(capacity); |
| 1694 | } |
| 1695 | } else { |
| 1696 | DCHECK(IsFastSmiOrObjectElementsKind(elements_kind)); |
| 1697 | if (mode == DONT_INITIALIZE_ARRAY_ELEMENTS) { |
| 1698 | elms = NewUninitializedFixedArray(capacity); |
| 1699 | } else { |
| 1700 | DCHECK(mode == INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE); |
| 1701 | elms = NewFixedArrayWithHoles(capacity); |
| 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | array->set_elements(*elms); |
| 1706 | array->set_length(Smi::FromInt(length)); |
| 1707 | } |
| 1708 | |
| 1709 | |
| 1710 | Handle<JSGeneratorObject> Factory::NewJSGeneratorObject( |
| 1711 | Handle<JSFunction> function) { |
| 1712 | DCHECK(function->shared()->is_generator()); |
| 1713 | JSFunction::EnsureHasInitialMap(function); |
| 1714 | Handle<Map> map(function->initial_map()); |
| 1715 | DCHECK(map->instance_type() == JS_GENERATOR_OBJECT_TYPE); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1716 | CALL_HEAP_FUNCTION( |
| 1717 | isolate(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1718 | isolate()->heap()->AllocateJSObjectFromMap(*map), |
| 1719 | JSGeneratorObject); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1723 | Handle<JSArrayBuffer> Factory::NewJSArrayBuffer() { |
| 1724 | Handle<JSFunction> array_buffer_fun( |
| 1725 | isolate()->native_context()->array_buffer_fun()); |
| 1726 | CALL_HEAP_FUNCTION( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1727 | isolate(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1728 | isolate()->heap()->AllocateJSObject(*array_buffer_fun), |
| 1729 | JSArrayBuffer); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1733 | Handle<JSDataView> Factory::NewJSDataView() { |
| 1734 | Handle<JSFunction> data_view_fun( |
| 1735 | isolate()->native_context()->data_view_fun()); |
| 1736 | CALL_HEAP_FUNCTION( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1737 | isolate(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1738 | isolate()->heap()->AllocateJSObject(*data_view_fun), |
| 1739 | JSDataView); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1743 | Handle<JSMapIterator> Factory::NewJSMapIterator() { |
| 1744 | Handle<Map> map(isolate()->native_context()->map_iterator_map()); |
| 1745 | CALL_HEAP_FUNCTION(isolate(), |
| 1746 | isolate()->heap()->AllocateJSObjectFromMap(*map), |
| 1747 | JSMapIterator); |
| 1748 | } |
| 1749 | |
| 1750 | |
| 1751 | Handle<JSSetIterator> Factory::NewJSSetIterator() { |
| 1752 | Handle<Map> map(isolate()->native_context()->set_iterator_map()); |
| 1753 | CALL_HEAP_FUNCTION(isolate(), |
| 1754 | isolate()->heap()->AllocateJSObjectFromMap(*map), |
| 1755 | JSSetIterator); |
| 1756 | } |
| 1757 | |
| 1758 | |
| 1759 | namespace { |
| 1760 | |
| 1761 | ElementsKind GetExternalArrayElementsKind(ExternalArrayType type) { |
| 1762 | switch (type) { |
| 1763 | #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \ |
| 1764 | case kExternal##Type##Array: \ |
| 1765 | return EXTERNAL_##TYPE##_ELEMENTS; |
| 1766 | TYPED_ARRAYS(TYPED_ARRAY_CASE) |
| 1767 | } |
| 1768 | UNREACHABLE(); |
| 1769 | return FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND; |
| 1770 | #undef TYPED_ARRAY_CASE |
| 1771 | } |
| 1772 | |
| 1773 | |
| 1774 | size_t GetExternalArrayElementSize(ExternalArrayType type) { |
| 1775 | switch (type) { |
| 1776 | #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \ |
| 1777 | case kExternal##Type##Array: \ |
| 1778 | return size; |
| 1779 | TYPED_ARRAYS(TYPED_ARRAY_CASE) |
| 1780 | } |
| 1781 | UNREACHABLE(); |
| 1782 | return 0; |
| 1783 | #undef TYPED_ARRAY_CASE |
| 1784 | } |
| 1785 | |
| 1786 | |
| 1787 | JSFunction* GetTypedArrayFun(ExternalArrayType type, Isolate* isolate) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1788 | Context* native_context = isolate->context()->native_context(); |
| 1789 | switch (type) { |
| 1790 | #define TYPED_ARRAY_FUN(Type, type, TYPE, ctype, size) \ |
| 1791 | case kExternal##Type##Array: \ |
| 1792 | return native_context->type##_array_fun(); |
| 1793 | |
| 1794 | TYPED_ARRAYS(TYPED_ARRAY_FUN) |
| 1795 | #undef TYPED_ARRAY_FUN |
| 1796 | |
| 1797 | default: |
| 1798 | UNREACHABLE(); |
| 1799 | return NULL; |
| 1800 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1804 | void SetupArrayBufferView(i::Isolate* isolate, |
| 1805 | i::Handle<i::JSArrayBufferView> obj, |
| 1806 | i::Handle<i::JSArrayBuffer> buffer, |
| 1807 | size_t byte_offset, size_t byte_length) { |
| 1808 | DCHECK(byte_offset + byte_length <= |
| 1809 | static_cast<size_t>(buffer->byte_length()->Number())); |
| 1810 | |
| 1811 | obj->set_buffer(*buffer); |
| 1812 | |
| 1813 | obj->set_weak_next(buffer->weak_first_view()); |
| 1814 | buffer->set_weak_first_view(*obj); |
| 1815 | |
| 1816 | i::Handle<i::Object> byte_offset_object = |
| 1817 | isolate->factory()->NewNumberFromSize(byte_offset); |
| 1818 | obj->set_byte_offset(*byte_offset_object); |
| 1819 | |
| 1820 | i::Handle<i::Object> byte_length_object = |
| 1821 | isolate->factory()->NewNumberFromSize(byte_length); |
| 1822 | obj->set_byte_length(*byte_length_object); |
| 1823 | } |
| 1824 | |
| 1825 | |
| 1826 | } // namespace |
| 1827 | |
| 1828 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1829 | Handle<JSTypedArray> Factory::NewJSTypedArray(ExternalArrayType type) { |
| 1830 | Handle<JSFunction> typed_array_fun_handle(GetTypedArrayFun(type, isolate())); |
| 1831 | |
| 1832 | CALL_HEAP_FUNCTION( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1833 | isolate(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1834 | isolate()->heap()->AllocateJSObject(*typed_array_fun_handle), |
| 1835 | JSTypedArray); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1839 | Handle<JSTypedArray> Factory::NewJSTypedArray(ExternalArrayType type, |
| 1840 | Handle<JSArrayBuffer> buffer, |
| 1841 | size_t byte_offset, |
| 1842 | size_t length) { |
| 1843 | Handle<JSTypedArray> obj = NewJSTypedArray(type); |
| 1844 | |
| 1845 | size_t element_size = GetExternalArrayElementSize(type); |
| 1846 | ElementsKind elements_kind = GetExternalArrayElementsKind(type); |
| 1847 | |
| 1848 | CHECK(byte_offset % element_size == 0); |
| 1849 | |
| 1850 | CHECK(length <= (std::numeric_limits<size_t>::max() / element_size)); |
| 1851 | CHECK(length <= static_cast<size_t>(Smi::kMaxValue)); |
| 1852 | size_t byte_length = length * element_size; |
| 1853 | SetupArrayBufferView(isolate(), obj, buffer, byte_offset, byte_length); |
| 1854 | |
| 1855 | Handle<Object> length_object = NewNumberFromSize(length); |
| 1856 | obj->set_length(*length_object); |
| 1857 | |
| 1858 | Handle<ExternalArray> elements = NewExternalArray( |
| 1859 | static_cast<int>(length), type, |
| 1860 | static_cast<uint8_t*>(buffer->backing_store()) + byte_offset); |
| 1861 | Handle<Map> map = JSObject::GetElementsTransitionMap(obj, elements_kind); |
| 1862 | JSObject::SetMapAndElements(obj, map, elements); |
| 1863 | return obj; |
| 1864 | } |
| 1865 | |
| 1866 | |
| 1867 | Handle<JSDataView> Factory::NewJSDataView(Handle<JSArrayBuffer> buffer, |
| 1868 | size_t byte_offset, |
| 1869 | size_t byte_length) { |
| 1870 | Handle<JSDataView> obj = NewJSDataView(); |
| 1871 | SetupArrayBufferView(isolate(), obj, buffer, byte_offset, byte_length); |
| 1872 | return obj; |
| 1873 | } |
| 1874 | |
| 1875 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1876 | Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler, |
| 1877 | Handle<Object> prototype) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1878 | // Allocate map. |
| 1879 | // TODO(rossberg): Once we optimize proxies, think about a scheme to share |
| 1880 | // maps. Will probably depend on the identity of the handler object, too. |
| 1881 | Handle<Map> map = NewMap(JS_PROXY_TYPE, JSProxy::kSize); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1882 | map->SetPrototype(prototype); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1883 | |
| 1884 | // Allocate the proxy object. |
| 1885 | Handle<JSProxy> result = New<JSProxy>(map, NEW_SPACE); |
| 1886 | result->InitializeBody(map->instance_size(), Smi::FromInt(0)); |
| 1887 | result->set_handler(*handler); |
| 1888 | result->set_hash(*undefined_value(), SKIP_WRITE_BARRIER); |
| 1889 | return result; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1893 | Handle<JSProxy> Factory::NewJSFunctionProxy(Handle<Object> handler, |
| 1894 | Handle<Object> call_trap, |
| 1895 | Handle<Object> construct_trap, |
| 1896 | Handle<Object> prototype) { |
| 1897 | // Allocate map. |
| 1898 | // TODO(rossberg): Once we optimize proxies, think about a scheme to share |
| 1899 | // maps. Will probably depend on the identity of the handler object, too. |
| 1900 | Handle<Map> map = NewMap(JS_FUNCTION_PROXY_TYPE, JSFunctionProxy::kSize); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1901 | map->SetPrototype(prototype); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1902 | |
| 1903 | // Allocate the proxy object. |
| 1904 | Handle<JSFunctionProxy> result = New<JSFunctionProxy>(map, NEW_SPACE); |
| 1905 | result->InitializeBody(map->instance_size(), Smi::FromInt(0)); |
| 1906 | result->set_handler(*handler); |
| 1907 | result->set_hash(*undefined_value(), SKIP_WRITE_BARRIER); |
| 1908 | result->set_call_trap(*call_trap); |
| 1909 | result->set_construct_trap(*construct_trap); |
| 1910 | return result; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1914 | void Factory::ReinitializeJSProxy(Handle<JSProxy> proxy, InstanceType type, |
| 1915 | int size) { |
| 1916 | DCHECK(type == JS_OBJECT_TYPE || type == JS_FUNCTION_TYPE); |
| 1917 | |
| 1918 | // Allocate fresh map. |
| 1919 | // TODO(rossberg): Once we optimize proxies, cache these maps. |
| 1920 | Handle<Map> map = NewMap(type, size); |
| 1921 | |
| 1922 | // Check that the receiver has at least the size of the fresh object. |
| 1923 | int size_difference = proxy->map()->instance_size() - map->instance_size(); |
| 1924 | DCHECK(size_difference >= 0); |
| 1925 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1926 | map->SetPrototype(handle(proxy->map()->prototype(), proxy->GetIsolate())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1927 | |
| 1928 | // Allocate the backing storage for the properties. |
| 1929 | int prop_size = map->InitialPropertiesLength(); |
| 1930 | Handle<FixedArray> properties = NewFixedArray(prop_size, TENURED); |
| 1931 | |
| 1932 | Heap* heap = isolate()->heap(); |
| 1933 | MaybeHandle<SharedFunctionInfo> shared; |
| 1934 | if (type == JS_FUNCTION_TYPE) { |
| 1935 | OneByteStringKey key(STATIC_CHAR_VECTOR("<freezing call trap>"), |
| 1936 | heap->HashSeed()); |
| 1937 | Handle<String> name = InternalizeStringWithKey(&key); |
| 1938 | shared = NewSharedFunctionInfo(name, MaybeHandle<Code>()); |
| 1939 | } |
| 1940 | |
| 1941 | // In order to keep heap in consistent state there must be no allocations |
| 1942 | // before object re-initialization is finished and filler object is installed. |
| 1943 | DisallowHeapAllocation no_allocation; |
| 1944 | |
| 1945 | // Put in filler if the new object is smaller than the old. |
| 1946 | if (size_difference > 0) { |
| 1947 | Address address = proxy->address(); |
| 1948 | heap->CreateFillerObjectAt(address + map->instance_size(), size_difference); |
| 1949 | heap->AdjustLiveBytes(address, -size_difference, Heap::FROM_MUTATOR); |
| 1950 | } |
| 1951 | |
| 1952 | // Reset the map for the object. |
| 1953 | proxy->synchronized_set_map(*map); |
| 1954 | Handle<JSObject> jsobj = Handle<JSObject>::cast(proxy); |
| 1955 | |
| 1956 | // Reinitialize the object from the constructor map. |
| 1957 | heap->InitializeJSObjectFromMap(*jsobj, *properties, *map); |
| 1958 | |
| 1959 | // The current native context is used to set up certain bits. |
| 1960 | // TODO(adamk): Using the current context seems wrong, it should be whatever |
| 1961 | // context the JSProxy originated in. But that context isn't stored anywhere. |
| 1962 | Handle<Context> context(isolate()->native_context()); |
| 1963 | |
| 1964 | // Functions require some minimal initialization. |
| 1965 | if (type == JS_FUNCTION_TYPE) { |
| 1966 | map->set_function_with_prototype(true); |
| 1967 | Handle<JSFunction> js_function = Handle<JSFunction>::cast(proxy); |
| 1968 | InitializeFunction(js_function, shared.ToHandleChecked(), context); |
| 1969 | } else { |
| 1970 | // Provide JSObjects with a constructor. |
| 1971 | map->set_constructor(context->object_function()); |
| 1972 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1976 | void Factory::ReinitializeJSGlobalProxy(Handle<JSGlobalProxy> object, |
| 1977 | Handle<JSFunction> constructor) { |
| 1978 | DCHECK(constructor->has_initial_map()); |
| 1979 | Handle<Map> map(constructor->initial_map(), isolate()); |
| 1980 | |
| 1981 | // The proxy's hash should be retained across reinitialization. |
| 1982 | Handle<Object> hash(object->hash(), isolate()); |
| 1983 | |
| 1984 | // Check that the already allocated object has the same size and type as |
| 1985 | // objects allocated using the constructor. |
| 1986 | DCHECK(map->instance_size() == object->map()->instance_size()); |
| 1987 | DCHECK(map->instance_type() == object->map()->instance_type()); |
| 1988 | |
| 1989 | // Allocate the backing storage for the properties. |
| 1990 | int prop_size = map->InitialPropertiesLength(); |
| 1991 | Handle<FixedArray> properties = NewFixedArray(prop_size, TENURED); |
| 1992 | |
| 1993 | // In order to keep heap in consistent state there must be no allocations |
| 1994 | // before object re-initialization is finished. |
| 1995 | DisallowHeapAllocation no_allocation; |
| 1996 | |
| 1997 | // Reset the map for the object. |
| 1998 | object->synchronized_set_map(*map); |
| 1999 | |
| 2000 | Heap* heap = isolate()->heap(); |
| 2001 | // Reinitialize the object from the constructor map. |
| 2002 | heap->InitializeJSObjectFromMap(*object, *properties, *map); |
| 2003 | |
| 2004 | // Restore the saved hash. |
| 2005 | object->set_hash(*hash); |
| 2006 | } |
| 2007 | |
| 2008 | |
| 2009 | void Factory::BecomeJSObject(Handle<JSProxy> proxy) { |
| 2010 | ReinitializeJSProxy(proxy, JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 2011 | } |
| 2012 | |
| 2013 | |
| 2014 | void Factory::BecomeJSFunction(Handle<JSProxy> proxy) { |
| 2015 | ReinitializeJSProxy(proxy, JS_FUNCTION_TYPE, JSFunction::kSize); |
| 2016 | } |
| 2017 | |
| 2018 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2019 | Handle<TypeFeedbackVector> Factory::NewTypeFeedbackVector( |
| 2020 | const FeedbackVectorSpec& spec) { |
| 2021 | return TypeFeedbackVector::Allocate(isolate(), spec); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2025 | Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2026 | Handle<String> name, int number_of_literals, FunctionKind kind, |
| 2027 | Handle<Code> code, Handle<ScopeInfo> scope_info, |
| 2028 | Handle<TypeFeedbackVector> feedback_vector) { |
| 2029 | DCHECK(IsValidFunctionKind(kind)); |
| 2030 | Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name, code); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 2031 | shared->set_scope_info(*scope_info); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2032 | shared->set_feedback_vector(*feedback_vector); |
| 2033 | shared->set_kind(kind); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2034 | int literals_array_size = number_of_literals; |
| 2035 | // If the function contains object, regexp or array literals, |
| 2036 | // allocate extra space for a literals array prefix containing the |
| 2037 | // context. |
| 2038 | if (number_of_literals > 0) { |
| 2039 | literals_array_size += JSFunction::kLiteralsPrefixSize; |
| 2040 | } |
| 2041 | shared->set_num_literals(literals_array_size); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2042 | if (IsGeneratorFunction(kind)) { |
| 2043 | shared->set_instance_class_name(isolate()->heap()->Generator_string()); |
| 2044 | shared->DisableOptimization(kGenerator); |
| 2045 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2046 | return shared; |
| 2047 | } |
| 2048 | |
| 2049 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2050 | Handle<JSMessageObject> Factory::NewJSMessageObject( |
| 2051 | Handle<String> type, |
| 2052 | Handle<JSArray> arguments, |
| 2053 | int start_position, |
| 2054 | int end_position, |
| 2055 | Handle<Object> script, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2056 | Handle<Object> stack_frames) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2057 | Handle<Map> map = message_object_map(); |
| 2058 | Handle<JSMessageObject> message = New<JSMessageObject>(map, NEW_SPACE); |
| 2059 | message->set_properties(*empty_fixed_array(), SKIP_WRITE_BARRIER); |
| 2060 | message->initialize_elements(); |
| 2061 | message->set_elements(*empty_fixed_array(), SKIP_WRITE_BARRIER); |
| 2062 | message->set_type(*type); |
| 2063 | message->set_arguments(*arguments); |
| 2064 | message->set_start_position(start_position); |
| 2065 | message->set_end_position(end_position); |
| 2066 | message->set_script(*script); |
| 2067 | message->set_stack_frames(*stack_frames); |
| 2068 | return message; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2072 | Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2073 | Handle<String> name, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2074 | MaybeHandle<Code> maybe_code) { |
| 2075 | Handle<Map> map = shared_function_info_map(); |
| 2076 | Handle<SharedFunctionInfo> share = New<SharedFunctionInfo>(map, |
| 2077 | OLD_POINTER_SPACE); |
| 2078 | |
| 2079 | // Set pointer fields. |
| 2080 | share->set_name(*name); |
| 2081 | Handle<Code> code; |
| 2082 | if (!maybe_code.ToHandle(&code)) { |
| 2083 | code = handle(isolate()->builtins()->builtin(Builtins::kIllegal)); |
| 2084 | } |
| 2085 | share->set_code(*code); |
| 2086 | share->set_optimized_code_map(Smi::FromInt(0)); |
| 2087 | share->set_scope_info(ScopeInfo::Empty(isolate())); |
| 2088 | Code* construct_stub = |
| 2089 | isolate()->builtins()->builtin(Builtins::kJSConstructStubGeneric); |
| 2090 | share->set_construct_stub(construct_stub); |
| 2091 | share->set_instance_class_name(*Object_string()); |
| 2092 | share->set_function_data(*undefined_value(), SKIP_WRITE_BARRIER); |
| 2093 | share->set_script(*undefined_value(), SKIP_WRITE_BARRIER); |
| 2094 | share->set_debug_info(*undefined_value(), SKIP_WRITE_BARRIER); |
| 2095 | share->set_inferred_name(*empty_string(), SKIP_WRITE_BARRIER); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2096 | FeedbackVectorSpec empty_spec; |
| 2097 | Handle<TypeFeedbackVector> feedback_vector = |
| 2098 | NewTypeFeedbackVector(empty_spec); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2099 | share->set_feedback_vector(*feedback_vector, SKIP_WRITE_BARRIER); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2100 | #if TRACE_MAPS |
| 2101 | share->set_unique_id(isolate()->GetNextUniqueSharedFunctionInfoId()); |
| 2102 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2103 | share->set_profiler_ticks(0); |
| 2104 | share->set_ast_node_count(0); |
| 2105 | share->set_counters(0); |
| 2106 | |
| 2107 | // Set integer fields (smi or int, depending on the architecture). |
| 2108 | share->set_length(0); |
| 2109 | share->set_formal_parameter_count(0); |
| 2110 | share->set_expected_nof_properties(0); |
| 2111 | share->set_num_literals(0); |
| 2112 | share->set_start_position_and_type(0); |
| 2113 | share->set_end_position(0); |
| 2114 | share->set_function_token_position(0); |
| 2115 | // All compiler hints default to false or 0. |
| 2116 | share->set_compiler_hints(0); |
| 2117 | share->set_opt_count_and_bailout_reason(0); |
| 2118 | |
| 2119 | return share; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2123 | static inline int NumberCacheHash(Handle<FixedArray> cache, |
| 2124 | Handle<Object> number) { |
| 2125 | int mask = (cache->length() >> 1) - 1; |
| 2126 | if (number->IsSmi()) { |
| 2127 | return Handle<Smi>::cast(number)->value() & mask; |
| 2128 | } else { |
| 2129 | DoubleRepresentation rep(number->Number()); |
| 2130 | return |
| 2131 | (static_cast<int>(rep.bits) ^ static_cast<int>(rep.bits >> 32)) & mask; |
| 2132 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2136 | Handle<Object> Factory::GetNumberStringCache(Handle<Object> number) { |
| 2137 | DisallowHeapAllocation no_gc; |
| 2138 | int hash = NumberCacheHash(number_string_cache(), number); |
| 2139 | Object* key = number_string_cache()->get(hash * 2); |
| 2140 | if (key == *number || (key->IsHeapNumber() && number->IsHeapNumber() && |
| 2141 | key->Number() == number->Number())) { |
| 2142 | return Handle<String>( |
| 2143 | String::cast(number_string_cache()->get(hash * 2 + 1)), isolate()); |
| 2144 | } |
| 2145 | return undefined_value(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2149 | void Factory::SetNumberStringCache(Handle<Object> number, |
| 2150 | Handle<String> string) { |
| 2151 | int hash = NumberCacheHash(number_string_cache(), number); |
| 2152 | if (number_string_cache()->get(hash * 2) != *undefined_value()) { |
| 2153 | int full_size = isolate()->heap()->FullSizeNumberStringCacheLength(); |
| 2154 | if (number_string_cache()->length() != full_size) { |
| 2155 | // The first time we have a hash collision, we move to the full sized |
| 2156 | // number string cache. The idea is to have a small number string |
| 2157 | // cache in the snapshot to keep boot-time memory usage down. |
| 2158 | // If we expand the number string cache already while creating |
| 2159 | // the snapshot then that didn't work out. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2160 | DCHECK(!isolate()->serializer_enabled()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2161 | Handle<FixedArray> new_cache = NewFixedArray(full_size, TENURED); |
| 2162 | isolate()->heap()->set_number_string_cache(*new_cache); |
| 2163 | return; |
| 2164 | } |
| 2165 | } |
| 2166 | number_string_cache()->set(hash * 2, *number); |
| 2167 | number_string_cache()->set(hash * 2 + 1, *string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2168 | } |
| 2169 | |
| 2170 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2171 | Handle<String> Factory::NumberToString(Handle<Object> number, |
| 2172 | bool check_number_string_cache) { |
| 2173 | isolate()->counters()->number_to_string_runtime()->Increment(); |
| 2174 | if (check_number_string_cache) { |
| 2175 | Handle<Object> cached = GetNumberStringCache(number); |
| 2176 | if (!cached->IsUndefined()) return Handle<String>::cast(cached); |
| 2177 | } |
| 2178 | |
| 2179 | char arr[100]; |
| 2180 | Vector<char> buffer(arr, arraysize(arr)); |
| 2181 | const char* str; |
| 2182 | if (number->IsSmi()) { |
| 2183 | int num = Handle<Smi>::cast(number)->value(); |
| 2184 | str = IntToCString(num, buffer); |
| 2185 | } else { |
| 2186 | double num = Handle<HeapNumber>::cast(number)->value(); |
| 2187 | str = DoubleToCString(num, buffer); |
| 2188 | } |
| 2189 | |
| 2190 | // We tenure the allocated string since it is referenced from the |
| 2191 | // number-string cache which lives in the old space. |
| 2192 | Handle<String> js_string = NewStringFromAsciiChecked(str, TENURED); |
| 2193 | SetNumberStringCache(number, js_string); |
| 2194 | return js_string; |
| 2195 | } |
| 2196 | |
| 2197 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2198 | Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) { |
| 2199 | // Get the original code of the function. |
| 2200 | Handle<Code> code(shared->code()); |
| 2201 | |
| 2202 | // Create a copy of the code before allocating the debug info object to avoid |
| 2203 | // allocation while setting up the debug info object. |
| 2204 | Handle<Code> original_code(*Factory::CopyCode(code)); |
| 2205 | |
| 2206 | // Allocate initial fixed array for active break points before allocating the |
| 2207 | // debug info object to avoid allocation while setting up the debug info |
| 2208 | // object. |
| 2209 | Handle<FixedArray> break_points( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2210 | NewFixedArray(DebugInfo::kEstimatedNofBreakPointsInFunction)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2211 | |
| 2212 | // Create and set up the debug info object. Debug info contains function, a |
| 2213 | // copy of the original code, the executing code and initial fixed array for |
| 2214 | // active break points. |
| 2215 | Handle<DebugInfo> debug_info = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2216 | Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2217 | debug_info->set_shared(*shared); |
| 2218 | debug_info->set_original_code(*original_code); |
| 2219 | debug_info->set_code(*code); |
| 2220 | debug_info->set_break_points(*break_points); |
| 2221 | |
| 2222 | // Link debug info to function. |
| 2223 | shared->set_debug_info(*debug_info); |
| 2224 | |
| 2225 | return debug_info; |
| 2226 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2227 | |
| 2228 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2229 | Handle<JSObject> Factory::NewArgumentsObject(Handle<JSFunction> callee, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2230 | int length) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2231 | bool strict_mode_callee = callee->shared()->strict_mode() == STRICT; |
| 2232 | Handle<Map> map = strict_mode_callee ? isolate()->strict_arguments_map() |
| 2233 | : isolate()->sloppy_arguments_map(); |
| 2234 | |
| 2235 | AllocationSiteUsageContext context(isolate(), Handle<AllocationSite>(), |
| 2236 | false); |
| 2237 | DCHECK(!isolate()->has_pending_exception()); |
| 2238 | Handle<JSObject> result = NewJSObjectFromMap(map); |
| 2239 | Handle<Smi> value(Smi::FromInt(length), isolate()); |
| 2240 | Object::SetProperty(result, length_string(), value, STRICT).Assert(); |
| 2241 | if (!strict_mode_callee) { |
| 2242 | Object::SetProperty(result, callee_string(), callee, STRICT).Assert(); |
| 2243 | } |
| 2244 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | |
| 2248 | Handle<JSFunction> Factory::CreateApiFunction( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2249 | Handle<FunctionTemplateInfo> obj, |
| 2250 | Handle<Object> prototype, |
| 2251 | ApiInstanceType instance_type) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2252 | Handle<Code> code = isolate()->builtins()->HandleApiCall(); |
| 2253 | Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2254 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2255 | Handle<JSFunction> result; |
| 2256 | if (obj->remove_prototype()) { |
| 2257 | result = NewFunctionWithoutPrototype(empty_string(), code); |
| 2258 | } else { |
| 2259 | int internal_field_count = 0; |
| 2260 | if (!obj->instance_template()->IsUndefined()) { |
| 2261 | Handle<ObjectTemplateInfo> instance_template = |
| 2262 | Handle<ObjectTemplateInfo>( |
| 2263 | ObjectTemplateInfo::cast(obj->instance_template())); |
| 2264 | internal_field_count = |
| 2265 | Smi::cast(instance_template->internal_field_count())->value(); |
| 2266 | } |
| 2267 | |
| 2268 | // TODO(svenpanne) Kill ApiInstanceType and refactor things by generalizing |
| 2269 | // JSObject::GetHeaderSize. |
| 2270 | int instance_size = kPointerSize * internal_field_count; |
| 2271 | InstanceType type; |
| 2272 | switch (instance_type) { |
| 2273 | case JavaScriptObjectType: |
| 2274 | type = JS_OBJECT_TYPE; |
| 2275 | instance_size += JSObject::kHeaderSize; |
| 2276 | break; |
| 2277 | case GlobalObjectType: |
| 2278 | type = JS_GLOBAL_OBJECT_TYPE; |
| 2279 | instance_size += JSGlobalObject::kSize; |
| 2280 | break; |
| 2281 | case GlobalProxyType: |
| 2282 | type = JS_GLOBAL_PROXY_TYPE; |
| 2283 | instance_size += JSGlobalProxy::kSize; |
| 2284 | break; |
| 2285 | default: |
| 2286 | UNREACHABLE(); |
| 2287 | type = JS_OBJECT_TYPE; // Keep the compiler happy. |
| 2288 | break; |
| 2289 | } |
| 2290 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2291 | result = NewFunction(empty_string(), code, prototype, type, instance_size, |
| 2292 | obj->read_only_prototype(), true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2295 | result->shared()->set_length(obj->length()); |
| 2296 | Handle<Object> class_name(obj->class_name(), isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2297 | if (class_name->IsString()) { |
| 2298 | result->shared()->set_instance_class_name(*class_name); |
| 2299 | result->shared()->set_name(*class_name); |
| 2300 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2301 | result->shared()->set_function_data(*obj); |
| 2302 | result->shared()->set_construct_stub(*construct_stub); |
| 2303 | result->shared()->DontAdaptArguments(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2304 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2305 | if (obj->remove_prototype()) { |
| 2306 | DCHECK(result->shared()->IsApiFunction()); |
| 2307 | DCHECK(!result->has_initial_map()); |
| 2308 | DCHECK(!result->has_prototype()); |
| 2309 | return result; |
| 2310 | } |
| 2311 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2312 | #ifdef DEBUG |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2313 | LookupIterator it(handle(JSObject::cast(result->prototype())), |
| 2314 | constructor_string(), LookupIterator::OWN_SKIP_INTERCEPTOR); |
| 2315 | MaybeHandle<Object> maybe_prop = Object::GetProperty(&it); |
| 2316 | DCHECK(it.IsFound()); |
| 2317 | DCHECK(maybe_prop.ToHandleChecked().is_identical_to(result)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2318 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2319 | |
| 2320 | // Down from here is only valid for API functions that can be used as a |
| 2321 | // constructor (don't set the "remove prototype" flag). |
| 2322 | |
| 2323 | Handle<Map> map(result->initial_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2324 | |
| 2325 | // Mark as undetectable if needed. |
| 2326 | if (obj->undetectable()) { |
| 2327 | map->set_is_undetectable(); |
| 2328 | } |
| 2329 | |
| 2330 | // Mark as hidden for the __proto__ accessor if needed. |
| 2331 | if (obj->hidden_prototype()) { |
| 2332 | map->set_is_hidden_prototype(); |
| 2333 | } |
| 2334 | |
| 2335 | // Mark as needs_access_check if needed. |
| 2336 | if (obj->needs_access_check()) { |
| 2337 | map->set_is_access_check_needed(true); |
| 2338 | } |
| 2339 | |
| 2340 | // Set interceptor information in the map. |
| 2341 | if (!obj->named_property_handler()->IsUndefined()) { |
| 2342 | map->set_has_named_interceptor(); |
| 2343 | } |
| 2344 | if (!obj->indexed_property_handler()->IsUndefined()) { |
| 2345 | map->set_has_indexed_interceptor(); |
| 2346 | } |
| 2347 | |
| 2348 | // Set instance call-as-function information in the map. |
| 2349 | if (!obj->instance_call_handler()->IsUndefined()) { |
| 2350 | map->set_has_instance_call_handler(); |
| 2351 | } |
| 2352 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2353 | // Recursively copy parent instance templates' accessors, |
| 2354 | // 'data' may be modified. |
| 2355 | int max_number_of_additional_properties = 0; |
| 2356 | int max_number_of_static_properties = 0; |
| 2357 | FunctionTemplateInfo* info = *obj; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2358 | while (true) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2359 | if (!info->instance_template()->IsUndefined()) { |
| 2360 | Object* props = |
| 2361 | ObjectTemplateInfo::cast( |
| 2362 | info->instance_template())->property_accessors(); |
| 2363 | if (!props->IsUndefined()) { |
| 2364 | Handle<Object> props_handle(props, isolate()); |
| 2365 | NeanderArray props_array(props_handle); |
| 2366 | max_number_of_additional_properties += props_array.length(); |
| 2367 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2368 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2369 | if (!info->property_accessors()->IsUndefined()) { |
| 2370 | Object* props = info->property_accessors(); |
| 2371 | if (!props->IsUndefined()) { |
| 2372 | Handle<Object> props_handle(props, isolate()); |
| 2373 | NeanderArray props_array(props_handle); |
| 2374 | max_number_of_static_properties += props_array.length(); |
| 2375 | } |
| 2376 | } |
| 2377 | Object* parent = info->parent_template(); |
| 2378 | if (parent->IsUndefined()) break; |
| 2379 | info = FunctionTemplateInfo::cast(parent); |
| 2380 | } |
| 2381 | |
| 2382 | Map::EnsureDescriptorSlack(map, max_number_of_additional_properties); |
| 2383 | |
| 2384 | // Use a temporary FixedArray to acculumate static accessors |
| 2385 | int valid_descriptors = 0; |
| 2386 | Handle<FixedArray> array; |
| 2387 | if (max_number_of_static_properties > 0) { |
| 2388 | array = NewFixedArray(max_number_of_static_properties); |
| 2389 | } |
| 2390 | |
| 2391 | while (true) { |
| 2392 | // Install instance descriptors |
| 2393 | if (!obj->instance_template()->IsUndefined()) { |
| 2394 | Handle<ObjectTemplateInfo> instance = |
| 2395 | Handle<ObjectTemplateInfo>( |
| 2396 | ObjectTemplateInfo::cast(obj->instance_template()), isolate()); |
| 2397 | Handle<Object> props = Handle<Object>(instance->property_accessors(), |
| 2398 | isolate()); |
| 2399 | if (!props->IsUndefined()) { |
| 2400 | Map::AppendCallbackDescriptors(map, props); |
| 2401 | } |
| 2402 | } |
| 2403 | // Accumulate static accessors |
| 2404 | if (!obj->property_accessors()->IsUndefined()) { |
| 2405 | Handle<Object> props = Handle<Object>(obj->property_accessors(), |
| 2406 | isolate()); |
| 2407 | valid_descriptors = |
| 2408 | AccessorInfo::AppendUnique(props, array, valid_descriptors); |
| 2409 | } |
| 2410 | // Climb parent chain |
| 2411 | Handle<Object> parent = Handle<Object>(obj->parent_template(), isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2412 | if (parent->IsUndefined()) break; |
| 2413 | obj = Handle<FunctionTemplateInfo>::cast(parent); |
| 2414 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2415 | |
| 2416 | // Install accumulated static accessors |
| 2417 | for (int i = 0; i < valid_descriptors; i++) { |
| 2418 | Handle<AccessorInfo> accessor(AccessorInfo::cast(array->get(i))); |
| 2419 | JSObject::SetAccessor(result, accessor).Assert(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2422 | DCHECK(result->shared()->IsApiFunction()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2423 | return result; |
| 2424 | } |
| 2425 | |
| 2426 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2427 | Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2428 | int number_of_properties, |
| 2429 | bool* is_result_from_cache) { |
| 2430 | const int kMapCacheSize = 128; |
| 2431 | |
| 2432 | if (number_of_properties > kMapCacheSize) { |
| 2433 | *is_result_from_cache = false; |
| 2434 | return Map::Create(isolate(), number_of_properties); |
| 2435 | } |
| 2436 | *is_result_from_cache = true; |
| 2437 | if (number_of_properties == 0) { |
| 2438 | // Reuse the initial map of the Object function if the literal has no |
| 2439 | // predeclared properties. |
| 2440 | return handle(context->object_function()->initial_map(), isolate()); |
| 2441 | } |
| 2442 | int cache_index = number_of_properties - 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2443 | if (context->map_cache()->IsUndefined()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2444 | // Allocate the new map cache for the native context. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2445 | Handle<FixedArray> new_cache = NewFixedArray(kMapCacheSize, TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2446 | context->set_map_cache(*new_cache); |
| 2447 | } |
| 2448 | // Check to see whether there is a matching element in the cache. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2449 | Handle<FixedArray> cache(FixedArray::cast(context->map_cache())); |
| 2450 | { |
| 2451 | Object* result = cache->get(cache_index); |
| 2452 | if (result->IsWeakCell()) { |
| 2453 | WeakCell* cell = WeakCell::cast(result); |
| 2454 | if (!cell->cleared()) { |
| 2455 | return handle(Map::cast(cell->value()), isolate()); |
| 2456 | } |
| 2457 | } |
| 2458 | } |
| 2459 | // Create a new map and add it to the cache. |
| 2460 | Handle<Map> map = Map::Create(isolate(), number_of_properties); |
| 2461 | Handle<WeakCell> cell = NewWeakCell(map); |
| 2462 | cache->set(cache_index, *cell); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2463 | return map; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | |
| 2467 | void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp, |
| 2468 | JSRegExp::Type type, |
| 2469 | Handle<String> source, |
| 2470 | JSRegExp::Flags flags, |
| 2471 | Handle<Object> data) { |
| 2472 | Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize); |
| 2473 | |
| 2474 | store->set(JSRegExp::kTagIndex, Smi::FromInt(type)); |
| 2475 | store->set(JSRegExp::kSourceIndex, *source); |
| 2476 | store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value())); |
| 2477 | store->set(JSRegExp::kAtomPatternIndex, *data); |
| 2478 | regexp->set_data(*store); |
| 2479 | } |
| 2480 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2481 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2482 | void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp, |
| 2483 | JSRegExp::Type type, |
| 2484 | Handle<String> source, |
| 2485 | JSRegExp::Flags flags, |
| 2486 | int capture_count) { |
| 2487 | Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2488 | Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2489 | store->set(JSRegExp::kTagIndex, Smi::FromInt(type)); |
| 2490 | store->set(JSRegExp::kSourceIndex, *source); |
| 2491 | store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2492 | store->set(JSRegExp::kIrregexpLatin1CodeIndex, uninitialized); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2493 | store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2494 | store->set(JSRegExp::kIrregexpLatin1CodeSavedIndex, uninitialized); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2495 | store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2496 | store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0)); |
| 2497 | store->set(JSRegExp::kIrregexpCaptureCountIndex, |
| 2498 | Smi::FromInt(capture_count)); |
| 2499 | regexp->set_data(*store); |
| 2500 | } |
| 2501 | |
| 2502 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2503 | MaybeHandle<FunctionTemplateInfo> Factory::ConfigureInstance( |
| 2504 | Handle<FunctionTemplateInfo> desc, Handle<JSObject> instance) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2505 | // Configure the instance by adding the properties specified by the |
| 2506 | // instance template. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2507 | Handle<Object> instance_template(desc->instance_template(), isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2508 | if (!instance_template->IsUndefined()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2509 | RETURN_ON_EXCEPTION( |
| 2510 | isolate(), |
| 2511 | Execution::ConfigureInstance(isolate(), instance, instance_template), |
| 2512 | FunctionTemplateInfo); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2513 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2514 | return desc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2518 | Handle<Object> Factory::GlobalConstantFor(Handle<String> name) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2519 | if (String::Equals(name, undefined_string())) return undefined_value(); |
| 2520 | if (String::Equals(name, nan_string())) return nan_value(); |
| 2521 | if (String::Equals(name, infinity_string())) return infinity_value(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2522 | return Handle<Object>::null(); |
| 2523 | } |
| 2524 | |
| 2525 | |
| 2526 | Handle<Object> Factory::ToBoolean(bool value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2527 | return value ? true_value() : false_value(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2531 | } } // namespace v8::internal |