1 // Copyright 2010 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.
37 // Interface for handle based allocation.
39 class Factory : public AllStatic {
41 // Allocate a new fixed array with undefined entries.
42 static Handle<FixedArray> NewFixedArray(
44 PretenureFlag pretenure = NOT_TENURED);
46 // Allocate a new fixed array with non-existing entries (the hole).
47 static Handle<FixedArray> NewFixedArrayWithHoles(
49 PretenureFlag pretenure = NOT_TENURED);
51 static Handle<NumberDictionary> NewNumberDictionary(int at_least_space_for);
53 static Handle<StringDictionary> NewStringDictionary(int at_least_space_for);
55 static Handle<DescriptorArray> NewDescriptorArray(int number_of_descriptors);
56 static Handle<DeoptimizationInputData> NewDeoptimizationInputData(
57 int deopt_entry_count,
58 PretenureFlag pretenure);
59 static Handle<DeoptimizationOutputData> NewDeoptimizationOutputData(
60 int deopt_entry_count,
61 PretenureFlag pretenure);
63 static Handle<String> LookupSymbol(Vector<const char> str);
64 static Handle<String> LookupAsciiSymbol(const char* str) {
65 return LookupSymbol(CStrVector(str));
69 // String creation functions. Most of the string creation functions take
70 // a Heap::PretenureFlag argument to optionally request that they be
71 // allocated in the old generation. The pretenure flag defaults to
74 // Creates a new String object. There are two String encodings: ASCII and
75 // two byte. One should choose between the three string factory functions
76 // based on the encoding of the string buffer that the string is
78 // - ...FromAscii initializes the string from a buffer that is ASCII
79 // encoded (it does not check that the buffer is ASCII encoded) and
80 // the result will be ASCII encoded.
81 // - ...FromUtf8 initializes the string from a buffer that is UTF-8
82 // encoded. If the characters are all single-byte characters, the
83 // result will be ASCII encoded, otherwise it will converted to two
85 // - ...FromTwoByte initializes the string from a buffer that is two
86 // byte encoded. If the characters are all single-byte characters,
87 // the result will be converted to ASCII, otherwise it will be left as
90 // ASCII strings are pretenured when used as keys in the SourceCodeCache.
91 static Handle<String> NewStringFromAscii(
92 Vector<const char> str,
93 PretenureFlag pretenure = NOT_TENURED);
95 // UTF8 strings are pretenured when used for regexp literal patterns and
96 // flags in the parser.
97 static Handle<String> NewStringFromUtf8(
98 Vector<const char> str,
99 PretenureFlag pretenure = NOT_TENURED);
101 static Handle<String> NewStringFromTwoByte(
102 Vector<const uc16> str,
103 PretenureFlag pretenure = NOT_TENURED);
105 // Allocates and partially initializes an ASCII or TwoByte String. The
106 // characters of the string are uninitialized. Currently used in regexp code
107 // only, where they are pretenured.
108 static Handle<String> NewRawAsciiString(
110 PretenureFlag pretenure = NOT_TENURED);
111 static Handle<String> NewRawTwoByteString(
113 PretenureFlag pretenure = NOT_TENURED);
115 // Create a new cons string object which consists of a pair of strings.
116 static Handle<String> NewConsString(Handle<String> first,
117 Handle<String> second);
119 // Create a new string object which holds a substring of a string.
120 static Handle<String> NewSubString(Handle<String> str,
124 // Creates a new external String object. There are two String encodings
125 // in the system: ASCII and two byte. Unlike other String types, it does
126 // not make sense to have a UTF-8 factory function for external strings,
127 // because we cannot change the underlying buffer.
128 static Handle<String> NewExternalStringFromAscii(
129 ExternalAsciiString::Resource* resource);
130 static Handle<String> NewExternalStringFromTwoByte(
131 ExternalTwoByteString::Resource* resource);
133 // Create a global (but otherwise uninitialized) context.
134 static Handle<Context> NewGlobalContext();
136 // Create a function context.
137 static Handle<Context> NewFunctionContext(int length,
138 Handle<JSFunction> closure);
140 // Create a 'with' context.
141 static Handle<Context> NewWithContext(Handle<Context> previous,
142 Handle<JSObject> extension,
143 bool is_catch_context);
145 // Return the Symbol matching the passed in string.
146 static Handle<String> SymbolFromString(Handle<String> value);
148 // Allocate a new struct. The struct is pretenured (allocated directly in
149 // the old generation).
150 static Handle<Struct> NewStruct(InstanceType type);
152 static Handle<AccessorInfo> NewAccessorInfo();
154 static Handle<Script> NewScript(Handle<String> source);
156 // Proxies are pretenured when allocated by the bootstrapper.
157 static Handle<Proxy> NewProxy(Address addr,
158 PretenureFlag pretenure = NOT_TENURED);
160 // Allocate a new proxy. The proxy is pretenured (allocated directly in
161 // the old generation).
162 static Handle<Proxy> NewProxy(const AccessorDescriptor* proxy);
164 static Handle<ByteArray> NewByteArray(int length,
165 PretenureFlag pretenure = NOT_TENURED);
167 static Handle<PixelArray> NewPixelArray(
169 uint8_t* external_pointer,
170 PretenureFlag pretenure = NOT_TENURED);
172 static Handle<ExternalArray> NewExternalArray(
174 ExternalArrayType array_type,
175 void* external_pointer,
176 PretenureFlag pretenure = NOT_TENURED);
178 static Handle<JSGlobalPropertyCell> NewJSGlobalPropertyCell(
179 Handle<Object> value);
181 static Handle<Map> NewMap(InstanceType type, int instance_size);
183 static Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
185 static Handle<Map> CopyMapDropDescriptors(Handle<Map> map);
187 // Copy the map adding more inobject properties if possible without
188 // overflowing the instance size.
189 static Handle<Map> CopyMap(Handle<Map> map, int extra_inobject_props);
191 static Handle<Map> CopyMapDropTransitions(Handle<Map> map);
193 static Handle<Map> GetFastElementsMap(Handle<Map> map);
195 static Handle<Map> GetSlowElementsMap(Handle<Map> map);
197 static Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
199 // Numbers (eg, literals) are pretenured by the parser.
200 static Handle<Object> NewNumber(double value,
201 PretenureFlag pretenure = NOT_TENURED);
203 static Handle<Object> NewNumberFromInt(int value);
204 static Handle<Object> NewNumberFromUint(uint32_t value);
206 // These objects are used by the api to create env-independent data
207 // structures in the heap.
208 static Handle<JSObject> NewNeanderObject();
210 static Handle<JSObject> NewArgumentsObject(Handle<Object> callee, int length);
212 // JS objects are pretenured when allocated by the bootstrapper and
214 static Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
215 PretenureFlag pretenure = NOT_TENURED);
217 // Global objects are pretenured.
218 static Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor);
220 // JS objects are pretenured when allocated by the bootstrapper and
222 static Handle<JSObject> NewJSObjectFromMap(Handle<Map> map);
224 // JS arrays are pretenured when allocated by the parser.
225 static Handle<JSArray> NewJSArray(int init_length,
226 PretenureFlag pretenure = NOT_TENURED);
228 static Handle<JSArray> NewJSArrayWithElements(
229 Handle<FixedArray> elements,
230 PretenureFlag pretenure = NOT_TENURED);
232 static Handle<JSFunction> NewFunction(Handle<String> name,
233 Handle<Object> prototype);
235 static Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name);
237 static Handle<JSFunction> NewFunction(Handle<Object> super, bool is_global);
239 static Handle<JSFunction> BaseNewFunctionFromSharedFunctionInfo(
240 Handle<SharedFunctionInfo> function_info,
241 Handle<Map> function_map,
242 PretenureFlag pretenure);
244 static Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
245 Handle<SharedFunctionInfo> function_info,
246 Handle<Context> context,
247 PretenureFlag pretenure = TENURED);
249 static Handle<Code> NewCode(const CodeDesc& desc,
251 Handle<Object> self_reference);
253 static Handle<Code> CopyCode(Handle<Code> code);
255 static Handle<Code> CopyCode(Handle<Code> code, Vector<byte> reloc_info);
257 static Handle<Object> ToObject(Handle<Object> object);
258 static Handle<Object> ToObject(Handle<Object> object,
259 Handle<Context> global_context);
261 // Interface for creating error objects.
263 static Handle<Object> NewError(const char* maker, const char* type,
264 Handle<JSArray> args);
265 static Handle<Object> NewError(const char* maker, const char* type,
266 Vector< Handle<Object> > args);
267 static Handle<Object> NewError(const char* type,
268 Vector< Handle<Object> > args);
269 static Handle<Object> NewError(Handle<String> message);
270 static Handle<Object> NewError(const char* constructor,
271 Handle<String> message);
273 static Handle<Object> NewTypeError(const char* type,
274 Vector< Handle<Object> > args);
275 static Handle<Object> NewTypeError(Handle<String> message);
277 static Handle<Object> NewRangeError(const char* type,
278 Vector< Handle<Object> > args);
279 static Handle<Object> NewRangeError(Handle<String> message);
281 static Handle<Object> NewSyntaxError(const char* type, Handle<JSArray> args);
282 static Handle<Object> NewSyntaxError(Handle<String> message);
284 static Handle<Object> NewReferenceError(const char* type,
285 Vector< Handle<Object> > args);
286 static Handle<Object> NewReferenceError(Handle<String> message);
288 static Handle<Object> NewEvalError(const char* type,
289 Vector< Handle<Object> > args);
292 static Handle<JSFunction> NewFunction(Handle<String> name,
296 bool force_initial_map);
298 static Handle<JSFunction> NewFunction(Handle<Map> function_map,
299 Handle<SharedFunctionInfo> shared, Handle<Object> prototype);
302 static Handle<JSFunction> NewFunctionWithPrototype(Handle<String> name,
305 Handle<JSObject> prototype,
307 bool force_initial_map);
309 static Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name,
312 static Handle<DescriptorArray> CopyAppendProxyDescriptor(
313 Handle<DescriptorArray> array,
315 Handle<Object> value,
316 PropertyAttributes attributes);
318 static Handle<String> NumberToString(Handle<Object> number);
320 enum ApiInstanceType {
326 static Handle<JSFunction> CreateApiFunction(
327 Handle<FunctionTemplateInfo> data,
328 ApiInstanceType type = JavaScriptObject);
330 static Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
332 // Installs interceptors on the instance. 'desc' is a function template,
333 // and instance is an object instance created by the function of this
334 // function template.
335 static void ConfigureInstance(Handle<FunctionTemplateInfo> desc,
336 Handle<JSObject> instance,
337 bool* pending_exception);
339 #define ROOT_ACCESSOR(type, name, camel_name) \
340 static inline Handle<type> name() { \
341 return Handle<type>(BitCast<type**>( \
342 &Heap::roots_[Heap::k##camel_name##RootIndex])); \
344 ROOT_LIST(ROOT_ACCESSOR)
345 #undef ROOT_ACCESSOR_ACCESSOR
347 #define SYMBOL_ACCESSOR(name, str) \
348 static inline Handle<String> name() { \
349 return Handle<String>(BitCast<String**>( \
350 &Heap::roots_[Heap::k##name##RootIndex])); \
352 SYMBOL_LIST(SYMBOL_ACCESSOR)
353 #undef SYMBOL_ACCESSOR
355 static Handle<String> hidden_symbol() {
356 return Handle<String>(&Heap::hidden_symbol_);
359 static Handle<SharedFunctionInfo> NewSharedFunctionInfo(
361 int number_of_literals,
363 Handle<SerializedScopeInfo> scope_info);
364 static Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name);
366 static Handle<NumberDictionary> DictionaryAtNumberPut(
367 Handle<NumberDictionary>,
369 Handle<Object> value);
371 #ifdef ENABLE_DEBUGGER_SUPPORT
372 static Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
375 // Return a map using the map cache in the global context.
376 // The key the an ordered set of property names.
377 static Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
378 Handle<FixedArray> keys);
380 // Creates a new FixedArray that holds the data associated with the
381 // atom regexp and stores it in the regexp.
382 static void SetRegExpAtomData(Handle<JSRegExp> regexp,
384 Handle<String> source,
385 JSRegExp::Flags flags,
386 Handle<Object> match_pattern);
388 // Creates a new FixedArray that holds the data associated with the
389 // irregexp regexp and stores it in the regexp.
390 static void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
392 Handle<String> source,
393 JSRegExp::Flags flags,
397 static Handle<JSFunction> NewFunctionHelper(Handle<String> name,
398 Handle<Object> prototype);
400 static Handle<JSFunction> NewFunctionWithoutPrototypeHelper(
401 Handle<String> name);
403 static Handle<DescriptorArray> CopyAppendCallbackDescriptors(
404 Handle<DescriptorArray> array,
405 Handle<Object> descriptors);
407 // Create a new map cache.
408 static Handle<MapCache> NewMapCache(int at_least_space_for);
410 // Update the map cache in the global context with (keys, map)
411 static Handle<MapCache> AddToMapCache(Handle<Context> context,
412 Handle<FixedArray> keys,
417 } } // namespace v8::internal
419 #endif // V8_FACTORY_H_