Upgrade V8 to 3.0.0
[platform/upstream/nodejs.git] / deps / v8 / src / factory.h
1 // Copyright 2010 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
34 namespace v8 {
35 namespace internal {
36
37 // Interface for handle based allocation.
38
39 class Factory : public AllStatic {
40  public:
41   // Allocate a new fixed array with undefined entries.
42   static Handle<FixedArray> NewFixedArray(
43       int size,
44       PretenureFlag pretenure = NOT_TENURED);
45
46   // Allocate a new fixed array with non-existing entries (the hole).
47   static Handle<FixedArray> NewFixedArrayWithHoles(
48       int size,
49       PretenureFlag pretenure = NOT_TENURED);
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   static Handle<DeoptimizationInputData> NewDeoptimizationInputData(
57       int deopt_entry_count,
58       PretenureFlag pretenure);
59   static Handle<DeoptimizationOutputData> NewDeoptimizationOutputData(
60       int deopt_entry_count,
61       PretenureFlag pretenure);
62
63   static Handle<String> LookupSymbol(Vector<const char> str);
64   static Handle<String> LookupAsciiSymbol(const char* str) {
65     return LookupSymbol(CStrVector(str));
66   }
67
68
69   // String creation functions.  Most of the string creation functions take
70   // a Heap::PretenureFlag argument to optionally request that they be
71   // allocated in the old generation.  The pretenure flag defaults to
72   // DONT_TENURE.
73   //
74   // Creates a new String object.  There are two String encodings: ASCII and
75   // two byte.  One should choose between the three string factory functions
76   // based on the encoding of the string buffer that the string is
77   // initialized from.
78   //   - ...FromAscii initializes the string from a buffer that is ASCII
79   //     encoded (it does not check that the buffer is ASCII encoded) and
80   //     the result will be ASCII encoded.
81   //   - ...FromUtf8 initializes the string from a buffer that is UTF-8
82   //     encoded.  If the characters are all single-byte characters, the
83   //     result will be ASCII encoded, otherwise it will converted to two
84   //     byte.
85   //   - ...FromTwoByte initializes the string from a buffer that is two
86   //     byte encoded.  If the characters are all single-byte characters,
87   //     the result will be converted to ASCII, otherwise it will be left as
88   //     two byte.
89   //
90   // ASCII strings are pretenured when used as keys in the SourceCodeCache.
91   static Handle<String> NewStringFromAscii(
92       Vector<const char> str,
93       PretenureFlag pretenure = NOT_TENURED);
94
95   // UTF8 strings are pretenured when used for regexp literal patterns and
96   // flags in the parser.
97   static Handle<String> NewStringFromUtf8(
98       Vector<const char> str,
99       PretenureFlag pretenure = NOT_TENURED);
100
101   static Handle<String> NewStringFromTwoByte(
102       Vector<const uc16> str,
103       PretenureFlag pretenure = NOT_TENURED);
104
105   // Allocates and partially initializes an ASCII or TwoByte String. The
106   // characters of the string are uninitialized. Currently used in regexp code
107   // only, where they are pretenured.
108   static Handle<String> NewRawAsciiString(
109       int length,
110       PretenureFlag pretenure = NOT_TENURED);
111   static Handle<String> NewRawTwoByteString(
112       int length,
113       PretenureFlag pretenure = NOT_TENURED);
114
115   // Create a new cons string object which consists of a pair of strings.
116   static Handle<String> NewConsString(Handle<String> first,
117                                       Handle<String> second);
118
119   // Create a new string object which holds a substring of a string.
120   static Handle<String> NewSubString(Handle<String> str,
121                                      int begin,
122                                      int end);
123
124   // Creates a new external String object.  There are two String encodings
125   // in the system: ASCII and two byte.  Unlike other String types, it does
126   // not make sense to have a UTF-8 factory function for external strings,
127   // because we cannot change the underlying buffer.
128   static Handle<String> NewExternalStringFromAscii(
129       ExternalAsciiString::Resource* resource);
130   static Handle<String> NewExternalStringFromTwoByte(
131       ExternalTwoByteString::Resource* resource);
132
133   // Create a global (but otherwise uninitialized) context.
134   static Handle<Context> NewGlobalContext();
135
136   // Create a function context.
137   static Handle<Context> NewFunctionContext(int length,
138                                             Handle<JSFunction> closure);
139
140   // Create a 'with' context.
141   static Handle<Context> NewWithContext(Handle<Context> previous,
142                                         Handle<JSObject> extension,
143                                         bool is_catch_context);
144
145   // Return the Symbol matching the passed in string.
146   static Handle<String> SymbolFromString(Handle<String> value);
147
148   // Allocate a new struct.  The struct is pretenured (allocated directly in
149   // the old generation).
150   static Handle<Struct> NewStruct(InstanceType type);
151
152   static Handle<AccessorInfo> NewAccessorInfo();
153
154   static Handle<Script> NewScript(Handle<String> source);
155
156   // Proxies are pretenured when allocated by the bootstrapper.
157   static Handle<Proxy> NewProxy(Address addr,
158                                 PretenureFlag pretenure = NOT_TENURED);
159
160   // Allocate a new proxy.  The proxy is pretenured (allocated directly in
161   // the old generation).
162   static Handle<Proxy> NewProxy(const AccessorDescriptor* proxy);
163
164   static Handle<ByteArray> NewByteArray(int length,
165                                         PretenureFlag pretenure = NOT_TENURED);
166
167   static Handle<PixelArray> NewPixelArray(
168       int length,
169       uint8_t* external_pointer,
170       PretenureFlag pretenure = NOT_TENURED);
171
172   static Handle<ExternalArray> NewExternalArray(
173       int length,
174       ExternalArrayType array_type,
175       void* external_pointer,
176       PretenureFlag pretenure = NOT_TENURED);
177
178   static Handle<JSGlobalPropertyCell> NewJSGlobalPropertyCell(
179       Handle<Object> value);
180
181   static Handle<Map> NewMap(InstanceType type, int instance_size);
182
183   static Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
184
185   static Handle<Map> CopyMapDropDescriptors(Handle<Map> map);
186
187   // Copy the map adding more inobject properties if possible without
188   // overflowing the instance size.
189   static Handle<Map> CopyMap(Handle<Map> map, int extra_inobject_props);
190
191   static Handle<Map> CopyMapDropTransitions(Handle<Map> map);
192
193   static Handle<Map> GetFastElementsMap(Handle<Map> map);
194
195   static Handle<Map> GetSlowElementsMap(Handle<Map> map);
196
197   static Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
198
199   // Numbers (eg, literals) are pretenured by the parser.
200   static Handle<Object> NewNumber(double value,
201                                   PretenureFlag pretenure = NOT_TENURED);
202
203   static Handle<Object> NewNumberFromInt(int value);
204   static Handle<Object> NewNumberFromUint(uint32_t value);
205
206   // These objects are used by the api to create env-independent data
207   // structures in the heap.
208   static Handle<JSObject> NewNeanderObject();
209
210   static Handle<JSObject> NewArgumentsObject(Handle<Object> callee, int length);
211
212   // JS objects are pretenured when allocated by the bootstrapper and
213   // runtime.
214   static Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
215                                       PretenureFlag pretenure = NOT_TENURED);
216
217   // Global objects are pretenured.
218   static Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor);
219
220   // JS objects are pretenured when allocated by the bootstrapper and
221   // runtime.
222   static Handle<JSObject> NewJSObjectFromMap(Handle<Map> map);
223
224   // JS arrays are pretenured when allocated by the parser.
225   static Handle<JSArray> NewJSArray(int init_length,
226                                     PretenureFlag pretenure = NOT_TENURED);
227
228   static Handle<JSArray> NewJSArrayWithElements(
229       Handle<FixedArray> elements,
230       PretenureFlag pretenure = NOT_TENURED);
231
232   static Handle<JSFunction> NewFunction(Handle<String> name,
233                                         Handle<Object> prototype);
234
235   static Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name);
236
237   static Handle<JSFunction> NewFunction(Handle<Object> super, bool is_global);
238
239   static Handle<JSFunction> BaseNewFunctionFromSharedFunctionInfo(
240       Handle<SharedFunctionInfo> function_info,
241       Handle<Map> function_map,
242       PretenureFlag pretenure);
243
244   static Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
245       Handle<SharedFunctionInfo> function_info,
246       Handle<Context> context,
247       PretenureFlag pretenure = TENURED);
248
249   static Handle<Code> NewCode(const CodeDesc& desc,
250                               Code::Flags flags,
251                               Handle<Object> self_reference);
252
253   static Handle<Code> CopyCode(Handle<Code> code);
254
255   static Handle<Code> CopyCode(Handle<Code> code, Vector<byte> reloc_info);
256
257   static Handle<Object> ToObject(Handle<Object> object);
258   static Handle<Object> ToObject(Handle<Object> object,
259                                  Handle<Context> global_context);
260
261   // Interface for creating error objects.
262
263   static Handle<Object> NewError(const char* maker, const char* type,
264                                  Handle<JSArray> args);
265   static Handle<Object> NewError(const char* maker, const char* type,
266                                  Vector< Handle<Object> > args);
267   static Handle<Object> NewError(const char* type,
268                                  Vector< Handle<Object> > args);
269   static Handle<Object> NewError(Handle<String> message);
270   static Handle<Object> NewError(const char* constructor,
271                                  Handle<String> message);
272
273   static Handle<Object> NewTypeError(const char* type,
274                                      Vector< Handle<Object> > args);
275   static Handle<Object> NewTypeError(Handle<String> message);
276
277   static Handle<Object> NewRangeError(const char* type,
278                                       Vector< Handle<Object> > args);
279   static Handle<Object> NewRangeError(Handle<String> message);
280
281   static Handle<Object> NewSyntaxError(const char* type, Handle<JSArray> args);
282   static Handle<Object> NewSyntaxError(Handle<String> message);
283
284   static Handle<Object> NewReferenceError(const char* type,
285                                           Vector< Handle<Object> > args);
286   static Handle<Object> NewReferenceError(Handle<String> message);
287
288   static Handle<Object> NewEvalError(const char* type,
289                                      Vector< Handle<Object> > args);
290
291
292   static Handle<JSFunction> NewFunction(Handle<String> name,
293                                         InstanceType type,
294                                         int instance_size,
295                                         Handle<Code> code,
296                                         bool force_initial_map);
297
298   static Handle<JSFunction> NewFunction(Handle<Map> function_map,
299       Handle<SharedFunctionInfo> shared, Handle<Object> prototype);
300
301
302   static Handle<JSFunction> NewFunctionWithPrototype(Handle<String> name,
303                                                      InstanceType type,
304                                                      int instance_size,
305                                                      Handle<JSObject> prototype,
306                                                      Handle<Code> code,
307                                                      bool force_initial_map);
308
309   static Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name,
310                                                         Handle<Code> code);
311
312   static Handle<DescriptorArray> CopyAppendProxyDescriptor(
313       Handle<DescriptorArray> array,
314       Handle<String> key,
315       Handle<Object> value,
316       PropertyAttributes attributes);
317
318   static Handle<String> NumberToString(Handle<Object> number);
319
320   enum ApiInstanceType {
321     JavaScriptObject,
322     InnerGlobalObject,
323     OuterGlobalObject
324   };
325
326   static Handle<JSFunction> CreateApiFunction(
327       Handle<FunctionTemplateInfo> data,
328       ApiInstanceType type = JavaScriptObject);
329
330   static Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
331
332   // Installs interceptors on the instance.  'desc' is a function template,
333   // and instance is an object instance created by the function of this
334   // function template.
335   static void ConfigureInstance(Handle<FunctionTemplateInfo> desc,
336                                 Handle<JSObject> instance,
337                                 bool* pending_exception);
338
339 #define ROOT_ACCESSOR(type, name, camel_name)                                  \
340   static inline Handle<type> name() {                                          \
341     return Handle<type>(BitCast<type**>(                                       \
342         &Heap::roots_[Heap::k##camel_name##RootIndex]));                       \
343   }
344   ROOT_LIST(ROOT_ACCESSOR)
345 #undef ROOT_ACCESSOR_ACCESSOR
346
347 #define SYMBOL_ACCESSOR(name, str) \
348   static inline Handle<String> name() {                                        \
349     return Handle<String>(BitCast<String**>(                                   \
350         &Heap::roots_[Heap::k##name##RootIndex]));                             \
351   }
352   SYMBOL_LIST(SYMBOL_ACCESSOR)
353 #undef SYMBOL_ACCESSOR
354
355   static Handle<String> hidden_symbol() {
356     return Handle<String>(&Heap::hidden_symbol_);
357   }
358
359   static Handle<SharedFunctionInfo> NewSharedFunctionInfo(
360       Handle<String> name,
361       int number_of_literals,
362       Handle<Code> code,
363       Handle<SerializedScopeInfo> scope_info);
364   static Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name);
365
366   static Handle<NumberDictionary> DictionaryAtNumberPut(
367       Handle<NumberDictionary>,
368       uint32_t key,
369       Handle<Object> value);
370
371 #ifdef ENABLE_DEBUGGER_SUPPORT
372   static Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
373 #endif
374
375   // Return a map using the map cache in the global context.
376   // The key the an ordered set of property names.
377   static Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
378                                                Handle<FixedArray> keys);
379
380   // Creates a new FixedArray that holds the data associated with the
381   // atom regexp and stores it in the regexp.
382   static void SetRegExpAtomData(Handle<JSRegExp> regexp,
383                                 JSRegExp::Type type,
384                                 Handle<String> source,
385                                 JSRegExp::Flags flags,
386                                 Handle<Object> match_pattern);
387
388   // Creates a new FixedArray that holds the data associated with the
389   // irregexp regexp and stores it in the regexp.
390   static void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
391                                     JSRegExp::Type type,
392                                     Handle<String> source,
393                                     JSRegExp::Flags flags,
394                                     int capture_count);
395
396  private:
397   static Handle<JSFunction> NewFunctionHelper(Handle<String> name,
398                                               Handle<Object> prototype);
399
400   static Handle<JSFunction> NewFunctionWithoutPrototypeHelper(
401       Handle<String> name);
402
403   static Handle<DescriptorArray> CopyAppendCallbackDescriptors(
404       Handle<DescriptorArray> array,
405       Handle<Object> descriptors);
406
407   // Create a new map cache.
408   static Handle<MapCache> NewMapCache(int at_least_space_for);
409
410   // Update the map cache in the global context with (keys, map)
411   static Handle<MapCache> AddToMapCache(Handle<Context> context,
412                                         Handle<FixedArray> keys,
413                                         Handle<Map> map);
414 };
415
416
417 } }  // namespace v8::internal
418
419 #endif  // V8_FACTORY_H_