blob: 1ec9f1ba39a5120628f2b902d03d05b44f814723 [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
31#include "heap.h"
kasperl@chromium.org71affb52009-05-26 05:44:31 +000032#include "zone-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033
kasperl@chromium.org71affb52009-05-26 05:44:31 +000034namespace v8 {
35namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
37
38// Interface for handle based allocation.
39
40class Factory : public AllStatic {
41 public:
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000042 // Allocate a new fixed array with undefined entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043 static Handle<FixedArray> NewFixedArray(
44 int size,
45 PretenureFlag pretenure = NOT_TENURED);
46
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000047 // Allocate a new fixed array with non-existing entries (the hole).
48 static Handle<FixedArray> NewFixedArrayWithHoles(int size);
49
50 static Handle<Dictionary> NewDictionary(int at_least_space_for);
51
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052 static Handle<DescriptorArray> NewDescriptorArray(int number_of_descriptors);
53
54 static Handle<String> LookupSymbol(Vector<const char> str);
55 static Handle<String> LookupAsciiSymbol(const char* str) {
56 return LookupSymbol(CStrVector(str));
57 }
58
59
60 // String creation functions. Most of the string creation functions take
61 // a Heap::PretenureFlag argument to optionally request that they be
62 // allocated in the old generation. The pretenure flag defaults to
63 // DONT_TENURE.
64 //
65 // Creates a new String object. There are two String encodings: ASCII and
66 // two byte. One should choose between the three string factory functions
67 // based on the encoding of the string buffer that the string is
68 // initialized from.
69 // - ...FromAscii initializes the string from a buffer that is ASCII
70 // encoded (it does not check that the buffer is ASCII encoded) and
71 // the result will be ASCII encoded.
72 // - ...FromUtf8 initializes the string from a buffer that is UTF-8
73 // encoded. If the characters are all single-byte characters, the
74 // result will be ASCII encoded, otherwise it will converted to two
75 // byte.
76 // - ...FromTwoByte initializes the string from a buffer that is two
77 // byte encoded. If the characters are all single-byte characters,
78 // the result will be converted to ASCII, otherwise it will be left as
79 // two byte.
80 //
81 // ASCII strings are pretenured when used as keys in the SourceCodeCache.
82 static Handle<String> NewStringFromAscii(
83 Vector<const char> str,
84 PretenureFlag pretenure = NOT_TENURED);
85
86 // UTF8 strings are pretenured when used for regexp literal patterns and
87 // flags in the parser.
88 static Handle<String> NewStringFromUtf8(
89 Vector<const char> str,
90 PretenureFlag pretenure = NOT_TENURED);
91
92 static Handle<String> NewStringFromTwoByte(Vector<const uc16> str);
93
94 // Allocates and partially initializes a TwoByte String. The characters of
95 // the string are uninitialized. Currently used in regexp code only, where
96 // they are pretenured.
97 static Handle<String> NewRawTwoByteString(
98 int length,
99 PretenureFlag pretenure = NOT_TENURED);
100
101 // Create a new cons string object which consists of a pair of strings.
102 static Handle<String> NewConsString(Handle<String> first,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000103 Handle<String> second);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104
105 // Create a new sliced string object which represents a substring of a
106 // backing string.
ager@chromium.org870a0b62008-11-04 11:43:05 +0000107 static Handle<String> NewStringSlice(Handle<String> str,
ager@chromium.org870a0b62008-11-04 11:43:05 +0000108 int begin,
109 int end);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000110
111 // Creates a new external String object. There are two String encodings
112 // in the system: ASCII and two byte. Unlike other String types, it does
113 // not make sense to have a UTF-8 factory function for external strings,
114 // because we cannot change the underlying buffer.
115 static Handle<String> NewExternalStringFromAscii(
116 ExternalAsciiString::Resource* resource);
117 static Handle<String> NewExternalStringFromTwoByte(
118 ExternalTwoByteString::Resource* resource);
119
120 // Create a global (but otherwise uninitialized) context.
121 static Handle<Context> NewGlobalContext();
122
123 // Create a function context.
124 static Handle<Context> NewFunctionContext(int length,
125 Handle<JSFunction> closure);
126
127 // Create a 'with' context.
128 static Handle<Context> NewWithContext(Handle<Context> previous,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000129 Handle<JSObject> extension,
130 bool is_catch_context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131
ager@chromium.org32912102009-01-16 10:38:43 +0000132 // Return the Symbol matching the passed in string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133 static Handle<String> SymbolFromString(Handle<String> value);
134
135 // Allocate a new struct. The struct is pretenured (allocated directly in
136 // the old generation).
137 static Handle<Struct> NewStruct(InstanceType type);
138
139 static Handle<AccessorInfo> NewAccessorInfo();
140
141 static Handle<Script> NewScript(Handle<String> source);
142
143 // Proxies are pretenured when allocated by the bootstrapper.
144 static Handle<Proxy> NewProxy(Address addr,
145 PretenureFlag pretenure = NOT_TENURED);
146
147 // Allocate a new proxy. The proxy is pretenured (allocated directly in
148 // the old generation).
149 static Handle<Proxy> NewProxy(const AccessorDescriptor* proxy);
150
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000151 static Handle<ByteArray> NewByteArray(int length,
152 PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000153
154 static Handle<Map> NewMap(InstanceType type, int instance_size);
155
156 static Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
157
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000158 static Handle<Map> CopyMapDropDescriptors(Handle<Map> map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159
ager@chromium.org32912102009-01-16 10:38:43 +0000160 // Copy the map adding more inobject properties if possible without
161 // overflowing the instance size.
162 static Handle<Map> CopyMap(Handle<Map> map, int extra_inobject_props);
163
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000164 static Handle<Map> CopyMapDropTransitions(Handle<Map> map);
165
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166 static Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
167
168 // Numbers (eg, literals) are pretenured by the parser.
169 static Handle<Object> NewNumber(double value,
170 PretenureFlag pretenure = NOT_TENURED);
171
172 static Handle<Object> NewNumberFromInt(int value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000173 static Handle<Object> NewNumberFromUint(uint32_t value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174
175 // These objects are used by the api to create env-independent data
176 // structures in the heap.
177 static Handle<JSObject> NewNeanderObject();
178
179 static Handle<JSObject> NewArgumentsObject(Handle<Object> callee, int length);
180
181 // JS objects are pretenured when allocated by the bootstrapper and
182 // runtime.
183 static Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
184 PretenureFlag pretenure = NOT_TENURED);
185
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000186 // Global objects are pretenured.
187 static Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor);
188
ager@chromium.org236ad962008-09-25 09:45:57 +0000189 // JS objects are pretenured when allocated by the bootstrapper and
190 // runtime.
191 static Handle<JSObject> NewJSObjectFromMap(Handle<Map> map);
192
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000193 // JS arrays are pretenured when allocated by the parser.
194 static Handle<JSArray> NewJSArray(int init_length,
195 PretenureFlag pretenure = NOT_TENURED);
196
197 static Handle<JSArray> NewJSArrayWithElements(
198 Handle<FixedArray> elements,
199 PretenureFlag pretenure = NOT_TENURED);
200
201 static Handle<JSFunction> NewFunction(Handle<String> name,
202 Handle<Object> prototype);
203
204 static Handle<JSFunction> NewFunction(Handle<Object> super, bool is_global);
205
206 static Handle<JSFunction> NewFunctionFromBoilerplate(
207 Handle<JSFunction> boilerplate,
208 Handle<Context> context);
209
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000210 static Handle<Code> NewCode(const CodeDesc& desc,
211 ZoneScopeInfo* sinfo,
212 Code::Flags flags,
213 Handle<Object> self_reference);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000214
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215 static Handle<Code> CopyCode(Handle<Code> code);
216
217 static Handle<Object> ToObject(Handle<Object> object,
218 Handle<Context> global_context);
219
220 // Interface for creating error objects.
221
222 static Handle<Object> NewError(const char* maker, const char* type,
223 Handle<JSArray> args);
224 static Handle<Object> NewError(const char* maker, const char* type,
225 Vector< Handle<Object> > args);
226 static Handle<Object> NewError(const char* type,
227 Vector< Handle<Object> > args);
228 static Handle<Object> NewError(Handle<String> message);
229 static Handle<Object> NewError(const char* constructor,
230 Handle<String> message);
231
232 static Handle<Object> NewTypeError(const char* type,
233 Vector< Handle<Object> > args);
234 static Handle<Object> NewTypeError(Handle<String> message);
235
236 static Handle<Object> NewRangeError(const char* type,
237 Vector< Handle<Object> > args);
238 static Handle<Object> NewRangeError(Handle<String> message);
239
240 static Handle<Object> NewSyntaxError(const char* type, Handle<JSArray> args);
241 static Handle<Object> NewSyntaxError(Handle<String> message);
242
243 static Handle<Object> NewReferenceError(const char* type,
244 Vector< Handle<Object> > args);
245 static Handle<Object> NewReferenceError(Handle<String> message);
246
247 static Handle<Object> NewEvalError(const char* type,
248 Vector< Handle<Object> > args);
249
250
251 static Handle<JSFunction> NewFunction(Handle<String> name,
252 InstanceType type,
253 int instance_size,
254 Handle<Code> code,
255 bool force_initial_map);
256
257 static Handle<JSFunction> NewFunctionBoilerplate(Handle<String> name,
258 int number_of_literals,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000259 bool contains_array_literal,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000260 Handle<Code> code);
261
262 static Handle<JSFunction> NewFunctionBoilerplate(Handle<String> name);
263
264 static Handle<JSFunction> NewFunction(Handle<Map> function_map,
265 Handle<SharedFunctionInfo> shared, Handle<Object> prototype);
266
267
268 static Handle<JSFunction> NewFunctionWithPrototype(Handle<String> name,
269 InstanceType type,
270 int instance_size,
271 Handle<JSObject> prototype,
272 Handle<Code> code,
273 bool force_initial_map);
274
275 static Handle<DescriptorArray> CopyAppendProxyDescriptor(
276 Handle<DescriptorArray> array,
277 Handle<String> key,
278 Handle<Object> value,
279 PropertyAttributes attributes);
280
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000281 enum ApiInstanceType {
282 JavaScriptObject,
283 InnerGlobalObject,
284 OuterGlobalObject
285 };
286
287 static Handle<JSFunction> CreateApiFunction(
288 Handle<FunctionTemplateInfo> data,
289 ApiInstanceType type = JavaScriptObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000290
291 static Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
292
293 // Installs interceptors on the instance. 'desc' is a function template,
294 // and instance is an object instance created by the function of this
ager@chromium.org32912102009-01-16 10:38:43 +0000295 // function template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296 static void ConfigureInstance(Handle<FunctionTemplateInfo> desc,
297 Handle<JSObject> instance,
298 bool* pending_exception);
299
300#define ROOT_ACCESSOR(type, name) \
301 static Handle<type> name() { return Handle<type>(&Heap::name##_); }
302 ROOT_LIST(ROOT_ACCESSOR)
303#undef ROOT_ACCESSOR_ACCESSOR
304
305#define SYMBOL_ACCESSOR(name, str) \
306 static Handle<String> name() { return Handle<String>(&Heap::name##_); }
307 SYMBOL_LIST(SYMBOL_ACCESSOR)
308#undef SYMBOL_ACCESSOR
309
ager@chromium.org3b45ab52009-03-19 22:21:34 +0000310 static Handle<String> hidden_symbol() {
311 return Handle<String>(&Heap::hidden_symbol_);
312 }
313
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314 static Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name);
315
316 static Handle<Dictionary> DictionaryAtNumberPut(Handle<Dictionary>,
317 uint32_t key,
318 Handle<Object> value);
319
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000320#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +0000321 static Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000322#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000323
324 // Return a map using the map cache in the global context.
325 // The key the an ordered set of property names.
326 static Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
327 Handle<FixedArray> keys);
328
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000329 // Creates a new FixedArray that holds the data associated with the
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000330 // atom regexp and stores it in the regexp.
331 static void SetRegExpAtomData(Handle<JSRegExp> regexp,
332 JSRegExp::Type type,
333 Handle<String> source,
334 JSRegExp::Flags flags,
335 Handle<Object> match_pattern);
336
337 // Creates a new FixedArray that holds the data associated with the
338 // irregexp regexp and stores it in the regexp.
339 static void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
340 JSRegExp::Type type,
341 Handle<String> source,
342 JSRegExp::Flags flags,
343 int capture_count);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000344
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345 private:
346 static Handle<JSFunction> NewFunctionHelper(Handle<String> name,
347 Handle<Object> prototype);
348
349 static Handle<DescriptorArray> CopyAppendCallbackDescriptors(
350 Handle<DescriptorArray> array,
351 Handle<Object> descriptors);
352
353 static Handle<JSFunction> BaseNewFunctionFromBoilerplate(
354 Handle<JSFunction> boilerplate,
355 Handle<Map> function_map);
ager@chromium.org236ad962008-09-25 09:45:57 +0000356
357 // Create a new map cache.
358 static Handle<MapCache> NewMapCache(int at_least_space_for);
359
360 // Update the map cache in the global context with (keys, map)
361 static Handle<MapCache> AddToMapCache(Handle<Context> context,
362 Handle<FixedArray> keys,
363 Handle<Map> map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364};
365
366
367} } // namespace v8::internal
368
369#endif // V8_FACTORY_H_