blob: 2a347cd6fd0e0f2eda7ffe434693039646f11342 [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
Steve Blockd0582a62009-12-15 09:54:21 +0000109 // Create a new string object which holds a substring of a string.
110 static Handle<String> NewSubString(Handle<String> str,
111 int begin,
112 int end);
Steve Blocka7e24c12009-10-30 11:49:00 +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,
132 Handle<JSObject> extension,
133 bool is_catch_context);
134
135 // Return the Symbol matching the passed in string.
136 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
154 static Handle<ByteArray> NewByteArray(int length,
155 PretenureFlag pretenure = NOT_TENURED);
156
Steve Block3ce2e202009-11-05 08:53:23 +0000157 static Handle<PixelArray> NewPixelArray(
158 int length,
Steve Blocka7e24c12009-10-30 11:49:00 +0000159 uint8_t* external_pointer,
160 PretenureFlag pretenure = NOT_TENURED);
161
Steve Block3ce2e202009-11-05 08:53:23 +0000162 static Handle<ExternalArray> NewExternalArray(
163 int length,
164 ExternalArrayType array_type,
165 void* external_pointer,
166 PretenureFlag pretenure = NOT_TENURED);
167
Steve Blocka7e24c12009-10-30 11:49:00 +0000168 static Handle<Map> NewMap(InstanceType type, int instance_size);
169
170 static Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
171
172 static Handle<Map> CopyMapDropDescriptors(Handle<Map> map);
173
174 // Copy the map adding more inobject properties if possible without
175 // overflowing the instance size.
176 static Handle<Map> CopyMap(Handle<Map> map, int extra_inobject_props);
177
178 static Handle<Map> CopyMapDropTransitions(Handle<Map> map);
179
180 static Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
181
182 // Numbers (eg, literals) are pretenured by the parser.
183 static Handle<Object> NewNumber(double value,
184 PretenureFlag pretenure = NOT_TENURED);
185
186 static Handle<Object> NewNumberFromInt(int value);
187 static Handle<Object> NewNumberFromUint(uint32_t value);
188
189 // These objects are used by the api to create env-independent data
190 // structures in the heap.
191 static Handle<JSObject> NewNeanderObject();
192
193 static Handle<JSObject> NewArgumentsObject(Handle<Object> callee, int length);
194
195 // JS objects are pretenured when allocated by the bootstrapper and
196 // runtime.
197 static Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
198 PretenureFlag pretenure = NOT_TENURED);
199
200 // Global objects are pretenured.
201 static Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor);
202
203 // JS objects are pretenured when allocated by the bootstrapper and
204 // runtime.
205 static Handle<JSObject> NewJSObjectFromMap(Handle<Map> map);
206
207 // JS arrays are pretenured when allocated by the parser.
208 static Handle<JSArray> NewJSArray(int init_length,
209 PretenureFlag pretenure = NOT_TENURED);
210
211 static Handle<JSArray> NewJSArrayWithElements(
212 Handle<FixedArray> elements,
213 PretenureFlag pretenure = NOT_TENURED);
214
215 static Handle<JSFunction> NewFunction(Handle<String> name,
216 Handle<Object> prototype);
217
218 static Handle<JSFunction> NewFunction(Handle<Object> super, bool is_global);
219
220 static Handle<JSFunction> NewFunctionFromBoilerplate(
221 Handle<JSFunction> boilerplate,
Leon Clarkee46be812010-01-19 14:06:41 +0000222 Handle<Context> context,
223 PretenureFlag pretenure = TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000224
225 static Handle<Code> NewCode(const CodeDesc& desc,
226 ZoneScopeInfo* sinfo,
227 Code::Flags flags,
228 Handle<Object> self_reference);
229
230 static Handle<Code> CopyCode(Handle<Code> code);
231
Leon Clarkee46be812010-01-19 14:06:41 +0000232 static Handle<Object> ToObject(Handle<Object> object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000233 static Handle<Object> ToObject(Handle<Object> object,
234 Handle<Context> global_context);
235
236 // Interface for creating error objects.
237
238 static Handle<Object> NewError(const char* maker, const char* type,
239 Handle<JSArray> args);
240 static Handle<Object> NewError(const char* maker, const char* type,
241 Vector< Handle<Object> > args);
242 static Handle<Object> NewError(const char* type,
243 Vector< Handle<Object> > args);
244 static Handle<Object> NewError(Handle<String> message);
245 static Handle<Object> NewError(const char* constructor,
246 Handle<String> message);
247
248 static Handle<Object> NewTypeError(const char* type,
249 Vector< Handle<Object> > args);
250 static Handle<Object> NewTypeError(Handle<String> message);
251
252 static Handle<Object> NewRangeError(const char* type,
253 Vector< Handle<Object> > args);
254 static Handle<Object> NewRangeError(Handle<String> message);
255
256 static Handle<Object> NewSyntaxError(const char* type, Handle<JSArray> args);
257 static Handle<Object> NewSyntaxError(Handle<String> message);
258
259 static Handle<Object> NewReferenceError(const char* type,
260 Vector< Handle<Object> > args);
261 static Handle<Object> NewReferenceError(Handle<String> message);
262
263 static Handle<Object> NewEvalError(const char* type,
264 Vector< Handle<Object> > args);
265
266
267 static Handle<JSFunction> NewFunction(Handle<String> name,
268 InstanceType type,
269 int instance_size,
270 Handle<Code> code,
271 bool force_initial_map);
272
273 static Handle<JSFunction> NewFunctionBoilerplate(Handle<String> name,
274 int number_of_literals,
Steve Blocka7e24c12009-10-30 11:49:00 +0000275 Handle<Code> code);
276
277 static Handle<JSFunction> NewFunctionBoilerplate(Handle<String> name);
278
279 static Handle<JSFunction> NewFunction(Handle<Map> function_map,
280 Handle<SharedFunctionInfo> shared, Handle<Object> prototype);
281
282
283 static Handle<JSFunction> NewFunctionWithPrototype(Handle<String> name,
284 InstanceType type,
285 int instance_size,
286 Handle<JSObject> prototype,
287 Handle<Code> code,
288 bool force_initial_map);
289
290 static Handle<DescriptorArray> CopyAppendProxyDescriptor(
291 Handle<DescriptorArray> array,
292 Handle<String> key,
293 Handle<Object> value,
294 PropertyAttributes attributes);
295
296 static Handle<String> NumberToString(Handle<Object> number);
297
298 enum ApiInstanceType {
299 JavaScriptObject,
300 InnerGlobalObject,
301 OuterGlobalObject
302 };
303
304 static Handle<JSFunction> CreateApiFunction(
305 Handle<FunctionTemplateInfo> data,
306 ApiInstanceType type = JavaScriptObject);
307
308 static Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
309
310 // Installs interceptors on the instance. 'desc' is a function template,
311 // and instance is an object instance created by the function of this
312 // function template.
313 static void ConfigureInstance(Handle<FunctionTemplateInfo> desc,
314 Handle<JSObject> instance,
315 bool* pending_exception);
316
317#define ROOT_ACCESSOR(type, name, camel_name) \
318 static inline Handle<type> name() { \
319 return Handle<type>(bit_cast<type**, Object**>( \
320 &Heap::roots_[Heap::k##camel_name##RootIndex])); \
321 }
322 ROOT_LIST(ROOT_ACCESSOR)
323#undef ROOT_ACCESSOR_ACCESSOR
324
325#define SYMBOL_ACCESSOR(name, str) \
326 static inline Handle<String> name() { \
327 return Handle<String>(bit_cast<String**, Object**>( \
328 &Heap::roots_[Heap::k##name##RootIndex])); \
329 }
330 SYMBOL_LIST(SYMBOL_ACCESSOR)
331#undef SYMBOL_ACCESSOR
332
333 static Handle<String> hidden_symbol() {
334 return Handle<String>(&Heap::hidden_symbol_);
335 }
336
337 static Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name);
338
339 static Handle<NumberDictionary> DictionaryAtNumberPut(
340 Handle<NumberDictionary>,
341 uint32_t key,
342 Handle<Object> value);
343
344#ifdef ENABLE_DEBUGGER_SUPPORT
345 static Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
346#endif
347
348 // Return a map using the map cache in the global context.
349 // The key the an ordered set of property names.
350 static Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
351 Handle<FixedArray> keys);
352
353 // Creates a new FixedArray that holds the data associated with the
354 // atom regexp and stores it in the regexp.
355 static void SetRegExpAtomData(Handle<JSRegExp> regexp,
356 JSRegExp::Type type,
357 Handle<String> source,
358 JSRegExp::Flags flags,
359 Handle<Object> match_pattern);
360
361 // Creates a new FixedArray that holds the data associated with the
362 // irregexp regexp and stores it in the regexp.
363 static void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
364 JSRegExp::Type type,
365 Handle<String> source,
366 JSRegExp::Flags flags,
367 int capture_count);
368
369 private:
370 static Handle<JSFunction> NewFunctionHelper(Handle<String> name,
371 Handle<Object> prototype);
372
373 static Handle<DescriptorArray> CopyAppendCallbackDescriptors(
374 Handle<DescriptorArray> array,
375 Handle<Object> descriptors);
376
377 static Handle<JSFunction> BaseNewFunctionFromBoilerplate(
378 Handle<JSFunction> boilerplate,
Leon Clarkee46be812010-01-19 14:06:41 +0000379 Handle<Map> function_map,
380 PretenureFlag pretenure);
Steve Blocka7e24c12009-10-30 11:49:00 +0000381
382 // Create a new map cache.
383 static Handle<MapCache> NewMapCache(int at_least_space_for);
384
385 // Update the map cache in the global context with (keys, map)
386 static Handle<MapCache> AddToMapCache(Handle<Context> context,
387 Handle<FixedArray> keys,
388 Handle<Map> map);
389};
390
391
392} } // namespace v8::internal
393
394#endif // V8_FACTORY_H_