blob: 56deda5ab5b094b8292703aaef6c591fb2013f09 [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
Leon Clarkeac952652010-07-15 11:15:24 +010098 static Handle<String> NewStringFromTwoByte(
99 Vector<const uc16> str,
Steve Blocka7e24c12009-10-30 11:49:00 +0000100 PretenureFlag pretenure = NOT_TENURED);
101
Leon Clarkeac952652010-07-15 11:15:24 +0100102 // Allocates and partially initializes an ASCII or TwoByte String. The
103 // characters of the string are uninitialized. Currently used in regexp code
104 // only, where they are pretenured.
105 static Handle<String> NewRawAsciiString(
106 int length,
107 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000108 static Handle<String> NewRawTwoByteString(
109 int length,
110 PretenureFlag pretenure = NOT_TENURED);
111
112 // Create a new cons string object which consists of a pair of strings.
113 static Handle<String> NewConsString(Handle<String> first,
114 Handle<String> second);
115
Steve Blockd0582a62009-12-15 09:54:21 +0000116 // Create a new string object which holds a substring of a string.
117 static Handle<String> NewSubString(Handle<String> str,
118 int begin,
119 int end);
Steve Blocka7e24c12009-10-30 11:49:00 +0000120
121 // Creates a new external String object. There are two String encodings
122 // in the system: ASCII and two byte. Unlike other String types, it does
123 // not make sense to have a UTF-8 factory function for external strings,
124 // because we cannot change the underlying buffer.
125 static Handle<String> NewExternalStringFromAscii(
126 ExternalAsciiString::Resource* resource);
127 static Handle<String> NewExternalStringFromTwoByte(
128 ExternalTwoByteString::Resource* resource);
129
130 // Create a global (but otherwise uninitialized) context.
131 static Handle<Context> NewGlobalContext();
132
133 // Create a function context.
134 static Handle<Context> NewFunctionContext(int length,
135 Handle<JSFunction> closure);
136
137 // Create a 'with' context.
138 static Handle<Context> NewWithContext(Handle<Context> previous,
139 Handle<JSObject> extension,
140 bool is_catch_context);
141
142 // Return the Symbol matching the passed in string.
143 static Handle<String> SymbolFromString(Handle<String> value);
144
145 // Allocate a new struct. The struct is pretenured (allocated directly in
146 // the old generation).
147 static Handle<Struct> NewStruct(InstanceType type);
148
149 static Handle<AccessorInfo> NewAccessorInfo();
150
151 static Handle<Script> NewScript(Handle<String> source);
152
153 // Proxies are pretenured when allocated by the bootstrapper.
154 static Handle<Proxy> NewProxy(Address addr,
155 PretenureFlag pretenure = NOT_TENURED);
156
157 // Allocate a new proxy. The proxy is pretenured (allocated directly in
158 // the old generation).
159 static Handle<Proxy> NewProxy(const AccessorDescriptor* proxy);
160
161 static Handle<ByteArray> NewByteArray(int length,
162 PretenureFlag pretenure = NOT_TENURED);
163
Steve Block3ce2e202009-11-05 08:53:23 +0000164 static Handle<PixelArray> NewPixelArray(
165 int length,
Steve Blocka7e24c12009-10-30 11:49:00 +0000166 uint8_t* external_pointer,
167 PretenureFlag pretenure = NOT_TENURED);
168
Steve Block3ce2e202009-11-05 08:53:23 +0000169 static Handle<ExternalArray> NewExternalArray(
170 int length,
171 ExternalArrayType array_type,
172 void* external_pointer,
173 PretenureFlag pretenure = NOT_TENURED);
174
Steve Blocka7e24c12009-10-30 11:49:00 +0000175 static Handle<Map> NewMap(InstanceType type, int instance_size);
176
177 static Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
178
179 static Handle<Map> CopyMapDropDescriptors(Handle<Map> map);
180
181 // Copy the map adding more inobject properties if possible without
182 // overflowing the instance size.
183 static Handle<Map> CopyMap(Handle<Map> map, int extra_inobject_props);
184
185 static Handle<Map> CopyMapDropTransitions(Handle<Map> map);
186
Steve Block8defd9f2010-07-08 12:39:36 +0100187 static Handle<Map> GetFastElementsMap(Handle<Map> map);
188
189 static Handle<Map> GetSlowElementsMap(Handle<Map> map);
190
Steve Blocka7e24c12009-10-30 11:49:00 +0000191 static Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
192
193 // Numbers (eg, literals) are pretenured by the parser.
194 static Handle<Object> NewNumber(double value,
195 PretenureFlag pretenure = NOT_TENURED);
196
197 static Handle<Object> NewNumberFromInt(int value);
198 static Handle<Object> NewNumberFromUint(uint32_t value);
199
200 // These objects are used by the api to create env-independent data
201 // structures in the heap.
202 static Handle<JSObject> NewNeanderObject();
203
204 static Handle<JSObject> NewArgumentsObject(Handle<Object> callee, int length);
205
206 // JS objects are pretenured when allocated by the bootstrapper and
207 // runtime.
208 static Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
209 PretenureFlag pretenure = NOT_TENURED);
210
211 // Global objects are pretenured.
212 static Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor);
213
214 // JS objects are pretenured when allocated by the bootstrapper and
215 // runtime.
216 static Handle<JSObject> NewJSObjectFromMap(Handle<Map> map);
217
218 // JS arrays are pretenured when allocated by the parser.
219 static Handle<JSArray> NewJSArray(int init_length,
220 PretenureFlag pretenure = NOT_TENURED);
221
222 static Handle<JSArray> NewJSArrayWithElements(
223 Handle<FixedArray> elements,
224 PretenureFlag pretenure = NOT_TENURED);
225
226 static Handle<JSFunction> NewFunction(Handle<String> name,
227 Handle<Object> prototype);
228
Steve Block6ded16b2010-05-10 14:33:55 +0100229 static Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name);
230
Steve Blocka7e24c12009-10-30 11:49:00 +0000231 static Handle<JSFunction> NewFunction(Handle<Object> super, bool is_global);
232
Steve Block6ded16b2010-05-10 14:33:55 +0100233 static Handle<JSFunction> BaseNewFunctionFromSharedFunctionInfo(
234 Handle<SharedFunctionInfo> function_info,
235 Handle<Map> function_map,
236 PretenureFlag pretenure);
237
238 static Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
239 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000240 Handle<Context> context,
241 PretenureFlag pretenure = TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000242
243 static Handle<Code> NewCode(const CodeDesc& desc,
244 ZoneScopeInfo* sinfo,
245 Code::Flags flags,
246 Handle<Object> self_reference);
247
248 static Handle<Code> CopyCode(Handle<Code> code);
249
Steve Block6ded16b2010-05-10 14:33:55 +0100250 static Handle<Code> CopyCode(Handle<Code> code, Vector<byte> reloc_info);
251
Leon Clarkee46be812010-01-19 14:06:41 +0000252 static Handle<Object> ToObject(Handle<Object> object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000253 static Handle<Object> ToObject(Handle<Object> object,
254 Handle<Context> global_context);
255
256 // Interface for creating error objects.
257
258 static Handle<Object> NewError(const char* maker, const char* type,
259 Handle<JSArray> args);
260 static Handle<Object> NewError(const char* maker, const char* type,
261 Vector< Handle<Object> > args);
262 static Handle<Object> NewError(const char* type,
263 Vector< Handle<Object> > args);
264 static Handle<Object> NewError(Handle<String> message);
265 static Handle<Object> NewError(const char* constructor,
266 Handle<String> message);
267
268 static Handle<Object> NewTypeError(const char* type,
269 Vector< Handle<Object> > args);
270 static Handle<Object> NewTypeError(Handle<String> message);
271
272 static Handle<Object> NewRangeError(const char* type,
273 Vector< Handle<Object> > args);
274 static Handle<Object> NewRangeError(Handle<String> message);
275
276 static Handle<Object> NewSyntaxError(const char* type, Handle<JSArray> args);
277 static Handle<Object> NewSyntaxError(Handle<String> message);
278
279 static Handle<Object> NewReferenceError(const char* type,
280 Vector< Handle<Object> > args);
281 static Handle<Object> NewReferenceError(Handle<String> message);
282
283 static Handle<Object> NewEvalError(const char* type,
284 Vector< Handle<Object> > args);
285
286
287 static Handle<JSFunction> NewFunction(Handle<String> name,
288 InstanceType type,
289 int instance_size,
290 Handle<Code> code,
291 bool force_initial_map);
292
Steve Blocka7e24c12009-10-30 11:49:00 +0000293 static Handle<JSFunction> NewFunction(Handle<Map> function_map,
294 Handle<SharedFunctionInfo> shared, Handle<Object> prototype);
295
296
297 static Handle<JSFunction> NewFunctionWithPrototype(Handle<String> name,
298 InstanceType type,
299 int instance_size,
300 Handle<JSObject> prototype,
301 Handle<Code> code,
302 bool force_initial_map);
303
Steve Block6ded16b2010-05-10 14:33:55 +0100304 static Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name,
305 Handle<Code> code);
306
Steve Blocka7e24c12009-10-30 11:49:00 +0000307 static Handle<DescriptorArray> CopyAppendProxyDescriptor(
308 Handle<DescriptorArray> array,
309 Handle<String> key,
310 Handle<Object> value,
311 PropertyAttributes attributes);
312
313 static Handle<String> NumberToString(Handle<Object> number);
314
315 enum ApiInstanceType {
316 JavaScriptObject,
317 InnerGlobalObject,
318 OuterGlobalObject
319 };
320
321 static Handle<JSFunction> CreateApiFunction(
322 Handle<FunctionTemplateInfo> data,
323 ApiInstanceType type = JavaScriptObject);
324
325 static Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
326
327 // Installs interceptors on the instance. 'desc' is a function template,
328 // and instance is an object instance created by the function of this
329 // function template.
330 static void ConfigureInstance(Handle<FunctionTemplateInfo> desc,
331 Handle<JSObject> instance,
332 bool* pending_exception);
333
334#define ROOT_ACCESSOR(type, name, camel_name) \
335 static inline Handle<type> name() { \
Steve Block6ded16b2010-05-10 14:33:55 +0100336 return Handle<type>(BitCast<type**, Object**>( \
Steve Blocka7e24c12009-10-30 11:49:00 +0000337 &Heap::roots_[Heap::k##camel_name##RootIndex])); \
338 }
339 ROOT_LIST(ROOT_ACCESSOR)
340#undef ROOT_ACCESSOR_ACCESSOR
341
342#define SYMBOL_ACCESSOR(name, str) \
343 static inline Handle<String> name() { \
Steve Block6ded16b2010-05-10 14:33:55 +0100344 return Handle<String>(BitCast<String**, Object**>( \
Steve Blocka7e24c12009-10-30 11:49:00 +0000345 &Heap::roots_[Heap::k##name##RootIndex])); \
346 }
347 SYMBOL_LIST(SYMBOL_ACCESSOR)
348#undef SYMBOL_ACCESSOR
349
350 static Handle<String> hidden_symbol() {
351 return Handle<String>(&Heap::hidden_symbol_);
352 }
353
Steve Block6ded16b2010-05-10 14:33:55 +0100354 static Handle<SharedFunctionInfo> NewSharedFunctionInfo(
355 Handle<String> name, int number_of_literals, Handle<Code> code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000356 static Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name);
357
358 static Handle<NumberDictionary> DictionaryAtNumberPut(
359 Handle<NumberDictionary>,
360 uint32_t key,
361 Handle<Object> value);
362
363#ifdef ENABLE_DEBUGGER_SUPPORT
364 static Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
365#endif
366
367 // Return a map using the map cache in the global context.
368 // The key the an ordered set of property names.
369 static Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
370 Handle<FixedArray> keys);
371
372 // Creates a new FixedArray that holds the data associated with the
373 // atom regexp and stores it in the regexp.
374 static void SetRegExpAtomData(Handle<JSRegExp> regexp,
375 JSRegExp::Type type,
376 Handle<String> source,
377 JSRegExp::Flags flags,
378 Handle<Object> match_pattern);
379
380 // Creates a new FixedArray that holds the data associated with the
381 // irregexp regexp and stores it in the regexp.
382 static void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
383 JSRegExp::Type type,
384 Handle<String> source,
385 JSRegExp::Flags flags,
386 int capture_count);
387
388 private:
389 static Handle<JSFunction> NewFunctionHelper(Handle<String> name,
390 Handle<Object> prototype);
391
Steve Block6ded16b2010-05-10 14:33:55 +0100392 static Handle<JSFunction> NewFunctionWithoutPrototypeHelper(
393 Handle<String> name);
394
Steve Blocka7e24c12009-10-30 11:49:00 +0000395 static Handle<DescriptorArray> CopyAppendCallbackDescriptors(
396 Handle<DescriptorArray> array,
397 Handle<Object> descriptors);
398
Steve Blocka7e24c12009-10-30 11:49:00 +0000399 // Create a new map cache.
400 static Handle<MapCache> NewMapCache(int at_least_space_for);
401
402 // Update the map cache in the global context with (keys, map)
403 static Handle<MapCache> AddToMapCache(Handle<Context> context,
404 Handle<FixedArray> keys,
405 Handle<Map> map);
406};
407
408
409} } // namespace v8::internal
410
411#endif // V8_FACTORY_H_