The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / src / factory.h @ 40c0f755

History | View | Annotate | Download (14.9 KB)

1
// 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 "heap.h"
32

    
33
namespace v8 { namespace internal {
34

    
35

    
36
// Interface for handle based allocation.
37

    
38
class Factory : public AllStatic {
39
 public:
40
  // Allocate a new fixed array with undefined entries.
41
  static Handle<FixedArray> NewFixedArray(
42
      int size,
43
      PretenureFlag pretenure = NOT_TENURED);
44

    
45
  // 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

    
50
  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,
101
                                      Handle<String> second);
102

    
103
  // Create a new sliced string object which represents a substring of a
104
  // backing string.
105
  static Handle<String> NewStringSlice(Handle<String> str,
106
                                       int begin,
107
                                       int end);
108

    
109
  // Creates a new external String object.  There are two String encodings
110
  // in the system: ASCII and two byte.  Unlike other String types, it does
111
  // not make sense to have a UTF-8 factory function for external strings,
112
  // because we cannot change the underlying buffer.
113
  static Handle<String> NewExternalStringFromAscii(
114
      ExternalAsciiString::Resource* resource);
115
  static Handle<String> NewExternalStringFromTwoByte(
116
      ExternalTwoByteString::Resource* resource);
117

    
118
  // Create a global (but otherwise uninitialized) context.
119
  static Handle<Context> NewGlobalContext();
120

    
121
  // Create a function context.
122
  static Handle<Context> NewFunctionContext(int length,
123
                                            Handle<JSFunction> closure);
124

    
125
  // Create a 'with' context.
126
  static Handle<Context> NewWithContext(Handle<Context> previous,
127
                                        Handle<JSObject> extension,
128
                                        bool is_catch_context);
129

    
130
  // Return the Symbol matching the passed in string.
131
  static Handle<String> SymbolFromString(Handle<String> value);
132

    
133
  // Allocate a new struct.  The struct is pretenured (allocated directly in
134
  // the old generation).
135
  static Handle<Struct> NewStruct(InstanceType type);
136

    
137
  static Handle<AccessorInfo> NewAccessorInfo();
138

    
139
  static Handle<Script> NewScript(Handle<String> source);
140

    
141
  // Proxies are pretenured when allocated by the bootstrapper.
142
  static Handle<Proxy> NewProxy(Address addr,
143
                                PretenureFlag pretenure = NOT_TENURED);
144

    
145
  // Allocate a new proxy.  The proxy is pretenured (allocated directly in
146
  // the old generation).
147
  static Handle<Proxy> NewProxy(const AccessorDescriptor* proxy);
148

    
149
  static Handle<ByteArray> NewByteArray(int length,
150
                                        PretenureFlag pretenure = NOT_TENURED);
151

    
152
  static Handle<Map> NewMap(InstanceType type, int instance_size);
153

    
154
  static Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
155

    
156
  static Handle<Map> CopyMap(Handle<Map> map);
157

    
158
  // Copy the map adding more inobject properties if possible without
159
  // overflowing the instance size.
160
  static Handle<Map> CopyMap(Handle<Map> map, int extra_inobject_props);
161

    
162
  static Handle<Map> CopyMapDropTransitions(Handle<Map> map);
163

    
164
  static Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
165

    
166
  // Numbers (eg, literals) are pretenured by the parser.
167
  static Handle<Object> NewNumber(double value,
168
                                  PretenureFlag pretenure = NOT_TENURED);
169

    
170
  static Handle<Object> NewNumberFromInt(int value);
171
  static Handle<Object> NewNumberFromUint(uint32_t value);
172

    
173
  // These objects are used by the api to create env-independent data
174
  // structures in the heap.
175
  static Handle<JSObject> NewNeanderObject();
176

    
177
  static Handle<JSObject> NewArgumentsObject(Handle<Object> callee, int length);
178

    
179
  // JS objects are pretenured when allocated by the bootstrapper and
180
  // runtime.
181
  static Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
182
                                      PretenureFlag pretenure = NOT_TENURED);
183

    
184
  // JS objects are pretenured when allocated by the bootstrapper and
185
  // runtime.
186
  static Handle<JSObject> NewJSObjectFromMap(Handle<Map> map);
187

    
188
  // JS arrays are pretenured when allocated by the parser.
189
  static Handle<JSArray> NewJSArray(int init_length,
190
                                    PretenureFlag pretenure = NOT_TENURED);
191

    
192
  static Handle<JSArray> NewJSArrayWithElements(
193
      Handle<FixedArray> elements,
194
      PretenureFlag pretenure = NOT_TENURED);
195

    
196
  static Handle<JSFunction> NewFunction(Handle<String> name,
197
                                        Handle<Object> prototype);
198

    
199
  static Handle<JSFunction> NewFunction(Handle<Object> super, bool is_global);
200

    
201
  static Handle<JSFunction> NewFunctionFromBoilerplate(
202
      Handle<JSFunction> boilerplate,
203
      Handle<Context> context);
204

    
205
  static Handle<Code> NewCode(const CodeDesc& desc, ScopeInfo<>* sinfo,
206
                              Code::Flags flags, Handle<Object> self_reference);
207

    
208
  static Handle<Code> CopyCode(Handle<Code> code);
209

    
210
  static Handle<Object> ToObject(Handle<Object> object,
211
                                 Handle<Context> global_context);
212

    
213
  // Interface for creating error objects.
214

    
215
  static Handle<Object> NewError(const char* maker, const char* type,
216
                                 Handle<JSArray> args);
217
  static Handle<Object> NewError(const char* maker, const char* type,
218
                                 Vector< Handle<Object> > args);
219
  static Handle<Object> NewError(const char* type,
220
                                 Vector< Handle<Object> > args);
221
  static Handle<Object> NewError(Handle<String> message);
222
  static Handle<Object> NewError(const char* constructor,
223
                                 Handle<String> message);
224

    
225
  static Handle<Object> NewTypeError(const char* type,
226
                                     Vector< Handle<Object> > args);
227
  static Handle<Object> NewTypeError(Handle<String> message);
228

    
229
  static Handle<Object> NewRangeError(const char* type,
230
                                      Vector< Handle<Object> > args);
231
  static Handle<Object> NewRangeError(Handle<String> message);
232

    
233
  static Handle<Object> NewSyntaxError(const char* type, Handle<JSArray> args);
234
  static Handle<Object> NewSyntaxError(Handle<String> message);
235

    
236
  static Handle<Object> NewReferenceError(const char* type,
237
                                          Vector< Handle<Object> > args);
238
  static Handle<Object> NewReferenceError(Handle<String> message);
239

    
240
  static Handle<Object> NewEvalError(const char* type,
241
                                     Vector< Handle<Object> > args);
242

    
243

    
244
  static Handle<JSFunction> NewFunction(Handle<String> name,
245
                                        InstanceType type,
246
                                        int instance_size,
247
                                        Handle<Code> code,
248
                                        bool force_initial_map);
249

    
250
  static Handle<JSFunction> NewFunctionBoilerplate(Handle<String> name,
251
                                                   int number_of_literals,
252
                                                   bool contains_array_literal,
253
                                                   Handle<Code> code);
254

    
255
  static Handle<JSFunction> NewFunctionBoilerplate(Handle<String> name);
256

    
257
  static Handle<JSFunction> NewFunction(Handle<Map> function_map,
258
      Handle<SharedFunctionInfo> shared, Handle<Object> prototype);
259

    
260

    
261
  static Handle<JSFunction> NewFunctionWithPrototype(Handle<String> name,
262
                                                     InstanceType type,
263
                                                     int instance_size,
264
                                                     Handle<JSObject> prototype,
265
                                                     Handle<Code> code,
266
                                                     bool force_initial_map);
267

    
268
  static Handle<DescriptorArray> CopyAppendProxyDescriptor(
269
      Handle<DescriptorArray> array,
270
      Handle<String> key,
271
      Handle<Object> value,
272
      PropertyAttributes attributes);
273

    
274
  enum ApiInstanceType {
275
    JavaScriptObject,
276
    InnerGlobalObject,
277
    OuterGlobalObject
278
  };
279

    
280
  static Handle<JSFunction> CreateApiFunction(
281
      Handle<FunctionTemplateInfo> data,
282
      ApiInstanceType type = JavaScriptObject);
283

    
284
  static Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
285

    
286
  // Installs interceptors on the instance.  'desc' is a function template,
287
  // and instance is an object instance created by the function of this
288
  // function template.
289
  static void ConfigureInstance(Handle<FunctionTemplateInfo> desc,
290
                                Handle<JSObject> instance,
291
                                bool* pending_exception);
292

    
293
#define ROOT_ACCESSOR(type, name) \
294
  static Handle<type> name() { return Handle<type>(&Heap::name##_); }
295
  ROOT_LIST(ROOT_ACCESSOR)
296
#undef ROOT_ACCESSOR_ACCESSOR
297

    
298
#define SYMBOL_ACCESSOR(name, str) \
299
  static Handle<String> name() { return Handle<String>(&Heap::name##_); }
300
  SYMBOL_LIST(SYMBOL_ACCESSOR)
301
#undef SYMBOL_ACCESSOR
302

    
303
  static Handle<String> hidden_symbol() {
304
    return Handle<String>(&Heap::hidden_symbol_);
305
  }
306

    
307
  static Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name);
308

    
309
  static Handle<Dictionary> DictionaryAtNumberPut(Handle<Dictionary>,
310
                                                  uint32_t key,
311
                                                  Handle<Object> value);
312

    
313
  static Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
314

    
315

    
316
  // Return a map using the map cache in the global context.
317
  // The key the an ordered set of property names.
318
  static Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
319
                                               Handle<FixedArray> keys);
320

    
321
  // Creates a new FixedArray that holds the data associated with the
322
  // atom regexp and stores it in the regexp.
323
  static void SetRegExpAtomData(Handle<JSRegExp> regexp,
324
                                JSRegExp::Type type,
325
                                Handle<String> source,
326
                                JSRegExp::Flags flags,
327
                                Handle<Object> match_pattern);
328

    
329
  // Creates a new FixedArray that holds the data associated with the
330
  // irregexp regexp and stores it in the regexp.
331
  static void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
332
                                    JSRegExp::Type type,
333
                                    Handle<String> source,
334
                                    JSRegExp::Flags flags,
335
                                    int capture_count);
336

    
337
 private:
338
  static Handle<JSFunction> NewFunctionHelper(Handle<String> name,
339
                                              Handle<Object> prototype);
340

    
341
  static Handle<DescriptorArray> CopyAppendCallbackDescriptors(
342
      Handle<DescriptorArray> array,
343
      Handle<Object> descriptors);
344

    
345
  static Handle<JSFunction> BaseNewFunctionFromBoilerplate(
346
      Handle<JSFunction> boilerplate,
347
      Handle<Map> function_map);
348

    
349
  // Create a new map cache.
350
  static Handle<MapCache> NewMapCache(int at_least_space_for);
351

    
352
  // Update the map cache in the global context with (keys, map)
353
  static Handle<MapCache> AddToMapCache(Handle<Context> context,
354
                                        Handle<FixedArray> keys,
355
                                        Handle<Map> map);
356
};
357

    
358

    
359
} }  // namespace v8::internal
360

    
361
#endif  // V8_FACTORY_H_