blob: a5e159123847f077d05e37317f98903810ba81a5 [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 Blocka7e24c12009-10-30 11:49:00 +0000199 static Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
200
201 // Numbers (eg, literals) are pretenured by the parser.
202 static Handle<Object> NewNumber(double value,
203 PretenureFlag pretenure = NOT_TENURED);
204
205 static Handle<Object> NewNumberFromInt(int value);
206 static Handle<Object> NewNumberFromUint(uint32_t value);
207
208 // These objects are used by the api to create env-independent data
209 // structures in the heap.
210 static Handle<JSObject> NewNeanderObject();
211
212 static Handle<JSObject> NewArgumentsObject(Handle<Object> callee, int length);
213
214 // JS objects are pretenured when allocated by the bootstrapper and
215 // runtime.
216 static Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
217 PretenureFlag pretenure = NOT_TENURED);
218
219 // Global objects are pretenured.
220 static Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor);
221
222 // JS objects are pretenured when allocated by the bootstrapper and
223 // runtime.
224 static Handle<JSObject> NewJSObjectFromMap(Handle<Map> map);
225
226 // JS arrays are pretenured when allocated by the parser.
227 static Handle<JSArray> NewJSArray(int init_length,
228 PretenureFlag pretenure = NOT_TENURED);
229
230 static Handle<JSArray> NewJSArrayWithElements(
231 Handle<FixedArray> elements,
232 PretenureFlag pretenure = NOT_TENURED);
233
234 static Handle<JSFunction> NewFunction(Handle<String> name,
235 Handle<Object> prototype);
236
Steve Block6ded16b2010-05-10 14:33:55 +0100237 static Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name);
238
Steve Blocka7e24c12009-10-30 11:49:00 +0000239 static Handle<JSFunction> NewFunction(Handle<Object> super, bool is_global);
240
Steve Block6ded16b2010-05-10 14:33:55 +0100241 static Handle<JSFunction> BaseNewFunctionFromSharedFunctionInfo(
242 Handle<SharedFunctionInfo> function_info,
243 Handle<Map> function_map,
244 PretenureFlag pretenure);
245
246 static Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
247 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000248 Handle<Context> context,
249 PretenureFlag pretenure = TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000250
251 static Handle<Code> NewCode(const CodeDesc& desc,
Steve Blocka7e24c12009-10-30 11:49:00 +0000252 Code::Flags flags,
253 Handle<Object> self_reference);
254
255 static Handle<Code> CopyCode(Handle<Code> code);
256
Steve Block6ded16b2010-05-10 14:33:55 +0100257 static Handle<Code> CopyCode(Handle<Code> code, Vector<byte> reloc_info);
258
Leon Clarkee46be812010-01-19 14:06:41 +0000259 static Handle<Object> ToObject(Handle<Object> object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000260 static Handle<Object> ToObject(Handle<Object> object,
261 Handle<Context> global_context);
262
263 // Interface for creating error objects.
264
265 static Handle<Object> NewError(const char* maker, const char* type,
266 Handle<JSArray> args);
267 static Handle<Object> NewError(const char* maker, const char* type,
268 Vector< Handle<Object> > args);
269 static Handle<Object> NewError(const char* type,
270 Vector< Handle<Object> > args);
271 static Handle<Object> NewError(Handle<String> message);
272 static Handle<Object> NewError(const char* constructor,
273 Handle<String> message);
274
275 static Handle<Object> NewTypeError(const char* type,
276 Vector< Handle<Object> > args);
277 static Handle<Object> NewTypeError(Handle<String> message);
278
279 static Handle<Object> NewRangeError(const char* type,
280 Vector< Handle<Object> > args);
281 static Handle<Object> NewRangeError(Handle<String> message);
282
283 static Handle<Object> NewSyntaxError(const char* type, Handle<JSArray> args);
284 static Handle<Object> NewSyntaxError(Handle<String> message);
285
286 static Handle<Object> NewReferenceError(const char* type,
287 Vector< Handle<Object> > args);
288 static Handle<Object> NewReferenceError(Handle<String> message);
289
290 static Handle<Object> NewEvalError(const char* type,
291 Vector< Handle<Object> > args);
292
293
294 static Handle<JSFunction> NewFunction(Handle<String> name,
295 InstanceType type,
296 int instance_size,
297 Handle<Code> code,
298 bool force_initial_map);
299
Steve Blocka7e24c12009-10-30 11:49:00 +0000300 static Handle<JSFunction> NewFunction(Handle<Map> function_map,
301 Handle<SharedFunctionInfo> shared, Handle<Object> prototype);
302
303
304 static Handle<JSFunction> NewFunctionWithPrototype(Handle<String> name,
305 InstanceType type,
306 int instance_size,
307 Handle<JSObject> prototype,
308 Handle<Code> code,
309 bool force_initial_map);
310
Steve Block6ded16b2010-05-10 14:33:55 +0100311 static Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name,
312 Handle<Code> code);
313
Steve Blocka7e24c12009-10-30 11:49:00 +0000314 static Handle<DescriptorArray> CopyAppendProxyDescriptor(
315 Handle<DescriptorArray> array,
316 Handle<String> key,
317 Handle<Object> value,
318 PropertyAttributes attributes);
319
320 static Handle<String> NumberToString(Handle<Object> number);
321
322 enum ApiInstanceType {
323 JavaScriptObject,
324 InnerGlobalObject,
325 OuterGlobalObject
326 };
327
328 static Handle<JSFunction> CreateApiFunction(
329 Handle<FunctionTemplateInfo> data,
330 ApiInstanceType type = JavaScriptObject);
331
332 static Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
333
334 // Installs interceptors on the instance. 'desc' is a function template,
335 // and instance is an object instance created by the function of this
336 // function template.
337 static void ConfigureInstance(Handle<FunctionTemplateInfo> desc,
338 Handle<JSObject> instance,
339 bool* pending_exception);
340
341#define ROOT_ACCESSOR(type, name, camel_name) \
342 static inline Handle<type> name() { \
Iain Merrick75681382010-08-19 15:07:18 +0100343 return Handle<type>(BitCast<type**>( \
Steve Blocka7e24c12009-10-30 11:49:00 +0000344 &Heap::roots_[Heap::k##camel_name##RootIndex])); \
345 }
346 ROOT_LIST(ROOT_ACCESSOR)
347#undef ROOT_ACCESSOR_ACCESSOR
348
349#define SYMBOL_ACCESSOR(name, str) \
350 static inline Handle<String> name() { \
Iain Merrick75681382010-08-19 15:07:18 +0100351 return Handle<String>(BitCast<String**>( \
Steve Blocka7e24c12009-10-30 11:49:00 +0000352 &Heap::roots_[Heap::k##name##RootIndex])); \
353 }
354 SYMBOL_LIST(SYMBOL_ACCESSOR)
355#undef SYMBOL_ACCESSOR
356
357 static Handle<String> hidden_symbol() {
358 return Handle<String>(&Heap::hidden_symbol_);
359 }
360
Steve Block6ded16b2010-05-10 14:33:55 +0100361 static Handle<SharedFunctionInfo> NewSharedFunctionInfo(
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100362 Handle<String> name,
363 int number_of_literals,
364 Handle<Code> code,
365 Handle<SerializedScopeInfo> scope_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000366 static Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name);
367
368 static Handle<NumberDictionary> DictionaryAtNumberPut(
369 Handle<NumberDictionary>,
370 uint32_t key,
371 Handle<Object> value);
372
373#ifdef ENABLE_DEBUGGER_SUPPORT
374 static Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
375#endif
376
377 // Return a map using the map cache in the global context.
378 // The key the an ordered set of property names.
379 static Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
380 Handle<FixedArray> keys);
381
382 // Creates a new FixedArray that holds the data associated with the
383 // atom regexp and stores it in the regexp.
384 static void SetRegExpAtomData(Handle<JSRegExp> regexp,
385 JSRegExp::Type type,
386 Handle<String> source,
387 JSRegExp::Flags flags,
388 Handle<Object> match_pattern);
389
390 // Creates a new FixedArray that holds the data associated with the
391 // irregexp regexp and stores it in the regexp.
392 static void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
393 JSRegExp::Type type,
394 Handle<String> source,
395 JSRegExp::Flags flags,
396 int capture_count);
397
398 private:
399 static Handle<JSFunction> NewFunctionHelper(Handle<String> name,
400 Handle<Object> prototype);
401
Steve Block6ded16b2010-05-10 14:33:55 +0100402 static Handle<JSFunction> NewFunctionWithoutPrototypeHelper(
403 Handle<String> name);
404
Steve Blocka7e24c12009-10-30 11:49:00 +0000405 static Handle<DescriptorArray> CopyAppendCallbackDescriptors(
406 Handle<DescriptorArray> array,
407 Handle<Object> descriptors);
408
Steve Blocka7e24c12009-10-30 11:49:00 +0000409 // Create a new map cache.
410 static Handle<MapCache> NewMapCache(int at_least_space_for);
411
412 // Update the map cache in the global context with (keys, map)
413 static Handle<MapCache> AddToMapCache(Handle<Context> context,
414 Handle<FixedArray> keys,
415 Handle<Map> map);
416};
417
418
419} } // namespace v8::internal
420
421#endif // V8_FACTORY_H_