1253779a1bb3071185ebd9cb124ff9fd276dcb21
[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(Handle<Object> name);
229
230   // Create a global (but otherwise uninitialized) context.
231   Handle<Context> NewNativeContext();
232
233   // Create a script context.
234   Handle<Context> NewScriptContext(Handle<JSFunction> function,
235                                    Handle<ScopeInfo> scope_info);
236
237   // Create an empty script context table.
238   Handle<ScriptContextTable> NewScriptContextTable();
239
240   // Create a module context.
241   Handle<Context> NewModuleContext(Handle<ScopeInfo> scope_info);
242
243   // Create a function context.
244   Handle<Context> NewFunctionContext(int length, Handle<JSFunction> function);
245
246   // Create a catch context.
247   Handle<Context> NewCatchContext(Handle<JSFunction> function,
248                                   Handle<Context> previous,
249                                   Handle<String> name,
250                                   Handle<Object> thrown_object);
251
252   // Create a 'with' context.
253   Handle<Context> NewWithContext(Handle<JSFunction> function,
254                                  Handle<Context> previous,
255                                  Handle<JSReceiver> extension);
256
257   // Create a block context.
258   Handle<Context> NewBlockContext(Handle<JSFunction> function,
259                                   Handle<Context> previous,
260                                   Handle<ScopeInfo> scope_info);
261
262   // Allocate a new struct.  The struct is pretenured (allocated directly in
263   // the old generation).
264   Handle<Struct> NewStruct(InstanceType type);
265
266   Handle<CodeCache> NewCodeCache();
267
268   Handle<AliasedArgumentsEntry> NewAliasedArgumentsEntry(
269       int aliased_context_slot);
270
271   Handle<ExecutableAccessorInfo> NewExecutableAccessorInfo();
272
273   Handle<Script> NewScript(Handle<String> source);
274
275   // Foreign objects are pretenured when allocated by the bootstrapper.
276   Handle<Foreign> NewForeign(Address addr,
277                              PretenureFlag pretenure = NOT_TENURED);
278
279   // Allocate a new foreign object.  The foreign is pretenured (allocated
280   // directly in the old generation).
281   Handle<Foreign> NewForeign(const AccessorDescriptor* foreign);
282
283   Handle<ByteArray> NewByteArray(int length,
284                                  PretenureFlag pretenure = NOT_TENURED);
285
286   Handle<BytecodeArray> NewBytecodeArray(int length, const byte* raw_bytecodes,
287                                          int frame_size);
288
289   Handle<ExternalArray> NewExternalArray(
290       int length,
291       ExternalArrayType array_type,
292       void* external_pointer,
293       PretenureFlag pretenure = NOT_TENURED);
294
295   Handle<FixedTypedArrayBase> NewFixedTypedArray(
296       int length, ExternalArrayType array_type, bool initialize,
297       PretenureFlag pretenure = NOT_TENURED);
298
299   Handle<Cell> NewCell(Handle<Object> value);
300
301   Handle<PropertyCell> NewPropertyCell();
302
303   Handle<WeakCell> NewWeakCell(Handle<HeapObject> value);
304
305   // Allocate a tenured AllocationSite. It's payload is null.
306   Handle<AllocationSite> NewAllocationSite();
307
308   Handle<Map> NewMap(
309       InstanceType type,
310       int instance_size,
311       ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND);
312
313   Handle<HeapObject> NewFillerObject(int size,
314                                      bool double_align,
315                                      AllocationSpace space);
316
317   Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
318
319   Handle<JSObject> CopyJSObject(Handle<JSObject> object);
320
321   Handle<JSObject> CopyJSObjectWithAllocationSite(Handle<JSObject> object,
322                                                   Handle<AllocationSite> site);
323
324   Handle<FixedArray> CopyFixedArrayWithMap(Handle<FixedArray> array,
325                                            Handle<Map> map);
326
327   Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
328
329   // This method expects a COW array in new space, and creates a copy
330   // of it in old space.
331   Handle<FixedArray> CopyAndTenureFixedCOWArray(Handle<FixedArray> array);
332
333   Handle<FixedDoubleArray> CopyFixedDoubleArray(
334       Handle<FixedDoubleArray> array);
335
336   // Numbers (e.g. literals) are pretenured by the parser.
337   // The return value may be a smi or a heap number.
338   Handle<Object> NewNumber(double value,
339                            PretenureFlag pretenure = NOT_TENURED);
340
341   Handle<Object> NewNumberFromInt(int32_t value,
342                                   PretenureFlag pretenure = NOT_TENURED);
343   Handle<Object> NewNumberFromUint(uint32_t value,
344                                   PretenureFlag pretenure = NOT_TENURED);
345   Handle<Object> NewNumberFromSize(size_t value,
346                                    PretenureFlag pretenure = NOT_TENURED) {
347     if (Smi::IsValid(static_cast<intptr_t>(value))) {
348       return Handle<Object>(Smi::FromIntptr(static_cast<intptr_t>(value)),
349                             isolate());
350     }
351     return NewNumber(static_cast<double>(value), pretenure);
352   }
353   Handle<HeapNumber> NewHeapNumber(double value,
354                                    MutableMode mode = IMMUTABLE,
355                                    PretenureFlag pretenure = NOT_TENURED);
356   Handle<Float32x4> NewFloat32x4(float w, float x, float y, float z,
357                                  PretenureFlag pretenure = NOT_TENURED);
358
359   // These objects are used by the api to create env-independent data
360   // structures in the heap.
361   inline Handle<JSObject> NewNeanderObject() {
362     return NewJSObjectFromMap(neander_map());
363   }
364
365   Handle<JSWeakMap> NewJSWeakMap();
366
367   Handle<JSObject> NewArgumentsObject(Handle<JSFunction> callee, int length);
368
369   // JS objects are pretenured when allocated by the bootstrapper and
370   // runtime.
371   Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
372                                PretenureFlag pretenure = NOT_TENURED);
373   // JSObject that should have a memento pointing to the allocation site.
374   Handle<JSObject> NewJSObjectWithMemento(Handle<JSFunction> constructor,
375                                           Handle<AllocationSite> site);
376
377   // Global objects are pretenured and initialized based on a constructor.
378   Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor);
379
380   // JS objects are pretenured when allocated by the bootstrapper and
381   // runtime.
382   Handle<JSObject> NewJSObjectFromMap(
383       Handle<Map> map,
384       PretenureFlag pretenure = NOT_TENURED,
385       Handle<AllocationSite> allocation_site = Handle<AllocationSite>::null());
386
387   // JS modules are pretenured.
388   Handle<JSModule> NewJSModule(Handle<Context> context,
389                                Handle<ScopeInfo> scope_info);
390
391   // JS arrays are pretenured when allocated by the parser.
392
393   // Create a JSArray with no elements.
394   Handle<JSArray> NewJSArray(ElementsKind elements_kind,
395                              Strength strength = 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       Strength strength = Strength::WEAK,
403       ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS,
404       PretenureFlag pretenure = NOT_TENURED);
405
406   Handle<JSArray> NewJSArray(
407       int capacity, ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
408       Strength strength = Strength::WEAK,
409       PretenureFlag pretenure = NOT_TENURED) {
410     if (capacity != 0) {
411       elements_kind = GetHoleyElementsKind(elements_kind);
412     }
413     return NewJSArray(elements_kind, 0, capacity, strength,
414                       INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE, pretenure);
415   }
416
417   // Create a JSArray with the given elements.
418   Handle<JSArray> NewJSArrayWithElements(Handle<FixedArrayBase> elements,
419                                          ElementsKind elements_kind, int length,
420                                          Strength strength = Strength::WEAK,
421                                          PretenureFlag pretenure = NOT_TENURED);
422
423   Handle<JSArray> NewJSArrayWithElements(
424       Handle<FixedArrayBase> elements,
425       ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
426       Strength strength = Strength::WEAK,
427       PretenureFlag pretenure = NOT_TENURED) {
428     return NewJSArrayWithElements(elements, elements_kind, elements->length(),
429                                   strength, pretenure);
430   }
431
432   void NewJSArrayStorage(
433       Handle<JSArray> array,
434       int length,
435       int capacity,
436       ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS);
437
438   Handle<JSGeneratorObject> NewJSGeneratorObject(Handle<JSFunction> function);
439
440   Handle<JSArrayBuffer> NewJSArrayBuffer(
441       SharedFlag shared = SharedFlag::kNotShared);
442
443   Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type);
444
445   Handle<JSTypedArray> NewJSTypedArray(ElementsKind elements_kind);
446
447   // Creates a new JSTypedArray with the specified buffer.
448   Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type,
449                                        Handle<JSArrayBuffer> buffer,
450                                        size_t byte_offset, size_t length);
451
452   // Creates a new on-heap JSTypedArray.
453   Handle<JSTypedArray> NewJSTypedArray(ElementsKind elements_kind,
454                                        size_t number_of_elements);
455
456   Handle<JSDataView> NewJSDataView();
457   Handle<JSDataView> NewJSDataView(Handle<JSArrayBuffer> buffer,
458                                    size_t byte_offset, size_t byte_length);
459
460   Handle<JSMap> NewJSMap();
461   Handle<JSSet> NewJSSet();
462
463   // TODO(aandrey): Maybe these should take table, index and kind arguments.
464   Handle<JSMapIterator> NewJSMapIterator();
465   Handle<JSSetIterator> NewJSSetIterator();
466
467   // Allocates a Harmony proxy.
468   Handle<JSProxy> NewJSProxy(Handle<Object> handler, Handle<Object> prototype);
469
470   // Allocates a Harmony function proxy.
471   Handle<JSProxy> NewJSFunctionProxy(Handle<Object> handler,
472                                      Handle<Object> call_trap,
473                                      Handle<Object> construct_trap,
474                                      Handle<Object> prototype);
475
476   // Reinitialize an JSGlobalProxy based on a constructor.  The object
477   // must have the same size as objects allocated using the
478   // constructor.  The object is reinitialized and behaves as an
479   // object that has been freshly allocated using the constructor.
480   void ReinitializeJSGlobalProxy(Handle<JSGlobalProxy> global,
481                                  Handle<JSFunction> constructor);
482
483   Handle<JSGlobalProxy> NewUninitializedJSGlobalProxy();
484
485   // Change the type of the argument into a JS object/function and reinitialize.
486   void BecomeJSObject(Handle<JSProxy> object);
487   void BecomeJSFunction(Handle<JSProxy> object);
488
489   Handle<JSFunction> NewFunction(Handle<String> name, Handle<Code> code,
490                                  Handle<Object> prototype,
491                                  bool read_only_prototype = false,
492                                  bool is_strict = false);
493   Handle<JSFunction> NewFunction(Handle<String> name);
494   Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name,
495                                                  Handle<Code> code,
496                                                  bool is_strict = false);
497
498   Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
499       Handle<SharedFunctionInfo> function_info,
500       Handle<Context> context,
501       PretenureFlag pretenure = TENURED);
502
503   Handle<JSFunction> NewFunction(Handle<String> name, Handle<Code> code,
504                                  Handle<Object> prototype, InstanceType type,
505                                  int instance_size,
506                                  bool read_only_prototype = false,
507                                  bool install_constructor = false,
508                                  bool is_strict = false);
509   Handle<JSFunction> NewFunction(Handle<String> name,
510                                  Handle<Code> code,
511                                  InstanceType type,
512                                  int instance_size);
513
514   // Create a serialized scope info.
515   Handle<ScopeInfo> NewScopeInfo(int length);
516
517   // Create an External object for V8's external API.
518   Handle<JSObject> NewExternal(void* value);
519
520   // The reference to the Code object is stored in self_reference.
521   // This allows generated code to reference its own Code object
522   // by containing this handle.
523   Handle<Code> NewCode(const CodeDesc& desc,
524                        Code::Flags flags,
525                        Handle<Object> self_reference,
526                        bool immovable = false,
527                        bool crankshafted = false,
528                        int prologue_offset = Code::kPrologueOffsetNotSet,
529                        bool is_debug = false);
530
531   Handle<Code> CopyCode(Handle<Code> code);
532
533   Handle<Code> CopyCode(Handle<Code> code, Vector<byte> reloc_info);
534
535   // Interface for creating error objects.
536
537   Handle<Object> NewError(const char* maker, const char* message,
538                           Handle<JSArray> args);
539   Handle<String> EmergencyNewError(const char* message, Handle<JSArray> args);
540
541   Handle<Object> NewError(const char* constructor, Handle<String> message);
542
543   Handle<Object> NewInvalidStringLengthError() {
544     return NewRangeError(MessageTemplate::kInvalidStringLength);
545   }
546
547   Handle<Object> NewError(const char* maker,
548                           MessageTemplate::Template template_index,
549                           Handle<Object> arg0 = Handle<Object>(),
550                           Handle<Object> arg1 = Handle<Object>(),
551                           Handle<Object> arg2 = Handle<Object>());
552
553   Handle<Object> NewError(MessageTemplate::Template template_index,
554                           Handle<Object> arg0 = Handle<Object>(),
555                           Handle<Object> arg1 = Handle<Object>(),
556                           Handle<Object> arg2 = Handle<Object>());
557
558   Handle<Object> NewTypeError(MessageTemplate::Template template_index,
559                               Handle<Object> arg0 = Handle<Object>(),
560                               Handle<Object> arg1 = Handle<Object>(),
561                               Handle<Object> arg2 = Handle<Object>());
562
563   Handle<Object> NewSyntaxError(MessageTemplate::Template template_index,
564                                 Handle<Object> arg0 = Handle<Object>(),
565                                 Handle<Object> arg1 = Handle<Object>(),
566                                 Handle<Object> arg2 = Handle<Object>());
567
568   Handle<Object> NewReferenceError(MessageTemplate::Template template_index,
569                                    Handle<Object> arg0 = Handle<Object>(),
570                                    Handle<Object> arg1 = Handle<Object>(),
571                                    Handle<Object> arg2 = Handle<Object>());
572
573   Handle<Object> NewRangeError(MessageTemplate::Template template_index,
574                                Handle<Object> arg0 = Handle<Object>(),
575                                Handle<Object> arg1 = Handle<Object>(),
576                                Handle<Object> arg2 = Handle<Object>());
577
578   Handle<Object> NewEvalError(MessageTemplate::Template template_index,
579                               Handle<Object> arg0 = Handle<Object>(),
580                               Handle<Object> arg1 = Handle<Object>(),
581                               Handle<Object> arg2 = Handle<Object>());
582
583   Handle<String> NumberToString(Handle<Object> number,
584                                 bool check_number_string_cache = true);
585
586   Handle<String> Uint32ToString(uint32_t value) {
587     return NumberToString(NewNumberFromUint(value));
588   }
589
590   Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
591
592 #define ROOT_ACCESSOR(type, name, camel_name)                         \
593   inline Handle<type> name() {                                        \
594     return Handle<type>(bit_cast<type**>(                             \
595         &isolate()->heap()->roots_[Heap::k##camel_name##RootIndex])); \
596   }
597   ROOT_LIST(ROOT_ACCESSOR)
598 #undef ROOT_ACCESSOR
599
600 #define STRUCT_MAP_ACCESSOR(NAME, Name, name)                      \
601   inline Handle<Map> name##_map() {                                \
602     return Handle<Map>(bit_cast<Map**>(                            \
603         &isolate()->heap()->roots_[Heap::k##Name##MapRootIndex])); \
604   }
605   STRUCT_LIST(STRUCT_MAP_ACCESSOR)
606 #undef STRUCT_MAP_ACCESSOR
607
608 #define STRING_ACCESSOR(name, str)                              \
609   inline Handle<String> name() {                                \
610     return Handle<String>(bit_cast<String**>(                   \
611         &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
612   }
613   INTERNALIZED_STRING_LIST(STRING_ACCESSOR)
614 #undef STRING_ACCESSOR
615
616 #define SYMBOL_ACCESSOR(name)                                   \
617   inline Handle<Symbol> name() {                                \
618     return Handle<Symbol>(bit_cast<Symbol**>(                   \
619         &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
620   }
621   PRIVATE_SYMBOL_LIST(SYMBOL_ACCESSOR)
622 #undef SYMBOL_ACCESSOR
623
624 #define SYMBOL_ACCESSOR(name, varname, description)             \
625   inline Handle<Symbol> name() {                                \
626     return Handle<Symbol>(bit_cast<Symbol**>(                   \
627         &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
628   }
629   PUBLIC_SYMBOL_LIST(SYMBOL_ACCESSOR)
630 #undef SYMBOL_ACCESSOR
631
632   inline void set_string_table(Handle<StringTable> table) {
633     isolate()->heap()->set_string_table(*table);
634   }
635
636   inline void set_weak_stack_trace_list(Handle<WeakFixedArray> list) {
637     isolate()->heap()->set_weak_stack_trace_list(*list);
638   }
639
640   Handle<String> hidden_string() {
641     return Handle<String>(&isolate()->heap()->hidden_string_);
642   }
643
644   // Allocates a new SharedFunctionInfo object.
645   Handle<SharedFunctionInfo> NewSharedFunctionInfo(
646       Handle<String> name, int number_of_literals, FunctionKind kind,
647       Handle<Code> code, Handle<ScopeInfo> scope_info,
648       Handle<TypeFeedbackVector> feedback_vector);
649   Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name,
650                                                    MaybeHandle<Code> code);
651
652   // Allocate a new type feedback vector
653   template <typename Spec>
654   Handle<TypeFeedbackVector> NewTypeFeedbackVector(const Spec* spec);
655
656   // Allocates a new JSMessageObject object.
657   Handle<JSMessageObject> NewJSMessageObject(MessageTemplate::Template message,
658                                              Handle<Object> argument,
659                                              int start_position,
660                                              int end_position,
661                                              Handle<Object> script,
662                                              Handle<Object> stack_frames);
663
664   Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
665
666   // Return a map for given number of properties using the map cache in the
667   // native context.
668   Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
669                                         int number_of_properties,
670                                         bool is_strong,
671                                         bool* is_result_from_cache);
672
673   // Creates a new FixedArray that holds the data associated with the
674   // atom regexp and stores it in the regexp.
675   void SetRegExpAtomData(Handle<JSRegExp> regexp,
676                          JSRegExp::Type type,
677                          Handle<String> source,
678                          JSRegExp::Flags flags,
679                          Handle<Object> match_pattern);
680
681   // Creates a new FixedArray that holds the data associated with the
682   // irregexp regexp and stores it in the regexp.
683   void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
684                              JSRegExp::Type type,
685                              Handle<String> source,
686                              JSRegExp::Flags flags,
687                              int capture_count);
688
689   // Returns the value for a known global constant (a property of the global
690   // object which is neither configurable nor writable) like 'undefined'.
691   // Returns a null handle when the given name is unknown.
692   Handle<Object> GlobalConstantFor(Handle<Name> name);
693
694   // Converts the given boolean condition to JavaScript boolean value.
695   Handle<Object> ToBoolean(bool value);
696
697  private:
698   Isolate* isolate() { return reinterpret_cast<Isolate*>(this); }
699
700   // Creates a heap object based on the map. The fields of the heap object are
701   // not initialized by New<>() functions. It's the responsibility of the caller
702   // to do that.
703   template<typename T>
704   Handle<T> New(Handle<Map> map, AllocationSpace space);
705
706   template<typename T>
707   Handle<T> New(Handle<Map> map,
708                 AllocationSpace space,
709                 Handle<AllocationSite> allocation_site);
710
711   // Creates a code object that is not yet fully initialized yet.
712   inline Handle<Code> NewCodeRaw(int object_size, bool immovable);
713
714   // Attempt to find the number in a small cache.  If we finds it, return
715   // the string representation of the number.  Otherwise return undefined.
716   Handle<Object> GetNumberStringCache(Handle<Object> number);
717
718   // Update the cache with a new number-string pair.
719   void SetNumberStringCache(Handle<Object> number, Handle<String> string);
720
721   // Initializes a function with a shared part and prototype.
722   // Note: this code was factored out of NewFunction such that other parts of
723   // the VM could use it. Specifically, a function that creates instances of
724   // type JS_FUNCTION_TYPE benefit from the use of this function.
725   inline void InitializeFunction(Handle<JSFunction> function,
726                                  Handle<SharedFunctionInfo> info,
727                                  Handle<Context> context);
728
729   // Creates a function initialized with a shared part.
730   Handle<JSFunction> NewFunction(Handle<Map> map,
731                                  Handle<SharedFunctionInfo> info,
732                                  Handle<Context> context,
733                                  PretenureFlag pretenure = TENURED);
734
735   Handle<JSFunction> NewFunction(Handle<Map> map,
736                                  Handle<String> name,
737                                  MaybeHandle<Code> maybe_code);
738
739   // Reinitialize a JSProxy into an (empty) JS object of respective type and
740   // size, but keeping the original prototype.  The receiver must have at least
741   // the size of the new object.  The object is reinitialized and behaves as an
742   // object that has been freshly allocated.
743   void ReinitializeJSProxy(Handle<JSProxy> proxy, InstanceType type, int size);
744 };
745
746 } }  // namespace v8::internal
747
748 #endif  // V8_FACTORY_H_