Line data Source code
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/feedback-vector.h"
9 : #include "src/globals.h"
10 : #include "src/isolate.h"
11 : #include "src/messages.h"
12 : #include "src/objects/descriptor-array.h"
13 : #include "src/objects/dictionary.h"
14 : #include "src/objects/scope-info.h"
15 :
16 : namespace v8 {
17 : namespace internal {
18 :
19 : class BoilerplateDescription;
20 : class ConstantElementsPair;
21 :
22 : enum FunctionMode {
23 : // With prototype.
24 : FUNCTION_WITH_WRITEABLE_PROTOTYPE,
25 : FUNCTION_WITH_READONLY_PROTOTYPE,
26 : // Without prototype.
27 : FUNCTION_WITHOUT_PROTOTYPE
28 : };
29 :
30 : // Interface for handle based allocation.
31 : class V8_EXPORT_PRIVATE Factory final {
32 : public:
33 : Handle<Oddball> NewOddball(Handle<Map> map, const char* to_string,
34 : Handle<Object> to_number, const char* type_of,
35 : byte kind);
36 :
37 : // Allocates a fixed array initialized with undefined values.
38 : Handle<FixedArray> NewFixedArray(int size,
39 : PretenureFlag pretenure = NOT_TENURED);
40 : // Tries allocating a fixed array initialized with undefined values.
41 : // In case of an allocation failure (OOM) an empty handle is returned.
42 : // The caller has to manually signal an
43 : // v8::internal::Heap::FatalProcessOutOfMemory typically by calling
44 : // NewFixedArray as a fallback.
45 : MUST_USE_RESULT
46 : MaybeHandle<FixedArray> TryNewFixedArray(
47 : int size, PretenureFlag pretenure = NOT_TENURED);
48 :
49 : // Allocate a new fixed array with non-existing entries (the hole).
50 : Handle<FixedArray> NewFixedArrayWithHoles(
51 : int size,
52 : PretenureFlag pretenure = NOT_TENURED);
53 :
54 : // Allocates an uninitialized fixed array. It must be filled by the caller.
55 : Handle<FixedArray> NewUninitializedFixedArray(int size);
56 :
57 : // Allocates a fixed array for name-value pairs of boilerplate properties and
58 : // calculates the number of properties we need to store in the backing store.
59 : Handle<BoilerplateDescription> NewBoilerplateDescription(int boilerplate,
60 : int all_properties,
61 : int index_keys,
62 : bool has_seen_proto);
63 :
64 : // Allocate a new uninitialized fixed double array.
65 : // The function returns a pre-allocated empty fixed array for capacity = 0,
66 : // so the return type must be the general fixed array class.
67 : Handle<FixedArrayBase> NewFixedDoubleArray(
68 : int size,
69 : PretenureFlag pretenure = NOT_TENURED);
70 :
71 : // Allocate a new fixed double array with hole values.
72 : Handle<FixedArrayBase> NewFixedDoubleArrayWithHoles(
73 : int size,
74 : PretenureFlag pretenure = NOT_TENURED);
75 :
76 : Handle<FrameArray> NewFrameArray(int number_of_frames,
77 : PretenureFlag pretenure = NOT_TENURED);
78 :
79 : Handle<OrderedHashSet> NewOrderedHashSet();
80 : Handle<OrderedHashMap> NewOrderedHashMap();
81 :
82 : // Create a new PrototypeInfo struct.
83 : Handle<PrototypeInfo> NewPrototypeInfo();
84 :
85 : // Create a new Tuple2 struct.
86 : Handle<Tuple2> NewTuple2(Handle<Object> value1, Handle<Object> value2);
87 :
88 : // Create a new Tuple3 struct.
89 : Handle<Tuple3> NewTuple3(Handle<Object> value1, Handle<Object> value2,
90 : Handle<Object> value3);
91 :
92 : // Create a new ContextExtension struct.
93 : Handle<ContextExtension> NewContextExtension(Handle<ScopeInfo> scope_info,
94 : Handle<Object> extension);
95 :
96 : // Create a new ConstantElementsPair struct.
97 : Handle<ConstantElementsPair> NewConstantElementsPair(
98 : ElementsKind elements_kind, Handle<FixedArrayBase> constant_values);
99 :
100 : // Create a pre-tenured empty AccessorPair.
101 : Handle<AccessorPair> NewAccessorPair();
102 :
103 : // Create an empty TypeFeedbackInfo.
104 : Handle<TypeFeedbackInfo> NewTypeFeedbackInfo();
105 :
106 : // Finds the internalized copy for string in the string table.
107 : // If not found, a new string is added to the table and returned.
108 : Handle<String> InternalizeUtf8String(Vector<const char> str);
109 8265487 : Handle<String> InternalizeUtf8String(const char* str) {
110 8265487 : return InternalizeUtf8String(CStrVector(str));
111 : }
112 :
113 : Handle<String> InternalizeOneByteString(Vector<const uint8_t> str);
114 : Handle<String> InternalizeOneByteString(
115 : Handle<SeqOneByteString>, int from, int length);
116 :
117 : Handle<String> InternalizeTwoByteString(Vector<const uc16> str);
118 :
119 : template<class StringTableKey>
120 : Handle<String> InternalizeStringWithKey(StringTableKey* key);
121 :
122 : // Internalized strings are created in the old generation (data space).
123 29989523 : Handle<String> InternalizeString(Handle<String> string) {
124 29989523 : if (string->IsInternalizedString()) return string;
125 233426 : return StringTable::LookupString(isolate(), string);
126 : }
127 :
128 296222910 : Handle<Name> InternalizeName(Handle<Name> name) {
129 296222910 : if (name->IsUniqueName()) return name;
130 23147936 : return StringTable::LookupString(isolate(), Handle<String>::cast(name));
131 : }
132 :
133 : // String creation functions. Most of the string creation functions take
134 : // a Heap::PretenureFlag argument to optionally request that they be
135 : // allocated in the old generation. The pretenure flag defaults to
136 : // DONT_TENURE.
137 : //
138 : // Creates a new String object. There are two String encodings: one-byte and
139 : // two-byte. One should choose between the three string factory functions
140 : // based on the encoding of the string buffer that the string is
141 : // initialized from.
142 : // - ...FromOneByte initializes the string from a buffer that is Latin1
143 : // encoded (it does not check that the buffer is Latin1 encoded) and
144 : // the result will be Latin1 encoded.
145 : // - ...FromUtf8 initializes the string from a buffer that is UTF-8
146 : // encoded. If the characters are all ASCII characters, the result
147 : // will be Latin1 encoded, otherwise it will converted to two-byte.
148 : // - ...FromTwoByte initializes the string from a buffer that is two-byte
149 : // encoded. If the characters are all Latin1 characters, the result
150 : // will be converted to Latin1, otherwise it will be left as two-byte.
151 : //
152 : // One-byte strings are pretenured when used as keys in the SourceCodeCache.
153 : MUST_USE_RESULT MaybeHandle<String> NewStringFromOneByte(
154 : Vector<const uint8_t> str, PretenureFlag pretenure = NOT_TENURED);
155 :
156 : template <size_t N>
157 150217 : inline Handle<String> NewStringFromStaticChars(
158 : const char (&str)[N], PretenureFlag pretenure = NOT_TENURED) {
159 : DCHECK(N == StrLength(str) + 1);
160 : return NewStringFromOneByte(STATIC_CHAR_VECTOR(str), pretenure)
161 300434 : .ToHandleChecked();
162 : }
163 :
164 53895680 : inline Handle<String> NewStringFromAsciiChecked(
165 : const char* str,
166 : PretenureFlag pretenure = NOT_TENURED) {
167 : return NewStringFromOneByte(
168 107791364 : OneByteVector(str), pretenure).ToHandleChecked();
169 : }
170 :
171 : // UTF8 strings are pretenured when used for regexp literal patterns and
172 : // flags in the parser.
173 : MUST_USE_RESULT MaybeHandle<String> NewStringFromUtf8(
174 : Vector<const char> str, PretenureFlag pretenure = NOT_TENURED);
175 :
176 : MUST_USE_RESULT MaybeHandle<String> NewStringFromUtf8SubString(
177 : Handle<SeqOneByteString> str, int begin, int end,
178 : PretenureFlag pretenure = NOT_TENURED);
179 :
180 : MUST_USE_RESULT MaybeHandle<String> NewStringFromTwoByte(
181 : Vector<const uc16> str, PretenureFlag pretenure = NOT_TENURED);
182 :
183 : MUST_USE_RESULT MaybeHandle<String> NewStringFromTwoByte(
184 : const ZoneVector<uc16>* str, PretenureFlag pretenure = NOT_TENURED);
185 :
186 : Handle<JSStringIterator> NewJSStringIterator(Handle<String> string);
187 :
188 : // Allocates an internalized string in old space based on the character
189 : // stream.
190 : Handle<String> NewInternalizedStringFromUtf8(Vector<const char> str,
191 : int chars, uint32_t hash_field);
192 :
193 : Handle<String> NewOneByteInternalizedString(Vector<const uint8_t> str,
194 : uint32_t hash_field);
195 :
196 : Handle<String> NewOneByteInternalizedSubString(
197 : Handle<SeqOneByteString> string, int offset, int length,
198 : uint32_t hash_field);
199 :
200 : Handle<String> NewTwoByteInternalizedString(Vector<const uc16> str,
201 : uint32_t hash_field);
202 :
203 : Handle<String> NewInternalizedStringImpl(Handle<String> string, int chars,
204 : uint32_t hash_field);
205 :
206 : // Compute the matching internalized string map for a string if possible.
207 : // Empty handle is returned if string is in new space or not flattened.
208 : MUST_USE_RESULT MaybeHandle<Map> InternalizedStringMapForString(
209 : Handle<String> string);
210 :
211 : // Creates an internalized copy of an external string. |string| must be
212 : // of type StringClass.
213 : template <class StringClass>
214 : Handle<StringClass> InternalizeExternalString(Handle<String> string);
215 :
216 : // Allocates and partially initializes an one-byte or two-byte String. The
217 : // characters of the string are uninitialized. Currently used in regexp code
218 : // only, where they are pretenured.
219 : MUST_USE_RESULT MaybeHandle<SeqOneByteString> NewRawOneByteString(
220 : int length,
221 : PretenureFlag pretenure = NOT_TENURED);
222 : MUST_USE_RESULT MaybeHandle<SeqTwoByteString> NewRawTwoByteString(
223 : int length,
224 : PretenureFlag pretenure = NOT_TENURED);
225 :
226 : // Creates a single character string where the character has given code.
227 : // A cache is used for Latin1 codes.
228 : Handle<String> LookupSingleCharacterStringFromCode(uint32_t code);
229 :
230 : // Create a new cons string object which consists of a pair of strings.
231 : MUST_USE_RESULT MaybeHandle<String> NewConsString(Handle<String> left,
232 : Handle<String> right);
233 :
234 : MUST_USE_RESULT Handle<String> NewConsString(Handle<String> left,
235 : Handle<String> right, int length,
236 : bool one_byte);
237 :
238 : // Create or lookup a single characters tring made up of a utf16 surrogate
239 : // pair.
240 : Handle<String> NewSurrogatePairString(uint16_t lead, uint16_t trail);
241 :
242 : // Create a new string object which holds a proper substring of a string.
243 : Handle<String> NewProperSubString(Handle<String> str,
244 : int begin,
245 : int end);
246 :
247 : // Create a new string object which holds a substring of a string.
248 6811752 : Handle<String> NewSubString(Handle<String> str, int begin, int end) {
249 9259515 : if (begin == 0 && end == str->length()) return str;
250 4771479 : return NewProperSubString(str, begin, end);
251 : }
252 :
253 : // Creates a new external String object. There are two String encodings
254 : // in the system: one-byte and two-byte. Unlike other String types, it does
255 : // not make sense to have a UTF-8 factory function for external strings,
256 : // because we cannot change the underlying buffer. Note that these strings
257 : // are backed by a string resource that resides outside the V8 heap.
258 : MUST_USE_RESULT MaybeHandle<String> NewExternalStringFromOneByte(
259 : const ExternalOneByteString::Resource* resource);
260 : MUST_USE_RESULT MaybeHandle<String> NewExternalStringFromTwoByte(
261 : const ExternalTwoByteString::Resource* resource);
262 : // Create a new external string object for one-byte encoded native script.
263 : // It does not cache the resource data pointer.
264 : Handle<ExternalOneByteString> NewNativeSourceString(
265 : const ExternalOneByteString::Resource* resource);
266 :
267 : // Create a symbol.
268 : Handle<Symbol> NewSymbol();
269 : Handle<Symbol> NewPrivateSymbol();
270 :
271 : // Create a promise.
272 : Handle<JSPromise> NewJSPromise();
273 :
274 : // Create a global (but otherwise uninitialized) context.
275 : Handle<Context> NewNativeContext();
276 :
277 : // Create a script context.
278 : Handle<Context> NewScriptContext(Handle<JSFunction> function,
279 : Handle<ScopeInfo> scope_info);
280 :
281 : // Create an empty script context table.
282 : Handle<ScriptContextTable> NewScriptContextTable();
283 :
284 : // Create a module context.
285 : Handle<Context> NewModuleContext(Handle<Module> module,
286 : Handle<JSFunction> function,
287 : Handle<ScopeInfo> scope_info);
288 :
289 : // Create a function or eval context.
290 : Handle<Context> NewFunctionContext(int length, Handle<JSFunction> function,
291 : ScopeType scope_type);
292 :
293 : // Create a catch context.
294 : Handle<Context> NewCatchContext(Handle<JSFunction> function,
295 : Handle<Context> previous,
296 : Handle<ScopeInfo> scope_info,
297 : Handle<String> name,
298 : Handle<Object> thrown_object);
299 :
300 : // Create a 'with' context.
301 : Handle<Context> NewWithContext(Handle<JSFunction> function,
302 : Handle<Context> previous,
303 : Handle<ScopeInfo> scope_info,
304 : Handle<JSReceiver> extension);
305 :
306 : Handle<Context> NewDebugEvaluateContext(Handle<Context> previous,
307 : Handle<ScopeInfo> scope_info,
308 : Handle<JSReceiver> extension,
309 : Handle<Context> wrapped,
310 : Handle<StringSet> whitelist);
311 :
312 : // Create a block context.
313 : Handle<Context> NewBlockContext(Handle<JSFunction> function,
314 : Handle<Context> previous,
315 : Handle<ScopeInfo> scope_info);
316 :
317 : // Allocate a new struct. The struct is pretenured (allocated directly in
318 : // the old generation).
319 : Handle<Struct> NewStruct(InstanceType type);
320 :
321 : Handle<AliasedArgumentsEntry> NewAliasedArgumentsEntry(
322 : int aliased_context_slot);
323 :
324 : Handle<AccessorInfo> NewAccessorInfo();
325 :
326 : Handle<Script> NewScript(Handle<String> source);
327 :
328 : Handle<BreakPointInfo> NewBreakPointInfo(int source_position);
329 : Handle<StackFrameInfo> NewStackFrameInfo();
330 : Handle<SourcePositionTableWithFrameCache>
331 : NewSourcePositionTableWithFrameCache(
332 : Handle<ByteArray> source_position_table,
333 : Handle<UnseededNumberDictionary> stack_frame_cache);
334 :
335 : // Foreign objects are pretenured when allocated by the bootstrapper.
336 : Handle<Foreign> NewForeign(Address addr,
337 : PretenureFlag pretenure = NOT_TENURED);
338 :
339 : // Allocate a new foreign object. The foreign is pretenured (allocated
340 : // directly in the old generation).
341 : Handle<Foreign> NewForeign(const AccessorDescriptor* foreign);
342 :
343 : Handle<ByteArray> NewByteArray(int length,
344 : PretenureFlag pretenure = NOT_TENURED);
345 :
346 : Handle<BytecodeArray> NewBytecodeArray(int length, const byte* raw_bytecodes,
347 : int frame_size, int parameter_count,
348 : Handle<FixedArray> constant_pool);
349 :
350 : Handle<FixedTypedArrayBase> NewFixedTypedArrayWithExternalPointer(
351 : int length, ExternalArrayType array_type, void* external_pointer,
352 : PretenureFlag pretenure = NOT_TENURED);
353 :
354 : Handle<FixedTypedArrayBase> NewFixedTypedArray(
355 : int length, ExternalArrayType array_type, bool initialize,
356 : PretenureFlag pretenure = NOT_TENURED);
357 :
358 : Handle<Cell> NewCell(Handle<Object> value);
359 :
360 : Handle<PropertyCell> NewPropertyCell();
361 :
362 : Handle<WeakCell> NewWeakCell(Handle<HeapObject> value);
363 :
364 : Handle<Cell> NewNoClosuresCell(Handle<Object> value);
365 : Handle<Cell> NewOneClosureCell(Handle<Object> value);
366 : Handle<Cell> NewManyClosuresCell(Handle<Object> value);
367 :
368 : Handle<TransitionArray> NewTransitionArray(int capacity);
369 :
370 : // Allocate a tenured AllocationSite. It's payload is null.
371 : Handle<AllocationSite> NewAllocationSite();
372 :
373 : Handle<Map> NewMap(
374 : InstanceType type,
375 : int instance_size,
376 : ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND);
377 :
378 : Handle<HeapObject> NewFillerObject(int size,
379 : bool double_align,
380 : AllocationSpace space);
381 :
382 : Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
383 :
384 : Handle<JSObject> CopyJSObject(Handle<JSObject> object);
385 :
386 : Handle<JSObject> CopyJSObjectWithAllocationSite(Handle<JSObject> object,
387 : Handle<AllocationSite> site);
388 :
389 : Handle<FixedArray> CopyFixedArrayWithMap(Handle<FixedArray> array,
390 : Handle<Map> map);
391 :
392 : Handle<FixedArray> CopyFixedArrayAndGrow(
393 : Handle<FixedArray> array, int grow_by,
394 : PretenureFlag pretenure = NOT_TENURED);
395 :
396 : Handle<FixedArray> CopyFixedArrayUpTo(Handle<FixedArray> array, int new_len,
397 : PretenureFlag pretenure = NOT_TENURED);
398 :
399 : Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
400 :
401 : // This method expects a COW array in new space, and creates a copy
402 : // of it in old space.
403 : Handle<FixedArray> CopyAndTenureFixedCOWArray(Handle<FixedArray> array);
404 :
405 : Handle<FixedDoubleArray> CopyFixedDoubleArray(
406 : Handle<FixedDoubleArray> array);
407 :
408 : // Numbers (e.g. literals) are pretenured by the parser.
409 : // The return value may be a smi or a heap number.
410 : Handle<Object> NewNumber(double value,
411 : PretenureFlag pretenure = NOT_TENURED);
412 :
413 : Handle<Object> NewNumberFromInt(int32_t value,
414 : PretenureFlag pretenure = NOT_TENURED);
415 : Handle<Object> NewNumberFromUint(uint32_t value,
416 : PretenureFlag pretenure = NOT_TENURED);
417 135073 : Handle<Object> NewNumberFromSize(size_t value,
418 : PretenureFlag pretenure = NOT_TENURED) {
419 : // We can't use Smi::IsValid() here because that operates on a signed
420 : // intptr_t, and casting from size_t could create a bogus sign bit.
421 135073 : if (value <= static_cast<size_t>(Smi::kMaxValue)) {
422 134979 : return Handle<Object>(Smi::FromIntptr(static_cast<intptr_t>(value)),
423 134979 : isolate());
424 : }
425 94 : return NewNumber(static_cast<double>(value), pretenure);
426 : }
427 42135 : Handle<Object> NewNumberFromInt64(int64_t value,
428 : PretenureFlag pretenure = NOT_TENURED) {
429 42051 : if (value <= std::numeric_limits<int32_t>::max() &&
430 42135 : value >= std::numeric_limits<int32_t>::min() &&
431 : Smi::IsValid(static_cast<int32_t>(value))) {
432 42051 : return Handle<Object>(Smi::FromInt(static_cast<int32_t>(value)),
433 42051 : isolate());
434 : }
435 84 : return NewNumber(static_cast<double>(value), pretenure);
436 : }
437 30133 : Handle<HeapNumber> NewHeapNumber(double value, MutableMode mode = IMMUTABLE,
438 : PretenureFlag pretenure = NOT_TENURED) {
439 36506636 : Handle<HeapNumber> heap_number = NewHeapNumber(mode, pretenure);
440 : heap_number->set_value(value);
441 30133 : return heap_number;
442 : }
443 : Handle<HeapNumber> NewHeapNumberFromBits(
444 : uint64_t bits, MutableMode mode = IMMUTABLE,
445 : PretenureFlag pretenure = NOT_TENURED) {
446 49544 : Handle<HeapNumber> heap_number = NewHeapNumber(mode, pretenure);
447 : heap_number->set_value_as_bits(bits);
448 : return heap_number;
449 : }
450 : // Creates mutable heap number object with value field set to hole NaN.
451 : Handle<HeapNumber> NewMutableHeapNumber(
452 : PretenureFlag pretenure = NOT_TENURED) {
453 : return NewHeapNumberFromBits(kHoleNanInt64, MUTABLE, pretenure);
454 : }
455 :
456 : // Creates heap number object with not yet set value field.
457 : Handle<HeapNumber> NewHeapNumber(MutableMode mode,
458 : PretenureFlag pretenure = NOT_TENURED);
459 :
460 : Handle<JSWeakMap> NewJSWeakMap();
461 :
462 : Handle<JSObject> NewArgumentsObject(Handle<JSFunction> callee, int length);
463 :
464 : // JS objects are pretenured when allocated by the bootstrapper and
465 : // runtime.
466 : Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
467 : PretenureFlag pretenure = NOT_TENURED);
468 : // JSObject without a prototype.
469 : Handle<JSObject> NewJSObjectWithNullProto(
470 : PretenureFlag pretenure = NOT_TENURED);
471 :
472 : // Global objects are pretenured and initialized based on a constructor.
473 : Handle<JSGlobalObject> NewJSGlobalObject(Handle<JSFunction> constructor);
474 :
475 : // JS objects are pretenured when allocated by the bootstrapper and
476 : // runtime.
477 : Handle<JSObject> NewJSObjectFromMap(
478 : Handle<Map> map,
479 : PretenureFlag pretenure = NOT_TENURED,
480 : Handle<AllocationSite> allocation_site = Handle<AllocationSite>::null());
481 :
482 : // JS arrays are pretenured when allocated by the parser.
483 :
484 : // Create a JSArray with a specified length and elements initialized
485 : // according to the specified mode.
486 : Handle<JSArray> NewJSArray(
487 : ElementsKind elements_kind, int length, int capacity,
488 : ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS,
489 : PretenureFlag pretenure = NOT_TENURED);
490 :
491 1960537 : Handle<JSArray> NewJSArray(
492 : int capacity, ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
493 : PretenureFlag pretenure = NOT_TENURED) {
494 1960537 : if (capacity != 0) {
495 : elements_kind = GetHoleyElementsKind(elements_kind);
496 : }
497 : return NewJSArray(elements_kind, 0, capacity,
498 1960537 : INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE, pretenure);
499 : }
500 :
501 : // Create a JSArray with the given elements.
502 : Handle<JSArray> NewJSArrayWithElements(Handle<FixedArrayBase> elements,
503 : ElementsKind elements_kind, int length,
504 : PretenureFlag pretenure = NOT_TENURED);
505 :
506 6148495 : Handle<JSArray> NewJSArrayWithElements(
507 : Handle<FixedArrayBase> elements,
508 : ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
509 : PretenureFlag pretenure = NOT_TENURED) {
510 : return NewJSArrayWithElements(elements, elements_kind, elements->length(),
511 18251388 : pretenure);
512 : }
513 :
514 : void NewJSArrayStorage(
515 : Handle<JSArray> array,
516 : int length,
517 : int capacity,
518 : ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS);
519 :
520 : Handle<JSGeneratorObject> NewJSGeneratorObject(Handle<JSFunction> function);
521 :
522 : Handle<JSModuleNamespace> NewJSModuleNamespace();
523 :
524 : Handle<Module> NewModule(Handle<SharedFunctionInfo> code);
525 :
526 : Handle<JSArrayBuffer> NewJSArrayBuffer(
527 : SharedFlag shared = SharedFlag::kNotShared,
528 : PretenureFlag pretenure = NOT_TENURED);
529 :
530 : ExternalArrayType GetArrayTypeFromElementsKind(ElementsKind kind);
531 : size_t GetExternalArrayElementSize(ExternalArrayType type);
532 :
533 : Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type,
534 : PretenureFlag pretenure = NOT_TENURED);
535 :
536 : Handle<JSTypedArray> NewJSTypedArray(ElementsKind elements_kind,
537 : PretenureFlag pretenure = NOT_TENURED);
538 :
539 : // Creates a new JSTypedArray with the specified buffer.
540 : Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type,
541 : Handle<JSArrayBuffer> buffer,
542 : size_t byte_offset, size_t length,
543 : PretenureFlag pretenure = NOT_TENURED);
544 :
545 : // Creates a new on-heap JSTypedArray.
546 : Handle<JSTypedArray> NewJSTypedArray(ElementsKind elements_kind,
547 : size_t number_of_elements,
548 : PretenureFlag pretenure = NOT_TENURED);
549 :
550 : Handle<JSDataView> NewJSDataView();
551 : Handle<JSDataView> NewJSDataView(Handle<JSArrayBuffer> buffer,
552 : size_t byte_offset, size_t byte_length);
553 :
554 : Handle<JSIteratorResult> NewJSIteratorResult(Handle<Object> value, bool done);
555 : Handle<JSAsyncFromSyncIterator> NewJSAsyncFromSyncIterator(
556 : Handle<JSReceiver> sync_iterator);
557 :
558 : Handle<JSMap> NewJSMap();
559 : Handle<JSSet> NewJSSet();
560 :
561 : // TODO(aandrey): Maybe these should take table, index and kind arguments.
562 : Handle<JSMapIterator> NewJSMapIterator();
563 : Handle<JSSetIterator> NewJSSetIterator();
564 :
565 : // Allocates a bound function.
566 : MaybeHandle<JSBoundFunction> NewJSBoundFunction(
567 : Handle<JSReceiver> target_function, Handle<Object> bound_this,
568 : Vector<Handle<Object>> bound_args);
569 :
570 : // Allocates a Harmony proxy.
571 : Handle<JSProxy> NewJSProxy(Handle<JSReceiver> target,
572 : Handle<JSReceiver> handler);
573 :
574 : // Reinitialize an JSGlobalProxy based on a constructor. The object
575 : // must have the same size as objects allocated using the
576 : // constructor. The object is reinitialized and behaves as an
577 : // object that has been freshly allocated using the constructor.
578 : void ReinitializeJSGlobalProxy(Handle<JSGlobalProxy> global,
579 : Handle<JSFunction> constructor);
580 :
581 : Handle<JSGlobalProxy> NewUninitializedJSGlobalProxy(int size);
582 :
583 : Handle<JSFunction> NewFunction(Handle<Map> map,
584 : Handle<SharedFunctionInfo> info,
585 : Handle<Object> context_or_undefined,
586 : PretenureFlag pretenure = TENURED);
587 : Handle<JSFunction> NewFunction(Handle<String> name, Handle<Code> code,
588 : Handle<Object> prototype,
589 : bool is_strict = false);
590 : Handle<JSFunction> NewFunction(Handle<String> name);
591 : Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name,
592 : Handle<Code> code,
593 : bool is_strict = false);
594 :
595 : Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
596 : Handle<Map> initial_map, Handle<SharedFunctionInfo> function_info,
597 : Handle<Object> context_or_undefined, Handle<Cell> vector,
598 : PretenureFlag pretenure = TENURED);
599 :
600 : Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
601 : Handle<SharedFunctionInfo> function_info, Handle<Context> context,
602 : Handle<Cell> vector, PretenureFlag pretenure = TENURED);
603 :
604 : Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
605 : Handle<Map> initial_map, Handle<SharedFunctionInfo> function_info,
606 : Handle<Object> context_or_undefined, PretenureFlag pretenure = TENURED);
607 :
608 : Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
609 : Handle<SharedFunctionInfo> function_info, Handle<Context> context,
610 : PretenureFlag pretenure = TENURED);
611 :
612 : Handle<JSFunction> NewFunction(Handle<String> name, Handle<Code> code,
613 : Handle<Object> prototype, InstanceType type,
614 : int instance_size,
615 : bool is_strict = false);
616 : Handle<JSFunction> NewFunction(Handle<String> name,
617 : Handle<Code> code,
618 : InstanceType type,
619 : int instance_size);
620 : Handle<JSFunction> NewFunction(Handle<Map> map, Handle<String> name,
621 : MaybeHandle<Code> maybe_code);
622 :
623 : // Create a serialized scope info.
624 : Handle<ScopeInfo> NewScopeInfo(int length);
625 :
626 : Handle<ModuleInfoEntry> NewModuleInfoEntry();
627 : Handle<ModuleInfo> NewModuleInfo();
628 :
629 : // Create an External object for V8's external API.
630 : Handle<JSObject> NewExternal(void* value);
631 :
632 : // The reference to the Code object is stored in self_reference.
633 : // This allows generated code to reference its own Code object
634 : // by containing this handle.
635 : Handle<Code> NewCode(const CodeDesc& desc,
636 : Code::Flags flags,
637 : Handle<Object> self_reference,
638 : bool immovable = false,
639 : bool crankshafted = false,
640 : int prologue_offset = Code::kPrologueOffsetNotSet,
641 : bool is_debug = false);
642 :
643 : Handle<Code> CopyCode(Handle<Code> code);
644 :
645 : Handle<BytecodeArray> CopyBytecodeArray(Handle<BytecodeArray>);
646 :
647 : // Interface for creating error objects.
648 : Handle<Object> NewError(Handle<JSFunction> constructor,
649 : Handle<String> message);
650 :
651 : Handle<Object> NewInvalidStringLengthError();
652 :
653 8212 : Handle<Object> NewURIError() {
654 : return NewError(isolate()->uri_error_function(),
655 8212 : MessageTemplate::kURIMalformed);
656 : }
657 :
658 : Handle<Object> NewError(Handle<JSFunction> constructor,
659 : MessageTemplate::Template template_index,
660 : Handle<Object> arg0 = Handle<Object>(),
661 : Handle<Object> arg1 = Handle<Object>(),
662 : Handle<Object> arg2 = Handle<Object>());
663 :
664 : #define DECLARE_ERROR(NAME) \
665 : Handle<Object> New##NAME(MessageTemplate::Template template_index, \
666 : Handle<Object> arg0 = Handle<Object>(), \
667 : Handle<Object> arg1 = Handle<Object>(), \
668 : Handle<Object> arg2 = Handle<Object>());
669 : DECLARE_ERROR(Error)
670 : DECLARE_ERROR(EvalError)
671 : DECLARE_ERROR(RangeError)
672 : DECLARE_ERROR(ReferenceError)
673 : DECLARE_ERROR(SyntaxError)
674 : DECLARE_ERROR(TypeError)
675 : DECLARE_ERROR(WasmCompileError)
676 : DECLARE_ERROR(WasmLinkError)
677 : DECLARE_ERROR(WasmRuntimeError)
678 : #undef DECLARE_ERROR
679 :
680 : Handle<String> NumberToString(Handle<Object> number,
681 : bool check_number_string_cache = true);
682 :
683 2786690 : Handle<String> Uint32ToString(uint32_t value) {
684 2786690 : Handle<String> result = NumberToString(NewNumberFromUint(value));
685 :
686 2786690 : if (result->length() <= String::kMaxArrayIndexSize) {
687 : uint32_t field =
688 2303138 : StringHasher::MakeArrayIndexHash(value, result->length());
689 : result->set_hash_field(field);
690 : }
691 2786690 : return result;
692 : }
693 :
694 : Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
695 :
696 : #define ROOT_ACCESSOR(type, name, camel_name) \
697 : inline Handle<type> name() { \
698 : return Handle<type>(bit_cast<type**>( \
699 : &isolate()->heap()->roots_[Heap::k##camel_name##RootIndex])); \
700 : }
701 1000049996 : ROOT_LIST(ROOT_ACCESSOR)
702 : #undef ROOT_ACCESSOR
703 :
704 : #define STRUCT_MAP_ACCESSOR(NAME, Name, name) \
705 : inline Handle<Map> name##_map() { \
706 : return Handle<Map>(bit_cast<Map**>( \
707 : &isolate()->heap()->roots_[Heap::k##Name##MapRootIndex])); \
708 : }
709 1306214 : STRUCT_LIST(STRUCT_MAP_ACCESSOR)
710 : #undef STRUCT_MAP_ACCESSOR
711 :
712 : #define STRING_ACCESSOR(name, str) \
713 : inline Handle<String> name() { \
714 : return Handle<String>(bit_cast<String**>( \
715 : &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
716 : }
717 77346828 : INTERNALIZED_STRING_LIST(STRING_ACCESSOR)
718 : #undef STRING_ACCESSOR
719 :
720 : #define SYMBOL_ACCESSOR(name) \
721 : inline Handle<Symbol> name() { \
722 : return Handle<Symbol>(bit_cast<Symbol**>( \
723 : &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
724 : }
725 58763868 : PRIVATE_SYMBOL_LIST(SYMBOL_ACCESSOR)
726 : #undef SYMBOL_ACCESSOR
727 :
728 : #define SYMBOL_ACCESSOR(name, description) \
729 : inline Handle<Symbol> name() { \
730 : return Handle<Symbol>(bit_cast<Symbol**>( \
731 : &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
732 : }
733 11111237 : PUBLIC_SYMBOL_LIST(SYMBOL_ACCESSOR)
734 4556457 : WELL_KNOWN_SYMBOL_LIST(SYMBOL_ACCESSOR)
735 : #undef SYMBOL_ACCESSOR
736 :
737 : // Allocates a new SharedFunctionInfo object.
738 : Handle<SharedFunctionInfo> NewSharedFunctionInfo(
739 : Handle<String> name, FunctionKind kind, Handle<Code> code,
740 : Handle<ScopeInfo> scope_info);
741 : Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name,
742 : MaybeHandle<Code> code,
743 : bool is_constructor);
744 :
745 : Handle<SharedFunctionInfo> NewSharedFunctionInfoForLiteral(
746 : FunctionLiteral* literal, Handle<Script> script);
747 :
748 : static bool IsFunctionModeWithPrototype(FunctionMode function_mode) {
749 : return (function_mode == FUNCTION_WITH_WRITEABLE_PROTOTYPE ||
750 : function_mode == FUNCTION_WITH_READONLY_PROTOTYPE);
751 : }
752 :
753 : Handle<Map> CreateSloppyFunctionMap(FunctionMode function_mode);
754 :
755 : Handle<Map> CreateStrictFunctionMap(FunctionMode function_mode,
756 : Handle<JSFunction> empty_function);
757 :
758 : Handle<Map> CreateClassFunctionMap(Handle<JSFunction> empty_function);
759 :
760 : // Allocates a new JSMessageObject object.
761 : Handle<JSMessageObject> NewJSMessageObject(MessageTemplate::Template message,
762 : Handle<Object> argument,
763 : int start_position,
764 : int end_position,
765 : Handle<Object> script,
766 : Handle<Object> stack_frames);
767 :
768 : Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
769 :
770 : // Return a map for given number of properties using the map cache in the
771 : // native context.
772 : Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
773 : int number_of_properties,
774 : bool* is_result_from_cache);
775 :
776 : Handle<RegExpMatchInfo> NewRegExpMatchInfo();
777 :
778 : // Creates a new FixedArray that holds the data associated with the
779 : // atom regexp and stores it in the regexp.
780 : void SetRegExpAtomData(Handle<JSRegExp> regexp,
781 : JSRegExp::Type type,
782 : Handle<String> source,
783 : JSRegExp::Flags flags,
784 : Handle<Object> match_pattern);
785 :
786 : // Creates a new FixedArray that holds the data associated with the
787 : // irregexp regexp and stores it in the regexp.
788 : void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
789 : JSRegExp::Type type,
790 : Handle<String> source,
791 : JSRegExp::Flags flags,
792 : int capture_count);
793 :
794 : // Returns the value for a known global constant (a property of the global
795 : // object which is neither configurable nor writable) like 'undefined'.
796 : // Returns a null handle when the given name is unknown.
797 : Handle<Object> GlobalConstantFor(Handle<Name> name);
798 :
799 : // Converts the given boolean condition to JavaScript boolean value.
800 : Handle<Object> ToBoolean(bool value);
801 :
802 : // Converts the given ToPrimitive hint to it's string representation.
803 : Handle<String> ToPrimitiveHintString(ToPrimitiveHint hint);
804 :
805 : private:
806 : Isolate* isolate() { return reinterpret_cast<Isolate*>(this); }
807 :
808 : // Creates a heap object based on the map. The fields of the heap object are
809 : // not initialized by New<>() functions. It's the responsibility of the caller
810 : // to do that.
811 : template<typename T>
812 : Handle<T> New(Handle<Map> map, AllocationSpace space);
813 :
814 : template<typename T>
815 : Handle<T> New(Handle<Map> map,
816 : AllocationSpace space,
817 : Handle<AllocationSite> allocation_site);
818 :
819 : MaybeHandle<String> NewStringFromTwoByte(const uc16* string, int length,
820 : PretenureFlag pretenure);
821 :
822 : // Creates a code object that is not yet fully initialized yet.
823 : inline Handle<Code> NewCodeRaw(int object_size, bool immovable);
824 :
825 : // Attempt to find the number in a small cache. If we finds it, return
826 : // the string representation of the number. Otherwise return undefined.
827 : Handle<Object> GetNumberStringCache(Handle<Object> number);
828 :
829 : // Update the cache with a new number-string pair.
830 : void SetNumberStringCache(Handle<Object> number, Handle<String> string);
831 :
832 : // Create a JSArray with no elements and no length.
833 : Handle<JSArray> NewJSArray(ElementsKind elements_kind,
834 : PretenureFlag pretenure = NOT_TENURED);
835 :
836 : void SetFunctionInstanceDescriptor(Handle<Map> map,
837 : FunctionMode function_mode);
838 :
839 : void SetStrictFunctionInstanceDescriptor(Handle<Map> map,
840 : FunctionMode function_mode);
841 :
842 : void SetClassFunctionInstanceDescriptor(Handle<Map> map);
843 : };
844 :
845 : } // namespace internal
846 : } // namespace v8
847 :
848 : #endif // V8_FACTORY_H_
|