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