blob: 0596fbf00cc25858374a55f81154d1c0f77053c5 [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"
33#include "zone-inl.h"
34
35namespace v8 {
36namespace internal {
37
38
39// Interface for handle based allocation.
40
41class Factory : public AllStatic {
42 public:
43 // Allocate a new fixed array with undefined entries.
44 static Handle<FixedArray> NewFixedArray(
45 int size,
46 PretenureFlag pretenure = NOT_TENURED);
47
48 // Allocate a new fixed array with non-existing entries (the hole).
49 static Handle<FixedArray> NewFixedArrayWithHoles(int size);
50
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
95 static Handle<String> NewStringFromTwoByte(Vector<const uc16> str,
96 PretenureFlag pretenure = NOT_TENURED);
97
98 // Allocates and partially initializes a TwoByte String. The characters of
99 // the string are uninitialized. Currently used in regexp code only, where
100 // they are pretenured.
101 static Handle<String> NewRawTwoByteString(
102 int length,
103 PretenureFlag pretenure = NOT_TENURED);
104
105 // Create a new cons string object which consists of a pair of strings.
106 static Handle<String> NewConsString(Handle<String> first,
107 Handle<String> second);
108
109 // Create a new sliced string object which represents a substring of a
110 // backing string.
111 static Handle<String> NewStringSlice(Handle<String> str,
112 int begin,
113 int end);
114
115 // Creates a new external String object. There are two String encodings
116 // in the system: ASCII and two byte. Unlike other String types, it does
117 // not make sense to have a UTF-8 factory function for external strings,
118 // because we cannot change the underlying buffer.
119 static Handle<String> NewExternalStringFromAscii(
120 ExternalAsciiString::Resource* resource);
121 static Handle<String> NewExternalStringFromTwoByte(
122 ExternalTwoByteString::Resource* resource);
123
124 // Create a global (but otherwise uninitialized) context.
125 static Handle<Context> NewGlobalContext();
126
127 // Create a function context.
128 static Handle<Context> NewFunctionContext(int length,
129 Handle<JSFunction> closure);
130
131 // Create a 'with' context.
132 static Handle<Context> NewWithContext(Handle<Context> previous,
133 Handle<JSObject> extension,
134 bool is_catch_context);
135
136 // Return the Symbol matching the passed in string.
137 static Handle<String> SymbolFromString(Handle<String> value);
138
139 // Allocate a new struct. The struct is pretenured (allocated directly in
140 // the old generation).
141 static Handle<Struct> NewStruct(InstanceType type);
142
143 static Handle<AccessorInfo> NewAccessorInfo();
144
145 static Handle<Script> NewScript(Handle<String> source);
146
147 // Proxies are pretenured when allocated by the bootstrapper.
148 static Handle<Proxy> NewProxy(Address addr,
149 PretenureFlag pretenure = NOT_TENURED);
150
151 // Allocate a new proxy. The proxy is pretenured (allocated directly in
152 // the old generation).
153 static Handle<Proxy> NewProxy(const AccessorDescriptor* proxy);
154
155 static Handle<ByteArray> NewByteArray(int length,
156 PretenureFlag pretenure = NOT_TENURED);
157
158 static Handle<PixelArray> NewPixelArray(int length,
159 uint8_t* external_pointer,
160 PretenureFlag pretenure = NOT_TENURED);
161
162 static Handle<Map> NewMap(InstanceType type, int instance_size);
163
164 static Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
165
166 static Handle<Map> CopyMapDropDescriptors(Handle<Map> map);
167
168 // Copy the map adding more inobject properties if possible without
169 // overflowing the instance size.
170 static Handle<Map> CopyMap(Handle<Map> map, int extra_inobject_props);
171
172 static Handle<Map> CopyMapDropTransitions(Handle<Map> map);
173
174 static Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
175
176 // Numbers (eg, literals) are pretenured by the parser.
177 static Handle<Object> NewNumber(double value,
178 PretenureFlag pretenure = NOT_TENURED);
179
180 static Handle<Object> NewNumberFromInt(int value);
181 static Handle<Object> NewNumberFromUint(uint32_t value);
182
183 // These objects are used by the api to create env-independent data
184 // structures in the heap.
185 static Handle<JSObject> NewNeanderObject();
186
187 static Handle<JSObject> NewArgumentsObject(Handle<Object> callee, int length);
188
189 // JS objects are pretenured when allocated by the bootstrapper and
190 // runtime.
191 static Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
192 PretenureFlag pretenure = NOT_TENURED);
193
194 // Global objects are pretenured.
195 static Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor);
196
197 // JS objects are pretenured when allocated by the bootstrapper and
198 // runtime.
199 static Handle<JSObject> NewJSObjectFromMap(Handle<Map> map);
200
201 // JS arrays are pretenured when allocated by the parser.
202 static Handle<JSArray> NewJSArray(int init_length,
203 PretenureFlag pretenure = NOT_TENURED);
204
205 static Handle<JSArray> NewJSArrayWithElements(
206 Handle<FixedArray> elements,
207 PretenureFlag pretenure = NOT_TENURED);
208
209 static Handle<JSFunction> NewFunction(Handle<String> name,
210 Handle<Object> prototype);
211
212 static Handle<JSFunction> NewFunction(Handle<Object> super, bool is_global);
213
214 static Handle<JSFunction> NewFunctionFromBoilerplate(
215 Handle<JSFunction> boilerplate,
216 Handle<Context> context);
217
218 static Handle<Code> NewCode(const CodeDesc& desc,
219 ZoneScopeInfo* sinfo,
220 Code::Flags flags,
221 Handle<Object> self_reference);
222
223 static Handle<Code> CopyCode(Handle<Code> code);
224
225 static Handle<Object> ToObject(Handle<Object> object,
226 Handle<Context> global_context);
227
228 // Interface for creating error objects.
229
230 static Handle<Object> NewError(const char* maker, const char* type,
231 Handle<JSArray> args);
232 static Handle<Object> NewError(const char* maker, const char* type,
233 Vector< Handle<Object> > args);
234 static Handle<Object> NewError(const char* type,
235 Vector< Handle<Object> > args);
236 static Handle<Object> NewError(Handle<String> message);
237 static Handle<Object> NewError(const char* constructor,
238 Handle<String> message);
239
240 static Handle<Object> NewTypeError(const char* type,
241 Vector< Handle<Object> > args);
242 static Handle<Object> NewTypeError(Handle<String> message);
243
244 static Handle<Object> NewRangeError(const char* type,
245 Vector< Handle<Object> > args);
246 static Handle<Object> NewRangeError(Handle<String> message);
247
248 static Handle<Object> NewSyntaxError(const char* type, Handle<JSArray> args);
249 static Handle<Object> NewSyntaxError(Handle<String> message);
250
251 static Handle<Object> NewReferenceError(const char* type,
252 Vector< Handle<Object> > args);
253 static Handle<Object> NewReferenceError(Handle<String> message);
254
255 static Handle<Object> NewEvalError(const char* type,
256 Vector< Handle<Object> > args);
257
258
259 static Handle<JSFunction> NewFunction(Handle<String> name,
260 InstanceType type,
261 int instance_size,
262 Handle<Code> code,
263 bool force_initial_map);
264
265 static Handle<JSFunction> NewFunctionBoilerplate(Handle<String> name,
266 int number_of_literals,
267 bool contains_array_literal,
268 Handle<Code> code);
269
270 static Handle<JSFunction> NewFunctionBoilerplate(Handle<String> name);
271
272 static Handle<JSFunction> NewFunction(Handle<Map> function_map,
273 Handle<SharedFunctionInfo> shared, Handle<Object> prototype);
274
275
276 static Handle<JSFunction> NewFunctionWithPrototype(Handle<String> name,
277 InstanceType type,
278 int instance_size,
279 Handle<JSObject> prototype,
280 Handle<Code> code,
281 bool force_initial_map);
282
283 static Handle<DescriptorArray> CopyAppendProxyDescriptor(
284 Handle<DescriptorArray> array,
285 Handle<String> key,
286 Handle<Object> value,
287 PropertyAttributes attributes);
288
289 static Handle<String> NumberToString(Handle<Object> number);
290
291 enum ApiInstanceType {
292 JavaScriptObject,
293 InnerGlobalObject,
294 OuterGlobalObject
295 };
296
297 static Handle<JSFunction> CreateApiFunction(
298 Handle<FunctionTemplateInfo> data,
299 ApiInstanceType type = JavaScriptObject);
300
301 static Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
302
303 // Installs interceptors on the instance. 'desc' is a function template,
304 // and instance is an object instance created by the function of this
305 // function template.
306 static void ConfigureInstance(Handle<FunctionTemplateInfo> desc,
307 Handle<JSObject> instance,
308 bool* pending_exception);
309
310#define ROOT_ACCESSOR(type, name, camel_name) \
311 static inline Handle<type> name() { \
312 return Handle<type>(bit_cast<type**, Object**>( \
313 &Heap::roots_[Heap::k##camel_name##RootIndex])); \
314 }
315 ROOT_LIST(ROOT_ACCESSOR)
316#undef ROOT_ACCESSOR_ACCESSOR
317
318#define SYMBOL_ACCESSOR(name, str) \
319 static inline Handle<String> name() { \
320 return Handle<String>(bit_cast<String**, Object**>( \
321 &Heap::roots_[Heap::k##name##RootIndex])); \
322 }
323 SYMBOL_LIST(SYMBOL_ACCESSOR)
324#undef SYMBOL_ACCESSOR
325
326 static Handle<String> hidden_symbol() {
327 return Handle<String>(&Heap::hidden_symbol_);
328 }
329
330 static Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name);
331
332 static Handle<NumberDictionary> DictionaryAtNumberPut(
333 Handle<NumberDictionary>,
334 uint32_t key,
335 Handle<Object> value);
336
337#ifdef ENABLE_DEBUGGER_SUPPORT
338 static Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
339#endif
340
341 // Return a map using the map cache in the global context.
342 // The key the an ordered set of property names.
343 static Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
344 Handle<FixedArray> keys);
345
346 // Creates a new FixedArray that holds the data associated with the
347 // atom regexp and stores it in the regexp.
348 static void SetRegExpAtomData(Handle<JSRegExp> regexp,
349 JSRegExp::Type type,
350 Handle<String> source,
351 JSRegExp::Flags flags,
352 Handle<Object> match_pattern);
353
354 // Creates a new FixedArray that holds the data associated with the
355 // irregexp regexp and stores it in the regexp.
356 static void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
357 JSRegExp::Type type,
358 Handle<String> source,
359 JSRegExp::Flags flags,
360 int capture_count);
361
362 private:
363 static Handle<JSFunction> NewFunctionHelper(Handle<String> name,
364 Handle<Object> prototype);
365
366 static Handle<DescriptorArray> CopyAppendCallbackDescriptors(
367 Handle<DescriptorArray> array,
368 Handle<Object> descriptors);
369
370 static Handle<JSFunction> BaseNewFunctionFromBoilerplate(
371 Handle<JSFunction> boilerplate,
372 Handle<Map> function_map);
373
374 // Create a new map cache.
375 static Handle<MapCache> NewMapCache(int at_least_space_for);
376
377 // Update the map cache in the global context with (keys, map)
378 static Handle<MapCache> AddToMapCache(Handle<Context> context,
379 Handle<FixedArray> keys,
380 Handle<Map> map);
381};
382
383
384} } // namespace v8::internal
385
386#endif // V8_FACTORY_H_