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 "accessors.h" |
| 31 | #include "api.h" |
| 32 | #include "bootstrapper.h" |
| 33 | #include "compiler.h" |
| 34 | #include "debug.h" |
| 35 | #include "execution.h" |
| 36 | #include "global-handles.h" |
| 37 | #include "macro-assembler.h" |
| 38 | #include "natives.h" |
| 39 | |
| 40 | namespace v8 { |
| 41 | namespace internal { |
| 42 | |
| 43 | // A SourceCodeCache uses a FixedArray to store pairs of |
| 44 | // (AsciiString*, JSFunction*), mapping names of native code files |
| 45 | // (runtime.js, etc.) to precompiled functions. Instead of mapping |
| 46 | // names to functions it might make sense to let the JS2C tool |
| 47 | // generate an index for each native JS file. |
| 48 | class SourceCodeCache BASE_EMBEDDED { |
| 49 | public: |
| 50 | explicit SourceCodeCache(Script::Type type): type_(type), cache_(NULL) { } |
| 51 | |
| 52 | void Initialize(bool create_heap_objects) { |
| 53 | cache_ = create_heap_objects ? Heap::empty_fixed_array() : NULL; |
| 54 | } |
| 55 | |
| 56 | void Iterate(ObjectVisitor* v) { |
| 57 | v->VisitPointer(bit_cast<Object**, FixedArray**>(&cache_)); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | bool Lookup(Vector<const char> name, Handle<JSFunction>* handle) { |
| 62 | for (int i = 0; i < cache_->length(); i+=2) { |
| 63 | SeqAsciiString* str = SeqAsciiString::cast(cache_->get(i)); |
| 64 | if (str->IsEqualTo(name)) { |
| 65 | *handle = Handle<JSFunction>(JSFunction::cast(cache_->get(i + 1))); |
| 66 | return true; |
| 67 | } |
| 68 | } |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | void Add(Vector<const char> name, Handle<JSFunction> fun) { |
| 74 | ASSERT(fun->IsBoilerplate()); |
| 75 | HandleScope scope; |
| 76 | int length = cache_->length(); |
| 77 | Handle<FixedArray> new_array = |
| 78 | Factory::NewFixedArray(length + 2, TENURED); |
| 79 | cache_->CopyTo(0, *new_array, 0, cache_->length()); |
| 80 | cache_ = *new_array; |
| 81 | Handle<String> str = Factory::NewStringFromAscii(name, TENURED); |
| 82 | cache_->set(length, *str); |
| 83 | cache_->set(length + 1, *fun); |
| 84 | Script::cast(fun->shared()->script())->set_type(Smi::FromInt(type_)); |
| 85 | } |
| 86 | |
| 87 | private: |
| 88 | Script::Type type_; |
| 89 | FixedArray* cache_; |
| 90 | DISALLOW_COPY_AND_ASSIGN(SourceCodeCache); |
| 91 | }; |
| 92 | |
| 93 | static SourceCodeCache natives_cache(Script::TYPE_NATIVE); |
| 94 | static SourceCodeCache extensions_cache(Script::TYPE_EXTENSION); |
| 95 | |
| 96 | |
| 97 | Handle<String> Bootstrapper::NativesSourceLookup(int index) { |
| 98 | ASSERT(0 <= index && index < Natives::GetBuiltinsCount()); |
| 99 | if (Heap::natives_source_cache()->get(index)->IsUndefined()) { |
| 100 | Handle<String> source_code = |
| 101 | Factory::NewStringFromAscii(Natives::GetScriptSource(index)); |
| 102 | Heap::natives_source_cache()->set(index, *source_code); |
| 103 | } |
| 104 | Handle<Object> cached_source(Heap::natives_source_cache()->get(index)); |
| 105 | return Handle<String>::cast(cached_source); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | bool Bootstrapper::NativesCacheLookup(Vector<const char> name, |
| 110 | Handle<JSFunction>* handle) { |
| 111 | return natives_cache.Lookup(name, handle); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | void Bootstrapper::NativesCacheAdd(Vector<const char> name, |
| 116 | Handle<JSFunction> fun) { |
| 117 | natives_cache.Add(name, fun); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | void Bootstrapper::Initialize(bool create_heap_objects) { |
| 122 | natives_cache.Initialize(create_heap_objects); |
| 123 | extensions_cache.Initialize(create_heap_objects); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | void Bootstrapper::TearDown() { |
| 128 | natives_cache.Initialize(false); // Yes, symmetrical |
| 129 | extensions_cache.Initialize(false); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | // Pending fixups are code positions that refer to builtin code |
| 134 | // objects that were not available at the time the code was generated. |
| 135 | // The pending list is processed whenever an environment has been |
| 136 | // created. |
| 137 | class PendingFixups : public AllStatic { |
| 138 | public: |
| 139 | static void Add(Code* code, MacroAssembler* masm); |
| 140 | static bool Process(Handle<JSBuiltinsObject> builtins); |
| 141 | |
| 142 | static void Iterate(ObjectVisitor* v); |
| 143 | |
| 144 | private: |
| 145 | static List<Object*> code_; |
| 146 | static List<const char*> name_; |
| 147 | static List<int> pc_; |
| 148 | static List<uint32_t> flags_; |
| 149 | |
| 150 | static void Clear(); |
| 151 | }; |
| 152 | |
| 153 | |
| 154 | List<Object*> PendingFixups::code_(0); |
| 155 | List<const char*> PendingFixups::name_(0); |
| 156 | List<int> PendingFixups::pc_(0); |
| 157 | List<uint32_t> PendingFixups::flags_(0); |
| 158 | |
| 159 | |
| 160 | void PendingFixups::Add(Code* code, MacroAssembler* masm) { |
| 161 | // Note this code is not only called during bootstrapping. |
| 162 | List<MacroAssembler::Unresolved>* unresolved = masm->unresolved(); |
| 163 | int n = unresolved->length(); |
| 164 | for (int i = 0; i < n; i++) { |
| 165 | const char* name = unresolved->at(i).name; |
| 166 | code_.Add(code); |
| 167 | name_.Add(name); |
| 168 | pc_.Add(unresolved->at(i).pc); |
| 169 | flags_.Add(unresolved->at(i).flags); |
| 170 | LOG(StringEvent("unresolved", name)); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | |
| 175 | bool PendingFixups::Process(Handle<JSBuiltinsObject> builtins) { |
| 176 | HandleScope scope; |
| 177 | // NOTE: Extra fixups may be added to the list during the iteration |
| 178 | // due to lazy compilation of functions during the processing. Do not |
| 179 | // cache the result of getting the length of the code list. |
| 180 | for (int i = 0; i < code_.length(); i++) { |
| 181 | const char* name = name_[i]; |
| 182 | uint32_t flags = flags_[i]; |
| 183 | Handle<String> symbol = Factory::LookupAsciiSymbol(name); |
| 184 | Object* o = builtins->GetProperty(*symbol); |
| 185 | #ifdef DEBUG |
| 186 | if (!o->IsJSFunction()) { |
| 187 | V8_Fatal(__FILE__, __LINE__, "Cannot resolve call to builtin %s", name); |
| 188 | } |
| 189 | #endif |
| 190 | Handle<JSFunction> f = Handle<JSFunction>(JSFunction::cast(o)); |
| 191 | // Make sure the number of parameters match the formal parameter count. |
| 192 | int argc = Bootstrapper::FixupFlagsArgumentsCount::decode(flags); |
| 193 | USE(argc); |
| 194 | ASSERT(f->shared()->formal_parameter_count() == argc); |
| 195 | if (!f->is_compiled()) { |
| 196 | // Do lazy compilation and check for stack overflows. |
| 197 | if (!CompileLazy(f, CLEAR_EXCEPTION)) { |
| 198 | Clear(); |
| 199 | return false; |
| 200 | } |
| 201 | } |
| 202 | Code* code = Code::cast(code_[i]); |
| 203 | Address pc = code->instruction_start() + pc_[i]; |
| 204 | bool is_pc_relative = Bootstrapper::FixupFlagsIsPCRelative::decode(flags); |
| 205 | bool use_code_object = Bootstrapper::FixupFlagsUseCodeObject::decode(flags); |
| 206 | |
| 207 | if (use_code_object) { |
| 208 | if (is_pc_relative) { |
| 209 | Assembler::set_target_address_at( |
| 210 | pc, reinterpret_cast<Address>(f->code())); |
| 211 | } else { |
| 212 | *reinterpret_cast<Object**>(pc) = f->code(); |
| 213 | } |
| 214 | } else { |
| 215 | Assembler::set_target_address_at(pc, f->code()->instruction_start()); |
| 216 | } |
| 217 | |
| 218 | LOG(StringEvent("resolved", name)); |
| 219 | } |
| 220 | Clear(); |
| 221 | |
| 222 | // TODO(1240818): We should probably try to avoid doing this for all |
| 223 | // the V8 builtin JS files. It should only happen after running |
| 224 | // runtime.js - just like there shouldn't be any fixups left after |
| 225 | // that. |
| 226 | for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) { |
| 227 | Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i); |
| 228 | Handle<String> name = Factory::LookupAsciiSymbol(Builtins::GetName(id)); |
| 229 | JSFunction* function = JSFunction::cast(builtins->GetProperty(*name)); |
| 230 | builtins->set_javascript_builtin(id, function); |
| 231 | } |
| 232 | |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | |
| 237 | void PendingFixups::Clear() { |
| 238 | code_.Clear(); |
| 239 | name_.Clear(); |
| 240 | pc_.Clear(); |
| 241 | flags_.Clear(); |
| 242 | } |
| 243 | |
| 244 | |
| 245 | void PendingFixups::Iterate(ObjectVisitor* v) { |
| 246 | if (!code_.is_empty()) { |
| 247 | v->VisitPointers(&code_[0], &code_[0] + code_.length()); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | |
| 252 | class Genesis BASE_EMBEDDED { |
| 253 | public: |
| 254 | Genesis(Handle<Object> global_object, |
| 255 | v8::Handle<v8::ObjectTemplate> global_template, |
| 256 | v8::ExtensionConfiguration* extensions); |
| 257 | ~Genesis(); |
| 258 | |
| 259 | Handle<Context> result() { return result_; } |
| 260 | |
| 261 | Genesis* previous() { return previous_; } |
| 262 | static Genesis* current() { return current_; } |
| 263 | |
| 264 | // Support for thread preemption. |
| 265 | static int ArchiveSpacePerThread(); |
| 266 | static char* ArchiveState(char* to); |
| 267 | static char* RestoreState(char* from); |
| 268 | |
| 269 | private: |
| 270 | Handle<Context> global_context_; |
| 271 | |
| 272 | // There may be more than one active genesis object: When GC is |
| 273 | // triggered during environment creation there may be weak handle |
| 274 | // processing callbacks which may create new environments. |
| 275 | Genesis* previous_; |
| 276 | static Genesis* current_; |
| 277 | |
| 278 | Handle<Context> global_context() { return global_context_; } |
| 279 | |
| 280 | void CreateRoots(v8::Handle<v8::ObjectTemplate> global_template, |
| 281 | Handle<Object> global_object); |
| 282 | void InstallNativeFunctions(); |
| 283 | bool InstallNatives(); |
| 284 | bool InstallExtensions(v8::ExtensionConfiguration* extensions); |
| 285 | bool InstallExtension(const char* name); |
| 286 | bool InstallExtension(v8::RegisteredExtension* current); |
| 287 | bool InstallSpecialObjects(); |
| 288 | bool ConfigureApiObject(Handle<JSObject> object, |
| 289 | Handle<ObjectTemplateInfo> object_template); |
| 290 | bool ConfigureGlobalObjects(v8::Handle<v8::ObjectTemplate> global_template); |
| 291 | |
| 292 | // Migrates all properties from the 'from' object to the 'to' |
| 293 | // object and overrides the prototype in 'to' with the one from |
| 294 | // 'from'. |
| 295 | void TransferObject(Handle<JSObject> from, Handle<JSObject> to); |
| 296 | void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to); |
| 297 | void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to); |
| 298 | |
| 299 | Handle<DescriptorArray> ComputeFunctionInstanceDescriptor( |
| 300 | bool make_prototype_read_only, |
| 301 | bool make_prototype_enumerable = false); |
| 302 | void MakeFunctionInstancePrototypeWritable(); |
| 303 | |
| 304 | void AddSpecialFunction(Handle<JSObject> prototype, |
| 305 | const char* name, |
| 306 | Handle<Code> code); |
| 307 | |
| 308 | void BuildSpecialFunctionTable(); |
| 309 | |
| 310 | static bool CompileBuiltin(int index); |
| 311 | static bool CompileNative(Vector<const char> name, Handle<String> source); |
| 312 | static bool CompileScriptCached(Vector<const char> name, |
| 313 | Handle<String> source, |
| 314 | SourceCodeCache* cache, |
| 315 | v8::Extension* extension, |
| 316 | bool use_runtime_context); |
| 317 | |
| 318 | Handle<Context> result_; |
| 319 | }; |
| 320 | |
| 321 | Genesis* Genesis::current_ = NULL; |
| 322 | |
| 323 | |
| 324 | void Bootstrapper::Iterate(ObjectVisitor* v) { |
| 325 | natives_cache.Iterate(v); |
| 326 | extensions_cache.Iterate(v); |
| 327 | PendingFixups::Iterate(v); |
| 328 | } |
| 329 | |
| 330 | |
| 331 | // While setting up the environment, we collect code positions that |
| 332 | // need to be patched before we can run any code in the environment. |
| 333 | void Bootstrapper::AddFixup(Code* code, MacroAssembler* masm) { |
| 334 | PendingFixups::Add(code, masm); |
| 335 | } |
| 336 | |
| 337 | |
| 338 | bool Bootstrapper::IsActive() { |
| 339 | return Genesis::current() != NULL; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | Handle<Context> Bootstrapper::CreateEnvironment( |
| 344 | Handle<Object> global_object, |
| 345 | v8::Handle<v8::ObjectTemplate> global_template, |
| 346 | v8::ExtensionConfiguration* extensions) { |
| 347 | Genesis genesis(global_object, global_template, extensions); |
| 348 | return genesis.result(); |
| 349 | } |
| 350 | |
| 351 | |
| 352 | static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) { |
| 353 | // object.__proto__ = proto; |
| 354 | Handle<Map> old_to_map = Handle<Map>(object->map()); |
| 355 | Handle<Map> new_to_map = Factory::CopyMapDropTransitions(old_to_map); |
| 356 | new_to_map->set_prototype(*proto); |
| 357 | object->set_map(*new_to_map); |
| 358 | } |
| 359 | |
| 360 | |
| 361 | void Bootstrapper::DetachGlobal(Handle<Context> env) { |
| 362 | JSGlobalProxy::cast(env->global_proxy())->set_context(*Factory::null_value()); |
| 363 | SetObjectPrototype(Handle<JSObject>(env->global_proxy()), |
| 364 | Factory::null_value()); |
| 365 | env->set_global_proxy(env->global()); |
| 366 | env->global()->set_global_receiver(env->global()); |
| 367 | } |
| 368 | |
| 369 | |
| 370 | Genesis::~Genesis() { |
| 371 | ASSERT(current_ == this); |
| 372 | current_ = previous_; |
| 373 | } |
| 374 | |
| 375 | |
| 376 | static Handle<JSFunction> InstallFunction(Handle<JSObject> target, |
| 377 | const char* name, |
| 378 | InstanceType type, |
| 379 | int instance_size, |
| 380 | Handle<JSObject> prototype, |
| 381 | Builtins::Name call, |
| 382 | bool is_ecma_native) { |
| 383 | Handle<String> symbol = Factory::LookupAsciiSymbol(name); |
| 384 | Handle<Code> call_code = Handle<Code>(Builtins::builtin(call)); |
| 385 | Handle<JSFunction> function = |
| 386 | Factory::NewFunctionWithPrototype(symbol, |
| 387 | type, |
| 388 | instance_size, |
| 389 | prototype, |
| 390 | call_code, |
| 391 | is_ecma_native); |
| 392 | SetProperty(target, symbol, function, DONT_ENUM); |
| 393 | if (is_ecma_native) { |
| 394 | function->shared()->set_instance_class_name(*symbol); |
| 395 | } |
| 396 | return function; |
| 397 | } |
| 398 | |
| 399 | |
| 400 | Handle<DescriptorArray> Genesis::ComputeFunctionInstanceDescriptor( |
| 401 | bool make_prototype_read_only, |
| 402 | bool make_prototype_enumerable) { |
| 403 | Handle<DescriptorArray> result = Factory::empty_descriptor_array(); |
| 404 | |
| 405 | // Add prototype. |
| 406 | PropertyAttributes attributes = static_cast<PropertyAttributes>( |
| 407 | (make_prototype_enumerable ? 0 : DONT_ENUM) |
| 408 | | DONT_DELETE |
| 409 | | (make_prototype_read_only ? READ_ONLY : 0)); |
| 410 | result = |
| 411 | Factory::CopyAppendProxyDescriptor( |
| 412 | result, |
| 413 | Factory::prototype_symbol(), |
| 414 | Factory::NewProxy(&Accessors::FunctionPrototype), |
| 415 | attributes); |
| 416 | |
| 417 | attributes = |
| 418 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 419 | // Add length. |
| 420 | result = |
| 421 | Factory::CopyAppendProxyDescriptor( |
| 422 | result, |
| 423 | Factory::length_symbol(), |
| 424 | Factory::NewProxy(&Accessors::FunctionLength), |
| 425 | attributes); |
| 426 | |
| 427 | // Add name. |
| 428 | result = |
| 429 | Factory::CopyAppendProxyDescriptor( |
| 430 | result, |
| 431 | Factory::name_symbol(), |
| 432 | Factory::NewProxy(&Accessors::FunctionName), |
| 433 | attributes); |
| 434 | |
| 435 | // Add arguments. |
| 436 | result = |
| 437 | Factory::CopyAppendProxyDescriptor( |
| 438 | result, |
| 439 | Factory::arguments_symbol(), |
| 440 | Factory::NewProxy(&Accessors::FunctionArguments), |
| 441 | attributes); |
| 442 | |
| 443 | // Add caller. |
| 444 | result = |
| 445 | Factory::CopyAppendProxyDescriptor( |
| 446 | result, |
| 447 | Factory::caller_symbol(), |
| 448 | Factory::NewProxy(&Accessors::FunctionCaller), |
| 449 | attributes); |
| 450 | |
| 451 | return result; |
| 452 | } |
| 453 | |
| 454 | |
| 455 | void Genesis::CreateRoots(v8::Handle<v8::ObjectTemplate> global_template, |
| 456 | Handle<Object> global_object) { |
| 457 | HandleScope scope; |
| 458 | // Allocate the global context FixedArray first and then patch the |
| 459 | // closure and extension object later (we need the empty function |
| 460 | // and the global object, but in order to create those, we need the |
| 461 | // global context). |
| 462 | global_context_ = |
| 463 | Handle<Context>::cast( |
| 464 | GlobalHandles::Create(*Factory::NewGlobalContext())); |
| 465 | Top::set_context(*global_context()); |
| 466 | |
| 467 | // Allocate the message listeners object. |
| 468 | v8::NeanderArray listeners; |
| 469 | global_context()->set_message_listeners(*listeners.value()); |
| 470 | |
| 471 | // Allocate the map for function instances. |
| 472 | Handle<Map> fm = Factory::NewMap(JS_FUNCTION_TYPE, JSFunction::kSize); |
| 473 | global_context()->set_function_instance_map(*fm); |
| 474 | // Please note that the prototype property for function instances must be |
| 475 | // writable. |
| 476 | Handle<DescriptorArray> function_map_descriptors = |
| 477 | ComputeFunctionInstanceDescriptor(false, false); |
| 478 | fm->set_instance_descriptors(*function_map_descriptors); |
| 479 | |
| 480 | // Allocate the function map first and then patch the prototype later |
| 481 | fm = Factory::NewMap(JS_FUNCTION_TYPE, JSFunction::kSize); |
| 482 | global_context()->set_function_map(*fm); |
| 483 | function_map_descriptors = ComputeFunctionInstanceDescriptor(true); |
| 484 | fm->set_instance_descriptors(*function_map_descriptors); |
| 485 | |
| 486 | Handle<String> object_name = Handle<String>(Heap::Object_symbol()); |
| 487 | |
| 488 | { // --- O b j e c t --- |
| 489 | Handle<JSFunction> object_fun = |
| 490 | Factory::NewFunction(object_name, Factory::null_value()); |
| 491 | Handle<Map> object_function_map = |
| 492 | Factory::NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 493 | object_fun->set_initial_map(*object_function_map); |
| 494 | object_function_map->set_constructor(*object_fun); |
| 495 | |
| 496 | global_context()->set_object_function(*object_fun); |
| 497 | |
| 498 | // Allocate a new prototype for the object function. |
| 499 | Handle<JSObject> prototype = Factory::NewJSObject(Top::object_function(), |
| 500 | TENURED); |
| 501 | |
| 502 | global_context()->set_initial_object_prototype(*prototype); |
| 503 | SetPrototype(object_fun, prototype); |
| 504 | object_function_map-> |
| 505 | set_instance_descriptors(Heap::empty_descriptor_array()); |
| 506 | } |
| 507 | |
| 508 | // Allocate the empty function as the prototype for function ECMAScript |
| 509 | // 262 15.3.4. |
| 510 | Handle<String> symbol = Factory::LookupAsciiSymbol("Empty"); |
| 511 | Handle<JSFunction> empty_function = |
| 512 | Factory::NewFunction(symbol, Factory::null_value()); |
| 513 | |
| 514 | { // --- E m p t y --- |
| 515 | Handle<Code> code = |
| 516 | Handle<Code>(Builtins::builtin(Builtins::EmptyFunction)); |
| 517 | empty_function->set_code(*code); |
| 518 | Handle<String> source = Factory::NewStringFromAscii(CStrVector("() {}")); |
| 519 | Handle<Script> script = Factory::NewScript(source); |
| 520 | script->set_type(Smi::FromInt(Script::TYPE_NATIVE)); |
| 521 | empty_function->shared()->set_script(*script); |
| 522 | empty_function->shared()->set_start_position(0); |
| 523 | empty_function->shared()->set_end_position(source->length()); |
| 524 | empty_function->shared()->DontAdaptArguments(); |
| 525 | global_context()->function_map()->set_prototype(*empty_function); |
| 526 | global_context()->function_instance_map()->set_prototype(*empty_function); |
| 527 | |
| 528 | // Allocate the function map first and then patch the prototype later |
| 529 | Handle<Map> empty_fm = Factory::CopyMapDropDescriptors(fm); |
| 530 | empty_fm->set_instance_descriptors(*function_map_descriptors); |
| 531 | empty_fm->set_prototype(global_context()->object_function()->prototype()); |
| 532 | empty_function->set_map(*empty_fm); |
| 533 | } |
| 534 | |
| 535 | { // --- G l o b a l --- |
| 536 | // Step 1: create a fresh inner JSGlobalObject |
| 537 | Handle<GlobalObject> object; |
| 538 | { |
| 539 | Handle<JSFunction> js_global_function; |
| 540 | Handle<ObjectTemplateInfo> js_global_template; |
| 541 | if (!global_template.IsEmpty()) { |
| 542 | // Get prototype template of the global_template |
| 543 | Handle<ObjectTemplateInfo> data = |
| 544 | v8::Utils::OpenHandle(*global_template); |
| 545 | Handle<FunctionTemplateInfo> global_constructor = |
| 546 | Handle<FunctionTemplateInfo>( |
| 547 | FunctionTemplateInfo::cast(data->constructor())); |
| 548 | Handle<Object> proto_template(global_constructor->prototype_template()); |
| 549 | if (!proto_template->IsUndefined()) { |
| 550 | js_global_template = |
| 551 | Handle<ObjectTemplateInfo>::cast(proto_template); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | if (js_global_template.is_null()) { |
| 556 | Handle<String> name = Handle<String>(Heap::empty_symbol()); |
| 557 | Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal)); |
| 558 | js_global_function = |
| 559 | Factory::NewFunction(name, JS_GLOBAL_OBJECT_TYPE, |
| 560 | JSGlobalObject::kSize, code, true); |
| 561 | // Change the constructor property of the prototype of the |
| 562 | // hidden global function to refer to the Object function. |
| 563 | Handle<JSObject> prototype = |
| 564 | Handle<JSObject>( |
| 565 | JSObject::cast(js_global_function->instance_prototype())); |
| 566 | SetProperty(prototype, Factory::constructor_symbol(), |
| 567 | Top::object_function(), NONE); |
| 568 | } else { |
| 569 | Handle<FunctionTemplateInfo> js_global_constructor( |
| 570 | FunctionTemplateInfo::cast(js_global_template->constructor())); |
| 571 | js_global_function = |
| 572 | Factory::CreateApiFunction(js_global_constructor, |
| 573 | Factory::InnerGlobalObject); |
| 574 | } |
| 575 | |
| 576 | js_global_function->initial_map()->set_is_hidden_prototype(); |
| 577 | object = Factory::NewGlobalObject(js_global_function); |
| 578 | } |
| 579 | |
| 580 | // Set the global context for the global object. |
| 581 | object->set_global_context(*global_context()); |
| 582 | |
| 583 | // Step 2: create or re-initialize the global proxy object. |
| 584 | Handle<JSGlobalProxy> global_proxy; |
| 585 | { |
| 586 | Handle<JSFunction> global_proxy_function; |
| 587 | if (global_template.IsEmpty()) { |
| 588 | Handle<String> name = Handle<String>(Heap::empty_symbol()); |
| 589 | Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal)); |
| 590 | global_proxy_function = |
| 591 | Factory::NewFunction(name, JS_GLOBAL_PROXY_TYPE, |
| 592 | JSGlobalProxy::kSize, code, true); |
| 593 | } else { |
| 594 | Handle<ObjectTemplateInfo> data = |
| 595 | v8::Utils::OpenHandle(*global_template); |
| 596 | Handle<FunctionTemplateInfo> global_constructor( |
| 597 | FunctionTemplateInfo::cast(data->constructor())); |
| 598 | global_proxy_function = |
| 599 | Factory::CreateApiFunction(global_constructor, |
| 600 | Factory::OuterGlobalObject); |
| 601 | } |
| 602 | |
| 603 | Handle<String> global_name = Factory::LookupAsciiSymbol("global"); |
| 604 | global_proxy_function->shared()->set_instance_class_name(*global_name); |
| 605 | global_proxy_function->initial_map()->set_is_access_check_needed(true); |
| 606 | |
| 607 | // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects |
| 608 | |
| 609 | if (global_object.location() != NULL) { |
| 610 | ASSERT(global_object->IsJSGlobalProxy()); |
| 611 | global_proxy = |
| 612 | ReinitializeJSGlobalProxy( |
| 613 | global_proxy_function, |
| 614 | Handle<JSGlobalProxy>::cast(global_object)); |
| 615 | } else { |
| 616 | global_proxy = Handle<JSGlobalProxy>::cast( |
| 617 | Factory::NewJSObject(global_proxy_function, TENURED)); |
| 618 | } |
| 619 | |
| 620 | // Security setup: Set the security token of the global object to |
| 621 | // its the inner global. This makes the security check between two |
| 622 | // different contexts fail by default even in case of global |
| 623 | // object reinitialization. |
| 624 | object->set_global_receiver(*global_proxy); |
| 625 | global_proxy->set_context(*global_context()); |
| 626 | } |
| 627 | |
| 628 | { // --- G l o b a l C o n t e x t --- |
| 629 | // use the empty function as closure (no scope info) |
| 630 | global_context()->set_closure(*empty_function); |
| 631 | global_context()->set_fcontext(*global_context()); |
| 632 | global_context()->set_previous(NULL); |
| 633 | |
| 634 | // set extension and global object |
| 635 | global_context()->set_extension(*object); |
| 636 | global_context()->set_global(*object); |
| 637 | global_context()->set_global_proxy(*global_proxy); |
| 638 | // use inner global object as security token by default |
| 639 | global_context()->set_security_token(*object); |
| 640 | } |
| 641 | |
| 642 | Handle<JSObject> global = Handle<JSObject>(global_context()->global()); |
| 643 | SetProperty(global, object_name, Top::object_function(), DONT_ENUM); |
| 644 | } |
| 645 | |
| 646 | Handle<JSObject> global = Handle<JSObject>(global_context()->global()); |
| 647 | |
| 648 | // Install global Function object |
| 649 | InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize, |
| 650 | empty_function, Builtins::Illegal, true); // ECMA native. |
| 651 | |
| 652 | { // --- A r r a y --- |
| 653 | Handle<JSFunction> array_function = |
| 654 | InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize, |
| 655 | Top::initial_object_prototype(), Builtins::ArrayCode, |
| 656 | true); |
| 657 | array_function->shared()->set_construct_stub( |
| 658 | Builtins::builtin(Builtins::ArrayConstructCode)); |
| 659 | array_function->shared()->DontAdaptArguments(); |
| 660 | |
| 661 | // This seems a bit hackish, but we need to make sure Array.length |
| 662 | // is 1. |
| 663 | array_function->shared()->set_length(1); |
| 664 | Handle<DescriptorArray> array_descriptors = |
| 665 | Factory::CopyAppendProxyDescriptor( |
| 666 | Factory::empty_descriptor_array(), |
| 667 | Factory::length_symbol(), |
| 668 | Factory::NewProxy(&Accessors::ArrayLength), |
| 669 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE)); |
| 670 | |
| 671 | // Cache the fast JavaScript array map |
| 672 | global_context()->set_js_array_map(array_function->initial_map()); |
| 673 | global_context()->js_array_map()->set_instance_descriptors( |
| 674 | *array_descriptors); |
| 675 | // array_function is used internally. JS code creating array object should |
| 676 | // search for the 'Array' property on the global object and use that one |
| 677 | // as the constructor. 'Array' property on a global object can be |
| 678 | // overwritten by JS code. |
| 679 | global_context()->set_array_function(*array_function); |
| 680 | } |
| 681 | |
| 682 | { // --- N u m b e r --- |
| 683 | Handle<JSFunction> number_fun = |
| 684 | InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize, |
| 685 | Top::initial_object_prototype(), Builtins::Illegal, |
| 686 | true); |
| 687 | global_context()->set_number_function(*number_fun); |
| 688 | } |
| 689 | |
| 690 | { // --- B o o l e a n --- |
| 691 | Handle<JSFunction> boolean_fun = |
| 692 | InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize, |
| 693 | Top::initial_object_prototype(), Builtins::Illegal, |
| 694 | true); |
| 695 | global_context()->set_boolean_function(*boolean_fun); |
| 696 | } |
| 697 | |
| 698 | { // --- S t r i n g --- |
| 699 | Handle<JSFunction> string_fun = |
| 700 | InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize, |
| 701 | Top::initial_object_prototype(), Builtins::Illegal, |
| 702 | true); |
| 703 | global_context()->set_string_function(*string_fun); |
| 704 | // Add 'length' property to strings. |
| 705 | Handle<DescriptorArray> string_descriptors = |
| 706 | Factory::CopyAppendProxyDescriptor( |
| 707 | Factory::empty_descriptor_array(), |
| 708 | Factory::length_symbol(), |
| 709 | Factory::NewProxy(&Accessors::StringLength), |
| 710 | static_cast<PropertyAttributes>(DONT_ENUM | |
| 711 | DONT_DELETE | |
| 712 | READ_ONLY)); |
| 713 | |
| 714 | Handle<Map> string_map = |
| 715 | Handle<Map>(global_context()->string_function()->initial_map()); |
| 716 | string_map->set_instance_descriptors(*string_descriptors); |
| 717 | } |
| 718 | |
| 719 | { // --- D a t e --- |
| 720 | // Builtin functions for Date.prototype. |
| 721 | Handle<JSFunction> date_fun = |
| 722 | InstallFunction(global, "Date", JS_VALUE_TYPE, JSValue::kSize, |
| 723 | Top::initial_object_prototype(), Builtins::Illegal, |
| 724 | true); |
| 725 | |
| 726 | global_context()->set_date_function(*date_fun); |
| 727 | } |
| 728 | |
| 729 | |
| 730 | { // -- R e g E x p |
| 731 | // Builtin functions for RegExp.prototype. |
| 732 | Handle<JSFunction> regexp_fun = |
| 733 | InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize, |
| 734 | Top::initial_object_prototype(), Builtins::Illegal, |
| 735 | true); |
| 736 | |
| 737 | global_context()->set_regexp_function(*regexp_fun); |
| 738 | } |
| 739 | |
| 740 | { // -- J S O N |
| 741 | Handle<String> name = Factory::NewStringFromAscii(CStrVector("JSON")); |
| 742 | Handle<JSFunction> cons = Factory::NewFunction( |
| 743 | name, |
| 744 | Factory::the_hole_value()); |
| 745 | cons->SetInstancePrototype(global_context()->initial_object_prototype()); |
| 746 | cons->SetInstanceClassName(*name); |
| 747 | Handle<JSObject> json_object = Factory::NewJSObject(cons, TENURED); |
| 748 | ASSERT(json_object->IsJSObject()); |
| 749 | SetProperty(global, name, json_object, DONT_ENUM); |
| 750 | global_context()->set_json_object(*json_object); |
| 751 | } |
| 752 | |
| 753 | { // --- arguments_boilerplate_ |
| 754 | // Make sure we can recognize argument objects at runtime. |
| 755 | // This is done by introducing an anonymous function with |
| 756 | // class_name equals 'Arguments'. |
| 757 | Handle<String> symbol = Factory::LookupAsciiSymbol("Arguments"); |
| 758 | Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal)); |
| 759 | Handle<JSObject> prototype = |
| 760 | Handle<JSObject>( |
| 761 | JSObject::cast(global_context()->object_function()->prototype())); |
| 762 | |
| 763 | Handle<JSFunction> function = |
| 764 | Factory::NewFunctionWithPrototype(symbol, |
| 765 | JS_OBJECT_TYPE, |
| 766 | JSObject::kHeaderSize, |
| 767 | prototype, |
| 768 | code, |
| 769 | false); |
| 770 | ASSERT(!function->has_initial_map()); |
| 771 | function->shared()->set_instance_class_name(*symbol); |
| 772 | function->shared()->set_expected_nof_properties(2); |
| 773 | Handle<JSObject> result = Factory::NewJSObject(function); |
| 774 | |
| 775 | global_context()->set_arguments_boilerplate(*result); |
| 776 | // Note: callee must be added as the first property and |
| 777 | // length must be added as the second property. |
| 778 | SetProperty(result, Factory::callee_symbol(), |
| 779 | Factory::undefined_value(), |
| 780 | DONT_ENUM); |
| 781 | SetProperty(result, Factory::length_symbol(), |
| 782 | Factory::undefined_value(), |
| 783 | DONT_ENUM); |
| 784 | |
| 785 | #ifdef DEBUG |
| 786 | LookupResult lookup; |
| 787 | result->LocalLookup(Heap::callee_symbol(), &lookup); |
| 788 | ASSERT(lookup.IsValid() && (lookup.type() == FIELD)); |
| 789 | ASSERT(lookup.GetFieldIndex() == Heap::arguments_callee_index); |
| 790 | |
| 791 | result->LocalLookup(Heap::length_symbol(), &lookup); |
| 792 | ASSERT(lookup.IsValid() && (lookup.type() == FIELD)); |
| 793 | ASSERT(lookup.GetFieldIndex() == Heap::arguments_length_index); |
| 794 | |
| 795 | ASSERT(result->map()->inobject_properties() > Heap::arguments_callee_index); |
| 796 | ASSERT(result->map()->inobject_properties() > Heap::arguments_length_index); |
| 797 | |
| 798 | // Check the state of the object. |
| 799 | ASSERT(result->HasFastProperties()); |
| 800 | ASSERT(result->HasFastElements()); |
| 801 | #endif |
| 802 | } |
| 803 | |
| 804 | { // --- context extension |
| 805 | // Create a function for the context extension objects. |
| 806 | Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal)); |
| 807 | Handle<JSFunction> context_extension_fun = |
| 808 | Factory::NewFunction(Factory::empty_symbol(), |
| 809 | JS_CONTEXT_EXTENSION_OBJECT_TYPE, |
| 810 | JSObject::kHeaderSize, |
| 811 | code, |
| 812 | true); |
| 813 | |
| 814 | Handle<String> name = Factory::LookupAsciiSymbol("context_extension"); |
| 815 | context_extension_fun->shared()->set_instance_class_name(*name); |
| 816 | global_context()->set_context_extension_function(*context_extension_fun); |
| 817 | } |
| 818 | |
| 819 | |
| 820 | { |
| 821 | // Setup the call-as-function delegate. |
| 822 | Handle<Code> code = |
| 823 | Handle<Code>(Builtins::builtin(Builtins::HandleApiCallAsFunction)); |
| 824 | Handle<JSFunction> delegate = |
| 825 | Factory::NewFunction(Factory::empty_symbol(), JS_OBJECT_TYPE, |
| 826 | JSObject::kHeaderSize, code, true); |
| 827 | global_context()->set_call_as_function_delegate(*delegate); |
| 828 | delegate->shared()->DontAdaptArguments(); |
| 829 | } |
| 830 | |
| 831 | { |
| 832 | // Setup the call-as-constructor delegate. |
| 833 | Handle<Code> code = |
| 834 | Handle<Code>(Builtins::builtin(Builtins::HandleApiCallAsConstructor)); |
| 835 | Handle<JSFunction> delegate = |
| 836 | Factory::NewFunction(Factory::empty_symbol(), JS_OBJECT_TYPE, |
| 837 | JSObject::kHeaderSize, code, true); |
| 838 | global_context()->set_call_as_constructor_delegate(*delegate); |
| 839 | delegate->shared()->DontAdaptArguments(); |
| 840 | } |
| 841 | |
| 842 | global_context()->set_special_function_table(Heap::empty_fixed_array()); |
| 843 | |
| 844 | // Initialize the out of memory slot. |
| 845 | global_context()->set_out_of_memory(Heap::false_value()); |
| 846 | |
| 847 | // Initialize the data slot. |
| 848 | global_context()->set_data(Heap::undefined_value()); |
| 849 | } |
| 850 | |
| 851 | |
| 852 | bool Genesis::CompileBuiltin(int index) { |
| 853 | Vector<const char> name = Natives::GetScriptName(index); |
| 854 | Handle<String> source_code = Bootstrapper::NativesSourceLookup(index); |
| 855 | return CompileNative(name, source_code); |
| 856 | } |
| 857 | |
| 858 | |
| 859 | bool Genesis::CompileNative(Vector<const char> name, Handle<String> source) { |
| 860 | HandleScope scope; |
| 861 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 862 | Debugger::set_compiling_natives(true); |
| 863 | #endif |
| 864 | bool result = |
| 865 | CompileScriptCached(name, source, &natives_cache, NULL, true); |
| 866 | ASSERT(Top::has_pending_exception() != result); |
| 867 | if (!result) Top::clear_pending_exception(); |
| 868 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 869 | Debugger::set_compiling_natives(false); |
| 870 | #endif |
| 871 | return result; |
| 872 | } |
| 873 | |
| 874 | |
| 875 | bool Genesis::CompileScriptCached(Vector<const char> name, |
| 876 | Handle<String> source, |
| 877 | SourceCodeCache* cache, |
| 878 | v8::Extension* extension, |
| 879 | bool use_runtime_context) { |
| 880 | HandleScope scope; |
| 881 | Handle<JSFunction> boilerplate; |
| 882 | |
| 883 | // If we can't find the function in the cache, we compile a new |
| 884 | // function and insert it into the cache. |
| 885 | if (!cache->Lookup(name, &boilerplate)) { |
| 886 | ASSERT(source->IsAsciiRepresentation()); |
| 887 | Handle<String> script_name = Factory::NewStringFromUtf8(name); |
| 888 | boilerplate = |
| 889 | Compiler::Compile(source, script_name, 0, 0, extension, NULL); |
| 890 | if (boilerplate.is_null()) return false; |
| 891 | cache->Add(name, boilerplate); |
| 892 | } |
| 893 | |
| 894 | // Setup the function context. Conceptually, we should clone the |
| 895 | // function before overwriting the context but since we're in a |
| 896 | // single-threaded environment it is not strictly necessary. |
| 897 | ASSERT(Top::context()->IsGlobalContext()); |
| 898 | Handle<Context> context = |
| 899 | Handle<Context>(use_runtime_context |
| 900 | ? Top::context()->runtime_context() |
| 901 | : Top::context()); |
| 902 | Handle<JSFunction> fun = |
| 903 | Factory::NewFunctionFromBoilerplate(boilerplate, context); |
| 904 | |
| 905 | // Call function using the either the runtime object or the global |
| 906 | // object as the receiver. Provide no parameters. |
| 907 | Handle<Object> receiver = |
| 908 | Handle<Object>(use_runtime_context |
| 909 | ? Top::context()->builtins() |
| 910 | : Top::context()->global()); |
| 911 | bool has_pending_exception; |
| 912 | Handle<Object> result = |
| 913 | Execution::Call(fun, receiver, 0, NULL, &has_pending_exception); |
| 914 | if (has_pending_exception) return false; |
| 915 | return PendingFixups::Process( |
| 916 | Handle<JSBuiltinsObject>(Top::context()->builtins())); |
| 917 | } |
| 918 | |
| 919 | |
| 920 | #define INSTALL_NATIVE(Type, name, var) \ |
| 921 | Handle<String> var##_name = Factory::LookupAsciiSymbol(name); \ |
| 922 | global_context()->set_##var(Type::cast(global_context()-> \ |
| 923 | builtins()-> \ |
| 924 | GetProperty(*var##_name))); |
| 925 | |
| 926 | void Genesis::InstallNativeFunctions() { |
| 927 | HandleScope scope; |
| 928 | INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun); |
| 929 | INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun); |
| 930 | INSTALL_NATIVE(JSFunction, "ToString", to_string_fun); |
| 931 | INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun); |
| 932 | INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun); |
| 933 | INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun); |
| 934 | INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun); |
| 935 | INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun); |
| 936 | INSTALL_NATIVE(JSFunction, "ToBoolean", to_boolean_fun); |
| 937 | INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun); |
| 938 | INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance", |
| 939 | configure_instance_fun); |
| 940 | INSTALL_NATIVE(JSFunction, "MakeMessage", make_message_fun); |
| 941 | INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun); |
| 942 | INSTALL_NATIVE(JSObject, "functionCache", function_cache); |
| 943 | } |
| 944 | |
| 945 | #undef INSTALL_NATIVE |
| 946 | |
| 947 | |
| 948 | bool Genesis::InstallNatives() { |
| 949 | HandleScope scope; |
| 950 | |
| 951 | // Create a function for the builtins object. Allocate space for the |
| 952 | // JavaScript builtins, a reference to the builtins object |
| 953 | // (itself) and a reference to the global_context directly in the object. |
| 954 | Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal)); |
| 955 | Handle<JSFunction> builtins_fun = |
| 956 | Factory::NewFunction(Factory::empty_symbol(), JS_BUILTINS_OBJECT_TYPE, |
| 957 | JSBuiltinsObject::kSize, code, true); |
| 958 | |
| 959 | Handle<String> name = Factory::LookupAsciiSymbol("builtins"); |
| 960 | builtins_fun->shared()->set_instance_class_name(*name); |
| 961 | |
| 962 | // Allocate the builtins object. |
| 963 | Handle<JSBuiltinsObject> builtins = |
| 964 | Handle<JSBuiltinsObject>::cast(Factory::NewGlobalObject(builtins_fun)); |
| 965 | builtins->set_builtins(*builtins); |
| 966 | builtins->set_global_context(*global_context()); |
| 967 | builtins->set_global_receiver(*builtins); |
| 968 | |
| 969 | // Setup the 'global' properties of the builtins object. The |
| 970 | // 'global' property that refers to the global object is the only |
| 971 | // way to get from code running in the builtins context to the |
| 972 | // global object. |
| 973 | static const PropertyAttributes attributes = |
| 974 | static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE); |
| 975 | SetProperty(builtins, Factory::LookupAsciiSymbol("global"), |
| 976 | Handle<Object>(global_context()->global()), attributes); |
| 977 | |
| 978 | // Setup the reference from the global object to the builtins object. |
| 979 | JSGlobalObject::cast(global_context()->global())->set_builtins(*builtins); |
| 980 | |
| 981 | // Create a bridge function that has context in the global context. |
| 982 | Handle<JSFunction> bridge = |
| 983 | Factory::NewFunction(Factory::empty_symbol(), Factory::undefined_value()); |
| 984 | ASSERT(bridge->context() == *Top::global_context()); |
| 985 | |
| 986 | // Allocate the builtins context. |
| 987 | Handle<Context> context = |
| 988 | Factory::NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge); |
| 989 | context->set_global(*builtins); // override builtins global object |
| 990 | |
| 991 | global_context()->set_runtime_context(*context); |
| 992 | |
| 993 | { // -- S c r i p t |
| 994 | // Builtin functions for Script. |
| 995 | Handle<JSFunction> script_fun = |
| 996 | InstallFunction(builtins, "Script", JS_VALUE_TYPE, JSValue::kSize, |
| 997 | Top::initial_object_prototype(), Builtins::Illegal, |
| 998 | false); |
| 999 | Handle<JSObject> prototype = |
| 1000 | Factory::NewJSObject(Top::object_function(), TENURED); |
| 1001 | SetPrototype(script_fun, prototype); |
| 1002 | global_context()->set_script_function(*script_fun); |
| 1003 | |
| 1004 | // Add 'source' and 'data' property to scripts. |
| 1005 | PropertyAttributes common_attributes = |
| 1006 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 1007 | Handle<Proxy> proxy_source = Factory::NewProxy(&Accessors::ScriptSource); |
| 1008 | Handle<DescriptorArray> script_descriptors = |
| 1009 | Factory::CopyAppendProxyDescriptor( |
| 1010 | Factory::empty_descriptor_array(), |
| 1011 | Factory::LookupAsciiSymbol("source"), |
| 1012 | proxy_source, |
| 1013 | common_attributes); |
| 1014 | Handle<Proxy> proxy_name = Factory::NewProxy(&Accessors::ScriptName); |
| 1015 | script_descriptors = |
| 1016 | Factory::CopyAppendProxyDescriptor( |
| 1017 | script_descriptors, |
| 1018 | Factory::LookupAsciiSymbol("name"), |
| 1019 | proxy_name, |
| 1020 | common_attributes); |
| 1021 | Handle<Proxy> proxy_id = Factory::NewProxy(&Accessors::ScriptId); |
| 1022 | script_descriptors = |
| 1023 | Factory::CopyAppendProxyDescriptor( |
| 1024 | script_descriptors, |
| 1025 | Factory::LookupAsciiSymbol("id"), |
| 1026 | proxy_id, |
| 1027 | common_attributes); |
| 1028 | Handle<Proxy> proxy_line_offset = |
| 1029 | Factory::NewProxy(&Accessors::ScriptLineOffset); |
| 1030 | script_descriptors = |
| 1031 | Factory::CopyAppendProxyDescriptor( |
| 1032 | script_descriptors, |
| 1033 | Factory::LookupAsciiSymbol("line_offset"), |
| 1034 | proxy_line_offset, |
| 1035 | common_attributes); |
| 1036 | Handle<Proxy> proxy_column_offset = |
| 1037 | Factory::NewProxy(&Accessors::ScriptColumnOffset); |
| 1038 | script_descriptors = |
| 1039 | Factory::CopyAppendProxyDescriptor( |
| 1040 | script_descriptors, |
| 1041 | Factory::LookupAsciiSymbol("column_offset"), |
| 1042 | proxy_column_offset, |
| 1043 | common_attributes); |
| 1044 | Handle<Proxy> proxy_data = Factory::NewProxy(&Accessors::ScriptData); |
| 1045 | script_descriptors = |
| 1046 | Factory::CopyAppendProxyDescriptor( |
| 1047 | script_descriptors, |
| 1048 | Factory::LookupAsciiSymbol("data"), |
| 1049 | proxy_data, |
| 1050 | common_attributes); |
| 1051 | Handle<Proxy> proxy_type = Factory::NewProxy(&Accessors::ScriptType); |
| 1052 | script_descriptors = |
| 1053 | Factory::CopyAppendProxyDescriptor( |
| 1054 | script_descriptors, |
| 1055 | Factory::LookupAsciiSymbol("type"), |
| 1056 | proxy_type, |
| 1057 | common_attributes); |
| 1058 | Handle<Proxy> proxy_compilation_type = |
| 1059 | Factory::NewProxy(&Accessors::ScriptCompilationType); |
| 1060 | script_descriptors = |
| 1061 | Factory::CopyAppendProxyDescriptor( |
| 1062 | script_descriptors, |
| 1063 | Factory::LookupAsciiSymbol("compilation_type"), |
| 1064 | proxy_compilation_type, |
| 1065 | common_attributes); |
| 1066 | Handle<Proxy> proxy_line_ends = |
| 1067 | Factory::NewProxy(&Accessors::ScriptLineEnds); |
| 1068 | script_descriptors = |
| 1069 | Factory::CopyAppendProxyDescriptor( |
| 1070 | script_descriptors, |
| 1071 | Factory::LookupAsciiSymbol("line_ends"), |
| 1072 | proxy_line_ends, |
| 1073 | common_attributes); |
| 1074 | Handle<Proxy> proxy_context_data = |
| 1075 | Factory::NewProxy(&Accessors::ScriptContextData); |
| 1076 | script_descriptors = |
| 1077 | Factory::CopyAppendProxyDescriptor( |
| 1078 | script_descriptors, |
| 1079 | Factory::LookupAsciiSymbol("context_data"), |
| 1080 | proxy_context_data, |
| 1081 | common_attributes); |
| 1082 | Handle<Proxy> proxy_eval_from_function = |
| 1083 | Factory::NewProxy(&Accessors::ScriptEvalFromFunction); |
| 1084 | script_descriptors = |
| 1085 | Factory::CopyAppendProxyDescriptor( |
| 1086 | script_descriptors, |
| 1087 | Factory::LookupAsciiSymbol("eval_from_function"), |
| 1088 | proxy_eval_from_function, |
| 1089 | common_attributes); |
| 1090 | Handle<Proxy> proxy_eval_from_position = |
| 1091 | Factory::NewProxy(&Accessors::ScriptEvalFromPosition); |
| 1092 | script_descriptors = |
| 1093 | Factory::CopyAppendProxyDescriptor( |
| 1094 | script_descriptors, |
| 1095 | Factory::LookupAsciiSymbol("eval_from_position"), |
| 1096 | proxy_eval_from_position, |
| 1097 | common_attributes); |
| 1098 | |
| 1099 | Handle<Map> script_map = Handle<Map>(script_fun->initial_map()); |
| 1100 | script_map->set_instance_descriptors(*script_descriptors); |
| 1101 | |
| 1102 | // Allocate the empty script. |
| 1103 | Handle<Script> script = Factory::NewScript(Factory::empty_string()); |
| 1104 | script->set_type(Smi::FromInt(Script::TYPE_NATIVE)); |
| 1105 | global_context()->set_empty_script(*script); |
| 1106 | } |
| 1107 | |
| 1108 | if (FLAG_natives_file == NULL) { |
| 1109 | // Without natives file, install default natives. |
| 1110 | for (int i = Natives::GetDelayCount(); |
| 1111 | i < Natives::GetBuiltinsCount(); |
| 1112 | i++) { |
| 1113 | if (!CompileBuiltin(i)) return false; |
| 1114 | } |
| 1115 | |
| 1116 | // Setup natives with lazy loading. |
| 1117 | SetupLazy(Handle<JSFunction>(global_context()->date_function()), |
| 1118 | Natives::GetIndex("date"), |
| 1119 | Top::global_context(), |
| 1120 | Handle<Context>(Top::context()->runtime_context())); |
| 1121 | SetupLazy(Handle<JSFunction>(global_context()->regexp_function()), |
| 1122 | Natives::GetIndex("regexp"), |
| 1123 | Top::global_context(), |
| 1124 | Handle<Context>(Top::context()->runtime_context())); |
| 1125 | SetupLazy(Handle<JSObject>(global_context()->json_object()), |
| 1126 | Natives::GetIndex("json"), |
| 1127 | Top::global_context(), |
| 1128 | Handle<Context>(Top::context()->runtime_context())); |
| 1129 | |
| 1130 | } else if (strlen(FLAG_natives_file) != 0) { |
| 1131 | // Otherwise install natives from natives file if file exists and |
| 1132 | // compiles. |
| 1133 | bool exists; |
| 1134 | Vector<const char> source = ReadFile(FLAG_natives_file, &exists); |
| 1135 | Handle<String> source_string = Factory::NewStringFromAscii(source); |
| 1136 | if (source.is_empty()) return false; |
| 1137 | bool result = CompileNative(CStrVector(FLAG_natives_file), source_string); |
| 1138 | if (!result) return false; |
| 1139 | |
| 1140 | } else { |
| 1141 | // Empty natives file name - do not install any natives. |
| 1142 | PrintF("Warning: Running without installed natives!\n"); |
| 1143 | return true; |
| 1144 | } |
| 1145 | |
| 1146 | InstallNativeFunctions(); |
| 1147 | |
| 1148 | // Install Function.prototype.call and apply. |
| 1149 | { Handle<String> key = Factory::function_class_symbol(); |
| 1150 | Handle<JSFunction> function = |
| 1151 | Handle<JSFunction>::cast(GetProperty(Top::global(), key)); |
| 1152 | Handle<JSObject> proto = |
| 1153 | Handle<JSObject>(JSObject::cast(function->instance_prototype())); |
| 1154 | |
| 1155 | // Install the call and the apply functions. |
| 1156 | Handle<JSFunction> call = |
| 1157 | InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize, |
| 1158 | Factory::NewJSObject(Top::object_function(), TENURED), |
| 1159 | Builtins::FunctionCall, |
| 1160 | false); |
| 1161 | Handle<JSFunction> apply = |
| 1162 | InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize, |
| 1163 | Factory::NewJSObject(Top::object_function(), TENURED), |
| 1164 | Builtins::FunctionApply, |
| 1165 | false); |
| 1166 | |
| 1167 | // Make sure that Function.prototype.call appears to be compiled. |
| 1168 | // The code will never be called, but inline caching for call will |
| 1169 | // only work if it appears to be compiled. |
| 1170 | call->shared()->DontAdaptArguments(); |
| 1171 | ASSERT(call->is_compiled()); |
| 1172 | |
| 1173 | // Set the expected parameters for apply to 2; required by builtin. |
| 1174 | apply->shared()->set_formal_parameter_count(2); |
| 1175 | |
| 1176 | // Set the lengths for the functions to satisfy ECMA-262. |
| 1177 | call->shared()->set_length(1); |
| 1178 | apply->shared()->set_length(2); |
| 1179 | } |
| 1180 | |
| 1181 | #ifdef DEBUG |
| 1182 | builtins->Verify(); |
| 1183 | #endif |
| 1184 | return true; |
| 1185 | } |
| 1186 | |
| 1187 | |
| 1188 | bool Genesis::InstallSpecialObjects() { |
| 1189 | HandleScope scope; |
| 1190 | Handle<JSGlobalObject> js_global( |
| 1191 | JSGlobalObject::cast(global_context()->global())); |
| 1192 | // Expose the natives in global if a name for it is specified. |
| 1193 | if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) { |
| 1194 | Handle<String> natives_string = |
| 1195 | Factory::LookupAsciiSymbol(FLAG_expose_natives_as); |
| 1196 | SetProperty(js_global, natives_string, |
| 1197 | Handle<JSObject>(js_global->builtins()), DONT_ENUM); |
| 1198 | } |
| 1199 | |
| 1200 | Handle<Object> Error = GetProperty(js_global, "Error"); |
| 1201 | if (Error->IsJSObject()) { |
| 1202 | Handle<String> name = Factory::LookupAsciiSymbol("stackTraceLimit"); |
| 1203 | SetProperty(Handle<JSObject>::cast(Error), |
| 1204 | name, |
| 1205 | Handle<Smi>(Smi::FromInt(FLAG_stack_trace_limit)), |
| 1206 | NONE); |
| 1207 | } |
| 1208 | |
| 1209 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 1210 | // Expose the debug global object in global if a name for it is specified. |
| 1211 | if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) { |
| 1212 | // If loading fails we just bail out without installing the |
| 1213 | // debugger but without tanking the whole context. |
| 1214 | if (!Debug::Load()) |
| 1215 | return true; |
| 1216 | // Set the security token for the debugger context to the same as |
| 1217 | // the shell global context to allow calling between these (otherwise |
| 1218 | // exposing debug global object doesn't make much sense). |
| 1219 | Debug::debug_context()->set_security_token( |
| 1220 | global_context()->security_token()); |
| 1221 | |
| 1222 | Handle<String> debug_string = |
| 1223 | Factory::LookupAsciiSymbol(FLAG_expose_debug_as); |
| 1224 | SetProperty(js_global, debug_string, |
| 1225 | Handle<Object>(Debug::debug_context()->global_proxy()), DONT_ENUM); |
| 1226 | } |
| 1227 | #endif |
| 1228 | |
| 1229 | return true; |
| 1230 | } |
| 1231 | |
| 1232 | |
| 1233 | bool Genesis::InstallExtensions(v8::ExtensionConfiguration* extensions) { |
| 1234 | // Clear coloring of extension list |
| 1235 | v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension(); |
| 1236 | while (current != NULL) { |
| 1237 | current->set_state(v8::UNVISITED); |
| 1238 | current = current->next(); |
| 1239 | } |
| 1240 | // Install auto extensions |
| 1241 | current = v8::RegisteredExtension::first_extension(); |
| 1242 | while (current != NULL) { |
| 1243 | if (current->extension()->auto_enable()) |
| 1244 | InstallExtension(current); |
| 1245 | current = current->next(); |
| 1246 | } |
| 1247 | |
| 1248 | if (FLAG_expose_gc) InstallExtension("v8/gc"); |
| 1249 | |
| 1250 | if (extensions == NULL) return true; |
| 1251 | // Install required extensions |
| 1252 | int count = v8::ImplementationUtilities::GetNameCount(extensions); |
| 1253 | const char** names = v8::ImplementationUtilities::GetNames(extensions); |
| 1254 | for (int i = 0; i < count; i++) { |
| 1255 | if (!InstallExtension(names[i])) |
| 1256 | return false; |
| 1257 | } |
| 1258 | |
| 1259 | return true; |
| 1260 | } |
| 1261 | |
| 1262 | |
| 1263 | // Installs a named extension. This methods is unoptimized and does |
| 1264 | // not scale well if we want to support a large number of extensions. |
| 1265 | bool Genesis::InstallExtension(const char* name) { |
| 1266 | v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension(); |
| 1267 | // Loop until we find the relevant extension |
| 1268 | while (current != NULL) { |
| 1269 | if (strcmp(name, current->extension()->name()) == 0) break; |
| 1270 | current = current->next(); |
| 1271 | } |
| 1272 | // Didn't find the extension; fail. |
| 1273 | if (current == NULL) { |
| 1274 | v8::Utils::ReportApiFailure( |
| 1275 | "v8::Context::New()", "Cannot find required extension"); |
| 1276 | return false; |
| 1277 | } |
| 1278 | return InstallExtension(current); |
| 1279 | } |
| 1280 | |
| 1281 | |
| 1282 | bool Genesis::InstallExtension(v8::RegisteredExtension* current) { |
| 1283 | HandleScope scope; |
| 1284 | |
| 1285 | if (current->state() == v8::INSTALLED) return true; |
| 1286 | // The current node has already been visited so there must be a |
| 1287 | // cycle in the dependency graph; fail. |
| 1288 | if (current->state() == v8::VISITED) { |
| 1289 | v8::Utils::ReportApiFailure( |
| 1290 | "v8::Context::New()", "Circular extension dependency"); |
| 1291 | return false; |
| 1292 | } |
| 1293 | ASSERT(current->state() == v8::UNVISITED); |
| 1294 | current->set_state(v8::VISITED); |
| 1295 | v8::Extension* extension = current->extension(); |
| 1296 | // Install the extension's dependencies |
| 1297 | for (int i = 0; i < extension->dependency_count(); i++) { |
| 1298 | if (!InstallExtension(extension->dependencies()[i])) return false; |
| 1299 | } |
| 1300 | Vector<const char> source = CStrVector(extension->source()); |
| 1301 | Handle<String> source_code = Factory::NewStringFromAscii(source); |
| 1302 | bool result = CompileScriptCached(CStrVector(extension->name()), |
| 1303 | source_code, |
| 1304 | &extensions_cache, extension, |
| 1305 | false); |
| 1306 | ASSERT(Top::has_pending_exception() != result); |
| 1307 | if (!result) { |
| 1308 | Top::clear_pending_exception(); |
| 1309 | v8::Utils::ReportApiFailure( |
| 1310 | "v8::Context::New()", "Error installing extension"); |
| 1311 | } |
| 1312 | current->set_state(v8::INSTALLED); |
| 1313 | return result; |
| 1314 | } |
| 1315 | |
| 1316 | |
| 1317 | bool Genesis::ConfigureGlobalObjects( |
| 1318 | v8::Handle<v8::ObjectTemplate> global_proxy_template) { |
| 1319 | Handle<JSObject> global_proxy( |
| 1320 | JSObject::cast(global_context()->global_proxy())); |
| 1321 | Handle<JSObject> js_global(JSObject::cast(global_context()->global())); |
| 1322 | |
| 1323 | if (!global_proxy_template.IsEmpty()) { |
| 1324 | // Configure the global proxy object. |
| 1325 | Handle<ObjectTemplateInfo> proxy_data = |
| 1326 | v8::Utils::OpenHandle(*global_proxy_template); |
| 1327 | if (!ConfigureApiObject(global_proxy, proxy_data)) return false; |
| 1328 | |
| 1329 | // Configure the inner global object. |
| 1330 | Handle<FunctionTemplateInfo> proxy_constructor( |
| 1331 | FunctionTemplateInfo::cast(proxy_data->constructor())); |
| 1332 | if (!proxy_constructor->prototype_template()->IsUndefined()) { |
| 1333 | Handle<ObjectTemplateInfo> inner_data( |
| 1334 | ObjectTemplateInfo::cast(proxy_constructor->prototype_template())); |
| 1335 | if (!ConfigureApiObject(js_global, inner_data)) return false; |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | SetObjectPrototype(global_proxy, js_global); |
| 1340 | return true; |
| 1341 | } |
| 1342 | |
| 1343 | |
| 1344 | bool Genesis::ConfigureApiObject(Handle<JSObject> object, |
| 1345 | Handle<ObjectTemplateInfo> object_template) { |
| 1346 | ASSERT(!object_template.is_null()); |
| 1347 | ASSERT(object->IsInstanceOf( |
| 1348 | FunctionTemplateInfo::cast(object_template->constructor()))); |
| 1349 | |
| 1350 | bool pending_exception = false; |
| 1351 | Handle<JSObject> obj = |
| 1352 | Execution::InstantiateObject(object_template, &pending_exception); |
| 1353 | if (pending_exception) { |
| 1354 | ASSERT(Top::has_pending_exception()); |
| 1355 | Top::clear_pending_exception(); |
| 1356 | return false; |
| 1357 | } |
| 1358 | TransferObject(obj, object); |
| 1359 | return true; |
| 1360 | } |
| 1361 | |
| 1362 | |
| 1363 | void Genesis::TransferNamedProperties(Handle<JSObject> from, |
| 1364 | Handle<JSObject> to) { |
| 1365 | if (from->HasFastProperties()) { |
| 1366 | Handle<DescriptorArray> descs = |
| 1367 | Handle<DescriptorArray>(from->map()->instance_descriptors()); |
| 1368 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 1369 | PropertyDetails details = PropertyDetails(descs->GetDetails(i)); |
| 1370 | switch (details.type()) { |
| 1371 | case FIELD: { |
| 1372 | HandleScope inner; |
| 1373 | Handle<String> key = Handle<String>(descs->GetKey(i)); |
| 1374 | int index = descs->GetFieldIndex(i); |
| 1375 | Handle<Object> value = Handle<Object>(from->FastPropertyAt(index)); |
| 1376 | SetProperty(to, key, value, details.attributes()); |
| 1377 | break; |
| 1378 | } |
| 1379 | case CONSTANT_FUNCTION: { |
| 1380 | HandleScope inner; |
| 1381 | Handle<String> key = Handle<String>(descs->GetKey(i)); |
| 1382 | Handle<JSFunction> fun = |
| 1383 | Handle<JSFunction>(descs->GetConstantFunction(i)); |
| 1384 | SetProperty(to, key, fun, details.attributes()); |
| 1385 | break; |
| 1386 | } |
| 1387 | case CALLBACKS: { |
| 1388 | LookupResult result; |
| 1389 | to->LocalLookup(descs->GetKey(i), &result); |
| 1390 | // If the property is already there we skip it |
| 1391 | if (result.IsValid()) continue; |
| 1392 | HandleScope inner; |
| 1393 | Handle<DescriptorArray> inst_descs = |
| 1394 | Handle<DescriptorArray>(to->map()->instance_descriptors()); |
| 1395 | Handle<String> key = Handle<String>(descs->GetKey(i)); |
| 1396 | Handle<Object> entry = Handle<Object>(descs->GetCallbacksObject(i)); |
| 1397 | inst_descs = Factory::CopyAppendProxyDescriptor(inst_descs, |
| 1398 | key, |
| 1399 | entry, |
| 1400 | details.attributes()); |
| 1401 | to->map()->set_instance_descriptors(*inst_descs); |
| 1402 | break; |
| 1403 | } |
| 1404 | case MAP_TRANSITION: |
| 1405 | case CONSTANT_TRANSITION: |
| 1406 | case NULL_DESCRIPTOR: |
| 1407 | // Ignore non-properties. |
| 1408 | break; |
| 1409 | case NORMAL: |
| 1410 | // Do not occur since the from object has fast properties. |
| 1411 | case INTERCEPTOR: |
| 1412 | // No element in instance descriptors have interceptor type. |
| 1413 | UNREACHABLE(); |
| 1414 | break; |
| 1415 | } |
| 1416 | } |
| 1417 | } else { |
| 1418 | Handle<StringDictionary> properties = |
| 1419 | Handle<StringDictionary>(from->property_dictionary()); |
| 1420 | int capacity = properties->Capacity(); |
| 1421 | for (int i = 0; i < capacity; i++) { |
| 1422 | Object* raw_key(properties->KeyAt(i)); |
| 1423 | if (properties->IsKey(raw_key)) { |
| 1424 | ASSERT(raw_key->IsString()); |
| 1425 | // If the property is already there we skip it. |
| 1426 | LookupResult result; |
| 1427 | to->LocalLookup(String::cast(raw_key), &result); |
| 1428 | if (result.IsValid()) continue; |
| 1429 | // Set the property. |
| 1430 | Handle<String> key = Handle<String>(String::cast(raw_key)); |
| 1431 | Handle<Object> value = Handle<Object>(properties->ValueAt(i)); |
| 1432 | if (value->IsJSGlobalPropertyCell()) { |
| 1433 | value = Handle<Object>(JSGlobalPropertyCell::cast(*value)->value()); |
| 1434 | } |
| 1435 | PropertyDetails details = properties->DetailsAt(i); |
| 1436 | SetProperty(to, key, value, details.attributes()); |
| 1437 | } |
| 1438 | } |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | |
| 1443 | void Genesis::TransferIndexedProperties(Handle<JSObject> from, |
| 1444 | Handle<JSObject> to) { |
| 1445 | // Cloning the elements array is sufficient. |
| 1446 | Handle<FixedArray> from_elements = |
| 1447 | Handle<FixedArray>(FixedArray::cast(from->elements())); |
| 1448 | Handle<FixedArray> to_elements = Factory::CopyFixedArray(from_elements); |
| 1449 | to->set_elements(*to_elements); |
| 1450 | } |
| 1451 | |
| 1452 | |
| 1453 | void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) { |
| 1454 | HandleScope outer; |
| 1455 | |
| 1456 | ASSERT(!from->IsJSArray()); |
| 1457 | ASSERT(!to->IsJSArray()); |
| 1458 | |
| 1459 | TransferNamedProperties(from, to); |
| 1460 | TransferIndexedProperties(from, to); |
| 1461 | |
| 1462 | // Transfer the prototype (new map is needed). |
| 1463 | Handle<Map> old_to_map = Handle<Map>(to->map()); |
| 1464 | Handle<Map> new_to_map = Factory::CopyMapDropTransitions(old_to_map); |
| 1465 | new_to_map->set_prototype(from->map()->prototype()); |
| 1466 | to->set_map(*new_to_map); |
| 1467 | } |
| 1468 | |
| 1469 | |
| 1470 | void Genesis::MakeFunctionInstancePrototypeWritable() { |
| 1471 | // Make a new function map so all future functions |
| 1472 | // will have settable and enumerable prototype properties. |
| 1473 | HandleScope scope; |
| 1474 | |
| 1475 | Handle<DescriptorArray> function_map_descriptors = |
| 1476 | ComputeFunctionInstanceDescriptor(false); |
| 1477 | Handle<Map> fm = Factory::CopyMapDropDescriptors(Top::function_map()); |
| 1478 | fm->set_instance_descriptors(*function_map_descriptors); |
| 1479 | Top::context()->global_context()->set_function_map(*fm); |
| 1480 | } |
| 1481 | |
| 1482 | |
| 1483 | void Genesis::AddSpecialFunction(Handle<JSObject> prototype, |
| 1484 | const char* name, |
| 1485 | Handle<Code> code) { |
| 1486 | Handle<String> key = Factory::LookupAsciiSymbol(name); |
| 1487 | Handle<Object> value = Handle<Object>(prototype->GetProperty(*key)); |
| 1488 | if (value->IsJSFunction()) { |
| 1489 | Handle<JSFunction> optimized = Factory::NewFunction(key, |
| 1490 | JS_OBJECT_TYPE, |
| 1491 | JSObject::kHeaderSize, |
| 1492 | code, |
| 1493 | false); |
| 1494 | optimized->shared()->DontAdaptArguments(); |
| 1495 | int len = global_context()->special_function_table()->length(); |
| 1496 | Handle<FixedArray> new_array = Factory::NewFixedArray(len + 3); |
| 1497 | for (int index = 0; index < len; index++) { |
| 1498 | new_array->set(index, |
| 1499 | global_context()->special_function_table()->get(index)); |
| 1500 | } |
| 1501 | new_array->set(len+0, *prototype); |
| 1502 | new_array->set(len+1, *value); |
| 1503 | new_array->set(len+2, *optimized); |
| 1504 | global_context()->set_special_function_table(*new_array); |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | |
| 1509 | void Genesis::BuildSpecialFunctionTable() { |
| 1510 | HandleScope scope; |
| 1511 | Handle<JSObject> global = Handle<JSObject>(global_context()->global()); |
| 1512 | // Add special versions for Array.prototype.pop and push. |
| 1513 | Handle<JSFunction> function = |
| 1514 | Handle<JSFunction>( |
| 1515 | JSFunction::cast(global->GetProperty(Heap::Array_symbol()))); |
| 1516 | Handle<JSObject> visible_prototype = |
| 1517 | Handle<JSObject>(JSObject::cast(function->prototype())); |
| 1518 | // Remember to put push and pop on the hidden prototype if it's there. |
| 1519 | Handle<JSObject> push_and_pop_prototype; |
| 1520 | Handle<Object> superproto(visible_prototype->GetPrototype()); |
| 1521 | if (superproto->IsJSObject() && |
| 1522 | JSObject::cast(*superproto)->map()->is_hidden_prototype()) { |
| 1523 | push_and_pop_prototype = Handle<JSObject>::cast(superproto); |
| 1524 | } else { |
| 1525 | push_and_pop_prototype = visible_prototype; |
| 1526 | } |
| 1527 | AddSpecialFunction(push_and_pop_prototype, "pop", |
| 1528 | Handle<Code>(Builtins::builtin(Builtins::ArrayPop))); |
| 1529 | AddSpecialFunction(push_and_pop_prototype, "push", |
| 1530 | Handle<Code>(Builtins::builtin(Builtins::ArrayPush))); |
| 1531 | } |
| 1532 | |
| 1533 | |
| 1534 | Genesis::Genesis(Handle<Object> global_object, |
| 1535 | v8::Handle<v8::ObjectTemplate> global_template, |
| 1536 | v8::ExtensionConfiguration* extensions) { |
| 1537 | // Link this genesis object into the stacked genesis chain. This |
| 1538 | // must be done before any early exits because the destructor |
| 1539 | // will always do unlinking. |
| 1540 | previous_ = current_; |
| 1541 | current_ = this; |
| 1542 | result_ = Handle<Context>::null(); |
| 1543 | |
| 1544 | // If V8 isn't running and cannot be initialized, just return. |
| 1545 | if (!V8::IsRunning() && !V8::Initialize(NULL)) return; |
| 1546 | |
| 1547 | // Before creating the roots we must save the context and restore it |
| 1548 | // on all function exits. |
| 1549 | HandleScope scope; |
| 1550 | SaveContext context; |
| 1551 | |
| 1552 | CreateRoots(global_template, global_object); |
| 1553 | |
| 1554 | if (!InstallNatives()) return; |
| 1555 | |
| 1556 | MakeFunctionInstancePrototypeWritable(); |
| 1557 | BuildSpecialFunctionTable(); |
| 1558 | |
| 1559 | if (!ConfigureGlobalObjects(global_template)) return; |
| 1560 | |
| 1561 | if (!InstallExtensions(extensions)) return; |
| 1562 | |
| 1563 | if (!InstallSpecialObjects()) return; |
| 1564 | |
| 1565 | result_ = global_context_; |
| 1566 | } |
| 1567 | |
| 1568 | |
| 1569 | // Support for thread preemption. |
| 1570 | |
| 1571 | // Reserve space for statics needing saving and restoring. |
| 1572 | int Bootstrapper::ArchiveSpacePerThread() { |
| 1573 | return Genesis::ArchiveSpacePerThread(); |
| 1574 | } |
| 1575 | |
| 1576 | |
| 1577 | // Archive statics that are thread local. |
| 1578 | char* Bootstrapper::ArchiveState(char* to) { |
| 1579 | return Genesis::ArchiveState(to); |
| 1580 | } |
| 1581 | |
| 1582 | |
| 1583 | // Restore statics that are thread local. |
| 1584 | char* Bootstrapper::RestoreState(char* from) { |
| 1585 | return Genesis::RestoreState(from); |
| 1586 | } |
| 1587 | |
| 1588 | |
| 1589 | // Called when the top-level V8 mutex is destroyed. |
| 1590 | void Bootstrapper::FreeThreadResources() { |
| 1591 | ASSERT(Genesis::current() == NULL); |
| 1592 | } |
| 1593 | |
| 1594 | |
| 1595 | // Reserve space for statics needing saving and restoring. |
| 1596 | int Genesis::ArchiveSpacePerThread() { |
| 1597 | return sizeof(current_); |
| 1598 | } |
| 1599 | |
| 1600 | |
| 1601 | // Archive statics that are thread local. |
| 1602 | char* Genesis::ArchiveState(char* to) { |
| 1603 | *reinterpret_cast<Genesis**>(to) = current_; |
| 1604 | current_ = NULL; |
| 1605 | return to + sizeof(current_); |
| 1606 | } |
| 1607 | |
| 1608 | |
| 1609 | // Restore statics that are thread local. |
| 1610 | char* Genesis::RestoreState(char* from) { |
| 1611 | current_ = *reinterpret_cast<Genesis**>(from); |
| 1612 | return from + sizeof(current_); |
| 1613 | } |
| 1614 | |
| 1615 | } } // namespace v8::internal |