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