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