blob: b7a2882e8358e5304e5e05c8f499307a21b54176 [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);
64 static Handle<String> LookupAsciiSymbol(const char* str) {
65 return LookupSymbol(CStrVector(str));
66 }
67
68
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
72 // DONT_TENURE.
73 //
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
77 // initialized from.
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
84 // byte.
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
88 // two byte.
89 //
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);
94
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);
100
Leon Clarkeac952652010-07-15 11:15:24 +0100101 static Handle<String> NewStringFromTwoByte(
102 Vector<const uc16> str,
Steve Blocka7e24c12009-10-30 11:49:00 +0000103 PretenureFlag pretenure = NOT_TENURED);
104
Leon Clarkeac952652010-07-15 11:15:24 +0100105 // 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(
109 int length,
110 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000111 static Handle<String> NewRawTwoByteString(
112 int length,
113 PretenureFlag pretenure = NOT_TENURED);
114
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);
118
Steve Blockd0582a62009-12-15 09:54:21 +0000119 // Create a new string object which holds a substring of a string.
120 static Handle<String> NewSubString(Handle<String> str,
121 int begin,
122 int end);
Steve Blocka7e24c12009-10-30 11:49:00 +0000123
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);
132
133 // Create a global (but otherwise uninitialized) context.
134 static Handle<Context> NewGlobalContext();
135
136 // Create a function context.
137 static Handle<Context> NewFunctionContext(int length,
138 Handle<JSFunction> closure);
139
140 // Create a 'with' context.
141 static Handle<Context> NewWithContext(Handle<Context> previous,
142 Handle<JSObject> extension,
143 bool is_catch_context);
144
145 // Return the Symbol matching the passed in string.
146 static Handle<String> SymbolFromString(Handle<String> value);
147
148 // Allocate a new struct. The struct is pretenured (allocated directly in
149 // the old generation).
150 static Handle<Struct> NewStruct(InstanceType type);
151
152 static Handle<AccessorInfo> NewAccessorInfo();
153
154 static Handle<Script> NewScript(Handle<String> source);
155
156 // Proxies are pretenured when allocated by the bootstrapper.
157 static Handle<Proxy> NewProxy(Address addr,
158 PretenureFlag pretenure = NOT_TENURED);
159
160 // Allocate a new proxy. The proxy is pretenured (allocated directly in
161 // the old generation).
162 static Handle<Proxy> NewProxy(const AccessorDescriptor* proxy);
163
164 static Handle<ByteArray> NewByteArray(int length,
165 PretenureFlag pretenure = NOT_TENURED);
166
Steve Block3ce2e202009-11-05 08:53:23 +0000167 static Handle<PixelArray> NewPixelArray(
168 int length,
Steve Blocka7e24c12009-10-30 11:49:00 +0000169 uint8_t* external_pointer,
170 PretenureFlag pretenure = NOT_TENURED);
171
Steve Block3ce2e202009-11-05 08:53:23 +0000172 static Handle<ExternalArray> NewExternalArray(
173 int length,
174 ExternalArrayType array_type,
175 void* external_pointer,
176 PretenureFlag pretenure = NOT_TENURED);
177
Ben Murdochb0fe1622011-05-05 13:52:32 +0100178 static Handle<JSGlobalPropertyCell> NewJSGlobalPropertyCell(
179 Handle<Object> value);
180
Steve Blocka7e24c12009-10-30 11:49:00 +0000181 static Handle<Map> NewMap(InstanceType type, int instance_size);
182
183 static Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
184
185 static Handle<Map> CopyMapDropDescriptors(Handle<Map> map);
186
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);
190
191 static Handle<Map> CopyMapDropTransitions(Handle<Map> map);
192
Steve Block8defd9f2010-07-08 12:39:36 +0100193 static Handle<Map> GetFastElementsMap(Handle<Map> map);
194
195 static Handle<Map> GetSlowElementsMap(Handle<Map> map);
196
Steve Blocka7e24c12009-10-30 11:49:00 +0000197 static Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
198
199 // Numbers (eg, literals) are pretenured by the parser.
200 static Handle<Object> NewNumber(double value,
201 PretenureFlag pretenure = NOT_TENURED);
202
203 static Handle<Object> NewNumberFromInt(int value);
204 static Handle<Object> NewNumberFromUint(uint32_t value);
205
206 // These objects are used by the api to create env-independent data
207 // structures in the heap.
208 static Handle<JSObject> NewNeanderObject();
209
210 static Handle<JSObject> NewArgumentsObject(Handle<Object> callee, int length);
211
212 // JS objects are pretenured when allocated by the bootstrapper and
213 // runtime.
214 static Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
215 PretenureFlag pretenure = NOT_TENURED);
216
217 // Global objects are pretenured.
218 static Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor);
219
220 // JS objects are pretenured when allocated by the bootstrapper and
221 // runtime.
222 static Handle<JSObject> NewJSObjectFromMap(Handle<Map> map);
223
224 // JS arrays are pretenured when allocated by the parser.
225 static Handle<JSArray> NewJSArray(int init_length,
226 PretenureFlag pretenure = NOT_TENURED);
227
228 static Handle<JSArray> NewJSArrayWithElements(
229 Handle<FixedArray> elements,
230 PretenureFlag pretenure = NOT_TENURED);
231
232 static Handle<JSFunction> NewFunction(Handle<String> name,
233 Handle<Object> prototype);
234
Steve Block6ded16b2010-05-10 14:33:55 +0100235 static Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name);
236
Steve Blocka7e24c12009-10-30 11:49:00 +0000237 static Handle<JSFunction> NewFunction(Handle<Object> super, bool is_global);
238
Steve Block6ded16b2010-05-10 14:33:55 +0100239 static Handle<JSFunction> BaseNewFunctionFromSharedFunctionInfo(
240 Handle<SharedFunctionInfo> function_info,
241 Handle<Map> function_map,
242 PretenureFlag pretenure);
243
244 static Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
245 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000246 Handle<Context> context,
247 PretenureFlag pretenure = TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000248
249 static Handle<Code> NewCode(const CodeDesc& desc,
Steve Blocka7e24c12009-10-30 11:49:00 +0000250 Code::Flags flags,
251 Handle<Object> self_reference);
252
253 static Handle<Code> CopyCode(Handle<Code> code);
254
Steve Block6ded16b2010-05-10 14:33:55 +0100255 static Handle<Code> CopyCode(Handle<Code> code, Vector<byte> reloc_info);
256
Leon Clarkee46be812010-01-19 14:06:41 +0000257 static Handle<Object> ToObject(Handle<Object> object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000258 static Handle<Object> ToObject(Handle<Object> object,
259 Handle<Context> global_context);
260
261 // Interface for creating error objects.
262
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);
272
273 static Handle<Object> NewTypeError(const char* type,
274 Vector< Handle<Object> > args);
275 static Handle<Object> NewTypeError(Handle<String> message);
276
277 static Handle<Object> NewRangeError(const char* type,
278 Vector< Handle<Object> > args);
279 static Handle<Object> NewRangeError(Handle<String> message);
280
281 static Handle<Object> NewSyntaxError(const char* type, Handle<JSArray> args);
282 static Handle<Object> NewSyntaxError(Handle<String> message);
283
284 static Handle<Object> NewReferenceError(const char* type,
285 Vector< Handle<Object> > args);
286 static Handle<Object> NewReferenceError(Handle<String> message);
287
288 static Handle<Object> NewEvalError(const char* type,
289 Vector< Handle<Object> > args);
290
291
292 static Handle<JSFunction> NewFunction(Handle<String> name,
293 InstanceType type,
294 int instance_size,
295 Handle<Code> code,
296 bool force_initial_map);
297
Steve Blocka7e24c12009-10-30 11:49:00 +0000298 static Handle<JSFunction> NewFunction(Handle<Map> function_map,
299 Handle<SharedFunctionInfo> shared, Handle<Object> prototype);
300
301
302 static Handle<JSFunction> NewFunctionWithPrototype(Handle<String> name,
303 InstanceType type,
304 int instance_size,
305 Handle<JSObject> prototype,
306 Handle<Code> code,
307 bool force_initial_map);
308
Steve Block6ded16b2010-05-10 14:33:55 +0100309 static Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name,
310 Handle<Code> code);
311
Steve Blocka7e24c12009-10-30 11:49:00 +0000312 static Handle<DescriptorArray> CopyAppendProxyDescriptor(
313 Handle<DescriptorArray> array,
314 Handle<String> key,
315 Handle<Object> value,
316 PropertyAttributes attributes);
317
318 static Handle<String> NumberToString(Handle<Object> number);
319
320 enum ApiInstanceType {
321 JavaScriptObject,
322 InnerGlobalObject,
323 OuterGlobalObject
324 };
325
326 static Handle<JSFunction> CreateApiFunction(
327 Handle<FunctionTemplateInfo> data,
328 ApiInstanceType type = JavaScriptObject);
329
330 static Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
331
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);
338
339#define ROOT_ACCESSOR(type, name, camel_name) \
340 static inline Handle<type> name() { \
Iain Merrick75681382010-08-19 15:07:18 +0100341 return Handle<type>(BitCast<type**>( \
Steve Blocka7e24c12009-10-30 11:49:00 +0000342 &Heap::roots_[Heap::k##camel_name##RootIndex])); \
343 }
344 ROOT_LIST(ROOT_ACCESSOR)
345#undef ROOT_ACCESSOR_ACCESSOR
346
347#define SYMBOL_ACCESSOR(name, str) \
348 static inline Handle<String> name() { \
Iain Merrick75681382010-08-19 15:07:18 +0100349 return Handle<String>(BitCast<String**>( \
Steve Blocka7e24c12009-10-30 11:49:00 +0000350 &Heap::roots_[Heap::k##name##RootIndex])); \
351 }
352 SYMBOL_LIST(SYMBOL_ACCESSOR)
353#undef SYMBOL_ACCESSOR
354
355 static Handle<String> hidden_symbol() {
356 return Handle<String>(&Heap::hidden_symbol_);
357 }
358
Steve Block6ded16b2010-05-10 14:33:55 +0100359 static Handle<SharedFunctionInfo> NewSharedFunctionInfo(
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100360 Handle<String> name,
361 int number_of_literals,
362 Handle<Code> code,
363 Handle<SerializedScopeInfo> scope_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000364 static Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name);
365
366 static Handle<NumberDictionary> DictionaryAtNumberPut(
367 Handle<NumberDictionary>,
368 uint32_t key,
369 Handle<Object> value);
370
371#ifdef ENABLE_DEBUGGER_SUPPORT
372 static Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
373#endif
374
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);
379
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,
383 JSRegExp::Type type,
384 Handle<String> source,
385 JSRegExp::Flags flags,
386 Handle<Object> match_pattern);
387
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,
391 JSRegExp::Type type,
392 Handle<String> source,
393 JSRegExp::Flags flags,
394 int capture_count);
395
396 private:
397 static Handle<JSFunction> NewFunctionHelper(Handle<String> name,
398 Handle<Object> prototype);
399
Steve Block6ded16b2010-05-10 14:33:55 +0100400 static Handle<JSFunction> NewFunctionWithoutPrototypeHelper(
401 Handle<String> name);
402
Steve Blocka7e24c12009-10-30 11:49:00 +0000403 static Handle<DescriptorArray> CopyAppendCallbackDescriptors(
404 Handle<DescriptorArray> array,
405 Handle<Object> descriptors);
406
Steve Blocka7e24c12009-10-30 11:49:00 +0000407 // Create a new map cache.
408 static Handle<MapCache> NewMapCache(int at_least_space_for);
409
410 // Update the map cache in the global context with (keys, map)
411 static Handle<MapCache> AddToMapCache(Handle<Context> context,
412 Handle<FixedArray> keys,
413 Handle<Map> map);
414};
415
416
417} } // namespace v8::internal
418
419#endif // V8_FACTORY_H_