1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 // Interface for handle based allocation.
42 // Allocate a new boxed value.
45 PretenureFlag pretenure = NOT_TENURED);
47 // Allocates a fixed array initialized with undefined values.
48 Handle<FixedArray> NewFixedArray(
50 PretenureFlag pretenure = NOT_TENURED);
52 // Allocate a new fixed array with non-existing entries (the hole).
53 Handle<FixedArray> NewFixedArrayWithHoles(
55 PretenureFlag pretenure = NOT_TENURED);
57 // Allocates an uninitialized fixed array. It must be filled by the caller.
58 Handle<FixedArray> NewUninitializedFixedArray(int size);
60 // Allocate a new uninitialized fixed double array.
61 Handle<FixedDoubleArray> NewFixedDoubleArray(
63 PretenureFlag pretenure = NOT_TENURED);
65 Handle<ConstantPoolArray> NewConstantPoolArray(
66 int number_of_int64_entries,
67 int number_of_code_ptr_entries,
68 int number_of_heap_ptr_entries,
69 int number_of_int32_entries);
71 Handle<SeededNumberDictionary> NewSeededNumberDictionary(
72 int at_least_space_for);
74 Handle<UnseededNumberDictionary> NewUnseededNumberDictionary(
75 int at_least_space_for);
77 Handle<NameDictionary> NewNameDictionary(int at_least_space_for);
79 Handle<ObjectHashSet> NewObjectHashSet(int at_least_space_for);
81 Handle<ObjectHashTable> NewObjectHashTable(
82 int at_least_space_for,
83 MinimumCapacity capacity_option = USE_DEFAULT_MINIMUM_CAPACITY);
85 Handle<WeakHashTable> NewWeakHashTable(int at_least_space_for);
87 Handle<DescriptorArray> NewDescriptorArray(int number_of_descriptors,
89 Handle<DeoptimizationInputData> NewDeoptimizationInputData(
90 int deopt_entry_count,
91 PretenureFlag pretenure);
92 Handle<DeoptimizationOutputData> NewDeoptimizationOutputData(
93 int deopt_entry_count,
94 PretenureFlag pretenure);
95 // Allocates a pre-tenured empty AccessorPair.
96 Handle<AccessorPair> NewAccessorPair();
98 Handle<TypeFeedbackInfo> NewTypeFeedbackInfo();
100 Handle<String> InternalizeUtf8String(Vector<const char> str);
101 Handle<String> InternalizeUtf8String(const char* str) {
102 return InternalizeUtf8String(CStrVector(str));
104 Handle<String> InternalizeString(Handle<String> str);
105 Handle<String> InternalizeOneByteString(Vector<const uint8_t> str);
106 Handle<String> InternalizeOneByteString(
107 Handle<SeqOneByteString>, int from, int length);
109 Handle<String> InternalizeTwoByteString(Vector<const uc16> str);
111 template<class StringTableKey>
112 Handle<String> InternalizeStringWithKey(StringTableKey* key);
115 // String creation functions. Most of the string creation functions take
116 // a Heap::PretenureFlag argument to optionally request that they be
117 // allocated in the old generation. The pretenure flag defaults to
120 // Creates a new String object. There are two String encodings: ASCII and
121 // two byte. One should choose between the three string factory functions
122 // based on the encoding of the string buffer that the string is
124 // - ...FromAscii initializes the string from a buffer that is ASCII
125 // encoded (it does not check that the buffer is ASCII encoded) and
126 // the result will be ASCII encoded.
127 // - ...FromUtf8 initializes the string from a buffer that is UTF-8
128 // encoded. If the characters are all single-byte characters, the
129 // result will be ASCII encoded, otherwise it will converted to two
131 // - ...FromTwoByte initializes the string from a buffer that is two
132 // byte encoded. If the characters are all single-byte characters,
133 // the result will be converted to ASCII, otherwise it will be left as
136 // ASCII strings are pretenured when used as keys in the SourceCodeCache.
137 Handle<String> NewStringFromOneByte(
138 Vector<const uint8_t> str,
139 PretenureFlag pretenure = NOT_TENURED);
140 // TODO(dcarney): remove this function.
141 inline Handle<String> NewStringFromAscii(
142 Vector<const char> str,
143 PretenureFlag pretenure = NOT_TENURED) {
144 return NewStringFromOneByte(Vector<const uint8_t>::cast(str), pretenure);
147 // UTF8 strings are pretenured when used for regexp literal patterns and
148 // flags in the parser.
149 Handle<String> NewStringFromUtf8(
150 Vector<const char> str,
151 PretenureFlag pretenure = NOT_TENURED);
153 Handle<String> NewStringFromTwoByte(
154 Vector<const uc16> str,
155 PretenureFlag pretenure = NOT_TENURED);
157 // Allocates and partially initializes an ASCII or TwoByte String. The
158 // characters of the string are uninitialized. Currently used in regexp code
159 // only, where they are pretenured.
160 Handle<SeqOneByteString> NewRawOneByteString(
162 PretenureFlag pretenure = NOT_TENURED);
163 Handle<SeqTwoByteString> NewRawTwoByteString(
165 PretenureFlag pretenure = NOT_TENURED);
167 // Create a new cons string object which consists of a pair of strings.
168 Handle<String> NewConsString(Handle<String> left,
169 Handle<String> right);
171 Handle<ConsString> NewRawConsString(String::Encoding encoding);
173 // Create a new sequential string containing the concatenation of the inputs.
174 Handle<String> NewFlatConcatString(Handle<String> first,
175 Handle<String> second);
177 // Create a new string object which holds a proper substring of a string.
178 Handle<String> NewProperSubString(Handle<String> str,
182 // Create a new string object which holds a substring of a string.
183 Handle<String> NewSubString(Handle<String> str, int begin, int end) {
184 if (begin == 0 && end == str->length()) return str;
185 return NewProperSubString(str, begin, end);
188 Handle<SlicedString> NewRawSlicedString(String::Encoding encoding);
190 // Creates a new external String object. There are two String encodings
191 // in the system: ASCII and two byte. Unlike other String types, it does
192 // not make sense to have a UTF-8 factory function for external strings,
193 // because we cannot change the underlying buffer.
194 Handle<String> NewExternalStringFromAscii(
195 const ExternalAsciiString::Resource* resource);
196 Handle<String> NewExternalStringFromTwoByte(
197 const ExternalTwoByteString::Resource* resource);
200 Handle<Symbol> NewSymbol();
201 Handle<Symbol> NewPrivateSymbol();
203 // Create a global (but otherwise uninitialized) context.
204 Handle<Context> NewNativeContext();
206 // Create a global context.
207 Handle<Context> NewGlobalContext(Handle<JSFunction> function,
208 Handle<ScopeInfo> scope_info);
210 // Create a module context.
211 Handle<Context> NewModuleContext(Handle<ScopeInfo> scope_info);
213 // Create a function context.
214 Handle<Context> NewFunctionContext(int length, Handle<JSFunction> function);
216 // Create a catch context.
217 Handle<Context> NewCatchContext(Handle<JSFunction> function,
218 Handle<Context> previous,
220 Handle<Object> thrown_object);
222 // Create a 'with' context.
223 Handle<Context> NewWithContext(Handle<JSFunction> function,
224 Handle<Context> previous,
225 Handle<JSObject> extension);
227 // Create a block context.
228 Handle<Context> NewBlockContext(Handle<JSFunction> function,
229 Handle<Context> previous,
230 Handle<ScopeInfo> scope_info);
232 // Allocate a new struct. The struct is pretenured (allocated directly in
233 // the old generation).
234 Handle<Struct> NewStruct(InstanceType type);
236 Handle<AliasedArgumentsEntry> NewAliasedArgumentsEntry(
237 int aliased_context_slot);
239 Handle<DeclaredAccessorDescriptor> NewDeclaredAccessorDescriptor();
241 Handle<DeclaredAccessorInfo> NewDeclaredAccessorInfo();
243 Handle<ExecutableAccessorInfo> NewExecutableAccessorInfo();
245 Handle<Script> NewScript(Handle<String> source);
247 // Foreign objects are pretenured when allocated by the bootstrapper.
248 Handle<Foreign> NewForeign(Address addr,
249 PretenureFlag pretenure = NOT_TENURED);
251 // Allocate a new foreign object. The foreign is pretenured (allocated
252 // directly in the old generation).
253 Handle<Foreign> NewForeign(const AccessorDescriptor* foreign);
255 Handle<ByteArray> NewByteArray(int length,
256 PretenureFlag pretenure = NOT_TENURED);
258 Handle<ExternalArray> NewExternalArray(
260 ExternalArrayType array_type,
261 void* external_pointer,
262 PretenureFlag pretenure = NOT_TENURED);
264 Handle<FixedTypedArrayBase> NewFixedTypedArray(
266 ExternalArrayType array_type,
267 PretenureFlag pretenure = NOT_TENURED);
269 Handle<Cell> NewCell(Handle<Object> value);
271 Handle<PropertyCell> NewPropertyCellWithHole();
273 Handle<PropertyCell> NewPropertyCell(Handle<Object> value);
275 Handle<AllocationSite> NewAllocationSite();
280 ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND);
282 Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
284 Handle<Map> CopyWithPreallocatedFieldDescriptors(Handle<Map> map);
286 // Copy the map adding more inobject properties if possible without
287 // overflowing the instance size.
288 Handle<Map> CopyMap(Handle<Map> map, int extra_inobject_props);
289 Handle<Map> CopyMap(Handle<Map> map);
291 Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
293 // This method expects a COW array in new space, and creates a copy
294 // of it in old space.
295 Handle<FixedArray> CopyAndTenureFixedCOWArray(Handle<FixedArray> array);
297 Handle<FixedArray> CopySizeFixedArray(Handle<FixedArray> array,
299 PretenureFlag pretenure = NOT_TENURED);
301 Handle<FixedDoubleArray> CopyFixedDoubleArray(
302 Handle<FixedDoubleArray> array);
304 Handle<ConstantPoolArray> CopyConstantPoolArray(
305 Handle<ConstantPoolArray> array);
307 // Numbers (e.g. literals) are pretenured by the parser.
308 Handle<Object> NewNumber(double value,
309 PretenureFlag pretenure = NOT_TENURED);
311 Handle<Object> NewNumberFromInt(int32_t value,
312 PretenureFlag pretenure = NOT_TENURED);
313 Handle<Object> NewNumberFromUint(uint32_t value,
314 PretenureFlag pretenure = NOT_TENURED);
315 inline Handle<Object> NewNumberFromSize(size_t value,
316 PretenureFlag pretenure = NOT_TENURED);
317 Handle<HeapNumber> NewHeapNumber(double value,
318 PretenureFlag pretenure = NOT_TENURED);
320 Handle<Float32x4> NewFloat32x4(float32x4_value_t value,
321 PretenureFlag pretenure = NOT_TENURED);
323 Handle<Int32x4> NewInt32x4(int32x4_value_t value,
324 PretenureFlag pretenure = NOT_TENURED);
326 // These objects are used by the api to create env-independent data
327 // structures in the heap.
328 Handle<JSObject> NewNeanderObject();
330 Handle<JSObject> NewArgumentsObject(Handle<Object> callee, int length);
332 // JS objects are pretenured when allocated by the bootstrapper and
334 Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
335 PretenureFlag pretenure = NOT_TENURED);
336 // JSObject that should have a memento pointing to the allocation site.
337 Handle<JSObject> NewJSObjectWithMemento(Handle<JSFunction> constructor,
338 Handle<AllocationSite> site);
340 // Global objects are pretenured and initialized based on a constructor.
341 Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor);
343 // JS objects are pretenured when allocated by the bootstrapper and
345 Handle<JSObject> NewJSObjectFromMap(
347 PretenureFlag pretenure = NOT_TENURED,
348 bool allocate_properties = true,
349 Handle<AllocationSite> allocation_site = Handle<AllocationSite>::null());
351 Handle<JSObject> NewJSObjectFromMapForDeoptimizer(
352 Handle<Map> map, PretenureFlag pretenure = NOT_TENURED);
354 // JS modules are pretenured.
355 Handle<JSModule> NewJSModule(Handle<Context> context,
356 Handle<ScopeInfo> scope_info);
358 // JS arrays are pretenured when allocated by the parser.
359 Handle<JSArray> NewJSArray(
360 ElementsKind elements_kind,
363 PretenureFlag pretenure = NOT_TENURED);
365 Handle<JSArray> NewJSArray(
367 ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
368 PretenureFlag pretenure = NOT_TENURED) {
369 return NewJSArray(elements_kind, 0, capacity, pretenure);
372 Handle<JSArray> NewJSArrayWithElements(
373 Handle<FixedArrayBase> elements,
374 ElementsKind elements_kind,
376 PretenureFlag pretenure = NOT_TENURED);
378 Handle<JSArray> NewJSArrayWithElements(
379 Handle<FixedArrayBase> elements,
380 ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
381 PretenureFlag pretenure = NOT_TENURED) {
382 return NewJSArrayWithElements(
383 elements, elements_kind, elements->length(), pretenure);
386 void NewJSArrayStorage(
387 Handle<JSArray> array,
390 ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS);
392 Handle<JSGeneratorObject> NewJSGeneratorObject(Handle<JSFunction> function);
394 Handle<JSArrayBuffer> NewJSArrayBuffer();
396 Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type);
398 Handle<JSDataView> NewJSDataView();
400 Handle<JSProxy> NewJSProxy(Handle<Object> handler, Handle<Object> prototype);
402 // Change the type of the argument into a JS object/function and reinitialize.
403 void BecomeJSObject(Handle<JSReceiver> object);
404 void BecomeJSFunction(Handle<JSReceiver> object);
406 Handle<JSFunction> NewFunction(Handle<String> name,
407 Handle<Object> prototype);
409 Handle<JSFunction> NewFunctionWithoutPrototype(
411 StrictMode strict_mode);
413 Handle<JSFunction> NewFunction(Handle<Object> super, bool is_global);
415 Handle<JSFunction> BaseNewFunctionFromSharedFunctionInfo(
416 Handle<SharedFunctionInfo> function_info,
417 Handle<Map> function_map,
418 PretenureFlag pretenure);
420 Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
421 Handle<SharedFunctionInfo> function_info,
422 Handle<Context> context,
423 PretenureFlag pretenure = TENURED);
425 Handle<ScopeInfo> NewScopeInfo(int length);
427 Handle<JSObject> NewExternal(void* value);
429 Handle<Code> NewCode(const CodeDesc& desc,
431 Handle<Object> self_reference,
432 bool immovable = false,
433 bool crankshafted = false,
434 int prologue_offset = Code::kPrologueOffsetNotSet);
436 Handle<Code> CopyCode(Handle<Code> code);
438 Handle<Code> CopyCode(Handle<Code> code, Vector<byte> reloc_info);
440 Handle<Object> ToObject(Handle<Object> object);
441 Handle<Object> ToObject(Handle<Object> object,
442 Handle<Context> native_context);
444 // Interface for creating error objects.
446 Handle<Object> NewError(const char* maker, const char* message,
447 Handle<JSArray> args);
448 Handle<String> EmergencyNewError(const char* message, Handle<JSArray> args);
449 Handle<Object> NewError(const char* maker, const char* message,
450 Vector< Handle<Object> > args);
451 Handle<Object> NewError(const char* message,
452 Vector< Handle<Object> > args);
453 Handle<Object> NewError(Handle<String> message);
454 Handle<Object> NewError(const char* constructor,
455 Handle<String> message);
457 Handle<Object> NewTypeError(const char* message,
458 Vector< Handle<Object> > args);
459 Handle<Object> NewTypeError(Handle<String> message);
461 Handle<Object> NewRangeError(const char* message,
462 Vector< Handle<Object> > args);
463 Handle<Object> NewRangeError(Handle<String> message);
465 Handle<Object> NewSyntaxError(const char* message, Handle<JSArray> args);
466 Handle<Object> NewSyntaxError(Handle<String> message);
468 Handle<Object> NewReferenceError(const char* message,
469 Vector< Handle<Object> > args);
470 Handle<Object> NewReferenceError(const char* message, Handle<JSArray> args);
471 Handle<Object> NewReferenceError(Handle<String> message);
473 Handle<Object> NewEvalError(const char* message,
474 Vector< Handle<Object> > args);
477 Handle<JSFunction> NewFunction(Handle<String> name,
481 bool force_initial_map);
483 Handle<JSFunction> NewFunction(Handle<Map> function_map,
484 Handle<SharedFunctionInfo> shared, Handle<Object> prototype);
487 Handle<JSFunction> NewFunctionWithPrototype(Handle<String> name,
490 Handle<JSObject> prototype,
492 bool force_initial_map);
494 Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name,
497 Handle<String> NumberToString(Handle<Object> number);
498 Handle<String> Uint32ToString(uint32_t value);
500 enum ApiInstanceType {
506 Handle<JSFunction> CreateApiFunction(
507 Handle<FunctionTemplateInfo> data,
508 ApiInstanceType type = JavaScriptObject);
510 Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
512 // Installs interceptors on the instance. 'desc' is a function template,
513 // and instance is an object instance created by the function of this
514 // function template.
515 void ConfigureInstance(Handle<FunctionTemplateInfo> desc,
516 Handle<JSObject> instance,
517 bool* pending_exception);
519 #define ROOT_ACCESSOR(type, name, camel_name) \
520 inline Handle<type> name() { \
521 return Handle<type>(BitCast<type**>( \
522 &isolate()->heap()->roots_[Heap::k##camel_name##RootIndex])); \
524 ROOT_LIST(ROOT_ACCESSOR)
527 #define STRUCT_MAP_ACCESSOR(NAME, Name, name) \
528 inline Handle<Map> name##_map() { \
529 return Handle<Map>(BitCast<Map**>( \
530 &isolate()->heap()->roots_[Heap::k##Name##MapRootIndex])); \
532 STRUCT_LIST(STRUCT_MAP_ACCESSOR)
533 #undef STRUCT_MAP_ACCESSOR
535 #define STRING_ACCESSOR(name, str) \
536 inline Handle<String> name() { \
537 return Handle<String>(BitCast<String**>( \
538 &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
540 INTERNALIZED_STRING_LIST(STRING_ACCESSOR)
541 #undef STRING_ACCESSOR
543 Handle<String> hidden_string() {
544 return Handle<String>(&isolate()->heap()->hidden_string_);
547 Handle<SharedFunctionInfo> NewSharedFunctionInfo(
549 int number_of_literals,
552 Handle<ScopeInfo> scope_info);
553 Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name);
555 Handle<JSMessageObject> NewJSMessageObject(
557 Handle<JSArray> arguments,
560 Handle<Object> script,
561 Handle<Object> stack_frames);
563 Handle<SeededNumberDictionary> DictionaryAtNumberPut(
564 Handle<SeededNumberDictionary>,
566 Handle<Object> value);
568 Handle<UnseededNumberDictionary> DictionaryAtNumberPut(
569 Handle<UnseededNumberDictionary>,
571 Handle<Object> value);
573 #ifdef ENABLE_DEBUGGER_SUPPORT
574 Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
577 // Return a map using the map cache in the native context.
578 // The key the an ordered set of property names.
579 Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
580 Handle<FixedArray> keys);
582 // Creates a new FixedArray that holds the data associated with the
583 // atom regexp and stores it in the regexp.
584 void SetRegExpAtomData(Handle<JSRegExp> regexp,
586 Handle<String> source,
587 JSRegExp::Flags flags,
588 Handle<Object> match_pattern);
590 // Creates a new FixedArray that holds the data associated with the
591 // irregexp regexp and stores it in the regexp.
592 void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
594 Handle<String> source,
595 JSRegExp::Flags flags,
598 // Returns the value for a known global constant (a property of the global
599 // object which is neither configurable nor writable) like 'undefined'.
600 // Returns a null handle when the given name is unknown.
601 Handle<Object> GlobalConstantFor(Handle<String> name);
603 // Converts the given boolean condition to JavaScript boolean value.
604 Handle<Object> ToBoolean(bool value);
607 Isolate* isolate() { return reinterpret_cast<Isolate*>(this); }
609 Handle<JSFunction> NewFunctionHelper(Handle<String> name,
610 Handle<Object> prototype);
612 Handle<JSFunction> NewFunctionWithoutPrototypeHelper(
614 StrictMode strict_mode);
616 // Create a new map cache.
617 Handle<MapCache> NewMapCache(int at_least_space_for);
619 // Update the map cache in the native context with (keys, map)
620 Handle<MapCache> AddToMapCache(Handle<Context> context,
621 Handle<FixedArray> keys,
626 Handle<Object> Factory::NewNumberFromSize(size_t value,
627 PretenureFlag pretenure) {
628 if (Smi::IsValid(static_cast<intptr_t>(value))) {
629 return Handle<Object>(Smi::FromIntptr(static_cast<intptr_t>(value)),
632 return NewNumber(static_cast<double>(value), pretenure);
637 } } // namespace v8::internal
639 #endif // V8_FACTORY_H_