Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #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" |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 35 | #include "objects-visiting.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 36 | |
| 37 | namespace v8 { |
| 38 | namespace internal { |
| 39 | |
| 40 | |
| 41 | Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) { |
| 42 | ASSERT(0 <= size); |
| 43 | CALL_HEAP_FUNCTION(Heap::AllocateFixedArray(size, pretenure), FixedArray); |
| 44 | } |
| 45 | |
| 46 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 47 | Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size, |
| 48 | PretenureFlag pretenure) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 49 | ASSERT(0 <= size); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 50 | CALL_HEAP_FUNCTION(Heap::AllocateFixedArrayWithHoles(size, pretenure), |
| 51 | FixedArray); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | |
| 55 | Handle<StringDictionary> Factory::NewStringDictionary(int at_least_space_for) { |
| 56 | ASSERT(0 <= at_least_space_for); |
| 57 | CALL_HEAP_FUNCTION(StringDictionary::Allocate(at_least_space_for), |
| 58 | StringDictionary); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | Handle<NumberDictionary> Factory::NewNumberDictionary(int at_least_space_for) { |
| 63 | ASSERT(0 <= at_least_space_for); |
| 64 | CALL_HEAP_FUNCTION(NumberDictionary::Allocate(at_least_space_for), |
| 65 | NumberDictionary); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) { |
| 70 | ASSERT(0 <= number_of_descriptors); |
| 71 | CALL_HEAP_FUNCTION(DescriptorArray::Allocate(number_of_descriptors), |
| 72 | DescriptorArray); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | // Symbols are created in the old generation (data space). |
| 77 | Handle<String> Factory::LookupSymbol(Vector<const char> string) { |
| 78 | CALL_HEAP_FUNCTION(Heap::LookupSymbol(string), String); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | Handle<String> Factory::NewStringFromAscii(Vector<const char> string, |
| 83 | PretenureFlag pretenure) { |
| 84 | CALL_HEAP_FUNCTION(Heap::AllocateStringFromAscii(string, pretenure), String); |
| 85 | } |
| 86 | |
| 87 | Handle<String> Factory::NewStringFromUtf8(Vector<const char> string, |
| 88 | PretenureFlag pretenure) { |
| 89 | CALL_HEAP_FUNCTION(Heap::AllocateStringFromUtf8(string, pretenure), String); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string, |
| 94 | PretenureFlag pretenure) { |
| 95 | CALL_HEAP_FUNCTION(Heap::AllocateStringFromTwoByte(string, pretenure), |
| 96 | String); |
| 97 | } |
| 98 | |
| 99 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 100 | Handle<String> Factory::NewRawAsciiString(int length, |
| 101 | PretenureFlag pretenure) { |
| 102 | CALL_HEAP_FUNCTION(Heap::AllocateRawAsciiString(length, pretenure), String); |
| 103 | } |
| 104 | |
| 105 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 106 | Handle<String> Factory::NewRawTwoByteString(int length, |
| 107 | PretenureFlag pretenure) { |
| 108 | CALL_HEAP_FUNCTION(Heap::AllocateRawTwoByteString(length, pretenure), String); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | Handle<String> Factory::NewConsString(Handle<String> first, |
| 113 | Handle<String> second) { |
| 114 | CALL_HEAP_FUNCTION(Heap::AllocateConsString(*first, *second), String); |
| 115 | } |
| 116 | |
| 117 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 118 | Handle<String> Factory::NewSubString(Handle<String> str, |
| 119 | int begin, |
| 120 | int end) { |
| 121 | CALL_HEAP_FUNCTION(str->SubString(begin, end), String); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | |
| 125 | Handle<String> Factory::NewExternalStringFromAscii( |
| 126 | ExternalAsciiString::Resource* resource) { |
| 127 | CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromAscii(resource), String); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | Handle<String> Factory::NewExternalStringFromTwoByte( |
| 132 | ExternalTwoByteString::Resource* resource) { |
| 133 | CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromTwoByte(resource), String); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | Handle<Context> Factory::NewGlobalContext() { |
| 138 | CALL_HEAP_FUNCTION(Heap::AllocateGlobalContext(), Context); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | Handle<Context> Factory::NewFunctionContext(int length, |
| 143 | Handle<JSFunction> closure) { |
| 144 | CALL_HEAP_FUNCTION(Heap::AllocateFunctionContext(length, *closure), Context); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | Handle<Context> Factory::NewWithContext(Handle<Context> previous, |
| 149 | Handle<JSObject> extension, |
| 150 | bool is_catch_context) { |
| 151 | CALL_HEAP_FUNCTION(Heap::AllocateWithContext(*previous, |
| 152 | *extension, |
| 153 | is_catch_context), |
| 154 | Context); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | Handle<Struct> Factory::NewStruct(InstanceType type) { |
| 159 | CALL_HEAP_FUNCTION(Heap::AllocateStruct(type), Struct); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | Handle<AccessorInfo> Factory::NewAccessorInfo() { |
| 164 | Handle<AccessorInfo> info = |
| 165 | Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE)); |
| 166 | info->set_flag(0); // Must clear the flag, it was initialized as undefined. |
| 167 | return info; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | Handle<Script> Factory::NewScript(Handle<String> source) { |
| 172 | // Generate id for this script. |
| 173 | int id; |
| 174 | if (Heap::last_script_id()->IsUndefined()) { |
| 175 | // Script ids start from one. |
| 176 | id = 1; |
| 177 | } else { |
| 178 | // Increment id, wrap when positive smi is exhausted. |
| 179 | id = Smi::cast(Heap::last_script_id())->value(); |
| 180 | id++; |
| 181 | if (!Smi::IsValid(id)) { |
| 182 | id = 0; |
| 183 | } |
| 184 | } |
| 185 | Heap::SetLastScriptId(Smi::FromInt(id)); |
| 186 | |
| 187 | // Create and initialize script object. |
| 188 | Handle<Proxy> wrapper = Factory::NewProxy(0, TENURED); |
| 189 | Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE)); |
| 190 | script->set_source(*source); |
| 191 | script->set_name(Heap::undefined_value()); |
| 192 | script->set_id(Heap::last_script_id()); |
| 193 | script->set_line_offset(Smi::FromInt(0)); |
| 194 | script->set_column_offset(Smi::FromInt(0)); |
| 195 | script->set_data(Heap::undefined_value()); |
| 196 | script->set_context_data(Heap::undefined_value()); |
| 197 | script->set_type(Smi::FromInt(Script::TYPE_NORMAL)); |
| 198 | script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST)); |
| 199 | script->set_wrapper(*wrapper); |
| 200 | script->set_line_ends(Heap::undefined_value()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 201 | script->set_eval_from_shared(Heap::undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 202 | script->set_eval_from_instructions_offset(Smi::FromInt(0)); |
| 203 | |
| 204 | return script; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | Handle<Proxy> Factory::NewProxy(Address addr, PretenureFlag pretenure) { |
| 209 | CALL_HEAP_FUNCTION(Heap::AllocateProxy(addr, pretenure), Proxy); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | Handle<Proxy> Factory::NewProxy(const AccessorDescriptor* desc) { |
| 214 | return NewProxy((Address) desc, TENURED); |
| 215 | } |
| 216 | |
| 217 | |
| 218 | Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) { |
| 219 | ASSERT(0 <= length); |
| 220 | CALL_HEAP_FUNCTION(Heap::AllocateByteArray(length, pretenure), ByteArray); |
| 221 | } |
| 222 | |
| 223 | |
| 224 | Handle<PixelArray> Factory::NewPixelArray(int length, |
| 225 | uint8_t* external_pointer, |
| 226 | PretenureFlag pretenure) { |
| 227 | ASSERT(0 <= length); |
| 228 | CALL_HEAP_FUNCTION(Heap::AllocatePixelArray(length, |
| 229 | external_pointer, |
| 230 | pretenure), PixelArray); |
| 231 | } |
| 232 | |
| 233 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 234 | Handle<ExternalArray> Factory::NewExternalArray(int length, |
| 235 | ExternalArrayType array_type, |
| 236 | void* external_pointer, |
| 237 | PretenureFlag pretenure) { |
| 238 | ASSERT(0 <= length); |
| 239 | CALL_HEAP_FUNCTION(Heap::AllocateExternalArray(length, |
| 240 | array_type, |
| 241 | external_pointer, |
| 242 | pretenure), ExternalArray); |
| 243 | } |
| 244 | |
| 245 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 246 | Handle<Map> Factory::NewMap(InstanceType type, int instance_size) { |
| 247 | CALL_HEAP_FUNCTION(Heap::AllocateMap(type, instance_size), Map); |
| 248 | } |
| 249 | |
| 250 | |
| 251 | Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) { |
| 252 | CALL_HEAP_FUNCTION(Heap::AllocateFunctionPrototype(*function), JSObject); |
| 253 | } |
| 254 | |
| 255 | |
| 256 | Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) { |
| 257 | CALL_HEAP_FUNCTION(src->CopyDropDescriptors(), Map); |
| 258 | } |
| 259 | |
| 260 | |
| 261 | Handle<Map> Factory::CopyMap(Handle<Map> src, |
| 262 | int extra_inobject_properties) { |
| 263 | Handle<Map> copy = CopyMapDropDescriptors(src); |
| 264 | // Check that we do not overflow the instance size when adding the |
| 265 | // extra inobject properties. |
| 266 | int instance_size_delta = extra_inobject_properties * kPointerSize; |
| 267 | int max_instance_size_delta = |
| 268 | JSObject::kMaxInstanceSize - copy->instance_size(); |
| 269 | if (instance_size_delta > max_instance_size_delta) { |
| 270 | // If the instance size overflows, we allocate as many properties |
| 271 | // as we can as inobject properties. |
| 272 | instance_size_delta = max_instance_size_delta; |
| 273 | extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2; |
| 274 | } |
| 275 | // Adjust the map with the extra inobject properties. |
| 276 | int inobject_properties = |
| 277 | copy->inobject_properties() + extra_inobject_properties; |
| 278 | copy->set_inobject_properties(inobject_properties); |
| 279 | copy->set_unused_property_fields(inobject_properties); |
| 280 | copy->set_instance_size(copy->instance_size() + instance_size_delta); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 281 | copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 282 | return copy; |
| 283 | } |
| 284 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 285 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 286 | Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) { |
| 287 | CALL_HEAP_FUNCTION(src->CopyDropTransitions(), Map); |
| 288 | } |
| 289 | |
| 290 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 291 | Handle<Map> Factory::GetFastElementsMap(Handle<Map> src) { |
| 292 | CALL_HEAP_FUNCTION(src->GetFastElementsMap(), Map); |
| 293 | } |
| 294 | |
| 295 | |
| 296 | Handle<Map> Factory::GetSlowElementsMap(Handle<Map> src) { |
| 297 | CALL_HEAP_FUNCTION(src->GetSlowElementsMap(), Map); |
| 298 | } |
| 299 | |
| 300 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 301 | Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) { |
| 302 | CALL_HEAP_FUNCTION(array->Copy(), FixedArray); |
| 303 | } |
| 304 | |
| 305 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 306 | Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo( |
| 307 | Handle<SharedFunctionInfo> function_info, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 308 | Handle<Map> function_map, |
| 309 | PretenureFlag pretenure) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 310 | CALL_HEAP_FUNCTION(Heap::AllocateFunction(*function_map, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 311 | *function_info, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 312 | Heap::the_hole_value(), |
| 313 | pretenure), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 314 | JSFunction); |
| 315 | } |
| 316 | |
| 317 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 318 | Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo( |
| 319 | Handle<SharedFunctionInfo> function_info, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 320 | Handle<Context> context, |
| 321 | PretenureFlag pretenure) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 322 | Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo( |
| 323 | function_info, Top::function_map(), pretenure); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 324 | result->set_context(*context); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 325 | int number_of_literals = function_info->num_literals(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 326 | Handle<FixedArray> literals = |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 327 | Factory::NewFixedArray(number_of_literals, pretenure); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 328 | if (number_of_literals > 0) { |
| 329 | // Store the object, regexp and array functions in the literals |
| 330 | // array prefix. These functions will be used when creating |
| 331 | // object, regexp and array literals in this function. |
| 332 | literals->set(JSFunction::kLiteralGlobalContextIndex, |
| 333 | context->global_context()); |
| 334 | } |
| 335 | result->set_literals(*literals); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 336 | return result; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | Handle<Object> Factory::NewNumber(double value, |
| 341 | PretenureFlag pretenure) { |
| 342 | CALL_HEAP_FUNCTION(Heap::NumberFromDouble(value, pretenure), Object); |
| 343 | } |
| 344 | |
| 345 | |
| 346 | Handle<Object> Factory::NewNumberFromInt(int value) { |
| 347 | CALL_HEAP_FUNCTION(Heap::NumberFromInt32(value), Object); |
| 348 | } |
| 349 | |
| 350 | |
| 351 | Handle<Object> Factory::NewNumberFromUint(uint32_t value) { |
| 352 | CALL_HEAP_FUNCTION(Heap::NumberFromUint32(value), Object); |
| 353 | } |
| 354 | |
| 355 | |
| 356 | Handle<JSObject> Factory::NewNeanderObject() { |
| 357 | CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(Heap::neander_map()), |
| 358 | JSObject); |
| 359 | } |
| 360 | |
| 361 | |
| 362 | Handle<Object> Factory::NewTypeError(const char* type, |
| 363 | Vector< Handle<Object> > args) { |
| 364 | return NewError("MakeTypeError", type, args); |
| 365 | } |
| 366 | |
| 367 | |
| 368 | Handle<Object> Factory::NewTypeError(Handle<String> message) { |
| 369 | return NewError("$TypeError", message); |
| 370 | } |
| 371 | |
| 372 | |
| 373 | Handle<Object> Factory::NewRangeError(const char* type, |
| 374 | Vector< Handle<Object> > args) { |
| 375 | return NewError("MakeRangeError", type, args); |
| 376 | } |
| 377 | |
| 378 | |
| 379 | Handle<Object> Factory::NewRangeError(Handle<String> message) { |
| 380 | return NewError("$RangeError", message); |
| 381 | } |
| 382 | |
| 383 | |
| 384 | Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) { |
| 385 | return NewError("MakeSyntaxError", type, args); |
| 386 | } |
| 387 | |
| 388 | |
| 389 | Handle<Object> Factory::NewSyntaxError(Handle<String> message) { |
| 390 | return NewError("$SyntaxError", message); |
| 391 | } |
| 392 | |
| 393 | |
| 394 | Handle<Object> Factory::NewReferenceError(const char* type, |
| 395 | Vector< Handle<Object> > args) { |
| 396 | return NewError("MakeReferenceError", type, args); |
| 397 | } |
| 398 | |
| 399 | |
| 400 | Handle<Object> Factory::NewReferenceError(Handle<String> message) { |
| 401 | return NewError("$ReferenceError", message); |
| 402 | } |
| 403 | |
| 404 | |
| 405 | Handle<Object> Factory::NewError(const char* maker, const char* type, |
| 406 | Vector< Handle<Object> > args) { |
| 407 | v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom. |
| 408 | Handle<FixedArray> array = Factory::NewFixedArray(args.length()); |
| 409 | for (int i = 0; i < args.length(); i++) { |
| 410 | array->set(i, *args[i]); |
| 411 | } |
| 412 | Handle<JSArray> object = Factory::NewJSArrayWithElements(array); |
| 413 | Handle<Object> result = NewError(maker, type, object); |
| 414 | return result.EscapeFrom(&scope); |
| 415 | } |
| 416 | |
| 417 | |
| 418 | Handle<Object> Factory::NewEvalError(const char* type, |
| 419 | Vector< Handle<Object> > args) { |
| 420 | return NewError("MakeEvalError", type, args); |
| 421 | } |
| 422 | |
| 423 | |
| 424 | Handle<Object> Factory::NewError(const char* type, |
| 425 | Vector< Handle<Object> > args) { |
| 426 | return NewError("MakeError", type, args); |
| 427 | } |
| 428 | |
| 429 | |
| 430 | Handle<Object> Factory::NewError(const char* maker, |
| 431 | const char* type, |
| 432 | Handle<JSArray> args) { |
| 433 | Handle<String> make_str = Factory::LookupAsciiSymbol(maker); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 434 | Handle<Object> fun_obj(Top::builtins()->GetPropertyNoExceptionThrown( |
| 435 | *make_str)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 436 | // If the builtins haven't been properly configured yet this error |
| 437 | // constructor may not have been defined. Bail out. |
| 438 | if (!fun_obj->IsJSFunction()) |
| 439 | return Factory::undefined_value(); |
| 440 | Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj); |
| 441 | Handle<Object> type_obj = Factory::LookupAsciiSymbol(type); |
| 442 | Object** argv[2] = { type_obj.location(), |
| 443 | Handle<Object>::cast(args).location() }; |
| 444 | |
| 445 | // Invoke the JavaScript factory method. If an exception is thrown while |
| 446 | // running the factory method, use the exception as the result. |
| 447 | bool caught_exception; |
| 448 | Handle<Object> result = Execution::TryCall(fun, |
| 449 | Top::builtins(), |
| 450 | 2, |
| 451 | argv, |
| 452 | &caught_exception); |
| 453 | return result; |
| 454 | } |
| 455 | |
| 456 | |
| 457 | Handle<Object> Factory::NewError(Handle<String> message) { |
| 458 | return NewError("$Error", message); |
| 459 | } |
| 460 | |
| 461 | |
| 462 | Handle<Object> Factory::NewError(const char* constructor, |
| 463 | Handle<String> message) { |
| 464 | Handle<String> constr = Factory::LookupAsciiSymbol(constructor); |
| 465 | Handle<JSFunction> fun = |
| 466 | Handle<JSFunction>( |
| 467 | JSFunction::cast( |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 468 | Top::builtins()->GetPropertyNoExceptionThrown(*constr))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 469 | Object** argv[1] = { Handle<Object>::cast(message).location() }; |
| 470 | |
| 471 | // Invoke the JavaScript factory method. If an exception is thrown while |
| 472 | // running the factory method, use the exception as the result. |
| 473 | bool caught_exception; |
| 474 | Handle<Object> result = Execution::TryCall(fun, |
| 475 | Top::builtins(), |
| 476 | 1, |
| 477 | argv, |
| 478 | &caught_exception); |
| 479 | return result; |
| 480 | } |
| 481 | |
| 482 | |
| 483 | Handle<JSFunction> Factory::NewFunction(Handle<String> name, |
| 484 | InstanceType type, |
| 485 | int instance_size, |
| 486 | Handle<Code> code, |
| 487 | bool force_initial_map) { |
| 488 | // Allocate the function |
| 489 | Handle<JSFunction> function = NewFunction(name, the_hole_value()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 490 | |
| 491 | // Setup the code pointer in both the shared function info and in |
| 492 | // the function itself. |
| 493 | function->shared()->set_code(*code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 494 | function->set_code(*code); |
| 495 | |
| 496 | if (force_initial_map || |
| 497 | type != JS_OBJECT_TYPE || |
| 498 | instance_size != JSObject::kHeaderSize) { |
| 499 | Handle<Map> initial_map = NewMap(type, instance_size); |
| 500 | Handle<JSObject> prototype = NewFunctionPrototype(function); |
| 501 | initial_map->set_prototype(*prototype); |
| 502 | function->set_initial_map(*initial_map); |
| 503 | initial_map->set_constructor(*function); |
| 504 | } else { |
| 505 | ASSERT(!function->has_initial_map()); |
| 506 | ASSERT(!function->has_prototype()); |
| 507 | } |
| 508 | |
| 509 | return function; |
| 510 | } |
| 511 | |
| 512 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 513 | Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name, |
| 514 | InstanceType type, |
| 515 | int instance_size, |
| 516 | Handle<JSObject> prototype, |
| 517 | Handle<Code> code, |
| 518 | bool force_initial_map) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 519 | // Allocate the function. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 520 | Handle<JSFunction> function = NewFunction(name, prototype); |
| 521 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 522 | // Setup the code pointer in both the shared function info and in |
| 523 | // the function itself. |
| 524 | function->shared()->set_code(*code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 525 | function->set_code(*code); |
| 526 | |
| 527 | if (force_initial_map || |
| 528 | type != JS_OBJECT_TYPE || |
| 529 | instance_size != JSObject::kHeaderSize) { |
| 530 | Handle<Map> initial_map = NewMap(type, instance_size); |
| 531 | function->set_initial_map(*initial_map); |
| 532 | initial_map->set_constructor(*function); |
| 533 | } |
| 534 | |
| 535 | // Set function.prototype and give the prototype a constructor |
| 536 | // property that refers to the function. |
| 537 | SetPrototypeProperty(function, prototype); |
| 538 | SetProperty(prototype, Factory::constructor_symbol(), function, DONT_ENUM); |
| 539 | return function; |
| 540 | } |
| 541 | |
| 542 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 543 | Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name, |
| 544 | Handle<Code> code) { |
| 545 | Handle<JSFunction> function = NewFunctionWithoutPrototype(name); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 546 | function->shared()->set_code(*code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 547 | function->set_code(*code); |
| 548 | ASSERT(!function->has_initial_map()); |
| 549 | ASSERT(!function->has_prototype()); |
| 550 | return function; |
| 551 | } |
| 552 | |
| 553 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 554 | Handle<Code> Factory::NewCode(const CodeDesc& desc, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 555 | Code::Flags flags, |
| 556 | Handle<Object> self_ref) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 557 | CALL_HEAP_FUNCTION(Heap::CreateCode(desc, flags, self_ref), Code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | |
| 561 | Handle<Code> Factory::CopyCode(Handle<Code> code) { |
| 562 | CALL_HEAP_FUNCTION(Heap::CopyCode(*code), Code); |
| 563 | } |
| 564 | |
| 565 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 566 | Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) { |
| 567 | CALL_HEAP_FUNCTION(Heap::CopyCode(*code, reloc_info), Code); |
| 568 | } |
| 569 | |
| 570 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 571 | MUST_USE_RESULT static inline MaybeObject* DoCopyInsert( |
| 572 | DescriptorArray* array, |
| 573 | String* key, |
| 574 | Object* value, |
| 575 | PropertyAttributes attributes) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 576 | CallbacksDescriptor desc(key, value, attributes); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 577 | MaybeObject* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 578 | return obj; |
| 579 | } |
| 580 | |
| 581 | |
| 582 | // Allocate the new array. |
| 583 | Handle<DescriptorArray> Factory::CopyAppendProxyDescriptor( |
| 584 | Handle<DescriptorArray> array, |
| 585 | Handle<String> key, |
| 586 | Handle<Object> value, |
| 587 | PropertyAttributes attributes) { |
| 588 | CALL_HEAP_FUNCTION(DoCopyInsert(*array, *key, *value, attributes), |
| 589 | DescriptorArray); |
| 590 | } |
| 591 | |
| 592 | |
| 593 | Handle<String> Factory::SymbolFromString(Handle<String> value) { |
| 594 | CALL_HEAP_FUNCTION(Heap::LookupSymbol(*value), String); |
| 595 | } |
| 596 | |
| 597 | |
| 598 | Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors( |
| 599 | Handle<DescriptorArray> array, |
| 600 | Handle<Object> descriptors) { |
| 601 | v8::NeanderArray callbacks(descriptors); |
| 602 | int nof_callbacks = callbacks.length(); |
| 603 | Handle<DescriptorArray> result = |
| 604 | NewDescriptorArray(array->number_of_descriptors() + nof_callbacks); |
| 605 | |
| 606 | // Number of descriptors added to the result so far. |
| 607 | int descriptor_count = 0; |
| 608 | |
| 609 | // Copy the descriptors from the array. |
| 610 | for (int i = 0; i < array->number_of_descriptors(); i++) { |
| 611 | if (array->GetType(i) != NULL_DESCRIPTOR) { |
| 612 | result->CopyFrom(descriptor_count++, *array, i); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | // Number of duplicates detected. |
| 617 | int duplicates = 0; |
| 618 | |
| 619 | // Fill in new callback descriptors. Process the callbacks from |
| 620 | // back to front so that the last callback with a given name takes |
| 621 | // precedence over previously added callbacks with that name. |
| 622 | for (int i = nof_callbacks - 1; i >= 0; i--) { |
| 623 | Handle<AccessorInfo> entry = |
| 624 | Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i))); |
| 625 | // Ensure the key is a symbol before writing into the instance descriptor. |
| 626 | Handle<String> key = |
| 627 | SymbolFromString(Handle<String>(String::cast(entry->name()))); |
| 628 | // Check if a descriptor with this name already exists before writing. |
| 629 | if (result->LinearSearch(*key, descriptor_count) == |
| 630 | DescriptorArray::kNotFound) { |
| 631 | CallbacksDescriptor desc(*key, *entry, entry->property_attributes()); |
| 632 | result->Set(descriptor_count, &desc); |
| 633 | descriptor_count++; |
| 634 | } else { |
| 635 | duplicates++; |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | // If duplicates were detected, allocate a result of the right size |
| 640 | // and transfer the elements. |
| 641 | if (duplicates > 0) { |
| 642 | int number_of_descriptors = result->number_of_descriptors() - duplicates; |
| 643 | Handle<DescriptorArray> new_result = |
| 644 | NewDescriptorArray(number_of_descriptors); |
| 645 | for (int i = 0; i < number_of_descriptors; i++) { |
| 646 | new_result->CopyFrom(i, *result, i); |
| 647 | } |
| 648 | result = new_result; |
| 649 | } |
| 650 | |
| 651 | // Sort the result before returning. |
| 652 | result->Sort(); |
| 653 | return result; |
| 654 | } |
| 655 | |
| 656 | |
| 657 | Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor, |
| 658 | PretenureFlag pretenure) { |
| 659 | CALL_HEAP_FUNCTION(Heap::AllocateJSObject(*constructor, pretenure), JSObject); |
| 660 | } |
| 661 | |
| 662 | |
| 663 | Handle<GlobalObject> Factory::NewGlobalObject( |
| 664 | Handle<JSFunction> constructor) { |
| 665 | CALL_HEAP_FUNCTION(Heap::AllocateGlobalObject(*constructor), |
| 666 | GlobalObject); |
| 667 | } |
| 668 | |
| 669 | |
| 670 | |
| 671 | Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) { |
| 672 | CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(*map, NOT_TENURED), |
| 673 | JSObject); |
| 674 | } |
| 675 | |
| 676 | |
| 677 | Handle<JSArray> Factory::NewJSArray(int length, |
| 678 | PretenureFlag pretenure) { |
| 679 | Handle<JSObject> obj = NewJSObject(Top::array_function(), pretenure); |
| 680 | CALL_HEAP_FUNCTION(Handle<JSArray>::cast(obj)->Initialize(length), JSArray); |
| 681 | } |
| 682 | |
| 683 | |
| 684 | Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements, |
| 685 | PretenureFlag pretenure) { |
| 686 | Handle<JSArray> result = |
| 687 | Handle<JSArray>::cast(NewJSObject(Top::array_function(), pretenure)); |
| 688 | result->SetContent(*elements); |
| 689 | return result; |
| 690 | } |
| 691 | |
| 692 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 693 | Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo( |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 694 | Handle<String> name, |
| 695 | int number_of_literals, |
| 696 | Handle<Code> code, |
| 697 | Handle<SerializedScopeInfo> scope_info) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 698 | Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name); |
| 699 | shared->set_code(*code); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 700 | shared->set_scope_info(*scope_info); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 701 | int literals_array_size = number_of_literals; |
| 702 | // If the function contains object, regexp or array literals, |
| 703 | // allocate extra space for a literals array prefix containing the |
| 704 | // context. |
| 705 | if (number_of_literals > 0) { |
| 706 | literals_array_size += JSFunction::kLiteralsPrefixSize; |
| 707 | } |
| 708 | shared->set_num_literals(literals_array_size); |
| 709 | return shared; |
| 710 | } |
| 711 | |
| 712 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 713 | Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) { |
| 714 | CALL_HEAP_FUNCTION(Heap::AllocateSharedFunctionInfo(*name), |
| 715 | SharedFunctionInfo); |
| 716 | } |
| 717 | |
| 718 | |
| 719 | Handle<String> Factory::NumberToString(Handle<Object> number) { |
| 720 | CALL_HEAP_FUNCTION(Heap::NumberToString(*number), String); |
| 721 | } |
| 722 | |
| 723 | |
| 724 | Handle<NumberDictionary> Factory::DictionaryAtNumberPut( |
| 725 | Handle<NumberDictionary> dictionary, |
| 726 | uint32_t key, |
| 727 | Handle<Object> value) { |
| 728 | CALL_HEAP_FUNCTION(dictionary->AtNumberPut(key, *value), NumberDictionary); |
| 729 | } |
| 730 | |
| 731 | |
| 732 | Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name, |
| 733 | Handle<Object> prototype) { |
| 734 | Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name); |
| 735 | CALL_HEAP_FUNCTION(Heap::AllocateFunction(*Top::function_map(), |
| 736 | *function_share, |
| 737 | *prototype), |
| 738 | JSFunction); |
| 739 | } |
| 740 | |
| 741 | |
| 742 | Handle<JSFunction> Factory::NewFunction(Handle<String> name, |
| 743 | Handle<Object> prototype) { |
| 744 | Handle<JSFunction> fun = NewFunctionHelper(name, prototype); |
| 745 | fun->set_context(Top::context()->global_context()); |
| 746 | return fun; |
| 747 | } |
| 748 | |
| 749 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 750 | Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper( |
| 751 | Handle<String> name) { |
| 752 | Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name); |
| 753 | CALL_HEAP_FUNCTION(Heap::AllocateFunction( |
| 754 | *Top::function_without_prototype_map(), |
| 755 | *function_share, |
| 756 | *the_hole_value()), |
| 757 | JSFunction); |
| 758 | } |
| 759 | |
| 760 | |
| 761 | Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name) { |
| 762 | Handle<JSFunction> fun = NewFunctionWithoutPrototypeHelper(name); |
| 763 | fun->set_context(Top::context()->global_context()); |
| 764 | return fun; |
| 765 | } |
| 766 | |
| 767 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 768 | Handle<Object> Factory::ToObject(Handle<Object> object) { |
| 769 | CALL_HEAP_FUNCTION(object->ToObject(), Object); |
| 770 | } |
| 771 | |
| 772 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 773 | Handle<Object> Factory::ToObject(Handle<Object> object, |
| 774 | Handle<Context> global_context) { |
| 775 | CALL_HEAP_FUNCTION(object->ToObject(*global_context), Object); |
| 776 | } |
| 777 | |
| 778 | |
| 779 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 780 | Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) { |
| 781 | // Get the original code of the function. |
| 782 | Handle<Code> code(shared->code()); |
| 783 | |
| 784 | // Create a copy of the code before allocating the debug info object to avoid |
| 785 | // allocation while setting up the debug info object. |
| 786 | Handle<Code> original_code(*Factory::CopyCode(code)); |
| 787 | |
| 788 | // Allocate initial fixed array for active break points before allocating the |
| 789 | // debug info object to avoid allocation while setting up the debug info |
| 790 | // object. |
| 791 | Handle<FixedArray> break_points( |
| 792 | Factory::NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction)); |
| 793 | |
| 794 | // Create and set up the debug info object. Debug info contains function, a |
| 795 | // copy of the original code, the executing code and initial fixed array for |
| 796 | // active break points. |
| 797 | Handle<DebugInfo> debug_info = |
| 798 | Handle<DebugInfo>::cast(Factory::NewStruct(DEBUG_INFO_TYPE)); |
| 799 | debug_info->set_shared(*shared); |
| 800 | debug_info->set_original_code(*original_code); |
| 801 | debug_info->set_code(*code); |
| 802 | debug_info->set_break_points(*break_points); |
| 803 | |
| 804 | // Link debug info to function. |
| 805 | shared->set_debug_info(*debug_info); |
| 806 | |
| 807 | return debug_info; |
| 808 | } |
| 809 | #endif |
| 810 | |
| 811 | |
| 812 | Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee, |
| 813 | int length) { |
| 814 | CALL_HEAP_FUNCTION(Heap::AllocateArgumentsObject(*callee, length), JSObject); |
| 815 | } |
| 816 | |
| 817 | |
| 818 | Handle<JSFunction> Factory::CreateApiFunction( |
| 819 | Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) { |
| 820 | Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::HandleApiCall)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 821 | Handle<Code> construct_stub = |
| 822 | Handle<Code>(Builtins::builtin(Builtins::JSConstructStubApi)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 823 | |
| 824 | int internal_field_count = 0; |
| 825 | if (!obj->instance_template()->IsUndefined()) { |
| 826 | Handle<ObjectTemplateInfo> instance_template = |
| 827 | Handle<ObjectTemplateInfo>( |
| 828 | ObjectTemplateInfo::cast(obj->instance_template())); |
| 829 | internal_field_count = |
| 830 | Smi::cast(instance_template->internal_field_count())->value(); |
| 831 | } |
| 832 | |
| 833 | int instance_size = kPointerSize * internal_field_count; |
| 834 | InstanceType type = INVALID_TYPE; |
| 835 | switch (instance_type) { |
| 836 | case JavaScriptObject: |
| 837 | type = JS_OBJECT_TYPE; |
| 838 | instance_size += JSObject::kHeaderSize; |
| 839 | break; |
| 840 | case InnerGlobalObject: |
| 841 | type = JS_GLOBAL_OBJECT_TYPE; |
| 842 | instance_size += JSGlobalObject::kSize; |
| 843 | break; |
| 844 | case OuterGlobalObject: |
| 845 | type = JS_GLOBAL_PROXY_TYPE; |
| 846 | instance_size += JSGlobalProxy::kSize; |
| 847 | break; |
| 848 | default: |
| 849 | break; |
| 850 | } |
| 851 | ASSERT(type != INVALID_TYPE); |
| 852 | |
| 853 | Handle<JSFunction> result = |
| 854 | Factory::NewFunction(Factory::empty_symbol(), |
| 855 | type, |
| 856 | instance_size, |
| 857 | code, |
| 858 | true); |
| 859 | // Set class name. |
| 860 | Handle<Object> class_name = Handle<Object>(obj->class_name()); |
| 861 | if (class_name->IsString()) { |
| 862 | result->shared()->set_instance_class_name(*class_name); |
| 863 | result->shared()->set_name(*class_name); |
| 864 | } |
| 865 | |
| 866 | Handle<Map> map = Handle<Map>(result->initial_map()); |
| 867 | |
| 868 | // Mark as undetectable if needed. |
| 869 | if (obj->undetectable()) { |
| 870 | map->set_is_undetectable(); |
| 871 | } |
| 872 | |
| 873 | // Mark as hidden for the __proto__ accessor if needed. |
| 874 | if (obj->hidden_prototype()) { |
| 875 | map->set_is_hidden_prototype(); |
| 876 | } |
| 877 | |
| 878 | // Mark as needs_access_check if needed. |
| 879 | if (obj->needs_access_check()) { |
| 880 | map->set_is_access_check_needed(true); |
| 881 | } |
| 882 | |
| 883 | // Set interceptor information in the map. |
| 884 | if (!obj->named_property_handler()->IsUndefined()) { |
| 885 | map->set_has_named_interceptor(); |
| 886 | } |
| 887 | if (!obj->indexed_property_handler()->IsUndefined()) { |
| 888 | map->set_has_indexed_interceptor(); |
| 889 | } |
| 890 | |
| 891 | // Set instance call-as-function information in the map. |
| 892 | if (!obj->instance_call_handler()->IsUndefined()) { |
| 893 | map->set_has_instance_call_handler(); |
| 894 | } |
| 895 | |
| 896 | result->shared()->set_function_data(*obj); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 897 | result->shared()->set_construct_stub(*construct_stub); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 898 | result->shared()->DontAdaptArguments(); |
| 899 | |
| 900 | // Recursively copy parent templates' accessors, 'data' may be modified. |
| 901 | Handle<DescriptorArray> array = |
| 902 | Handle<DescriptorArray>(map->instance_descriptors()); |
| 903 | while (true) { |
| 904 | Handle<Object> props = Handle<Object>(obj->property_accessors()); |
| 905 | if (!props->IsUndefined()) { |
| 906 | array = Factory::CopyAppendCallbackDescriptors(array, props); |
| 907 | } |
| 908 | Handle<Object> parent = Handle<Object>(obj->parent_template()); |
| 909 | if (parent->IsUndefined()) break; |
| 910 | obj = Handle<FunctionTemplateInfo>::cast(parent); |
| 911 | } |
| 912 | if (!array->IsEmpty()) { |
| 913 | map->set_instance_descriptors(*array); |
| 914 | } |
| 915 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 916 | ASSERT(result->shared()->IsApiFunction()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 917 | return result; |
| 918 | } |
| 919 | |
| 920 | |
| 921 | Handle<MapCache> Factory::NewMapCache(int at_least_space_for) { |
| 922 | CALL_HEAP_FUNCTION(MapCache::Allocate(at_least_space_for), MapCache); |
| 923 | } |
| 924 | |
| 925 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 926 | MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context, |
| 927 | FixedArray* keys, |
| 928 | Map* map) { |
| 929 | Object* result; |
| 930 | { MaybeObject* maybe_result = |
| 931 | MapCache::cast(context->map_cache())->Put(keys, map); |
| 932 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 933 | } |
| 934 | context->set_map_cache(MapCache::cast(result)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 935 | return result; |
| 936 | } |
| 937 | |
| 938 | |
| 939 | Handle<MapCache> Factory::AddToMapCache(Handle<Context> context, |
| 940 | Handle<FixedArray> keys, |
| 941 | Handle<Map> map) { |
| 942 | CALL_HEAP_FUNCTION(UpdateMapCacheWith(*context, *keys, *map), MapCache); |
| 943 | } |
| 944 | |
| 945 | |
| 946 | Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context, |
| 947 | Handle<FixedArray> keys) { |
| 948 | if (context->map_cache()->IsUndefined()) { |
| 949 | // Allocate the new map cache for the global context. |
| 950 | Handle<MapCache> new_cache = NewMapCache(24); |
| 951 | context->set_map_cache(*new_cache); |
| 952 | } |
| 953 | // Check to see whether there is a matching element in the cache. |
| 954 | Handle<MapCache> cache = |
| 955 | Handle<MapCache>(MapCache::cast(context->map_cache())); |
| 956 | Handle<Object> result = Handle<Object>(cache->Lookup(*keys)); |
| 957 | if (result->IsMap()) return Handle<Map>::cast(result); |
| 958 | // Create a new map and add it to the cache. |
| 959 | Handle<Map> map = |
| 960 | CopyMap(Handle<Map>(context->object_function()->initial_map()), |
| 961 | keys->length()); |
| 962 | AddToMapCache(context, keys, map); |
| 963 | return Handle<Map>(map); |
| 964 | } |
| 965 | |
| 966 | |
| 967 | void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp, |
| 968 | JSRegExp::Type type, |
| 969 | Handle<String> source, |
| 970 | JSRegExp::Flags flags, |
| 971 | Handle<Object> data) { |
| 972 | Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize); |
| 973 | |
| 974 | store->set(JSRegExp::kTagIndex, Smi::FromInt(type)); |
| 975 | store->set(JSRegExp::kSourceIndex, *source); |
| 976 | store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value())); |
| 977 | store->set(JSRegExp::kAtomPatternIndex, *data); |
| 978 | regexp->set_data(*store); |
| 979 | } |
| 980 | |
| 981 | void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp, |
| 982 | JSRegExp::Type type, |
| 983 | Handle<String> source, |
| 984 | JSRegExp::Flags flags, |
| 985 | int capture_count) { |
| 986 | Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize); |
| 987 | |
| 988 | store->set(JSRegExp::kTagIndex, Smi::FromInt(type)); |
| 989 | store->set(JSRegExp::kSourceIndex, *source); |
| 990 | store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value())); |
| 991 | store->set(JSRegExp::kIrregexpASCIICodeIndex, Heap::the_hole_value()); |
| 992 | store->set(JSRegExp::kIrregexpUC16CodeIndex, Heap::the_hole_value()); |
| 993 | store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0)); |
| 994 | store->set(JSRegExp::kIrregexpCaptureCountIndex, |
| 995 | Smi::FromInt(capture_count)); |
| 996 | regexp->set_data(*store); |
| 997 | } |
| 998 | |
| 999 | |
| 1000 | |
| 1001 | void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc, |
| 1002 | Handle<JSObject> instance, |
| 1003 | bool* pending_exception) { |
| 1004 | // Configure the instance by adding the properties specified by the |
| 1005 | // instance template. |
| 1006 | Handle<Object> instance_template = Handle<Object>(desc->instance_template()); |
| 1007 | if (!instance_template->IsUndefined()) { |
| 1008 | Execution::ConfigureInstance(instance, |
| 1009 | instance_template, |
| 1010 | pending_exception); |
| 1011 | } else { |
| 1012 | *pending_exception = false; |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | |
| 1017 | } } // namespace v8::internal |