1 // Copyright 2006-2008 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.
39 // Interface for handle based allocation.
41 class Factory : public AllStatic {
43 // Allocate a new fixed array with undefined entries.
44 static Handle<FixedArray> NewFixedArray(
46 PretenureFlag pretenure = NOT_TENURED);
48 // Allocate a new fixed array with non-existing entries (the hole).
49 static Handle<FixedArray> NewFixedArrayWithHoles(int size);
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);
57 static Handle<String> LookupSymbol(Vector<const char> str);
58 static Handle<String> LookupAsciiSymbol(const char* str) {
59 return LookupSymbol(CStrVector(str));
63 // String creation functions. Most of the string creation functions take
64 // a Heap::PretenureFlag argument to optionally request that they be
65 // allocated in the old generation. The pretenure flag defaults to
68 // Creates a new String object. There are two String encodings: ASCII and
69 // two byte. One should choose between the three string factory functions
70 // based on the encoding of the string buffer that the string is
72 // - ...FromAscii initializes the string from a buffer that is ASCII
73 // encoded (it does not check that the buffer is ASCII encoded) and
74 // the result will be ASCII encoded.
75 // - ...FromUtf8 initializes the string from a buffer that is UTF-8
76 // encoded. If the characters are all single-byte characters, the
77 // result will be ASCII encoded, otherwise it will converted to two
79 // - ...FromTwoByte initializes the string from a buffer that is two
80 // byte encoded. If the characters are all single-byte characters,
81 // the result will be converted to ASCII, otherwise it will be left as
84 // ASCII strings are pretenured when used as keys in the SourceCodeCache.
85 static Handle<String> NewStringFromAscii(
86 Vector<const char> str,
87 PretenureFlag pretenure = NOT_TENURED);
89 // UTF8 strings are pretenured when used for regexp literal patterns and
90 // flags in the parser.
91 static Handle<String> NewStringFromUtf8(
92 Vector<const char> str,
93 PretenureFlag pretenure = NOT_TENURED);
95 static Handle<String> NewStringFromTwoByte(Vector<const uc16> str,
96 PretenureFlag pretenure = NOT_TENURED);
98 // Allocates and partially initializes a TwoByte String. The characters of
99 // the string are uninitialized. Currently used in regexp code only, where
100 // they are pretenured.
101 static Handle<String> NewRawTwoByteString(
103 PretenureFlag pretenure = NOT_TENURED);
105 // Create a new cons string object which consists of a pair of strings.
106 static Handle<String> NewConsString(Handle<String> first,
107 Handle<String> second);
109 // Create a new sliced string object which represents a substring of a
111 static Handle<String> NewStringSlice(Handle<String> str,
115 // Creates a new external String object. There are two String encodings
116 // in the system: ASCII and two byte. Unlike other String types, it does
117 // not make sense to have a UTF-8 factory function for external strings,
118 // because we cannot change the underlying buffer.
119 static Handle<String> NewExternalStringFromAscii(
120 ExternalAsciiString::Resource* resource);
121 static Handle<String> NewExternalStringFromTwoByte(
122 ExternalTwoByteString::Resource* resource);
124 // Create a global (but otherwise uninitialized) context.
125 static Handle<Context> NewGlobalContext();
127 // Create a function context.
128 static Handle<Context> NewFunctionContext(int length,
129 Handle<JSFunction> closure);
131 // Create a 'with' context.
132 static Handle<Context> NewWithContext(Handle<Context> previous,
133 Handle<JSObject> extension,
134 bool is_catch_context);
136 // Return the Symbol matching the passed in string.
137 static Handle<String> SymbolFromString(Handle<String> value);
139 // Allocate a new struct. The struct is pretenured (allocated directly in
140 // the old generation).
141 static Handle<Struct> NewStruct(InstanceType type);
143 static Handle<AccessorInfo> NewAccessorInfo();
145 static Handle<Script> NewScript(Handle<String> source);
147 // Proxies are pretenured when allocated by the bootstrapper.
148 static Handle<Proxy> NewProxy(Address addr,
149 PretenureFlag pretenure = NOT_TENURED);
151 // Allocate a new proxy. The proxy is pretenured (allocated directly in
152 // the old generation).
153 static Handle<Proxy> NewProxy(const AccessorDescriptor* proxy);
155 static Handle<ByteArray> NewByteArray(int length,
156 PretenureFlag pretenure = NOT_TENURED);
158 static Handle<PixelArray> NewPixelArray(int length,
159 uint8_t* external_pointer,
160 PretenureFlag pretenure = NOT_TENURED);
162 static Handle<Map> NewMap(InstanceType type, int instance_size);
164 static Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
166 static Handle<Map> CopyMapDropDescriptors(Handle<Map> map);
168 // Copy the map adding more inobject properties if possible without
169 // overflowing the instance size.
170 static Handle<Map> CopyMap(Handle<Map> map, int extra_inobject_props);
172 static Handle<Map> CopyMapDropTransitions(Handle<Map> map);
174 static Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
176 // Numbers (eg, literals) are pretenured by the parser.
177 static Handle<Object> NewNumber(double value,
178 PretenureFlag pretenure = NOT_TENURED);
180 static Handle<Object> NewNumberFromInt(int value);
181 static Handle<Object> NewNumberFromUint(uint32_t value);
183 // These objects are used by the api to create env-independent data
184 // structures in the heap.
185 static Handle<JSObject> NewNeanderObject();
187 static Handle<JSObject> NewArgumentsObject(Handle<Object> callee, int length);
189 // JS objects are pretenured when allocated by the bootstrapper and
191 static Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
192 PretenureFlag pretenure = NOT_TENURED);
194 // Global objects are pretenured.
195 static Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor);
197 // JS objects are pretenured when allocated by the bootstrapper and
199 static Handle<JSObject> NewJSObjectFromMap(Handle<Map> map);
201 // JS arrays are pretenured when allocated by the parser.
202 static Handle<JSArray> NewJSArray(int init_length,
203 PretenureFlag pretenure = NOT_TENURED);
205 static Handle<JSArray> NewJSArrayWithElements(
206 Handle<FixedArray> elements,
207 PretenureFlag pretenure = NOT_TENURED);
209 static Handle<JSFunction> NewFunction(Handle<String> name,
210 Handle<Object> prototype);
212 static Handle<JSFunction> NewFunction(Handle<Object> super, bool is_global);
214 static Handle<JSFunction> NewFunctionFromBoilerplate(
215 Handle<JSFunction> boilerplate,
216 Handle<Context> context);
218 static Handle<Code> NewCode(const CodeDesc& desc,
219 ZoneScopeInfo* sinfo,
221 Handle<Object> self_reference);
223 static Handle<Code> CopyCode(Handle<Code> code);
225 static Handle<Object> ToObject(Handle<Object> object,
226 Handle<Context> global_context);
228 // Interface for creating error objects.
230 static Handle<Object> NewError(const char* maker, const char* type,
231 Handle<JSArray> args);
232 static Handle<Object> NewError(const char* maker, const char* type,
233 Vector< Handle<Object> > args);
234 static Handle<Object> NewError(const char* type,
235 Vector< Handle<Object> > args);
236 static Handle<Object> NewError(Handle<String> message);
237 static Handle<Object> NewError(const char* constructor,
238 Handle<String> message);
240 static Handle<Object> NewTypeError(const char* type,
241 Vector< Handle<Object> > args);
242 static Handle<Object> NewTypeError(Handle<String> message);
244 static Handle<Object> NewRangeError(const char* type,
245 Vector< Handle<Object> > args);
246 static Handle<Object> NewRangeError(Handle<String> message);
248 static Handle<Object> NewSyntaxError(const char* type, Handle<JSArray> args);
249 static Handle<Object> NewSyntaxError(Handle<String> message);
251 static Handle<Object> NewReferenceError(const char* type,
252 Vector< Handle<Object> > args);
253 static Handle<Object> NewReferenceError(Handle<String> message);
255 static Handle<Object> NewEvalError(const char* type,
256 Vector< Handle<Object> > args);
259 static Handle<JSFunction> NewFunction(Handle<String> name,
263 bool force_initial_map);
265 static Handle<JSFunction> NewFunctionBoilerplate(Handle<String> name,
266 int number_of_literals,
267 bool contains_array_literal,
270 static Handle<JSFunction> NewFunctionBoilerplate(Handle<String> name);
272 static Handle<JSFunction> NewFunction(Handle<Map> function_map,
273 Handle<SharedFunctionInfo> shared, Handle<Object> prototype);
276 static Handle<JSFunction> NewFunctionWithPrototype(Handle<String> name,
279 Handle<JSObject> prototype,
281 bool force_initial_map);
283 static Handle<DescriptorArray> CopyAppendProxyDescriptor(
284 Handle<DescriptorArray> array,
286 Handle<Object> value,
287 PropertyAttributes attributes);
289 static Handle<String> NumberToString(Handle<Object> number);
291 enum ApiInstanceType {
297 static Handle<JSFunction> CreateApiFunction(
298 Handle<FunctionTemplateInfo> data,
299 ApiInstanceType type = JavaScriptObject);
301 static Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
303 // Installs interceptors on the instance. 'desc' is a function template,
304 // and instance is an object instance created by the function of this
305 // function template.
306 static void ConfigureInstance(Handle<FunctionTemplateInfo> desc,
307 Handle<JSObject> instance,
308 bool* pending_exception);
310 #define ROOT_ACCESSOR(type, name, camel_name) \
311 static inline Handle<type> name() { \
312 return Handle<type>(bit_cast<type**, Object**>( \
313 &Heap::roots_[Heap::k##camel_name##RootIndex])); \
315 ROOT_LIST(ROOT_ACCESSOR)
316 #undef ROOT_ACCESSOR_ACCESSOR
318 #define SYMBOL_ACCESSOR(name, str) \
319 static inline Handle<String> name() { \
320 return Handle<String>(bit_cast<String**, Object**>( \
321 &Heap::roots_[Heap::k##name##RootIndex])); \
323 SYMBOL_LIST(SYMBOL_ACCESSOR)
324 #undef SYMBOL_ACCESSOR
326 static Handle<String> hidden_symbol() {
327 return Handle<String>(&Heap::hidden_symbol_);
330 static Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name);
332 static Handle<NumberDictionary> DictionaryAtNumberPut(
333 Handle<NumberDictionary>,
335 Handle<Object> value);
337 #ifdef ENABLE_DEBUGGER_SUPPORT
338 static Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
341 // Return a map using the map cache in the global context.
342 // The key the an ordered set of property names.
343 static Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
344 Handle<FixedArray> keys);
346 // Creates a new FixedArray that holds the data associated with the
347 // atom regexp and stores it in the regexp.
348 static void SetRegExpAtomData(Handle<JSRegExp> regexp,
350 Handle<String> source,
351 JSRegExp::Flags flags,
352 Handle<Object> match_pattern);
354 // Creates a new FixedArray that holds the data associated with the
355 // irregexp regexp and stores it in the regexp.
356 static void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
358 Handle<String> source,
359 JSRegExp::Flags flags,
363 static Handle<JSFunction> NewFunctionHelper(Handle<String> name,
364 Handle<Object> prototype);
366 static Handle<DescriptorArray> CopyAppendCallbackDescriptors(
367 Handle<DescriptorArray> array,
368 Handle<Object> descriptors);
370 static Handle<JSFunction> BaseNewFunctionFromBoilerplate(
371 Handle<JSFunction> boilerplate,
372 Handle<Map> function_map);
374 // Create a new map cache.
375 static Handle<MapCache> NewMapCache(int at_least_space_for);
377 // Update the map cache in the global context with (keys, map)
378 static Handle<MapCache> AddToMapCache(Handle<Context> context,
379 Handle<FixedArray> keys,
384 } } // namespace v8::internal
386 #endif // V8_FACTORY_H_