Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +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 | #include "v8.h" |
| 29 | |
| 30 | #include "api.h" |
| 31 | #include "debug.h" |
| 32 | #include "execution.h" |
| 33 | #include "factory.h" |
| 34 | #include "macro-assembler.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 35 | #include "objects.h" |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 36 | #include "objects-visiting.h" |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 37 | #include "scopeinfo.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 38 | |
| 39 | namespace v8 { |
| 40 | namespace internal { |
| 41 | |
| 42 | |
| 43 | Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) { |
| 44 | ASSERT(0 <= size); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 45 | CALL_HEAP_FUNCTION( |
| 46 | isolate(), |
| 47 | isolate()->heap()->AllocateFixedArray(size, pretenure), |
| 48 | FixedArray); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 52 | Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size, |
| 53 | PretenureFlag pretenure) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 54 | ASSERT(0 <= size); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 55 | CALL_HEAP_FUNCTION( |
| 56 | isolate(), |
| 57 | isolate()->heap()->AllocateFixedArrayWithHoles(size, pretenure), |
| 58 | FixedArray); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 62 | Handle<FixedArray> Factory::NewFixedDoubleArray(int size, |
| 63 | PretenureFlag pretenure) { |
| 64 | ASSERT(0 <= size); |
| 65 | CALL_HEAP_FUNCTION( |
| 66 | isolate(), |
| 67 | isolate()->heap()->AllocateUninitializedFixedDoubleArray(size, pretenure), |
| 68 | FixedArray); |
| 69 | } |
| 70 | |
| 71 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 72 | Handle<StringDictionary> Factory::NewStringDictionary(int at_least_space_for) { |
| 73 | ASSERT(0 <= at_least_space_for); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 74 | CALL_HEAP_FUNCTION(isolate(), |
| 75 | StringDictionary::Allocate(at_least_space_for), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 76 | StringDictionary); |
| 77 | } |
| 78 | |
| 79 | |
Ben Murdoch | 2b4ba11 | 2012-01-20 14:57:15 +0000 | [diff] [blame^] | 80 | Handle<SeededNumberDictionary> Factory::NewSeededNumberDictionary( |
| 81 | int at_least_space_for) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 82 | ASSERT(0 <= at_least_space_for); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 83 | CALL_HEAP_FUNCTION(isolate(), |
Ben Murdoch | 2b4ba11 | 2012-01-20 14:57:15 +0000 | [diff] [blame^] | 84 | SeededNumberDictionary::Allocate(at_least_space_for), |
| 85 | SeededNumberDictionary); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | Handle<UnseededNumberDictionary> Factory::NewUnseededNumberDictionary( |
| 90 | int at_least_space_for) { |
| 91 | ASSERT(0 <= at_least_space_for); |
| 92 | CALL_HEAP_FUNCTION(isolate(), |
| 93 | UnseededNumberDictionary::Allocate(at_least_space_for), |
| 94 | UnseededNumberDictionary); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 98 | Handle<ObjectHashTable> Factory::NewObjectHashTable(int at_least_space_for) { |
| 99 | ASSERT(0 <= at_least_space_for); |
| 100 | CALL_HEAP_FUNCTION(isolate(), |
| 101 | ObjectHashTable::Allocate(at_least_space_for), |
| 102 | ObjectHashTable); |
| 103 | } |
| 104 | |
| 105 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 106 | Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) { |
| 107 | ASSERT(0 <= number_of_descriptors); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 108 | CALL_HEAP_FUNCTION(isolate(), |
| 109 | DescriptorArray::Allocate(number_of_descriptors), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 110 | DescriptorArray); |
| 111 | } |
| 112 | |
| 113 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 114 | Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData( |
| 115 | int deopt_entry_count, |
| 116 | PretenureFlag pretenure) { |
| 117 | ASSERT(deopt_entry_count > 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 118 | CALL_HEAP_FUNCTION(isolate(), |
| 119 | DeoptimizationInputData::Allocate(deopt_entry_count, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 120 | pretenure), |
| 121 | DeoptimizationInputData); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData( |
| 126 | int deopt_entry_count, |
| 127 | PretenureFlag pretenure) { |
| 128 | ASSERT(deopt_entry_count > 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 129 | CALL_HEAP_FUNCTION(isolate(), |
| 130 | DeoptimizationOutputData::Allocate(deopt_entry_count, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 131 | pretenure), |
| 132 | DeoptimizationOutputData); |
| 133 | } |
| 134 | |
| 135 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 136 | // Symbols are created in the old generation (data space). |
| 137 | Handle<String> Factory::LookupSymbol(Vector<const char> string) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 138 | CALL_HEAP_FUNCTION(isolate(), |
| 139 | isolate()->heap()->LookupSymbol(string), |
| 140 | String); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 143 | // Symbols are created in the old generation (data space). |
| 144 | Handle<String> Factory::LookupSymbol(Handle<String> string) { |
| 145 | CALL_HEAP_FUNCTION(isolate(), |
| 146 | isolate()->heap()->LookupSymbol(*string), |
| 147 | String); |
| 148 | } |
| 149 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 150 | Handle<String> Factory::LookupAsciiSymbol(Vector<const char> string) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 151 | CALL_HEAP_FUNCTION(isolate(), |
| 152 | isolate()->heap()->LookupAsciiSymbol(string), |
| 153 | String); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 156 | |
| 157 | Handle<String> Factory::LookupAsciiSymbol(Handle<SeqAsciiString> string, |
| 158 | int from, |
| 159 | int length) { |
| 160 | CALL_HEAP_FUNCTION(isolate(), |
| 161 | isolate()->heap()->LookupAsciiSymbol(string, |
| 162 | from, |
| 163 | length), |
| 164 | String); |
| 165 | } |
| 166 | |
| 167 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 168 | Handle<String> Factory::LookupTwoByteSymbol(Vector<const uc16> string) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 169 | CALL_HEAP_FUNCTION(isolate(), |
| 170 | isolate()->heap()->LookupTwoByteSymbol(string), |
| 171 | String); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 174 | |
| 175 | Handle<String> Factory::NewStringFromAscii(Vector<const char> string, |
| 176 | PretenureFlag pretenure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 177 | CALL_HEAP_FUNCTION( |
| 178 | isolate(), |
| 179 | isolate()->heap()->AllocateStringFromAscii(string, pretenure), |
| 180 | String); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | Handle<String> Factory::NewStringFromUtf8(Vector<const char> string, |
| 184 | PretenureFlag pretenure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 185 | CALL_HEAP_FUNCTION( |
| 186 | isolate(), |
| 187 | isolate()->heap()->AllocateStringFromUtf8(string, pretenure), |
| 188 | String); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | |
| 192 | Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string, |
| 193 | PretenureFlag pretenure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 194 | CALL_HEAP_FUNCTION( |
| 195 | isolate(), |
| 196 | isolate()->heap()->AllocateStringFromTwoByte(string, pretenure), |
| 197 | String); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 201 | Handle<SeqAsciiString> Factory::NewRawAsciiString(int length, |
| 202 | PretenureFlag pretenure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 203 | CALL_HEAP_FUNCTION( |
| 204 | isolate(), |
| 205 | isolate()->heap()->AllocateRawAsciiString(length, pretenure), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 206 | SeqAsciiString); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 210 | Handle<SeqTwoByteString> Factory::NewRawTwoByteString(int length, |
| 211 | PretenureFlag pretenure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 212 | CALL_HEAP_FUNCTION( |
| 213 | isolate(), |
| 214 | isolate()->heap()->AllocateRawTwoByteString(length, pretenure), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 215 | SeqTwoByteString); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | |
| 219 | Handle<String> Factory::NewConsString(Handle<String> first, |
| 220 | Handle<String> second) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 221 | CALL_HEAP_FUNCTION(isolate(), |
| 222 | isolate()->heap()->AllocateConsString(*first, *second), |
| 223 | String); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 227 | Handle<String> Factory::NewSubString(Handle<String> str, |
| 228 | int begin, |
| 229 | int end) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 230 | CALL_HEAP_FUNCTION(isolate(), |
| 231 | str->SubString(begin, end), |
| 232 | String); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 236 | Handle<String> Factory::NewProperSubString(Handle<String> str, |
| 237 | int begin, |
| 238 | int end) { |
| 239 | ASSERT(begin > 0 || end < str->length()); |
| 240 | CALL_HEAP_FUNCTION(isolate(), |
| 241 | isolate()->heap()->AllocateSubString(*str, begin, end), |
| 242 | String); |
| 243 | } |
| 244 | |
| 245 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 246 | Handle<String> Factory::NewExternalStringFromAscii( |
| 247 | ExternalAsciiString::Resource* resource) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 248 | CALL_HEAP_FUNCTION( |
| 249 | isolate(), |
| 250 | isolate()->heap()->AllocateExternalStringFromAscii(resource), |
| 251 | String); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | |
| 255 | Handle<String> Factory::NewExternalStringFromTwoByte( |
| 256 | ExternalTwoByteString::Resource* resource) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 257 | CALL_HEAP_FUNCTION( |
| 258 | isolate(), |
| 259 | isolate()->heap()->AllocateExternalStringFromTwoByte(resource), |
| 260 | String); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | |
| 264 | Handle<Context> Factory::NewGlobalContext() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 265 | CALL_HEAP_FUNCTION( |
| 266 | isolate(), |
| 267 | isolate()->heap()->AllocateGlobalContext(), |
| 268 | Context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | |
| 272 | Handle<Context> Factory::NewFunctionContext(int length, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 273 | Handle<JSFunction> function) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 274 | CALL_HEAP_FUNCTION( |
| 275 | isolate(), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 276 | isolate()->heap()->AllocateFunctionContext(length, *function), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 277 | Context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 281 | Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function, |
| 282 | Handle<Context> previous, |
| 283 | Handle<String> name, |
| 284 | Handle<Object> thrown_object) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 285 | CALL_HEAP_FUNCTION( |
| 286 | isolate(), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 287 | isolate()->heap()->AllocateCatchContext(*function, |
| 288 | *previous, |
| 289 | *name, |
| 290 | *thrown_object), |
| 291 | Context); |
| 292 | } |
| 293 | |
| 294 | |
| 295 | Handle<Context> Factory::NewWithContext(Handle<JSFunction> function, |
| 296 | Handle<Context> previous, |
| 297 | Handle<JSObject> extension) { |
| 298 | CALL_HEAP_FUNCTION( |
| 299 | isolate(), |
| 300 | isolate()->heap()->AllocateWithContext(*function, *previous, *extension), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 301 | Context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 305 | Handle<Context> Factory::NewBlockContext( |
| 306 | Handle<JSFunction> function, |
| 307 | Handle<Context> previous, |
| 308 | Handle<SerializedScopeInfo> scope_info) { |
| 309 | CALL_HEAP_FUNCTION( |
| 310 | isolate(), |
| 311 | isolate()->heap()->AllocateBlockContext(*function, |
| 312 | *previous, |
| 313 | *scope_info), |
| 314 | Context); |
| 315 | } |
| 316 | |
| 317 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 318 | Handle<Struct> Factory::NewStruct(InstanceType type) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 319 | CALL_HEAP_FUNCTION( |
| 320 | isolate(), |
| 321 | isolate()->heap()->AllocateStruct(type), |
| 322 | Struct); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | |
| 326 | Handle<AccessorInfo> Factory::NewAccessorInfo() { |
| 327 | Handle<AccessorInfo> info = |
| 328 | Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE)); |
| 329 | info->set_flag(0); // Must clear the flag, it was initialized as undefined. |
| 330 | return info; |
| 331 | } |
| 332 | |
| 333 | |
| 334 | Handle<Script> Factory::NewScript(Handle<String> source) { |
| 335 | // Generate id for this script. |
| 336 | int id; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 337 | Heap* heap = isolate()->heap(); |
| 338 | if (heap->last_script_id()->IsUndefined()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 339 | // Script ids start from one. |
| 340 | id = 1; |
| 341 | } else { |
| 342 | // Increment id, wrap when positive smi is exhausted. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 343 | id = Smi::cast(heap->last_script_id())->value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 344 | id++; |
| 345 | if (!Smi::IsValid(id)) { |
| 346 | id = 0; |
| 347 | } |
| 348 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 349 | heap->SetLastScriptId(Smi::FromInt(id)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 350 | |
| 351 | // Create and initialize script object. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 352 | Handle<Foreign> wrapper = NewForeign(0, TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 353 | Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE)); |
| 354 | script->set_source(*source); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 355 | script->set_name(heap->undefined_value()); |
| 356 | script->set_id(heap->last_script_id()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 357 | script->set_line_offset(Smi::FromInt(0)); |
| 358 | script->set_column_offset(Smi::FromInt(0)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 359 | script->set_data(heap->undefined_value()); |
| 360 | script->set_context_data(heap->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 361 | script->set_type(Smi::FromInt(Script::TYPE_NORMAL)); |
| 362 | script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST)); |
| 363 | script->set_wrapper(*wrapper); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 364 | script->set_line_ends(heap->undefined_value()); |
| 365 | script->set_eval_from_shared(heap->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 366 | script->set_eval_from_instructions_offset(Smi::FromInt(0)); |
| 367 | |
| 368 | return script; |
| 369 | } |
| 370 | |
| 371 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 372 | Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 373 | CALL_HEAP_FUNCTION(isolate(), |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 374 | isolate()->heap()->AllocateForeign(addr, pretenure), |
| 375 | Foreign); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 379 | Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) { |
| 380 | return NewForeign((Address) desc, TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | |
| 384 | Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) { |
| 385 | ASSERT(0 <= length); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 386 | CALL_HEAP_FUNCTION( |
| 387 | isolate(), |
| 388 | isolate()->heap()->AllocateByteArray(length, pretenure), |
| 389 | ByteArray); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 393 | Handle<ExternalArray> Factory::NewExternalArray(int length, |
| 394 | ExternalArrayType array_type, |
| 395 | void* external_pointer, |
| 396 | PretenureFlag pretenure) { |
| 397 | ASSERT(0 <= length); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 398 | CALL_HEAP_FUNCTION( |
| 399 | isolate(), |
| 400 | isolate()->heap()->AllocateExternalArray(length, |
| 401 | array_type, |
| 402 | external_pointer, |
| 403 | pretenure), |
| 404 | ExternalArray); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 408 | Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell( |
| 409 | Handle<Object> value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 410 | CALL_HEAP_FUNCTION( |
| 411 | isolate(), |
| 412 | isolate()->heap()->AllocateJSGlobalPropertyCell(*value), |
| 413 | JSGlobalPropertyCell); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 417 | Handle<Map> Factory::NewMap(InstanceType type, int instance_size) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 418 | CALL_HEAP_FUNCTION( |
| 419 | isolate(), |
| 420 | isolate()->heap()->AllocateMap(type, instance_size), |
| 421 | Map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | |
| 425 | Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 426 | CALL_HEAP_FUNCTION( |
| 427 | isolate(), |
| 428 | isolate()->heap()->AllocateFunctionPrototype(*function), |
| 429 | JSObject); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | |
| 433 | Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 434 | CALL_HEAP_FUNCTION(isolate(), src->CopyDropDescriptors(), Map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | |
| 438 | Handle<Map> Factory::CopyMap(Handle<Map> src, |
| 439 | int extra_inobject_properties) { |
| 440 | Handle<Map> copy = CopyMapDropDescriptors(src); |
| 441 | // Check that we do not overflow the instance size when adding the |
| 442 | // extra inobject properties. |
| 443 | int instance_size_delta = extra_inobject_properties * kPointerSize; |
| 444 | int max_instance_size_delta = |
| 445 | JSObject::kMaxInstanceSize - copy->instance_size(); |
| 446 | if (instance_size_delta > max_instance_size_delta) { |
| 447 | // If the instance size overflows, we allocate as many properties |
| 448 | // as we can as inobject properties. |
| 449 | instance_size_delta = max_instance_size_delta; |
| 450 | extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2; |
| 451 | } |
| 452 | // Adjust the map with the extra inobject properties. |
| 453 | int inobject_properties = |
| 454 | copy->inobject_properties() + extra_inobject_properties; |
| 455 | copy->set_inobject_properties(inobject_properties); |
| 456 | copy->set_unused_property_fields(inobject_properties); |
| 457 | copy->set_instance_size(copy->instance_size() + instance_size_delta); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 458 | copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 459 | return copy; |
| 460 | } |
| 461 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 462 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 463 | Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 464 | CALL_HEAP_FUNCTION(isolate(), src->CopyDropTransitions(), Map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 468 | Handle<Map> Factory::GetFastElementsMap(Handle<Map> src) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 469 | CALL_HEAP_FUNCTION(isolate(), src->GetFastElementsMap(), Map); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | |
| 473 | Handle<Map> Factory::GetSlowElementsMap(Handle<Map> src) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 474 | CALL_HEAP_FUNCTION(isolate(), src->GetSlowElementsMap(), Map); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 478 | Handle<Map> Factory::GetElementsTransitionMap( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 479 | Handle<Map> src, |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 480 | ElementsKind elements_kind, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 481 | bool safe_to_add_transition) { |
| 482 | CALL_HEAP_FUNCTION(isolate(), |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 483 | src->GetElementsTransitionMap(elements_kind, |
| 484 | safe_to_add_transition), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 485 | Map); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 489 | Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 490 | CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 494 | Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo( |
| 495 | Handle<SharedFunctionInfo> function_info, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 496 | Handle<Map> function_map, |
| 497 | PretenureFlag pretenure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 498 | CALL_HEAP_FUNCTION( |
| 499 | isolate(), |
| 500 | isolate()->heap()->AllocateFunction(*function_map, |
| 501 | *function_info, |
| 502 | isolate()->heap()->the_hole_value(), |
| 503 | pretenure), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 504 | JSFunction); |
| 505 | } |
| 506 | |
| 507 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 508 | Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo( |
| 509 | Handle<SharedFunctionInfo> function_info, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 510 | Handle<Context> context, |
| 511 | PretenureFlag pretenure) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 512 | Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 513 | function_info, |
| 514 | function_info->strict_mode() |
| 515 | ? isolate()->strict_mode_function_map() |
| 516 | : isolate()->function_map(), |
| 517 | pretenure); |
| 518 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 519 | result->set_context(*context); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 520 | int number_of_literals = function_info->num_literals(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 521 | Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 522 | if (number_of_literals > 0) { |
| 523 | // Store the object, regexp and array functions in the literals |
| 524 | // array prefix. These functions will be used when creating |
| 525 | // object, regexp and array literals in this function. |
| 526 | literals->set(JSFunction::kLiteralGlobalContextIndex, |
| 527 | context->global_context()); |
| 528 | } |
| 529 | result->set_literals(*literals); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 530 | result->set_next_function_link(isolate()->heap()->undefined_value()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 531 | |
| 532 | if (V8::UseCrankshaft() && |
| 533 | FLAG_always_opt && |
| 534 | result->is_compiled() && |
| 535 | !function_info->is_toplevel() && |
| 536 | function_info->allows_lazy_compilation()) { |
| 537 | result->MarkForLazyRecompilation(); |
| 538 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 539 | return result; |
| 540 | } |
| 541 | |
| 542 | |
| 543 | Handle<Object> Factory::NewNumber(double value, |
| 544 | PretenureFlag pretenure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 545 | CALL_HEAP_FUNCTION( |
| 546 | isolate(), |
| 547 | isolate()->heap()->NumberFromDouble(value, pretenure), Object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | |
| 551 | Handle<Object> Factory::NewNumberFromInt(int value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 552 | CALL_HEAP_FUNCTION( |
| 553 | isolate(), |
| 554 | isolate()->heap()->NumberFromInt32(value), Object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | |
| 558 | Handle<Object> Factory::NewNumberFromUint(uint32_t value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 559 | CALL_HEAP_FUNCTION( |
| 560 | isolate(), |
| 561 | isolate()->heap()->NumberFromUint32(value), Object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | |
| 565 | Handle<JSObject> Factory::NewNeanderObject() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 566 | CALL_HEAP_FUNCTION( |
| 567 | isolate(), |
| 568 | isolate()->heap()->AllocateJSObjectFromMap( |
| 569 | isolate()->heap()->neander_map()), |
| 570 | JSObject); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | |
| 574 | Handle<Object> Factory::NewTypeError(const char* type, |
| 575 | Vector< Handle<Object> > args) { |
| 576 | return NewError("MakeTypeError", type, args); |
| 577 | } |
| 578 | |
| 579 | |
| 580 | Handle<Object> Factory::NewTypeError(Handle<String> message) { |
| 581 | return NewError("$TypeError", message); |
| 582 | } |
| 583 | |
| 584 | |
| 585 | Handle<Object> Factory::NewRangeError(const char* type, |
| 586 | Vector< Handle<Object> > args) { |
| 587 | return NewError("MakeRangeError", type, args); |
| 588 | } |
| 589 | |
| 590 | |
| 591 | Handle<Object> Factory::NewRangeError(Handle<String> message) { |
| 592 | return NewError("$RangeError", message); |
| 593 | } |
| 594 | |
| 595 | |
| 596 | Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) { |
| 597 | return NewError("MakeSyntaxError", type, args); |
| 598 | } |
| 599 | |
| 600 | |
| 601 | Handle<Object> Factory::NewSyntaxError(Handle<String> message) { |
| 602 | return NewError("$SyntaxError", message); |
| 603 | } |
| 604 | |
| 605 | |
| 606 | Handle<Object> Factory::NewReferenceError(const char* type, |
| 607 | Vector< Handle<Object> > args) { |
| 608 | return NewError("MakeReferenceError", type, args); |
| 609 | } |
| 610 | |
| 611 | |
| 612 | Handle<Object> Factory::NewReferenceError(Handle<String> message) { |
| 613 | return NewError("$ReferenceError", message); |
| 614 | } |
| 615 | |
| 616 | |
| 617 | Handle<Object> Factory::NewError(const char* maker, const char* type, |
| 618 | Vector< Handle<Object> > args) { |
| 619 | v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 620 | Handle<FixedArray> array = NewFixedArray(args.length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 621 | for (int i = 0; i < args.length(); i++) { |
| 622 | array->set(i, *args[i]); |
| 623 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 624 | Handle<JSArray> object = NewJSArrayWithElements(array); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 625 | Handle<Object> result = NewError(maker, type, object); |
| 626 | return result.EscapeFrom(&scope); |
| 627 | } |
| 628 | |
| 629 | |
| 630 | Handle<Object> Factory::NewEvalError(const char* type, |
| 631 | Vector< Handle<Object> > args) { |
| 632 | return NewError("MakeEvalError", type, args); |
| 633 | } |
| 634 | |
| 635 | |
| 636 | Handle<Object> Factory::NewError(const char* type, |
| 637 | Vector< Handle<Object> > args) { |
| 638 | return NewError("MakeError", type, args); |
| 639 | } |
| 640 | |
| 641 | |
| 642 | Handle<Object> Factory::NewError(const char* maker, |
| 643 | const char* type, |
| 644 | Handle<JSArray> args) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 645 | Handle<String> make_str = LookupAsciiSymbol(maker); |
| 646 | Handle<Object> fun_obj( |
| 647 | isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 648 | // If the builtins haven't been properly configured yet this error |
| 649 | // constructor may not have been defined. Bail out. |
| 650 | if (!fun_obj->IsJSFunction()) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 651 | return undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 652 | Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 653 | Handle<Object> type_obj = LookupAsciiSymbol(type); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 654 | Object** argv[2] = { type_obj.location(), |
| 655 | Handle<Object>::cast(args).location() }; |
| 656 | |
| 657 | // Invoke the JavaScript factory method. If an exception is thrown while |
| 658 | // running the factory method, use the exception as the result. |
| 659 | bool caught_exception; |
| 660 | Handle<Object> result = Execution::TryCall(fun, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 661 | isolate()->js_builtins_object(), 2, argv, &caught_exception); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 662 | return result; |
| 663 | } |
| 664 | |
| 665 | |
| 666 | Handle<Object> Factory::NewError(Handle<String> message) { |
| 667 | return NewError("$Error", message); |
| 668 | } |
| 669 | |
| 670 | |
| 671 | Handle<Object> Factory::NewError(const char* constructor, |
| 672 | Handle<String> message) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 673 | Handle<String> constr = LookupAsciiSymbol(constructor); |
| 674 | Handle<JSFunction> fun = Handle<JSFunction>( |
| 675 | JSFunction::cast(isolate()->js_builtins_object()-> |
| 676 | GetPropertyNoExceptionThrown(*constr))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 677 | Object** argv[1] = { Handle<Object>::cast(message).location() }; |
| 678 | |
| 679 | // Invoke the JavaScript factory method. If an exception is thrown while |
| 680 | // running the factory method, use the exception as the result. |
| 681 | bool caught_exception; |
| 682 | Handle<Object> result = Execution::TryCall(fun, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 683 | isolate()->js_builtins_object(), 1, argv, &caught_exception); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 684 | return result; |
| 685 | } |
| 686 | |
| 687 | |
| 688 | Handle<JSFunction> Factory::NewFunction(Handle<String> name, |
| 689 | InstanceType type, |
| 690 | int instance_size, |
| 691 | Handle<Code> code, |
| 692 | bool force_initial_map) { |
| 693 | // Allocate the function |
| 694 | Handle<JSFunction> function = NewFunction(name, the_hole_value()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 695 | |
| 696 | // Setup the code pointer in both the shared function info and in |
| 697 | // the function itself. |
| 698 | function->shared()->set_code(*code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 699 | function->set_code(*code); |
| 700 | |
| 701 | if (force_initial_map || |
| 702 | type != JS_OBJECT_TYPE || |
| 703 | instance_size != JSObject::kHeaderSize) { |
| 704 | Handle<Map> initial_map = NewMap(type, instance_size); |
| 705 | Handle<JSObject> prototype = NewFunctionPrototype(function); |
| 706 | initial_map->set_prototype(*prototype); |
| 707 | function->set_initial_map(*initial_map); |
| 708 | initial_map->set_constructor(*function); |
| 709 | } else { |
| 710 | ASSERT(!function->has_initial_map()); |
| 711 | ASSERT(!function->has_prototype()); |
| 712 | } |
| 713 | |
| 714 | return function; |
| 715 | } |
| 716 | |
| 717 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 718 | Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name, |
| 719 | InstanceType type, |
| 720 | int instance_size, |
| 721 | Handle<JSObject> prototype, |
| 722 | Handle<Code> code, |
| 723 | bool force_initial_map) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 724 | // Allocate the function. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 725 | Handle<JSFunction> function = NewFunction(name, prototype); |
| 726 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 727 | // Setup the code pointer in both the shared function info and in |
| 728 | // the function itself. |
| 729 | function->shared()->set_code(*code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 730 | function->set_code(*code); |
| 731 | |
| 732 | if (force_initial_map || |
| 733 | type != JS_OBJECT_TYPE || |
| 734 | instance_size != JSObject::kHeaderSize) { |
| 735 | Handle<Map> initial_map = NewMap(type, instance_size); |
| 736 | function->set_initial_map(*initial_map); |
| 737 | initial_map->set_constructor(*function); |
| 738 | } |
| 739 | |
| 740 | // Set function.prototype and give the prototype a constructor |
| 741 | // property that refers to the function. |
| 742 | SetPrototypeProperty(function, prototype); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 743 | // Currently safe because it is only invoked from Genesis. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 744 | SetLocalPropertyNoThrow(prototype, constructor_symbol(), function, DONT_ENUM); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 745 | return function; |
| 746 | } |
| 747 | |
| 748 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 749 | Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name, |
| 750 | Handle<Code> code) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 751 | Handle<JSFunction> function = NewFunctionWithoutPrototype(name, |
| 752 | kNonStrictMode); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 753 | function->shared()->set_code(*code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 754 | function->set_code(*code); |
| 755 | ASSERT(!function->has_initial_map()); |
| 756 | ASSERT(!function->has_prototype()); |
| 757 | return function; |
| 758 | } |
| 759 | |
| 760 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 761 | Handle<SerializedScopeInfo> Factory::NewSerializedScopeInfo(int length) { |
| 762 | CALL_HEAP_FUNCTION( |
| 763 | isolate(), |
| 764 | isolate()->heap()->AllocateSerializedScopeInfo(length), |
| 765 | SerializedScopeInfo); |
| 766 | } |
| 767 | |
| 768 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 769 | Handle<Code> Factory::NewCode(const CodeDesc& desc, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 770 | Code::Flags flags, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 771 | Handle<Object> self_ref, |
| 772 | bool immovable) { |
| 773 | CALL_HEAP_FUNCTION(isolate(), |
| 774 | isolate()->heap()->CreateCode( |
| 775 | desc, flags, self_ref, immovable), |
| 776 | Code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | |
| 780 | Handle<Code> Factory::CopyCode(Handle<Code> code) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 781 | CALL_HEAP_FUNCTION(isolate(), |
| 782 | isolate()->heap()->CopyCode(*code), |
| 783 | Code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 787 | Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 788 | CALL_HEAP_FUNCTION(isolate(), |
| 789 | isolate()->heap()->CopyCode(*code, reloc_info), |
| 790 | Code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 794 | MUST_USE_RESULT static inline MaybeObject* DoCopyInsert( |
| 795 | DescriptorArray* array, |
| 796 | String* key, |
| 797 | Object* value, |
| 798 | PropertyAttributes attributes) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 799 | CallbacksDescriptor desc(key, value, attributes); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 800 | MaybeObject* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 801 | return obj; |
| 802 | } |
| 803 | |
| 804 | |
| 805 | // Allocate the new array. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 806 | Handle<DescriptorArray> Factory::CopyAppendForeignDescriptor( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 807 | Handle<DescriptorArray> array, |
| 808 | Handle<String> key, |
| 809 | Handle<Object> value, |
| 810 | PropertyAttributes attributes) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 811 | CALL_HEAP_FUNCTION(isolate(), |
| 812 | DoCopyInsert(*array, *key, *value, attributes), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 813 | DescriptorArray); |
| 814 | } |
| 815 | |
| 816 | |
| 817 | Handle<String> Factory::SymbolFromString(Handle<String> value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 818 | CALL_HEAP_FUNCTION(isolate(), |
| 819 | isolate()->heap()->LookupSymbol(*value), String); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | |
| 823 | Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors( |
| 824 | Handle<DescriptorArray> array, |
| 825 | Handle<Object> descriptors) { |
| 826 | v8::NeanderArray callbacks(descriptors); |
| 827 | int nof_callbacks = callbacks.length(); |
| 828 | Handle<DescriptorArray> result = |
| 829 | NewDescriptorArray(array->number_of_descriptors() + nof_callbacks); |
| 830 | |
| 831 | // Number of descriptors added to the result so far. |
| 832 | int descriptor_count = 0; |
| 833 | |
| 834 | // Copy the descriptors from the array. |
| 835 | for (int i = 0; i < array->number_of_descriptors(); i++) { |
| 836 | if (array->GetType(i) != NULL_DESCRIPTOR) { |
| 837 | result->CopyFrom(descriptor_count++, *array, i); |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | // Number of duplicates detected. |
| 842 | int duplicates = 0; |
| 843 | |
| 844 | // Fill in new callback descriptors. Process the callbacks from |
| 845 | // back to front so that the last callback with a given name takes |
| 846 | // precedence over previously added callbacks with that name. |
| 847 | for (int i = nof_callbacks - 1; i >= 0; i--) { |
| 848 | Handle<AccessorInfo> entry = |
| 849 | Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i))); |
| 850 | // Ensure the key is a symbol before writing into the instance descriptor. |
| 851 | Handle<String> key = |
| 852 | SymbolFromString(Handle<String>(String::cast(entry->name()))); |
| 853 | // Check if a descriptor with this name already exists before writing. |
| 854 | if (result->LinearSearch(*key, descriptor_count) == |
| 855 | DescriptorArray::kNotFound) { |
| 856 | CallbacksDescriptor desc(*key, *entry, entry->property_attributes()); |
| 857 | result->Set(descriptor_count, &desc); |
| 858 | descriptor_count++; |
| 859 | } else { |
| 860 | duplicates++; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | // If duplicates were detected, allocate a result of the right size |
| 865 | // and transfer the elements. |
| 866 | if (duplicates > 0) { |
| 867 | int number_of_descriptors = result->number_of_descriptors() - duplicates; |
| 868 | Handle<DescriptorArray> new_result = |
| 869 | NewDescriptorArray(number_of_descriptors); |
| 870 | for (int i = 0; i < number_of_descriptors; i++) { |
| 871 | new_result->CopyFrom(i, *result, i); |
| 872 | } |
| 873 | result = new_result; |
| 874 | } |
| 875 | |
| 876 | // Sort the result before returning. |
| 877 | result->Sort(); |
| 878 | return result; |
| 879 | } |
| 880 | |
| 881 | |
| 882 | Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor, |
| 883 | PretenureFlag pretenure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 884 | CALL_HEAP_FUNCTION( |
| 885 | isolate(), |
| 886 | isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | |
| 890 | Handle<GlobalObject> Factory::NewGlobalObject( |
| 891 | Handle<JSFunction> constructor) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 892 | CALL_HEAP_FUNCTION(isolate(), |
| 893 | isolate()->heap()->AllocateGlobalObject(*constructor), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 894 | GlobalObject); |
| 895 | } |
| 896 | |
| 897 | |
| 898 | |
| 899 | Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 900 | CALL_HEAP_FUNCTION( |
| 901 | isolate(), |
| 902 | isolate()->heap()->AllocateJSObjectFromMap(*map, NOT_TENURED), |
| 903 | JSObject); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 907 | Handle<JSArray> Factory::NewJSArray(int capacity, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 908 | PretenureFlag pretenure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 909 | Handle<JSObject> obj = NewJSObject(isolate()->array_function(), pretenure); |
| 910 | CALL_HEAP_FUNCTION(isolate(), |
| 911 | Handle<JSArray>::cast(obj)->Initialize(capacity), |
| 912 | JSArray); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | |
| 916 | Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements, |
| 917 | PretenureFlag pretenure) { |
| 918 | Handle<JSArray> result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 919 | Handle<JSArray>::cast(NewJSObject(isolate()->array_function(), |
| 920 | pretenure)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 921 | result->SetContent(*elements); |
| 922 | return result; |
| 923 | } |
| 924 | |
| 925 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 926 | Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler, |
| 927 | Handle<Object> prototype) { |
| 928 | CALL_HEAP_FUNCTION( |
| 929 | isolate(), |
| 930 | isolate()->heap()->AllocateJSProxy(*handler, *prototype), |
| 931 | JSProxy); |
| 932 | } |
| 933 | |
| 934 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 935 | void Factory::BecomeJSObject(Handle<JSReceiver> object) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 936 | CALL_HEAP_FUNCTION_VOID( |
| 937 | isolate(), |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 938 | isolate()->heap()->ReinitializeJSReceiver( |
| 939 | *object, JS_OBJECT_TYPE, JSObject::kHeaderSize)); |
| 940 | } |
| 941 | |
| 942 | |
| 943 | void Factory::BecomeJSFunction(Handle<JSReceiver> object) { |
| 944 | CALL_HEAP_FUNCTION_VOID( |
| 945 | isolate(), |
| 946 | isolate()->heap()->ReinitializeJSReceiver( |
| 947 | *object, JS_FUNCTION_TYPE, JSFunction::kSize)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 951 | Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo( |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 952 | Handle<String> name, |
| 953 | int number_of_literals, |
| 954 | Handle<Code> code, |
| 955 | Handle<SerializedScopeInfo> scope_info) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 956 | Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name); |
| 957 | shared->set_code(*code); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 958 | shared->set_scope_info(*scope_info); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 959 | int literals_array_size = number_of_literals; |
| 960 | // If the function contains object, regexp or array literals, |
| 961 | // allocate extra space for a literals array prefix containing the |
| 962 | // context. |
| 963 | if (number_of_literals > 0) { |
| 964 | literals_array_size += JSFunction::kLiteralsPrefixSize; |
| 965 | } |
| 966 | shared->set_num_literals(literals_array_size); |
| 967 | return shared; |
| 968 | } |
| 969 | |
| 970 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 971 | Handle<JSMessageObject> Factory::NewJSMessageObject( |
| 972 | Handle<String> type, |
| 973 | Handle<JSArray> arguments, |
| 974 | int start_position, |
| 975 | int end_position, |
| 976 | Handle<Object> script, |
| 977 | Handle<Object> stack_trace, |
| 978 | Handle<Object> stack_frames) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 979 | CALL_HEAP_FUNCTION(isolate(), |
| 980 | isolate()->heap()->AllocateJSMessageObject(*type, |
| 981 | *arguments, |
| 982 | start_position, |
| 983 | end_position, |
| 984 | *script, |
| 985 | *stack_trace, |
| 986 | *stack_frames), |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 987 | JSMessageObject); |
| 988 | } |
| 989 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 990 | Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 991 | CALL_HEAP_FUNCTION(isolate(), |
| 992 | isolate()->heap()->AllocateSharedFunctionInfo(*name), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 993 | SharedFunctionInfo); |
| 994 | } |
| 995 | |
| 996 | |
| 997 | Handle<String> Factory::NumberToString(Handle<Object> number) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 998 | CALL_HEAP_FUNCTION(isolate(), |
| 999 | isolate()->heap()->NumberToString(*number), String); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | |
Ben Murdoch | 2b4ba11 | 2012-01-20 14:57:15 +0000 | [diff] [blame^] | 1003 | Handle<SeededNumberDictionary> Factory::DictionaryAtNumberPut( |
| 1004 | Handle<SeededNumberDictionary> dictionary, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1005 | uint32_t key, |
| 1006 | Handle<Object> value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1007 | CALL_HEAP_FUNCTION(isolate(), |
| 1008 | dictionary->AtNumberPut(key, *value), |
Ben Murdoch | 2b4ba11 | 2012-01-20 14:57:15 +0000 | [diff] [blame^] | 1009 | SeededNumberDictionary); |
| 1010 | } |
| 1011 | |
| 1012 | |
| 1013 | Handle<UnseededNumberDictionary> Factory::DictionaryAtNumberPut( |
| 1014 | Handle<UnseededNumberDictionary> dictionary, |
| 1015 | uint32_t key, |
| 1016 | Handle<Object> value) { |
| 1017 | CALL_HEAP_FUNCTION(isolate(), |
| 1018 | dictionary->AtNumberPut(key, *value), |
| 1019 | UnseededNumberDictionary); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | |
| 1023 | Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name, |
| 1024 | Handle<Object> prototype) { |
| 1025 | Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1026 | CALL_HEAP_FUNCTION( |
| 1027 | isolate(), |
| 1028 | isolate()->heap()->AllocateFunction(*isolate()->function_map(), |
| 1029 | *function_share, |
| 1030 | *prototype), |
| 1031 | JSFunction); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | |
| 1035 | Handle<JSFunction> Factory::NewFunction(Handle<String> name, |
| 1036 | Handle<Object> prototype) { |
| 1037 | Handle<JSFunction> fun = NewFunctionHelper(name, prototype); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1038 | fun->set_context(isolate()->context()->global_context()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1039 | return fun; |
| 1040 | } |
| 1041 | |
| 1042 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1043 | Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1044 | Handle<String> name, |
| 1045 | StrictModeFlag strict_mode) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1046 | Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1047 | Handle<Map> map = strict_mode == kStrictMode |
| 1048 | ? isolate()->strict_mode_function_without_prototype_map() |
| 1049 | : isolate()->function_without_prototype_map(); |
| 1050 | CALL_HEAP_FUNCTION(isolate(), |
| 1051 | isolate()->heap()->AllocateFunction( |
| 1052 | *map, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1053 | *function_share, |
| 1054 | *the_hole_value()), |
| 1055 | JSFunction); |
| 1056 | } |
| 1057 | |
| 1058 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1059 | Handle<JSFunction> Factory::NewFunctionWithoutPrototype( |
| 1060 | Handle<String> name, |
| 1061 | StrictModeFlag strict_mode) { |
| 1062 | Handle<JSFunction> fun = NewFunctionWithoutPrototypeHelper(name, strict_mode); |
| 1063 | fun->set_context(isolate()->context()->global_context()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1064 | return fun; |
| 1065 | } |
| 1066 | |
| 1067 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1068 | Handle<Object> Factory::ToObject(Handle<Object> object) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1069 | CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1073 | Handle<Object> Factory::ToObject(Handle<Object> object, |
| 1074 | Handle<Context> global_context) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1075 | CALL_HEAP_FUNCTION(isolate(), object->ToObject(*global_context), Object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | |
| 1079 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 1080 | Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) { |
| 1081 | // Get the original code of the function. |
| 1082 | Handle<Code> code(shared->code()); |
| 1083 | |
| 1084 | // Create a copy of the code before allocating the debug info object to avoid |
| 1085 | // allocation while setting up the debug info object. |
| 1086 | Handle<Code> original_code(*Factory::CopyCode(code)); |
| 1087 | |
| 1088 | // Allocate initial fixed array for active break points before allocating the |
| 1089 | // debug info object to avoid allocation while setting up the debug info |
| 1090 | // object. |
| 1091 | Handle<FixedArray> break_points( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1092 | NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1093 | |
| 1094 | // Create and set up the debug info object. Debug info contains function, a |
| 1095 | // copy of the original code, the executing code and initial fixed array for |
| 1096 | // active break points. |
| 1097 | Handle<DebugInfo> debug_info = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1098 | Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1099 | debug_info->set_shared(*shared); |
| 1100 | debug_info->set_original_code(*original_code); |
| 1101 | debug_info->set_code(*code); |
| 1102 | debug_info->set_break_points(*break_points); |
| 1103 | |
| 1104 | // Link debug info to function. |
| 1105 | shared->set_debug_info(*debug_info); |
| 1106 | |
| 1107 | return debug_info; |
| 1108 | } |
| 1109 | #endif |
| 1110 | |
| 1111 | |
| 1112 | Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee, |
| 1113 | int length) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1114 | CALL_HEAP_FUNCTION( |
| 1115 | isolate(), |
| 1116 | isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | |
| 1120 | Handle<JSFunction> Factory::CreateApiFunction( |
| 1121 | Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1122 | Handle<Code> code = isolate()->builtins()->HandleApiCall(); |
| 1123 | Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1124 | |
| 1125 | int internal_field_count = 0; |
| 1126 | if (!obj->instance_template()->IsUndefined()) { |
| 1127 | Handle<ObjectTemplateInfo> instance_template = |
| 1128 | Handle<ObjectTemplateInfo>( |
| 1129 | ObjectTemplateInfo::cast(obj->instance_template())); |
| 1130 | internal_field_count = |
| 1131 | Smi::cast(instance_template->internal_field_count())->value(); |
| 1132 | } |
| 1133 | |
| 1134 | int instance_size = kPointerSize * internal_field_count; |
| 1135 | InstanceType type = INVALID_TYPE; |
| 1136 | switch (instance_type) { |
| 1137 | case JavaScriptObject: |
| 1138 | type = JS_OBJECT_TYPE; |
| 1139 | instance_size += JSObject::kHeaderSize; |
| 1140 | break; |
| 1141 | case InnerGlobalObject: |
| 1142 | type = JS_GLOBAL_OBJECT_TYPE; |
| 1143 | instance_size += JSGlobalObject::kSize; |
| 1144 | break; |
| 1145 | case OuterGlobalObject: |
| 1146 | type = JS_GLOBAL_PROXY_TYPE; |
| 1147 | instance_size += JSGlobalProxy::kSize; |
| 1148 | break; |
| 1149 | default: |
| 1150 | break; |
| 1151 | } |
| 1152 | ASSERT(type != INVALID_TYPE); |
| 1153 | |
| 1154 | Handle<JSFunction> result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1155 | NewFunction(Factory::empty_symbol(), |
| 1156 | type, |
| 1157 | instance_size, |
| 1158 | code, |
| 1159 | true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1160 | // Set class name. |
| 1161 | Handle<Object> class_name = Handle<Object>(obj->class_name()); |
| 1162 | if (class_name->IsString()) { |
| 1163 | result->shared()->set_instance_class_name(*class_name); |
| 1164 | result->shared()->set_name(*class_name); |
| 1165 | } |
| 1166 | |
| 1167 | Handle<Map> map = Handle<Map>(result->initial_map()); |
| 1168 | |
| 1169 | // Mark as undetectable if needed. |
| 1170 | if (obj->undetectable()) { |
| 1171 | map->set_is_undetectable(); |
| 1172 | } |
| 1173 | |
| 1174 | // Mark as hidden for the __proto__ accessor if needed. |
| 1175 | if (obj->hidden_prototype()) { |
| 1176 | map->set_is_hidden_prototype(); |
| 1177 | } |
| 1178 | |
| 1179 | // Mark as needs_access_check if needed. |
| 1180 | if (obj->needs_access_check()) { |
| 1181 | map->set_is_access_check_needed(true); |
| 1182 | } |
| 1183 | |
| 1184 | // Set interceptor information in the map. |
| 1185 | if (!obj->named_property_handler()->IsUndefined()) { |
| 1186 | map->set_has_named_interceptor(); |
| 1187 | } |
| 1188 | if (!obj->indexed_property_handler()->IsUndefined()) { |
| 1189 | map->set_has_indexed_interceptor(); |
| 1190 | } |
| 1191 | |
| 1192 | // Set instance call-as-function information in the map. |
| 1193 | if (!obj->instance_call_handler()->IsUndefined()) { |
| 1194 | map->set_has_instance_call_handler(); |
| 1195 | } |
| 1196 | |
| 1197 | result->shared()->set_function_data(*obj); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1198 | result->shared()->set_construct_stub(*construct_stub); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1199 | result->shared()->DontAdaptArguments(); |
| 1200 | |
| 1201 | // Recursively copy parent templates' accessors, 'data' may be modified. |
| 1202 | Handle<DescriptorArray> array = |
| 1203 | Handle<DescriptorArray>(map->instance_descriptors()); |
| 1204 | while (true) { |
| 1205 | Handle<Object> props = Handle<Object>(obj->property_accessors()); |
| 1206 | if (!props->IsUndefined()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1207 | array = CopyAppendCallbackDescriptors(array, props); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1208 | } |
| 1209 | Handle<Object> parent = Handle<Object>(obj->parent_template()); |
| 1210 | if (parent->IsUndefined()) break; |
| 1211 | obj = Handle<FunctionTemplateInfo>::cast(parent); |
| 1212 | } |
| 1213 | if (!array->IsEmpty()) { |
| 1214 | map->set_instance_descriptors(*array); |
| 1215 | } |
| 1216 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1217 | ASSERT(result->shared()->IsApiFunction()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1218 | return result; |
| 1219 | } |
| 1220 | |
| 1221 | |
| 1222 | Handle<MapCache> Factory::NewMapCache(int at_least_space_for) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1223 | CALL_HEAP_FUNCTION(isolate(), |
| 1224 | MapCache::Allocate(at_least_space_for), MapCache); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1228 | MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context, |
| 1229 | FixedArray* keys, |
| 1230 | Map* map) { |
| 1231 | Object* result; |
| 1232 | { MaybeObject* maybe_result = |
| 1233 | MapCache::cast(context->map_cache())->Put(keys, map); |
| 1234 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 1235 | } |
| 1236 | context->set_map_cache(MapCache::cast(result)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1237 | return result; |
| 1238 | } |
| 1239 | |
| 1240 | |
| 1241 | Handle<MapCache> Factory::AddToMapCache(Handle<Context> context, |
| 1242 | Handle<FixedArray> keys, |
| 1243 | Handle<Map> map) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1244 | CALL_HEAP_FUNCTION(isolate(), |
| 1245 | UpdateMapCacheWith(*context, *keys, *map), MapCache); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | |
| 1249 | Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context, |
| 1250 | Handle<FixedArray> keys) { |
| 1251 | if (context->map_cache()->IsUndefined()) { |
| 1252 | // Allocate the new map cache for the global context. |
| 1253 | Handle<MapCache> new_cache = NewMapCache(24); |
| 1254 | context->set_map_cache(*new_cache); |
| 1255 | } |
| 1256 | // Check to see whether there is a matching element in the cache. |
| 1257 | Handle<MapCache> cache = |
| 1258 | Handle<MapCache>(MapCache::cast(context->map_cache())); |
| 1259 | Handle<Object> result = Handle<Object>(cache->Lookup(*keys)); |
| 1260 | if (result->IsMap()) return Handle<Map>::cast(result); |
| 1261 | // Create a new map and add it to the cache. |
| 1262 | Handle<Map> map = |
| 1263 | CopyMap(Handle<Map>(context->object_function()->initial_map()), |
| 1264 | keys->length()); |
| 1265 | AddToMapCache(context, keys, map); |
| 1266 | return Handle<Map>(map); |
| 1267 | } |
| 1268 | |
| 1269 | |
| 1270 | void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp, |
| 1271 | JSRegExp::Type type, |
| 1272 | Handle<String> source, |
| 1273 | JSRegExp::Flags flags, |
| 1274 | Handle<Object> data) { |
| 1275 | Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize); |
| 1276 | |
| 1277 | store->set(JSRegExp::kTagIndex, Smi::FromInt(type)); |
| 1278 | store->set(JSRegExp::kSourceIndex, *source); |
| 1279 | store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value())); |
| 1280 | store->set(JSRegExp::kAtomPatternIndex, *data); |
| 1281 | regexp->set_data(*store); |
| 1282 | } |
| 1283 | |
| 1284 | void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp, |
| 1285 | JSRegExp::Type type, |
| 1286 | Handle<String> source, |
| 1287 | JSRegExp::Flags flags, |
| 1288 | int capture_count) { |
| 1289 | Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1290 | Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1291 | store->set(JSRegExp::kTagIndex, Smi::FromInt(type)); |
| 1292 | store->set(JSRegExp::kSourceIndex, *source); |
| 1293 | store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value())); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1294 | store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized); |
| 1295 | store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized); |
| 1296 | store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized); |
| 1297 | store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1298 | store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0)); |
| 1299 | store->set(JSRegExp::kIrregexpCaptureCountIndex, |
| 1300 | Smi::FromInt(capture_count)); |
| 1301 | regexp->set_data(*store); |
| 1302 | } |
| 1303 | |
| 1304 | |
| 1305 | |
| 1306 | void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc, |
| 1307 | Handle<JSObject> instance, |
| 1308 | bool* pending_exception) { |
| 1309 | // Configure the instance by adding the properties specified by the |
| 1310 | // instance template. |
| 1311 | Handle<Object> instance_template = Handle<Object>(desc->instance_template()); |
| 1312 | if (!instance_template->IsUndefined()) { |
| 1313 | Execution::ConfigureInstance(instance, |
| 1314 | instance_template, |
| 1315 | pending_exception); |
| 1316 | } else { |
| 1317 | *pending_exception = false; |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | |
| 1322 | } } // namespace v8::internal |