blob: 0afdd76a49b19c5072de7d0e5fa2d7281fb64575 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000031#include "globals.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "heap.h"
kasperl@chromium.org71affb52009-05-26 05:44:31 +000033#include "zone-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000034
kasperl@chromium.org71affb52009-05-26 05:44:31 +000035namespace v8 {
36namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037
38
39// Interface for handle based allocation.
40
41class Factory : public AllStatic {
42 public:
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000043 // Allocate a new fixed array with undefined entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044 static Handle<FixedArray> NewFixedArray(
45 int size,
46 PretenureFlag pretenure = NOT_TENURED);
47
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000048 // Allocate a new fixed array with non-existing entries (the hole).
49 static Handle<FixedArray> NewFixedArrayWithHoles(int size);
50
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000051 static Handle<NumberDictionary> NewNumberDictionary(int at_least_space_for);
52
53 static Handle<StringDictionary> NewStringDictionary(int at_least_space_for);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000054
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000055 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
95 static Handle<String> NewStringFromTwoByte(Vector<const uc16> str);
96
97 // Allocates and partially initializes a TwoByte String. The characters of
98 // the string are uninitialized. Currently used in regexp code only, where
99 // they are pretenured.
100 static Handle<String> NewRawTwoByteString(
101 int length,
102 PretenureFlag pretenure = NOT_TENURED);
103
104 // Create a new cons string object which consists of a pair of strings.
105 static Handle<String> NewConsString(Handle<String> first,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000106 Handle<String> second);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000107
108 // Create a new sliced string object which represents a substring of a
109 // backing string.
ager@chromium.org870a0b62008-11-04 11:43:05 +0000110 static Handle<String> NewStringSlice(Handle<String> str,
ager@chromium.org870a0b62008-11-04 11:43:05 +0000111 int begin,
112 int end);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113
114 // Creates a new external String object. There are two String encodings
115 // in the system: ASCII and two byte. Unlike other String types, it does
116 // not make sense to have a UTF-8 factory function for external strings,
117 // because we cannot change the underlying buffer.
118 static Handle<String> NewExternalStringFromAscii(
119 ExternalAsciiString::Resource* resource);
120 static Handle<String> NewExternalStringFromTwoByte(
121 ExternalTwoByteString::Resource* resource);
122
123 // Create a global (but otherwise uninitialized) context.
124 static Handle<Context> NewGlobalContext();
125
126 // Create a function context.
127 static Handle<Context> NewFunctionContext(int length,
128 Handle<JSFunction> closure);
129
130 // Create a 'with' context.
131 static Handle<Context> NewWithContext(Handle<Context> previous,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000132 Handle<JSObject> extension,
133 bool is_catch_context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134
ager@chromium.org32912102009-01-16 10:38:43 +0000135 // Return the Symbol matching the passed in string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000136 static Handle<String> SymbolFromString(Handle<String> value);
137
138 // Allocate a new struct. The struct is pretenured (allocated directly in
139 // the old generation).
140 static Handle<Struct> NewStruct(InstanceType type);
141
142 static Handle<AccessorInfo> NewAccessorInfo();
143
144 static Handle<Script> NewScript(Handle<String> source);
145
146 // Proxies are pretenured when allocated by the bootstrapper.
147 static Handle<Proxy> NewProxy(Address addr,
148 PretenureFlag pretenure = NOT_TENURED);
149
150 // Allocate a new proxy. The proxy is pretenured (allocated directly in
151 // the old generation).
152 static Handle<Proxy> NewProxy(const AccessorDescriptor* proxy);
153
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000154 static Handle<ByteArray> NewByteArray(int length,
155 PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156
157 static Handle<Map> NewMap(InstanceType type, int instance_size);
158
159 static Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
160
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000161 static Handle<Map> CopyMapDropDescriptors(Handle<Map> map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162
ager@chromium.org32912102009-01-16 10:38:43 +0000163 // Copy the map adding more inobject properties if possible without
164 // overflowing the instance size.
165 static Handle<Map> CopyMap(Handle<Map> map, int extra_inobject_props);
166
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000167 static Handle<Map> CopyMapDropTransitions(Handle<Map> map);
168
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169 static Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
170
171 // Numbers (eg, literals) are pretenured by the parser.
172 static Handle<Object> NewNumber(double value,
173 PretenureFlag pretenure = NOT_TENURED);
174
175 static Handle<Object> NewNumberFromInt(int value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000176 static Handle<Object> NewNumberFromUint(uint32_t value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000177
178 // These objects are used by the api to create env-independent data
179 // structures in the heap.
180 static Handle<JSObject> NewNeanderObject();
181
182 static Handle<JSObject> NewArgumentsObject(Handle<Object> callee, int length);
183
184 // JS objects are pretenured when allocated by the bootstrapper and
185 // runtime.
186 static Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
187 PretenureFlag pretenure = NOT_TENURED);
188
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000189 // Global objects are pretenured.
190 static Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor);
191
ager@chromium.org236ad962008-09-25 09:45:57 +0000192 // JS objects are pretenured when allocated by the bootstrapper and
193 // runtime.
194 static Handle<JSObject> NewJSObjectFromMap(Handle<Map> map);
195
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000196 // JS arrays are pretenured when allocated by the parser.
197 static Handle<JSArray> NewJSArray(int init_length,
198 PretenureFlag pretenure = NOT_TENURED);
199
200 static Handle<JSArray> NewJSArrayWithElements(
201 Handle<FixedArray> elements,
202 PretenureFlag pretenure = NOT_TENURED);
203
204 static Handle<JSFunction> NewFunction(Handle<String> name,
205 Handle<Object> prototype);
206
207 static Handle<JSFunction> NewFunction(Handle<Object> super, bool is_global);
208
209 static Handle<JSFunction> NewFunctionFromBoilerplate(
210 Handle<JSFunction> boilerplate,
211 Handle<Context> context);
212
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000213 static Handle<Code> NewCode(const CodeDesc& desc,
214 ZoneScopeInfo* sinfo,
215 Code::Flags flags,
216 Handle<Object> self_reference);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000217
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000218 static Handle<Code> CopyCode(Handle<Code> code);
219
220 static Handle<Object> ToObject(Handle<Object> object,
221 Handle<Context> global_context);
222
223 // Interface for creating error objects.
224
225 static Handle<Object> NewError(const char* maker, const char* type,
226 Handle<JSArray> args);
227 static Handle<Object> NewError(const char* maker, const char* type,
228 Vector< Handle<Object> > args);
229 static Handle<Object> NewError(const char* type,
230 Vector< Handle<Object> > args);
231 static Handle<Object> NewError(Handle<String> message);
232 static Handle<Object> NewError(const char* constructor,
233 Handle<String> message);
234
235 static Handle<Object> NewTypeError(const char* type,
236 Vector< Handle<Object> > args);
237 static Handle<Object> NewTypeError(Handle<String> message);
238
239 static Handle<Object> NewRangeError(const char* type,
240 Vector< Handle<Object> > args);
241 static Handle<Object> NewRangeError(Handle<String> message);
242
243 static Handle<Object> NewSyntaxError(const char* type, Handle<JSArray> args);
244 static Handle<Object> NewSyntaxError(Handle<String> message);
245
246 static Handle<Object> NewReferenceError(const char* type,
247 Vector< Handle<Object> > args);
248 static Handle<Object> NewReferenceError(Handle<String> message);
249
250 static Handle<Object> NewEvalError(const char* type,
251 Vector< Handle<Object> > args);
252
253
254 static Handle<JSFunction> NewFunction(Handle<String> name,
255 InstanceType type,
256 int instance_size,
257 Handle<Code> code,
258 bool force_initial_map);
259
260 static Handle<JSFunction> NewFunctionBoilerplate(Handle<String> name,
261 int number_of_literals,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000262 bool contains_array_literal,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000263 Handle<Code> code);
264
265 static Handle<JSFunction> NewFunctionBoilerplate(Handle<String> name);
266
267 static Handle<JSFunction> NewFunction(Handle<Map> function_map,
268 Handle<SharedFunctionInfo> shared, Handle<Object> prototype);
269
270
271 static Handle<JSFunction> NewFunctionWithPrototype(Handle<String> name,
272 InstanceType type,
273 int instance_size,
274 Handle<JSObject> prototype,
275 Handle<Code> code,
276 bool force_initial_map);
277
278 static Handle<DescriptorArray> CopyAppendProxyDescriptor(
279 Handle<DescriptorArray> array,
280 Handle<String> key,
281 Handle<Object> value,
282 PropertyAttributes attributes);
283
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000284 enum ApiInstanceType {
285 JavaScriptObject,
286 InnerGlobalObject,
287 OuterGlobalObject
288 };
289
290 static Handle<JSFunction> CreateApiFunction(
291 Handle<FunctionTemplateInfo> data,
292 ApiInstanceType type = JavaScriptObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293
294 static Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
295
296 // Installs interceptors on the instance. 'desc' is a function template,
297 // and instance is an object instance created by the function of this
ager@chromium.org32912102009-01-16 10:38:43 +0000298 // function template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299 static void ConfigureInstance(Handle<FunctionTemplateInfo> desc,
300 Handle<JSObject> instance,
301 bool* pending_exception);
302
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000303#define ROOT_ACCESSOR(type, name, camel_name) \
304 static inline Handle<type> name() { \
305 return Handle<type>(bit_cast<type**, Object**>( \
306 &Heap::roots_[Heap::k##camel_name##RootIndex])); \
307 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308 ROOT_LIST(ROOT_ACCESSOR)
309#undef ROOT_ACCESSOR_ACCESSOR
310
311#define SYMBOL_ACCESSOR(name, str) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000312 static inline Handle<String> name() { \
313 return Handle<String>(bit_cast<String**, Object**>( \
314 &Heap::roots_[Heap::k##name##RootIndex])); \
315 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316 SYMBOL_LIST(SYMBOL_ACCESSOR)
317#undef SYMBOL_ACCESSOR
318
ager@chromium.org3b45ab52009-03-19 22:21:34 +0000319 static Handle<String> hidden_symbol() {
320 return Handle<String>(&Heap::hidden_symbol_);
321 }
322
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000323 static Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name);
324
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000325 static Handle<NumberDictionary> DictionaryAtNumberPut(
326 Handle<NumberDictionary>,
327 uint32_t key,
328 Handle<Object> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000330#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +0000331 static Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000332#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000333
334 // Return a map using the map cache in the global context.
335 // The key the an ordered set of property names.
336 static Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
337 Handle<FixedArray> keys);
338
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000339 // Creates a new FixedArray that holds the data associated with the
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000340 // atom regexp and stores it in the regexp.
341 static void SetRegExpAtomData(Handle<JSRegExp> regexp,
342 JSRegExp::Type type,
343 Handle<String> source,
344 JSRegExp::Flags flags,
345 Handle<Object> match_pattern);
346
347 // Creates a new FixedArray that holds the data associated with the
348 // irregexp regexp and stores it in the regexp.
349 static void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
350 JSRegExp::Type type,
351 Handle<String> source,
352 JSRegExp::Flags flags,
353 int capture_count);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000354
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000355 private:
356 static Handle<JSFunction> NewFunctionHelper(Handle<String> name,
357 Handle<Object> prototype);
358
359 static Handle<DescriptorArray> CopyAppendCallbackDescriptors(
360 Handle<DescriptorArray> array,
361 Handle<Object> descriptors);
362
363 static Handle<JSFunction> BaseNewFunctionFromBoilerplate(
364 Handle<JSFunction> boilerplate,
365 Handle<Map> function_map);
ager@chromium.org236ad962008-09-25 09:45:57 +0000366
367 // Create a new map cache.
368 static Handle<MapCache> NewMapCache(int at_least_space_for);
369
370 // Update the map cache in the global context with (keys, map)
371 static Handle<MapCache> AddToMapCache(Handle<Context> context,
372 Handle<FixedArray> keys,
373 Handle<Map> map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374};
375
376
377} } // namespace v8::internal
378
379#endif // V8_FACTORY_H_