blob: 8a190fa09a900943b175072ac12cf43e7bf7e4b4 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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
4// met:
5//
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.
15//
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.
27
28#ifndef V8_FACTORY_H_
29#define V8_FACTORY_H_
30
31#include "globals.h"
32#include "heap.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000033
34namespace v8 {
35namespace internal {
36
Steve Block6ded16b2010-05-10 14:33:55 +010037// Forward declarations.
38class ZoneScopeInfo;
Steve Blocka7e24c12009-10-30 11:49:00 +000039
40// Interface for handle based allocation.
41
42class Factory : public AllStatic {
43 public:
44 // Allocate a new fixed array with undefined entries.
45 static Handle<FixedArray> NewFixedArray(
46 int size,
47 PretenureFlag pretenure = NOT_TENURED);
48
49 // Allocate a new fixed array with non-existing entries (the hole).
Steve Block6ded16b2010-05-10 14:33:55 +010050 static Handle<FixedArray> NewFixedArrayWithHoles(
51 int size,
52 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +000053
54 static Handle<NumberDictionary> NewNumberDictionary(int at_least_space_for);
55
56 static Handle<StringDictionary> NewStringDictionary(int at_least_space_for);
57
58 static Handle<DescriptorArray> NewDescriptorArray(int number_of_descriptors);
59
60 static Handle<String> LookupSymbol(Vector<const char> str);
61 static Handle<String> LookupAsciiSymbol(const char* str) {
62 return LookupSymbol(CStrVector(str));
63 }
64
65
66 // String creation functions. Most of the string creation functions take
67 // a Heap::PretenureFlag argument to optionally request that they be
68 // allocated in the old generation. The pretenure flag defaults to
69 // DONT_TENURE.
70 //
71 // Creates a new String object. There are two String encodings: ASCII and
72 // two byte. One should choose between the three string factory functions
73 // based on the encoding of the string buffer that the string is
74 // initialized from.
75 // - ...FromAscii initializes the string from a buffer that is ASCII
76 // encoded (it does not check that the buffer is ASCII encoded) and
77 // the result will be ASCII encoded.
78 // - ...FromUtf8 initializes the string from a buffer that is UTF-8
79 // encoded. If the characters are all single-byte characters, the
80 // result will be ASCII encoded, otherwise it will converted to two
81 // byte.
82 // - ...FromTwoByte initializes the string from a buffer that is two
83 // byte encoded. If the characters are all single-byte characters,
84 // the result will be converted to ASCII, otherwise it will be left as
85 // two byte.
86 //
87 // ASCII strings are pretenured when used as keys in the SourceCodeCache.
88 static Handle<String> NewStringFromAscii(
89 Vector<const char> str,
90 PretenureFlag pretenure = NOT_TENURED);
91
92 // UTF8 strings are pretenured when used for regexp literal patterns and
93 // flags in the parser.
94 static Handle<String> NewStringFromUtf8(
95 Vector<const char> str,
96 PretenureFlag pretenure = NOT_TENURED);
97
98 static Handle<String> NewStringFromTwoByte(Vector<const uc16> str,
99 PretenureFlag pretenure = NOT_TENURED);
100
101 // Allocates and partially initializes a TwoByte String. The characters of
102 // the string are uninitialized. Currently used in regexp code only, where
103 // they are pretenured.
104 static Handle<String> NewRawTwoByteString(
105 int length,
106 PretenureFlag pretenure = NOT_TENURED);
107
108 // Create a new cons string object which consists of a pair of strings.
109 static Handle<String> NewConsString(Handle<String> first,
110 Handle<String> second);
111
Steve Blockd0582a62009-12-15 09:54:21 +0000112 // Create a new string object which holds a substring of a string.
113 static Handle<String> NewSubString(Handle<String> str,
114 int begin,
115 int end);
Steve Blocka7e24c12009-10-30 11:49:00 +0000116
117 // Creates a new external String object. There are two String encodings
118 // in the system: ASCII and two byte. Unlike other String types, it does
119 // not make sense to have a UTF-8 factory function for external strings,
120 // because we cannot change the underlying buffer.
121 static Handle<String> NewExternalStringFromAscii(
122 ExternalAsciiString::Resource* resource);
123 static Handle<String> NewExternalStringFromTwoByte(
124 ExternalTwoByteString::Resource* resource);
125
126 // Create a global (but otherwise uninitialized) context.
127 static Handle<Context> NewGlobalContext();
128
129 // Create a function context.
130 static Handle<Context> NewFunctionContext(int length,
131 Handle<JSFunction> closure);
132
133 // Create a 'with' context.
134 static Handle<Context> NewWithContext(Handle<Context> previous,
135 Handle<JSObject> extension,
136 bool is_catch_context);
137
138 // Return the Symbol matching the passed in string.
139 static Handle<String> SymbolFromString(Handle<String> value);
140
141 // Allocate a new struct. The struct is pretenured (allocated directly in
142 // the old generation).
143 static Handle<Struct> NewStruct(InstanceType type);
144
145 static Handle<AccessorInfo> NewAccessorInfo();
146
147 static Handle<Script> NewScript(Handle<String> source);
148
149 // Proxies are pretenured when allocated by the bootstrapper.
150 static Handle<Proxy> NewProxy(Address addr,
151 PretenureFlag pretenure = NOT_TENURED);
152
153 // Allocate a new proxy. The proxy is pretenured (allocated directly in
154 // the old generation).
155 static Handle<Proxy> NewProxy(const AccessorDescriptor* proxy);
156
157 static Handle<ByteArray> NewByteArray(int length,
158 PretenureFlag pretenure = NOT_TENURED);
159
Steve Block3ce2e202009-11-05 08:53:23 +0000160 static Handle<PixelArray> NewPixelArray(
161 int length,
Steve Blocka7e24c12009-10-30 11:49:00 +0000162 uint8_t* external_pointer,
163 PretenureFlag pretenure = NOT_TENURED);
164
Steve Block3ce2e202009-11-05 08:53:23 +0000165 static Handle<ExternalArray> NewExternalArray(
166 int length,
167 ExternalArrayType array_type,
168 void* external_pointer,
169 PretenureFlag pretenure = NOT_TENURED);
170
Steve Blocka7e24c12009-10-30 11:49:00 +0000171 static Handle<Map> NewMap(InstanceType type, int instance_size);
172
173 static Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
174
175 static Handle<Map> CopyMapDropDescriptors(Handle<Map> map);
176
177 // Copy the map adding more inobject properties if possible without
178 // overflowing the instance size.
179 static Handle<Map> CopyMap(Handle<Map> map, int extra_inobject_props);
180
181 static Handle<Map> CopyMapDropTransitions(Handle<Map> map);
182
183 static Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
184
185 // Numbers (eg, literals) are pretenured by the parser.
186 static Handle<Object> NewNumber(double value,
187 PretenureFlag pretenure = NOT_TENURED);
188
189 static Handle<Object> NewNumberFromInt(int value);
190 static Handle<Object> NewNumberFromUint(uint32_t value);
191
192 // These objects are used by the api to create env-independent data
193 // structures in the heap.
194 static Handle<JSObject> NewNeanderObject();
195
196 static Handle<JSObject> NewArgumentsObject(Handle<Object> callee, int length);
197
198 // JS objects are pretenured when allocated by the bootstrapper and
199 // runtime.
200 static Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
201 PretenureFlag pretenure = NOT_TENURED);
202
203 // Global objects are pretenured.
204 static Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor);
205
206 // JS objects are pretenured when allocated by the bootstrapper and
207 // runtime.
208 static Handle<JSObject> NewJSObjectFromMap(Handle<Map> map);
209
210 // JS arrays are pretenured when allocated by the parser.
211 static Handle<JSArray> NewJSArray(int init_length,
212 PretenureFlag pretenure = NOT_TENURED);
213
214 static Handle<JSArray> NewJSArrayWithElements(
215 Handle<FixedArray> elements,
216 PretenureFlag pretenure = NOT_TENURED);
217
218 static Handle<JSFunction> NewFunction(Handle<String> name,
219 Handle<Object> prototype);
220
Steve Block6ded16b2010-05-10 14:33:55 +0100221 static Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name);
222
Steve Blocka7e24c12009-10-30 11:49:00 +0000223 static Handle<JSFunction> NewFunction(Handle<Object> super, bool is_global);
224
Steve Block6ded16b2010-05-10 14:33:55 +0100225 static Handle<JSFunction> BaseNewFunctionFromSharedFunctionInfo(
226 Handle<SharedFunctionInfo> function_info,
227 Handle<Map> function_map,
228 PretenureFlag pretenure);
229
230 static Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
231 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000232 Handle<Context> context,
233 PretenureFlag pretenure = TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000234
235 static Handle<Code> NewCode(const CodeDesc& desc,
236 ZoneScopeInfo* sinfo,
237 Code::Flags flags,
238 Handle<Object> self_reference);
239
240 static Handle<Code> CopyCode(Handle<Code> code);
241
Steve Block6ded16b2010-05-10 14:33:55 +0100242 static Handle<Code> CopyCode(Handle<Code> code, Vector<byte> reloc_info);
243
Leon Clarkee46be812010-01-19 14:06:41 +0000244 static Handle<Object> ToObject(Handle<Object> object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000245 static Handle<Object> ToObject(Handle<Object> object,
246 Handle<Context> global_context);
247
248 // Interface for creating error objects.
249
250 static Handle<Object> NewError(const char* maker, const char* type,
251 Handle<JSArray> args);
252 static Handle<Object> NewError(const char* maker, const char* type,
253 Vector< Handle<Object> > args);
254 static Handle<Object> NewError(const char* type,
255 Vector< Handle<Object> > args);
256 static Handle<Object> NewError(Handle<String> message);
257 static Handle<Object> NewError(const char* constructor,
258 Handle<String> message);
259
260 static Handle<Object> NewTypeError(const char* type,
261 Vector< Handle<Object> > args);
262 static Handle<Object> NewTypeError(Handle<String> message);
263
264 static Handle<Object> NewRangeError(const char* type,
265 Vector< Handle<Object> > args);
266 static Handle<Object> NewRangeError(Handle<String> message);
267
268 static Handle<Object> NewSyntaxError(const char* type, Handle<JSArray> args);
269 static Handle<Object> NewSyntaxError(Handle<String> message);
270
271 static Handle<Object> NewReferenceError(const char* type,
272 Vector< Handle<Object> > args);
273 static Handle<Object> NewReferenceError(Handle<String> message);
274
275 static Handle<Object> NewEvalError(const char* type,
276 Vector< Handle<Object> > args);
277
278
279 static Handle<JSFunction> NewFunction(Handle<String> name,
280 InstanceType type,
281 int instance_size,
282 Handle<Code> code,
283 bool force_initial_map);
284
Steve Blocka7e24c12009-10-30 11:49:00 +0000285 static Handle<JSFunction> NewFunction(Handle<Map> function_map,
286 Handle<SharedFunctionInfo> shared, Handle<Object> prototype);
287
288
289 static Handle<JSFunction> NewFunctionWithPrototype(Handle<String> name,
290 InstanceType type,
291 int instance_size,
292 Handle<JSObject> prototype,
293 Handle<Code> code,
294 bool force_initial_map);
295
Steve Block6ded16b2010-05-10 14:33:55 +0100296 static Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name,
297 Handle<Code> code);
298
Steve Blocka7e24c12009-10-30 11:49:00 +0000299 static Handle<DescriptorArray> CopyAppendProxyDescriptor(
300 Handle<DescriptorArray> array,
301 Handle<String> key,
302 Handle<Object> value,
303 PropertyAttributes attributes);
304
305 static Handle<String> NumberToString(Handle<Object> number);
306
307 enum ApiInstanceType {
308 JavaScriptObject,
309 InnerGlobalObject,
310 OuterGlobalObject
311 };
312
313 static Handle<JSFunction> CreateApiFunction(
314 Handle<FunctionTemplateInfo> data,
315 ApiInstanceType type = JavaScriptObject);
316
317 static Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
318
319 // Installs interceptors on the instance. 'desc' is a function template,
320 // and instance is an object instance created by the function of this
321 // function template.
322 static void ConfigureInstance(Handle<FunctionTemplateInfo> desc,
323 Handle<JSObject> instance,
324 bool* pending_exception);
325
326#define ROOT_ACCESSOR(type, name, camel_name) \
327 static inline Handle<type> name() { \
Steve Block6ded16b2010-05-10 14:33:55 +0100328 return Handle<type>(BitCast<type**, Object**>( \
Steve Blocka7e24c12009-10-30 11:49:00 +0000329 &Heap::roots_[Heap::k##camel_name##RootIndex])); \
330 }
331 ROOT_LIST(ROOT_ACCESSOR)
332#undef ROOT_ACCESSOR_ACCESSOR
333
334#define SYMBOL_ACCESSOR(name, str) \
335 static inline Handle<String> name() { \
Steve Block6ded16b2010-05-10 14:33:55 +0100336 return Handle<String>(BitCast<String**, Object**>( \
Steve Blocka7e24c12009-10-30 11:49:00 +0000337 &Heap::roots_[Heap::k##name##RootIndex])); \
338 }
339 SYMBOL_LIST(SYMBOL_ACCESSOR)
340#undef SYMBOL_ACCESSOR
341
342 static Handle<String> hidden_symbol() {
343 return Handle<String>(&Heap::hidden_symbol_);
344 }
345
Steve Block6ded16b2010-05-10 14:33:55 +0100346 static Handle<SharedFunctionInfo> NewSharedFunctionInfo(
347 Handle<String> name, int number_of_literals, Handle<Code> code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000348 static Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name);
349
350 static Handle<NumberDictionary> DictionaryAtNumberPut(
351 Handle<NumberDictionary>,
352 uint32_t key,
353 Handle<Object> value);
354
355#ifdef ENABLE_DEBUGGER_SUPPORT
356 static Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
357#endif
358
359 // Return a map using the map cache in the global context.
360 // The key the an ordered set of property names.
361 static Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
362 Handle<FixedArray> keys);
363
364 // Creates a new FixedArray that holds the data associated with the
365 // atom regexp and stores it in the regexp.
366 static void SetRegExpAtomData(Handle<JSRegExp> regexp,
367 JSRegExp::Type type,
368 Handle<String> source,
369 JSRegExp::Flags flags,
370 Handle<Object> match_pattern);
371
372 // Creates a new FixedArray that holds the data associated with the
373 // irregexp regexp and stores it in the regexp.
374 static void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
375 JSRegExp::Type type,
376 Handle<String> source,
377 JSRegExp::Flags flags,
378 int capture_count);
379
380 private:
381 static Handle<JSFunction> NewFunctionHelper(Handle<String> name,
382 Handle<Object> prototype);
383
Steve Block6ded16b2010-05-10 14:33:55 +0100384 static Handle<JSFunction> NewFunctionWithoutPrototypeHelper(
385 Handle<String> name);
386
Steve Blocka7e24c12009-10-30 11:49:00 +0000387 static Handle<DescriptorArray> CopyAppendCallbackDescriptors(
388 Handle<DescriptorArray> array,
389 Handle<Object> descriptors);
390
Steve Blocka7e24c12009-10-30 11:49:00 +0000391 // Create a new map cache.
392 static Handle<MapCache> NewMapCache(int at_least_space_for);
393
394 // Update the map cache in the global context with (keys, map)
395 static Handle<MapCache> AddToMapCache(Handle<Context> context,
396 Handle<FixedArray> keys,
397 Handle<Map> map);
398};
399
400
401} } // namespace v8::internal
402
403#endif // V8_FACTORY_H_