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