blob: 24a66478a3b35730a0bed35d6b867391a9b48040 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_FACTORY_H_
6#define V8_FACTORY_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/isolate.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00009
10namespace v8 {
11namespace internal {
12
Emily Bernierd0a1eb72015-03-24 16:35:39 -040013class FeedbackVectorSpec;
Steve Blocka7e24c12009-10-30 11:49:00 +000014
Emily Bernierd0a1eb72015-03-24 16:35:39 -040015// Interface for handle based allocation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016class Factory FINAL {
Steve Blocka7e24c12009-10-30 11:49:00 +000017 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018 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.
Steve Block44f0eee2011-05-26 01:26:41 +010024 Handle<FixedArray> NewFixedArray(
Steve Blocka7e24c12009-10-30 11:49:00 +000025 int size,
26 PretenureFlag pretenure = NOT_TENURED);
27
28 // Allocate a new fixed array with non-existing entries (the hole).
Steve Block44f0eee2011-05-26 01:26:41 +010029 Handle<FixedArray> NewFixedArrayWithHoles(
Steve Block6ded16b2010-05-10 14:33:55 +010030 int size,
31 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +000032
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033 // Allocates an uninitialized fixed array. It must be filled by the caller.
34 Handle<FixedArray> NewUninitializedFixedArray(int size);
35
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000036 // Allocate a new uninitialized fixed double array.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037 // 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(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000040 int size,
41 PretenureFlag pretenure = NOT_TENURED);
42
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043 // Allocate a new fixed double array with hole values.
44 Handle<FixedArrayBase> NewFixedDoubleArrayWithHoles(
45 int size,
46 PretenureFlag pretenure = NOT_TENURED);
Ben Murdochc7cc0282012-03-05 14:35:55 +000047
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048 Handle<ConstantPoolArray> NewConstantPoolArray(
49 const ConstantPoolArray::NumberOfEntries& small);
Steve Blocka7e24c12009-10-30 11:49:00 +000050
Ben Murdochb8a8cc12014-11-26 15:28:44 +000051 Handle<ConstantPoolArray> NewExtendedConstantPoolArray(
52 const ConstantPoolArray::NumberOfEntries& small,
53 const ConstantPoolArray::NumberOfEntries& extended);
Steve Blocka7e24c12009-10-30 11:49:00 +000054
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055 Handle<OrderedHashSet> NewOrderedHashSet();
56 Handle<OrderedHashMap> NewOrderedHashMap();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010057
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058 // Create a new boxed value.
59 Handle<Box> NewBox(Handle<Object> value);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000060
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061 // Create a pre-tenured empty AccessorPair.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010062 Handle<AccessorPair> NewAccessorPair();
63
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064 // Create an empty TypeFeedbackInfo.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010065 Handle<TypeFeedbackInfo> NewTypeFeedbackInfo();
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +010066
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067 // 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));
Steve Blocka7e24c12009-10-30 11:49:00 +000072 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073 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);
Steve Blocka7e24c12009-10-30 11:49:00 +000082
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 //
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089 // 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
Steve Blocka7e24c12009-10-30 11:49:00 +000091 // based on the encoding of the string buffer that the string is
92 // initialized from.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000093 // - ...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.
Steve Blocka7e24c12009-10-30 11:49:00 +000096 // - ...FromUtf8 initializes the string from a buffer that is UTF-8
Ben Murdochb8a8cc12014-11-26 15:28:44 +000097 // 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.
Steve Blocka7e24c12009-10-30 11:49:00 +0000102 //
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000103 // 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,
Steve Blocka7e24c12009-10-30 11:49:00 +0000106 PretenureFlag pretenure = NOT_TENURED);
107
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000108 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
Steve Blocka7e24c12009-10-30 11:49:00 +0000146 // UTF8 strings are pretenured when used for regexp literal patterns and
147 // flags in the parser.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000148 MUST_USE_RESULT MaybeHandle<String> NewStringFromUtf8(
Steve Blocka7e24c12009-10-30 11:49:00 +0000149 Vector<const char> str,
150 PretenureFlag pretenure = NOT_TENURED);
151
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000152 MUST_USE_RESULT MaybeHandle<String> NewStringFromTwoByte(
Leon Clarkeac952652010-07-15 11:15:24 +0100153 Vector<const uc16> str,
Steve Blocka7e24c12009-10-30 11:49:00 +0000154 PretenureFlag pretenure = NOT_TENURED);
155
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000156 // 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
Leon Clarkeac952652010-07-15 11:15:24 +0100183 // characters of the string are uninitialized. Currently used in regexp code
184 // only, where they are pretenured.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000185 MUST_USE_RESULT MaybeHandle<SeqOneByteString> NewRawOneByteString(
Leon Clarkeac952652010-07-15 11:15:24 +0100186 int length,
187 PretenureFlag pretenure = NOT_TENURED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000188 MUST_USE_RESULT MaybeHandle<SeqTwoByteString> NewRawTwoByteString(
Steve Blocka7e24c12009-10-30 11:49:00 +0000189 int length,
190 PretenureFlag pretenure = NOT_TENURED);
191
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000192 // 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
Steve Blocka7e24c12009-10-30 11:49:00 +0000196 // Create a new cons string object which consists of a pair of strings.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000197 MUST_USE_RESULT MaybeHandle<String> NewConsString(Handle<String> left,
198 Handle<String> right);
Steve Blocka7e24c12009-10-30 11:49:00 +0000199
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000200 // 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000205 // 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
Steve Blocka7e24c12009-10-30 11:49:00 +0000211 // Creates a new external String object. There are two String encodings
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000212 // in the system: one-byte and two-byte. Unlike other String types, it does
Steve Blocka7e24c12009-10-30 11:49:00 +0000213 // not make sense to have a UTF-8 factory function for external strings,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000214 // 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(
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100219 const ExternalTwoByteString::Resource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +0000220
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000221 // Create a symbol.
222 Handle<Symbol> NewSymbol();
223 Handle<Symbol> NewPrivateSymbol();
224 Handle<Symbol> NewPrivateOwnSymbol();
225
Steve Blocka7e24c12009-10-30 11:49:00 +0000226 // Create a global (but otherwise uninitialized) context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000227 Handle<Context> NewNativeContext();
228
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400229 // Create a script context.
230 Handle<Context> NewScriptContext(Handle<JSFunction> function,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000231 Handle<ScopeInfo> scope_info);
232
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400233 // Create an empty script context table.
234 Handle<ScriptContextTable> NewScriptContextTable();
235
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000236 // Create a module context.
237 Handle<Context> NewModuleContext(Handle<ScopeInfo> scope_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000238
239 // Create a function context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000240 Handle<Context> NewFunctionContext(int length, Handle<JSFunction> function);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000241
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);
Steve Blocka7e24c12009-10-30 11:49:00 +0000247
248 // Create a 'with' context.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000249 Handle<Context> NewWithContext(Handle<JSFunction> function,
250 Handle<Context> previous,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000251 Handle<JSReceiver> extension);
Steve Blocka7e24c12009-10-30 11:49:00 +0000252
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000253 // Create a block context.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000254 Handle<Context> NewBlockContext(Handle<JSFunction> function,
255 Handle<Context> previous,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100256 Handle<ScopeInfo> scope_info);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000257
Steve Blocka7e24c12009-10-30 11:49:00 +0000258 // Allocate a new struct. The struct is pretenured (allocated directly in
259 // the old generation).
Steve Block44f0eee2011-05-26 01:26:41 +0100260 Handle<Struct> NewStruct(InstanceType type);
Steve Blocka7e24c12009-10-30 11:49:00 +0000261
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000262 Handle<CodeCache> NewCodeCache();
263
264 Handle<AliasedArgumentsEntry> NewAliasedArgumentsEntry(
265 int aliased_context_slot);
266
267 Handle<DeclaredAccessorDescriptor> NewDeclaredAccessorDescriptor();
268
269 Handle<DeclaredAccessorInfo> NewDeclaredAccessorInfo();
270
271 Handle<ExecutableAccessorInfo> NewExecutableAccessorInfo();
Steve Blocka7e24c12009-10-30 11:49:00 +0000272
Steve Block44f0eee2011-05-26 01:26:41 +0100273 Handle<Script> NewScript(Handle<String> source);
Steve Blocka7e24c12009-10-30 11:49:00 +0000274
Ben Murdoch257744e2011-11-30 15:57:28 +0000275 // Foreign objects are pretenured when allocated by the bootstrapper.
276 Handle<Foreign> NewForeign(Address addr,
277 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000278
Ben Murdoch257744e2011-11-30 15:57:28 +0000279 // Allocate a new foreign object. The foreign is pretenured (allocated
280 // directly in the old generation).
281 Handle<Foreign> NewForeign(const AccessorDescriptor* foreign);
Steve Blocka7e24c12009-10-30 11:49:00 +0000282
Steve Block44f0eee2011-05-26 01:26:41 +0100283 Handle<ByteArray> NewByteArray(int length,
284 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000285
Steve Block44f0eee2011-05-26 01:26:41 +0100286 Handle<ExternalArray> NewExternalArray(
Steve Block3ce2e202009-11-05 08:53:23 +0000287 int length,
288 ExternalArrayType array_type,
289 void* external_pointer,
290 PretenureFlag pretenure = NOT_TENURED);
291
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000292 Handle<FixedTypedArrayBase> NewFixedTypedArray(
293 int length,
294 ExternalArrayType array_type,
295 PretenureFlag pretenure = NOT_TENURED);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100296
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000297 Handle<Cell> NewCell(Handle<Object> value);
298
299 Handle<PropertyCell> NewPropertyCellWithHole();
300
301 Handle<PropertyCell> NewPropertyCell(Handle<Object> value);
302
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400303 Handle<WeakCell> NewWeakCell(Handle<HeapObject> value);
304
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000305 // 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);
Steve Blocka7e24c12009-10-30 11:49:00 +0000316
Steve Block44f0eee2011-05-26 01:26:41 +0100317 Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
Steve Blocka7e24c12009-10-30 11:49:00 +0000318
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000319 Handle<JSObject> CopyJSObject(Handle<JSObject> object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000320
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000321 Handle<JSObject> CopyJSObjectWithAllocationSite(Handle<JSObject> object,
322 Handle<AllocationSite> site);
Steve Blocka7e24c12009-10-30 11:49:00 +0000323
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000324 Handle<FixedArray> CopyFixedArrayWithMap(Handle<FixedArray> array,
325 Handle<Map> map);
Steve Block1e0659c2011-05-24 12:43:12 +0100326
Steve Block44f0eee2011-05-26 01:26:41 +0100327 Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
Steve Blocka7e24c12009-10-30 11:49:00 +0000328
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000329 // 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
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100333 Handle<FixedDoubleArray> CopyFixedDoubleArray(
334 Handle<FixedDoubleArray> array);
335
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000336 Handle<ConstantPoolArray> CopyConstantPoolArray(
337 Handle<ConstantPoolArray> array);
338
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100339 // Numbers (e.g. literals) are pretenured by the parser.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000340 // The return value may be a smi or a heap number.
Steve Block44f0eee2011-05-26 01:26:41 +0100341 Handle<Object> NewNumber(double value,
342 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000343
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100344 Handle<Object> NewNumberFromInt(int32_t value,
345 PretenureFlag pretenure = NOT_TENURED);
346 Handle<Object> NewNumberFromUint(uint32_t value,
347 PretenureFlag pretenure = NOT_TENURED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000348 Handle<Object> NewNumberFromSize(size_t value,
349 PretenureFlag pretenure = NOT_TENURED) {
350 if (Smi::IsValid(static_cast<intptr_t>(value))) {
351 return Handle<Object>(Smi::FromIntptr(static_cast<intptr_t>(value)),
352 isolate());
353 }
354 return NewNumber(static_cast<double>(value), pretenure);
355 }
356 Handle<HeapNumber> NewHeapNumber(double value,
357 MutableMode mode = IMMUTABLE,
358 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000359
360 // These objects are used by the api to create env-independent data
361 // structures in the heap.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000362 inline Handle<JSObject> NewNeanderObject() {
363 return NewJSObjectFromMap(neander_map());
364 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000365
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000366 Handle<JSObject> NewArgumentsObject(Handle<JSFunction> callee, int length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000367
368 // JS objects are pretenured when allocated by the bootstrapper and
369 // runtime.
Steve Block44f0eee2011-05-26 01:26:41 +0100370 Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
371 PretenureFlag pretenure = NOT_TENURED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000372 // JSObject that should have a memento pointing to the allocation site.
373 Handle<JSObject> NewJSObjectWithMemento(Handle<JSFunction> constructor,
374 Handle<AllocationSite> site);
Steve Blocka7e24c12009-10-30 11:49:00 +0000375
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000376 // Global objects are pretenured and initialized based on a constructor.
Steve Block44f0eee2011-05-26 01:26:41 +0100377 Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor);
Steve Blocka7e24c12009-10-30 11:49:00 +0000378
379 // JS objects are pretenured when allocated by the bootstrapper and
380 // runtime.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000381 Handle<JSObject> NewJSObjectFromMap(
382 Handle<Map> map,
383 PretenureFlag pretenure = NOT_TENURED,
384 bool allocate_properties = true,
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);
Steve Blocka7e24c12009-10-30 11:49:00 +0000390
391 // JS arrays are pretenured when allocated by the parser.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000392
393 // Create a JSArray with no elements.
394 Handle<JSArray> NewJSArray(
395 ElementsKind elements_kind,
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 ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS,
403 PretenureFlag pretenure = NOT_TENURED);
404
405 Handle<JSArray> NewJSArray(
406 int capacity,
407 ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
408 PretenureFlag pretenure = NOT_TENURED) {
409 if (capacity != 0) {
410 elements_kind = GetHoleyElementsKind(elements_kind);
411 }
412 return NewJSArray(elements_kind, 0, capacity,
413 INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE, pretenure);
414 }
415
416 // Create a JSArray with the given elements.
417 Handle<JSArray> NewJSArrayWithElements(
418 Handle<FixedArrayBase> elements,
419 ElementsKind elements_kind,
420 int length,
421 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000422
Steve Block44f0eee2011-05-26 01:26:41 +0100423 Handle<JSArray> NewJSArrayWithElements(
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100424 Handle<FixedArrayBase> elements,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000425 ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
426 PretenureFlag pretenure = NOT_TENURED) {
427 return NewJSArrayWithElements(
428 elements, elements_kind, elements->length(), pretenure);
429 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000430
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000431 void NewJSArrayStorage(
432 Handle<JSArray> array,
433 int length,
434 int capacity,
435 ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100436
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000437 Handle<JSGeneratorObject> NewJSGeneratorObject(Handle<JSFunction> function);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100438
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000439 Handle<JSArrayBuffer> NewJSArrayBuffer();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100440
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000441 Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type);
442
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400443 // Creates a new JSTypedArray with the specified buffer.
444 Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type,
445 Handle<JSArrayBuffer> buffer,
446 size_t byte_offset, size_t length);
447
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000448 Handle<JSDataView> NewJSDataView();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400449 Handle<JSDataView> NewJSDataView(Handle<JSArrayBuffer> buffer,
450 size_t byte_offset, size_t byte_length);
451
452 // TODO(aandrey): Maybe these should take table, index and kind arguments.
453 Handle<JSMapIterator> NewJSMapIterator();
454 Handle<JSSetIterator> NewJSSetIterator();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000455
456 // Allocates a Harmony proxy.
Ben Murdoch257744e2011-11-30 15:57:28 +0000457 Handle<JSProxy> NewJSProxy(Handle<Object> handler, Handle<Object> prototype);
458
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000459 // Allocates a Harmony function proxy.
460 Handle<JSProxy> NewJSFunctionProxy(Handle<Object> handler,
461 Handle<Object> call_trap,
462 Handle<Object> construct_trap,
463 Handle<Object> prototype);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000464
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000465 // Reinitialize an JSGlobalProxy based on a constructor. The object
466 // must have the same size as objects allocated using the
467 // constructor. The object is reinitialized and behaves as an
468 // object that has been freshly allocated using the constructor.
469 void ReinitializeJSGlobalProxy(Handle<JSGlobalProxy> global,
470 Handle<JSFunction> constructor);
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);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100475
Steve Block44f0eee2011-05-26 01:26:41 +0100476 Handle<JSFunction> NewFunction(Handle<String> name,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000477 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);
Steve Block6ded16b2010-05-10 14:33:55 +0100483
Steve Block44f0eee2011-05-26 01:26:41 +0100484 Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
Steve Block6ded16b2010-05-10 14:33:55 +0100485 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000486 Handle<Context> context,
487 PretenureFlag pretenure = TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000488
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400489 Handle<JSFunction> NewFunction(Handle<String> name, Handle<Code> code,
490 Handle<Object> prototype, InstanceType type,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000491 int instance_size,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400492 bool read_only_prototype = false,
493 bool install_constructor = false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000494 Handle<JSFunction> NewFunction(Handle<String> name,
495 Handle<Code> code,
496 InstanceType type,
497 int instance_size);
498
499 // Create a serialized scope info.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100500 Handle<ScopeInfo> NewScopeInfo(int length);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000501
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000502 // 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.
Steve Block44f0eee2011-05-26 01:26:41 +0100508 Handle<Code> NewCode(const CodeDesc& desc,
509 Code::Flags flags,
510 Handle<Object> self_reference,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000511 bool immovable = false,
512 bool crankshafted = false,
513 int prologue_offset = Code::kPrologueOffsetNotSet,
514 bool is_debug = false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000515
Steve Block44f0eee2011-05-26 01:26:41 +0100516 Handle<Code> CopyCode(Handle<Code> code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000517
Steve Block44f0eee2011-05-26 01:26:41 +0100518 Handle<Code> CopyCode(Handle<Code> code, Vector<byte> reloc_info);
Steve Block6ded16b2010-05-10 14:33:55 +0100519
Steve Blocka7e24c12009-10-30 11:49:00 +0000520 // Interface for creating error objects.
521
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000522 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);
Steve Blocka7e24c12009-10-30 11:49:00 +0000531
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000532 MaybeHandle<Object> NewTypeError(const char* message,
533 Vector<Handle<Object> > args);
534 MaybeHandle<Object> NewTypeError(Handle<String> message);
Steve Blocka7e24c12009-10-30 11:49:00 +0000535
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000536 MaybeHandle<Object> NewRangeError(const char* message,
537 Vector<Handle<Object> > args);
538 MaybeHandle<Object> NewRangeError(Handle<String> message);
Steve Blocka7e24c12009-10-30 11:49:00 +0000539
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000540 MaybeHandle<Object> NewInvalidStringLengthError() {
541 return NewRangeError("invalid_string_length",
542 HandleVector<Object>(NULL, 0));
543 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000544
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000545 MaybeHandle<Object> NewSyntaxError(const char* message, Handle<JSArray> args);
546 MaybeHandle<Object> NewSyntaxError(Handle<String> message);
Steve Blocka7e24c12009-10-30 11:49:00 +0000547
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000548 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);
Steve Blocka7e24c12009-10-30 11:49:00 +0000553
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000554 MaybeHandle<Object> NewEvalError(const char* message,
555 Vector<Handle<Object> > args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000556
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000557 Handle<String> NumberToString(Handle<Object> number,
558 bool check_number_string_cache = true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000559
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000560 Handle<String> Uint32ToString(uint32_t value) {
561 return NumberToString(NewNumberFromUint(value));
562 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000563
564 enum ApiInstanceType {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000565 JavaScriptObjectType,
566 GlobalObjectType,
567 GlobalProxyType
Steve Blocka7e24c12009-10-30 11:49:00 +0000568 };
569
Steve Block44f0eee2011-05-26 01:26:41 +0100570 Handle<JSFunction> CreateApiFunction(
Steve Blocka7e24c12009-10-30 11:49:00 +0000571 Handle<FunctionTemplateInfo> data,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000572 Handle<Object> prototype,
573 ApiInstanceType type = JavaScriptObjectType);
Steve Blocka7e24c12009-10-30 11:49:00 +0000574
Steve Block44f0eee2011-05-26 01:26:41 +0100575 Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
Steve Blocka7e24c12009-10-30 11:49:00 +0000576
577 // Installs interceptors on the instance. 'desc' is a function template,
578 // and instance is an object instance created by the function of this
579 // function template.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000580 MUST_USE_RESULT MaybeHandle<FunctionTemplateInfo> ConfigureInstance(
581 Handle<FunctionTemplateInfo> desc, Handle<JSObject> instance);
Steve Blocka7e24c12009-10-30 11:49:00 +0000582
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000583#define ROOT_ACCESSOR(type, name, camel_name) \
584 inline Handle<type> name() { \
585 return Handle<type>(bit_cast<type**>( \
586 &isolate()->heap()->roots_[Heap::k##camel_name##RootIndex])); \
Steve Blocka7e24c12009-10-30 11:49:00 +0000587 }
588 ROOT_LIST(ROOT_ACCESSOR)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000589#undef ROOT_ACCESSOR
Steve Blocka7e24c12009-10-30 11:49:00 +0000590
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000591#define STRUCT_MAP_ACCESSOR(NAME, Name, name) \
592 inline Handle<Map> name##_map() { \
593 return Handle<Map>(bit_cast<Map**>( \
594 &isolate()->heap()->roots_[Heap::k##Name##MapRootIndex])); \
Steve Blocka7e24c12009-10-30 11:49:00 +0000595 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000596 STRUCT_LIST(STRUCT_MAP_ACCESSOR)
597#undef STRUCT_MAP_ACCESSOR
Steve Blocka7e24c12009-10-30 11:49:00 +0000598
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000599#define STRING_ACCESSOR(name, str) \
600 inline Handle<String> name() { \
601 return Handle<String>(bit_cast<String**>( \
602 &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
603 }
604 INTERNALIZED_STRING_LIST(STRING_ACCESSOR)
605#undef STRING_ACCESSOR
606
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400607#define SYMBOL_ACCESSOR(name) \
608 inline Handle<Symbol> name() { \
609 return Handle<Symbol>(bit_cast<Symbol**>( \
610 &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
611 }
612 PRIVATE_SYMBOL_LIST(SYMBOL_ACCESSOR)
613#undef SYMBOL_ACCESSOR
614
615#define SYMBOL_ACCESSOR(name, varname, description) \
616 inline Handle<Symbol> name() { \
617 return Handle<Symbol>(bit_cast<Symbol**>( \
618 &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
619 }
620 PUBLIC_SYMBOL_LIST(SYMBOL_ACCESSOR)
621#undef SYMBOL_ACCESSOR
622
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000623 inline void set_string_table(Handle<StringTable> table) {
624 isolate()->heap()->set_string_table(*table);
Steve Blocka7e24c12009-10-30 11:49:00 +0000625 }
626
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000627 Handle<String> hidden_string() {
628 return Handle<String>(&isolate()->heap()->hidden_string_);
629 }
630
631 // Allocates a new SharedFunctionInfo object.
Steve Block44f0eee2011-05-26 01:26:41 +0100632 Handle<SharedFunctionInfo> NewSharedFunctionInfo(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000633 Handle<String> name, int number_of_literals, FunctionKind kind,
634 Handle<Code> code, Handle<ScopeInfo> scope_info,
635 Handle<TypeFeedbackVector> feedback_vector);
636 Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name,
637 MaybeHandle<Code> code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000638
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000639 // Allocate a new type feedback vector
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400640 Handle<TypeFeedbackVector> NewTypeFeedbackVector(
641 const FeedbackVectorSpec& spec);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000642
643 // Allocates a new JSMessageObject object.
Steve Block44f0eee2011-05-26 01:26:41 +0100644 Handle<JSMessageObject> NewJSMessageObject(
Steve Block1e0659c2011-05-24 12:43:12 +0100645 Handle<String> type,
646 Handle<JSArray> arguments,
647 int start_position,
648 int end_position,
649 Handle<Object> script,
Steve Block1e0659c2011-05-24 12:43:12 +0100650 Handle<Object> stack_frames);
651
Steve Block44f0eee2011-05-26 01:26:41 +0100652 Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
Steve Blocka7e24c12009-10-30 11:49:00 +0000653
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400654 // Return a map for given number of properties using the map cache in the
655 // native context.
Steve Block44f0eee2011-05-26 01:26:41 +0100656 Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400657 int number_of_properties,
658 bool* is_result_from_cache);
Steve Blocka7e24c12009-10-30 11:49:00 +0000659
660 // Creates a new FixedArray that holds the data associated with the
661 // atom regexp and stores it in the regexp.
Steve Block44f0eee2011-05-26 01:26:41 +0100662 void SetRegExpAtomData(Handle<JSRegExp> regexp,
663 JSRegExp::Type type,
664 Handle<String> source,
665 JSRegExp::Flags flags,
666 Handle<Object> match_pattern);
Steve Blocka7e24c12009-10-30 11:49:00 +0000667
668 // Creates a new FixedArray that holds the data associated with the
669 // irregexp regexp and stores it in the regexp.
Steve Block44f0eee2011-05-26 01:26:41 +0100670 void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
671 JSRegExp::Type type,
672 Handle<String> source,
673 JSRegExp::Flags flags,
674 int capture_count);
Steve Blocka7e24c12009-10-30 11:49:00 +0000675
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100676 // Returns the value for a known global constant (a property of the global
677 // object which is neither configurable nor writable) like 'undefined'.
678 // Returns a null handle when the given name is unknown.
679 Handle<Object> GlobalConstantFor(Handle<String> name);
680
681 // Converts the given boolean condition to JavaScript boolean value.
682 Handle<Object> ToBoolean(bool value);
683
Steve Blocka7e24c12009-10-30 11:49:00 +0000684 private:
Steve Block44f0eee2011-05-26 01:26:41 +0100685 Isolate* isolate() { return reinterpret_cast<Isolate*>(this); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000686
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000687 // Creates a heap object based on the map. The fields of the heap object are
688 // not initialized by New<>() functions. It's the responsibility of the caller
689 // to do that.
690 template<typename T>
691 Handle<T> New(Handle<Map> map, AllocationSpace space);
Steve Block6ded16b2010-05-10 14:33:55 +0100692
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000693 template<typename T>
694 Handle<T> New(Handle<Map> map,
695 AllocationSpace space,
696 Handle<AllocationSite> allocation_site);
Steve Block44f0eee2011-05-26 01:26:41 +0100697
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000698 // Creates a code object that is not yet fully initialized yet.
699 inline Handle<Code> NewCodeRaw(int object_size, bool immovable);
Steve Blocka7e24c12009-10-30 11:49:00 +0000700
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000701 // Attempt to find the number in a small cache. If we finds it, return
702 // the string representation of the number. Otherwise return undefined.
703 Handle<Object> GetNumberStringCache(Handle<Object> number);
704
705 // Update the cache with a new number-string pair.
706 void SetNumberStringCache(Handle<Object> number, Handle<String> string);
707
708 // Initializes a function with a shared part and prototype.
709 // Note: this code was factored out of NewFunction such that other parts of
710 // the VM could use it. Specifically, a function that creates instances of
711 // type JS_FUNCTION_TYPE benefit from the use of this function.
712 inline void InitializeFunction(Handle<JSFunction> function,
713 Handle<SharedFunctionInfo> info,
714 Handle<Context> context);
715
716 // Creates a function initialized with a shared part.
717 Handle<JSFunction> NewFunction(Handle<Map> map,
718 Handle<SharedFunctionInfo> info,
719 Handle<Context> context,
720 PretenureFlag pretenure = TENURED);
721
722 Handle<JSFunction> NewFunction(Handle<Map> map,
723 Handle<String> name,
724 MaybeHandle<Code> maybe_code);
725
726 // Reinitialize a JSProxy into an (empty) JS object of respective type and
727 // size, but keeping the original prototype. The receiver must have at least
728 // the size of the new object. The object is reinitialized and behaves as an
729 // object that has been freshly allocated.
730 void ReinitializeJSProxy(Handle<JSProxy> proxy, InstanceType type, int size);
731};
Steve Blocka7e24c12009-10-30 11:49:00 +0000732
733} } // namespace v8::internal
734
735#endif // V8_FACTORY_H_