Line data Source code
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.
4 :
5 : #ifndef V8_FACTORY_H_
6 : #define V8_FACTORY_H_
7 :
8 : #include "src/feedback-vector.h"
9 : #include "src/globals.h"
10 : #include "src/isolate.h"
11 : #include "src/messages.h"
12 : #include "src/objects/descriptor-array.h"
13 : #include "src/objects/dictionary.h"
14 : #include "src/objects/js-array.h"
15 : #include "src/objects/js-regexp.h"
16 : #include "src/objects/scope-info.h"
17 : #include "src/objects/string.h"
18 : #include "src/string-hasher.h"
19 :
20 : namespace v8 {
21 : namespace internal {
22 :
23 : // Forward declarations.
24 : class AliasedArgumentsEntry;
25 : class BigInt;
26 : class BreakPointInfo;
27 : class BreakPoint;
28 : class BoilerplateDescription;
29 : class ConstantElementsPair;
30 : class CoverageInfo;
31 : class DebugInfo;
32 : class JSModuleNamespace;
33 : struct SourceRange;
34 : class PreParsedScopeData;
35 : class TemplateObjectDescription;
36 :
37 : enum FunctionMode {
38 : kWithNameBit = 1 << 0,
39 : kWithHomeObjectBit = 1 << 1,
40 : kWithWritablePrototypeBit = 1 << 2,
41 : kWithReadonlyPrototypeBit = 1 << 3,
42 : kWithPrototypeBits = kWithWritablePrototypeBit | kWithReadonlyPrototypeBit,
43 :
44 : // Without prototype.
45 : FUNCTION_WITHOUT_PROTOTYPE = 0,
46 : METHOD_WITH_NAME = kWithNameBit,
47 : METHOD_WITH_HOME_OBJECT = kWithHomeObjectBit,
48 : METHOD_WITH_NAME_AND_HOME_OBJECT = kWithNameBit | kWithHomeObjectBit,
49 :
50 : // With writable prototype.
51 : FUNCTION_WITH_WRITEABLE_PROTOTYPE = kWithWritablePrototypeBit,
52 : FUNCTION_WITH_NAME_AND_WRITEABLE_PROTOTYPE =
53 : kWithWritablePrototypeBit | kWithNameBit,
54 : FUNCTION_WITH_HOME_OBJECT_AND_WRITEABLE_PROTOTYPE =
55 : kWithWritablePrototypeBit | kWithHomeObjectBit,
56 : FUNCTION_WITH_NAME_AND_HOME_OBJECT_AND_WRITEABLE_PROTOTYPE =
57 : kWithWritablePrototypeBit | kWithNameBit | kWithHomeObjectBit,
58 :
59 : // With readonly prototype.
60 : FUNCTION_WITH_READONLY_PROTOTYPE = kWithReadonlyPrototypeBit,
61 : FUNCTION_WITH_NAME_AND_READONLY_PROTOTYPE =
62 : kWithReadonlyPrototypeBit | kWithNameBit,
63 : };
64 :
65 : // Interface for handle based allocation.
66 : class V8_EXPORT_PRIVATE Factory final {
67 : public:
68 : Handle<Oddball> NewOddball(Handle<Map> map, const char* to_string,
69 : Handle<Object> to_number, const char* type_of,
70 : byte kind);
71 :
72 : // Allocates a fixed array initialized with undefined values.
73 : Handle<FixedArray> NewFixedArray(int size,
74 : PretenureFlag pretenure = NOT_TENURED);
75 : Handle<PropertyArray> NewPropertyArray(int size,
76 : PretenureFlag pretenure = NOT_TENURED);
77 : // Tries allocating a fixed array initialized with undefined values.
78 : // In case of an allocation failure (OOM) an empty handle is returned.
79 : // The caller has to manually signal an
80 : // v8::internal::Heap::FatalProcessOutOfMemory typically by calling
81 : // NewFixedArray as a fallback.
82 : MUST_USE_RESULT
83 : MaybeHandle<FixedArray> TryNewFixedArray(
84 : int size, PretenureFlag pretenure = NOT_TENURED);
85 :
86 : // Allocate a new fixed array with non-existing entries (the hole).
87 : Handle<FixedArray> NewFixedArrayWithHoles(
88 : int size,
89 : PretenureFlag pretenure = NOT_TENURED);
90 :
91 : // Allocates an uninitialized fixed array. It must be filled by the caller.
92 : Handle<FixedArray> NewUninitializedFixedArray(int size);
93 :
94 : // Allocates a feedback vector whose slots are initialized with undefined
95 : // values.
96 : Handle<FeedbackVector> NewFeedbackVector(
97 : Handle<SharedFunctionInfo> shared, PretenureFlag pretenure = NOT_TENURED);
98 :
99 : // Allocates a fixed array for name-value pairs of boilerplate properties and
100 : // calculates the number of properties we need to store in the backing store.
101 : Handle<BoilerplateDescription> NewBoilerplateDescription(int boilerplate,
102 : int all_properties,
103 : int index_keys,
104 : bool has_seen_proto);
105 :
106 : // Allocate a new uninitialized fixed double array.
107 : // The function returns a pre-allocated empty fixed array for capacity = 0,
108 : // so the return type must be the general fixed array class.
109 : Handle<FixedArrayBase> NewFixedDoubleArray(
110 : int size,
111 : PretenureFlag pretenure = NOT_TENURED);
112 :
113 : // Allocate a new fixed double array with hole values.
114 : Handle<FixedArrayBase> NewFixedDoubleArrayWithHoles(
115 : int size,
116 : PretenureFlag pretenure = NOT_TENURED);
117 :
118 : Handle<FrameArray> NewFrameArray(int number_of_frames,
119 : PretenureFlag pretenure = NOT_TENURED);
120 :
121 : Handle<OrderedHashSet> NewOrderedHashSet();
122 : Handle<OrderedHashMap> NewOrderedHashMap();
123 :
124 : Handle<SmallOrderedHashSet> NewSmallOrderedHashSet(
125 : int size = SmallOrderedHashSet::kMinCapacity,
126 : PretenureFlag pretenure = NOT_TENURED);
127 : Handle<SmallOrderedHashMap> NewSmallOrderedHashMap(
128 : int size = SmallOrderedHashMap::kMinCapacity,
129 : PretenureFlag pretenure = NOT_TENURED);
130 :
131 : // Create a new PrototypeInfo struct.
132 : Handle<PrototypeInfo> NewPrototypeInfo();
133 :
134 : // Create a new EnumCache struct.
135 : Handle<EnumCache> NewEnumCache(Handle<FixedArray> keys,
136 : Handle<FixedArray> indices);
137 :
138 : // Create a new Tuple2 struct.
139 : Handle<Tuple2> NewTuple2(Handle<Object> value1, Handle<Object> value2,
140 : PretenureFlag pretenure);
141 :
142 : // Create a new Tuple3 struct.
143 : Handle<Tuple3> NewTuple3(Handle<Object> value1, Handle<Object> value2,
144 : Handle<Object> value3, PretenureFlag pretenure);
145 :
146 : // Create a new ContextExtension struct.
147 : Handle<ContextExtension> NewContextExtension(Handle<ScopeInfo> scope_info,
148 : Handle<Object> extension);
149 :
150 : // Create a new ConstantElementsPair struct.
151 : Handle<ConstantElementsPair> NewConstantElementsPair(
152 : ElementsKind elements_kind, Handle<FixedArrayBase> constant_values);
153 :
154 : // Create a new TemplateObjectDescription struct.
155 : Handle<TemplateObjectDescription> NewTemplateObjectDescription(
156 : int hash, Handle<FixedArray> raw_strings,
157 : Handle<FixedArray> cooked_strings);
158 :
159 : // Create a pre-tenured empty AccessorPair.
160 : Handle<AccessorPair> NewAccessorPair();
161 :
162 : // Create an empty TypeFeedbackInfo.
163 : Handle<TypeFeedbackInfo> NewTypeFeedbackInfo();
164 :
165 : // Finds the internalized copy for string in the string table.
166 : // If not found, a new string is added to the table and returned.
167 : Handle<String> InternalizeUtf8String(Vector<const char> str);
168 7147866 : Handle<String> InternalizeUtf8String(const char* str) {
169 7147866 : return InternalizeUtf8String(CStrVector(str));
170 : }
171 :
172 : Handle<String> InternalizeOneByteString(Vector<const uint8_t> str);
173 : Handle<String> InternalizeOneByteString(
174 : Handle<SeqOneByteString>, int from, int length);
175 :
176 : Handle<String> InternalizeTwoByteString(Vector<const uc16> str);
177 :
178 : template<class StringTableKey>
179 : Handle<String> InternalizeStringWithKey(StringTableKey* key);
180 :
181 : // Internalized strings are created in the old generation (data space).
182 : inline Handle<String> InternalizeString(Handle<String> string);
183 :
184 : inline Handle<Name> InternalizeName(Handle<Name> name);
185 :
186 : // String creation functions. Most of the string creation functions take
187 : // a Heap::PretenureFlag argument to optionally request that they be
188 : // allocated in the old generation. The pretenure flag defaults to
189 : // DONT_TENURE.
190 : //
191 : // Creates a new String object. There are two String encodings: one-byte and
192 : // two-byte. One should choose between the three string factory functions
193 : // based on the encoding of the string buffer that the string is
194 : // initialized from.
195 : // - ...FromOneByte initializes the string from a buffer that is Latin1
196 : // encoded (it does not check that the buffer is Latin1 encoded) and
197 : // the result will be Latin1 encoded.
198 : // - ...FromUtf8 initializes the string from a buffer that is UTF-8
199 : // encoded. If the characters are all ASCII characters, the result
200 : // will be Latin1 encoded, otherwise it will converted to two-byte.
201 : // - ...FromTwoByte initializes the string from a buffer that is two-byte
202 : // encoded. If the characters are all Latin1 characters, the result
203 : // will be converted to Latin1, otherwise it will be left as two-byte.
204 : //
205 : // One-byte strings are pretenured when used as keys in the SourceCodeCache.
206 : MUST_USE_RESULT MaybeHandle<String> NewStringFromOneByte(
207 : Vector<const uint8_t> str, PretenureFlag pretenure = NOT_TENURED);
208 :
209 : template <size_t N>
210 99359 : inline Handle<String> NewStringFromStaticChars(
211 : const char (&str)[N], PretenureFlag pretenure = NOT_TENURED) {
212 : DCHECK(N == StrLength(str) + 1);
213 99359 : return NewStringFromOneByte(STATIC_CHAR_VECTOR(str), pretenure)
214 298077 : .ToHandleChecked();
215 : }
216 :
217 39492175 : inline Handle<String> NewStringFromAsciiChecked(
218 : const char* str,
219 : PretenureFlag pretenure = NOT_TENURED) {
220 : return NewStringFromOneByte(
221 78984350 : OneByteVector(str), pretenure).ToHandleChecked();
222 : }
223 :
224 : // UTF8 strings are pretenured when used for regexp literal patterns and
225 : // flags in the parser.
226 : MUST_USE_RESULT MaybeHandle<String> NewStringFromUtf8(
227 : Vector<const char> str, PretenureFlag pretenure = NOT_TENURED);
228 :
229 : MUST_USE_RESULT MaybeHandle<String> NewStringFromUtf8SubString(
230 : Handle<SeqOneByteString> str, int begin, int end,
231 : PretenureFlag pretenure = NOT_TENURED);
232 :
233 : MUST_USE_RESULT MaybeHandle<String> NewStringFromTwoByte(
234 : Vector<const uc16> str, PretenureFlag pretenure = NOT_TENURED);
235 :
236 : MUST_USE_RESULT MaybeHandle<String> NewStringFromTwoByte(
237 : const ZoneVector<uc16>* str, PretenureFlag pretenure = NOT_TENURED);
238 :
239 : Handle<JSStringIterator> NewJSStringIterator(Handle<String> string);
240 :
241 : // Allocates an internalized string in old space based on the character
242 : // stream.
243 : Handle<String> NewInternalizedStringFromUtf8(Vector<const char> str,
244 : int chars, uint32_t hash_field);
245 :
246 : Handle<String> NewOneByteInternalizedString(Vector<const uint8_t> str,
247 : uint32_t hash_field);
248 :
249 : Handle<String> NewOneByteInternalizedSubString(
250 : Handle<SeqOneByteString> string, int offset, int length,
251 : uint32_t hash_field);
252 :
253 : Handle<String> NewTwoByteInternalizedString(Vector<const uc16> str,
254 : uint32_t hash_field);
255 :
256 : Handle<String> NewInternalizedStringImpl(Handle<String> string, int chars,
257 : uint32_t hash_field);
258 :
259 : // Compute the matching internalized string map for a string if possible.
260 : // Empty handle is returned if string is in new space or not flattened.
261 : MUST_USE_RESULT MaybeHandle<Map> InternalizedStringMapForString(
262 : Handle<String> string);
263 :
264 : // Creates an internalized copy of an external string. |string| must be
265 : // of type StringClass.
266 : template <class StringClass>
267 : Handle<StringClass> InternalizeExternalString(Handle<String> string);
268 :
269 : // Allocates and partially initializes an one-byte or two-byte String. The
270 : // characters of the string are uninitialized. Currently used in regexp code
271 : // only, where they are pretenured.
272 : MUST_USE_RESULT MaybeHandle<SeqOneByteString> NewRawOneByteString(
273 : int length,
274 : PretenureFlag pretenure = NOT_TENURED);
275 : MUST_USE_RESULT MaybeHandle<SeqTwoByteString> NewRawTwoByteString(
276 : int length,
277 : PretenureFlag pretenure = NOT_TENURED);
278 :
279 : // Creates a single character string where the character has given code.
280 : // A cache is used for Latin1 codes.
281 : Handle<String> LookupSingleCharacterStringFromCode(uint32_t code);
282 :
283 : // Create a new cons string object which consists of a pair of strings.
284 : MUST_USE_RESULT MaybeHandle<String> NewConsString(Handle<String> left,
285 : Handle<String> right);
286 :
287 : MUST_USE_RESULT Handle<String> NewConsString(Handle<String> left,
288 : Handle<String> right, int length,
289 : bool one_byte);
290 :
291 : // Create or lookup a single characters tring made up of a utf16 surrogate
292 : // pair.
293 : Handle<String> NewSurrogatePairString(uint16_t lead, uint16_t trail);
294 :
295 : // Create a new string object which holds a proper substring of a string.
296 : Handle<String> NewProperSubString(Handle<String> str,
297 : int begin,
298 : int end);
299 :
300 : // Create a new string object which holds a substring of a string.
301 : inline Handle<String> NewSubString(Handle<String> str, int begin, int end);
302 :
303 : // Creates a new external String object. There are two String encodings
304 : // in the system: one-byte and two-byte. Unlike other String types, it does
305 : // not make sense to have a UTF-8 factory function for external strings,
306 : // because we cannot change the underlying buffer. Note that these strings
307 : // are backed by a string resource that resides outside the V8 heap.
308 : MUST_USE_RESULT MaybeHandle<String> NewExternalStringFromOneByte(
309 : const ExternalOneByteString::Resource* resource);
310 : MUST_USE_RESULT MaybeHandle<String> NewExternalStringFromTwoByte(
311 : const ExternalTwoByteString::Resource* resource);
312 : // Create a new external string object for one-byte encoded native script.
313 : // It does not cache the resource data pointer.
314 : Handle<ExternalOneByteString> NewNativeSourceString(
315 : const ExternalOneByteString::Resource* resource);
316 :
317 : // Create a symbol.
318 : Handle<Symbol> NewSymbol();
319 : Handle<Symbol> NewPrivateSymbol();
320 :
321 : // Create a global (but otherwise uninitialized) context.
322 : Handle<Context> NewNativeContext();
323 :
324 : // Create a script context.
325 : Handle<Context> NewScriptContext(Handle<JSFunction> function,
326 : Handle<ScopeInfo> scope_info);
327 :
328 : // Create an empty script context table.
329 : Handle<ScriptContextTable> NewScriptContextTable();
330 :
331 : // Create a module context.
332 : Handle<Context> NewModuleContext(Handle<Module> module,
333 : Handle<JSFunction> function,
334 : Handle<ScopeInfo> scope_info);
335 :
336 : // Create a function or eval context.
337 : Handle<Context> NewFunctionContext(int length, Handle<JSFunction> function,
338 : ScopeType scope_type);
339 :
340 : // Create a catch context.
341 : Handle<Context> NewCatchContext(Handle<JSFunction> function,
342 : Handle<Context> previous,
343 : Handle<ScopeInfo> scope_info,
344 : Handle<String> name,
345 : Handle<Object> thrown_object);
346 :
347 : // Create a 'with' context.
348 : Handle<Context> NewWithContext(Handle<JSFunction> function,
349 : Handle<Context> previous,
350 : Handle<ScopeInfo> scope_info,
351 : Handle<JSReceiver> extension);
352 :
353 : Handle<Context> NewDebugEvaluateContext(Handle<Context> previous,
354 : Handle<ScopeInfo> scope_info,
355 : Handle<JSReceiver> extension,
356 : Handle<Context> wrapped,
357 : Handle<StringSet> whitelist);
358 :
359 : // Create a block context.
360 : Handle<Context> NewBlockContext(Handle<JSFunction> function,
361 : Handle<Context> previous,
362 : Handle<ScopeInfo> scope_info);
363 :
364 : Handle<Struct> NewStruct(InstanceType type,
365 : PretenureFlag pretenure = NOT_TENURED);
366 :
367 : Handle<AliasedArgumentsEntry> NewAliasedArgumentsEntry(
368 : int aliased_context_slot);
369 :
370 : Handle<AccessorInfo> NewAccessorInfo();
371 :
372 : Handle<Script> NewScript(Handle<String> source);
373 :
374 : Handle<BreakPointInfo> NewBreakPointInfo(int source_position);
375 : Handle<BreakPoint> NewBreakPoint(int id, Handle<String> condition);
376 : Handle<StackFrameInfo> NewStackFrameInfo();
377 : Handle<SourcePositionTableWithFrameCache>
378 : NewSourcePositionTableWithFrameCache(
379 : Handle<ByteArray> source_position_table,
380 : Handle<UnseededNumberDictionary> stack_frame_cache);
381 :
382 : // Foreign objects are pretenured when allocated by the bootstrapper.
383 : Handle<Foreign> NewForeign(Address addr,
384 : PretenureFlag pretenure = NOT_TENURED);
385 :
386 : // Allocate a new foreign object. The foreign is pretenured (allocated
387 : // directly in the old generation).
388 : Handle<Foreign> NewForeign(const AccessorDescriptor* foreign);
389 :
390 : Handle<ByteArray> NewByteArray(int length,
391 : PretenureFlag pretenure = NOT_TENURED);
392 :
393 : Handle<BytecodeArray> NewBytecodeArray(int length, const byte* raw_bytecodes,
394 : int frame_size, int parameter_count,
395 : Handle<FixedArray> constant_pool);
396 :
397 : Handle<FixedTypedArrayBase> NewFixedTypedArrayWithExternalPointer(
398 : int length, ExternalArrayType array_type, void* external_pointer,
399 : PretenureFlag pretenure = NOT_TENURED);
400 :
401 : Handle<FixedTypedArrayBase> NewFixedTypedArray(
402 : int length, ExternalArrayType array_type, bool initialize,
403 : PretenureFlag pretenure = NOT_TENURED);
404 :
405 : Handle<Cell> NewCell(Handle<Object> value);
406 :
407 : Handle<PropertyCell> NewPropertyCell(Handle<Name> name);
408 :
409 : Handle<WeakCell> NewWeakCell(Handle<HeapObject> value);
410 :
411 : Handle<Cell> NewNoClosuresCell(Handle<Object> value);
412 : Handle<Cell> NewOneClosureCell(Handle<Object> value);
413 : Handle<Cell> NewManyClosuresCell(Handle<Object> value);
414 :
415 : Handle<TransitionArray> NewTransitionArray(int capacity);
416 :
417 : // Allocate a tenured AllocationSite. It's payload is null.
418 : Handle<AllocationSite> NewAllocationSite();
419 :
420 : Handle<Map> NewMap(InstanceType type, int instance_size,
421 : ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
422 : int inobject_properties = 0);
423 :
424 : Handle<HeapObject> NewFillerObject(int size,
425 : bool double_align,
426 : AllocationSpace space);
427 :
428 : Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
429 :
430 : Handle<JSObject> CopyJSObject(Handle<JSObject> object);
431 :
432 : Handle<JSObject> CopyJSObjectWithAllocationSite(Handle<JSObject> object,
433 : Handle<AllocationSite> site);
434 :
435 : Handle<FixedArray> CopyFixedArrayWithMap(Handle<FixedArray> array,
436 : Handle<Map> map);
437 :
438 : Handle<FixedArray> CopyFixedArrayAndGrow(
439 : Handle<FixedArray> array, int grow_by,
440 : PretenureFlag pretenure = NOT_TENURED);
441 :
442 : Handle<PropertyArray> CopyPropertyArrayAndGrow(
443 : Handle<PropertyArray> array, int grow_by,
444 : PretenureFlag pretenure = NOT_TENURED);
445 :
446 : Handle<FixedArray> CopyFixedArrayUpTo(Handle<FixedArray> array, int new_len,
447 : PretenureFlag pretenure = NOT_TENURED);
448 :
449 : Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
450 :
451 : // This method expects a COW array in new space, and creates a copy
452 : // of it in old space.
453 : Handle<FixedArray> CopyAndTenureFixedCOWArray(Handle<FixedArray> array);
454 :
455 : Handle<FixedDoubleArray> CopyFixedDoubleArray(
456 : Handle<FixedDoubleArray> array);
457 :
458 : Handle<FeedbackVector> CopyFeedbackVector(Handle<FeedbackVector> array);
459 :
460 : // Numbers (e.g. literals) are pretenured by the parser.
461 : // The return value may be a smi or a heap number.
462 : Handle<Object> NewNumber(double value,
463 : PretenureFlag pretenure = NOT_TENURED);
464 :
465 : Handle<Object> NewNumberFromInt(int32_t value,
466 : PretenureFlag pretenure = NOT_TENURED);
467 : Handle<Object> NewNumberFromUint(uint32_t value,
468 : PretenureFlag pretenure = NOT_TENURED);
469 : inline Handle<Object> NewNumberFromSize(
470 : size_t value, PretenureFlag pretenure = NOT_TENURED);
471 : inline Handle<Object> NewNumberFromInt64(
472 : int64_t value, PretenureFlag pretenure = NOT_TENURED);
473 : inline Handle<HeapNumber> NewHeapNumber(
474 : double value, MutableMode mode = IMMUTABLE,
475 : PretenureFlag pretenure = NOT_TENURED);
476 : inline Handle<HeapNumber> NewHeapNumberFromBits(
477 : uint64_t bits, MutableMode mode = IMMUTABLE,
478 : PretenureFlag pretenure = NOT_TENURED);
479 : // Creates mutable heap number object with value field set to hole NaN.
480 : inline Handle<HeapNumber> NewMutableHeapNumber(
481 : PretenureFlag pretenure = NOT_TENURED);
482 :
483 : // Creates heap number object with not yet set value field.
484 : Handle<HeapNumber> NewHeapNumber(MutableMode mode,
485 : PretenureFlag pretenure = NOT_TENURED);
486 :
487 : // Allocates a new BigInt with {length} digits and zero-initializes them.
488 : Handle<BigInt> NewBigInt(int length, PretenureFlag pretenure = NOT_TENURED);
489 : // Initializes length and sign fields, but leaves digits uninitialized.
490 : Handle<BigInt> NewBigIntRaw(int length,
491 : PretenureFlag pretenure = NOT_TENURED);
492 : Handle<BigInt> NewBigIntFromInt(int value,
493 : PretenureFlag pretenure = NOT_TENURED);
494 :
495 : Handle<JSWeakMap> NewJSWeakMap();
496 :
497 : Handle<JSObject> NewArgumentsObject(Handle<JSFunction> callee, int length);
498 :
499 : // JS objects are pretenured when allocated by the bootstrapper and
500 : // runtime.
501 : Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
502 : PretenureFlag pretenure = NOT_TENURED);
503 : // JSObject without a prototype.
504 : Handle<JSObject> NewJSObjectWithNullProto(
505 : PretenureFlag pretenure = NOT_TENURED);
506 :
507 : // Global objects are pretenured and initialized based on a constructor.
508 : Handle<JSGlobalObject> NewJSGlobalObject(Handle<JSFunction> constructor);
509 :
510 : // JS objects are pretenured when allocated by the bootstrapper and
511 : // runtime.
512 : Handle<JSObject> NewJSObjectFromMap(
513 : Handle<Map> map,
514 : PretenureFlag pretenure = NOT_TENURED,
515 : Handle<AllocationSite> allocation_site = Handle<AllocationSite>::null());
516 : Handle<JSObject> NewSlowJSObjectFromMap(
517 : Handle<Map> map,
518 : int number_of_slow_properties = NameDictionary::kInitialCapacity,
519 : PretenureFlag pretenure = NOT_TENURED);
520 :
521 : // JS arrays are pretenured when allocated by the parser.
522 :
523 : // Create a JSArray with a specified length and elements initialized
524 : // according to the specified mode.
525 : Handle<JSArray> NewJSArray(
526 : ElementsKind elements_kind, int length, int capacity,
527 : ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS,
528 : PretenureFlag pretenure = NOT_TENURED);
529 :
530 1266404 : Handle<JSArray> NewJSArray(
531 : int capacity, ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
532 : PretenureFlag pretenure = NOT_TENURED) {
533 1266404 : if (capacity != 0) {
534 : elements_kind = GetHoleyElementsKind(elements_kind);
535 : }
536 : return NewJSArray(elements_kind, 0, capacity,
537 1266404 : INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE, pretenure);
538 : }
539 :
540 : // Create a JSArray with the given elements.
541 : Handle<JSArray> NewJSArrayWithElements(Handle<FixedArrayBase> elements,
542 : ElementsKind elements_kind, int length,
543 : PretenureFlag pretenure = NOT_TENURED);
544 :
545 : inline Handle<JSArray> NewJSArrayWithElements(
546 : Handle<FixedArrayBase> elements,
547 : ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
548 : PretenureFlag pretenure = NOT_TENURED);
549 :
550 : void NewJSArrayStorage(
551 : Handle<JSArray> array,
552 : int length,
553 : int capacity,
554 : ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS);
555 :
556 : Handle<JSGeneratorObject> NewJSGeneratorObject(Handle<JSFunction> function);
557 :
558 : Handle<JSModuleNamespace> NewJSModuleNamespace();
559 :
560 : Handle<Module> NewModule(Handle<SharedFunctionInfo> code);
561 :
562 : Handle<JSArrayBuffer> NewJSArrayBuffer(
563 : SharedFlag shared = SharedFlag::kNotShared,
564 : PretenureFlag pretenure = NOT_TENURED);
565 :
566 : ExternalArrayType GetArrayTypeFromElementsKind(ElementsKind kind);
567 : size_t GetExternalArrayElementSize(ExternalArrayType type);
568 :
569 : Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type,
570 : PretenureFlag pretenure = NOT_TENURED);
571 :
572 : Handle<JSTypedArray> NewJSTypedArray(ElementsKind elements_kind,
573 : PretenureFlag pretenure = NOT_TENURED);
574 :
575 : // Creates a new JSTypedArray with the specified buffer.
576 : Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type,
577 : Handle<JSArrayBuffer> buffer,
578 : size_t byte_offset, size_t length,
579 : PretenureFlag pretenure = NOT_TENURED);
580 :
581 : // Creates a new on-heap JSTypedArray.
582 : Handle<JSTypedArray> NewJSTypedArray(ElementsKind elements_kind,
583 : size_t number_of_elements,
584 : PretenureFlag pretenure = NOT_TENURED);
585 :
586 : Handle<JSDataView> NewJSDataView();
587 : Handle<JSDataView> NewJSDataView(Handle<JSArrayBuffer> buffer,
588 : size_t byte_offset, size_t byte_length);
589 :
590 : Handle<JSIteratorResult> NewJSIteratorResult(Handle<Object> value, bool done);
591 : Handle<JSAsyncFromSyncIterator> NewJSAsyncFromSyncIterator(
592 : Handle<JSReceiver> sync_iterator);
593 :
594 : Handle<JSMap> NewJSMap();
595 : Handle<JSSet> NewJSSet();
596 :
597 : Handle<JSMapIterator> NewJSMapIterator(Handle<Map> map,
598 : Handle<OrderedHashMap> table,
599 : int index);
600 : Handle<JSSetIterator> NewJSSetIterator(Handle<Map> map,
601 : Handle<OrderedHashSet> table,
602 : int index);
603 :
604 : // Allocates a bound function.
605 : MaybeHandle<JSBoundFunction> NewJSBoundFunction(
606 : Handle<JSReceiver> target_function, Handle<Object> bound_this,
607 : Vector<Handle<Object>> bound_args);
608 :
609 : // Allocates a Harmony proxy.
610 : Handle<JSProxy> NewJSProxy(Handle<JSReceiver> target,
611 : Handle<JSReceiver> handler);
612 :
613 : // Reinitialize an JSGlobalProxy based on a constructor. The object
614 : // must have the same size as objects allocated using the
615 : // constructor. The object is reinitialized and behaves as an
616 : // object that has been freshly allocated using the constructor.
617 : void ReinitializeJSGlobalProxy(Handle<JSGlobalProxy> global,
618 : Handle<JSFunction> constructor);
619 :
620 : Handle<JSGlobalProxy> NewUninitializedJSGlobalProxy(int size);
621 :
622 : Handle<JSFunction> NewFunction(Handle<Map> map,
623 : Handle<SharedFunctionInfo> info,
624 : Handle<Object> context_or_undefined,
625 : PretenureFlag pretenure = TENURED);
626 : Handle<JSFunction> NewFunction(
627 : Handle<String> name, Handle<Code> code, Handle<Object> prototype,
628 : LanguageMode language_mode = LanguageMode::kSloppy,
629 : MutableMode prototype_mutability = MUTABLE);
630 : Handle<JSFunction> NewFunction(Handle<String> name);
631 : Handle<JSFunction> NewFunctionWithoutPrototype(
632 : Handle<String> name, Handle<Code> code,
633 : LanguageMode language_mode = LanguageMode::kSloppy);
634 :
635 : Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
636 : Handle<Map> initial_map, Handle<SharedFunctionInfo> function_info,
637 : Handle<Object> context_or_undefined, Handle<Cell> vector,
638 : PretenureFlag pretenure = TENURED);
639 :
640 : Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
641 : Handle<SharedFunctionInfo> function_info, Handle<Context> context,
642 : Handle<Cell> vector, PretenureFlag pretenure = TENURED);
643 :
644 : Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
645 : Handle<Map> initial_map, Handle<SharedFunctionInfo> function_info,
646 : Handle<Object> context_or_undefined, PretenureFlag pretenure = TENURED);
647 :
648 : Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
649 : Handle<SharedFunctionInfo> function_info, Handle<Context> context,
650 : PretenureFlag pretenure = TENURED);
651 :
652 : Handle<JSFunction> NewFunction(
653 : Handle<String> name, Handle<Code> code, Handle<Object> prototype,
654 : InstanceType type, int instance_size, int inobject_properties,
655 : LanguageMode language_mode = LanguageMode::kSloppy,
656 : MutableMode prototype_mutability = MUTABLE);
657 : Handle<JSFunction> NewFunction(Handle<String> name, Handle<Code> code,
658 : InstanceType type, int instance_size,
659 : int inobject_properties);
660 : Handle<JSFunction> NewFunction(Handle<Map> map, Handle<String> name,
661 : MaybeHandle<Code> maybe_code);
662 :
663 : // Create a serialized scope info.
664 : Handle<ScopeInfo> NewScopeInfo(int length);
665 :
666 : Handle<ModuleInfoEntry> NewModuleInfoEntry();
667 : Handle<ModuleInfo> NewModuleInfo();
668 :
669 : Handle<PreParsedScopeData> NewPreParsedScopeData();
670 :
671 : // Create an External object for V8's external API.
672 : Handle<JSObject> NewExternal(void* value);
673 :
674 : // The reference to the Code object is stored in self_reference.
675 : // This allows generated code to reference its own Code object
676 : // by containing this handle.
677 : Handle<Code> NewCode(const CodeDesc& desc, Code::Kind kind,
678 : Handle<Object> self_reference,
679 : MaybeHandle<HandlerTable> maybe_handler_table =
680 : MaybeHandle<HandlerTable>(),
681 : MaybeHandle<ByteArray> maybe_source_position_table =
682 : MaybeHandle<ByteArray>(),
683 : MaybeHandle<DeoptimizationData> maybe_deopt_data =
684 : MaybeHandle<DeoptimizationData>(),
685 : bool immovable = false);
686 :
687 : // Allocates a new, empty code object for use by builtin deserialization. The
688 : // given {size} argument specifies the size of the entire code object.
689 : Handle<Code> NewCodeForDeserialization(uint32_t size);
690 :
691 : Handle<Code> CopyCode(Handle<Code> code);
692 :
693 : Handle<BytecodeArray> CopyBytecodeArray(Handle<BytecodeArray>);
694 :
695 : // Interface for creating error objects.
696 : Handle<Object> NewError(Handle<JSFunction> constructor,
697 : Handle<String> message);
698 :
699 : Handle<Object> NewInvalidStringLengthError();
700 :
701 : inline Handle<Object> NewURIError();
702 :
703 : Handle<Object> NewError(Handle<JSFunction> constructor,
704 : MessageTemplate::Template template_index,
705 : Handle<Object> arg0 = Handle<Object>(),
706 : Handle<Object> arg1 = Handle<Object>(),
707 : Handle<Object> arg2 = Handle<Object>());
708 :
709 : #define DECLARE_ERROR(NAME) \
710 : Handle<Object> New##NAME(MessageTemplate::Template template_index, \
711 : Handle<Object> arg0 = Handle<Object>(), \
712 : Handle<Object> arg1 = Handle<Object>(), \
713 : Handle<Object> arg2 = Handle<Object>());
714 : DECLARE_ERROR(Error)
715 : DECLARE_ERROR(EvalError)
716 : DECLARE_ERROR(RangeError)
717 : DECLARE_ERROR(ReferenceError)
718 : DECLARE_ERROR(SyntaxError)
719 : DECLARE_ERROR(TypeError)
720 : DECLARE_ERROR(WasmCompileError)
721 : DECLARE_ERROR(WasmLinkError)
722 : DECLARE_ERROR(WasmRuntimeError)
723 : #undef DECLARE_ERROR
724 :
725 : Handle<String> NumberToString(Handle<Object> number,
726 : bool check_number_string_cache = true);
727 :
728 : inline Handle<String> Uint32ToString(uint32_t value);
729 :
730 : Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
731 :
732 : #define ROOT_ACCESSOR(type, name, camel_name) inline Handle<type> name();
733 : ROOT_LIST(ROOT_ACCESSOR)
734 : #undef ROOT_ACCESSOR
735 :
736 : #define STRUCT_MAP_ACCESSOR(NAME, Name, name) inline Handle<Map> name##_map();
737 : STRUCT_LIST(STRUCT_MAP_ACCESSOR)
738 : #undef STRUCT_MAP_ACCESSOR
739 :
740 : #define STRING_ACCESSOR(name, str) inline Handle<String> name();
741 : INTERNALIZED_STRING_LIST(STRING_ACCESSOR)
742 : #undef STRING_ACCESSOR
743 :
744 : #define SYMBOL_ACCESSOR(name) inline Handle<Symbol> name();
745 : PRIVATE_SYMBOL_LIST(SYMBOL_ACCESSOR)
746 : #undef SYMBOL_ACCESSOR
747 :
748 : #define SYMBOL_ACCESSOR(name, description) inline Handle<Symbol> name();
749 : PUBLIC_SYMBOL_LIST(SYMBOL_ACCESSOR)
750 : WELL_KNOWN_SYMBOL_LIST(SYMBOL_ACCESSOR)
751 : #undef SYMBOL_ACCESSOR
752 :
753 : // Allocates a new SharedFunctionInfo object.
754 : Handle<SharedFunctionInfo> NewSharedFunctionInfo(
755 : MaybeHandle<String> name, FunctionKind kind, Handle<Code> code,
756 : Handle<ScopeInfo> scope_info);
757 : Handle<SharedFunctionInfo> NewSharedFunctionInfo(
758 : MaybeHandle<String> name, MaybeHandle<Code> code, bool is_constructor,
759 : FunctionKind kind = kNormalFunction);
760 :
761 : Handle<SharedFunctionInfo> NewSharedFunctionInfoForLiteral(
762 : FunctionLiteral* literal, Handle<Script> script);
763 :
764 : static bool IsFunctionModeWithPrototype(FunctionMode function_mode) {
765 854 : return (function_mode & kWithPrototypeBits) != 0;
766 : }
767 :
768 : static bool IsFunctionModeWithWritablePrototype(FunctionMode function_mode) {
769 488 : return (function_mode & kWithWritablePrototypeBit) != 0;
770 : }
771 :
772 : static bool IsFunctionModeWithName(FunctionMode function_mode) {
773 854 : return (function_mode & kWithNameBit) != 0;
774 : }
775 :
776 : static bool IsFunctionModeWithHomeObject(FunctionMode function_mode) {
777 549 : return (function_mode & kWithHomeObjectBit) != 0;
778 : }
779 :
780 : Handle<Map> CreateSloppyFunctionMap(
781 : FunctionMode function_mode, MaybeHandle<JSFunction> maybe_empty_function);
782 :
783 : Handle<Map> CreateStrictFunctionMap(FunctionMode function_mode,
784 : Handle<JSFunction> empty_function);
785 :
786 : Handle<Map> CreateClassFunctionMap(Handle<JSFunction> empty_function);
787 :
788 : // Allocates a new JSMessageObject object.
789 : Handle<JSMessageObject> NewJSMessageObject(MessageTemplate::Template message,
790 : Handle<Object> argument,
791 : int start_position,
792 : int end_position,
793 : Handle<Object> script,
794 : Handle<Object> stack_frames);
795 :
796 : Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
797 :
798 : Handle<CoverageInfo> NewCoverageInfo(const ZoneVector<SourceRange>& slots);
799 :
800 : // Return a map for given number of properties using the map cache in the
801 : // native context.
802 : Handle<Map> ObjectLiteralMapFromCache(Handle<Context> native_context,
803 : int number_of_properties);
804 :
805 : Handle<RegExpMatchInfo> NewRegExpMatchInfo();
806 :
807 : // Creates a new FixedArray that holds the data associated with the
808 : // atom regexp and stores it in the regexp.
809 : void SetRegExpAtomData(Handle<JSRegExp> regexp,
810 : JSRegExp::Type type,
811 : Handle<String> source,
812 : JSRegExp::Flags flags,
813 : Handle<Object> match_pattern);
814 :
815 : // Creates a new FixedArray that holds the data associated with the
816 : // irregexp regexp and stores it in the regexp.
817 : void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
818 : JSRegExp::Type type,
819 : Handle<String> source,
820 : JSRegExp::Flags flags,
821 : int capture_count);
822 :
823 : // Returns the value for a known global constant (a property of the global
824 : // object which is neither configurable nor writable) like 'undefined'.
825 : // Returns a null handle when the given name is unknown.
826 : Handle<Object> GlobalConstantFor(Handle<Name> name);
827 :
828 : // Converts the given boolean condition to JavaScript boolean value.
829 : Handle<Object> ToBoolean(bool value);
830 :
831 : // Converts the given ToPrimitive hint to it's string representation.
832 : Handle<String> ToPrimitiveHintString(ToPrimitiveHint hint);
833 :
834 : private:
835 : Isolate* isolate() { return reinterpret_cast<Isolate*>(this); }
836 :
837 : // Creates a heap object based on the map. The fields of the heap object are
838 : // not initialized by New<>() functions. It's the responsibility of the caller
839 : // to do that.
840 : template<typename T>
841 : Handle<T> New(Handle<Map> map, AllocationSpace space);
842 :
843 : template<typename T>
844 : Handle<T> New(Handle<Map> map,
845 : AllocationSpace space,
846 : Handle<AllocationSite> allocation_site);
847 :
848 : MaybeHandle<String> NewStringFromTwoByte(const uc16* string, int length,
849 : PretenureFlag pretenure);
850 :
851 : // Creates a code object that is not yet fully initialized yet.
852 : Handle<Code> NewCodeRaw(int object_size, bool immovable);
853 :
854 : // Attempt to find the number in a small cache. If we finds it, return
855 : // the string representation of the number. Otherwise return undefined.
856 : Handle<Object> GetNumberStringCache(Handle<Object> number);
857 :
858 : // Update the cache with a new number-string pair.
859 : void SetNumberStringCache(Handle<Object> number, Handle<String> string);
860 :
861 : // Create a JSArray with no elements and no length.
862 : Handle<JSArray> NewJSArray(ElementsKind elements_kind,
863 : PretenureFlag pretenure = NOT_TENURED);
864 : };
865 :
866 : } // namespace internal
867 : } // namespace v8
868 :
869 : #endif // V8_FACTORY_H_
|