blob: 429c483c42b96eaffe12aa5d4731ff5425ff28f9 [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"
32
33namespace v8 { namespace internal {
34
35
36// Interface for handle based allocation.
37
38class Factory : public AllStatic {
39 public:
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000040 // Allocate a new fixed array with undefined entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041 static Handle<FixedArray> NewFixedArray(
42 int size,
43 PretenureFlag pretenure = NOT_TENURED);
44
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000045 // Allocate a new fixed array with non-existing entries (the hole).
46 static Handle<FixedArray> NewFixedArrayWithHoles(int size);
47
48 static Handle<Dictionary> NewDictionary(int at_least_space_for);
49
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050 static Handle<DescriptorArray> NewDescriptorArray(int number_of_descriptors);
51
52 static Handle<String> LookupSymbol(Vector<const char> str);
53 static Handle<String> LookupAsciiSymbol(const char* str) {
54 return LookupSymbol(CStrVector(str));
55 }
56
57
58 // String creation functions. Most of the string creation functions take
59 // a Heap::PretenureFlag argument to optionally request that they be
60 // allocated in the old generation. The pretenure flag defaults to
61 // DONT_TENURE.
62 //
63 // Creates a new String object. There are two String encodings: ASCII and
64 // two byte. One should choose between the three string factory functions
65 // based on the encoding of the string buffer that the string is
66 // initialized from.
67 // - ...FromAscii initializes the string from a buffer that is ASCII
68 // encoded (it does not check that the buffer is ASCII encoded) and
69 // the result will be ASCII encoded.
70 // - ...FromUtf8 initializes the string from a buffer that is UTF-8
71 // encoded. If the characters are all single-byte characters, the
72 // result will be ASCII encoded, otherwise it will converted to two
73 // byte.
74 // - ...FromTwoByte initializes the string from a buffer that is two
75 // byte encoded. If the characters are all single-byte characters,
76 // the result will be converted to ASCII, otherwise it will be left as
77 // two byte.
78 //
79 // ASCII strings are pretenured when used as keys in the SourceCodeCache.
80 static Handle<String> NewStringFromAscii(
81 Vector<const char> str,
82 PretenureFlag pretenure = NOT_TENURED);
83
84 // UTF8 strings are pretenured when used for regexp literal patterns and
85 // flags in the parser.
86 static Handle<String> NewStringFromUtf8(
87 Vector<const char> str,
88 PretenureFlag pretenure = NOT_TENURED);
89
90 static Handle<String> NewStringFromTwoByte(Vector<const uc16> str);
91
92 // Allocates and partially initializes a TwoByte String. The characters of
93 // the string are uninitialized. Currently used in regexp code only, where
94 // they are pretenured.
95 static Handle<String> NewRawTwoByteString(
96 int length,
97 PretenureFlag pretenure = NOT_TENURED);
98
99 // Create a new cons string object which consists of a pair of strings.
100 static Handle<String> NewConsString(Handle<String> first,
ager@chromium.org870a0b62008-11-04 11:43:05 +0000101 StringShape first_shape,
102 Handle<String> second,
103 StringShape second_shape);
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,
129 Handle<JSObject> extension);
130
131 // Return the Symbol maching the passed in string.
132 static Handle<String> SymbolFromString(Handle<String> value);
133
134 // Allocate a new struct. The struct is pretenured (allocated directly in
135 // the old generation).
136 static Handle<Struct> NewStruct(InstanceType type);
137
138 static Handle<AccessorInfo> NewAccessorInfo();
139
140 static Handle<Script> NewScript(Handle<String> source);
141
142 // Proxies are pretenured when allocated by the bootstrapper.
143 static Handle<Proxy> NewProxy(Address addr,
144 PretenureFlag pretenure = NOT_TENURED);
145
146 // Allocate a new proxy. The proxy is pretenured (allocated directly in
147 // the old generation).
148 static Handle<Proxy> NewProxy(const AccessorDescriptor* proxy);
149
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000150 static Handle<ByteArray> NewByteArray(int length,
151 PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000152
153 static Handle<Map> NewMap(InstanceType type, int instance_size);
154
155 static Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
156
157 static Handle<Map> CopyMap(Handle<Map> map);
158
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000159 static Handle<Map> CopyMapDropTransitions(Handle<Map> map);
160
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161 static Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
162
163 // Numbers (eg, literals) are pretenured by the parser.
164 static Handle<Object> NewNumber(double value,
165 PretenureFlag pretenure = NOT_TENURED);
166
167 static Handle<Object> NewNumberFromInt(int value);
168
169 // These objects are used by the api to create env-independent data
170 // structures in the heap.
171 static Handle<JSObject> NewNeanderObject();
172
173 static Handle<JSObject> NewArgumentsObject(Handle<Object> callee, int length);
174
175 // JS objects are pretenured when allocated by the bootstrapper and
176 // runtime.
177 static Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
178 PretenureFlag pretenure = NOT_TENURED);
179
ager@chromium.org236ad962008-09-25 09:45:57 +0000180 // JS objects are pretenured when allocated by the bootstrapper and
181 // runtime.
182 static Handle<JSObject> NewJSObjectFromMap(Handle<Map> map);
183
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184 // Allocate a JS object representing an object literal. The object is
185 // pretenured (allocated directly in the old generation).
186 static Handle<JSObject> NewObjectLiteral(int expected_number_of_properties);
187
188 // Allocate a JS array representing an array literal. The array is
189 // pretenured (allocated directly in the old generation).
190 static Handle<JSArray> NewArrayLiteral(int length);
191
192 // JS arrays are pretenured when allocated by the parser.
193 static Handle<JSArray> NewJSArray(int init_length,
194 PretenureFlag pretenure = NOT_TENURED);
195
196 static Handle<JSArray> NewJSArrayWithElements(
197 Handle<FixedArray> elements,
198 PretenureFlag pretenure = NOT_TENURED);
199
200 static Handle<JSFunction> NewFunction(Handle<String> name,
201 Handle<Object> prototype);
202
203 static Handle<JSFunction> NewFunction(Handle<Object> super, bool is_global);
204
205 static Handle<JSFunction> NewFunctionFromBoilerplate(
206 Handle<JSFunction> boilerplate,
207 Handle<Context> context);
208
209 static Handle<Code> NewCode(const CodeDesc& desc, ScopeInfo<>* sinfo,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000210 Code::Flags flags, Handle<Object> self_reference);
211
212 static Handle<Code> NewCode(const CodeDesc& desc, ScopeInfo<>* sinfo,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213 Code::Flags flags);
214
215 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
295 // function tempalte.
296 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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000310 static Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name);
311
312 static Handle<Dictionary> DictionaryAtNumberPut(Handle<Dictionary>,
313 uint32_t key,
314 Handle<Object> value);
315
v8.team.kasperl727e9952008-09-02 14:56:44 +0000316 static Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
317
ager@chromium.org236ad962008-09-25 09:45:57 +0000318
319 // Return a map using the map cache in the global context.
320 // The key the an ordered set of property names.
321 static Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
322 Handle<FixedArray> keys);
323
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000324 // Creates a new FixedArray that holds the data associated with the
325 // regexp and stores it in the regexp.
326 static void SetRegExpData(Handle<JSRegExp> regexp,
327 JSRegExp::Type type,
328 Handle<String> source,
329 JSRegExp::Flags flags,
330 Handle<Object> data);
331
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332 private:
333 static Handle<JSFunction> NewFunctionHelper(Handle<String> name,
334 Handle<Object> prototype);
335
336 static Handle<DescriptorArray> CopyAppendCallbackDescriptors(
337 Handle<DescriptorArray> array,
338 Handle<Object> descriptors);
339
340 static Handle<JSFunction> BaseNewFunctionFromBoilerplate(
341 Handle<JSFunction> boilerplate,
342 Handle<Map> function_map);
ager@chromium.org236ad962008-09-25 09:45:57 +0000343
344 // Create a new map cache.
345 static Handle<MapCache> NewMapCache(int at_least_space_for);
346
347 // Update the map cache in the global context with (keys, map)
348 static Handle<MapCache> AddToMapCache(Handle<Context> context,
349 Handle<FixedArray> keys,
350 Handle<Map> map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000351};
352
353
354} } // namespace v8::internal
355
356#endif // V8_FACTORY_H_