blob: c014986f4e6cd885116e97fe1f794d7276f0fd78 [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 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);
56
57 static Handle<String> LookupSymbol(Vector<const char> str);
58 static Handle<String> LookupAsciiSymbol(const char* str) {
59 return LookupSymbol(CStrVector(str));
60 }
61
62
63 // String creation functions. Most of the string creation functions take
64 // a Heap::PretenureFlag argument to optionally request that they be
65 // allocated in the old generation. The pretenure flag defaults to
66 // DONT_TENURE.
67 //
68 // Creates a new String object. There are two String encodings: ASCII and
69 // two byte. One should choose between the three string factory functions
70 // based on the encoding of the string buffer that the string is
71 // initialized from.
72 // - ...FromAscii initializes the string from a buffer that is ASCII
73 // encoded (it does not check that the buffer is ASCII encoded) and
74 // the result will be ASCII encoded.
75 // - ...FromUtf8 initializes the string from a buffer that is UTF-8
76 // encoded. If the characters are all single-byte characters, the
77 // result will be ASCII encoded, otherwise it will converted to two
78 // byte.
79 // - ...FromTwoByte initializes the string from a buffer that is two
80 // byte encoded. If the characters are all single-byte characters,
81 // the result will be converted to ASCII, otherwise it will be left as
82 // two byte.
83 //
84 // ASCII strings are pretenured when used as keys in the SourceCodeCache.
85 static Handle<String> NewStringFromAscii(
86 Vector<const char> str,
87 PretenureFlag pretenure = NOT_TENURED);
88
89 // UTF8 strings are pretenured when used for regexp literal patterns and
90 // flags in the parser.
91 static Handle<String> NewStringFromUtf8(
92 Vector<const char> str,
93 PretenureFlag pretenure = NOT_TENURED);
94
Leon Clarkeac952652010-07-15 11:15:24 +010095 static Handle<String> NewStringFromTwoByte(
96 Vector<const uc16> str,
Steve Blocka7e24c12009-10-30 11:49:00 +000097 PretenureFlag pretenure = NOT_TENURED);
98
Leon Clarkeac952652010-07-15 11:15:24 +010099 // Allocates and partially initializes an ASCII or TwoByte String. The
100 // characters of the string are uninitialized. Currently used in regexp code
101 // only, where they are pretenured.
102 static Handle<String> NewRawAsciiString(
103 int length,
104 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000105 static Handle<String> NewRawTwoByteString(
106 int length,
107 PretenureFlag pretenure = NOT_TENURED);
108
109 // Create a new cons string object which consists of a pair of strings.
110 static Handle<String> NewConsString(Handle<String> first,
111 Handle<String> second);
112
Steve Blockd0582a62009-12-15 09:54:21 +0000113 // Create a new string object which holds a substring of a string.
114 static Handle<String> NewSubString(Handle<String> str,
115 int begin,
116 int end);
Steve Blocka7e24c12009-10-30 11:49:00 +0000117
118 // Creates a new external String object. There are two String encodings
119 // in the system: ASCII and two byte. Unlike other String types, it does
120 // not make sense to have a UTF-8 factory function for external strings,
121 // because we cannot change the underlying buffer.
122 static Handle<String> NewExternalStringFromAscii(
123 ExternalAsciiString::Resource* resource);
124 static Handle<String> NewExternalStringFromTwoByte(
125 ExternalTwoByteString::Resource* resource);
126
127 // Create a global (but otherwise uninitialized) context.
128 static Handle<Context> NewGlobalContext();
129
130 // Create a function context.
131 static Handle<Context> NewFunctionContext(int length,
132 Handle<JSFunction> closure);
133
134 // Create a 'with' context.
135 static Handle<Context> NewWithContext(Handle<Context> previous,
136 Handle<JSObject> extension,
137 bool is_catch_context);
138
139 // Return the Symbol matching the passed in string.
140 static Handle<String> SymbolFromString(Handle<String> value);
141
142 // Allocate a new struct. The struct is pretenured (allocated directly in
143 // the old generation).
144 static Handle<Struct> NewStruct(InstanceType type);
145
146 static Handle<AccessorInfo> NewAccessorInfo();
147
148 static Handle<Script> NewScript(Handle<String> source);
149
150 // Proxies are pretenured when allocated by the bootstrapper.
151 static Handle<Proxy> NewProxy(Address addr,
152 PretenureFlag pretenure = NOT_TENURED);
153
154 // Allocate a new proxy. The proxy is pretenured (allocated directly in
155 // the old generation).
156 static Handle<Proxy> NewProxy(const AccessorDescriptor* proxy);
157
158 static Handle<ByteArray> NewByteArray(int length,
159 PretenureFlag pretenure = NOT_TENURED);
160
Steve Block3ce2e202009-11-05 08:53:23 +0000161 static Handle<PixelArray> NewPixelArray(
162 int length,
Steve Blocka7e24c12009-10-30 11:49:00 +0000163 uint8_t* external_pointer,
164 PretenureFlag pretenure = NOT_TENURED);
165
Steve Block3ce2e202009-11-05 08:53:23 +0000166 static Handle<ExternalArray> NewExternalArray(
167 int length,
168 ExternalArrayType array_type,
169 void* external_pointer,
170 PretenureFlag pretenure = NOT_TENURED);
171
Steve Blocka7e24c12009-10-30 11:49:00 +0000172 static Handle<Map> NewMap(InstanceType type, int instance_size);
173
174 static Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
175
176 static Handle<Map> CopyMapDropDescriptors(Handle<Map> map);
177
178 // Copy the map adding more inobject properties if possible without
179 // overflowing the instance size.
180 static Handle<Map> CopyMap(Handle<Map> map, int extra_inobject_props);
181
182 static Handle<Map> CopyMapDropTransitions(Handle<Map> map);
183
Steve Block8defd9f2010-07-08 12:39:36 +0100184 static Handle<Map> GetFastElementsMap(Handle<Map> map);
185
186 static Handle<Map> GetSlowElementsMap(Handle<Map> map);
187
Steve Blocka7e24c12009-10-30 11:49:00 +0000188 static Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
189
190 // Numbers (eg, literals) are pretenured by the parser.
191 static Handle<Object> NewNumber(double value,
192 PretenureFlag pretenure = NOT_TENURED);
193
194 static Handle<Object> NewNumberFromInt(int value);
195 static Handle<Object> NewNumberFromUint(uint32_t value);
196
197 // These objects are used by the api to create env-independent data
198 // structures in the heap.
199 static Handle<JSObject> NewNeanderObject();
200
201 static Handle<JSObject> NewArgumentsObject(Handle<Object> callee, int length);
202
203 // JS objects are pretenured when allocated by the bootstrapper and
204 // runtime.
205 static Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
206 PretenureFlag pretenure = NOT_TENURED);
207
208 // Global objects are pretenured.
209 static Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor);
210
211 // JS objects are pretenured when allocated by the bootstrapper and
212 // runtime.
213 static Handle<JSObject> NewJSObjectFromMap(Handle<Map> map);
214
215 // JS arrays are pretenured when allocated by the parser.
216 static Handle<JSArray> NewJSArray(int init_length,
217 PretenureFlag pretenure = NOT_TENURED);
218
219 static Handle<JSArray> NewJSArrayWithElements(
220 Handle<FixedArray> elements,
221 PretenureFlag pretenure = NOT_TENURED);
222
223 static Handle<JSFunction> NewFunction(Handle<String> name,
224 Handle<Object> prototype);
225
Steve Block6ded16b2010-05-10 14:33:55 +0100226 static Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name);
227
Steve Blocka7e24c12009-10-30 11:49:00 +0000228 static Handle<JSFunction> NewFunction(Handle<Object> super, bool is_global);
229
Steve Block6ded16b2010-05-10 14:33:55 +0100230 static Handle<JSFunction> BaseNewFunctionFromSharedFunctionInfo(
231 Handle<SharedFunctionInfo> function_info,
232 Handle<Map> function_map,
233 PretenureFlag pretenure);
234
235 static Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
236 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000237 Handle<Context> context,
238 PretenureFlag pretenure = TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000239
240 static Handle<Code> NewCode(const CodeDesc& desc,
Steve Blocka7e24c12009-10-30 11:49:00 +0000241 Code::Flags flags,
242 Handle<Object> self_reference);
243
244 static Handle<Code> CopyCode(Handle<Code> code);
245
Steve Block6ded16b2010-05-10 14:33:55 +0100246 static Handle<Code> CopyCode(Handle<Code> code, Vector<byte> reloc_info);
247
Leon Clarkee46be812010-01-19 14:06:41 +0000248 static Handle<Object> ToObject(Handle<Object> object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000249 static Handle<Object> ToObject(Handle<Object> object,
250 Handle<Context> global_context);
251
252 // Interface for creating error objects.
253
254 static Handle<Object> NewError(const char* maker, const char* type,
255 Handle<JSArray> args);
256 static Handle<Object> NewError(const char* maker, const char* type,
257 Vector< Handle<Object> > args);
258 static Handle<Object> NewError(const char* type,
259 Vector< Handle<Object> > args);
260 static Handle<Object> NewError(Handle<String> message);
261 static Handle<Object> NewError(const char* constructor,
262 Handle<String> message);
263
264 static Handle<Object> NewTypeError(const char* type,
265 Vector< Handle<Object> > args);
266 static Handle<Object> NewTypeError(Handle<String> message);
267
268 static Handle<Object> NewRangeError(const char* type,
269 Vector< Handle<Object> > args);
270 static Handle<Object> NewRangeError(Handle<String> message);
271
272 static Handle<Object> NewSyntaxError(const char* type, Handle<JSArray> args);
273 static Handle<Object> NewSyntaxError(Handle<String> message);
274
275 static Handle<Object> NewReferenceError(const char* type,
276 Vector< Handle<Object> > args);
277 static Handle<Object> NewReferenceError(Handle<String> message);
278
279 static Handle<Object> NewEvalError(const char* type,
280 Vector< Handle<Object> > args);
281
282
283 static Handle<JSFunction> NewFunction(Handle<String> name,
284 InstanceType type,
285 int instance_size,
286 Handle<Code> code,
287 bool force_initial_map);
288
Steve Blocka7e24c12009-10-30 11:49:00 +0000289 static Handle<JSFunction> NewFunction(Handle<Map> function_map,
290 Handle<SharedFunctionInfo> shared, Handle<Object> prototype);
291
292
293 static Handle<JSFunction> NewFunctionWithPrototype(Handle<String> name,
294 InstanceType type,
295 int instance_size,
296 Handle<JSObject> prototype,
297 Handle<Code> code,
298 bool force_initial_map);
299
Steve Block6ded16b2010-05-10 14:33:55 +0100300 static Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name,
301 Handle<Code> code);
302
Steve Blocka7e24c12009-10-30 11:49:00 +0000303 static Handle<DescriptorArray> CopyAppendProxyDescriptor(
304 Handle<DescriptorArray> array,
305 Handle<String> key,
306 Handle<Object> value,
307 PropertyAttributes attributes);
308
309 static Handle<String> NumberToString(Handle<Object> number);
310
311 enum ApiInstanceType {
312 JavaScriptObject,
313 InnerGlobalObject,
314 OuterGlobalObject
315 };
316
317 static Handle<JSFunction> CreateApiFunction(
318 Handle<FunctionTemplateInfo> data,
319 ApiInstanceType type = JavaScriptObject);
320
321 static Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
322
323 // Installs interceptors on the instance. 'desc' is a function template,
324 // and instance is an object instance created by the function of this
325 // function template.
326 static void ConfigureInstance(Handle<FunctionTemplateInfo> desc,
327 Handle<JSObject> instance,
328 bool* pending_exception);
329
330#define ROOT_ACCESSOR(type, name, camel_name) \
331 static inline Handle<type> name() { \
Iain Merrick75681382010-08-19 15:07:18 +0100332 return Handle<type>(BitCast<type**>( \
Steve Blocka7e24c12009-10-30 11:49:00 +0000333 &Heap::roots_[Heap::k##camel_name##RootIndex])); \
334 }
335 ROOT_LIST(ROOT_ACCESSOR)
336#undef ROOT_ACCESSOR_ACCESSOR
337
338#define SYMBOL_ACCESSOR(name, str) \
339 static inline Handle<String> name() { \
Iain Merrick75681382010-08-19 15:07:18 +0100340 return Handle<String>(BitCast<String**>( \
Steve Blocka7e24c12009-10-30 11:49:00 +0000341 &Heap::roots_[Heap::k##name##RootIndex])); \
342 }
343 SYMBOL_LIST(SYMBOL_ACCESSOR)
344#undef SYMBOL_ACCESSOR
345
346 static Handle<String> hidden_symbol() {
347 return Handle<String>(&Heap::hidden_symbol_);
348 }
349
Steve Block6ded16b2010-05-10 14:33:55 +0100350 static Handle<SharedFunctionInfo> NewSharedFunctionInfo(
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100351 Handle<String> name,
352 int number_of_literals,
353 Handle<Code> code,
354 Handle<SerializedScopeInfo> scope_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000355 static Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name);
356
357 static Handle<NumberDictionary> DictionaryAtNumberPut(
358 Handle<NumberDictionary>,
359 uint32_t key,
360 Handle<Object> value);
361
362#ifdef ENABLE_DEBUGGER_SUPPORT
363 static Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
364#endif
365
366 // Return a map using the map cache in the global context.
367 // The key the an ordered set of property names.
368 static Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
369 Handle<FixedArray> keys);
370
371 // Creates a new FixedArray that holds the data associated with the
372 // atom regexp and stores it in the regexp.
373 static void SetRegExpAtomData(Handle<JSRegExp> regexp,
374 JSRegExp::Type type,
375 Handle<String> source,
376 JSRegExp::Flags flags,
377 Handle<Object> match_pattern);
378
379 // Creates a new FixedArray that holds the data associated with the
380 // irregexp regexp and stores it in the regexp.
381 static void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
382 JSRegExp::Type type,
383 Handle<String> source,
384 JSRegExp::Flags flags,
385 int capture_count);
386
387 private:
388 static Handle<JSFunction> NewFunctionHelper(Handle<String> name,
389 Handle<Object> prototype);
390
Steve Block6ded16b2010-05-10 14:33:55 +0100391 static Handle<JSFunction> NewFunctionWithoutPrototypeHelper(
392 Handle<String> name);
393
Steve Blocka7e24c12009-10-30 11:49:00 +0000394 static Handle<DescriptorArray> CopyAppendCallbackDescriptors(
395 Handle<DescriptorArray> array,
396 Handle<Object> descriptors);
397
Steve Blocka7e24c12009-10-30 11:49:00 +0000398 // Create a new map cache.
399 static Handle<MapCache> NewMapCache(int at_least_space_for);
400
401 // Update the map cache in the global context with (keys, map)
402 static Handle<MapCache> AddToMapCache(Handle<Context> context,
403 Handle<FixedArray> keys,
404 Handle<Map> map);
405};
406
407
408} } // namespace v8::internal
409
410#endif // V8_FACTORY_H_