ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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" |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 32 | #include "zone-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 34 | namespace v8 { |
| 35 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | |
| 37 | |
| 38 | // Interface for handle based allocation. |
| 39 | |
| 40 | class Factory : public AllStatic { |
| 41 | public: |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 42 | // Allocate a new fixed array with undefined entries. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 43 | static Handle<FixedArray> NewFixedArray( |
| 44 | int size, |
| 45 | PretenureFlag pretenure = NOT_TENURED); |
| 46 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 47 | // Allocate a new fixed array with non-existing entries (the hole). |
| 48 | static Handle<FixedArray> NewFixedArrayWithHoles(int size); |
| 49 | |
| 50 | static Handle<Dictionary> NewDictionary(int at_least_space_for); |
| 51 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 52 | static Handle<DescriptorArray> NewDescriptorArray(int number_of_descriptors); |
| 53 | |
| 54 | static Handle<String> LookupSymbol(Vector<const char> str); |
| 55 | static Handle<String> LookupAsciiSymbol(const char* str) { |
| 56 | return LookupSymbol(CStrVector(str)); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | // String creation functions. Most of the string creation functions take |
| 61 | // a Heap::PretenureFlag argument to optionally request that they be |
| 62 | // allocated in the old generation. The pretenure flag defaults to |
| 63 | // DONT_TENURE. |
| 64 | // |
| 65 | // Creates a new String object. There are two String encodings: ASCII and |
| 66 | // two byte. One should choose between the three string factory functions |
| 67 | // based on the encoding of the string buffer that the string is |
| 68 | // initialized from. |
| 69 | // - ...FromAscii initializes the string from a buffer that is ASCII |
| 70 | // encoded (it does not check that the buffer is ASCII encoded) and |
| 71 | // the result will be ASCII encoded. |
| 72 | // - ...FromUtf8 initializes the string from a buffer that is UTF-8 |
| 73 | // encoded. If the characters are all single-byte characters, the |
| 74 | // result will be ASCII encoded, otherwise it will converted to two |
| 75 | // byte. |
| 76 | // - ...FromTwoByte initializes the string from a buffer that is two |
| 77 | // byte encoded. If the characters are all single-byte characters, |
| 78 | // the result will be converted to ASCII, otherwise it will be left as |
| 79 | // two byte. |
| 80 | // |
| 81 | // ASCII strings are pretenured when used as keys in the SourceCodeCache. |
| 82 | static Handle<String> NewStringFromAscii( |
| 83 | Vector<const char> str, |
| 84 | PretenureFlag pretenure = NOT_TENURED); |
| 85 | |
| 86 | // UTF8 strings are pretenured when used for regexp literal patterns and |
| 87 | // flags in the parser. |
| 88 | static Handle<String> NewStringFromUtf8( |
| 89 | Vector<const char> str, |
| 90 | PretenureFlag pretenure = NOT_TENURED); |
| 91 | |
| 92 | static Handle<String> NewStringFromTwoByte(Vector<const uc16> str); |
| 93 | |
| 94 | // Allocates and partially initializes a TwoByte String. The characters of |
| 95 | // the string are uninitialized. Currently used in regexp code only, where |
| 96 | // they are pretenured. |
| 97 | static Handle<String> NewRawTwoByteString( |
| 98 | int length, |
| 99 | PretenureFlag pretenure = NOT_TENURED); |
| 100 | |
| 101 | // Create a new cons string object which consists of a pair of strings. |
| 102 | static Handle<String> NewConsString(Handle<String> first, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 103 | Handle<String> second); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 104 | |
| 105 | // Create a new sliced string object which represents a substring of a |
| 106 | // backing string. |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 107 | static Handle<String> NewStringSlice(Handle<String> str, |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 108 | int begin, |
| 109 | int end); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 110 | |
| 111 | // Creates a new external String object. There are two String encodings |
| 112 | // in the system: ASCII and two byte. Unlike other String types, it does |
| 113 | // not make sense to have a UTF-8 factory function for external strings, |
| 114 | // because we cannot change the underlying buffer. |
| 115 | static Handle<String> NewExternalStringFromAscii( |
| 116 | ExternalAsciiString::Resource* resource); |
| 117 | static Handle<String> NewExternalStringFromTwoByte( |
| 118 | ExternalTwoByteString::Resource* resource); |
| 119 | |
| 120 | // Create a global (but otherwise uninitialized) context. |
| 121 | static Handle<Context> NewGlobalContext(); |
| 122 | |
| 123 | // Create a function context. |
| 124 | static Handle<Context> NewFunctionContext(int length, |
| 125 | Handle<JSFunction> closure); |
| 126 | |
| 127 | // Create a 'with' context. |
| 128 | static Handle<Context> NewWithContext(Handle<Context> previous, |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 129 | Handle<JSObject> extension, |
| 130 | bool is_catch_context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 131 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 132 | // Return the Symbol matching the passed in string. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 133 | static Handle<String> SymbolFromString(Handle<String> value); |
| 134 | |
| 135 | // Allocate a new struct. The struct is pretenured (allocated directly in |
| 136 | // the old generation). |
| 137 | static Handle<Struct> NewStruct(InstanceType type); |
| 138 | |
| 139 | static Handle<AccessorInfo> NewAccessorInfo(); |
| 140 | |
| 141 | static Handle<Script> NewScript(Handle<String> source); |
| 142 | |
| 143 | // Proxies are pretenured when allocated by the bootstrapper. |
| 144 | static Handle<Proxy> NewProxy(Address addr, |
| 145 | PretenureFlag pretenure = NOT_TENURED); |
| 146 | |
| 147 | // Allocate a new proxy. The proxy is pretenured (allocated directly in |
| 148 | // the old generation). |
| 149 | static Handle<Proxy> NewProxy(const AccessorDescriptor* proxy); |
| 150 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 151 | static Handle<ByteArray> NewByteArray(int length, |
| 152 | PretenureFlag pretenure = NOT_TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 153 | |
| 154 | static Handle<Map> NewMap(InstanceType type, int instance_size); |
| 155 | |
| 156 | static Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function); |
| 157 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 158 | static Handle<Map> CopyMapDropDescriptors(Handle<Map> map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 159 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 160 | // Copy the map adding more inobject properties if possible without |
| 161 | // overflowing the instance size. |
| 162 | static Handle<Map> CopyMap(Handle<Map> map, int extra_inobject_props); |
| 163 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 164 | static Handle<Map> CopyMapDropTransitions(Handle<Map> map); |
| 165 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 166 | static Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array); |
| 167 | |
| 168 | // Numbers (eg, literals) are pretenured by the parser. |
| 169 | static Handle<Object> NewNumber(double value, |
| 170 | PretenureFlag pretenure = NOT_TENURED); |
| 171 | |
| 172 | static Handle<Object> NewNumberFromInt(int value); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 173 | static Handle<Object> NewNumberFromUint(uint32_t value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 174 | |
| 175 | // These objects are used by the api to create env-independent data |
| 176 | // structures in the heap. |
| 177 | static Handle<JSObject> NewNeanderObject(); |
| 178 | |
| 179 | static Handle<JSObject> NewArgumentsObject(Handle<Object> callee, int length); |
| 180 | |
| 181 | // JS objects are pretenured when allocated by the bootstrapper and |
| 182 | // runtime. |
| 183 | static Handle<JSObject> NewJSObject(Handle<JSFunction> constructor, |
| 184 | PretenureFlag pretenure = NOT_TENURED); |
| 185 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 186 | // Global objects are pretenured. |
| 187 | static Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor); |
| 188 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 189 | // JS objects are pretenured when allocated by the bootstrapper and |
| 190 | // runtime. |
| 191 | static Handle<JSObject> NewJSObjectFromMap(Handle<Map> map); |
| 192 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 193 | // JS arrays are pretenured when allocated by the parser. |
| 194 | static Handle<JSArray> NewJSArray(int init_length, |
| 195 | PretenureFlag pretenure = NOT_TENURED); |
| 196 | |
| 197 | static Handle<JSArray> NewJSArrayWithElements( |
| 198 | Handle<FixedArray> elements, |
| 199 | PretenureFlag pretenure = NOT_TENURED); |
| 200 | |
| 201 | static Handle<JSFunction> NewFunction(Handle<String> name, |
| 202 | Handle<Object> prototype); |
| 203 | |
| 204 | static Handle<JSFunction> NewFunction(Handle<Object> super, bool is_global); |
| 205 | |
| 206 | static Handle<JSFunction> NewFunctionFromBoilerplate( |
| 207 | Handle<JSFunction> boilerplate, |
| 208 | Handle<Context> context); |
| 209 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 210 | static Handle<Code> NewCode(const CodeDesc& desc, |
| 211 | ZoneScopeInfo* sinfo, |
| 212 | Code::Flags flags, |
| 213 | Handle<Object> self_reference); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 214 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 215 | static Handle<Code> CopyCode(Handle<Code> code); |
| 216 | |
| 217 | static Handle<Object> ToObject(Handle<Object> object, |
| 218 | Handle<Context> global_context); |
| 219 | |
| 220 | // Interface for creating error objects. |
| 221 | |
| 222 | static Handle<Object> NewError(const char* maker, const char* type, |
| 223 | Handle<JSArray> args); |
| 224 | static Handle<Object> NewError(const char* maker, const char* type, |
| 225 | Vector< Handle<Object> > args); |
| 226 | static Handle<Object> NewError(const char* type, |
| 227 | Vector< Handle<Object> > args); |
| 228 | static Handle<Object> NewError(Handle<String> message); |
| 229 | static Handle<Object> NewError(const char* constructor, |
| 230 | Handle<String> message); |
| 231 | |
| 232 | static Handle<Object> NewTypeError(const char* type, |
| 233 | Vector< Handle<Object> > args); |
| 234 | static Handle<Object> NewTypeError(Handle<String> message); |
| 235 | |
| 236 | static Handle<Object> NewRangeError(const char* type, |
| 237 | Vector< Handle<Object> > args); |
| 238 | static Handle<Object> NewRangeError(Handle<String> message); |
| 239 | |
| 240 | static Handle<Object> NewSyntaxError(const char* type, Handle<JSArray> args); |
| 241 | static Handle<Object> NewSyntaxError(Handle<String> message); |
| 242 | |
| 243 | static Handle<Object> NewReferenceError(const char* type, |
| 244 | Vector< Handle<Object> > args); |
| 245 | static Handle<Object> NewReferenceError(Handle<String> message); |
| 246 | |
| 247 | static Handle<Object> NewEvalError(const char* type, |
| 248 | Vector< Handle<Object> > args); |
| 249 | |
| 250 | |
| 251 | static Handle<JSFunction> NewFunction(Handle<String> name, |
| 252 | InstanceType type, |
| 253 | int instance_size, |
| 254 | Handle<Code> code, |
| 255 | bool force_initial_map); |
| 256 | |
| 257 | static Handle<JSFunction> NewFunctionBoilerplate(Handle<String> name, |
| 258 | int number_of_literals, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 259 | bool contains_array_literal, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 260 | Handle<Code> code); |
| 261 | |
| 262 | static Handle<JSFunction> NewFunctionBoilerplate(Handle<String> name); |
| 263 | |
| 264 | static Handle<JSFunction> NewFunction(Handle<Map> function_map, |
| 265 | Handle<SharedFunctionInfo> shared, Handle<Object> prototype); |
| 266 | |
| 267 | |
| 268 | static Handle<JSFunction> NewFunctionWithPrototype(Handle<String> name, |
| 269 | InstanceType type, |
| 270 | int instance_size, |
| 271 | Handle<JSObject> prototype, |
| 272 | Handle<Code> code, |
| 273 | bool force_initial_map); |
| 274 | |
| 275 | static Handle<DescriptorArray> CopyAppendProxyDescriptor( |
| 276 | Handle<DescriptorArray> array, |
| 277 | Handle<String> key, |
| 278 | Handle<Object> value, |
| 279 | PropertyAttributes attributes); |
| 280 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 281 | enum ApiInstanceType { |
| 282 | JavaScriptObject, |
| 283 | InnerGlobalObject, |
| 284 | OuterGlobalObject |
| 285 | }; |
| 286 | |
| 287 | static Handle<JSFunction> CreateApiFunction( |
| 288 | Handle<FunctionTemplateInfo> data, |
| 289 | ApiInstanceType type = JavaScriptObject); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 290 | |
| 291 | static Handle<JSFunction> InstallMembers(Handle<JSFunction> function); |
| 292 | |
| 293 | // Installs interceptors on the instance. 'desc' is a function template, |
| 294 | // and instance is an object instance created by the function of this |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 295 | // function template. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 296 | static void ConfigureInstance(Handle<FunctionTemplateInfo> desc, |
| 297 | Handle<JSObject> instance, |
| 298 | bool* pending_exception); |
| 299 | |
| 300 | #define ROOT_ACCESSOR(type, name) \ |
| 301 | static Handle<type> name() { return Handle<type>(&Heap::name##_); } |
| 302 | ROOT_LIST(ROOT_ACCESSOR) |
| 303 | #undef ROOT_ACCESSOR_ACCESSOR |
| 304 | |
| 305 | #define SYMBOL_ACCESSOR(name, str) \ |
| 306 | static Handle<String> name() { return Handle<String>(&Heap::name##_); } |
| 307 | SYMBOL_LIST(SYMBOL_ACCESSOR) |
| 308 | #undef SYMBOL_ACCESSOR |
| 309 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 310 | static Handle<String> hidden_symbol() { |
| 311 | return Handle<String>(&Heap::hidden_symbol_); |
| 312 | } |
| 313 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 314 | static Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name); |
| 315 | |
| 316 | static Handle<Dictionary> DictionaryAtNumberPut(Handle<Dictionary>, |
| 317 | uint32_t key, |
| 318 | Handle<Object> value); |
| 319 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 320 | #ifdef ENABLE_DEBUGGER_SUPPORT |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 321 | static Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 322 | #endif |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 323 | |
| 324 | // Return a map using the map cache in the global context. |
| 325 | // The key the an ordered set of property names. |
| 326 | static Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context, |
| 327 | Handle<FixedArray> keys); |
| 328 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 329 | // Creates a new FixedArray that holds the data associated with the |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 330 | // atom regexp and stores it in the regexp. |
| 331 | static void SetRegExpAtomData(Handle<JSRegExp> regexp, |
| 332 | JSRegExp::Type type, |
| 333 | Handle<String> source, |
| 334 | JSRegExp::Flags flags, |
| 335 | Handle<Object> match_pattern); |
| 336 | |
| 337 | // Creates a new FixedArray that holds the data associated with the |
| 338 | // irregexp regexp and stores it in the regexp. |
| 339 | static void SetRegExpIrregexpData(Handle<JSRegExp> regexp, |
| 340 | JSRegExp::Type type, |
| 341 | Handle<String> source, |
| 342 | JSRegExp::Flags flags, |
| 343 | int capture_count); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 344 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 345 | private: |
| 346 | static Handle<JSFunction> NewFunctionHelper(Handle<String> name, |
| 347 | Handle<Object> prototype); |
| 348 | |
| 349 | static Handle<DescriptorArray> CopyAppendCallbackDescriptors( |
| 350 | Handle<DescriptorArray> array, |
| 351 | Handle<Object> descriptors); |
| 352 | |
| 353 | static Handle<JSFunction> BaseNewFunctionFromBoilerplate( |
| 354 | Handle<JSFunction> boilerplate, |
| 355 | Handle<Map> function_map); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 356 | |
| 357 | // Create a new map cache. |
| 358 | static Handle<MapCache> NewMapCache(int at_least_space_for); |
| 359 | |
| 360 | // Update the map cache in the global context with (keys, map) |
| 361 | static Handle<MapCache> AddToMapCache(Handle<Context> context, |
| 362 | Handle<FixedArray> keys, |
| 363 | Handle<Map> map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 364 | }; |
| 365 | |
| 366 | |
| 367 | } } // namespace v8::internal |
| 368 | |
| 369 | #endif // V8_FACTORY_H_ |