christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame^] | 1 | // Copyright 2006-2008 Google Inc. 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 "execution.h" |
| 32 | #include "factory.h" |
| 33 | #include "macro-assembler.h" |
| 34 | |
| 35 | namespace v8 { namespace internal { |
| 36 | |
| 37 | |
| 38 | Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) { |
| 39 | ASSERT(0 <= size); |
| 40 | CALL_HEAP_FUNCTION(Heap::AllocateFixedArray(size, pretenure), FixedArray); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) { |
| 45 | ASSERT(0 <= number_of_descriptors); |
| 46 | CALL_HEAP_FUNCTION(DescriptorArray::Allocate(number_of_descriptors), |
| 47 | DescriptorArray); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | // Symbols are created in the old generation (code space). |
| 52 | Handle<String> Factory::LookupSymbol(Vector<const char> string) { |
| 53 | CALL_HEAP_FUNCTION(Heap::LookupSymbol(string), String); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | Handle<String> Factory::NewStringFromAscii(Vector<const char> string, |
| 58 | PretenureFlag pretenure) { |
| 59 | CALL_HEAP_FUNCTION(Heap::AllocateStringFromAscii(string, pretenure), String); |
| 60 | } |
| 61 | |
| 62 | Handle<String> Factory::NewStringFromUtf8(Vector<const char> string, |
| 63 | PretenureFlag pretenure) { |
| 64 | CALL_HEAP_FUNCTION(Heap::AllocateStringFromUtf8(string, pretenure), String); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string) { |
| 69 | CALL_HEAP_FUNCTION(Heap::AllocateStringFromTwoByte(string), String); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | Handle<String> Factory::NewRawTwoByteString(int length, |
| 74 | PretenureFlag pretenure) { |
| 75 | CALL_HEAP_FUNCTION(Heap::AllocateRawTwoByteString(length, pretenure), String); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | Handle<String> Factory::NewConsString(Handle<String> first, |
| 80 | Handle<String> second) { |
| 81 | CALL_HEAP_FUNCTION(Heap::AllocateConsString(*first, *second), String); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | Handle<String> Factory::NewStringSlice(Handle<String> str, int begin, int end) { |
| 86 | CALL_HEAP_FUNCTION(str->Slice(begin, end), String); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | Handle<String> Factory::NewExternalStringFromAscii( |
| 91 | ExternalAsciiString::Resource* resource) { |
| 92 | CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromAscii(resource), String); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | Handle<String> Factory::NewExternalStringFromTwoByte( |
| 97 | ExternalTwoByteString::Resource* resource) { |
| 98 | CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromTwoByte(resource), String); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | Handle<Context> Factory::NewGlobalContext() { |
| 103 | CALL_HEAP_FUNCTION(Heap::AllocateGlobalContext(), Context); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | Handle<Context> Factory::NewFunctionContext(int length, |
| 108 | Handle<JSFunction> closure) { |
| 109 | CALL_HEAP_FUNCTION(Heap::AllocateFunctionContext(length, *closure), Context); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | Handle<Context> Factory::NewWithContext(Handle<Context> previous, |
| 114 | Handle<JSObject> extension) { |
| 115 | CALL_HEAP_FUNCTION(Heap::AllocateWithContext(*previous, *extension), Context); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | Handle<Struct> Factory::NewStruct(InstanceType type) { |
| 120 | CALL_HEAP_FUNCTION(Heap::AllocateStruct(type), Struct); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | Handle<AccessorInfo> Factory::NewAccessorInfo() { |
| 125 | Handle<AccessorInfo> info = |
| 126 | Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE)); |
| 127 | info->set_flag(0); // Must clear the flag, it was initialized as undefined. |
| 128 | return info; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | Handle<Script> Factory::NewScript(Handle<String> source) { |
| 133 | Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE)); |
| 134 | script->set_source(*source); |
| 135 | script->set_name(Heap::undefined_value()); |
| 136 | script->set_line_offset(Smi::FromInt(0)); |
| 137 | script->set_column_offset(Smi::FromInt(0)); |
| 138 | script->set_wrapper(*Factory::NewProxy(0, TENURED)); |
| 139 | script->set_type(Smi::FromInt(SCRIPT_TYPE_NORMAL)); |
| 140 | return script; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | Handle<Proxy> Factory::NewProxy(Address addr, PretenureFlag pretenure) { |
| 145 | CALL_HEAP_FUNCTION(Heap::AllocateProxy(addr, pretenure), Proxy); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | Handle<Proxy> Factory::NewProxy(const AccessorDescriptor* desc) { |
| 150 | return NewProxy((Address) desc, TENURED); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | Handle<ByteArray> Factory::NewByteArray(int length) { |
| 155 | ASSERT(0 <= length); |
| 156 | CALL_HEAP_FUNCTION(Heap::AllocateByteArray(length), ByteArray); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | Handle<Map> Factory::NewMap(InstanceType type, int instance_size) { |
| 161 | CALL_HEAP_FUNCTION(Heap::AllocateMap(type, instance_size), Map); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) { |
| 166 | CALL_HEAP_FUNCTION(Heap::AllocateFunctionPrototype(*function), JSObject); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | Handle<Map> Factory::CopyMap(Handle<Map> src) { |
| 171 | CALL_HEAP_FUNCTION(src->Copy(), Map); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) { |
| 176 | CALL_HEAP_FUNCTION(array->Copy(), FixedArray); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | Handle<JSFunction> Factory::BaseNewFunctionFromBoilerplate( |
| 181 | Handle<JSFunction> boilerplate, |
| 182 | Handle<Map> function_map) { |
| 183 | ASSERT(boilerplate->IsBoilerplate()); |
| 184 | ASSERT(!boilerplate->has_initial_map()); |
| 185 | ASSERT(!boilerplate->has_prototype()); |
| 186 | ASSERT(boilerplate->properties() == Heap::empty_fixed_array()); |
| 187 | ASSERT(boilerplate->elements() == Heap::empty_fixed_array()); |
| 188 | CALL_HEAP_FUNCTION(Heap::AllocateFunction(*function_map, |
| 189 | boilerplate->shared(), |
| 190 | Heap::the_hole_value()), |
| 191 | JSFunction); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | Handle<JSFunction> Factory::NewFunctionFromBoilerplate( |
| 196 | Handle<JSFunction> boilerplate, |
| 197 | Handle<Context> context) { |
| 198 | Handle<JSFunction> result = |
| 199 | BaseNewFunctionFromBoilerplate(boilerplate, Top::function_map()); |
| 200 | result->set_context(*context); |
| 201 | int number_of_literals = boilerplate->literals()->length(); |
| 202 | if (number_of_literals > 0) { |
| 203 | Handle<FixedArray> literals = |
| 204 | Factory::NewFixedArray(number_of_literals, TENURED); |
| 205 | result->set_literals(*literals); |
| 206 | } |
| 207 | ASSERT(!result->IsBoilerplate()); |
| 208 | return result; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | Handle<Object> Factory::NewNumber(double value, |
| 213 | PretenureFlag pretenure) { |
| 214 | CALL_HEAP_FUNCTION(Heap::NumberFromDouble(value, pretenure), Object); |
| 215 | } |
| 216 | |
| 217 | |
| 218 | Handle<Object> Factory::NewNumberFromInt(int value) { |
| 219 | CALL_HEAP_FUNCTION(Heap::NumberFromInt32(value), Object); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | Handle<JSObject> Factory::NewNeanderObject() { |
| 224 | CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(Heap::neander_map()), |
| 225 | JSObject); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | Handle<Object> Factory::NewTypeError(const char* type, |
| 230 | Vector< Handle<Object> > args) { |
| 231 | return NewError("MakeTypeError", type, args); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | Handle<Object> Factory::NewTypeError(Handle<String> message) { |
| 236 | return NewError("$TypeError", message); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | Handle<Object> Factory::NewRangeError(const char* type, |
| 241 | Vector< Handle<Object> > args) { |
| 242 | return NewError("MakeRangeError", type, args); |
| 243 | } |
| 244 | |
| 245 | |
| 246 | Handle<Object> Factory::NewRangeError(Handle<String> message) { |
| 247 | return NewError("$RangeError", message); |
| 248 | } |
| 249 | |
| 250 | |
| 251 | Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) { |
| 252 | return NewError("MakeSyntaxError", type, args); |
| 253 | } |
| 254 | |
| 255 | |
| 256 | Handle<Object> Factory::NewSyntaxError(Handle<String> message) { |
| 257 | return NewError("$SyntaxError", message); |
| 258 | } |
| 259 | |
| 260 | |
| 261 | Handle<Object> Factory::NewReferenceError(const char* type, |
| 262 | Vector< Handle<Object> > args) { |
| 263 | return NewError("MakeReferenceError", type, args); |
| 264 | } |
| 265 | |
| 266 | |
| 267 | Handle<Object> Factory::NewReferenceError(Handle<String> message) { |
| 268 | return NewError("$ReferenceError", message); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | Handle<Object> Factory::NewError(const char* maker, const char* type, |
| 273 | Vector< Handle<Object> > args) { |
| 274 | HandleScope scope; |
| 275 | Handle<JSArray> array = NewJSArray(args.length()); |
| 276 | for (int i = 0; i < args.length(); i++) |
| 277 | SetElement(array, i, args[i]); |
| 278 | Handle<Object> result = NewError(maker, type, array); |
| 279 | return result.EscapeFrom(&scope); |
| 280 | } |
| 281 | |
| 282 | |
| 283 | Handle<Object> Factory::NewEvalError(const char* type, |
| 284 | Vector< Handle<Object> > args) { |
| 285 | return NewError("MakeEvalError", type, args); |
| 286 | } |
| 287 | |
| 288 | |
| 289 | Handle<Object> Factory::NewError(const char* type, |
| 290 | Vector< Handle<Object> > args) { |
| 291 | return NewError("MakeError", type, args); |
| 292 | } |
| 293 | |
| 294 | |
| 295 | Handle<Object> Factory::NewError(const char* maker, |
| 296 | const char* type, |
| 297 | Handle<JSArray> args) { |
| 298 | Handle<String> make_str = Factory::LookupAsciiSymbol(maker); |
| 299 | Handle<JSFunction> fun = |
| 300 | Handle<JSFunction>( |
| 301 | JSFunction::cast( |
| 302 | Top::security_context_builtins()->GetProperty(*make_str))); |
| 303 | Handle<Object> type_obj = Factory::LookupAsciiSymbol(type); |
| 304 | Object** argv[2] = { type_obj.location(), |
| 305 | Handle<Object>::cast(args).location() }; |
| 306 | |
| 307 | // Invoke the JavaScript factory method. If an exception is thrown while |
| 308 | // running the factory method, use the exception as the result. |
| 309 | bool caught_exception; |
| 310 | Handle<Object> result = Execution::TryCall(fun, |
| 311 | Top::security_context_builtins(), |
| 312 | 2, |
| 313 | argv, |
| 314 | &caught_exception); |
| 315 | return result; |
| 316 | } |
| 317 | |
| 318 | |
| 319 | Handle<Object> Factory::NewError(Handle<String> message) { |
| 320 | return NewError("$Error", message); |
| 321 | } |
| 322 | |
| 323 | |
| 324 | Handle<Object> Factory::NewError(const char* constructor, |
| 325 | Handle<String> message) { |
| 326 | Handle<String> constr = Factory::LookupAsciiSymbol(constructor); |
| 327 | Handle<JSFunction> fun = |
| 328 | Handle<JSFunction>( |
| 329 | JSFunction::cast( |
| 330 | Top::security_context_builtins()->GetProperty(*constr))); |
| 331 | Object** argv[1] = { Handle<Object>::cast(message).location() }; |
| 332 | |
| 333 | // Invoke the JavaScript factory method. If an exception is thrown while |
| 334 | // running the factory method, use the exception as the result. |
| 335 | bool caught_exception; |
| 336 | Handle<Object> result = Execution::TryCall(fun, |
| 337 | Top::security_context_builtins(), |
| 338 | 1, |
| 339 | argv, |
| 340 | &caught_exception); |
| 341 | return result; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | Handle<JSFunction> Factory::NewFunction(Handle<String> name, |
| 346 | InstanceType type, |
| 347 | int instance_size, |
| 348 | Handle<Code> code, |
| 349 | bool force_initial_map) { |
| 350 | // Allocate the function |
| 351 | Handle<JSFunction> function = NewFunction(name, the_hole_value()); |
| 352 | function->set_code(*code); |
| 353 | |
| 354 | if (force_initial_map || |
| 355 | type != JS_OBJECT_TYPE || |
| 356 | instance_size != JSObject::kHeaderSize) { |
| 357 | Handle<Map> initial_map = NewMap(type, instance_size); |
| 358 | Handle<JSObject> prototype = NewFunctionPrototype(function); |
| 359 | initial_map->set_prototype(*prototype); |
| 360 | function->set_initial_map(*initial_map); |
| 361 | initial_map->set_constructor(*function); |
| 362 | } else { |
| 363 | ASSERT(!function->has_initial_map()); |
| 364 | ASSERT(!function->has_prototype()); |
| 365 | } |
| 366 | |
| 367 | return function; |
| 368 | } |
| 369 | |
| 370 | |
| 371 | Handle<JSFunction> Factory::NewFunctionBoilerplate(Handle<String> name, |
| 372 | int number_of_literals, |
| 373 | Handle<Code> code) { |
| 374 | Handle<JSFunction> function = NewFunctionBoilerplate(name); |
| 375 | function->set_code(*code); |
| 376 | if (number_of_literals > 0) { |
| 377 | Handle<FixedArray> literals = |
| 378 | Factory::NewFixedArray(number_of_literals, TENURED); |
| 379 | function->set_literals(*literals); |
| 380 | } else { |
| 381 | function->set_literals(Heap::empty_fixed_array()); |
| 382 | } |
| 383 | ASSERT(!function->has_initial_map()); |
| 384 | ASSERT(!function->has_prototype()); |
| 385 | return function; |
| 386 | } |
| 387 | |
| 388 | |
| 389 | Handle<JSFunction> Factory::NewFunctionBoilerplate(Handle<String> name) { |
| 390 | Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name); |
| 391 | CALL_HEAP_FUNCTION(Heap::AllocateFunction(Heap::boilerplate_function_map(), |
| 392 | *shared, |
| 393 | Heap::the_hole_value()), |
| 394 | JSFunction); |
| 395 | } |
| 396 | |
| 397 | |
| 398 | Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name, |
| 399 | InstanceType type, |
| 400 | int instance_size, |
| 401 | Handle<JSObject> prototype, |
| 402 | Handle<Code> code, |
| 403 | bool force_initial_map) { |
| 404 | // Allocate the function |
| 405 | Handle<JSFunction> function = NewFunction(name, prototype); |
| 406 | |
| 407 | function->set_code(*code); |
| 408 | |
| 409 | if (force_initial_map || |
| 410 | type != JS_OBJECT_TYPE || |
| 411 | instance_size != JSObject::kHeaderSize) { |
| 412 | Handle<Map> initial_map = NewMap(type, instance_size); |
| 413 | function->set_initial_map(*initial_map); |
| 414 | initial_map->set_constructor(*function); |
| 415 | } |
| 416 | |
| 417 | // Set function.prototype and give the prototype a constructor |
| 418 | // property that refers to the function. |
| 419 | SetPrototypeProperty(function, prototype); |
| 420 | SetProperty(prototype, Factory::constructor_symbol(), function, DONT_ENUM); |
| 421 | return function; |
| 422 | } |
| 423 | |
| 424 | Handle<Code> Factory::NewCode(const CodeDesc& desc, ScopeInfo<>* sinfo, |
| 425 | Code::Flags flags) { |
| 426 | CALL_HEAP_FUNCTION(Heap::CreateCode(desc, sinfo, flags), Code); |
| 427 | } |
| 428 | |
| 429 | |
| 430 | Handle<Code> Factory::CopyCode(Handle<Code> code) { |
| 431 | CALL_HEAP_FUNCTION(Heap::CopyCode(*code), Code); |
| 432 | } |
| 433 | |
| 434 | |
| 435 | #define CALL_GC(RETRY) \ |
| 436 | do { \ |
| 437 | if (!Heap::CollectGarbage(Failure::cast(RETRY)->requested(), \ |
| 438 | Failure::cast(RETRY)->allocation_space())) { \ |
| 439 | /* TODO(1181417): Fix this. */ \ |
| 440 | V8::FatalProcessOutOfMemory("Factory CALL_GC"); \ |
| 441 | } \ |
| 442 | } while (false) |
| 443 | |
| 444 | |
| 445 | // Allocate the new array. We cannot use the CALL_HEAP_FUNCTION macro here, |
| 446 | // because the stack-allocated CallbacksDescriptor instance is not GC safe. |
| 447 | Handle<DescriptorArray> Factory::CopyAppendProxyDescriptor( |
| 448 | Handle<DescriptorArray> array, |
| 449 | Handle<String> key, |
| 450 | Handle<Object> value, |
| 451 | PropertyAttributes attributes) { |
| 452 | GC_GREEDY_CHECK(); |
| 453 | CallbacksDescriptor desc(*key, *value, attributes); |
| 454 | Object* obj = array->CopyInsert(&desc); |
| 455 | if (obj->IsRetryAfterGC()) { |
| 456 | CALL_GC(obj); |
| 457 | CallbacksDescriptor desc(*key, *value, attributes); |
| 458 | obj = array->CopyInsert(&desc); |
| 459 | if (obj->IsFailure()) { |
| 460 | // TODO(1181417): Fix this. |
| 461 | V8::FatalProcessOutOfMemory("CopyAppendProxyDescriptor"); |
| 462 | } |
| 463 | } |
| 464 | return Handle<DescriptorArray>(DescriptorArray::cast(obj)); |
| 465 | } |
| 466 | |
| 467 | #undef CALL_GC |
| 468 | |
| 469 | |
| 470 | Handle<String> Factory::SymbolFromString(Handle<String> value) { |
| 471 | CALL_HEAP_FUNCTION(Heap::LookupSymbol(*value), String); |
| 472 | } |
| 473 | |
| 474 | |
| 475 | Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors( |
| 476 | Handle<DescriptorArray> array, |
| 477 | Handle<Object> descriptors) { |
| 478 | v8::NeanderArray callbacks(descriptors); |
| 479 | int nof_callbacks = callbacks.length(); |
| 480 | Handle<DescriptorArray> result = |
| 481 | NewDescriptorArray(array->number_of_descriptors() + nof_callbacks); |
| 482 | |
| 483 | // Number of descriptors added to the result so far. |
| 484 | int descriptor_count = 0; |
| 485 | |
| 486 | // Copy the descriptors from the array. |
| 487 | DescriptorWriter w(*result); |
| 488 | for (DescriptorReader r(*array); !r.eos(); r.advance()) { |
| 489 | w.WriteFrom(&r); |
| 490 | descriptor_count++; |
| 491 | } |
| 492 | |
| 493 | // Number of duplicates detected. |
| 494 | int duplicates = 0; |
| 495 | |
| 496 | // Fill in new callback descriptors. Process the callbacks from |
| 497 | // back to front so that the last callback with a given name takes |
| 498 | // precedence over previously added callbacks with that name. |
| 499 | for (int i = nof_callbacks - 1; i >= 0; i--) { |
| 500 | Handle<AccessorInfo> entry = |
| 501 | Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i))); |
| 502 | // Ensure the key is a symbol before writing into the instance descriptor. |
| 503 | Handle<String> key = |
| 504 | SymbolFromString(Handle<String>(String::cast(entry->name()))); |
| 505 | // Check if a descriptor with this name already exists before writing. |
| 506 | if (result->BinarySearch(*key, 0, descriptor_count - 1) == |
| 507 | DescriptorArray::kNotFound) { |
| 508 | CallbacksDescriptor desc(*key, *entry, entry->property_attributes()); |
| 509 | w.Write(&desc); |
| 510 | descriptor_count++; |
| 511 | } else { |
| 512 | duplicates++; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | // If duplicates were detected, allocate a result of the right size |
| 517 | // and transfer the elements. |
| 518 | if (duplicates > 0) { |
| 519 | Handle<DescriptorArray> new_result = |
| 520 | NewDescriptorArray(result->number_of_descriptors() - duplicates); |
| 521 | DescriptorWriter w(*new_result); |
| 522 | DescriptorReader r(*result); |
| 523 | while (!w.eos()) { |
| 524 | w.WriteFrom(&r); |
| 525 | r.advance(); |
| 526 | } |
| 527 | result = new_result; |
| 528 | } |
| 529 | |
| 530 | // Sort the result before returning. |
| 531 | result->Sort(); |
| 532 | return result; |
| 533 | } |
| 534 | |
| 535 | |
| 536 | Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor, |
| 537 | PretenureFlag pretenure) { |
| 538 | CALL_HEAP_FUNCTION(Heap::AllocateJSObject(*constructor, pretenure), JSObject); |
| 539 | } |
| 540 | |
| 541 | |
| 542 | Handle<JSObject> Factory::NewObjectLiteral(int expected_number_of_properties) { |
| 543 | Handle<Map> map = Handle<Map>(Top::object_function()->initial_map()); |
| 544 | map = Factory::CopyMap(map); |
| 545 | map->set_instance_descriptors( |
| 546 | DescriptorArray::cast(Heap::empty_fixed_array())); |
| 547 | map->set_unused_property_fields(expected_number_of_properties); |
| 548 | CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(*map, TENURED), |
| 549 | JSObject); |
| 550 | } |
| 551 | |
| 552 | |
| 553 | Handle<JSArray> Factory::NewArrayLiteral(int length) { |
| 554 | return NewJSArrayWithElements(NewFixedArray(length), TENURED); |
| 555 | } |
| 556 | |
| 557 | |
| 558 | Handle<JSArray> Factory::NewJSArray(int length, |
| 559 | PretenureFlag pretenure) { |
| 560 | Handle<JSObject> obj = NewJSObject(Top::array_function(), pretenure); |
| 561 | CALL_HEAP_FUNCTION(Handle<JSArray>::cast(obj)->Initialize(length), JSArray); |
| 562 | } |
| 563 | |
| 564 | |
| 565 | Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements, |
| 566 | PretenureFlag pretenure) { |
| 567 | Handle<JSArray> result = |
| 568 | Handle<JSArray>::cast(NewJSObject(Top::array_function(), pretenure)); |
| 569 | result->SetContent(*elements); |
| 570 | return result; |
| 571 | } |
| 572 | |
| 573 | |
| 574 | Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) { |
| 575 | CALL_HEAP_FUNCTION(Heap::AllocateSharedFunctionInfo(*name), |
| 576 | SharedFunctionInfo); |
| 577 | } |
| 578 | |
| 579 | |
| 580 | Handle<Dictionary> Factory::DictionaryAtNumberPut(Handle<Dictionary> dictionary, |
| 581 | uint32_t key, |
| 582 | Handle<Object> value) { |
| 583 | CALL_HEAP_FUNCTION(dictionary->AtNumberPut(key, *value), Dictionary); |
| 584 | } |
| 585 | |
| 586 | |
| 587 | Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name, |
| 588 | Handle<Object> prototype) { |
| 589 | Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name); |
| 590 | CALL_HEAP_FUNCTION(Heap::AllocateFunction(*Top::function_map(), |
| 591 | *function_share, |
| 592 | *prototype), |
| 593 | JSFunction); |
| 594 | } |
| 595 | |
| 596 | |
| 597 | Handle<JSFunction> Factory::NewFunction(Handle<String> name, |
| 598 | Handle<Object> prototype) { |
| 599 | Handle<JSFunction> fun = NewFunctionHelper(name, prototype); |
| 600 | fun->set_context(Top::context()->global_context()); |
| 601 | return fun; |
| 602 | } |
| 603 | |
| 604 | |
| 605 | Handle<Object> Factory::ToObject(Handle<Object> object, |
| 606 | Handle<Context> global_context) { |
| 607 | CALL_HEAP_FUNCTION(object->ToObject(*global_context), Object); |
| 608 | } |
| 609 | |
| 610 | |
| 611 | Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee, |
| 612 | int length) { |
| 613 | CALL_HEAP_FUNCTION(Heap::AllocateArgumentsObject(*callee, length), JSObject); |
| 614 | } |
| 615 | |
| 616 | |
| 617 | Handle<JSFunction> Factory::CreateApiFunction( |
| 618 | Handle<FunctionTemplateInfo> obj, |
| 619 | bool is_global) { |
| 620 | Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::HandleApiCall)); |
| 621 | |
| 622 | int internal_field_count = Smi::cast(obj->internal_field_count())->value(); |
| 623 | int size = kPointerSize * internal_field_count; |
| 624 | if (is_global) { |
| 625 | size += JSGlobalObject::kSize; |
| 626 | } else { |
| 627 | size += JSObject::kHeaderSize; |
| 628 | } |
| 629 | |
| 630 | InstanceType type = is_global ? JS_GLOBAL_OBJECT_TYPE : JS_OBJECT_TYPE; |
| 631 | |
| 632 | Handle<JSFunction> result = |
| 633 | Factory::NewFunction(Factory::empty_symbol(), type, size, code, true); |
| 634 | // Set class name. |
| 635 | Handle<Object> class_name = Handle<Object>(obj->class_name()); |
| 636 | if (class_name->IsString()) { |
| 637 | result->shared()->set_instance_class_name(*class_name); |
| 638 | result->shared()->set_name(*class_name); |
| 639 | } |
| 640 | |
| 641 | Handle<Map> map = Handle<Map>(result->initial_map()); |
| 642 | |
| 643 | // Mark as undetectable if needed. |
| 644 | if (obj->undetectable()) { |
| 645 | map->set_is_undetectable(); |
| 646 | } |
| 647 | |
| 648 | // Mark as hidden for the __proto__ accessor if needed. |
| 649 | if (obj->hidden_prototype()) { |
| 650 | map->set_is_hidden_prototype(); |
| 651 | } |
| 652 | |
| 653 | // Mark as needs_access_check if needed. |
| 654 | if (obj->needs_access_check()) { |
| 655 | map->set_needs_access_check(); |
| 656 | } |
| 657 | |
| 658 | // If the function template info specifies a lookup handler the |
| 659 | // initial_map must have set the bit has_special_lookup. |
| 660 | if (obj->lookup_callback()->IsProxy()) { |
| 661 | ASSERT(!map->has_special_lookup()); |
| 662 | map->set_special_lookup(); |
| 663 | } |
| 664 | |
| 665 | // Set interceptor information in the map. |
| 666 | if (!obj->named_property_handler()->IsUndefined()) { |
| 667 | map->set_has_named_interceptor(); |
| 668 | } |
| 669 | if (!obj->indexed_property_handler()->IsUndefined()) { |
| 670 | map->set_has_indexed_interceptor(); |
| 671 | } |
| 672 | |
| 673 | // Set instance call-as-function information in the map. |
| 674 | if (!obj->instance_call_handler()->IsUndefined()) { |
| 675 | map->set_has_instance_call_handler(); |
| 676 | } |
| 677 | |
| 678 | result->shared()->set_function_data(*obj); |
| 679 | |
| 680 | // Recursively copy parent templates' accessors, 'data' may be modified. |
| 681 | Handle<DescriptorArray> array = |
| 682 | Handle<DescriptorArray>(map->instance_descriptors()); |
| 683 | while (true) { |
| 684 | Handle<Object> props = Handle<Object>(obj->property_accessors()); |
| 685 | if (!props->IsUndefined()) { |
| 686 | array = Factory::CopyAppendCallbackDescriptors(array, props); |
| 687 | } |
| 688 | Handle<Object> parent = Handle<Object>(obj->parent_template()); |
| 689 | if (parent->IsUndefined()) break; |
| 690 | obj = Handle<FunctionTemplateInfo>::cast(parent); |
| 691 | } |
| 692 | if (array->length() > 0) { |
| 693 | map->set_instance_descriptors(*array); |
| 694 | } |
| 695 | |
| 696 | return result; |
| 697 | } |
| 698 | |
| 699 | |
| 700 | void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc, |
| 701 | Handle<JSObject> instance, |
| 702 | bool* pending_exception) { |
| 703 | // Configure the instance by adding the properties specified by the |
| 704 | // instance template. |
| 705 | Handle<Object> instance_template = Handle<Object>(desc->instance_template()); |
| 706 | if (!instance_template->IsUndefined()) { |
| 707 | Execution::ConfigureInstance(instance, |
| 708 | instance_template, |
| 709 | pending_exception); |
| 710 | } else { |
| 711 | *pending_exception = false; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | |
| 716 | } } // namespace v8::internal |