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