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" |
| 32 | |
| 33 | namespace v8 { namespace internal { |
| 34 | |
| 35 | |
| 36 | // Interface for handle based allocation. |
| 37 | |
| 38 | class Factory : public AllStatic { |
| 39 | public: |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 40 | // Allocate a new fixed array with undefined entries. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | static Handle<FixedArray> NewFixedArray( |
| 42 | int size, |
| 43 | PretenureFlag pretenure = NOT_TENURED); |
| 44 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 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 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 101 | Handle<String> second); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 102 | |
| 103 | // Create a new sliced string object which represents a substring of a |
| 104 | // backing string. |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 105 | static Handle<String> NewStringSlice(Handle<String> str, |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 106 | int begin, |
| 107 | int end); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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, |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 127 | Handle<JSObject> extension, |
| 128 | bool is_catch_context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 129 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 130 | // Return the Symbol matching the passed in string. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 149 | static Handle<ByteArray> NewByteArray(int length, |
| 150 | PretenureFlag pretenure = NOT_TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 151 | |
| 152 | static Handle<Map> NewMap(InstanceType type, int instance_size); |
| 153 | |
| 154 | static Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function); |
| 155 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame^] | 156 | static Handle<Map> CopyMapDropDescriptors(Handle<Map> map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 157 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 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 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 162 | static Handle<Map> CopyMapDropTransitions(Handle<Map> map); |
| 163 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 171 | static Handle<Object> NewNumberFromUint(uint32_t value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 184 | // JS objects are pretenured when allocated by the bootstrapper and |
| 185 | // runtime. |
| 186 | static Handle<JSObject> NewJSObjectFromMap(Handle<Map> map); |
| 187 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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, |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 206 | Code::Flags flags, Handle<Object> self_reference); |
| 207 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 252 | bool contains_array_literal, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 274 | enum ApiInstanceType { |
| 275 | JavaScriptObject, |
| 276 | InnerGlobalObject, |
| 277 | OuterGlobalObject |
| 278 | }; |
| 279 | |
| 280 | static Handle<JSFunction> CreateApiFunction( |
| 281 | Handle<FunctionTemplateInfo> data, |
| 282 | ApiInstanceType type = JavaScriptObject); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 288 | // function template. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 303 | static Handle<String> hidden_symbol() { |
| 304 | return Handle<String>(&Heap::hidden_symbol_); |
| 305 | } |
| 306 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 313 | #ifdef ENABLE_DEBUGGER_SUPPORT |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 314 | static Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 315 | #endif |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 316 | |
| 317 | // Return a map using the map cache in the global context. |
| 318 | // The key the an ordered set of property names. |
| 319 | static Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context, |
| 320 | Handle<FixedArray> keys); |
| 321 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 322 | // Creates a new FixedArray that holds the data associated with the |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 323 | // atom regexp and stores it in the regexp. |
| 324 | static void SetRegExpAtomData(Handle<JSRegExp> regexp, |
| 325 | JSRegExp::Type type, |
| 326 | Handle<String> source, |
| 327 | JSRegExp::Flags flags, |
| 328 | Handle<Object> match_pattern); |
| 329 | |
| 330 | // Creates a new FixedArray that holds the data associated with the |
| 331 | // irregexp regexp and stores it in the regexp. |
| 332 | static void SetRegExpIrregexpData(Handle<JSRegExp> regexp, |
| 333 | JSRegExp::Type type, |
| 334 | Handle<String> source, |
| 335 | JSRegExp::Flags flags, |
| 336 | int capture_count); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 337 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 338 | private: |
| 339 | static Handle<JSFunction> NewFunctionHelper(Handle<String> name, |
| 340 | Handle<Object> prototype); |
| 341 | |
| 342 | static Handle<DescriptorArray> CopyAppendCallbackDescriptors( |
| 343 | Handle<DescriptorArray> array, |
| 344 | Handle<Object> descriptors); |
| 345 | |
| 346 | static Handle<JSFunction> BaseNewFunctionFromBoilerplate( |
| 347 | Handle<JSFunction> boilerplate, |
| 348 | Handle<Map> function_map); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 349 | |
| 350 | // Create a new map cache. |
| 351 | static Handle<MapCache> NewMapCache(int at_least_space_for); |
| 352 | |
| 353 | // Update the map cache in the global context with (keys, map) |
| 354 | static Handle<MapCache> AddToMapCache(Handle<Context> context, |
| 355 | Handle<FixedArray> keys, |
| 356 | Handle<Map> map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 357 | }; |
| 358 | |
| 359 | |
| 360 | } } // namespace v8::internal |
| 361 | |
| 362 | #endif // V8_FACTORY_H_ |