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