8b6fadab6f4696c46b30fc09337efcdbb57a1ea0
[platform/upstream/v8.git] / src / factory.h
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/isolate.h"
9 #include "src/messages.h"
10
11 namespace v8 {
12 namespace internal {
13
14 class FeedbackVectorSpec;
15
16 // Interface for handle based allocation.
17 class Factory final {
18  public:
19   Handle<Oddball> NewOddball(Handle<Map> map,
20                              const char* to_string,
21                              Handle<Object> to_number,
22                              byte kind);
23
24   // Allocates a fixed array initialized with undefined values.
25   Handle<FixedArray> NewFixedArray(
26       int size,
27       PretenureFlag pretenure = NOT_TENURED);
28
29   // Allocate a new fixed array with non-existing entries (the hole).
30   Handle<FixedArray> NewFixedArrayWithHoles(
31       int size,
32       PretenureFlag pretenure = NOT_TENURED);
33
34   // Allocates an uninitialized fixed array. It must be filled by the caller.
35   Handle<FixedArray> NewUninitializedFixedArray(int size);
36
37   // Allocate a new uninitialized fixed double array.
38   // The function returns a pre-allocated empty fixed array for capacity = 0,
39   // so the return type must be the general fixed array class.
40   Handle<FixedArrayBase> NewFixedDoubleArray(
41       int size,
42       PretenureFlag pretenure = NOT_TENURED);
43
44   // Allocate a new fixed double array with hole values.
45   Handle<FixedArrayBase> NewFixedDoubleArrayWithHoles(
46       int size,
47       PretenureFlag pretenure = NOT_TENURED);
48
49   Handle<OrderedHashSet> NewOrderedHashSet();
50   Handle<OrderedHashMap> NewOrderedHashMap();
51
52   // Create a new boxed value.
53   Handle<Box> NewBox(Handle<Object> value);
54
55   // Create a new PrototypeInfo struct.
56   Handle<PrototypeInfo> NewPrototypeInfo();
57
58   // Create a pre-tenured empty AccessorPair.
59   Handle<AccessorPair> NewAccessorPair();
60
61   // Create an empty TypeFeedbackInfo.
62   Handle<TypeFeedbackInfo> NewTypeFeedbackInfo();
63
64   // Finds the internalized copy for string in the string table.
65   // If not found, a new string is added to the table and returned.
66   Handle<String> InternalizeUtf8String(Vector<const char> str);
67   Handle<String> InternalizeUtf8String(const char* str) {
68     return InternalizeUtf8String(CStrVector(str));
69   }
70   Handle<String> InternalizeString(Handle<String> str);
71   Handle<String> InternalizeOneByteString(Vector<const uint8_t> str);
72   Handle<String> InternalizeOneByteString(
73       Handle<SeqOneByteString>, int from, int length);
74
75   Handle<String> InternalizeTwoByteString(Vector<const uc16> str);
76
77   template<class StringTableKey>
78   Handle<String> InternalizeStringWithKey(StringTableKey* key);
79
80
81   // String creation functions.  Most of the string creation functions take
82   // a Heap::PretenureFlag argument to optionally request that they be
83   // allocated in the old generation.  The pretenure flag defaults to
84   // DONT_TENURE.
85   //
86   // Creates a new String object.  There are two String encodings: one-byte and
87   // two-byte.  One should choose between the three string factory functions
88   // based on the encoding of the string buffer that the string is
89   // initialized from.
90   //   - ...FromOneByte initializes the string from a buffer that is Latin1
91   //     encoded (it does not check that the buffer is Latin1 encoded) and
92   //     the result will be Latin1 encoded.
93   //   - ...FromUtf8 initializes the string from a buffer that is UTF-8
94   //     encoded.  If the characters are all ASCII characters, the result
95   //     will be Latin1 encoded, otherwise it will converted to two-byte.
96   //   - ...FromTwoByte initializes the string from a buffer that is two-byte
97   //     encoded.  If the characters are all Latin1 characters, the result
98   //     will be converted to Latin1, otherwise it will be left as two-byte.
99   //
100   // One-byte strings are pretenured when used as keys in the SourceCodeCache.
101   MUST_USE_RESULT MaybeHandle<String> NewStringFromOneByte(
102       Vector<const uint8_t> str,
103       PretenureFlag pretenure = NOT_TENURED);
104
105   template <size_t N>
106   inline Handle<String> NewStringFromStaticChars(
107       const char (&str)[N], PretenureFlag pretenure = NOT_TENURED) {
108     DCHECK(N == StrLength(str) + 1);
109     return NewStringFromOneByte(STATIC_CHAR_VECTOR(str), pretenure)
110         .ToHandleChecked();
111   }
112
113   inline Handle<String> NewStringFromAsciiChecked(
114       const char* str,
115       PretenureFlag pretenure = NOT_TENURED) {
116     return NewStringFromOneByte(
117         OneByteVector(str), pretenure).ToHandleChecked();
118   }
119
120
121   // Allocates and fully initializes a String.  There are two String encodings:
122   // one-byte and two-byte. One should choose between the threestring
123   // allocation functions based on the encoding of the string buffer used to
124   // initialized the string.
125   //   - ...FromOneByte initializes the string from a buffer that is Latin1
126   //     encoded (it does not check that the buffer is Latin1 encoded) and the
127   //     result will be Latin1 encoded.
128   //   - ...FromUTF8 initializes the string from a buffer that is UTF-8
129   //     encoded.  If the characters are all ASCII characters, the result
130   //     will be Latin1 encoded, otherwise it will converted to two-byte.
131   //   - ...FromTwoByte initializes the string from a buffer that is two-byte
132   //     encoded.  If the characters are all Latin1 characters, the
133   //     result will be converted to Latin1, otherwise it will be left as
134   //     two-byte.
135
136   // TODO(dcarney): remove this function.
137   MUST_USE_RESULT inline MaybeHandle<String> NewStringFromAscii(
138       Vector<const char> str,
139       PretenureFlag pretenure = NOT_TENURED) {
140     return NewStringFromOneByte(Vector<const uint8_t>::cast(str), pretenure);
141   }
142
143   // UTF8 strings are pretenured when used for regexp literal patterns and
144   // flags in the parser.
145   MUST_USE_RESULT MaybeHandle<String> NewStringFromUtf8(
146       Vector<const char> str,
147       PretenureFlag pretenure = NOT_TENURED);
148
149   MUST_USE_RESULT MaybeHandle<String> NewStringFromTwoByte(
150       Vector<const uc16> str,
151       PretenureFlag pretenure = NOT_TENURED);
152
153   // Allocates an internalized string in old space based on the character
154   // stream.
155   MUST_USE_RESULT Handle<String> NewInternalizedStringFromUtf8(
156       Vector<const char> str,
157       int chars,
158       uint32_t hash_field);
159
160   MUST_USE_RESULT Handle<String> NewOneByteInternalizedString(
161       Vector<const uint8_t> str, uint32_t hash_field);
162
163   MUST_USE_RESULT Handle<String> NewOneByteInternalizedSubString(
164       Handle<SeqOneByteString> string, int offset, int length,
165       uint32_t hash_field);
166
167   MUST_USE_RESULT Handle<String> NewTwoByteInternalizedString(
168         Vector<const uc16> str,
169         uint32_t hash_field);
170
171   MUST_USE_RESULT Handle<String> NewInternalizedStringImpl(
172       Handle<String> string, int chars, uint32_t hash_field);
173
174   // Compute the matching internalized string map for a string if possible.
175   // Empty handle is returned if string is in new space or not flattened.
176   MUST_USE_RESULT MaybeHandle<Map> InternalizedStringMapForString(
177       Handle<String> string);
178
179   // Allocates and partially initializes an one-byte or two-byte String. The
180   // characters of the string are uninitialized. Currently used in regexp code
181   // only, where they are pretenured.
182   MUST_USE_RESULT MaybeHandle<SeqOneByteString> NewRawOneByteString(
183       int length,
184       PretenureFlag pretenure = NOT_TENURED);
185   MUST_USE_RESULT MaybeHandle<SeqTwoByteString> NewRawTwoByteString(
186       int length,
187       PretenureFlag pretenure = NOT_TENURED);
188
189   // Creates a single character string where the character has given code.
190   // A cache is used for Latin1 codes.
191   Handle<String> LookupSingleCharacterStringFromCode(uint32_t code);
192
193   // Create a new cons string object which consists of a pair of strings.
194   MUST_USE_RESULT MaybeHandle<String> NewConsString(Handle<String> left,
195                                                     Handle<String> right);
196   MUST_USE_RESULT MaybeHandle<String> NewOneByteConsString(
197       int length, Handle<String> left, Handle<String> right);
198   MUST_USE_RESULT MaybeHandle<String> NewTwoByteConsString(
199       int length, Handle<String> left, Handle<String> right);
200   MUST_USE_RESULT MaybeHandle<String> NewRawConsString(Handle<Map> map,
201                                                        int length,
202                                                        Handle<String> left,
203                                                        Handle<String> right);
204
205   // Create a new string object which holds a proper substring of a string.
206   Handle<String> NewProperSubString(Handle<String> str,
207                                     int begin,
208                                     int end);
209
210   // Create a new string object which holds a substring of a string.
211   Handle<String> NewSubString(Handle<String> str, int begin, int end) {
212     if (begin == 0 && end == str->length()) return str;
213     return NewProperSubString(str, begin, end);
214   }
215
216   // Creates a new external String object.  There are two String encodings
217   // in the system: one-byte and two-byte.  Unlike other String types, it does
218   // not make sense to have a UTF-8 factory function for external strings,
219   // because we cannot change the underlying buffer.  Note that these strings
220   // are backed by a string resource that resides outside the V8 heap.
221   MUST_USE_RESULT MaybeHandle<String> NewExternalStringFromOneByte(
222       const ExternalOneByteString::Resource* resource);
223   MUST_USE_RESULT MaybeHandle<String> NewExternalStringFromTwoByte(
224       const ExternalTwoByteString::Resource* resource);
225
226   // Create a symbol.
227   Handle<Symbol> NewSymbol();
228   Handle<Symbol> NewPrivateSymbol();
229   Handle<Symbol> NewPrivateOwnSymbol(Handle<Object> name);
230
231   // Create a global (but otherwise uninitialized) context.
232   Handle<Context> NewNativeContext();
233
234   // Create a script context.
235   Handle<Context> NewScriptContext(Handle<JSFunction> function,
236                                    Handle<ScopeInfo> scope_info);
237
238   // Create an empty script context table.
239   Handle<ScriptContextTable> NewScriptContextTable();
240
241   // Create a module context.
242   Handle<Context> NewModuleContext(Handle<ScopeInfo> scope_info);
243
244   // Create a function context.
245   Handle<Context> NewFunctionContext(int length, Handle<JSFunction> function);
246
247   // Create a catch context.
248   Handle<Context> NewCatchContext(Handle<JSFunction> function,
249                                   Handle<Context> previous,
250                                   Handle<String> name,
251                                   Handle<Object> thrown_object);
252
253   // Create a 'with' context.
254   Handle<Context> NewWithContext(Handle<JSFunction> function,
255                                  Handle<Context> previous,
256                                  Handle<JSReceiver> extension);
257
258   // Create a block context.
259   Handle<Context> NewBlockContext(Handle<JSFunction> function,
260                                   Handle<Context> previous,
261                                   Handle<ScopeInfo> scope_info);
262
263   // Allocate a new struct.  The struct is pretenured (allocated directly in
264   // the old generation).
265   Handle<Struct> NewStruct(InstanceType type);
266
267   Handle<CodeCache> NewCodeCache();
268
269   Handle<AliasedArgumentsEntry> NewAliasedArgumentsEntry(
270       int aliased_context_slot);
271
272   Handle<ExecutableAccessorInfo> NewExecutableAccessorInfo();
273
274   Handle<Script> NewScript(Handle<String> source);
275
276   // Foreign objects are pretenured when allocated by the bootstrapper.
277   Handle<Foreign> NewForeign(Address addr,
278                              PretenureFlag pretenure = NOT_TENURED);
279
280   // Allocate a new foreign object.  The foreign is pretenured (allocated
281   // directly in the old generation).
282   Handle<Foreign> NewForeign(const AccessorDescriptor* foreign);
283
284   Handle<ByteArray> NewByteArray(int length,
285                                  PretenureFlag pretenure = NOT_TENURED);
286
287   Handle<ExternalArray> NewExternalArray(
288       int length,
289       ExternalArrayType array_type,
290       void* external_pointer,
291       PretenureFlag pretenure = NOT_TENURED);
292
293   Handle<FixedTypedArrayBase> NewFixedTypedArray(
294       int length, ExternalArrayType array_type, bool initialize,
295       PretenureFlag pretenure = NOT_TENURED);
296
297   Handle<Cell> NewCell(Handle<Object> value);
298
299   Handle<PropertyCell> NewPropertyCell();
300
301   Handle<WeakCell> NewWeakCell(Handle<HeapObject> value);
302
303   // Allocate a tenured AllocationSite. It's payload is null.
304   Handle<AllocationSite> NewAllocationSite();
305
306   Handle<Map> NewMap(
307       InstanceType type,
308       int instance_size,
309       ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND);
310
311   Handle<HeapObject> NewFillerObject(int size,
312                                      bool double_align,
313                                      AllocationSpace space);
314
315   Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
316
317   Handle<JSObject> CopyJSObject(Handle<JSObject> object);
318
319   Handle<JSObject> CopyJSObjectWithAllocationSite(Handle<JSObject> object,
320                                                   Handle<AllocationSite> site);
321
322   Handle<FixedArray> CopyFixedArrayWithMap(Handle<FixedArray> array,
323                                            Handle<Map> map);
324
325   Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
326
327   // This method expects a COW array in new space, and creates a copy
328   // of it in old space.
329   Handle<FixedArray> CopyAndTenureFixedCOWArray(Handle<FixedArray> array);
330
331   Handle<FixedDoubleArray> CopyFixedDoubleArray(
332       Handle<FixedDoubleArray> array);
333
334   // Numbers (e.g. literals) are pretenured by the parser.
335   // The return value may be a smi or a heap number.
336   Handle<Object> NewNumber(double value,
337                            PretenureFlag pretenure = NOT_TENURED);
338
339   Handle<Object> NewNumberFromInt(int32_t value,
340                                   PretenureFlag pretenure = NOT_TENURED);
341   Handle<Object> NewNumberFromUint(uint32_t value,
342                                   PretenureFlag pretenure = NOT_TENURED);
343   Handle<Object> NewNumberFromSize(size_t value,
344                                    PretenureFlag pretenure = NOT_TENURED) {
345     if (Smi::IsValid(static_cast<intptr_t>(value))) {
346       return Handle<Object>(Smi::FromIntptr(static_cast<intptr_t>(value)),
347                             isolate());
348     }
349     return NewNumber(static_cast<double>(value), pretenure);
350   }
351   Handle<HeapNumber> NewHeapNumber(double value,
352                                    MutableMode mode = IMMUTABLE,
353                                    PretenureFlag pretenure = NOT_TENURED);
354   Handle<Float32x4> NewFloat32x4(float w, float x, float y, float z,
355                                  PretenureFlag pretenure = NOT_TENURED);
356
357   // These objects are used by the api to create env-independent data
358   // structures in the heap.
359   inline Handle<JSObject> NewNeanderObject() {
360     return NewJSObjectFromMap(neander_map());
361   }
362
363   Handle<JSWeakMap> NewJSWeakMap();
364
365   Handle<JSObject> NewArgumentsObject(Handle<JSFunction> callee, int length);
366
367   // JS objects are pretenured when allocated by the bootstrapper and
368   // runtime.
369   Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
370                                PretenureFlag pretenure = NOT_TENURED);
371   // JSObject that should have a memento pointing to the allocation site.
372   Handle<JSObject> NewJSObjectWithMemento(Handle<JSFunction> constructor,
373                                           Handle<AllocationSite> site);
374
375   // Global objects are pretenured and initialized based on a constructor.
376   Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor);
377
378   // JS objects are pretenured when allocated by the bootstrapper and
379   // runtime.
380   Handle<JSObject> NewJSObjectFromMap(
381       Handle<Map> map,
382       PretenureFlag pretenure = NOT_TENURED,
383       bool allocate_properties = true,
384       Handle<AllocationSite> allocation_site = Handle<AllocationSite>::null());
385
386   // JS modules are pretenured.
387   Handle<JSModule> NewJSModule(Handle<Context> context,
388                                Handle<ScopeInfo> scope_info);
389
390   // JS arrays are pretenured when allocated by the parser.
391
392   // Create a JSArray with no elements.
393   Handle<JSArray> NewJSArray(
394       ElementsKind elements_kind,
395       ObjectStrength strength = WEAK,
396       PretenureFlag pretenure = NOT_TENURED);
397
398   // Create a JSArray with a specified length and elements initialized
399   // according to the specified mode.
400   Handle<JSArray> NewJSArray(
401       ElementsKind elements_kind, int length, int capacity,
402       ObjectStrength strength = WEAK,
403       ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS,
404       PretenureFlag pretenure = NOT_TENURED);
405
406   Handle<JSArray> NewJSArray(
407       int capacity,
408       ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
409       ObjectStrength strength = WEAK,
410       PretenureFlag pretenure = NOT_TENURED) {
411     if (capacity != 0) {
412       elements_kind = GetHoleyElementsKind(elements_kind);
413     }
414     return NewJSArray(elements_kind, 0, capacity, strength,
415                       INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE, pretenure);
416   }
417
418   // Create a JSArray with the given elements.
419   Handle<JSArray> NewJSArrayWithElements(
420       Handle<FixedArrayBase> elements,
421       ElementsKind elements_kind,
422       int length,
423       ObjectStrength strength = WEAK,
424       PretenureFlag pretenure = NOT_TENURED);
425
426   Handle<JSArray> NewJSArrayWithElements(
427       Handle<FixedArrayBase> elements,
428       ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
429       ObjectStrength strength = WEAK,
430       PretenureFlag pretenure = NOT_TENURED) {
431     return NewJSArrayWithElements(elements, elements_kind, elements->length(),
432                                   strength, pretenure);
433   }
434
435   void NewJSArrayStorage(
436       Handle<JSArray> array,
437       int length,
438       int capacity,
439       ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS);
440
441   Handle<JSGeneratorObject> NewJSGeneratorObject(Handle<JSFunction> function);
442
443   Handle<JSArrayBuffer> NewJSArrayBuffer(
444       SharedFlag shared = SharedFlag::kNotShared);
445
446   Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type);
447
448   Handle<JSTypedArray> NewJSTypedArray(ElementsKind elements_kind);
449
450   // Creates a new JSTypedArray with the specified buffer.
451   Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type,
452                                        Handle<JSArrayBuffer> buffer,
453                                        size_t byte_offset, size_t length);
454
455   // Creates a new on-heap JSTypedArray.
456   Handle<JSTypedArray> NewJSTypedArray(ElementsKind elements_kind,
457                                        size_t number_of_elements);
458
459   Handle<JSDataView> NewJSDataView();
460   Handle<JSDataView> NewJSDataView(Handle<JSArrayBuffer> buffer,
461                                    size_t byte_offset, size_t byte_length);
462
463   Handle<JSMap> NewJSMap();
464   Handle<JSSet> NewJSSet();
465
466   // TODO(aandrey): Maybe these should take table, index and kind arguments.
467   Handle<JSMapIterator> NewJSMapIterator();
468   Handle<JSSetIterator> NewJSSetIterator();
469
470   // Allocates a Harmony proxy.
471   Handle<JSProxy> NewJSProxy(Handle<Object> handler, Handle<Object> prototype);
472
473   // Allocates a Harmony function proxy.
474   Handle<JSProxy> NewJSFunctionProxy(Handle<Object> handler,
475                                      Handle<Object> call_trap,
476                                      Handle<Object> construct_trap,
477                                      Handle<Object> prototype);
478
479   // Reinitialize an JSGlobalProxy based on a constructor.  The object
480   // must have the same size as objects allocated using the
481   // constructor.  The object is reinitialized and behaves as an
482   // object that has been freshly allocated using the constructor.
483   void ReinitializeJSGlobalProxy(Handle<JSGlobalProxy> global,
484                                  Handle<JSFunction> constructor);
485
486   Handle<JSGlobalProxy> NewUninitializedJSGlobalProxy();
487
488   // Change the type of the argument into a JS object/function and reinitialize.
489   void BecomeJSObject(Handle<JSProxy> object);
490   void BecomeJSFunction(Handle<JSProxy> object);
491
492   Handle<JSFunction> NewFunction(Handle<String> name, Handle<Code> code,
493                                  Handle<Object> prototype,
494                                  bool read_only_prototype = false,
495                                  bool is_strict = false);
496   Handle<JSFunction> NewFunction(Handle<String> name);
497   Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name,
498                                                  Handle<Code> code,
499                                                  bool is_strict = false);
500
501   Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
502       Handle<SharedFunctionInfo> function_info,
503       Handle<Context> context,
504       PretenureFlag pretenure = TENURED);
505
506   Handle<JSFunction> NewFunction(Handle<String> name, Handle<Code> code,
507                                  Handle<Object> prototype, InstanceType type,
508                                  int instance_size,
509                                  bool read_only_prototype = false,
510                                  bool install_constructor = false,
511                                  bool is_strict = false);
512   Handle<JSFunction> NewFunction(Handle<String> name,
513                                  Handle<Code> code,
514                                  InstanceType type,
515                                  int instance_size);
516
517   // Create a serialized scope info.
518   Handle<ScopeInfo> NewScopeInfo(int length);
519
520   // Create an External object for V8's external API.
521   Handle<JSObject> NewExternal(void* value);
522
523   // The reference to the Code object is stored in self_reference.
524   // This allows generated code to reference its own Code object
525   // by containing this handle.
526   Handle<Code> NewCode(const CodeDesc& desc,
527                        Code::Flags flags,
528                        Handle<Object> self_reference,
529                        bool immovable = false,
530                        bool crankshafted = false,
531                        int prologue_offset = Code::kPrologueOffsetNotSet,
532                        bool is_debug = false);
533
534   Handle<Code> CopyCode(Handle<Code> code);
535
536   Handle<Code> CopyCode(Handle<Code> code, Vector<byte> reloc_info);
537
538   // Interface for creating error objects.
539
540   Handle<Object> NewError(const char* maker, const char* message,
541                           Handle<JSArray> args);
542   Handle<String> EmergencyNewError(const char* message, Handle<JSArray> args);
543
544   Handle<Object> NewError(const char* constructor, Handle<String> message);
545
546   Handle<Object> NewInvalidStringLengthError() {
547     return NewRangeError(MessageTemplate::kInvalidStringLength);
548   }
549
550   Handle<Object> NewError(const char* maker,
551                           MessageTemplate::Template template_index,
552                           Handle<Object> arg0 = Handle<Object>(),
553                           Handle<Object> arg1 = Handle<Object>(),
554                           Handle<Object> arg2 = Handle<Object>());
555
556   Handle<Object> NewError(MessageTemplate::Template template_index,
557                           Handle<Object> arg0 = Handle<Object>(),
558                           Handle<Object> arg1 = Handle<Object>(),
559                           Handle<Object> arg2 = Handle<Object>());
560
561   Handle<Object> NewTypeError(MessageTemplate::Template template_index,
562                               Handle<Object> arg0 = Handle<Object>(),
563                               Handle<Object> arg1 = Handle<Object>(),
564                               Handle<Object> arg2 = Handle<Object>());
565
566   Handle<Object> NewSyntaxError(MessageTemplate::Template template_index,
567                                 Handle<Object> arg0 = Handle<Object>(),
568                                 Handle<Object> arg1 = Handle<Object>(),
569                                 Handle<Object> arg2 = Handle<Object>());
570
571   Handle<Object> NewReferenceError(MessageTemplate::Template template_index,
572                                    Handle<Object> arg0 = Handle<Object>(),
573                                    Handle<Object> arg1 = Handle<Object>(),
574                                    Handle<Object> arg2 = Handle<Object>());
575
576   Handle<Object> NewRangeError(MessageTemplate::Template template_index,
577                                Handle<Object> arg0 = Handle<Object>(),
578                                Handle<Object> arg1 = Handle<Object>(),
579                                Handle<Object> arg2 = Handle<Object>());
580
581   Handle<Object> NewEvalError(MessageTemplate::Template template_index,
582                               Handle<Object> arg0 = Handle<Object>(),
583                               Handle<Object> arg1 = Handle<Object>(),
584                               Handle<Object> arg2 = Handle<Object>());
585
586   Handle<String> NumberToString(Handle<Object> number,
587                                 bool check_number_string_cache = true);
588
589   Handle<String> Uint32ToString(uint32_t value) {
590     return NumberToString(NewNumberFromUint(value));
591   }
592
593   Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
594
595 #define ROOT_ACCESSOR(type, name, camel_name)                         \
596   inline Handle<type> name() {                                        \
597     return Handle<type>(bit_cast<type**>(                             \
598         &isolate()->heap()->roots_[Heap::k##camel_name##RootIndex])); \
599   }
600   ROOT_LIST(ROOT_ACCESSOR)
601 #undef ROOT_ACCESSOR
602
603 #define STRUCT_MAP_ACCESSOR(NAME, Name, name)                      \
604   inline Handle<Map> name##_map() {                                \
605     return Handle<Map>(bit_cast<Map**>(                            \
606         &isolate()->heap()->roots_[Heap::k##Name##MapRootIndex])); \
607   }
608   STRUCT_LIST(STRUCT_MAP_ACCESSOR)
609 #undef STRUCT_MAP_ACCESSOR
610
611 #define STRING_ACCESSOR(name, str)                              \
612   inline Handle<String> name() {                                \
613     return Handle<String>(bit_cast<String**>(                   \
614         &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
615   }
616   INTERNALIZED_STRING_LIST(STRING_ACCESSOR)
617 #undef STRING_ACCESSOR
618
619 #define SYMBOL_ACCESSOR(name)                                   \
620   inline Handle<Symbol> name() {                                \
621     return Handle<Symbol>(bit_cast<Symbol**>(                   \
622         &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
623   }
624   PRIVATE_SYMBOL_LIST(SYMBOL_ACCESSOR)
625 #undef SYMBOL_ACCESSOR
626
627 #define SYMBOL_ACCESSOR(name, varname, description)             \
628   inline Handle<Symbol> name() {                                \
629     return Handle<Symbol>(bit_cast<Symbol**>(                   \
630         &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
631   }
632   PUBLIC_SYMBOL_LIST(SYMBOL_ACCESSOR)
633 #undef SYMBOL_ACCESSOR
634
635   inline void set_string_table(Handle<StringTable> table) {
636     isolate()->heap()->set_string_table(*table);
637   }
638
639   inline void set_weak_stack_trace_list(Handle<WeakFixedArray> list) {
640     isolate()->heap()->set_weak_stack_trace_list(*list);
641   }
642
643   Handle<String> hidden_string() {
644     return Handle<String>(&isolate()->heap()->hidden_string_);
645   }
646
647   // Allocates a new SharedFunctionInfo object.
648   Handle<SharedFunctionInfo> NewSharedFunctionInfo(
649       Handle<String> name, int number_of_literals, FunctionKind kind,
650       Handle<Code> code, Handle<ScopeInfo> scope_info,
651       Handle<TypeFeedbackVector> feedback_vector);
652   Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name,
653                                                    MaybeHandle<Code> code);
654
655   // Allocate a new type feedback vector
656   template <typename Spec>
657   Handle<TypeFeedbackVector> NewTypeFeedbackVector(const Spec* spec);
658
659   // Allocates a new JSMessageObject object.
660   Handle<JSMessageObject> NewJSMessageObject(MessageTemplate::Template message,
661                                              Handle<Object> argument,
662                                              int start_position,
663                                              int end_position,
664                                              Handle<Object> script,
665                                              Handle<Object> stack_frames);
666
667   Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
668
669   // Return a map for given number of properties using the map cache in the
670   // native context.
671   Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
672                                         int number_of_properties,
673                                         bool is_strong,
674                                         bool* is_result_from_cache);
675
676   // Creates a new FixedArray that holds the data associated with the
677   // atom regexp and stores it in the regexp.
678   void SetRegExpAtomData(Handle<JSRegExp> regexp,
679                          JSRegExp::Type type,
680                          Handle<String> source,
681                          JSRegExp::Flags flags,
682                          Handle<Object> match_pattern);
683
684   // Creates a new FixedArray that holds the data associated with the
685   // irregexp regexp and stores it in the regexp.
686   void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
687                              JSRegExp::Type type,
688                              Handle<String> source,
689                              JSRegExp::Flags flags,
690                              int capture_count);
691
692   // Returns the value for a known global constant (a property of the global
693   // object which is neither configurable nor writable) like 'undefined'.
694   // Returns a null handle when the given name is unknown.
695   Handle<Object> GlobalConstantFor(Handle<Name> name);
696
697   // Converts the given boolean condition to JavaScript boolean value.
698   Handle<Object> ToBoolean(bool value);
699
700  private:
701   Isolate* isolate() { return reinterpret_cast<Isolate*>(this); }
702
703   // Creates a heap object based on the map. The fields of the heap object are
704   // not initialized by New<>() functions. It's the responsibility of the caller
705   // to do that.
706   template<typename T>
707   Handle<T> New(Handle<Map> map, AllocationSpace space);
708
709   template<typename T>
710   Handle<T> New(Handle<Map> map,
711                 AllocationSpace space,
712                 Handle<AllocationSite> allocation_site);
713
714   // Creates a code object that is not yet fully initialized yet.
715   inline Handle<Code> NewCodeRaw(int object_size, bool immovable);
716
717   // Attempt to find the number in a small cache.  If we finds it, return
718   // the string representation of the number.  Otherwise return undefined.
719   Handle<Object> GetNumberStringCache(Handle<Object> number);
720
721   // Update the cache with a new number-string pair.
722   void SetNumberStringCache(Handle<Object> number, Handle<String> string);
723
724   // Initializes a function with a shared part and prototype.
725   // Note: this code was factored out of NewFunction such that other parts of
726   // the VM could use it. Specifically, a function that creates instances of
727   // type JS_FUNCTION_TYPE benefit from the use of this function.
728   inline void InitializeFunction(Handle<JSFunction> function,
729                                  Handle<SharedFunctionInfo> info,
730                                  Handle<Context> context);
731
732   // Creates a function initialized with a shared part.
733   Handle<JSFunction> NewFunction(Handle<Map> map,
734                                  Handle<SharedFunctionInfo> info,
735                                  Handle<Context> context,
736                                  PretenureFlag pretenure = TENURED);
737
738   Handle<JSFunction> NewFunction(Handle<Map> map,
739                                  Handle<String> name,
740                                  MaybeHandle<Code> maybe_code);
741
742   // Reinitialize a JSProxy into an (empty) JS object of respective type and
743   // size, but keeping the original prototype.  The receiver must have at least
744   // the size of the new object.  The object is reinitialized and behaves as an
745   // object that has been freshly allocated.
746   void ReinitializeJSProxy(Handle<JSProxy> proxy, InstanceType type, int size);
747 };
748
749 } }  // namespace v8::internal
750
751 #endif  // V8_FACTORY_H_