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" |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 39 | #include "snapshot.h" |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 40 | #include "stub-cache.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 42 | namespace v8 { |
| 43 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 45 | // A SourceCodeCache uses a FixedArray to store pairs of |
| 46 | // (AsciiString*, JSFunction*), mapping names of native code files |
| 47 | // (runtime.js, etc.) to precompiled functions. Instead of mapping |
| 48 | // names to functions it might make sense to let the JS2C tool |
| 49 | // generate an index for each native JS file. |
| 50 | class SourceCodeCache BASE_EMBEDDED { |
| 51 | public: |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 52 | explicit SourceCodeCache(Script::Type type): type_(type), cache_(NULL) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 53 | |
| 54 | void Initialize(bool create_heap_objects) { |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 55 | cache_ = create_heap_objects ? Heap::empty_fixed_array() : NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void Iterate(ObjectVisitor* v) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 59 | v->VisitPointer(BitCast<Object**, FixedArray**>(&cache_)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 63 | bool Lookup(Vector<const char> name, Handle<SharedFunctionInfo>* handle) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 64 | for (int i = 0; i < cache_->length(); i+=2) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 65 | SeqAsciiString* str = SeqAsciiString::cast(cache_->get(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 66 | if (str->IsEqualTo(name)) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 67 | *handle = Handle<SharedFunctionInfo>( |
| 68 | SharedFunctionInfo::cast(cache_->get(i + 1))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 69 | return true; |
| 70 | } |
| 71 | } |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 76 | void Add(Vector<const char> name, Handle<SharedFunctionInfo> shared) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 77 | HandleScope scope; |
| 78 | int length = cache_->length(); |
| 79 | Handle<FixedArray> new_array = |
| 80 | Factory::NewFixedArray(length + 2, TENURED); |
| 81 | cache_->CopyTo(0, *new_array, 0, cache_->length()); |
| 82 | cache_ = *new_array; |
| 83 | Handle<String> str = Factory::NewStringFromAscii(name, TENURED); |
| 84 | cache_->set(length, *str); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 85 | cache_->set(length + 1, *shared); |
| 86 | Script::cast(shared->script())->set_type(Smi::FromInt(type_)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | private: |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 90 | Script::Type type_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 91 | FixedArray* cache_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 92 | DISALLOW_COPY_AND_ASSIGN(SourceCodeCache); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 95 | static SourceCodeCache extensions_cache(Script::TYPE_EXTENSION); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 96 | // This is for delete, not delete[]. |
| 97 | static List<char*>* delete_these_non_arrays_on_tear_down = NULL; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 98 | // This is for delete[] |
| 99 | static List<char*>* delete_these_arrays_on_tear_down = NULL; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 100 | |
| 101 | |
| 102 | NativesExternalStringResource::NativesExternalStringResource(const char* source) |
| 103 | : data_(source), length_(StrLength(source)) { |
| 104 | if (delete_these_non_arrays_on_tear_down == NULL) { |
| 105 | delete_these_non_arrays_on_tear_down = new List<char*>(2); |
| 106 | } |
| 107 | // The resources are small objects and we only make a fixed number of |
| 108 | // them, but let's clean them up on exit for neatness. |
| 109 | delete_these_non_arrays_on_tear_down-> |
| 110 | Add(reinterpret_cast<char*>(this)); |
| 111 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 112 | |
| 113 | |
| 114 | Handle<String> Bootstrapper::NativesSourceLookup(int index) { |
| 115 | ASSERT(0 <= index && index < Natives::GetBuiltinsCount()); |
| 116 | if (Heap::natives_source_cache()->get(index)->IsUndefined()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 117 | if (!Snapshot::IsEnabled() || FLAG_new_snapshot) { |
| 118 | // We can use external strings for the natives. |
| 119 | NativesExternalStringResource* resource = |
| 120 | new NativesExternalStringResource( |
| 121 | Natives::GetScriptSource(index).start()); |
| 122 | Handle<String> source_code = |
| 123 | Factory::NewExternalStringFromAscii(resource); |
| 124 | Heap::natives_source_cache()->set(index, *source_code); |
| 125 | } else { |
| 126 | // Old snapshot code can't cope with external strings at all. |
| 127 | Handle<String> source_code = |
| 128 | Factory::NewStringFromAscii(Natives::GetScriptSource(index)); |
| 129 | Heap::natives_source_cache()->set(index, *source_code); |
| 130 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 131 | } |
| 132 | Handle<Object> cached_source(Heap::natives_source_cache()->get(index)); |
| 133 | return Handle<String>::cast(cached_source); |
| 134 | } |
| 135 | |
| 136 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 137 | void Bootstrapper::Initialize(bool create_heap_objects) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 138 | extensions_cache.Initialize(create_heap_objects); |
| 139 | } |
| 140 | |
| 141 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 142 | char* Bootstrapper::AllocateAutoDeletedArray(int bytes) { |
| 143 | char* memory = new char[bytes]; |
| 144 | if (memory != NULL) { |
| 145 | if (delete_these_arrays_on_tear_down == NULL) { |
| 146 | delete_these_arrays_on_tear_down = new List<char*>(2); |
| 147 | } |
| 148 | delete_these_arrays_on_tear_down->Add(memory); |
| 149 | } |
| 150 | return memory; |
| 151 | } |
| 152 | |
| 153 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 154 | void Bootstrapper::TearDown() { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 155 | if (delete_these_non_arrays_on_tear_down != NULL) { |
| 156 | int len = delete_these_non_arrays_on_tear_down->length(); |
| 157 | ASSERT(len < 20); // Don't use this mechanism for unbounded allocations. |
| 158 | for (int i = 0; i < len; i++) { |
| 159 | delete delete_these_non_arrays_on_tear_down->at(i); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 160 | delete_these_non_arrays_on_tear_down->at(i) = NULL; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 161 | } |
| 162 | delete delete_these_non_arrays_on_tear_down; |
| 163 | delete_these_non_arrays_on_tear_down = NULL; |
| 164 | } |
| 165 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 166 | if (delete_these_arrays_on_tear_down != NULL) { |
| 167 | int len = delete_these_arrays_on_tear_down->length(); |
| 168 | ASSERT(len < 1000); // Don't use this mechanism for unbounded allocations. |
| 169 | for (int i = 0; i < len; i++) { |
| 170 | delete[] delete_these_arrays_on_tear_down->at(i); |
| 171 | delete_these_arrays_on_tear_down->at(i) = NULL; |
| 172 | } |
| 173 | delete delete_these_arrays_on_tear_down; |
| 174 | delete_these_arrays_on_tear_down = NULL; |
| 175 | } |
| 176 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 177 | extensions_cache.Initialize(false); // Yes, symmetrical |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 181 | class Genesis BASE_EMBEDDED { |
| 182 | public: |
| 183 | Genesis(Handle<Object> global_object, |
| 184 | v8::Handle<v8::ObjectTemplate> global_template, |
| 185 | v8::ExtensionConfiguration* extensions); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 186 | ~Genesis() { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 187 | |
| 188 | Handle<Context> result() { return result_; } |
| 189 | |
| 190 | Genesis* previous() { return previous_; } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 191 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 192 | private: |
| 193 | Handle<Context> global_context_; |
| 194 | |
| 195 | // There may be more than one active genesis object: When GC is |
| 196 | // triggered during environment creation there may be weak handle |
| 197 | // processing callbacks which may create new environments. |
| 198 | Genesis* previous_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 199 | |
| 200 | Handle<Context> global_context() { return global_context_; } |
| 201 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 202 | // Creates some basic objects. Used for creating a context from scratch. |
| 203 | void CreateRoots(); |
| 204 | // Creates the empty function. Used for creating a context from scratch. |
| 205 | Handle<JSFunction> CreateEmptyFunction(); |
| 206 | // Creates the global objects using the global and the template passed in |
| 207 | // through the API. We call this regardless of whether we are building a |
| 208 | // context from scratch or using a deserialized one from the partial snapshot |
| 209 | // but in the latter case we don't use the objects it produces directly, as |
| 210 | // we have to used the deserialized ones that are linked together with the |
| 211 | // rest of the context snapshot. |
| 212 | Handle<JSGlobalProxy> CreateNewGlobals( |
| 213 | v8::Handle<v8::ObjectTemplate> global_template, |
| 214 | Handle<Object> global_object, |
| 215 | Handle<GlobalObject>* global_proxy_out); |
| 216 | // Hooks the given global proxy into the context. If the context was created |
| 217 | // by deserialization then this will unhook the global proxy that was |
| 218 | // deserialized, leaving the GC to pick it up. |
| 219 | void HookUpGlobalProxy(Handle<GlobalObject> inner_global, |
| 220 | Handle<JSGlobalProxy> global_proxy); |
| 221 | // Similarly, we want to use the inner global that has been created by the |
| 222 | // templates passed through the API. The inner global from the snapshot is |
| 223 | // detached from the other objects in the snapshot. |
| 224 | void HookUpInnerGlobal(Handle<GlobalObject> inner_global); |
| 225 | // New context initialization. Used for creating a context from scratch. |
| 226 | void InitializeGlobal(Handle<GlobalObject> inner_global, |
| 227 | Handle<JSFunction> empty_function); |
| 228 | // Installs the contents of the native .js files on the global objects. |
| 229 | // Used for creating a context from scratch. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 230 | void InstallNativeFunctions(); |
| 231 | bool InstallNatives(); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 232 | void InstallCustomCallGenerators(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 233 | void InstallJSFunctionResultCaches(); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 234 | // Used both for deserialized and from-scratch contexts to add the extensions |
| 235 | // provided. |
| 236 | static bool InstallExtensions(Handle<Context> global_context, |
| 237 | v8::ExtensionConfiguration* extensions); |
| 238 | static bool InstallExtension(const char* name); |
| 239 | static bool InstallExtension(v8::RegisteredExtension* current); |
| 240 | static void InstallSpecialObjects(Handle<Context> global_context); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 241 | bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 242 | bool ConfigureApiObject(Handle<JSObject> object, |
| 243 | Handle<ObjectTemplateInfo> object_template); |
| 244 | bool ConfigureGlobalObjects(v8::Handle<v8::ObjectTemplate> global_template); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 245 | |
| 246 | // Migrates all properties from the 'from' object to the 'to' |
| 247 | // object and overrides the prototype in 'to' with the one from |
| 248 | // 'from'. |
| 249 | void TransferObject(Handle<JSObject> from, Handle<JSObject> to); |
| 250 | void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to); |
| 251 | void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to); |
| 252 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 253 | enum PrototypePropertyMode { |
| 254 | DONT_ADD_PROTOTYPE, |
| 255 | ADD_READONLY_PROTOTYPE, |
| 256 | ADD_WRITEABLE_PROTOTYPE |
| 257 | }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 258 | Handle<DescriptorArray> ComputeFunctionInstanceDescriptor( |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 259 | PrototypePropertyMode prototypeMode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 260 | void MakeFunctionInstancePrototypeWritable(); |
| 261 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 262 | static bool CompileBuiltin(int index); |
| 263 | static bool CompileNative(Vector<const char> name, Handle<String> source); |
| 264 | static bool CompileScriptCached(Vector<const char> name, |
| 265 | Handle<String> source, |
| 266 | SourceCodeCache* cache, |
| 267 | v8::Extension* extension, |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 268 | Handle<Context> top_context, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 269 | bool use_runtime_context); |
| 270 | |
| 271 | Handle<Context> result_; |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 272 | Handle<JSFunction> empty_function_; |
| 273 | BootstrapperActive active_; |
| 274 | friend class Bootstrapper; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 275 | }; |
| 276 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 277 | |
| 278 | void Bootstrapper::Iterate(ObjectVisitor* v) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 279 | extensions_cache.Iterate(v); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 280 | v->Synchronize("Extensions"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 284 | Handle<Context> Bootstrapper::CreateEnvironment( |
| 285 | Handle<Object> global_object, |
| 286 | v8::Handle<v8::ObjectTemplate> global_template, |
| 287 | v8::ExtensionConfiguration* extensions) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 288 | HandleScope scope; |
| 289 | Handle<Context> env; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 290 | Genesis genesis(global_object, global_template, extensions); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 291 | env = genesis.result(); |
| 292 | if (!env.is_null()) { |
| 293 | if (InstallExtensions(env, extensions)) { |
| 294 | return env; |
| 295 | } |
| 296 | } |
| 297 | return Handle<Context>(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 301 | static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) { |
| 302 | // object.__proto__ = proto; |
| 303 | Handle<Map> old_to_map = Handle<Map>(object->map()); |
| 304 | Handle<Map> new_to_map = Factory::CopyMapDropTransitions(old_to_map); |
| 305 | new_to_map->set_prototype(*proto); |
| 306 | object->set_map(*new_to_map); |
| 307 | } |
| 308 | |
| 309 | |
| 310 | void Bootstrapper::DetachGlobal(Handle<Context> env) { |
| 311 | JSGlobalProxy::cast(env->global_proxy())->set_context(*Factory::null_value()); |
| 312 | SetObjectPrototype(Handle<JSObject>(env->global_proxy()), |
| 313 | Factory::null_value()); |
| 314 | env->set_global_proxy(env->global()); |
| 315 | env->global()->set_global_receiver(env->global()); |
| 316 | } |
| 317 | |
| 318 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 319 | void Bootstrapper::ReattachGlobal(Handle<Context> env, |
| 320 | Handle<Object> global_object) { |
| 321 | ASSERT(global_object->IsJSGlobalProxy()); |
| 322 | Handle<JSGlobalProxy> global = Handle<JSGlobalProxy>::cast(global_object); |
| 323 | env->global()->set_global_receiver(*global); |
| 324 | env->set_global_proxy(*global); |
| 325 | SetObjectPrototype(global, Handle<JSObject>(env->global())); |
| 326 | global->set_context(*env); |
| 327 | } |
| 328 | |
| 329 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 330 | static Handle<JSFunction> InstallFunction(Handle<JSObject> target, |
| 331 | const char* name, |
| 332 | InstanceType type, |
| 333 | int instance_size, |
| 334 | Handle<JSObject> prototype, |
| 335 | Builtins::Name call, |
| 336 | bool is_ecma_native) { |
| 337 | Handle<String> symbol = Factory::LookupAsciiSymbol(name); |
| 338 | Handle<Code> call_code = Handle<Code>(Builtins::builtin(call)); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 339 | Handle<JSFunction> function = prototype.is_null() ? |
| 340 | Factory::NewFunctionWithoutPrototype(symbol, call_code) : |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 341 | Factory::NewFunctionWithPrototype(symbol, |
| 342 | type, |
| 343 | instance_size, |
| 344 | prototype, |
| 345 | call_code, |
| 346 | is_ecma_native); |
| 347 | SetProperty(target, symbol, function, DONT_ENUM); |
| 348 | if (is_ecma_native) { |
| 349 | function->shared()->set_instance_class_name(*symbol); |
| 350 | } |
| 351 | return function; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | Handle<DescriptorArray> Genesis::ComputeFunctionInstanceDescriptor( |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 356 | PrototypePropertyMode prototypeMode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 357 | Handle<DescriptorArray> result = Factory::empty_descriptor_array(); |
| 358 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 359 | if (prototypeMode != DONT_ADD_PROTOTYPE) { |
| 360 | PropertyAttributes attributes = static_cast<PropertyAttributes>( |
| 361 | DONT_ENUM | |
| 362 | DONT_DELETE | |
| 363 | (prototypeMode == ADD_READONLY_PROTOTYPE ? READ_ONLY : 0)); |
| 364 | result = |
| 365 | Factory::CopyAppendProxyDescriptor( |
| 366 | result, |
| 367 | Factory::prototype_symbol(), |
| 368 | Factory::NewProxy(&Accessors::FunctionPrototype), |
| 369 | attributes); |
| 370 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 371 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 372 | PropertyAttributes attributes = |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 373 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 374 | // Add length. |
| 375 | result = |
| 376 | Factory::CopyAppendProxyDescriptor( |
| 377 | result, |
| 378 | Factory::length_symbol(), |
| 379 | Factory::NewProxy(&Accessors::FunctionLength), |
| 380 | attributes); |
| 381 | |
| 382 | // Add name. |
| 383 | result = |
| 384 | Factory::CopyAppendProxyDescriptor( |
| 385 | result, |
| 386 | Factory::name_symbol(), |
| 387 | Factory::NewProxy(&Accessors::FunctionName), |
| 388 | attributes); |
| 389 | |
| 390 | // Add arguments. |
| 391 | result = |
| 392 | Factory::CopyAppendProxyDescriptor( |
| 393 | result, |
| 394 | Factory::arguments_symbol(), |
| 395 | Factory::NewProxy(&Accessors::FunctionArguments), |
| 396 | attributes); |
| 397 | |
| 398 | // Add caller. |
| 399 | result = |
| 400 | Factory::CopyAppendProxyDescriptor( |
| 401 | result, |
| 402 | Factory::caller_symbol(), |
| 403 | Factory::NewProxy(&Accessors::FunctionCaller), |
| 404 | attributes); |
| 405 | |
| 406 | return result; |
| 407 | } |
| 408 | |
| 409 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 410 | Handle<JSFunction> Genesis::CreateEmptyFunction() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 411 | // Allocate the map for function instances. |
| 412 | Handle<Map> fm = Factory::NewMap(JS_FUNCTION_TYPE, JSFunction::kSize); |
| 413 | global_context()->set_function_instance_map(*fm); |
| 414 | // Please note that the prototype property for function instances must be |
| 415 | // writable. |
| 416 | Handle<DescriptorArray> function_map_descriptors = |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 417 | ComputeFunctionInstanceDescriptor(ADD_WRITEABLE_PROTOTYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 418 | fm->set_instance_descriptors(*function_map_descriptors); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 419 | fm->set_function_with_prototype(true); |
| 420 | |
| 421 | // Functions with this map will not have a 'prototype' property, and |
| 422 | // can not be used as constructors. |
| 423 | Handle<Map> function_without_prototype_map = |
| 424 | Factory::NewMap(JS_FUNCTION_TYPE, JSFunction::kSize); |
| 425 | global_context()->set_function_without_prototype_map( |
| 426 | *function_without_prototype_map); |
| 427 | Handle<DescriptorArray> function_without_prototype_map_descriptors = |
| 428 | ComputeFunctionInstanceDescriptor(DONT_ADD_PROTOTYPE); |
| 429 | function_without_prototype_map->set_instance_descriptors( |
| 430 | *function_without_prototype_map_descriptors); |
| 431 | function_without_prototype_map->set_function_with_prototype(false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 432 | |
| 433 | // Allocate the function map first and then patch the prototype later |
| 434 | fm = Factory::NewMap(JS_FUNCTION_TYPE, JSFunction::kSize); |
| 435 | global_context()->set_function_map(*fm); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 436 | function_map_descriptors = |
| 437 | ComputeFunctionInstanceDescriptor(ADD_READONLY_PROTOTYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 438 | fm->set_instance_descriptors(*function_map_descriptors); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 439 | fm->set_function_with_prototype(true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 440 | |
| 441 | Handle<String> object_name = Handle<String>(Heap::Object_symbol()); |
| 442 | |
| 443 | { // --- O b j e c t --- |
| 444 | Handle<JSFunction> object_fun = |
| 445 | Factory::NewFunction(object_name, Factory::null_value()); |
| 446 | Handle<Map> object_function_map = |
| 447 | Factory::NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 448 | object_fun->set_initial_map(*object_function_map); |
| 449 | object_function_map->set_constructor(*object_fun); |
| 450 | |
| 451 | global_context()->set_object_function(*object_fun); |
| 452 | |
| 453 | // Allocate a new prototype for the object function. |
| 454 | Handle<JSObject> prototype = Factory::NewJSObject(Top::object_function(), |
| 455 | TENURED); |
| 456 | |
| 457 | global_context()->set_initial_object_prototype(*prototype); |
| 458 | SetPrototype(object_fun, prototype); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 459 | object_function_map-> |
| 460 | set_instance_descriptors(Heap::empty_descriptor_array()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | // Allocate the empty function as the prototype for function ECMAScript |
| 464 | // 262 15.3.4. |
| 465 | Handle<String> symbol = Factory::LookupAsciiSymbol("Empty"); |
| 466 | Handle<JSFunction> empty_function = |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 467 | Factory::NewFunctionWithoutPrototype(symbol); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 468 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 469 | // --- E m p t y --- |
| 470 | Handle<Code> code = |
| 471 | Handle<Code>(Builtins::builtin(Builtins::EmptyFunction)); |
| 472 | empty_function->set_code(*code); |
| 473 | Handle<String> source = Factory::NewStringFromAscii(CStrVector("() {}")); |
| 474 | Handle<Script> script = Factory::NewScript(source); |
| 475 | script->set_type(Smi::FromInt(Script::TYPE_NATIVE)); |
| 476 | empty_function->shared()->set_script(*script); |
| 477 | empty_function->shared()->set_start_position(0); |
| 478 | empty_function->shared()->set_end_position(source->length()); |
| 479 | empty_function->shared()->DontAdaptArguments(); |
| 480 | global_context()->function_map()->set_prototype(*empty_function); |
| 481 | global_context()->function_instance_map()->set_prototype(*empty_function); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 482 | global_context()->function_without_prototype_map()-> |
| 483 | set_prototype(*empty_function); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 484 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 485 | // Allocate the function map first and then patch the prototype later |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 486 | Handle<Map> empty_fm = Factory::CopyMapDropDescriptors( |
| 487 | function_without_prototype_map); |
| 488 | empty_fm->set_instance_descriptors( |
| 489 | *function_without_prototype_map_descriptors); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 490 | empty_fm->set_prototype(global_context()->object_function()->prototype()); |
| 491 | empty_function->set_map(*empty_fm); |
| 492 | return empty_function; |
| 493 | } |
| 494 | |
| 495 | |
| 496 | void Genesis::CreateRoots() { |
| 497 | // Allocate the global context FixedArray first and then patch the |
| 498 | // closure and extension object later (we need the empty function |
| 499 | // and the global object, but in order to create those, we need the |
| 500 | // global context). |
| 501 | global_context_ = |
| 502 | Handle<Context>::cast( |
| 503 | GlobalHandles::Create(*Factory::NewGlobalContext())); |
| 504 | Top::set_context(*global_context()); |
| 505 | |
| 506 | // Allocate the message listeners object. |
| 507 | { |
| 508 | v8::NeanderArray listeners; |
| 509 | global_context()->set_message_listeners(*listeners.value()); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | |
| 514 | Handle<JSGlobalProxy> Genesis::CreateNewGlobals( |
| 515 | v8::Handle<v8::ObjectTemplate> global_template, |
| 516 | Handle<Object> global_object, |
| 517 | Handle<GlobalObject>* inner_global_out) { |
| 518 | // The argument global_template aka data is an ObjectTemplateInfo. |
| 519 | // It has a constructor pointer that points at global_constructor which is a |
| 520 | // FunctionTemplateInfo. |
| 521 | // The global_constructor is used to create or reinitialize the global_proxy. |
| 522 | // The global_constructor also has a prototype_template pointer that points at |
| 523 | // js_global_template which is an ObjectTemplateInfo. |
| 524 | // That in turn has a constructor pointer that points at |
| 525 | // js_global_constructor which is a FunctionTemplateInfo. |
| 526 | // js_global_constructor is used to make js_global_function |
| 527 | // js_global_function is used to make the new inner_global. |
| 528 | // |
| 529 | // --- G l o b a l --- |
| 530 | // Step 1: Create a fresh inner JSGlobalObject. |
| 531 | Handle<JSFunction> js_global_function; |
| 532 | Handle<ObjectTemplateInfo> js_global_template; |
| 533 | if (!global_template.IsEmpty()) { |
| 534 | // Get prototype template of the global_template. |
| 535 | Handle<ObjectTemplateInfo> data = |
| 536 | v8::Utils::OpenHandle(*global_template); |
| 537 | Handle<FunctionTemplateInfo> global_constructor = |
| 538 | Handle<FunctionTemplateInfo>( |
| 539 | FunctionTemplateInfo::cast(data->constructor())); |
| 540 | Handle<Object> proto_template(global_constructor->prototype_template()); |
| 541 | if (!proto_template->IsUndefined()) { |
| 542 | js_global_template = |
| 543 | Handle<ObjectTemplateInfo>::cast(proto_template); |
| 544 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 545 | } |
| 546 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 547 | if (js_global_template.is_null()) { |
| 548 | Handle<String> name = Handle<String>(Heap::empty_symbol()); |
| 549 | Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal)); |
| 550 | js_global_function = |
| 551 | Factory::NewFunction(name, JS_GLOBAL_OBJECT_TYPE, |
| 552 | JSGlobalObject::kSize, code, true); |
| 553 | // Change the constructor property of the prototype of the |
| 554 | // hidden global function to refer to the Object function. |
| 555 | Handle<JSObject> prototype = |
| 556 | Handle<JSObject>( |
| 557 | JSObject::cast(js_global_function->instance_prototype())); |
| 558 | SetProperty(prototype, Factory::constructor_symbol(), |
| 559 | Top::object_function(), NONE); |
| 560 | } else { |
| 561 | Handle<FunctionTemplateInfo> js_global_constructor( |
| 562 | FunctionTemplateInfo::cast(js_global_template->constructor())); |
| 563 | js_global_function = |
| 564 | Factory::CreateApiFunction(js_global_constructor, |
| 565 | Factory::InnerGlobalObject); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 566 | } |
| 567 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 568 | js_global_function->initial_map()->set_is_hidden_prototype(); |
| 569 | Handle<GlobalObject> inner_global = |
| 570 | Factory::NewGlobalObject(js_global_function); |
| 571 | if (inner_global_out != NULL) { |
| 572 | *inner_global_out = inner_global; |
| 573 | } |
| 574 | |
| 575 | // Step 2: create or re-initialize the global proxy object. |
| 576 | Handle<JSFunction> global_proxy_function; |
| 577 | if (global_template.IsEmpty()) { |
| 578 | Handle<String> name = Handle<String>(Heap::empty_symbol()); |
| 579 | Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal)); |
| 580 | global_proxy_function = |
| 581 | Factory::NewFunction(name, JS_GLOBAL_PROXY_TYPE, |
| 582 | JSGlobalProxy::kSize, code, true); |
| 583 | } else { |
| 584 | Handle<ObjectTemplateInfo> data = |
| 585 | v8::Utils::OpenHandle(*global_template); |
| 586 | Handle<FunctionTemplateInfo> global_constructor( |
| 587 | FunctionTemplateInfo::cast(data->constructor())); |
| 588 | global_proxy_function = |
| 589 | Factory::CreateApiFunction(global_constructor, |
| 590 | Factory::OuterGlobalObject); |
| 591 | } |
| 592 | |
| 593 | Handle<String> global_name = Factory::LookupAsciiSymbol("global"); |
| 594 | global_proxy_function->shared()->set_instance_class_name(*global_name); |
| 595 | global_proxy_function->initial_map()->set_is_access_check_needed(true); |
| 596 | |
| 597 | // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects |
| 598 | // Return the global proxy. |
| 599 | |
| 600 | if (global_object.location() != NULL) { |
| 601 | ASSERT(global_object->IsJSGlobalProxy()); |
| 602 | return ReinitializeJSGlobalProxy( |
| 603 | global_proxy_function, |
| 604 | Handle<JSGlobalProxy>::cast(global_object)); |
| 605 | } else { |
| 606 | return Handle<JSGlobalProxy>::cast( |
| 607 | Factory::NewJSObject(global_proxy_function, TENURED)); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | |
| 612 | void Genesis::HookUpGlobalProxy(Handle<GlobalObject> inner_global, |
| 613 | Handle<JSGlobalProxy> global_proxy) { |
| 614 | // Set the global context for the global object. |
| 615 | inner_global->set_global_context(*global_context()); |
| 616 | inner_global->set_global_receiver(*global_proxy); |
| 617 | global_proxy->set_context(*global_context()); |
| 618 | global_context()->set_global_proxy(*global_proxy); |
| 619 | } |
| 620 | |
| 621 | |
| 622 | void Genesis::HookUpInnerGlobal(Handle<GlobalObject> inner_global) { |
| 623 | Handle<GlobalObject> inner_global_from_snapshot( |
| 624 | GlobalObject::cast(global_context_->extension())); |
| 625 | Handle<JSBuiltinsObject> builtins_global(global_context_->builtins()); |
| 626 | global_context_->set_extension(*inner_global); |
| 627 | global_context_->set_global(*inner_global); |
| 628 | global_context_->set_security_token(*inner_global); |
| 629 | static const PropertyAttributes attributes = |
| 630 | static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE); |
| 631 | ForceSetProperty(builtins_global, |
| 632 | Factory::LookupAsciiSymbol("global"), |
| 633 | inner_global, |
| 634 | attributes); |
| 635 | // Setup the reference from the global object to the builtins object. |
| 636 | JSGlobalObject::cast(*inner_global)->set_builtins(*builtins_global); |
| 637 | TransferNamedProperties(inner_global_from_snapshot, inner_global); |
| 638 | TransferIndexedProperties(inner_global_from_snapshot, inner_global); |
| 639 | } |
| 640 | |
| 641 | |
| 642 | // This is only called if we are not using snapshots. The equivalent |
| 643 | // work in the snapshot case is done in HookUpInnerGlobal. |
| 644 | void Genesis::InitializeGlobal(Handle<GlobalObject> inner_global, |
| 645 | Handle<JSFunction> empty_function) { |
| 646 | // --- G l o b a l C o n t e x t --- |
| 647 | // Use the empty function as closure (no scope info). |
| 648 | global_context()->set_closure(*empty_function); |
| 649 | global_context()->set_fcontext(*global_context()); |
| 650 | global_context()->set_previous(NULL); |
| 651 | // Set extension and global object. |
| 652 | global_context()->set_extension(*inner_global); |
| 653 | global_context()->set_global(*inner_global); |
| 654 | // Security setup: Set the security token of the global object to |
| 655 | // its the inner global. This makes the security check between two |
| 656 | // different contexts fail by default even in case of global |
| 657 | // object reinitialization. |
| 658 | global_context()->set_security_token(*inner_global); |
| 659 | |
| 660 | Handle<String> object_name = Handle<String>(Heap::Object_symbol()); |
| 661 | SetProperty(inner_global, object_name, Top::object_function(), DONT_ENUM); |
| 662 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 663 | Handle<JSObject> global = Handle<JSObject>(global_context()->global()); |
| 664 | |
| 665 | // Install global Function object |
| 666 | InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize, |
| 667 | empty_function, Builtins::Illegal, true); // ECMA native. |
| 668 | |
| 669 | { // --- A r r a y --- |
| 670 | Handle<JSFunction> array_function = |
| 671 | InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize, |
| 672 | Top::initial_object_prototype(), Builtins::ArrayCode, |
| 673 | true); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 674 | array_function->shared()->set_construct_stub( |
| 675 | Builtins::builtin(Builtins::ArrayConstructCode)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 676 | array_function->shared()->DontAdaptArguments(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 677 | |
| 678 | // This seems a bit hackish, but we need to make sure Array.length |
| 679 | // is 1. |
| 680 | array_function->shared()->set_length(1); |
| 681 | Handle<DescriptorArray> array_descriptors = |
| 682 | Factory::CopyAppendProxyDescriptor( |
| 683 | Factory::empty_descriptor_array(), |
| 684 | Factory::length_symbol(), |
| 685 | Factory::NewProxy(&Accessors::ArrayLength), |
| 686 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE)); |
| 687 | |
| 688 | // Cache the fast JavaScript array map |
| 689 | global_context()->set_js_array_map(array_function->initial_map()); |
| 690 | global_context()->js_array_map()->set_instance_descriptors( |
| 691 | *array_descriptors); |
| 692 | // array_function is used internally. JS code creating array object should |
| 693 | // search for the 'Array' property on the global object and use that one |
| 694 | // as the constructor. 'Array' property on a global object can be |
| 695 | // overwritten by JS code. |
| 696 | global_context()->set_array_function(*array_function); |
| 697 | } |
| 698 | |
| 699 | { // --- N u m b e r --- |
| 700 | Handle<JSFunction> number_fun = |
| 701 | InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize, |
| 702 | Top::initial_object_prototype(), Builtins::Illegal, |
| 703 | true); |
| 704 | global_context()->set_number_function(*number_fun); |
| 705 | } |
| 706 | |
| 707 | { // --- B o o l e a n --- |
| 708 | Handle<JSFunction> boolean_fun = |
| 709 | InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize, |
| 710 | Top::initial_object_prototype(), Builtins::Illegal, |
| 711 | true); |
| 712 | global_context()->set_boolean_function(*boolean_fun); |
| 713 | } |
| 714 | |
| 715 | { // --- S t r i n g --- |
| 716 | Handle<JSFunction> string_fun = |
| 717 | InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize, |
| 718 | Top::initial_object_prototype(), Builtins::Illegal, |
| 719 | true); |
| 720 | global_context()->set_string_function(*string_fun); |
| 721 | // Add 'length' property to strings. |
| 722 | Handle<DescriptorArray> string_descriptors = |
| 723 | Factory::CopyAppendProxyDescriptor( |
| 724 | Factory::empty_descriptor_array(), |
| 725 | Factory::length_symbol(), |
| 726 | Factory::NewProxy(&Accessors::StringLength), |
| 727 | static_cast<PropertyAttributes>(DONT_ENUM | |
| 728 | DONT_DELETE | |
| 729 | READ_ONLY)); |
| 730 | |
| 731 | Handle<Map> string_map = |
| 732 | Handle<Map>(global_context()->string_function()->initial_map()); |
| 733 | string_map->set_instance_descriptors(*string_descriptors); |
| 734 | } |
| 735 | |
| 736 | { // --- D a t e --- |
| 737 | // Builtin functions for Date.prototype. |
| 738 | Handle<JSFunction> date_fun = |
| 739 | InstallFunction(global, "Date", JS_VALUE_TYPE, JSValue::kSize, |
| 740 | Top::initial_object_prototype(), Builtins::Illegal, |
| 741 | true); |
| 742 | |
| 743 | global_context()->set_date_function(*date_fun); |
| 744 | } |
| 745 | |
| 746 | |
| 747 | { // -- R e g E x p |
| 748 | // Builtin functions for RegExp.prototype. |
| 749 | Handle<JSFunction> regexp_fun = |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 750 | InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 751 | Top::initial_object_prototype(), Builtins::Illegal, |
| 752 | true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 753 | global_context()->set_regexp_function(*regexp_fun); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 754 | |
| 755 | ASSERT(regexp_fun->has_initial_map()); |
| 756 | Handle<Map> initial_map(regexp_fun->initial_map()); |
| 757 | |
| 758 | ASSERT_EQ(0, initial_map->inobject_properties()); |
| 759 | |
| 760 | Handle<DescriptorArray> descriptors = Factory::NewDescriptorArray(5); |
| 761 | PropertyAttributes final = |
| 762 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 763 | int enum_index = 0; |
| 764 | { |
| 765 | // ECMA-262, section 15.10.7.1. |
| 766 | FieldDescriptor field(Heap::source_symbol(), |
| 767 | JSRegExp::kSourceFieldIndex, |
| 768 | final, |
| 769 | enum_index++); |
| 770 | descriptors->Set(0, &field); |
| 771 | } |
| 772 | { |
| 773 | // ECMA-262, section 15.10.7.2. |
| 774 | FieldDescriptor field(Heap::global_symbol(), |
| 775 | JSRegExp::kGlobalFieldIndex, |
| 776 | final, |
| 777 | enum_index++); |
| 778 | descriptors->Set(1, &field); |
| 779 | } |
| 780 | { |
| 781 | // ECMA-262, section 15.10.7.3. |
| 782 | FieldDescriptor field(Heap::ignore_case_symbol(), |
| 783 | JSRegExp::kIgnoreCaseFieldIndex, |
| 784 | final, |
| 785 | enum_index++); |
| 786 | descriptors->Set(2, &field); |
| 787 | } |
| 788 | { |
| 789 | // ECMA-262, section 15.10.7.4. |
| 790 | FieldDescriptor field(Heap::multiline_symbol(), |
| 791 | JSRegExp::kMultilineFieldIndex, |
| 792 | final, |
| 793 | enum_index++); |
| 794 | descriptors->Set(3, &field); |
| 795 | } |
| 796 | { |
| 797 | // ECMA-262, section 15.10.7.5. |
| 798 | PropertyAttributes writable = |
| 799 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE); |
| 800 | FieldDescriptor field(Heap::last_index_symbol(), |
| 801 | JSRegExp::kLastIndexFieldIndex, |
| 802 | writable, |
| 803 | enum_index++); |
| 804 | descriptors->Set(4, &field); |
| 805 | } |
| 806 | descriptors->SetNextEnumerationIndex(enum_index); |
| 807 | descriptors->Sort(); |
| 808 | |
| 809 | initial_map->set_inobject_properties(5); |
| 810 | initial_map->set_pre_allocated_property_fields(5); |
| 811 | initial_map->set_unused_property_fields(0); |
| 812 | initial_map->set_instance_size( |
| 813 | initial_map->instance_size() + 5 * kPointerSize); |
| 814 | initial_map->set_instance_descriptors(*descriptors); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 815 | } |
| 816 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 817 | { // -- J S O N |
| 818 | Handle<String> name = Factory::NewStringFromAscii(CStrVector("JSON")); |
| 819 | Handle<JSFunction> cons = Factory::NewFunction( |
| 820 | name, |
| 821 | Factory::the_hole_value()); |
| 822 | cons->SetInstancePrototype(global_context()->initial_object_prototype()); |
| 823 | cons->SetInstanceClassName(*name); |
| 824 | Handle<JSObject> json_object = Factory::NewJSObject(cons, TENURED); |
| 825 | ASSERT(json_object->IsJSObject()); |
| 826 | SetProperty(global, name, json_object, DONT_ENUM); |
| 827 | global_context()->set_json_object(*json_object); |
| 828 | } |
| 829 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 830 | { // --- arguments_boilerplate_ |
| 831 | // Make sure we can recognize argument objects at runtime. |
| 832 | // This is done by introducing an anonymous function with |
| 833 | // class_name equals 'Arguments'. |
| 834 | Handle<String> symbol = Factory::LookupAsciiSymbol("Arguments"); |
| 835 | Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal)); |
| 836 | Handle<JSObject> prototype = |
| 837 | Handle<JSObject>( |
| 838 | JSObject::cast(global_context()->object_function()->prototype())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 839 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 840 | Handle<JSFunction> function = |
| 841 | Factory::NewFunctionWithPrototype(symbol, |
| 842 | JS_OBJECT_TYPE, |
| 843 | JSObject::kHeaderSize, |
| 844 | prototype, |
| 845 | code, |
| 846 | false); |
| 847 | ASSERT(!function->has_initial_map()); |
| 848 | function->shared()->set_instance_class_name(*symbol); |
| 849 | function->shared()->set_expected_nof_properties(2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 850 | Handle<JSObject> result = Factory::NewJSObject(function); |
| 851 | |
| 852 | global_context()->set_arguments_boilerplate(*result); |
| 853 | // Note: callee must be added as the first property and |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 854 | // length must be added as the second property. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 855 | SetProperty(result, Factory::callee_symbol(), |
| 856 | Factory::undefined_value(), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 857 | DONT_ENUM); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 858 | SetProperty(result, Factory::length_symbol(), |
| 859 | Factory::undefined_value(), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 860 | DONT_ENUM); |
| 861 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 862 | #ifdef DEBUG |
| 863 | LookupResult lookup; |
| 864 | result->LocalLookup(Heap::callee_symbol(), &lookup); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 865 | ASSERT(lookup.IsProperty() && (lookup.type() == FIELD)); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 866 | ASSERT(lookup.GetFieldIndex() == Heap::arguments_callee_index); |
| 867 | |
| 868 | result->LocalLookup(Heap::length_symbol(), &lookup); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 869 | ASSERT(lookup.IsProperty() && (lookup.type() == FIELD)); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 870 | ASSERT(lookup.GetFieldIndex() == Heap::arguments_length_index); |
| 871 | |
| 872 | ASSERT(result->map()->inobject_properties() > Heap::arguments_callee_index); |
| 873 | ASSERT(result->map()->inobject_properties() > Heap::arguments_length_index); |
| 874 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 875 | // Check the state of the object. |
| 876 | ASSERT(result->HasFastProperties()); |
| 877 | ASSERT(result->HasFastElements()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 878 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | { // --- context extension |
| 882 | // Create a function for the context extension objects. |
| 883 | Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal)); |
| 884 | Handle<JSFunction> context_extension_fun = |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 885 | Factory::NewFunction(Factory::empty_symbol(), |
| 886 | JS_CONTEXT_EXTENSION_OBJECT_TYPE, |
| 887 | JSObject::kHeaderSize, |
| 888 | code, |
| 889 | true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 890 | |
| 891 | Handle<String> name = Factory::LookupAsciiSymbol("context_extension"); |
| 892 | context_extension_fun->shared()->set_instance_class_name(*name); |
| 893 | global_context()->set_context_extension_function(*context_extension_fun); |
| 894 | } |
| 895 | |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 896 | |
| 897 | { |
| 898 | // Setup the call-as-function delegate. |
| 899 | Handle<Code> code = |
| 900 | Handle<Code>(Builtins::builtin(Builtins::HandleApiCallAsFunction)); |
| 901 | Handle<JSFunction> delegate = |
| 902 | Factory::NewFunction(Factory::empty_symbol(), JS_OBJECT_TYPE, |
| 903 | JSObject::kHeaderSize, code, true); |
| 904 | global_context()->set_call_as_function_delegate(*delegate); |
| 905 | delegate->shared()->DontAdaptArguments(); |
| 906 | } |
| 907 | |
| 908 | { |
| 909 | // Setup the call-as-constructor delegate. |
| 910 | Handle<Code> code = |
| 911 | Handle<Code>(Builtins::builtin(Builtins::HandleApiCallAsConstructor)); |
| 912 | Handle<JSFunction> delegate = |
| 913 | Factory::NewFunction(Factory::empty_symbol(), JS_OBJECT_TYPE, |
| 914 | JSObject::kHeaderSize, code, true); |
| 915 | global_context()->set_call_as_constructor_delegate(*delegate); |
| 916 | delegate->shared()->DontAdaptArguments(); |
| 917 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 918 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 919 | // Initialize the out of memory slot. |
| 920 | global_context()->set_out_of_memory(Heap::false_value()); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 921 | |
| 922 | // Initialize the data slot. |
| 923 | global_context()->set_data(Heap::undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | |
| 927 | bool Genesis::CompileBuiltin(int index) { |
| 928 | Vector<const char> name = Natives::GetScriptName(index); |
| 929 | Handle<String> source_code = Bootstrapper::NativesSourceLookup(index); |
| 930 | return CompileNative(name, source_code); |
| 931 | } |
| 932 | |
| 933 | |
| 934 | bool Genesis::CompileNative(Vector<const char> name, Handle<String> source) { |
| 935 | HandleScope scope; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 936 | #ifdef ENABLE_DEBUGGER_SUPPORT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 937 | Debugger::set_compiling_natives(true); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 938 | #endif |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 939 | bool result = CompileScriptCached(name, |
| 940 | source, |
| 941 | NULL, |
| 942 | NULL, |
| 943 | Handle<Context>(Top::context()), |
| 944 | true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 945 | ASSERT(Top::has_pending_exception() != result); |
| 946 | if (!result) Top::clear_pending_exception(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 947 | #ifdef ENABLE_DEBUGGER_SUPPORT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 948 | Debugger::set_compiling_natives(false); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 949 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 950 | return result; |
| 951 | } |
| 952 | |
| 953 | |
| 954 | bool Genesis::CompileScriptCached(Vector<const char> name, |
| 955 | Handle<String> source, |
| 956 | SourceCodeCache* cache, |
| 957 | v8::Extension* extension, |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 958 | Handle<Context> top_context, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 959 | bool use_runtime_context) { |
| 960 | HandleScope scope; |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 961 | Handle<SharedFunctionInfo> function_info; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 962 | |
| 963 | // If we can't find the function in the cache, we compile a new |
| 964 | // function and insert it into the cache. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 965 | if (cache == NULL || !cache->Lookup(name, &function_info)) { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 966 | ASSERT(source->IsAsciiRepresentation()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 967 | Handle<String> script_name = Factory::NewStringFromUtf8(name); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 968 | function_info = Compiler::Compile( |
| 969 | source, |
| 970 | script_name, |
| 971 | 0, |
| 972 | 0, |
| 973 | extension, |
| 974 | NULL, |
| 975 | Handle<String>::null(), |
| 976 | use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE); |
| 977 | if (function_info.is_null()) return false; |
| 978 | if (cache != NULL) cache->Add(name, function_info); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | // Setup the function context. Conceptually, we should clone the |
| 982 | // function before overwriting the context but since we're in a |
| 983 | // single-threaded environment it is not strictly necessary. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 984 | ASSERT(top_context->IsGlobalContext()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 985 | Handle<Context> context = |
| 986 | Handle<Context>(use_runtime_context |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 987 | ? Handle<Context>(top_context->runtime_context()) |
| 988 | : top_context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 989 | Handle<JSFunction> fun = |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 990 | Factory::NewFunctionFromSharedFunctionInfo(function_info, context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 991 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 992 | // Call function using either the runtime object or the global |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 993 | // object as the receiver. Provide no parameters. |
| 994 | Handle<Object> receiver = |
| 995 | Handle<Object>(use_runtime_context |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 996 | ? top_context->builtins() |
| 997 | : top_context->global()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 998 | bool has_pending_exception; |
| 999 | Handle<Object> result = |
| 1000 | Execution::Call(fun, receiver, 0, NULL, &has_pending_exception); |
| 1001 | if (has_pending_exception) return false; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1002 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | |
| 1006 | #define INSTALL_NATIVE(Type, name, var) \ |
| 1007 | Handle<String> var##_name = Factory::LookupAsciiSymbol(name); \ |
| 1008 | global_context()->set_##var(Type::cast(global_context()-> \ |
| 1009 | builtins()-> \ |
| 1010 | GetProperty(*var##_name))); |
| 1011 | |
| 1012 | void Genesis::InstallNativeFunctions() { |
| 1013 | HandleScope scope; |
| 1014 | INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun); |
| 1015 | INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun); |
| 1016 | INSTALL_NATIVE(JSFunction, "ToString", to_string_fun); |
| 1017 | INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun); |
| 1018 | INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun); |
| 1019 | INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun); |
| 1020 | INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun); |
| 1021 | INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1022 | INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1023 | INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun); |
| 1024 | INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance", |
| 1025 | configure_instance_fun); |
| 1026 | INSTALL_NATIVE(JSFunction, "MakeMessage", make_message_fun); |
| 1027 | INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun); |
| 1028 | INSTALL_NATIVE(JSObject, "functionCache", function_cache); |
| 1029 | } |
| 1030 | |
| 1031 | #undef INSTALL_NATIVE |
| 1032 | |
| 1033 | |
| 1034 | bool Genesis::InstallNatives() { |
| 1035 | HandleScope scope; |
| 1036 | |
| 1037 | // Create a function for the builtins object. Allocate space for the |
| 1038 | // JavaScript builtins, a reference to the builtins object |
| 1039 | // (itself) and a reference to the global_context directly in the object. |
| 1040 | Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal)); |
| 1041 | Handle<JSFunction> builtins_fun = |
| 1042 | Factory::NewFunction(Factory::empty_symbol(), JS_BUILTINS_OBJECT_TYPE, |
| 1043 | JSBuiltinsObject::kSize, code, true); |
| 1044 | |
| 1045 | Handle<String> name = Factory::LookupAsciiSymbol("builtins"); |
| 1046 | builtins_fun->shared()->set_instance_class_name(*name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1047 | |
| 1048 | // Allocate the builtins object. |
| 1049 | Handle<JSBuiltinsObject> builtins = |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1050 | Handle<JSBuiltinsObject>::cast(Factory::NewGlobalObject(builtins_fun)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1051 | builtins->set_builtins(*builtins); |
| 1052 | builtins->set_global_context(*global_context()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1053 | builtins->set_global_receiver(*builtins); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1054 | |
| 1055 | // Setup the 'global' properties of the builtins object. The |
| 1056 | // 'global' property that refers to the global object is the only |
| 1057 | // way to get from code running in the builtins context to the |
| 1058 | // global object. |
| 1059 | static const PropertyAttributes attributes = |
| 1060 | static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE); |
| 1061 | SetProperty(builtins, Factory::LookupAsciiSymbol("global"), |
| 1062 | Handle<Object>(global_context()->global()), attributes); |
| 1063 | |
| 1064 | // Setup the reference from the global object to the builtins object. |
| 1065 | JSGlobalObject::cast(global_context()->global())->set_builtins(*builtins); |
| 1066 | |
| 1067 | // Create a bridge function that has context in the global context. |
| 1068 | Handle<JSFunction> bridge = |
| 1069 | Factory::NewFunction(Factory::empty_symbol(), Factory::undefined_value()); |
| 1070 | ASSERT(bridge->context() == *Top::global_context()); |
| 1071 | |
| 1072 | // Allocate the builtins context. |
| 1073 | Handle<Context> context = |
| 1074 | Factory::NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge); |
| 1075 | context->set_global(*builtins); // override builtins global object |
| 1076 | |
| 1077 | global_context()->set_runtime_context(*context); |
| 1078 | |
| 1079 | { // -- S c r i p t |
| 1080 | // Builtin functions for Script. |
| 1081 | Handle<JSFunction> script_fun = |
| 1082 | InstallFunction(builtins, "Script", JS_VALUE_TYPE, JSValue::kSize, |
| 1083 | Top::initial_object_prototype(), Builtins::Illegal, |
| 1084 | false); |
| 1085 | Handle<JSObject> prototype = |
| 1086 | Factory::NewJSObject(Top::object_function(), TENURED); |
| 1087 | SetPrototype(script_fun, prototype); |
| 1088 | global_context()->set_script_function(*script_fun); |
| 1089 | |
| 1090 | // Add 'source' and 'data' property to scripts. |
| 1091 | PropertyAttributes common_attributes = |
| 1092 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 1093 | Handle<Proxy> proxy_source = Factory::NewProxy(&Accessors::ScriptSource); |
| 1094 | Handle<DescriptorArray> script_descriptors = |
| 1095 | Factory::CopyAppendProxyDescriptor( |
| 1096 | Factory::empty_descriptor_array(), |
| 1097 | Factory::LookupAsciiSymbol("source"), |
| 1098 | proxy_source, |
| 1099 | common_attributes); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1100 | Handle<Proxy> proxy_name = Factory::NewProxy(&Accessors::ScriptName); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1101 | script_descriptors = |
| 1102 | Factory::CopyAppendProxyDescriptor( |
| 1103 | script_descriptors, |
| 1104 | Factory::LookupAsciiSymbol("name"), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1105 | proxy_name, |
| 1106 | common_attributes); |
| 1107 | Handle<Proxy> proxy_id = Factory::NewProxy(&Accessors::ScriptId); |
| 1108 | script_descriptors = |
| 1109 | Factory::CopyAppendProxyDescriptor( |
| 1110 | script_descriptors, |
| 1111 | Factory::LookupAsciiSymbol("id"), |
| 1112 | proxy_id, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1113 | common_attributes); |
| 1114 | Handle<Proxy> proxy_line_offset = |
| 1115 | Factory::NewProxy(&Accessors::ScriptLineOffset); |
| 1116 | script_descriptors = |
| 1117 | Factory::CopyAppendProxyDescriptor( |
| 1118 | script_descriptors, |
| 1119 | Factory::LookupAsciiSymbol("line_offset"), |
| 1120 | proxy_line_offset, |
| 1121 | common_attributes); |
| 1122 | Handle<Proxy> proxy_column_offset = |
| 1123 | Factory::NewProxy(&Accessors::ScriptColumnOffset); |
| 1124 | script_descriptors = |
| 1125 | Factory::CopyAppendProxyDescriptor( |
| 1126 | script_descriptors, |
| 1127 | Factory::LookupAsciiSymbol("column_offset"), |
| 1128 | proxy_column_offset, |
| 1129 | common_attributes); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1130 | Handle<Proxy> proxy_data = Factory::NewProxy(&Accessors::ScriptData); |
| 1131 | script_descriptors = |
| 1132 | Factory::CopyAppendProxyDescriptor( |
| 1133 | script_descriptors, |
| 1134 | Factory::LookupAsciiSymbol("data"), |
| 1135 | proxy_data, |
| 1136 | common_attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1137 | Handle<Proxy> proxy_type = Factory::NewProxy(&Accessors::ScriptType); |
| 1138 | script_descriptors = |
| 1139 | Factory::CopyAppendProxyDescriptor( |
| 1140 | script_descriptors, |
| 1141 | Factory::LookupAsciiSymbol("type"), |
| 1142 | proxy_type, |
| 1143 | common_attributes); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1144 | Handle<Proxy> proxy_compilation_type = |
| 1145 | Factory::NewProxy(&Accessors::ScriptCompilationType); |
| 1146 | script_descriptors = |
| 1147 | Factory::CopyAppendProxyDescriptor( |
| 1148 | script_descriptors, |
| 1149 | Factory::LookupAsciiSymbol("compilation_type"), |
| 1150 | proxy_compilation_type, |
| 1151 | common_attributes); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1152 | Handle<Proxy> proxy_line_ends = |
| 1153 | Factory::NewProxy(&Accessors::ScriptLineEnds); |
| 1154 | script_descriptors = |
| 1155 | Factory::CopyAppendProxyDescriptor( |
| 1156 | script_descriptors, |
| 1157 | Factory::LookupAsciiSymbol("line_ends"), |
| 1158 | proxy_line_ends, |
| 1159 | common_attributes); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1160 | Handle<Proxy> proxy_context_data = |
| 1161 | Factory::NewProxy(&Accessors::ScriptContextData); |
| 1162 | script_descriptors = |
| 1163 | Factory::CopyAppendProxyDescriptor( |
| 1164 | script_descriptors, |
| 1165 | Factory::LookupAsciiSymbol("context_data"), |
| 1166 | proxy_context_data, |
| 1167 | common_attributes); |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 1168 | Handle<Proxy> proxy_eval_from_script = |
| 1169 | Factory::NewProxy(&Accessors::ScriptEvalFromScript); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1170 | script_descriptors = |
| 1171 | Factory::CopyAppendProxyDescriptor( |
| 1172 | script_descriptors, |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 1173 | Factory::LookupAsciiSymbol("eval_from_script"), |
| 1174 | proxy_eval_from_script, |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1175 | common_attributes); |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 1176 | Handle<Proxy> proxy_eval_from_script_position = |
| 1177 | Factory::NewProxy(&Accessors::ScriptEvalFromScriptPosition); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1178 | script_descriptors = |
| 1179 | Factory::CopyAppendProxyDescriptor( |
| 1180 | script_descriptors, |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 1181 | Factory::LookupAsciiSymbol("eval_from_script_position"), |
| 1182 | proxy_eval_from_script_position, |
| 1183 | common_attributes); |
| 1184 | Handle<Proxy> proxy_eval_from_function_name = |
| 1185 | Factory::NewProxy(&Accessors::ScriptEvalFromFunctionName); |
| 1186 | script_descriptors = |
| 1187 | Factory::CopyAppendProxyDescriptor( |
| 1188 | script_descriptors, |
| 1189 | Factory::LookupAsciiSymbol("eval_from_function_name"), |
| 1190 | proxy_eval_from_function_name, |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1191 | common_attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1192 | |
| 1193 | Handle<Map> script_map = Handle<Map>(script_fun->initial_map()); |
| 1194 | script_map->set_instance_descriptors(*script_descriptors); |
| 1195 | |
| 1196 | // Allocate the empty script. |
| 1197 | Handle<Script> script = Factory::NewScript(Factory::empty_string()); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1198 | script->set_type(Smi::FromInt(Script::TYPE_NATIVE)); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1199 | Heap::public_set_empty_script(*script); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1200 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1201 | { |
| 1202 | // Builtin function for OpaqueReference -- a JSValue-based object, |
| 1203 | // that keeps its field isolated from JavaScript code. It may store |
| 1204 | // objects, that JavaScript code may not access. |
| 1205 | Handle<JSFunction> opaque_reference_fun = |
| 1206 | InstallFunction(builtins, "OpaqueReference", JS_VALUE_TYPE, |
| 1207 | JSValue::kSize, Top::initial_object_prototype(), |
| 1208 | Builtins::Illegal, false); |
| 1209 | Handle<JSObject> prototype = |
| 1210 | Factory::NewJSObject(Top::object_function(), TENURED); |
| 1211 | SetPrototype(opaque_reference_fun, prototype); |
| 1212 | global_context()->set_opaque_reference_function(*opaque_reference_fun); |
| 1213 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1214 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1215 | if (FLAG_disable_native_files) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1216 | PrintF("Warning: Running without installed natives!\n"); |
| 1217 | return true; |
| 1218 | } |
| 1219 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1220 | // Install natives. |
| 1221 | for (int i = Natives::GetDebuggerCount(); |
| 1222 | i < Natives::GetBuiltinsCount(); |
| 1223 | i++) { |
| 1224 | Vector<const char> name = Natives::GetScriptName(i); |
| 1225 | if (!CompileBuiltin(i)) return false; |
| 1226 | // TODO(ager): We really only need to install the JS builtin |
| 1227 | // functions on the builtins object after compiling and running |
| 1228 | // runtime.js. |
| 1229 | if (!InstallJSBuiltins(builtins)) return false; |
| 1230 | } |
| 1231 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1232 | InstallNativeFunctions(); |
| 1233 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1234 | InstallCustomCallGenerators(); |
| 1235 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1236 | // Install Function.prototype.call and apply. |
| 1237 | { Handle<String> key = Factory::function_class_symbol(); |
| 1238 | Handle<JSFunction> function = |
| 1239 | Handle<JSFunction>::cast(GetProperty(Top::global(), key)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1240 | Handle<JSObject> proto = |
| 1241 | Handle<JSObject>(JSObject::cast(function->instance_prototype())); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1242 | |
| 1243 | // Install the call and the apply functions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1244 | Handle<JSFunction> call = |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1245 | InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize, |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1246 | Handle<JSObject>::null(), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1247 | Builtins::FunctionCall, |
| 1248 | false); |
| 1249 | Handle<JSFunction> apply = |
| 1250 | InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize, |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1251 | Handle<JSObject>::null(), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1252 | Builtins::FunctionApply, |
| 1253 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1254 | |
| 1255 | // Make sure that Function.prototype.call appears to be compiled. |
| 1256 | // The code will never be called, but inline caching for call will |
| 1257 | // only work if it appears to be compiled. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1258 | call->shared()->DontAdaptArguments(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1259 | ASSERT(call->is_compiled()); |
| 1260 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1261 | // Set the expected parameters for apply to 2; required by builtin. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1262 | apply->shared()->set_formal_parameter_count(2); |
| 1263 | |
| 1264 | // Set the lengths for the functions to satisfy ECMA-262. |
| 1265 | call->shared()->set_length(1); |
| 1266 | apply->shared()->set_length(2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1267 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1268 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1269 | // Create a constructor for RegExp results (a variant of Array that |
| 1270 | // predefines the two properties index and match). |
| 1271 | { |
| 1272 | // RegExpResult initial map. |
| 1273 | |
| 1274 | // Find global.Array.prototype to inherit from. |
| 1275 | Handle<JSFunction> array_constructor(global_context()->array_function()); |
| 1276 | Handle<JSObject> array_prototype( |
| 1277 | JSObject::cast(array_constructor->instance_prototype())); |
| 1278 | |
| 1279 | // Add initial map. |
| 1280 | Handle<Map> initial_map = |
| 1281 | Factory::NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize); |
| 1282 | initial_map->set_constructor(*array_constructor); |
| 1283 | |
| 1284 | // Set prototype on map. |
| 1285 | initial_map->set_non_instance_prototype(false); |
| 1286 | initial_map->set_prototype(*array_prototype); |
| 1287 | |
| 1288 | // Update map with length accessor from Array and add "index" and "input". |
| 1289 | Handle<Map> array_map(global_context()->js_array_map()); |
| 1290 | Handle<DescriptorArray> array_descriptors( |
| 1291 | array_map->instance_descriptors()); |
| 1292 | ASSERT_EQ(1, array_descriptors->number_of_descriptors()); |
| 1293 | |
| 1294 | Handle<DescriptorArray> reresult_descriptors = |
| 1295 | Factory::NewDescriptorArray(3); |
| 1296 | |
| 1297 | reresult_descriptors->CopyFrom(0, *array_descriptors, 0); |
| 1298 | |
| 1299 | int enum_index = 0; |
| 1300 | { |
| 1301 | FieldDescriptor index_field(Heap::index_symbol(), |
| 1302 | JSRegExpResult::kIndexIndex, |
| 1303 | NONE, |
| 1304 | enum_index++); |
| 1305 | reresult_descriptors->Set(1, &index_field); |
| 1306 | } |
| 1307 | |
| 1308 | { |
| 1309 | FieldDescriptor input_field(Heap::input_symbol(), |
| 1310 | JSRegExpResult::kInputIndex, |
| 1311 | NONE, |
| 1312 | enum_index++); |
| 1313 | reresult_descriptors->Set(2, &input_field); |
| 1314 | } |
| 1315 | reresult_descriptors->Sort(); |
| 1316 | |
| 1317 | initial_map->set_inobject_properties(2); |
| 1318 | initial_map->set_pre_allocated_property_fields(2); |
| 1319 | initial_map->set_unused_property_fields(0); |
| 1320 | initial_map->set_instance_descriptors(*reresult_descriptors); |
| 1321 | |
| 1322 | global_context()->set_regexp_result_map(*initial_map); |
| 1323 | } |
| 1324 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1325 | #ifdef DEBUG |
| 1326 | builtins->Verify(); |
| 1327 | #endif |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1328 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1329 | return true; |
| 1330 | } |
| 1331 | |
| 1332 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1333 | static void InstallCustomCallGenerator(Handle<JSFunction> holder_function, |
| 1334 | const char* function_name, |
| 1335 | int id) { |
| 1336 | Handle<JSObject> proto(JSObject::cast(holder_function->instance_prototype())); |
| 1337 | Handle<String> name = Factory::LookupAsciiSymbol(function_name); |
| 1338 | Handle<JSFunction> function(JSFunction::cast(proto->GetProperty(*name))); |
| 1339 | function->shared()->set_function_data(Smi::FromInt(id)); |
| 1340 | } |
| 1341 | |
| 1342 | |
| 1343 | void Genesis::InstallCustomCallGenerators() { |
| 1344 | HandleScope scope; |
| 1345 | #define INSTALL_CALL_GENERATOR(holder_fun, fun_name, name) \ |
| 1346 | { \ |
| 1347 | Handle<JSFunction> holder(global_context()->holder_fun##_function()); \ |
| 1348 | const int id = CallStubCompiler::k##name##CallGenerator; \ |
| 1349 | InstallCustomCallGenerator(holder, #fun_name, id); \ |
| 1350 | } |
| 1351 | CUSTOM_CALL_IC_GENERATORS(INSTALL_CALL_GENERATOR) |
| 1352 | #undef INSTALL_CALL_GENERATOR |
| 1353 | } |
| 1354 | |
| 1355 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1356 | // Do not forget to update macros.py with named constant |
| 1357 | // of cache id. |
| 1358 | #define JSFUNCTION_RESULT_CACHE_LIST(F) \ |
| 1359 | F(16, global_context()->regexp_function()) |
| 1360 | |
| 1361 | |
| 1362 | static FixedArray* CreateCache(int size, JSFunction* factory) { |
| 1363 | // Caches are supposed to live for a long time, allocate in old space. |
| 1364 | int array_size = JSFunctionResultCache::kEntriesIndex + 2 * size; |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1365 | // Cannot use cast as object is not fully initialized yet. |
| 1366 | JSFunctionResultCache* cache = reinterpret_cast<JSFunctionResultCache*>( |
| 1367 | *Factory::NewFixedArrayWithHoles(array_size, TENURED)); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1368 | cache->set(JSFunctionResultCache::kFactoryIndex, factory); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1369 | cache->MakeZeroSize(); |
| 1370 | return cache; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | |
| 1374 | void Genesis::InstallJSFunctionResultCaches() { |
| 1375 | const int kNumberOfCaches = 0 + |
| 1376 | #define F(size, func) + 1 |
| 1377 | JSFUNCTION_RESULT_CACHE_LIST(F) |
| 1378 | #undef F |
| 1379 | ; |
| 1380 | |
| 1381 | Handle<FixedArray> caches = Factory::NewFixedArray(kNumberOfCaches, TENURED); |
| 1382 | |
| 1383 | int index = 0; |
| 1384 | #define F(size, func) caches->set(index++, CreateCache(size, func)); |
| 1385 | JSFUNCTION_RESULT_CACHE_LIST(F) |
| 1386 | #undef F |
| 1387 | |
| 1388 | global_context()->set_jsfunction_result_caches(*caches); |
| 1389 | } |
| 1390 | |
| 1391 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1392 | int BootstrapperActive::nesting_ = 0; |
| 1393 | |
| 1394 | |
| 1395 | bool Bootstrapper::InstallExtensions(Handle<Context> global_context, |
| 1396 | v8::ExtensionConfiguration* extensions) { |
| 1397 | BootstrapperActive active; |
| 1398 | SaveContext saved_context; |
| 1399 | Top::set_context(*global_context); |
| 1400 | if (!Genesis::InstallExtensions(global_context, extensions)) return false; |
| 1401 | Genesis::InstallSpecialObjects(global_context); |
| 1402 | return true; |
| 1403 | } |
| 1404 | |
| 1405 | |
| 1406 | void Genesis::InstallSpecialObjects(Handle<Context> global_context) { |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 1407 | HandleScope scope; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1408 | Handle<JSGlobalObject> js_global( |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1409 | JSGlobalObject::cast(global_context->global())); |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 1410 | // Expose the natives in global if a name for it is specified. |
| 1411 | if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) { |
| 1412 | Handle<String> natives_string = |
| 1413 | Factory::LookupAsciiSymbol(FLAG_expose_natives_as); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1414 | SetProperty(js_global, natives_string, |
| 1415 | Handle<JSObject>(js_global->builtins()), DONT_ENUM); |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1418 | Handle<Object> Error = GetProperty(js_global, "Error"); |
| 1419 | if (Error->IsJSObject()) { |
| 1420 | Handle<String> name = Factory::LookupAsciiSymbol("stackTraceLimit"); |
| 1421 | SetProperty(Handle<JSObject>::cast(Error), |
| 1422 | name, |
| 1423 | Handle<Smi>(Smi::FromInt(FLAG_stack_trace_limit)), |
| 1424 | NONE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1427 | #ifdef ENABLE_DEBUGGER_SUPPORT |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 1428 | // Expose the debug global object in global if a name for it is specified. |
| 1429 | if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) { |
| 1430 | // If loading fails we just bail out without installing the |
| 1431 | // debugger but without tanking the whole context. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1432 | if (!Debug::Load()) return; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1433 | // Set the security token for the debugger context to the same as |
| 1434 | // the shell global context to allow calling between these (otherwise |
| 1435 | // exposing debug global object doesn't make much sense). |
| 1436 | Debug::debug_context()->set_security_token( |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1437 | global_context->security_token()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1438 | |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 1439 | Handle<String> debug_string = |
| 1440 | Factory::LookupAsciiSymbol(FLAG_expose_debug_as); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1441 | SetProperty(js_global, debug_string, |
| 1442 | Handle<Object>(Debug::debug_context()->global_proxy()), DONT_ENUM); |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 1443 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1444 | #endif |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1448 | bool Genesis::InstallExtensions(Handle<Context> global_context, |
| 1449 | v8::ExtensionConfiguration* extensions) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1450 | // Clear coloring of extension list |
| 1451 | v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension(); |
| 1452 | while (current != NULL) { |
| 1453 | current->set_state(v8::UNVISITED); |
| 1454 | current = current->next(); |
| 1455 | } |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1456 | // Install auto extensions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1457 | current = v8::RegisteredExtension::first_extension(); |
| 1458 | while (current != NULL) { |
| 1459 | if (current->extension()->auto_enable()) |
| 1460 | InstallExtension(current); |
| 1461 | current = current->next(); |
| 1462 | } |
| 1463 | |
| 1464 | if (FLAG_expose_gc) InstallExtension("v8/gc"); |
| 1465 | |
| 1466 | if (extensions == NULL) return true; |
| 1467 | // Install required extensions |
| 1468 | int count = v8::ImplementationUtilities::GetNameCount(extensions); |
| 1469 | const char** names = v8::ImplementationUtilities::GetNames(extensions); |
| 1470 | for (int i = 0; i < count; i++) { |
| 1471 | if (!InstallExtension(names[i])) |
| 1472 | return false; |
| 1473 | } |
| 1474 | |
| 1475 | return true; |
| 1476 | } |
| 1477 | |
| 1478 | |
| 1479 | // Installs a named extension. This methods is unoptimized and does |
| 1480 | // not scale well if we want to support a large number of extensions. |
| 1481 | bool Genesis::InstallExtension(const char* name) { |
| 1482 | v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension(); |
| 1483 | // Loop until we find the relevant extension |
| 1484 | while (current != NULL) { |
| 1485 | if (strcmp(name, current->extension()->name()) == 0) break; |
| 1486 | current = current->next(); |
| 1487 | } |
| 1488 | // Didn't find the extension; fail. |
| 1489 | if (current == NULL) { |
| 1490 | v8::Utils::ReportApiFailure( |
| 1491 | "v8::Context::New()", "Cannot find required extension"); |
| 1492 | return false; |
| 1493 | } |
| 1494 | return InstallExtension(current); |
| 1495 | } |
| 1496 | |
| 1497 | |
| 1498 | bool Genesis::InstallExtension(v8::RegisteredExtension* current) { |
| 1499 | HandleScope scope; |
| 1500 | |
| 1501 | if (current->state() == v8::INSTALLED) return true; |
| 1502 | // The current node has already been visited so there must be a |
| 1503 | // cycle in the dependency graph; fail. |
| 1504 | if (current->state() == v8::VISITED) { |
| 1505 | v8::Utils::ReportApiFailure( |
| 1506 | "v8::Context::New()", "Circular extension dependency"); |
| 1507 | return false; |
| 1508 | } |
| 1509 | ASSERT(current->state() == v8::UNVISITED); |
| 1510 | current->set_state(v8::VISITED); |
| 1511 | v8::Extension* extension = current->extension(); |
| 1512 | // Install the extension's dependencies |
| 1513 | for (int i = 0; i < extension->dependency_count(); i++) { |
| 1514 | if (!InstallExtension(extension->dependencies()[i])) return false; |
| 1515 | } |
| 1516 | Vector<const char> source = CStrVector(extension->source()); |
| 1517 | Handle<String> source_code = Factory::NewStringFromAscii(source); |
| 1518 | bool result = CompileScriptCached(CStrVector(extension->name()), |
| 1519 | source_code, |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1520 | &extensions_cache, |
| 1521 | extension, |
| 1522 | Handle<Context>(Top::context()), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1523 | false); |
| 1524 | ASSERT(Top::has_pending_exception() != result); |
| 1525 | if (!result) { |
| 1526 | Top::clear_pending_exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1527 | } |
| 1528 | current->set_state(v8::INSTALLED); |
| 1529 | return result; |
| 1530 | } |
| 1531 | |
| 1532 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1533 | bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) { |
| 1534 | HandleScope scope; |
| 1535 | for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) { |
| 1536 | Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i); |
| 1537 | Handle<String> name = Factory::LookupAsciiSymbol(Builtins::GetName(id)); |
| 1538 | Handle<JSFunction> function |
| 1539 | = Handle<JSFunction>(JSFunction::cast(builtins->GetProperty(*name))); |
| 1540 | builtins->set_javascript_builtin(id, *function); |
| 1541 | Handle<SharedFunctionInfo> shared |
| 1542 | = Handle<SharedFunctionInfo>(function->shared()); |
| 1543 | if (!EnsureCompiled(shared, CLEAR_EXCEPTION)) return false; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1544 | builtins->set_javascript_builtin_code(id, shared->code()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1545 | } |
| 1546 | return true; |
| 1547 | } |
| 1548 | |
| 1549 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1550 | bool Genesis::ConfigureGlobalObjects( |
| 1551 | v8::Handle<v8::ObjectTemplate> global_proxy_template) { |
| 1552 | Handle<JSObject> global_proxy( |
| 1553 | JSObject::cast(global_context()->global_proxy())); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1554 | Handle<JSObject> inner_global(JSObject::cast(global_context()->global())); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1555 | |
| 1556 | if (!global_proxy_template.IsEmpty()) { |
| 1557 | // Configure the global proxy object. |
| 1558 | Handle<ObjectTemplateInfo> proxy_data = |
| 1559 | v8::Utils::OpenHandle(*global_proxy_template); |
| 1560 | if (!ConfigureApiObject(global_proxy, proxy_data)) return false; |
| 1561 | |
| 1562 | // Configure the inner global object. |
| 1563 | Handle<FunctionTemplateInfo> proxy_constructor( |
| 1564 | FunctionTemplateInfo::cast(proxy_data->constructor())); |
| 1565 | if (!proxy_constructor->prototype_template()->IsUndefined()) { |
| 1566 | Handle<ObjectTemplateInfo> inner_data( |
| 1567 | ObjectTemplateInfo::cast(proxy_constructor->prototype_template())); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1568 | if (!ConfigureApiObject(inner_global, inner_data)) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1569 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1570 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1571 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1572 | SetObjectPrototype(global_proxy, inner_global); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1573 | return true; |
| 1574 | } |
| 1575 | |
| 1576 | |
| 1577 | bool Genesis::ConfigureApiObject(Handle<JSObject> object, |
| 1578 | Handle<ObjectTemplateInfo> object_template) { |
| 1579 | ASSERT(!object_template.is_null()); |
| 1580 | ASSERT(object->IsInstanceOf( |
| 1581 | FunctionTemplateInfo::cast(object_template->constructor()))); |
| 1582 | |
| 1583 | bool pending_exception = false; |
| 1584 | Handle<JSObject> obj = |
| 1585 | Execution::InstantiateObject(object_template, &pending_exception); |
| 1586 | if (pending_exception) { |
| 1587 | ASSERT(Top::has_pending_exception()); |
| 1588 | Top::clear_pending_exception(); |
| 1589 | return false; |
| 1590 | } |
| 1591 | TransferObject(obj, object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1592 | return true; |
| 1593 | } |
| 1594 | |
| 1595 | |
| 1596 | void Genesis::TransferNamedProperties(Handle<JSObject> from, |
| 1597 | Handle<JSObject> to) { |
| 1598 | if (from->HasFastProperties()) { |
| 1599 | Handle<DescriptorArray> descs = |
| 1600 | Handle<DescriptorArray>(from->map()->instance_descriptors()); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1601 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 1602 | PropertyDetails details = PropertyDetails(descs->GetDetails(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1603 | switch (details.type()) { |
| 1604 | case FIELD: { |
| 1605 | HandleScope inner; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1606 | Handle<String> key = Handle<String>(descs->GetKey(i)); |
| 1607 | int index = descs->GetFieldIndex(i); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1608 | Handle<Object> value = Handle<Object>(from->FastPropertyAt(index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1609 | SetProperty(to, key, value, details.attributes()); |
| 1610 | break; |
| 1611 | } |
| 1612 | case CONSTANT_FUNCTION: { |
| 1613 | HandleScope inner; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1614 | Handle<String> key = Handle<String>(descs->GetKey(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1615 | Handle<JSFunction> fun = |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1616 | Handle<JSFunction>(descs->GetConstantFunction(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1617 | SetProperty(to, key, fun, details.attributes()); |
| 1618 | break; |
| 1619 | } |
| 1620 | case CALLBACKS: { |
| 1621 | LookupResult result; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1622 | to->LocalLookup(descs->GetKey(i), &result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1623 | // If the property is already there we skip it |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1624 | if (result.IsProperty()) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1625 | HandleScope inner; |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1626 | ASSERT(!to->HasFastProperties()); |
| 1627 | // Add to dictionary. |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1628 | Handle<String> key = Handle<String>(descs->GetKey(i)); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1629 | Handle<Object> callbacks(descs->GetCallbacksObject(i)); |
| 1630 | PropertyDetails d = |
| 1631 | PropertyDetails(details.attributes(), CALLBACKS, details.index()); |
| 1632 | SetNormalizedProperty(to, key, callbacks, d); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1633 | break; |
| 1634 | } |
| 1635 | case MAP_TRANSITION: |
| 1636 | case CONSTANT_TRANSITION: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1637 | case NULL_DESCRIPTOR: |
| 1638 | // Ignore non-properties. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1639 | break; |
| 1640 | case NORMAL: |
| 1641 | // Do not occur since the from object has fast properties. |
| 1642 | case INTERCEPTOR: |
| 1643 | // No element in instance descriptors have interceptor type. |
| 1644 | UNREACHABLE(); |
| 1645 | break; |
| 1646 | } |
| 1647 | } |
| 1648 | } else { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1649 | Handle<StringDictionary> properties = |
| 1650 | Handle<StringDictionary>(from->property_dictionary()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1651 | int capacity = properties->Capacity(); |
| 1652 | for (int i = 0; i < capacity; i++) { |
| 1653 | Object* raw_key(properties->KeyAt(i)); |
| 1654 | if (properties->IsKey(raw_key)) { |
| 1655 | ASSERT(raw_key->IsString()); |
| 1656 | // If the property is already there we skip it. |
| 1657 | LookupResult result; |
| 1658 | to->LocalLookup(String::cast(raw_key), &result); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1659 | if (result.IsProperty()) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1660 | // Set the property. |
| 1661 | Handle<String> key = Handle<String>(String::cast(raw_key)); |
| 1662 | Handle<Object> value = Handle<Object>(properties->ValueAt(i)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1663 | if (value->IsJSGlobalPropertyCell()) { |
| 1664 | value = Handle<Object>(JSGlobalPropertyCell::cast(*value)->value()); |
| 1665 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1666 | PropertyDetails details = properties->DetailsAt(i); |
| 1667 | SetProperty(to, key, value, details.attributes()); |
| 1668 | } |
| 1669 | } |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | |
| 1674 | void Genesis::TransferIndexedProperties(Handle<JSObject> from, |
| 1675 | Handle<JSObject> to) { |
| 1676 | // Cloning the elements array is sufficient. |
| 1677 | Handle<FixedArray> from_elements = |
| 1678 | Handle<FixedArray>(FixedArray::cast(from->elements())); |
| 1679 | Handle<FixedArray> to_elements = Factory::CopyFixedArray(from_elements); |
| 1680 | to->set_elements(*to_elements); |
| 1681 | } |
| 1682 | |
| 1683 | |
| 1684 | void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) { |
| 1685 | HandleScope outer; |
| 1686 | |
| 1687 | ASSERT(!from->IsJSArray()); |
| 1688 | ASSERT(!to->IsJSArray()); |
| 1689 | |
| 1690 | TransferNamedProperties(from, to); |
| 1691 | TransferIndexedProperties(from, to); |
| 1692 | |
| 1693 | // Transfer the prototype (new map is needed). |
| 1694 | Handle<Map> old_to_map = Handle<Map>(to->map()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1695 | Handle<Map> new_to_map = Factory::CopyMapDropTransitions(old_to_map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1696 | new_to_map->set_prototype(from->map()->prototype()); |
| 1697 | to->set_map(*new_to_map); |
| 1698 | } |
| 1699 | |
| 1700 | |
| 1701 | void Genesis::MakeFunctionInstancePrototypeWritable() { |
| 1702 | // Make a new function map so all future functions |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1703 | // will have settable and enumerable prototype properties. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1704 | HandleScope scope; |
| 1705 | |
| 1706 | Handle<DescriptorArray> function_map_descriptors = |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1707 | ComputeFunctionInstanceDescriptor(ADD_WRITEABLE_PROTOTYPE); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 1708 | Handle<Map> fm = Factory::CopyMapDropDescriptors(Top::function_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1709 | fm->set_instance_descriptors(*function_map_descriptors); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1710 | fm->set_function_with_prototype(true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1711 | Top::context()->global_context()->set_function_map(*fm); |
| 1712 | } |
| 1713 | |
| 1714 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1715 | Genesis::Genesis(Handle<Object> global_object, |
| 1716 | v8::Handle<v8::ObjectTemplate> global_template, |
| 1717 | v8::ExtensionConfiguration* extensions) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1718 | result_ = Handle<Context>::null(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1719 | // If V8 isn't running and cannot be initialized, just return. |
| 1720 | if (!V8::IsRunning() && !V8::Initialize(NULL)) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1721 | |
| 1722 | // Before creating the roots we must save the context and restore it |
| 1723 | // on all function exits. |
| 1724 | HandleScope scope; |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1725 | SaveContext saved_context; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1726 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1727 | Handle<Context> new_context = Snapshot::NewContextFromSnapshot(); |
| 1728 | if (!new_context.is_null()) { |
| 1729 | global_context_ = |
| 1730 | Handle<Context>::cast(GlobalHandles::Create(*new_context)); |
| 1731 | Top::set_context(*global_context_); |
| 1732 | i::Counters::contexts_created_by_snapshot.Increment(); |
| 1733 | result_ = global_context_; |
| 1734 | JSFunction* empty_function = |
| 1735 | JSFunction::cast(result_->function_map()->prototype()); |
| 1736 | empty_function_ = Handle<JSFunction>(empty_function); |
| 1737 | Handle<GlobalObject> inner_global; |
| 1738 | Handle<JSGlobalProxy> global_proxy = |
| 1739 | CreateNewGlobals(global_template, |
| 1740 | global_object, |
| 1741 | &inner_global); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1742 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1743 | HookUpGlobalProxy(inner_global, global_proxy); |
| 1744 | HookUpInnerGlobal(inner_global); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1745 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1746 | if (!ConfigureGlobalObjects(global_template)) return; |
| 1747 | } else { |
| 1748 | // We get here if there was no context snapshot. |
| 1749 | CreateRoots(); |
| 1750 | Handle<JSFunction> empty_function = CreateEmptyFunction(); |
| 1751 | Handle<GlobalObject> inner_global; |
| 1752 | Handle<JSGlobalProxy> global_proxy = |
| 1753 | CreateNewGlobals(global_template, global_object, &inner_global); |
| 1754 | HookUpGlobalProxy(inner_global, global_proxy); |
| 1755 | InitializeGlobal(inner_global, empty_function); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1756 | InstallJSFunctionResultCaches(); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame^] | 1757 | if (!InstallNatives()) return; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1758 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1759 | MakeFunctionInstancePrototypeWritable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1760 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1761 | if (!ConfigureGlobalObjects(global_template)) return; |
| 1762 | i::Counters::contexts_created_from_scratch.Increment(); |
| 1763 | } |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 1764 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1765 | result_ = global_context_; |
| 1766 | } |
| 1767 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1768 | |
| 1769 | // Support for thread preemption. |
| 1770 | |
| 1771 | // Reserve space for statics needing saving and restoring. |
| 1772 | int Bootstrapper::ArchiveSpacePerThread() { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1773 | return BootstrapperActive::ArchiveSpacePerThread(); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | |
| 1777 | // Archive statics that are thread local. |
| 1778 | char* Bootstrapper::ArchiveState(char* to) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1779 | return BootstrapperActive::ArchiveState(to); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | |
| 1783 | // Restore statics that are thread local. |
| 1784 | char* Bootstrapper::RestoreState(char* from) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1785 | return BootstrapperActive::RestoreState(from); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1789 | // Called when the top-level V8 mutex is destroyed. |
| 1790 | void Bootstrapper::FreeThreadResources() { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1791 | ASSERT(!BootstrapperActive::IsActive()); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1795 | // Reserve space for statics needing saving and restoring. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1796 | int BootstrapperActive::ArchiveSpacePerThread() { |
| 1797 | return sizeof(nesting_); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | |
| 1801 | // Archive statics that are thread local. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1802 | char* BootstrapperActive::ArchiveState(char* to) { |
| 1803 | *reinterpret_cast<int*>(to) = nesting_; |
| 1804 | nesting_ = 0; |
| 1805 | return to + sizeof(nesting_); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
| 1808 | |
| 1809 | // Restore statics that are thread local. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1810 | char* BootstrapperActive::RestoreState(char* from) { |
| 1811 | nesting_ = *reinterpret_cast<int*>(from); |
| 1812 | return from + sizeof(nesting_); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1815 | } } // namespace v8::internal |