blob: 7547f7c452b3200e89c3dde36c29942272012f04 [file] [log] [blame]
Ben Murdochb0fe1622011-05-05 13:52:32 +01001// Copyright 2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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 Blocka7e24c12009-10-30 11:49:00 +000037// Interface for handle based allocation.
38
39class Factory : public AllStatic {
40 public:
41 // Allocate a new fixed array with undefined entries.
42 static Handle<FixedArray> NewFixedArray(
43 int size,
44 PretenureFlag pretenure = NOT_TENURED);
45
46 // Allocate a new fixed array with non-existing entries (the hole).
Steve Block6ded16b2010-05-10 14:33:55 +010047 static Handle<FixedArray> NewFixedArrayWithHoles(
48 int size,
49 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +000050
51 static Handle<NumberDictionary> NewNumberDictionary(int at_least_space_for);
52
53 static Handle<StringDictionary> NewStringDictionary(int at_least_space_for);
54
55 static Handle<DescriptorArray> NewDescriptorArray(int number_of_descriptors);
Ben Murdochb0fe1622011-05-05 13:52:32 +010056 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);
Steve Blocka7e24c12009-10-30 11:49:00 +000062
63 static Handle<String> LookupSymbol(Vector<const char> str);
Steve Block9fac8402011-05-12 15:51:54 +010064 static Handle<String> LookupAsciiSymbol(Vector<const char> str);
65 static Handle<String> LookupTwoByteSymbol(Vector<const uc16> str);
Steve Blocka7e24c12009-10-30 11:49:00 +000066 static Handle<String> LookupAsciiSymbol(const char* str) {
67 return LookupSymbol(CStrVector(str));
68 }
69
70
71 // String creation functions. Most of the string creation functions take
72 // a Heap::PretenureFlag argument to optionally request that they be
73 // allocated in the old generation. The pretenure flag defaults to
74 // DONT_TENURE.
75 //
76 // Creates a new String object. There are two String encodings: ASCII and
77 // two byte. One should choose between the three string factory functions
78 // based on the encoding of the string buffer that the string is
79 // initialized from.
80 // - ...FromAscii initializes the string from a buffer that is ASCII
81 // encoded (it does not check that the buffer is ASCII encoded) and
82 // the result will be ASCII encoded.
83 // - ...FromUtf8 initializes the string from a buffer that is UTF-8
84 // encoded. If the characters are all single-byte characters, the
85 // result will be ASCII encoded, otherwise it will converted to two
86 // byte.
87 // - ...FromTwoByte initializes the string from a buffer that is two
88 // byte encoded. If the characters are all single-byte characters,
89 // the result will be converted to ASCII, otherwise it will be left as
90 // two byte.
91 //
92 // ASCII strings are pretenured when used as keys in the SourceCodeCache.
93 static Handle<String> NewStringFromAscii(
94 Vector<const char> str,
95 PretenureFlag pretenure = NOT_TENURED);
96
97 // UTF8 strings are pretenured when used for regexp literal patterns and
98 // flags in the parser.
99 static Handle<String> NewStringFromUtf8(
100 Vector<const char> str,
101 PretenureFlag pretenure = NOT_TENURED);
102
Leon Clarkeac952652010-07-15 11:15:24 +0100103 static Handle<String> NewStringFromTwoByte(
104 Vector<const uc16> str,
Steve Blocka7e24c12009-10-30 11:49:00 +0000105 PretenureFlag pretenure = NOT_TENURED);
106
Leon Clarkeac952652010-07-15 11:15:24 +0100107 // Allocates and partially initializes an ASCII or TwoByte String. The
108 // characters of the string are uninitialized. Currently used in regexp code
109 // only, where they are pretenured.
110 static Handle<String> NewRawAsciiString(
111 int length,
112 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000113 static Handle<String> NewRawTwoByteString(
114 int length,
115 PretenureFlag pretenure = NOT_TENURED);
116
117 // Create a new cons string object which consists of a pair of strings.
118 static Handle<String> NewConsString(Handle<String> first,
119 Handle<String> second);
120
Steve Blockd0582a62009-12-15 09:54:21 +0000121 // Create a new string object which holds a substring of a string.
122 static Handle<String> NewSubString(Handle<String> str,
123 int begin,
124 int end);
Steve Blocka7e24c12009-10-30 11:49:00 +0000125
126 // Creates a new external String object. There are two String encodings
127 // in the system: ASCII and two byte. Unlike other String types, it does
128 // not make sense to have a UTF-8 factory function for external strings,
129 // because we cannot change the underlying buffer.
130 static Handle<String> NewExternalStringFromAscii(
131 ExternalAsciiString::Resource* resource);
132 static Handle<String> NewExternalStringFromTwoByte(
133 ExternalTwoByteString::Resource* resource);
134
135 // Create a global (but otherwise uninitialized) context.
136 static Handle<Context> NewGlobalContext();
137
138 // Create a function context.
139 static Handle<Context> NewFunctionContext(int length,
140 Handle<JSFunction> closure);
141
142 // Create a 'with' context.
143 static Handle<Context> NewWithContext(Handle<Context> previous,
144 Handle<JSObject> extension,
145 bool is_catch_context);
146
147 // Return the Symbol matching the passed in string.
148 static Handle<String> SymbolFromString(Handle<String> value);
149
150 // Allocate a new struct. The struct is pretenured (allocated directly in
151 // the old generation).
152 static Handle<Struct> NewStruct(InstanceType type);
153
154 static Handle<AccessorInfo> NewAccessorInfo();
155
156 static Handle<Script> NewScript(Handle<String> source);
157
158 // Proxies are pretenured when allocated by the bootstrapper.
159 static Handle<Proxy> NewProxy(Address addr,
160 PretenureFlag pretenure = NOT_TENURED);
161
162 // Allocate a new proxy. The proxy is pretenured (allocated directly in
163 // the old generation).
164 static Handle<Proxy> NewProxy(const AccessorDescriptor* proxy);
165
166 static Handle<ByteArray> NewByteArray(int length,
167 PretenureFlag pretenure = NOT_TENURED);
168
Steve Block3ce2e202009-11-05 08:53:23 +0000169 static Handle<PixelArray> NewPixelArray(
170 int length,
Steve Blocka7e24c12009-10-30 11:49:00 +0000171 uint8_t* external_pointer,
172 PretenureFlag pretenure = NOT_TENURED);
173
Steve Block3ce2e202009-11-05 08:53:23 +0000174 static Handle<ExternalArray> NewExternalArray(
175 int length,
176 ExternalArrayType array_type,
177 void* external_pointer,
178 PretenureFlag pretenure = NOT_TENURED);
179
Ben Murdochb0fe1622011-05-05 13:52:32 +0100180 static Handle<JSGlobalPropertyCell> NewJSGlobalPropertyCell(
181 Handle<Object> value);
182
Steve Blocka7e24c12009-10-30 11:49:00 +0000183 static Handle<Map> NewMap(InstanceType type, int instance_size);
184
185 static Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
186
187 static Handle<Map> CopyMapDropDescriptors(Handle<Map> map);
188
189 // Copy the map adding more inobject properties if possible without
190 // overflowing the instance size.
191 static Handle<Map> CopyMap(Handle<Map> map, int extra_inobject_props);
192
193 static Handle<Map> CopyMapDropTransitions(Handle<Map> map);
194
Steve Block8defd9f2010-07-08 12:39:36 +0100195 static Handle<Map> GetFastElementsMap(Handle<Map> map);
196
197 static Handle<Map> GetSlowElementsMap(Handle<Map> map);
198
Steve Block1e0659c2011-05-24 12:43:12 +0100199 static Handle<Map> GetPixelArrayElementsMap(Handle<Map> map);
200
Steve Blocka7e24c12009-10-30 11:49:00 +0000201 static Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
202
203 // Numbers (eg, literals) are pretenured by the parser.
204 static Handle<Object> NewNumber(double value,
205 PretenureFlag pretenure = NOT_TENURED);
206
207 static Handle<Object> NewNumberFromInt(int value);
208 static Handle<Object> NewNumberFromUint(uint32_t value);
209
210 // These objects are used by the api to create env-independent data
211 // structures in the heap.
212 static Handle<JSObject> NewNeanderObject();
213
214 static Handle<JSObject> NewArgumentsObject(Handle<Object> callee, int length);
215
216 // JS objects are pretenured when allocated by the bootstrapper and
217 // runtime.
218 static Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
219 PretenureFlag pretenure = NOT_TENURED);
220
221 // Global objects are pretenured.
222 static Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor);
223
224 // JS objects are pretenured when allocated by the bootstrapper and
225 // runtime.
226 static Handle<JSObject> NewJSObjectFromMap(Handle<Map> map);
227
228 // JS arrays are pretenured when allocated by the parser.
229 static Handle<JSArray> NewJSArray(int init_length,
230 PretenureFlag pretenure = NOT_TENURED);
231
232 static Handle<JSArray> NewJSArrayWithElements(
233 Handle<FixedArray> elements,
234 PretenureFlag pretenure = NOT_TENURED);
235
236 static Handle<JSFunction> NewFunction(Handle<String> name,
237 Handle<Object> prototype);
238
Steve Block6ded16b2010-05-10 14:33:55 +0100239 static Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name);
240
Steve Blocka7e24c12009-10-30 11:49:00 +0000241 static Handle<JSFunction> NewFunction(Handle<Object> super, bool is_global);
242
Steve Block6ded16b2010-05-10 14:33:55 +0100243 static Handle<JSFunction> BaseNewFunctionFromSharedFunctionInfo(
244 Handle<SharedFunctionInfo> function_info,
245 Handle<Map> function_map,
246 PretenureFlag pretenure);
247
248 static Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
249 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000250 Handle<Context> context,
251 PretenureFlag pretenure = TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000252
253 static Handle<Code> NewCode(const CodeDesc& desc,
Steve Blocka7e24c12009-10-30 11:49:00 +0000254 Code::Flags flags,
255 Handle<Object> self_reference);
256
257 static Handle<Code> CopyCode(Handle<Code> code);
258
Steve Block6ded16b2010-05-10 14:33:55 +0100259 static Handle<Code> CopyCode(Handle<Code> code, Vector<byte> reloc_info);
260
Leon Clarkee46be812010-01-19 14:06:41 +0000261 static Handle<Object> ToObject(Handle<Object> object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000262 static Handle<Object> ToObject(Handle<Object> object,
263 Handle<Context> global_context);
264
265 // Interface for creating error objects.
266
267 static Handle<Object> NewError(const char* maker, const char* type,
268 Handle<JSArray> args);
269 static Handle<Object> NewError(const char* maker, const char* type,
270 Vector< Handle<Object> > args);
271 static Handle<Object> NewError(const char* type,
272 Vector< Handle<Object> > args);
273 static Handle<Object> NewError(Handle<String> message);
274 static Handle<Object> NewError(const char* constructor,
275 Handle<String> message);
276
277 static Handle<Object> NewTypeError(const char* type,
278 Vector< Handle<Object> > args);
279 static Handle<Object> NewTypeError(Handle<String> message);
280
281 static Handle<Object> NewRangeError(const char* type,
282 Vector< Handle<Object> > args);
283 static Handle<Object> NewRangeError(Handle<String> message);
284
285 static Handle<Object> NewSyntaxError(const char* type, Handle<JSArray> args);
286 static Handle<Object> NewSyntaxError(Handle<String> message);
287
288 static Handle<Object> NewReferenceError(const char* type,
289 Vector< Handle<Object> > args);
290 static Handle<Object> NewReferenceError(Handle<String> message);
291
292 static Handle<Object> NewEvalError(const char* type,
293 Vector< Handle<Object> > args);
294
295
296 static Handle<JSFunction> NewFunction(Handle<String> name,
297 InstanceType type,
298 int instance_size,
299 Handle<Code> code,
300 bool force_initial_map);
301
Steve Blocka7e24c12009-10-30 11:49:00 +0000302 static Handle<JSFunction> NewFunction(Handle<Map> function_map,
303 Handle<SharedFunctionInfo> shared, Handle<Object> prototype);
304
305
306 static Handle<JSFunction> NewFunctionWithPrototype(Handle<String> name,
307 InstanceType type,
308 int instance_size,
309 Handle<JSObject> prototype,
310 Handle<Code> code,
311 bool force_initial_map);
312
Steve Block6ded16b2010-05-10 14:33:55 +0100313 static Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name,
314 Handle<Code> code);
315
Steve Blocka7e24c12009-10-30 11:49:00 +0000316 static Handle<DescriptorArray> CopyAppendProxyDescriptor(
317 Handle<DescriptorArray> array,
318 Handle<String> key,
319 Handle<Object> value,
320 PropertyAttributes attributes);
321
322 static Handle<String> NumberToString(Handle<Object> number);
323
324 enum ApiInstanceType {
325 JavaScriptObject,
326 InnerGlobalObject,
327 OuterGlobalObject
328 };
329
330 static Handle<JSFunction> CreateApiFunction(
331 Handle<FunctionTemplateInfo> data,
332 ApiInstanceType type = JavaScriptObject);
333
334 static Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
335
336 // Installs interceptors on the instance. 'desc' is a function template,
337 // and instance is an object instance created by the function of this
338 // function template.
339 static void ConfigureInstance(Handle<FunctionTemplateInfo> desc,
340 Handle<JSObject> instance,
341 bool* pending_exception);
342
343#define ROOT_ACCESSOR(type, name, camel_name) \
344 static inline Handle<type> name() { \
Iain Merrick75681382010-08-19 15:07:18 +0100345 return Handle<type>(BitCast<type**>( \
Steve Blocka7e24c12009-10-30 11:49:00 +0000346 &Heap::roots_[Heap::k##camel_name##RootIndex])); \
347 }
348 ROOT_LIST(ROOT_ACCESSOR)
349#undef ROOT_ACCESSOR_ACCESSOR
350
351#define SYMBOL_ACCESSOR(name, str) \
352 static inline Handle<String> name() { \
Iain Merrick75681382010-08-19 15:07:18 +0100353 return Handle<String>(BitCast<String**>( \
Steve Blocka7e24c12009-10-30 11:49:00 +0000354 &Heap::roots_[Heap::k##name##RootIndex])); \
355 }
356 SYMBOL_LIST(SYMBOL_ACCESSOR)
357#undef SYMBOL_ACCESSOR
358
359 static Handle<String> hidden_symbol() {
360 return Handle<String>(&Heap::hidden_symbol_);
361 }
362
Steve Block6ded16b2010-05-10 14:33:55 +0100363 static Handle<SharedFunctionInfo> NewSharedFunctionInfo(
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100364 Handle<String> name,
365 int number_of_literals,
366 Handle<Code> code,
367 Handle<SerializedScopeInfo> scope_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000368 static Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name);
369
Steve Block1e0659c2011-05-24 12:43:12 +0100370 static Handle<JSMessageObject> NewJSMessageObject(
371 Handle<String> type,
372 Handle<JSArray> arguments,
373 int start_position,
374 int end_position,
375 Handle<Object> script,
376 Handle<Object> stack_trace,
377 Handle<Object> stack_frames);
378
Steve Blocka7e24c12009-10-30 11:49:00 +0000379 static Handle<NumberDictionary> DictionaryAtNumberPut(
380 Handle<NumberDictionary>,
381 uint32_t key,
382 Handle<Object> value);
383
384#ifdef ENABLE_DEBUGGER_SUPPORT
385 static Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
386#endif
387
388 // Return a map using the map cache in the global context.
389 // The key the an ordered set of property names.
390 static Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
391 Handle<FixedArray> keys);
392
393 // Creates a new FixedArray that holds the data associated with the
394 // atom regexp and stores it in the regexp.
395 static void SetRegExpAtomData(Handle<JSRegExp> regexp,
396 JSRegExp::Type type,
397 Handle<String> source,
398 JSRegExp::Flags flags,
399 Handle<Object> match_pattern);
400
401 // Creates a new FixedArray that holds the data associated with the
402 // irregexp regexp and stores it in the regexp.
403 static void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
404 JSRegExp::Type type,
405 Handle<String> source,
406 JSRegExp::Flags flags,
407 int capture_count);
408
409 private:
410 static Handle<JSFunction> NewFunctionHelper(Handle<String> name,
411 Handle<Object> prototype);
412
Steve Block6ded16b2010-05-10 14:33:55 +0100413 static Handle<JSFunction> NewFunctionWithoutPrototypeHelper(
414 Handle<String> name);
415
Steve Blocka7e24c12009-10-30 11:49:00 +0000416 static Handle<DescriptorArray> CopyAppendCallbackDescriptors(
417 Handle<DescriptorArray> array,
418 Handle<Object> descriptors);
419
Steve Blocka7e24c12009-10-30 11:49:00 +0000420 // Create a new map cache.
421 static Handle<MapCache> NewMapCache(int at_least_space_for);
422
423 // Update the map cache in the global context with (keys, map)
424 static Handle<MapCache> AddToMapCache(Handle<Context> context,
425 Handle<FixedArray> keys,
426 Handle<Map> map);
427};
428
429
430} } // namespace v8::internal
431
432#endif // V8_FACTORY_H_