danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1 | // Copyright 2011 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 | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 39 | #include "objects-visiting.h" |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 40 | #include "snapshot.h" |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 41 | #include "extensions/externalize-string-extension.h" |
| 42 | #include "extensions/gc-extension.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 43 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 44 | namespace v8 { |
| 45 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 46 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 47 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 48 | NativesExternalStringResource::NativesExternalStringResource( |
| 49 | Bootstrapper* bootstrapper, |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 50 | const char* source, |
| 51 | size_t length) |
| 52 | : data_(source), length_(length) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 53 | if (bootstrapper->delete_these_non_arrays_on_tear_down_ == NULL) { |
| 54 | bootstrapper->delete_these_non_arrays_on_tear_down_ = new List<char*>(2); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 55 | } |
| 56 | // The resources are small objects and we only make a fixed number of |
| 57 | // them, but let's clean them up on exit for neatness. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 58 | bootstrapper->delete_these_non_arrays_on_tear_down_-> |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 59 | Add(reinterpret_cast<char*>(this)); |
| 60 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 61 | |
| 62 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 63 | Bootstrapper::Bootstrapper() |
| 64 | : nesting_(0), |
| 65 | extensions_cache_(Script::TYPE_EXTENSION), |
| 66 | delete_these_non_arrays_on_tear_down_(NULL), |
| 67 | delete_these_arrays_on_tear_down_(NULL) { |
| 68 | } |
| 69 | |
| 70 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 71 | Handle<String> Bootstrapper::NativesSourceLookup(int index) { |
| 72 | ASSERT(0 <= index && index < Natives::GetBuiltinsCount()); |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 73 | Isolate* isolate = Isolate::Current(); |
| 74 | Factory* factory = isolate->factory(); |
| 75 | Heap* heap = isolate->heap(); |
| 76 | if (heap->natives_source_cache()->get(index)->IsUndefined()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 77 | if (!Snapshot::IsEnabled() || FLAG_new_snapshot) { |
| 78 | // We can use external strings for the natives. |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 79 | Vector<const char> source = Natives::GetRawScriptSource(index); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 80 | NativesExternalStringResource* resource = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 81 | new NativesExternalStringResource(this, |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 82 | source.start(), |
| 83 | source.length()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 84 | Handle<String> source_code = |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 85 | factory->NewExternalStringFromAscii(resource); |
| 86 | heap->natives_source_cache()->set(index, *source_code); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 87 | } else { |
| 88 | // Old snapshot code can't cope with external strings at all. |
| 89 | Handle<String> source_code = |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 90 | factory->NewStringFromAscii(Natives::GetRawScriptSource(index)); |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 91 | heap->natives_source_cache()->set(index, *source_code); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 92 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 93 | } |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 94 | Handle<Object> cached_source(heap->natives_source_cache()->get(index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 95 | return Handle<String>::cast(cached_source); |
| 96 | } |
| 97 | |
| 98 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 99 | void Bootstrapper::Initialize(bool create_heap_objects) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 100 | extensions_cache_.Initialize(create_heap_objects); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 101 | GCExtension::Register(); |
| 102 | ExternalizeStringExtension::Register(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 106 | char* Bootstrapper::AllocateAutoDeletedArray(int bytes) { |
| 107 | char* memory = new char[bytes]; |
| 108 | if (memory != NULL) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 109 | if (delete_these_arrays_on_tear_down_ == NULL) { |
| 110 | delete_these_arrays_on_tear_down_ = new List<char*>(2); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 111 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 112 | delete_these_arrays_on_tear_down_->Add(memory); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 113 | } |
| 114 | return memory; |
| 115 | } |
| 116 | |
| 117 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 118 | void Bootstrapper::TearDown() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 119 | if (delete_these_non_arrays_on_tear_down_ != NULL) { |
| 120 | int len = delete_these_non_arrays_on_tear_down_->length(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 121 | ASSERT(len < 20); // Don't use this mechanism for unbounded allocations. |
| 122 | for (int i = 0; i < len; i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 123 | delete delete_these_non_arrays_on_tear_down_->at(i); |
| 124 | delete_these_non_arrays_on_tear_down_->at(i) = NULL; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 125 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 126 | delete delete_these_non_arrays_on_tear_down_; |
| 127 | delete_these_non_arrays_on_tear_down_ = NULL; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 128 | } |
| 129 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 130 | if (delete_these_arrays_on_tear_down_ != NULL) { |
| 131 | int len = delete_these_arrays_on_tear_down_->length(); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 132 | ASSERT(len < 1000); // Don't use this mechanism for unbounded allocations. |
| 133 | for (int i = 0; i < len; i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 134 | delete[] delete_these_arrays_on_tear_down_->at(i); |
| 135 | delete_these_arrays_on_tear_down_->at(i) = NULL; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 136 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 137 | delete delete_these_arrays_on_tear_down_; |
| 138 | delete_these_arrays_on_tear_down_ = NULL; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 139 | } |
| 140 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 141 | extensions_cache_.Initialize(false); // Yes, symmetrical |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 145 | class Genesis BASE_EMBEDDED { |
| 146 | public: |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 147 | Genesis(Isolate* isolate, |
| 148 | Handle<Object> global_object, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 149 | v8::Handle<v8::ObjectTemplate> global_template, |
| 150 | v8::ExtensionConfiguration* extensions); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 151 | ~Genesis() { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 152 | |
| 153 | Handle<Context> result() { return result_; } |
| 154 | |
| 155 | Genesis* previous() { return previous_; } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 156 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 157 | Isolate* isolate() const { return isolate_; } |
| 158 | Factory* factory() const { return isolate_->factory(); } |
| 159 | Heap* heap() const { return isolate_->heap(); } |
| 160 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 161 | private: |
| 162 | Handle<Context> global_context_; |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 163 | Isolate* isolate_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 164 | |
| 165 | // There may be more than one active genesis object: When GC is |
| 166 | // triggered during environment creation there may be weak handle |
| 167 | // processing callbacks which may create new environments. |
| 168 | Genesis* previous_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 169 | |
| 170 | Handle<Context> global_context() { return global_context_; } |
| 171 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 172 | // Creates some basic objects. Used for creating a context from scratch. |
| 173 | void CreateRoots(); |
| 174 | // Creates the empty function. Used for creating a context from scratch. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 175 | Handle<JSFunction> CreateEmptyFunction(Isolate* isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 176 | // Creates the ThrowTypeError function. ECMA 5th Ed. 13.2.3 |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 177 | Handle<JSFunction> GetThrowTypeErrorFunction(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 178 | |
| 179 | void CreateStrictModeFunctionMaps(Handle<JSFunction> empty); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 180 | // Creates the global objects using the global and the template passed in |
| 181 | // through the API. We call this regardless of whether we are building a |
| 182 | // context from scratch or using a deserialized one from the partial snapshot |
| 183 | // but in the latter case we don't use the objects it produces directly, as |
| 184 | // we have to used the deserialized ones that are linked together with the |
| 185 | // rest of the context snapshot. |
| 186 | Handle<JSGlobalProxy> CreateNewGlobals( |
| 187 | v8::Handle<v8::ObjectTemplate> global_template, |
| 188 | Handle<Object> global_object, |
| 189 | Handle<GlobalObject>* global_proxy_out); |
| 190 | // Hooks the given global proxy into the context. If the context was created |
| 191 | // by deserialization then this will unhook the global proxy that was |
| 192 | // deserialized, leaving the GC to pick it up. |
| 193 | void HookUpGlobalProxy(Handle<GlobalObject> inner_global, |
| 194 | Handle<JSGlobalProxy> global_proxy); |
| 195 | // Similarly, we want to use the inner global that has been created by the |
| 196 | // templates passed through the API. The inner global from the snapshot is |
| 197 | // detached from the other objects in the snapshot. |
| 198 | void HookUpInnerGlobal(Handle<GlobalObject> inner_global); |
| 199 | // New context initialization. Used for creating a context from scratch. |
| 200 | void InitializeGlobal(Handle<GlobalObject> inner_global, |
| 201 | Handle<JSFunction> empty_function); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 202 | void InitializeExperimentalGlobal(); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 203 | // Installs the contents of the native .js files on the global objects. |
| 204 | // Used for creating a context from scratch. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 205 | void InstallNativeFunctions(); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 206 | void InstallExperimentalNativeFunctions(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 207 | bool InstallNatives(); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 208 | bool InstallExperimentalNatives(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 209 | void InstallBuiltinFunctionIds(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 210 | void InstallJSFunctionResultCaches(); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 211 | void InitializeNormalizedMapCaches(); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 212 | // Used both for deserialized and from-scratch contexts to add the extensions |
| 213 | // provided. |
| 214 | static bool InstallExtensions(Handle<Context> global_context, |
| 215 | v8::ExtensionConfiguration* extensions); |
| 216 | static bool InstallExtension(const char* name); |
| 217 | static bool InstallExtension(v8::RegisteredExtension* current); |
| 218 | static void InstallSpecialObjects(Handle<Context> global_context); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 219 | bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 220 | bool ConfigureApiObject(Handle<JSObject> object, |
| 221 | Handle<ObjectTemplateInfo> object_template); |
| 222 | bool ConfigureGlobalObjects(v8::Handle<v8::ObjectTemplate> global_template); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 223 | |
| 224 | // Migrates all properties from the 'from' object to the 'to' |
| 225 | // object and overrides the prototype in 'to' with the one from |
| 226 | // 'from'. |
| 227 | void TransferObject(Handle<JSObject> from, Handle<JSObject> to); |
| 228 | void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to); |
| 229 | void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to); |
| 230 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 231 | enum PrototypePropertyMode { |
| 232 | DONT_ADD_PROTOTYPE, |
| 233 | ADD_READONLY_PROTOTYPE, |
| 234 | ADD_WRITEABLE_PROTOTYPE |
| 235 | }; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 236 | |
| 237 | Handle<Map> CreateFunctionMap(PrototypePropertyMode prototype_mode); |
| 238 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 239 | Handle<DescriptorArray> ComputeFunctionInstanceDescriptor( |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 240 | PrototypePropertyMode prototypeMode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 241 | void MakeFunctionInstancePrototypeWritable(); |
| 242 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 243 | Handle<Map> CreateStrictModeFunctionMap( |
| 244 | PrototypePropertyMode prototype_mode, |
| 245 | Handle<JSFunction> empty_function, |
| 246 | Handle<FixedArray> arguments_callbacks, |
| 247 | Handle<FixedArray> caller_callbacks); |
| 248 | |
| 249 | Handle<DescriptorArray> ComputeStrictFunctionInstanceDescriptor( |
| 250 | PrototypePropertyMode propertyMode, |
| 251 | Handle<FixedArray> arguments, |
| 252 | Handle<FixedArray> caller); |
| 253 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 254 | static bool CompileBuiltin(Isolate* isolate, int index); |
| 255 | static bool CompileExperimentalBuiltin(Isolate* isolate, int index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 256 | static bool CompileNative(Vector<const char> name, Handle<String> source); |
| 257 | static bool CompileScriptCached(Vector<const char> name, |
| 258 | Handle<String> source, |
| 259 | SourceCodeCache* cache, |
| 260 | v8::Extension* extension, |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 261 | Handle<Context> top_context, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 262 | bool use_runtime_context); |
| 263 | |
| 264 | Handle<Context> result_; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 265 | |
| 266 | // Function instance maps. Function literal maps are created initially with |
| 267 | // a read only prototype for the processing of JS builtins. Later the function |
| 268 | // instance maps are replaced in order to make prototype writable. |
| 269 | // These are the final, writable prototype, maps. |
| 270 | Handle<Map> function_instance_map_writable_prototype_; |
| 271 | Handle<Map> strict_mode_function_instance_map_writable_prototype_; |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 272 | Handle<JSFunction> throw_type_error_function; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 273 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 274 | BootstrapperActive active_; |
| 275 | friend class Bootstrapper; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 276 | }; |
| 277 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 278 | |
| 279 | void Bootstrapper::Iterate(ObjectVisitor* v) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 280 | extensions_cache_.Iterate(v); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 281 | v->Synchronize("Extensions"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 285 | Handle<Context> Bootstrapper::CreateEnvironment( |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 286 | Isolate* isolate, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 287 | Handle<Object> global_object, |
| 288 | v8::Handle<v8::ObjectTemplate> global_template, |
| 289 | v8::ExtensionConfiguration* extensions) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 290 | HandleScope scope; |
| 291 | Handle<Context> env; |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 292 | Genesis genesis(isolate, global_object, global_template, extensions); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 293 | env = genesis.result(); |
| 294 | if (!env.is_null()) { |
| 295 | if (InstallExtensions(env, extensions)) { |
| 296 | return env; |
| 297 | } |
| 298 | } |
| 299 | return Handle<Context>(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 303 | static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) { |
| 304 | // object.__proto__ = proto; |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 305 | Factory* factory = object->GetIsolate()->factory(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 306 | Handle<Map> old_to_map = Handle<Map>(object->map()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 307 | Handle<Map> new_to_map = factory->CopyMapDropTransitions(old_to_map); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 308 | new_to_map->set_prototype(*proto); |
| 309 | object->set_map(*new_to_map); |
| 310 | } |
| 311 | |
| 312 | |
| 313 | void Bootstrapper::DetachGlobal(Handle<Context> env) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 314 | Factory* factory = env->GetIsolate()->factory(); |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 315 | JSGlobalProxy::cast(env->global_proxy())->set_context(*factory->null_value()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 316 | SetObjectPrototype(Handle<JSObject>(env->global_proxy()), |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 317 | factory->null_value()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 318 | env->set_global_proxy(env->global()); |
| 319 | env->global()->set_global_receiver(env->global()); |
| 320 | } |
| 321 | |
| 322 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 323 | void Bootstrapper::ReattachGlobal(Handle<Context> env, |
| 324 | Handle<Object> global_object) { |
| 325 | ASSERT(global_object->IsJSGlobalProxy()); |
| 326 | Handle<JSGlobalProxy> global = Handle<JSGlobalProxy>::cast(global_object); |
| 327 | env->global()->set_global_receiver(*global); |
| 328 | env->set_global_proxy(*global); |
| 329 | SetObjectPrototype(global, Handle<JSObject>(env->global())); |
| 330 | global->set_context(*env); |
| 331 | } |
| 332 | |
| 333 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 334 | static Handle<JSFunction> InstallFunction(Handle<JSObject> target, |
| 335 | const char* name, |
| 336 | InstanceType type, |
| 337 | int instance_size, |
| 338 | Handle<JSObject> prototype, |
| 339 | Builtins::Name call, |
| 340 | bool is_ecma_native) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 341 | Isolate* isolate = target->GetIsolate(); |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 342 | Factory* factory = isolate->factory(); |
| 343 | Handle<String> symbol = factory->LookupAsciiSymbol(name); |
| 344 | Handle<Code> call_code = Handle<Code>(isolate->builtins()->builtin(call)); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 345 | Handle<JSFunction> function = prototype.is_null() ? |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 346 | factory->NewFunctionWithoutPrototype(symbol, call_code) : |
| 347 | factory->NewFunctionWithPrototype(symbol, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 348 | type, |
| 349 | instance_size, |
| 350 | prototype, |
| 351 | call_code, |
| 352 | is_ecma_native); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 353 | SetLocalPropertyNoThrow(target, symbol, function, DONT_ENUM); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 354 | if (is_ecma_native) { |
| 355 | function->shared()->set_instance_class_name(*symbol); |
| 356 | } |
| 357 | return function; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | Handle<DescriptorArray> Genesis::ComputeFunctionInstanceDescriptor( |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 362 | PrototypePropertyMode prototypeMode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 363 | Handle<DescriptorArray> descriptors = |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 364 | factory()->NewDescriptorArray(prototypeMode == DONT_ADD_PROTOTYPE |
| 365 | ? 4 |
| 366 | : 5); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 367 | PropertyAttributes attributes = |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 368 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 369 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 370 | { // Add length. |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 371 | Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionLength); |
| 372 | CallbacksDescriptor d(*factory()->length_symbol(), *foreign, attributes); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 373 | descriptors->Set(0, &d); |
| 374 | } |
| 375 | { // Add name. |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 376 | Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionName); |
| 377 | CallbacksDescriptor d(*factory()->name_symbol(), *foreign, attributes); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 378 | descriptors->Set(1, &d); |
| 379 | } |
| 380 | { // Add arguments. |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 381 | Handle<Foreign> foreign = |
| 382 | factory()->NewForeign(&Accessors::FunctionArguments); |
| 383 | CallbacksDescriptor d(*factory()->arguments_symbol(), *foreign, attributes); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 384 | descriptors->Set(2, &d); |
| 385 | } |
| 386 | { // Add caller. |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 387 | Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionCaller); |
| 388 | CallbacksDescriptor d(*factory()->caller_symbol(), *foreign, attributes); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 389 | descriptors->Set(3, &d); |
| 390 | } |
| 391 | if (prototypeMode != DONT_ADD_PROTOTYPE) { |
| 392 | // Add prototype. |
| 393 | if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) { |
| 394 | attributes = static_cast<PropertyAttributes>(attributes & ~READ_ONLY); |
| 395 | } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 396 | Handle<Foreign> foreign = |
| 397 | factory()->NewForeign(&Accessors::FunctionPrototype); |
| 398 | CallbacksDescriptor d(*factory()->prototype_symbol(), *foreign, attributes); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 399 | descriptors->Set(4, &d); |
| 400 | } |
| 401 | descriptors->Sort(); |
| 402 | return descriptors; |
| 403 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 404 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 405 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 406 | Handle<Map> Genesis::CreateFunctionMap(PrototypePropertyMode prototype_mode) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 407 | Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 408 | Handle<DescriptorArray> descriptors = |
| 409 | ComputeFunctionInstanceDescriptor(prototype_mode); |
| 410 | map->set_instance_descriptors(*descriptors); |
| 411 | map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE); |
| 412 | return map; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 416 | Handle<JSFunction> Genesis::CreateEmptyFunction(Isolate* isolate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 417 | // Allocate the map for function instances. Maps are allocated first and their |
| 418 | // prototypes patched later, once empty function is created. |
| 419 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 420 | // Please note that the prototype property for function instances must be |
| 421 | // writable. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 422 | Handle<Map> function_instance_map = |
| 423 | CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE); |
| 424 | global_context()->set_function_instance_map(*function_instance_map); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 425 | |
| 426 | // Functions with this map will not have a 'prototype' property, and |
| 427 | // can not be used as constructors. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 428 | Handle<Map> function_without_prototype_map = |
| 429 | CreateFunctionMap(DONT_ADD_PROTOTYPE); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 430 | global_context()->set_function_without_prototype_map( |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 431 | *function_without_prototype_map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 432 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 433 | // Allocate the function map. This map is temporary, used only for processing |
| 434 | // of builtins. |
| 435 | // Later the map is replaced with writable prototype map, allocated below. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 436 | Handle<Map> function_map = CreateFunctionMap(ADD_READONLY_PROTOTYPE); |
| 437 | global_context()->set_function_map(*function_map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 438 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 439 | // The final map for functions. Writeable prototype. |
| 440 | // This map is installed in MakeFunctionInstancePrototypeWritable. |
| 441 | function_instance_map_writable_prototype_ = |
| 442 | CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE); |
| 443 | |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 444 | Factory* factory = isolate->factory(); |
| 445 | Heap* heap = isolate->heap(); |
| 446 | |
| 447 | Handle<String> object_name = Handle<String>(heap->Object_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 448 | |
| 449 | { // --- O b j e c t --- |
| 450 | Handle<JSFunction> object_fun = |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 451 | factory->NewFunction(object_name, factory->null_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 452 | Handle<Map> object_function_map = |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 453 | factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 454 | object_fun->set_initial_map(*object_function_map); |
| 455 | object_function_map->set_constructor(*object_fun); |
| 456 | |
| 457 | global_context()->set_object_function(*object_fun); |
| 458 | |
| 459 | // Allocate a new prototype for the object function. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 460 | Handle<JSObject> prototype = factory->NewJSObject( |
| 461 | isolate->object_function(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 462 | TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 463 | |
| 464 | global_context()->set_initial_object_prototype(*prototype); |
| 465 | SetPrototype(object_fun, prototype); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 466 | object_function_map-> |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 467 | set_instance_descriptors(heap->empty_descriptor_array()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | // Allocate the empty function as the prototype for function ECMAScript |
| 471 | // 262 15.3.4. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 472 | Handle<String> symbol = factory->LookupAsciiSymbol("Empty"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 473 | Handle<JSFunction> empty_function = |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 474 | factory->NewFunctionWithoutPrototype(symbol, kNonStrictMode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 475 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 476 | // --- E m p t y --- |
| 477 | Handle<Code> code = |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 478 | Handle<Code>(isolate->builtins()->builtin( |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 479 | Builtins::kEmptyFunction)); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 480 | empty_function->set_code(*code); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 481 | empty_function->shared()->set_code(*code); |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 482 | Handle<String> source = factory->NewStringFromAscii(CStrVector("() {}")); |
| 483 | Handle<Script> script = factory->NewScript(source); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 484 | script->set_type(Smi::FromInt(Script::TYPE_NATIVE)); |
| 485 | empty_function->shared()->set_script(*script); |
| 486 | empty_function->shared()->set_start_position(0); |
| 487 | empty_function->shared()->set_end_position(source->length()); |
| 488 | empty_function->shared()->DontAdaptArguments(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 489 | |
| 490 | // Set prototypes for the function maps. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 491 | global_context()->function_map()->set_prototype(*empty_function); |
| 492 | global_context()->function_instance_map()->set_prototype(*empty_function); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 493 | global_context()->function_without_prototype_map()-> |
| 494 | set_prototype(*empty_function); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 495 | function_instance_map_writable_prototype_->set_prototype(*empty_function); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 496 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 497 | // Allocate the function map first and then patch the prototype later |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 498 | Handle<Map> empty_fm = factory->CopyMapDropDescriptors( |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 499 | function_without_prototype_map); |
| 500 | empty_fm->set_instance_descriptors( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 501 | function_without_prototype_map->instance_descriptors()); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 502 | empty_fm->set_prototype(global_context()->object_function()->prototype()); |
| 503 | empty_function->set_map(*empty_fm); |
| 504 | return empty_function; |
| 505 | } |
| 506 | |
| 507 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 508 | Handle<DescriptorArray> Genesis::ComputeStrictFunctionInstanceDescriptor( |
| 509 | PrototypePropertyMode prototypeMode, |
| 510 | Handle<FixedArray> arguments, |
| 511 | Handle<FixedArray> caller) { |
| 512 | Handle<DescriptorArray> descriptors = |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 513 | factory()->NewDescriptorArray(prototypeMode == DONT_ADD_PROTOTYPE |
| 514 | ? 4 |
| 515 | : 5); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 516 | PropertyAttributes attributes = static_cast<PropertyAttributes>( |
| 517 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 518 | |
| 519 | { // length |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 520 | Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionLength); |
| 521 | CallbacksDescriptor d(*factory()->length_symbol(), *foreign, attributes); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 522 | descriptors->Set(0, &d); |
| 523 | } |
| 524 | { // name |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 525 | Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionName); |
| 526 | CallbacksDescriptor d(*factory()->name_symbol(), *foreign, attributes); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 527 | descriptors->Set(1, &d); |
| 528 | } |
| 529 | { // arguments |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 530 | CallbacksDescriptor d(*factory()->arguments_symbol(), |
| 531 | *arguments, |
| 532 | attributes); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 533 | descriptors->Set(2, &d); |
| 534 | } |
| 535 | { // caller |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 536 | CallbacksDescriptor d(*factory()->caller_symbol(), *caller, attributes); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 537 | descriptors->Set(3, &d); |
| 538 | } |
| 539 | |
| 540 | // prototype |
| 541 | if (prototypeMode != DONT_ADD_PROTOTYPE) { |
| 542 | if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) { |
| 543 | attributes = static_cast<PropertyAttributes>(attributes & ~READ_ONLY); |
| 544 | } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 545 | Handle<Foreign> foreign = |
| 546 | factory()->NewForeign(&Accessors::FunctionPrototype); |
| 547 | CallbacksDescriptor d(*factory()->prototype_symbol(), *foreign, attributes); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 548 | descriptors->Set(4, &d); |
| 549 | } |
| 550 | |
| 551 | descriptors->Sort(); |
| 552 | return descriptors; |
| 553 | } |
| 554 | |
| 555 | |
| 556 | // ECMAScript 5th Edition, 13.2.3 |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 557 | Handle<JSFunction> Genesis::GetThrowTypeErrorFunction() { |
| 558 | if (throw_type_error_function.is_null()) { |
| 559 | Handle<String> name = factory()->LookupAsciiSymbol("ThrowTypeError"); |
| 560 | throw_type_error_function = |
| 561 | factory()->NewFunctionWithoutPrototype(name, kNonStrictMode); |
| 562 | Handle<Code> code(isolate()->builtins()->builtin( |
| 563 | Builtins::kStrictModePoisonPill)); |
| 564 | throw_type_error_function->set_map( |
| 565 | global_context()->function_map()); |
| 566 | throw_type_error_function->set_code(*code); |
| 567 | throw_type_error_function->shared()->set_code(*code); |
| 568 | throw_type_error_function->shared()->DontAdaptArguments(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 569 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 570 | PreventExtensions(throw_type_error_function); |
| 571 | } |
| 572 | return throw_type_error_function; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | |
| 576 | Handle<Map> Genesis::CreateStrictModeFunctionMap( |
| 577 | PrototypePropertyMode prototype_mode, |
| 578 | Handle<JSFunction> empty_function, |
| 579 | Handle<FixedArray> arguments_callbacks, |
| 580 | Handle<FixedArray> caller_callbacks) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 581 | Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 582 | Handle<DescriptorArray> descriptors = |
| 583 | ComputeStrictFunctionInstanceDescriptor(prototype_mode, |
| 584 | arguments_callbacks, |
| 585 | caller_callbacks); |
| 586 | map->set_instance_descriptors(*descriptors); |
| 587 | map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE); |
| 588 | map->set_prototype(*empty_function); |
| 589 | return map; |
| 590 | } |
| 591 | |
| 592 | |
| 593 | void Genesis::CreateStrictModeFunctionMaps(Handle<JSFunction> empty) { |
| 594 | // Create the callbacks arrays for ThrowTypeError functions. |
| 595 | // The get/set callacks are filled in after the maps are created below. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 596 | Factory* factory = empty->GetIsolate()->factory(); |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 597 | Handle<FixedArray> arguments = factory->NewFixedArray(2, TENURED); |
| 598 | Handle<FixedArray> caller = factory->NewFixedArray(2, TENURED); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 599 | |
| 600 | // Allocate map for the strict mode function instances. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 601 | Handle<Map> strict_mode_function_instance_map = |
| 602 | CreateStrictModeFunctionMap( |
| 603 | ADD_WRITEABLE_PROTOTYPE, empty, arguments, caller); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 604 | global_context()->set_strict_mode_function_instance_map( |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 605 | *strict_mode_function_instance_map); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 606 | |
| 607 | // Allocate map for the prototype-less strict mode instances. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 608 | Handle<Map> strict_mode_function_without_prototype_map = |
| 609 | CreateStrictModeFunctionMap( |
| 610 | DONT_ADD_PROTOTYPE, empty, arguments, caller); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 611 | global_context()->set_strict_mode_function_without_prototype_map( |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 612 | *strict_mode_function_without_prototype_map); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 613 | |
| 614 | // Allocate map for the strict mode functions. This map is temporary, used |
| 615 | // only for processing of builtins. |
| 616 | // Later the map is replaced with writable prototype map, allocated below. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 617 | Handle<Map> strict_mode_function_map = |
| 618 | CreateStrictModeFunctionMap( |
| 619 | ADD_READONLY_PROTOTYPE, empty, arguments, caller); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 620 | global_context()->set_strict_mode_function_map( |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 621 | *strict_mode_function_map); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 622 | |
| 623 | // The final map for the strict mode functions. Writeable prototype. |
| 624 | // This map is installed in MakeFunctionInstancePrototypeWritable. |
| 625 | strict_mode_function_instance_map_writable_prototype_ = |
| 626 | CreateStrictModeFunctionMap( |
| 627 | ADD_WRITEABLE_PROTOTYPE, empty, arguments, caller); |
| 628 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 629 | // Create the ThrowTypeError function instance. |
| 630 | Handle<JSFunction> throw_function = |
| 631 | GetThrowTypeErrorFunction(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 632 | |
| 633 | // Complete the callback fixed arrays. |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 634 | arguments->set(0, *throw_function); |
| 635 | arguments->set(1, *throw_function); |
| 636 | caller->set(0, *throw_function); |
| 637 | caller->set(1, *throw_function); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 641 | static void AddToWeakGlobalContextList(Context* context) { |
| 642 | ASSERT(context->IsGlobalContext()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 643 | Heap* heap = context->GetIsolate()->heap(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 644 | #ifdef DEBUG |
| 645 | { // NOLINT |
| 646 | ASSERT(context->get(Context::NEXT_CONTEXT_LINK)->IsUndefined()); |
| 647 | // Check that context is not in the list yet. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 648 | for (Object* current = heap->global_contexts_list(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 649 | !current->IsUndefined(); |
| 650 | current = Context::cast(current)->get(Context::NEXT_CONTEXT_LINK)) { |
| 651 | ASSERT(current != context); |
| 652 | } |
| 653 | } |
| 654 | #endif |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 655 | context->set(Context::NEXT_CONTEXT_LINK, heap->global_contexts_list()); |
| 656 | heap->set_global_contexts_list(context); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 660 | void Genesis::CreateRoots() { |
| 661 | // Allocate the global context FixedArray first and then patch the |
| 662 | // closure and extension object later (we need the empty function |
| 663 | // and the global object, but in order to create those, we need the |
| 664 | // global context). |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 665 | global_context_ = Handle<Context>::cast(isolate()->global_handles()->Create( |
| 666 | *factory()->NewGlobalContext())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 667 | AddToWeakGlobalContextList(*global_context_); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 668 | isolate()->set_context(*global_context()); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 669 | |
| 670 | // Allocate the message listeners object. |
| 671 | { |
| 672 | v8::NeanderArray listeners; |
| 673 | global_context()->set_message_listeners(*listeners.value()); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | |
| 678 | Handle<JSGlobalProxy> Genesis::CreateNewGlobals( |
| 679 | v8::Handle<v8::ObjectTemplate> global_template, |
| 680 | Handle<Object> global_object, |
| 681 | Handle<GlobalObject>* inner_global_out) { |
| 682 | // The argument global_template aka data is an ObjectTemplateInfo. |
| 683 | // It has a constructor pointer that points at global_constructor which is a |
| 684 | // FunctionTemplateInfo. |
| 685 | // The global_constructor is used to create or reinitialize the global_proxy. |
| 686 | // The global_constructor also has a prototype_template pointer that points at |
| 687 | // js_global_template which is an ObjectTemplateInfo. |
| 688 | // That in turn has a constructor pointer that points at |
| 689 | // js_global_constructor which is a FunctionTemplateInfo. |
| 690 | // js_global_constructor is used to make js_global_function |
| 691 | // js_global_function is used to make the new inner_global. |
| 692 | // |
| 693 | // --- G l o b a l --- |
| 694 | // Step 1: Create a fresh inner JSGlobalObject. |
| 695 | Handle<JSFunction> js_global_function; |
| 696 | Handle<ObjectTemplateInfo> js_global_template; |
| 697 | if (!global_template.IsEmpty()) { |
| 698 | // Get prototype template of the global_template. |
| 699 | Handle<ObjectTemplateInfo> data = |
| 700 | v8::Utils::OpenHandle(*global_template); |
| 701 | Handle<FunctionTemplateInfo> global_constructor = |
| 702 | Handle<FunctionTemplateInfo>( |
| 703 | FunctionTemplateInfo::cast(data->constructor())); |
| 704 | Handle<Object> proto_template(global_constructor->prototype_template()); |
| 705 | if (!proto_template->IsUndefined()) { |
| 706 | js_global_template = |
| 707 | Handle<ObjectTemplateInfo>::cast(proto_template); |
| 708 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 709 | } |
| 710 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 711 | if (js_global_template.is_null()) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 712 | Handle<String> name = Handle<String>(heap()->empty_symbol()); |
| 713 | Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin( |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 714 | Builtins::kIllegal)); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 715 | js_global_function = |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 716 | factory()->NewFunction(name, JS_GLOBAL_OBJECT_TYPE, |
| 717 | JSGlobalObject::kSize, code, true); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 718 | // Change the constructor property of the prototype of the |
| 719 | // hidden global function to refer to the Object function. |
| 720 | Handle<JSObject> prototype = |
| 721 | Handle<JSObject>( |
| 722 | JSObject::cast(js_global_function->instance_prototype())); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 723 | SetLocalPropertyNoThrow( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 724 | prototype, |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 725 | factory()->constructor_symbol(), |
| 726 | isolate()->object_function(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 727 | NONE); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 728 | } else { |
| 729 | Handle<FunctionTemplateInfo> js_global_constructor( |
| 730 | FunctionTemplateInfo::cast(js_global_template->constructor())); |
| 731 | js_global_function = |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 732 | factory()->CreateApiFunction(js_global_constructor, |
| 733 | factory()->InnerGlobalObject); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 734 | } |
| 735 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 736 | js_global_function->initial_map()->set_is_hidden_prototype(); |
| 737 | Handle<GlobalObject> inner_global = |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 738 | factory()->NewGlobalObject(js_global_function); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 739 | if (inner_global_out != NULL) { |
| 740 | *inner_global_out = inner_global; |
| 741 | } |
| 742 | |
| 743 | // Step 2: create or re-initialize the global proxy object. |
| 744 | Handle<JSFunction> global_proxy_function; |
| 745 | if (global_template.IsEmpty()) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 746 | Handle<String> name = Handle<String>(heap()->empty_symbol()); |
| 747 | Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin( |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 748 | Builtins::kIllegal)); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 749 | global_proxy_function = |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 750 | factory()->NewFunction(name, JS_GLOBAL_PROXY_TYPE, |
| 751 | JSGlobalProxy::kSize, code, true); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 752 | } else { |
| 753 | Handle<ObjectTemplateInfo> data = |
| 754 | v8::Utils::OpenHandle(*global_template); |
| 755 | Handle<FunctionTemplateInfo> global_constructor( |
| 756 | FunctionTemplateInfo::cast(data->constructor())); |
| 757 | global_proxy_function = |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 758 | factory()->CreateApiFunction(global_constructor, |
| 759 | factory()->OuterGlobalObject); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 760 | } |
| 761 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 762 | Handle<String> global_name = factory()->LookupAsciiSymbol("global"); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 763 | global_proxy_function->shared()->set_instance_class_name(*global_name); |
| 764 | global_proxy_function->initial_map()->set_is_access_check_needed(true); |
| 765 | |
| 766 | // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects |
| 767 | // Return the global proxy. |
| 768 | |
| 769 | if (global_object.location() != NULL) { |
| 770 | ASSERT(global_object->IsJSGlobalProxy()); |
| 771 | return ReinitializeJSGlobalProxy( |
| 772 | global_proxy_function, |
| 773 | Handle<JSGlobalProxy>::cast(global_object)); |
| 774 | } else { |
| 775 | return Handle<JSGlobalProxy>::cast( |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 776 | factory()->NewJSObject(global_proxy_function, TENURED)); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | |
| 780 | |
| 781 | void Genesis::HookUpGlobalProxy(Handle<GlobalObject> inner_global, |
| 782 | Handle<JSGlobalProxy> global_proxy) { |
| 783 | // Set the global context for the global object. |
| 784 | inner_global->set_global_context(*global_context()); |
| 785 | inner_global->set_global_receiver(*global_proxy); |
| 786 | global_proxy->set_context(*global_context()); |
| 787 | global_context()->set_global_proxy(*global_proxy); |
| 788 | } |
| 789 | |
| 790 | |
| 791 | void Genesis::HookUpInnerGlobal(Handle<GlobalObject> inner_global) { |
| 792 | Handle<GlobalObject> inner_global_from_snapshot( |
| 793 | GlobalObject::cast(global_context_->extension())); |
| 794 | Handle<JSBuiltinsObject> builtins_global(global_context_->builtins()); |
| 795 | global_context_->set_extension(*inner_global); |
| 796 | global_context_->set_global(*inner_global); |
| 797 | global_context_->set_security_token(*inner_global); |
| 798 | static const PropertyAttributes attributes = |
| 799 | static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE); |
| 800 | ForceSetProperty(builtins_global, |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 801 | factory()->LookupAsciiSymbol("global"), |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 802 | inner_global, |
| 803 | attributes); |
| 804 | // Setup the reference from the global object to the builtins object. |
| 805 | JSGlobalObject::cast(*inner_global)->set_builtins(*builtins_global); |
| 806 | TransferNamedProperties(inner_global_from_snapshot, inner_global); |
| 807 | TransferIndexedProperties(inner_global_from_snapshot, inner_global); |
| 808 | } |
| 809 | |
| 810 | |
| 811 | // This is only called if we are not using snapshots. The equivalent |
| 812 | // work in the snapshot case is done in HookUpInnerGlobal. |
| 813 | void Genesis::InitializeGlobal(Handle<GlobalObject> inner_global, |
| 814 | Handle<JSFunction> empty_function) { |
| 815 | // --- G l o b a l C o n t e x t --- |
| 816 | // Use the empty function as closure (no scope info). |
| 817 | global_context()->set_closure(*empty_function); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 818 | global_context()->set_previous(NULL); |
| 819 | // Set extension and global object. |
| 820 | global_context()->set_extension(*inner_global); |
| 821 | global_context()->set_global(*inner_global); |
| 822 | // Security setup: Set the security token of the global object to |
| 823 | // its the inner global. This makes the security check between two |
| 824 | // different contexts fail by default even in case of global |
| 825 | // object reinitialization. |
| 826 | global_context()->set_security_token(*inner_global); |
| 827 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 828 | Isolate* isolate = inner_global->GetIsolate(); |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 829 | Factory* factory = isolate->factory(); |
| 830 | Heap* heap = isolate->heap(); |
| 831 | |
| 832 | Handle<String> object_name = Handle<String>(heap->Object_symbol()); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 833 | SetLocalPropertyNoThrow(inner_global, object_name, |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 834 | isolate->object_function(), DONT_ENUM); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 835 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 836 | Handle<JSObject> global = Handle<JSObject>(global_context()->global()); |
| 837 | |
| 838 | // Install global Function object |
| 839 | InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize, |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 840 | empty_function, Builtins::kIllegal, true); // ECMA native. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 841 | |
| 842 | { // --- A r r a y --- |
| 843 | Handle<JSFunction> array_function = |
| 844 | InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize, |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 845 | isolate->initial_object_prototype(), |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 846 | Builtins::kArrayCode, true); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 847 | array_function->shared()->set_construct_stub( |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 848 | isolate->builtins()->builtin(Builtins::kArrayConstructCode)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 849 | array_function->shared()->DontAdaptArguments(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 850 | |
| 851 | // This seems a bit hackish, but we need to make sure Array.length |
| 852 | // is 1. |
| 853 | array_function->shared()->set_length(1); |
| 854 | Handle<DescriptorArray> array_descriptors = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 855 | factory->CopyAppendForeignDescriptor( |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 856 | factory->empty_descriptor_array(), |
| 857 | factory->length_symbol(), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 858 | factory->NewForeign(&Accessors::ArrayLength), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 859 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE)); |
| 860 | |
| 861 | // Cache the fast JavaScript array map |
| 862 | global_context()->set_js_array_map(array_function->initial_map()); |
| 863 | global_context()->js_array_map()->set_instance_descriptors( |
| 864 | *array_descriptors); |
| 865 | // array_function is used internally. JS code creating array object should |
| 866 | // search for the 'Array' property on the global object and use that one |
| 867 | // as the constructor. 'Array' property on a global object can be |
| 868 | // overwritten by JS code. |
| 869 | global_context()->set_array_function(*array_function); |
| 870 | } |
| 871 | |
| 872 | { // --- N u m b e r --- |
| 873 | Handle<JSFunction> number_fun = |
| 874 | InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize, |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 875 | isolate->initial_object_prototype(), |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 876 | Builtins::kIllegal, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 877 | global_context()->set_number_function(*number_fun); |
| 878 | } |
| 879 | |
| 880 | { // --- B o o l e a n --- |
| 881 | Handle<JSFunction> boolean_fun = |
| 882 | InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize, |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 883 | isolate->initial_object_prototype(), |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 884 | Builtins::kIllegal, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 885 | global_context()->set_boolean_function(*boolean_fun); |
| 886 | } |
| 887 | |
| 888 | { // --- S t r i n g --- |
| 889 | Handle<JSFunction> string_fun = |
| 890 | InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize, |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 891 | isolate->initial_object_prototype(), |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 892 | Builtins::kIllegal, true); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 893 | string_fun->shared()->set_construct_stub( |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 894 | isolate->builtins()->builtin(Builtins::kStringConstructCode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 895 | global_context()->set_string_function(*string_fun); |
| 896 | // Add 'length' property to strings. |
| 897 | Handle<DescriptorArray> string_descriptors = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 898 | factory->CopyAppendForeignDescriptor( |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 899 | factory->empty_descriptor_array(), |
| 900 | factory->length_symbol(), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 901 | factory->NewForeign(&Accessors::StringLength), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 902 | static_cast<PropertyAttributes>(DONT_ENUM | |
| 903 | DONT_DELETE | |
| 904 | READ_ONLY)); |
| 905 | |
| 906 | Handle<Map> string_map = |
| 907 | Handle<Map>(global_context()->string_function()->initial_map()); |
| 908 | string_map->set_instance_descriptors(*string_descriptors); |
| 909 | } |
| 910 | |
| 911 | { // --- D a t e --- |
| 912 | // Builtin functions for Date.prototype. |
| 913 | Handle<JSFunction> date_fun = |
| 914 | InstallFunction(global, "Date", JS_VALUE_TYPE, JSValue::kSize, |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 915 | isolate->initial_object_prototype(), |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 916 | Builtins::kIllegal, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 917 | |
| 918 | global_context()->set_date_function(*date_fun); |
| 919 | } |
| 920 | |
| 921 | |
| 922 | { // -- R e g E x p |
| 923 | // Builtin functions for RegExp.prototype. |
| 924 | Handle<JSFunction> regexp_fun = |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 925 | InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize, |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 926 | isolate->initial_object_prototype(), |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 927 | Builtins::kIllegal, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 928 | global_context()->set_regexp_function(*regexp_fun); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 929 | |
| 930 | ASSERT(regexp_fun->has_initial_map()); |
| 931 | Handle<Map> initial_map(regexp_fun->initial_map()); |
| 932 | |
| 933 | ASSERT_EQ(0, initial_map->inobject_properties()); |
| 934 | |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 935 | Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(5); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 936 | PropertyAttributes final = |
| 937 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 938 | int enum_index = 0; |
| 939 | { |
| 940 | // ECMA-262, section 15.10.7.1. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 941 | FieldDescriptor field(heap->source_symbol(), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 942 | JSRegExp::kSourceFieldIndex, |
| 943 | final, |
| 944 | enum_index++); |
| 945 | descriptors->Set(0, &field); |
| 946 | } |
| 947 | { |
| 948 | // ECMA-262, section 15.10.7.2. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 949 | FieldDescriptor field(heap->global_symbol(), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 950 | JSRegExp::kGlobalFieldIndex, |
| 951 | final, |
| 952 | enum_index++); |
| 953 | descriptors->Set(1, &field); |
| 954 | } |
| 955 | { |
| 956 | // ECMA-262, section 15.10.7.3. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 957 | FieldDescriptor field(heap->ignore_case_symbol(), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 958 | JSRegExp::kIgnoreCaseFieldIndex, |
| 959 | final, |
| 960 | enum_index++); |
| 961 | descriptors->Set(2, &field); |
| 962 | } |
| 963 | { |
| 964 | // ECMA-262, section 15.10.7.4. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 965 | FieldDescriptor field(heap->multiline_symbol(), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 966 | JSRegExp::kMultilineFieldIndex, |
| 967 | final, |
| 968 | enum_index++); |
| 969 | descriptors->Set(3, &field); |
| 970 | } |
| 971 | { |
| 972 | // ECMA-262, section 15.10.7.5. |
| 973 | PropertyAttributes writable = |
| 974 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE); |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 975 | FieldDescriptor field(heap->last_index_symbol(), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 976 | JSRegExp::kLastIndexFieldIndex, |
| 977 | writable, |
| 978 | enum_index++); |
| 979 | descriptors->Set(4, &field); |
| 980 | } |
| 981 | descriptors->SetNextEnumerationIndex(enum_index); |
| 982 | descriptors->Sort(); |
| 983 | |
| 984 | initial_map->set_inobject_properties(5); |
| 985 | initial_map->set_pre_allocated_property_fields(5); |
| 986 | initial_map->set_unused_property_fields(0); |
| 987 | initial_map->set_instance_size( |
| 988 | initial_map->instance_size() + 5 * kPointerSize); |
| 989 | initial_map->set_instance_descriptors(*descriptors); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 990 | initial_map->set_visitor_id(StaticVisitorBase::GetVisitorId(*initial_map)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 991 | } |
| 992 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 993 | { // -- J S O N |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 994 | Handle<String> name = factory->NewStringFromAscii(CStrVector("JSON")); |
| 995 | Handle<JSFunction> cons = factory->NewFunction( |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 996 | name, |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 997 | factory->the_hole_value()); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 998 | cons->SetInstancePrototype(global_context()->initial_object_prototype()); |
| 999 | cons->SetInstanceClassName(*name); |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1000 | Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 1001 | ASSERT(json_object->IsJSObject()); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1002 | SetLocalPropertyNoThrow(global, name, json_object, DONT_ENUM); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 1003 | global_context()->set_json_object(*json_object); |
| 1004 | } |
| 1005 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1006 | { // --- arguments_boilerplate_ |
| 1007 | // Make sure we can recognize argument objects at runtime. |
| 1008 | // This is done by introducing an anonymous function with |
| 1009 | // class_name equals 'Arguments'. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1010 | Handle<String> symbol = factory->LookupAsciiSymbol("Arguments"); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1011 | Handle<Code> code = Handle<Code>( |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1012 | isolate->builtins()->builtin(Builtins::kIllegal)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1013 | Handle<JSObject> prototype = |
| 1014 | Handle<JSObject>( |
| 1015 | JSObject::cast(global_context()->object_function()->prototype())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1016 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1017 | Handle<JSFunction> function = |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1018 | factory->NewFunctionWithPrototype(symbol, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1019 | JS_OBJECT_TYPE, |
| 1020 | JSObject::kHeaderSize, |
| 1021 | prototype, |
| 1022 | code, |
| 1023 | false); |
| 1024 | ASSERT(!function->has_initial_map()); |
| 1025 | function->shared()->set_instance_class_name(*symbol); |
| 1026 | function->shared()->set_expected_nof_properties(2); |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1027 | Handle<JSObject> result = factory->NewJSObject(function); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1028 | |
| 1029 | global_context()->set_arguments_boilerplate(*result); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1030 | // Note: length must be added as the first property and |
| 1031 | // callee must be added as the second property. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1032 | SetLocalPropertyNoThrow(result, factory->length_symbol(), |
| 1033 | factory->undefined_value(), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1034 | DONT_ENUM); |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1035 | SetLocalPropertyNoThrow(result, factory->callee_symbol(), |
| 1036 | factory->undefined_value(), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1037 | DONT_ENUM); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1038 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1039 | #ifdef DEBUG |
| 1040 | LookupResult lookup; |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1041 | result->LocalLookup(heap->callee_symbol(), &lookup); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1042 | ASSERT(lookup.IsProperty() && (lookup.type() == FIELD)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1043 | ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsCalleeIndex); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1044 | |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1045 | result->LocalLookup(heap->length_symbol(), &lookup); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1046 | ASSERT(lookup.IsProperty() && (lookup.type() == FIELD)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1047 | ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1048 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1049 | ASSERT(result->map()->inobject_properties() > Heap::kArgumentsCalleeIndex); |
| 1050 | ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex); |
| 1051 | |
| 1052 | // Check the state of the object. |
| 1053 | ASSERT(result->HasFastProperties()); |
| 1054 | ASSERT(result->HasFastElements()); |
| 1055 | #endif |
| 1056 | } |
| 1057 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1058 | { // --- aliased_arguments_boilerplate_ |
| 1059 | Handle<Map> old_map(global_context()->arguments_boilerplate()->map()); |
| 1060 | Handle<Map> new_map = factory->CopyMapDropTransitions(old_map); |
| 1061 | new_map->set_pre_allocated_property_fields(2); |
| 1062 | Handle<JSObject> result = factory->NewJSObjectFromMap(new_map); |
| 1063 | new_map->set_elements_kind(JSObject::NON_STRICT_ARGUMENTS_ELEMENTS); |
| 1064 | // Set up a well-formed parameter map to make assertions happy. |
| 1065 | Handle<FixedArray> elements = factory->NewFixedArray(2); |
| 1066 | elements->set_map(heap->non_strict_arguments_elements_map()); |
| 1067 | Handle<FixedArray> array; |
| 1068 | array = factory->NewFixedArray(0); |
| 1069 | elements->set(0, *array); |
| 1070 | array = factory->NewFixedArray(0); |
| 1071 | elements->set(1, *array); |
| 1072 | result->set_elements(*elements); |
| 1073 | global_context()->set_aliased_arguments_boilerplate(*result); |
| 1074 | } |
| 1075 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1076 | { // --- strict mode arguments boilerplate |
| 1077 | const PropertyAttributes attributes = |
| 1078 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 1079 | |
| 1080 | // Create the ThrowTypeError functions. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1081 | Handle<FixedArray> callee = factory->NewFixedArray(2, TENURED); |
| 1082 | Handle<FixedArray> caller = factory->NewFixedArray(2, TENURED); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1083 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 1084 | Handle<JSFunction> throw_function = |
| 1085 | GetThrowTypeErrorFunction(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1086 | |
| 1087 | // Install the ThrowTypeError functions. |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 1088 | callee->set(0, *throw_function); |
| 1089 | callee->set(1, *throw_function); |
| 1090 | caller->set(0, *throw_function); |
| 1091 | caller->set(1, *throw_function); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1092 | |
| 1093 | // Create the descriptor array for the arguments object. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1094 | Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1095 | { // length |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1096 | FieldDescriptor d(*factory->length_symbol(), 0, DONT_ENUM); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1097 | descriptors->Set(0, &d); |
| 1098 | } |
| 1099 | { // callee |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1100 | CallbacksDescriptor d(*factory->callee_symbol(), *callee, attributes); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1101 | descriptors->Set(1, &d); |
| 1102 | } |
| 1103 | { // caller |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1104 | CallbacksDescriptor d(*factory->caller_symbol(), *caller, attributes); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1105 | descriptors->Set(2, &d); |
| 1106 | } |
| 1107 | descriptors->Sort(); |
| 1108 | |
| 1109 | // Create the map. Allocate one in-object field for length. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1110 | Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1111 | Heap::kArgumentsObjectSizeStrict); |
| 1112 | map->set_instance_descriptors(*descriptors); |
| 1113 | map->set_function_with_prototype(true); |
| 1114 | map->set_prototype(global_context()->object_function()->prototype()); |
| 1115 | map->set_pre_allocated_property_fields(1); |
| 1116 | map->set_inobject_properties(1); |
| 1117 | |
| 1118 | // Copy constructor from the non-strict arguments boilerplate. |
| 1119 | map->set_constructor( |
| 1120 | global_context()->arguments_boilerplate()->map()->constructor()); |
| 1121 | |
| 1122 | // Allocate the arguments boilerplate object. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1123 | Handle<JSObject> result = factory->NewJSObjectFromMap(map); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1124 | global_context()->set_strict_mode_arguments_boilerplate(*result); |
| 1125 | |
| 1126 | // Add length property only for strict mode boilerplate. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1127 | SetLocalPropertyNoThrow(result, factory->length_symbol(), |
| 1128 | factory->undefined_value(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1129 | DONT_ENUM); |
| 1130 | |
| 1131 | #ifdef DEBUG |
| 1132 | LookupResult lookup; |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1133 | result->LocalLookup(heap->length_symbol(), &lookup); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1134 | ASSERT(lookup.IsProperty() && (lookup.type() == FIELD)); |
| 1135 | ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex); |
| 1136 | |
| 1137 | ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1138 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1139 | // Check the state of the object. |
| 1140 | ASSERT(result->HasFastProperties()); |
| 1141 | ASSERT(result->HasFastElements()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1142 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | { // --- context extension |
| 1146 | // Create a function for the context extension objects. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1147 | Handle<Code> code = Handle<Code>( |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1148 | isolate->builtins()->builtin(Builtins::kIllegal)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1149 | Handle<JSFunction> context_extension_fun = |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1150 | factory->NewFunction(factory->empty_symbol(), |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1151 | JS_CONTEXT_EXTENSION_OBJECT_TYPE, |
| 1152 | JSObject::kHeaderSize, |
| 1153 | code, |
| 1154 | true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1155 | |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1156 | Handle<String> name = factory->LookupAsciiSymbol("context_extension"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1157 | context_extension_fun->shared()->set_instance_class_name(*name); |
| 1158 | global_context()->set_context_extension_function(*context_extension_fun); |
| 1159 | } |
| 1160 | |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 1161 | |
| 1162 | { |
| 1163 | // Setup the call-as-function delegate. |
| 1164 | Handle<Code> code = |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1165 | Handle<Code>(isolate->builtins()->builtin( |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1166 | Builtins::kHandleApiCallAsFunction)); |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 1167 | Handle<JSFunction> delegate = |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1168 | factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE, |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 1169 | JSObject::kHeaderSize, code, true); |
| 1170 | global_context()->set_call_as_function_delegate(*delegate); |
| 1171 | delegate->shared()->DontAdaptArguments(); |
| 1172 | } |
| 1173 | |
| 1174 | { |
| 1175 | // Setup the call-as-constructor delegate. |
| 1176 | Handle<Code> code = |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1177 | Handle<Code>(isolate->builtins()->builtin( |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1178 | Builtins::kHandleApiCallAsConstructor)); |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 1179 | Handle<JSFunction> delegate = |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1180 | factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE, |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 1181 | JSObject::kHeaderSize, code, true); |
| 1182 | global_context()->set_call_as_constructor_delegate(*delegate); |
| 1183 | delegate->shared()->DontAdaptArguments(); |
| 1184 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1185 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1186 | // Initialize the out of memory slot. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1187 | global_context()->set_out_of_memory(heap->false_value()); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1188 | |
| 1189 | // Initialize the data slot. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1190 | global_context()->set_data(heap->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1194 | void Genesis::InitializeExperimentalGlobal() { |
| 1195 | Isolate* isolate = this->isolate(); |
| 1196 | Handle<JSObject> global = Handle<JSObject>(global_context()->global()); |
| 1197 | |
| 1198 | // TODO(mstarzinger): Move this into Genesis::InitializeGlobal once we no |
| 1199 | // longer need to live behind a flag, so WeakMap gets added to the snapshot. |
| 1200 | if (FLAG_harmony_weakmaps) { // -- W e a k M a p |
| 1201 | Handle<JSFunction> weakmap_fun = |
| 1202 | InstallFunction(global, "WeakMap", JS_WEAK_MAP_TYPE, JSWeakMap::kSize, |
| 1203 | isolate->initial_object_prototype(), |
| 1204 | Builtins::kIllegal, true); |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1209 | bool Genesis::CompileBuiltin(Isolate* isolate, int index) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1210 | Vector<const char> name = Natives::GetScriptName(index); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1211 | Handle<String> source_code = |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1212 | isolate->bootstrapper()->NativesSourceLookup(index); |
| 1213 | return CompileNative(name, source_code); |
| 1214 | } |
| 1215 | |
| 1216 | |
| 1217 | bool Genesis::CompileExperimentalBuiltin(Isolate* isolate, int index) { |
| 1218 | Vector<const char> name = ExperimentalNatives::GetScriptName(index); |
| 1219 | Factory* factory = isolate->factory(); |
| 1220 | Handle<String> source_code = |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 1221 | factory->NewStringFromAscii( |
| 1222 | ExperimentalNatives::GetRawScriptSource(index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1223 | return CompileNative(name, source_code); |
| 1224 | } |
| 1225 | |
| 1226 | |
| 1227 | bool Genesis::CompileNative(Vector<const char> name, Handle<String> source) { |
| 1228 | HandleScope scope; |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1229 | Isolate* isolate = source->GetIsolate(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1230 | #ifdef ENABLE_DEBUGGER_SUPPORT |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1231 | isolate->debugger()->set_compiling_natives(true); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1232 | #endif |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1233 | bool result = CompileScriptCached(name, |
| 1234 | source, |
| 1235 | NULL, |
| 1236 | NULL, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1237 | Handle<Context>(isolate->context()), |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1238 | true); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1239 | ASSERT(isolate->has_pending_exception() != result); |
| 1240 | if (!result) isolate->clear_pending_exception(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1241 | #ifdef ENABLE_DEBUGGER_SUPPORT |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1242 | isolate->debugger()->set_compiling_natives(false); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1243 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1244 | return result; |
| 1245 | } |
| 1246 | |
| 1247 | |
| 1248 | bool Genesis::CompileScriptCached(Vector<const char> name, |
| 1249 | Handle<String> source, |
| 1250 | SourceCodeCache* cache, |
| 1251 | v8::Extension* extension, |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1252 | Handle<Context> top_context, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1253 | bool use_runtime_context) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1254 | Factory* factory = source->GetIsolate()->factory(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1255 | HandleScope scope; |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1256 | Handle<SharedFunctionInfo> function_info; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1257 | |
| 1258 | // If we can't find the function in the cache, we compile a new |
| 1259 | // function and insert it into the cache. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1260 | if (cache == NULL || !cache->Lookup(name, &function_info)) { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 1261 | ASSERT(source->IsAsciiRepresentation()); |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1262 | Handle<String> script_name = factory->NewStringFromUtf8(name); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1263 | function_info = Compiler::Compile( |
| 1264 | source, |
| 1265 | script_name, |
| 1266 | 0, |
| 1267 | 0, |
| 1268 | extension, |
| 1269 | NULL, |
| 1270 | Handle<String>::null(), |
| 1271 | use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE); |
| 1272 | if (function_info.is_null()) return false; |
| 1273 | if (cache != NULL) cache->Add(name, function_info); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | // Setup the function context. Conceptually, we should clone the |
| 1277 | // function before overwriting the context but since we're in a |
| 1278 | // single-threaded environment it is not strictly necessary. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1279 | ASSERT(top_context->IsGlobalContext()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1280 | Handle<Context> context = |
| 1281 | Handle<Context>(use_runtime_context |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1282 | ? Handle<Context>(top_context->runtime_context()) |
| 1283 | : top_context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1284 | Handle<JSFunction> fun = |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1285 | factory->NewFunctionFromSharedFunctionInfo(function_info, context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1286 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1287 | // Call function using either the runtime object or the global |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1288 | // object as the receiver. Provide no parameters. |
| 1289 | Handle<Object> receiver = |
| 1290 | Handle<Object>(use_runtime_context |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1291 | ? top_context->builtins() |
| 1292 | : top_context->global()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1293 | bool has_pending_exception; |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 1294 | Execution::Call(fun, receiver, 0, NULL, &has_pending_exception); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1295 | if (has_pending_exception) return false; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1296 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1300 | #define INSTALL_NATIVE(Type, name, var) \ |
| 1301 | Handle<String> var##_name = factory()->LookupAsciiSymbol(name); \ |
| 1302 | Object* var##_native = \ |
| 1303 | global_context()->builtins()->GetPropertyNoExceptionThrown( \ |
| 1304 | *var##_name); \ |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1305 | global_context()->set_##var(Type::cast(var##_native)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1306 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1307 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1308 | void Genesis::InstallNativeFunctions() { |
| 1309 | HandleScope scope; |
| 1310 | INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun); |
| 1311 | INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun); |
| 1312 | INSTALL_NATIVE(JSFunction, "ToString", to_string_fun); |
| 1313 | INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun); |
| 1314 | INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun); |
| 1315 | INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun); |
| 1316 | INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun); |
| 1317 | INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1318 | INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1319 | INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun); |
| 1320 | INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance", |
| 1321 | configure_instance_fun); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1322 | INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun); |
| 1323 | INSTALL_NATIVE(JSObject, "functionCache", function_cache); |
| 1324 | } |
| 1325 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1326 | void Genesis::InstallExperimentalNativeFunctions() { |
| 1327 | if (FLAG_harmony_proxies) { |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 1328 | INSTALL_NATIVE(JSFunction, "DerivedHasTrap", derived_has_trap); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1329 | INSTALL_NATIVE(JSFunction, "DerivedGetTrap", derived_get_trap); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1330 | INSTALL_NATIVE(JSFunction, "DerivedSetTrap", derived_set_trap); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1331 | } |
| 1332 | } |
| 1333 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1334 | #undef INSTALL_NATIVE |
| 1335 | |
| 1336 | |
| 1337 | bool Genesis::InstallNatives() { |
| 1338 | HandleScope scope; |
| 1339 | |
| 1340 | // Create a function for the builtins object. Allocate space for the |
| 1341 | // JavaScript builtins, a reference to the builtins object |
| 1342 | // (itself) and a reference to the global_context directly in the object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1343 | Handle<Code> code = Handle<Code>( |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1344 | isolate()->builtins()->builtin(Builtins::kIllegal)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1345 | Handle<JSFunction> builtins_fun = |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1346 | factory()->NewFunction(factory()->empty_symbol(), |
| 1347 | JS_BUILTINS_OBJECT_TYPE, |
| 1348 | JSBuiltinsObject::kSize, code, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1349 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1350 | Handle<String> name = factory()->LookupAsciiSymbol("builtins"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1351 | builtins_fun->shared()->set_instance_class_name(*name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1352 | |
| 1353 | // Allocate the builtins object. |
| 1354 | Handle<JSBuiltinsObject> builtins = |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1355 | Handle<JSBuiltinsObject>::cast(factory()->NewGlobalObject(builtins_fun)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1356 | builtins->set_builtins(*builtins); |
| 1357 | builtins->set_global_context(*global_context()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1358 | builtins->set_global_receiver(*builtins); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1359 | |
| 1360 | // Setup the 'global' properties of the builtins object. The |
| 1361 | // 'global' property that refers to the global object is the only |
| 1362 | // way to get from code running in the builtins context to the |
| 1363 | // global object. |
| 1364 | static const PropertyAttributes attributes = |
| 1365 | static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1366 | Handle<String> global_symbol = factory()->LookupAsciiSymbol("global"); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1367 | Handle<Object> global_obj(global_context()->global()); |
| 1368 | SetLocalPropertyNoThrow(builtins, global_symbol, global_obj, attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1369 | |
| 1370 | // Setup the reference from the global object to the builtins object. |
| 1371 | JSGlobalObject::cast(global_context()->global())->set_builtins(*builtins); |
| 1372 | |
| 1373 | // Create a bridge function that has context in the global context. |
| 1374 | Handle<JSFunction> bridge = |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1375 | factory()->NewFunction(factory()->empty_symbol(), |
| 1376 | factory()->undefined_value()); |
| 1377 | ASSERT(bridge->context() == *isolate()->global_context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1378 | |
| 1379 | // Allocate the builtins context. |
| 1380 | Handle<Context> context = |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1381 | factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1382 | context->set_global(*builtins); // override builtins global object |
| 1383 | |
| 1384 | global_context()->set_runtime_context(*context); |
| 1385 | |
| 1386 | { // -- S c r i p t |
| 1387 | // Builtin functions for Script. |
| 1388 | Handle<JSFunction> script_fun = |
| 1389 | InstallFunction(builtins, "Script", JS_VALUE_TYPE, JSValue::kSize, |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1390 | isolate()->initial_object_prototype(), |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1391 | Builtins::kIllegal, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1392 | Handle<JSObject> prototype = |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1393 | factory()->NewJSObject(isolate()->object_function(), TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1394 | SetPrototype(script_fun, prototype); |
| 1395 | global_context()->set_script_function(*script_fun); |
| 1396 | |
| 1397 | // Add 'source' and 'data' property to scripts. |
| 1398 | PropertyAttributes common_attributes = |
| 1399 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1400 | Handle<Foreign> foreign_source = |
| 1401 | factory()->NewForeign(&Accessors::ScriptSource); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1402 | Handle<DescriptorArray> script_descriptors = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1403 | factory()->CopyAppendForeignDescriptor( |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1404 | factory()->empty_descriptor_array(), |
| 1405 | factory()->LookupAsciiSymbol("source"), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1406 | foreign_source, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1407 | common_attributes); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1408 | Handle<Foreign> foreign_name = |
| 1409 | factory()->NewForeign(&Accessors::ScriptName); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1410 | script_descriptors = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1411 | factory()->CopyAppendForeignDescriptor( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1412 | script_descriptors, |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1413 | factory()->LookupAsciiSymbol("name"), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1414 | foreign_name, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1415 | common_attributes); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1416 | Handle<Foreign> foreign_id = factory()->NewForeign(&Accessors::ScriptId); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1417 | script_descriptors = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1418 | factory()->CopyAppendForeignDescriptor( |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1419 | script_descriptors, |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1420 | factory()->LookupAsciiSymbol("id"), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1421 | foreign_id, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1422 | common_attributes); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1423 | Handle<Foreign> foreign_line_offset = |
| 1424 | factory()->NewForeign(&Accessors::ScriptLineOffset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1425 | script_descriptors = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1426 | factory()->CopyAppendForeignDescriptor( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1427 | script_descriptors, |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1428 | factory()->LookupAsciiSymbol("line_offset"), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1429 | foreign_line_offset, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1430 | common_attributes); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1431 | Handle<Foreign> foreign_column_offset = |
| 1432 | factory()->NewForeign(&Accessors::ScriptColumnOffset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1433 | script_descriptors = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1434 | factory()->CopyAppendForeignDescriptor( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1435 | script_descriptors, |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1436 | factory()->LookupAsciiSymbol("column_offset"), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1437 | foreign_column_offset, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1438 | common_attributes); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1439 | Handle<Foreign> foreign_data = |
| 1440 | factory()->NewForeign(&Accessors::ScriptData); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1441 | script_descriptors = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1442 | factory()->CopyAppendForeignDescriptor( |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1443 | script_descriptors, |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1444 | factory()->LookupAsciiSymbol("data"), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1445 | foreign_data, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1446 | common_attributes); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1447 | Handle<Foreign> foreign_type = |
| 1448 | factory()->NewForeign(&Accessors::ScriptType); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1449 | script_descriptors = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1450 | factory()->CopyAppendForeignDescriptor( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1451 | script_descriptors, |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1452 | factory()->LookupAsciiSymbol("type"), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1453 | foreign_type, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1454 | common_attributes); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1455 | Handle<Foreign> foreign_compilation_type = |
| 1456 | factory()->NewForeign(&Accessors::ScriptCompilationType); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1457 | script_descriptors = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1458 | factory()->CopyAppendForeignDescriptor( |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1459 | script_descriptors, |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1460 | factory()->LookupAsciiSymbol("compilation_type"), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1461 | foreign_compilation_type, |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1462 | common_attributes); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1463 | Handle<Foreign> foreign_line_ends = |
| 1464 | factory()->NewForeign(&Accessors::ScriptLineEnds); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1465 | script_descriptors = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1466 | factory()->CopyAppendForeignDescriptor( |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1467 | script_descriptors, |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1468 | factory()->LookupAsciiSymbol("line_ends"), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1469 | foreign_line_ends, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1470 | common_attributes); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1471 | Handle<Foreign> foreign_context_data = |
| 1472 | factory()->NewForeign(&Accessors::ScriptContextData); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1473 | script_descriptors = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1474 | factory()->CopyAppendForeignDescriptor( |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1475 | script_descriptors, |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1476 | factory()->LookupAsciiSymbol("context_data"), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1477 | foreign_context_data, |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1478 | common_attributes); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1479 | Handle<Foreign> foreign_eval_from_script = |
| 1480 | factory()->NewForeign(&Accessors::ScriptEvalFromScript); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1481 | script_descriptors = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1482 | factory()->CopyAppendForeignDescriptor( |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1483 | script_descriptors, |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1484 | factory()->LookupAsciiSymbol("eval_from_script"), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1485 | foreign_eval_from_script, |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1486 | common_attributes); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1487 | Handle<Foreign> foreign_eval_from_script_position = |
| 1488 | factory()->NewForeign(&Accessors::ScriptEvalFromScriptPosition); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1489 | script_descriptors = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1490 | factory()->CopyAppendForeignDescriptor( |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1491 | script_descriptors, |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1492 | factory()->LookupAsciiSymbol("eval_from_script_position"), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1493 | foreign_eval_from_script_position, |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 1494 | common_attributes); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1495 | Handle<Foreign> foreign_eval_from_function_name = |
| 1496 | factory()->NewForeign(&Accessors::ScriptEvalFromFunctionName); |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 1497 | script_descriptors = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1498 | factory()->CopyAppendForeignDescriptor( |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 1499 | script_descriptors, |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1500 | factory()->LookupAsciiSymbol("eval_from_function_name"), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1501 | foreign_eval_from_function_name, |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1502 | common_attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1503 | |
| 1504 | Handle<Map> script_map = Handle<Map>(script_fun->initial_map()); |
| 1505 | script_map->set_instance_descriptors(*script_descriptors); |
| 1506 | |
| 1507 | // Allocate the empty script. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1508 | Handle<Script> script = factory()->NewScript(factory()->empty_string()); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1509 | script->set_type(Smi::FromInt(Script::TYPE_NATIVE)); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1510 | heap()->public_set_empty_script(*script); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1511 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1512 | { |
| 1513 | // Builtin function for OpaqueReference -- a JSValue-based object, |
| 1514 | // that keeps its field isolated from JavaScript code. It may store |
| 1515 | // objects, that JavaScript code may not access. |
| 1516 | Handle<JSFunction> opaque_reference_fun = |
| 1517 | InstallFunction(builtins, "OpaqueReference", JS_VALUE_TYPE, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1518 | JSValue::kSize, |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1519 | isolate()->initial_object_prototype(), |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1520 | Builtins::kIllegal, false); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1521 | Handle<JSObject> prototype = |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1522 | factory()->NewJSObject(isolate()->object_function(), TENURED); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1523 | SetPrototype(opaque_reference_fun, prototype); |
| 1524 | global_context()->set_opaque_reference_function(*opaque_reference_fun); |
| 1525 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1526 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1527 | { // --- I n t e r n a l A r r a y --- |
| 1528 | // An array constructor on the builtins object that works like |
| 1529 | // the public Array constructor, except that its prototype |
| 1530 | // doesn't inherit from Object.prototype. |
| 1531 | // To be used only for internal work by builtins. Instances |
| 1532 | // must not be leaked to user code. |
| 1533 | // Only works correctly when called as a constructor. The normal |
| 1534 | // Array code uses Array.prototype as prototype when called as |
| 1535 | // a function. |
| 1536 | Handle<JSFunction> array_function = |
| 1537 | InstallFunction(builtins, |
| 1538 | "InternalArray", |
| 1539 | JS_ARRAY_TYPE, |
| 1540 | JSArray::kSize, |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1541 | isolate()->initial_object_prototype(), |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1542 | Builtins::kArrayCode, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1543 | true); |
| 1544 | Handle<JSObject> prototype = |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1545 | factory()->NewJSObject(isolate()->object_function(), TENURED); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1546 | SetPrototype(array_function, prototype); |
| 1547 | |
| 1548 | array_function->shared()->set_construct_stub( |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1549 | isolate()->builtins()->builtin(Builtins::kArrayConstructCode)); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1550 | array_function->shared()->DontAdaptArguments(); |
| 1551 | |
| 1552 | // Make "length" magic on instances. |
| 1553 | Handle<DescriptorArray> array_descriptors = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1554 | factory()->CopyAppendForeignDescriptor( |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1555 | factory()->empty_descriptor_array(), |
| 1556 | factory()->length_symbol(), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1557 | factory()->NewForeign(&Accessors::ArrayLength), |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1558 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE)); |
| 1559 | |
| 1560 | array_function->initial_map()->set_instance_descriptors( |
| 1561 | *array_descriptors); |
| 1562 | } |
| 1563 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1564 | if (FLAG_disable_native_files) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1565 | PrintF("Warning: Running without installed natives!\n"); |
| 1566 | return true; |
| 1567 | } |
| 1568 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1569 | // Install natives. |
| 1570 | for (int i = Natives::GetDebuggerCount(); |
| 1571 | i < Natives::GetBuiltinsCount(); |
| 1572 | i++) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1573 | if (!CompileBuiltin(isolate(), i)) return false; |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1574 | // TODO(ager): We really only need to install the JS builtin |
| 1575 | // functions on the builtins object after compiling and running |
| 1576 | // runtime.js. |
| 1577 | if (!InstallJSBuiltins(builtins)) return false; |
| 1578 | } |
| 1579 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1580 | InstallNativeFunctions(); |
| 1581 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1582 | // Store the map for the string prototype after the natives has been compiled |
| 1583 | // and the String function has been setup. |
| 1584 | Handle<JSFunction> string_function(global_context()->string_function()); |
| 1585 | ASSERT(JSObject::cast( |
| 1586 | string_function->initial_map()->prototype())->HasFastProperties()); |
| 1587 | global_context()->set_string_function_prototype_map( |
| 1588 | HeapObject::cast(string_function->initial_map()->prototype())->map()); |
| 1589 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1590 | // Install Function.prototype.call and apply. |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1591 | { Handle<String> key = factory()->function_class_symbol(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1592 | Handle<JSFunction> function = |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1593 | Handle<JSFunction>::cast(GetProperty(isolate()->global(), key)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1594 | Handle<JSObject> proto = |
| 1595 | Handle<JSObject>(JSObject::cast(function->instance_prototype())); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1596 | |
| 1597 | // Install the call and the apply functions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1598 | Handle<JSFunction> call = |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1599 | InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize, |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1600 | Handle<JSObject>::null(), |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1601 | Builtins::kFunctionCall, |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1602 | false); |
| 1603 | Handle<JSFunction> apply = |
| 1604 | InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize, |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1605 | Handle<JSObject>::null(), |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1606 | Builtins::kFunctionApply, |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1607 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1608 | |
| 1609 | // Make sure that Function.prototype.call appears to be compiled. |
| 1610 | // The code will never be called, but inline caching for call will |
| 1611 | // only work if it appears to be compiled. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1612 | call->shared()->DontAdaptArguments(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1613 | ASSERT(call->is_compiled()); |
| 1614 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1615 | // Set the expected parameters for apply to 2; required by builtin. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1616 | apply->shared()->set_formal_parameter_count(2); |
| 1617 | |
| 1618 | // Set the lengths for the functions to satisfy ECMA-262. |
| 1619 | call->shared()->set_length(1); |
| 1620 | apply->shared()->set_length(2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1621 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1622 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame^] | 1623 | InstallBuiltinFunctionIds(); |
| 1624 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1625 | // Create a constructor for RegExp results (a variant of Array that |
| 1626 | // predefines the two properties index and match). |
| 1627 | { |
| 1628 | // RegExpResult initial map. |
| 1629 | |
| 1630 | // Find global.Array.prototype to inherit from. |
| 1631 | Handle<JSFunction> array_constructor(global_context()->array_function()); |
| 1632 | Handle<JSObject> array_prototype( |
| 1633 | JSObject::cast(array_constructor->instance_prototype())); |
| 1634 | |
| 1635 | // Add initial map. |
| 1636 | Handle<Map> initial_map = |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1637 | factory()->NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1638 | initial_map->set_constructor(*array_constructor); |
| 1639 | |
| 1640 | // Set prototype on map. |
| 1641 | initial_map->set_non_instance_prototype(false); |
| 1642 | initial_map->set_prototype(*array_prototype); |
| 1643 | |
| 1644 | // Update map with length accessor from Array and add "index" and "input". |
| 1645 | Handle<Map> array_map(global_context()->js_array_map()); |
| 1646 | Handle<DescriptorArray> array_descriptors( |
| 1647 | array_map->instance_descriptors()); |
| 1648 | ASSERT_EQ(1, array_descriptors->number_of_descriptors()); |
| 1649 | |
| 1650 | Handle<DescriptorArray> reresult_descriptors = |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1651 | factory()->NewDescriptorArray(3); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1652 | |
| 1653 | reresult_descriptors->CopyFrom(0, *array_descriptors, 0); |
| 1654 | |
| 1655 | int enum_index = 0; |
| 1656 | { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1657 | FieldDescriptor index_field(heap()->index_symbol(), |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1658 | JSRegExpResult::kIndexIndex, |
| 1659 | NONE, |
| 1660 | enum_index++); |
| 1661 | reresult_descriptors->Set(1, &index_field); |
| 1662 | } |
| 1663 | |
| 1664 | { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1665 | FieldDescriptor input_field(heap()->input_symbol(), |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1666 | JSRegExpResult::kInputIndex, |
| 1667 | NONE, |
| 1668 | enum_index++); |
| 1669 | reresult_descriptors->Set(2, &input_field); |
| 1670 | } |
| 1671 | reresult_descriptors->Sort(); |
| 1672 | |
| 1673 | initial_map->set_inobject_properties(2); |
| 1674 | initial_map->set_pre_allocated_property_fields(2); |
| 1675 | initial_map->set_unused_property_fields(0); |
| 1676 | initial_map->set_instance_descriptors(*reresult_descriptors); |
| 1677 | |
| 1678 | global_context()->set_regexp_result_map(*initial_map); |
| 1679 | } |
| 1680 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1681 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1682 | #ifdef DEBUG |
| 1683 | builtins->Verify(); |
| 1684 | #endif |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1685 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1686 | return true; |
| 1687 | } |
| 1688 | |
| 1689 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1690 | bool Genesis::InstallExperimentalNatives() { |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 1691 | for (int i = ExperimentalNatives::GetDebuggerCount(); |
| 1692 | i < ExperimentalNatives::GetBuiltinsCount(); |
| 1693 | i++) { |
| 1694 | if (FLAG_harmony_proxies && |
| 1695 | strcmp(ExperimentalNatives::GetScriptName(i).start(), |
| 1696 | "native proxy.js") == 0) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1697 | if (!CompileExperimentalBuiltin(isolate(), i)) return false; |
| 1698 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1699 | if (FLAG_harmony_weakmaps && |
| 1700 | strcmp(ExperimentalNatives::GetScriptName(i).start(), |
| 1701 | "native weakmap.js") == 0) { |
| 1702 | if (!CompileExperimentalBuiltin(isolate(), i)) return false; |
| 1703 | } |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1704 | } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1705 | |
| 1706 | InstallExperimentalNativeFunctions(); |
| 1707 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1708 | return true; |
| 1709 | } |
| 1710 | |
| 1711 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1712 | static Handle<JSObject> ResolveBuiltinIdHolder( |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 1713 | Handle<Context> global_context, |
| 1714 | const char* holder_expr) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1715 | Factory* factory = global_context->GetIsolate()->factory(); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 1716 | Handle<GlobalObject> global(global_context->global()); |
| 1717 | const char* period_pos = strchr(holder_expr, '.'); |
| 1718 | if (period_pos == NULL) { |
| 1719 | return Handle<JSObject>::cast( |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1720 | GetProperty(global, factory->LookupAsciiSymbol(holder_expr))); |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 1721 | } |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 1722 | ASSERT_EQ(".prototype", period_pos); |
| 1723 | Vector<const char> property(holder_expr, |
| 1724 | static_cast<int>(period_pos - holder_expr)); |
| 1725 | Handle<JSFunction> function = Handle<JSFunction>::cast( |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1726 | GetProperty(global, factory->LookupSymbol(property))); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 1727 | return Handle<JSObject>(JSObject::cast(function->prototype())); |
| 1728 | } |
| 1729 | |
| 1730 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1731 | static void InstallBuiltinFunctionId(Handle<JSObject> holder, |
| 1732 | const char* function_name, |
| 1733 | BuiltinFunctionId id) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1734 | Factory* factory = holder->GetIsolate()->factory(); |
| 1735 | Handle<String> name = factory->LookupAsciiSymbol(function_name); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1736 | Object* function_object = holder->GetProperty(*name)->ToObjectUnchecked(); |
| 1737 | Handle<JSFunction> function(JSFunction::cast(function_object)); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1738 | function->shared()->set_function_data(Smi::FromInt(id)); |
| 1739 | } |
| 1740 | |
| 1741 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1742 | void Genesis::InstallBuiltinFunctionIds() { |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1743 | HandleScope scope; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1744 | #define INSTALL_BUILTIN_ID(holder_expr, fun_name, name) \ |
| 1745 | { \ |
| 1746 | Handle<JSObject> holder = ResolveBuiltinIdHolder( \ |
| 1747 | global_context(), #holder_expr); \ |
| 1748 | BuiltinFunctionId id = k##name; \ |
| 1749 | InstallBuiltinFunctionId(holder, #fun_name, id); \ |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1750 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1751 | FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID) |
| 1752 | #undef INSTALL_BUILTIN_ID |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1756 | // Do not forget to update macros.py with named constant |
| 1757 | // of cache id. |
| 1758 | #define JSFUNCTION_RESULT_CACHE_LIST(F) \ |
| 1759 | F(16, global_context()->regexp_function()) |
| 1760 | |
| 1761 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 1762 | static FixedArray* CreateCache(int size, Handle<JSFunction> factory_function) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1763 | Factory* factory = factory_function->GetIsolate()->factory(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1764 | // Caches are supposed to live for a long time, allocate in old space. |
| 1765 | int array_size = JSFunctionResultCache::kEntriesIndex + 2 * size; |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1766 | // Cannot use cast as object is not fully initialized yet. |
| 1767 | JSFunctionResultCache* cache = reinterpret_cast<JSFunctionResultCache*>( |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1768 | *factory->NewFixedArrayWithHoles(array_size, TENURED)); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 1769 | cache->set(JSFunctionResultCache::kFactoryIndex, *factory_function); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1770 | cache->MakeZeroSize(); |
| 1771 | return cache; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | |
| 1775 | void Genesis::InstallJSFunctionResultCaches() { |
| 1776 | const int kNumberOfCaches = 0 + |
| 1777 | #define F(size, func) + 1 |
| 1778 | JSFUNCTION_RESULT_CACHE_LIST(F) |
| 1779 | #undef F |
| 1780 | ; |
| 1781 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1782 | Handle<FixedArray> caches = FACTORY->NewFixedArray(kNumberOfCaches, TENURED); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1783 | |
| 1784 | int index = 0; |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1785 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 1786 | #define F(size, func) do { \ |
| 1787 | FixedArray* cache = CreateCache((size), Handle<JSFunction>(func)); \ |
| 1788 | caches->set(index++, cache); \ |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1789 | } while (false) |
| 1790 | |
| 1791 | JSFUNCTION_RESULT_CACHE_LIST(F); |
| 1792 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1793 | #undef F |
| 1794 | |
| 1795 | global_context()->set_jsfunction_result_caches(*caches); |
| 1796 | } |
| 1797 | |
| 1798 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1799 | void Genesis::InitializeNormalizedMapCaches() { |
| 1800 | Handle<FixedArray> array( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1801 | FACTORY->NewFixedArray(NormalizedMapCache::kEntries, TENURED)); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1802 | global_context()->set_normalized_map_cache(NormalizedMapCache::cast(*array)); |
| 1803 | } |
| 1804 | |
| 1805 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1806 | bool Bootstrapper::InstallExtensions(Handle<Context> global_context, |
| 1807 | v8::ExtensionConfiguration* extensions) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1808 | Isolate* isolate = global_context->GetIsolate(); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1809 | BootstrapperActive active; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1810 | SaveContext saved_context(isolate); |
| 1811 | isolate->set_context(*global_context); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1812 | if (!Genesis::InstallExtensions(global_context, extensions)) return false; |
| 1813 | Genesis::InstallSpecialObjects(global_context); |
| 1814 | return true; |
| 1815 | } |
| 1816 | |
| 1817 | |
| 1818 | void Genesis::InstallSpecialObjects(Handle<Context> global_context) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1819 | Factory* factory = global_context->GetIsolate()->factory(); |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 1820 | HandleScope scope; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1821 | Handle<JSGlobalObject> js_global( |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1822 | JSGlobalObject::cast(global_context->global())); |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 1823 | // Expose the natives in global if a name for it is specified. |
| 1824 | if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) { |
| 1825 | Handle<String> natives_string = |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1826 | factory->LookupAsciiSymbol(FLAG_expose_natives_as); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1827 | SetLocalPropertyNoThrow(js_global, natives_string, |
| 1828 | Handle<JSObject>(js_global->builtins()), DONT_ENUM); |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1831 | Handle<Object> Error = GetProperty(js_global, "Error"); |
| 1832 | if (Error->IsJSObject()) { |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1833 | Handle<String> name = factory->LookupAsciiSymbol("stackTraceLimit"); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1834 | SetLocalPropertyNoThrow(Handle<JSObject>::cast(Error), |
| 1835 | name, |
| 1836 | Handle<Smi>(Smi::FromInt(FLAG_stack_trace_limit)), |
| 1837 | NONE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1838 | } |
| 1839 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1840 | #ifdef ENABLE_DEBUGGER_SUPPORT |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 1841 | // Expose the debug global object in global if a name for it is specified. |
| 1842 | if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1843 | Debug* debug = Isolate::Current()->debug(); |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 1844 | // If loading fails we just bail out without installing the |
| 1845 | // debugger but without tanking the whole context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1846 | if (!debug->Load()) return; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1847 | // Set the security token for the debugger context to the same as |
| 1848 | // the shell global context to allow calling between these (otherwise |
| 1849 | // exposing debug global object doesn't make much sense). |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1850 | debug->debug_context()->set_security_token( |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1851 | global_context->security_token()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1852 | |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 1853 | Handle<String> debug_string = |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1854 | factory->LookupAsciiSymbol(FLAG_expose_debug_as); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1855 | Handle<Object> global_proxy(debug->debug_context()->global_proxy()); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1856 | SetLocalPropertyNoThrow(js_global, debug_string, global_proxy, DONT_ENUM); |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 1857 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1858 | #endif |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1862 | bool Genesis::InstallExtensions(Handle<Context> global_context, |
| 1863 | v8::ExtensionConfiguration* extensions) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1864 | // TODO(isolates): Extensions on multiple isolates may take a little more |
| 1865 | // effort. (The external API reads 'ignore'-- does that mean |
| 1866 | // we can break the interface?) |
| 1867 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1868 | // Clear coloring of extension list |
| 1869 | v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension(); |
| 1870 | while (current != NULL) { |
| 1871 | current->set_state(v8::UNVISITED); |
| 1872 | current = current->next(); |
| 1873 | } |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1874 | // Install auto extensions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1875 | current = v8::RegisteredExtension::first_extension(); |
| 1876 | while (current != NULL) { |
| 1877 | if (current->extension()->auto_enable()) |
| 1878 | InstallExtension(current); |
| 1879 | current = current->next(); |
| 1880 | } |
| 1881 | |
| 1882 | if (FLAG_expose_gc) InstallExtension("v8/gc"); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 1883 | if (FLAG_expose_externalize_string) InstallExtension("v8/externalize"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1884 | |
| 1885 | if (extensions == NULL) return true; |
| 1886 | // Install required extensions |
| 1887 | int count = v8::ImplementationUtilities::GetNameCount(extensions); |
| 1888 | const char** names = v8::ImplementationUtilities::GetNames(extensions); |
| 1889 | for (int i = 0; i < count; i++) { |
| 1890 | if (!InstallExtension(names[i])) |
| 1891 | return false; |
| 1892 | } |
| 1893 | |
| 1894 | return true; |
| 1895 | } |
| 1896 | |
| 1897 | |
| 1898 | // Installs a named extension. This methods is unoptimized and does |
| 1899 | // not scale well if we want to support a large number of extensions. |
| 1900 | bool Genesis::InstallExtension(const char* name) { |
| 1901 | v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension(); |
| 1902 | // Loop until we find the relevant extension |
| 1903 | while (current != NULL) { |
| 1904 | if (strcmp(name, current->extension()->name()) == 0) break; |
| 1905 | current = current->next(); |
| 1906 | } |
| 1907 | // Didn't find the extension; fail. |
| 1908 | if (current == NULL) { |
| 1909 | v8::Utils::ReportApiFailure( |
| 1910 | "v8::Context::New()", "Cannot find required extension"); |
| 1911 | return false; |
| 1912 | } |
| 1913 | return InstallExtension(current); |
| 1914 | } |
| 1915 | |
| 1916 | |
| 1917 | bool Genesis::InstallExtension(v8::RegisteredExtension* current) { |
| 1918 | HandleScope scope; |
| 1919 | |
| 1920 | if (current->state() == v8::INSTALLED) return true; |
| 1921 | // The current node has already been visited so there must be a |
| 1922 | // cycle in the dependency graph; fail. |
| 1923 | if (current->state() == v8::VISITED) { |
| 1924 | v8::Utils::ReportApiFailure( |
| 1925 | "v8::Context::New()", "Circular extension dependency"); |
| 1926 | return false; |
| 1927 | } |
| 1928 | ASSERT(current->state() == v8::UNVISITED); |
| 1929 | current->set_state(v8::VISITED); |
| 1930 | v8::Extension* extension = current->extension(); |
| 1931 | // Install the extension's dependencies |
| 1932 | for (int i = 0; i < extension->dependency_count(); i++) { |
| 1933 | if (!InstallExtension(extension->dependencies()[i])) return false; |
| 1934 | } |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1935 | Isolate* isolate = Isolate::Current(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1936 | Vector<const char> source = CStrVector(extension->source()); |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1937 | Handle<String> source_code = isolate->factory()->NewStringFromAscii(source); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1938 | bool result = CompileScriptCached(CStrVector(extension->name()), |
| 1939 | source_code, |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1940 | isolate->bootstrapper()->extensions_cache(), |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1941 | extension, |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1942 | Handle<Context>(isolate->context()), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1943 | false); |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1944 | ASSERT(isolate->has_pending_exception() != result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1945 | if (!result) { |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1946 | isolate->clear_pending_exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1947 | } |
| 1948 | current->set_state(v8::INSTALLED); |
| 1949 | return result; |
| 1950 | } |
| 1951 | |
| 1952 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1953 | bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) { |
| 1954 | HandleScope scope; |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1955 | Factory* factory = builtins->GetIsolate()->factory(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1956 | for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) { |
| 1957 | Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 1958 | Handle<String> name = factory->LookupAsciiSymbol(Builtins::GetName(id)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1959 | Object* function_object = builtins->GetPropertyNoExceptionThrown(*name); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1960 | Handle<JSFunction> function |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1961 | = Handle<JSFunction>(JSFunction::cast(function_object)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1962 | builtins->set_javascript_builtin(id, *function); |
| 1963 | Handle<SharedFunctionInfo> shared |
| 1964 | = Handle<SharedFunctionInfo>(function->shared()); |
| 1965 | if (!EnsureCompiled(shared, CLEAR_EXCEPTION)) return false; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1966 | // Set the code object on the function object. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1967 | function->ReplaceCode(function->shared()->code()); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1968 | builtins->set_javascript_builtin_code(id, shared->code()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1969 | } |
| 1970 | return true; |
| 1971 | } |
| 1972 | |
| 1973 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1974 | bool Genesis::ConfigureGlobalObjects( |
| 1975 | v8::Handle<v8::ObjectTemplate> global_proxy_template) { |
| 1976 | Handle<JSObject> global_proxy( |
| 1977 | JSObject::cast(global_context()->global_proxy())); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1978 | Handle<JSObject> inner_global(JSObject::cast(global_context()->global())); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1979 | |
| 1980 | if (!global_proxy_template.IsEmpty()) { |
| 1981 | // Configure the global proxy object. |
| 1982 | Handle<ObjectTemplateInfo> proxy_data = |
| 1983 | v8::Utils::OpenHandle(*global_proxy_template); |
| 1984 | if (!ConfigureApiObject(global_proxy, proxy_data)) return false; |
| 1985 | |
| 1986 | // Configure the inner global object. |
| 1987 | Handle<FunctionTemplateInfo> proxy_constructor( |
| 1988 | FunctionTemplateInfo::cast(proxy_data->constructor())); |
| 1989 | if (!proxy_constructor->prototype_template()->IsUndefined()) { |
| 1990 | Handle<ObjectTemplateInfo> inner_data( |
| 1991 | ObjectTemplateInfo::cast(proxy_constructor->prototype_template())); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1992 | if (!ConfigureApiObject(inner_global, inner_data)) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1993 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1994 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1995 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1996 | SetObjectPrototype(global_proxy, inner_global); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1997 | return true; |
| 1998 | } |
| 1999 | |
| 2000 | |
| 2001 | bool Genesis::ConfigureApiObject(Handle<JSObject> object, |
| 2002 | Handle<ObjectTemplateInfo> object_template) { |
| 2003 | ASSERT(!object_template.is_null()); |
| 2004 | ASSERT(object->IsInstanceOf( |
| 2005 | FunctionTemplateInfo::cast(object_template->constructor()))); |
| 2006 | |
| 2007 | bool pending_exception = false; |
| 2008 | Handle<JSObject> obj = |
| 2009 | Execution::InstantiateObject(object_template, &pending_exception); |
| 2010 | if (pending_exception) { |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2011 | ASSERT(isolate()->has_pending_exception()); |
| 2012 | isolate()->clear_pending_exception(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2013 | return false; |
| 2014 | } |
| 2015 | TransferObject(obj, object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2016 | return true; |
| 2017 | } |
| 2018 | |
| 2019 | |
| 2020 | void Genesis::TransferNamedProperties(Handle<JSObject> from, |
| 2021 | Handle<JSObject> to) { |
| 2022 | if (from->HasFastProperties()) { |
| 2023 | Handle<DescriptorArray> descs = |
| 2024 | Handle<DescriptorArray>(from->map()->instance_descriptors()); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2025 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 2026 | PropertyDetails details = PropertyDetails(descs->GetDetails(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2027 | switch (details.type()) { |
| 2028 | case FIELD: { |
| 2029 | HandleScope inner; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2030 | Handle<String> key = Handle<String>(descs->GetKey(i)); |
| 2031 | int index = descs->GetFieldIndex(i); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2032 | Handle<Object> value = Handle<Object>(from->FastPropertyAt(index)); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2033 | SetLocalPropertyNoThrow(to, key, value, details.attributes()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2034 | break; |
| 2035 | } |
| 2036 | case CONSTANT_FUNCTION: { |
| 2037 | HandleScope inner; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2038 | Handle<String> key = Handle<String>(descs->GetKey(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2039 | Handle<JSFunction> fun = |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2040 | Handle<JSFunction>(descs->GetConstantFunction(i)); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2041 | SetLocalPropertyNoThrow(to, key, fun, details.attributes()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2042 | break; |
| 2043 | } |
| 2044 | case CALLBACKS: { |
| 2045 | LookupResult result; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2046 | to->LocalLookup(descs->GetKey(i), &result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2047 | // If the property is already there we skip it |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2048 | if (result.IsProperty()) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2049 | HandleScope inner; |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2050 | ASSERT(!to->HasFastProperties()); |
| 2051 | // Add to dictionary. |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2052 | Handle<String> key = Handle<String>(descs->GetKey(i)); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2053 | Handle<Object> callbacks(descs->GetCallbacksObject(i)); |
| 2054 | PropertyDetails d = |
| 2055 | PropertyDetails(details.attributes(), CALLBACKS, details.index()); |
| 2056 | SetNormalizedProperty(to, key, callbacks, d); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2057 | break; |
| 2058 | } |
| 2059 | case MAP_TRANSITION: |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2060 | case EXTERNAL_ARRAY_TRANSITION: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2061 | case CONSTANT_TRANSITION: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2062 | case NULL_DESCRIPTOR: |
| 2063 | // Ignore non-properties. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2064 | break; |
| 2065 | case NORMAL: |
| 2066 | // Do not occur since the from object has fast properties. |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 2067 | case HANDLER: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2068 | case INTERCEPTOR: |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 2069 | // No element in instance descriptors have proxy or interceptor type. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2070 | UNREACHABLE(); |
| 2071 | break; |
| 2072 | } |
| 2073 | } |
| 2074 | } else { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 2075 | Handle<StringDictionary> properties = |
| 2076 | Handle<StringDictionary>(from->property_dictionary()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2077 | int capacity = properties->Capacity(); |
| 2078 | for (int i = 0; i < capacity; i++) { |
| 2079 | Object* raw_key(properties->KeyAt(i)); |
| 2080 | if (properties->IsKey(raw_key)) { |
| 2081 | ASSERT(raw_key->IsString()); |
| 2082 | // If the property is already there we skip it. |
| 2083 | LookupResult result; |
| 2084 | to->LocalLookup(String::cast(raw_key), &result); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2085 | if (result.IsProperty()) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2086 | // Set the property. |
| 2087 | Handle<String> key = Handle<String>(String::cast(raw_key)); |
| 2088 | Handle<Object> value = Handle<Object>(properties->ValueAt(i)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2089 | if (value->IsJSGlobalPropertyCell()) { |
| 2090 | value = Handle<Object>(JSGlobalPropertyCell::cast(*value)->value()); |
| 2091 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2092 | PropertyDetails details = properties->DetailsAt(i); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2093 | SetLocalPropertyNoThrow(to, key, value, details.attributes()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2094 | } |
| 2095 | } |
| 2096 | } |
| 2097 | } |
| 2098 | |
| 2099 | |
| 2100 | void Genesis::TransferIndexedProperties(Handle<JSObject> from, |
| 2101 | Handle<JSObject> to) { |
| 2102 | // Cloning the elements array is sufficient. |
| 2103 | Handle<FixedArray> from_elements = |
| 2104 | Handle<FixedArray>(FixedArray::cast(from->elements())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2105 | Handle<FixedArray> to_elements = FACTORY->CopyFixedArray(from_elements); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2106 | to->set_elements(*to_elements); |
| 2107 | } |
| 2108 | |
| 2109 | |
| 2110 | void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) { |
| 2111 | HandleScope outer; |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2112 | Factory* factory = from->GetIsolate()->factory(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2113 | |
| 2114 | ASSERT(!from->IsJSArray()); |
| 2115 | ASSERT(!to->IsJSArray()); |
| 2116 | |
| 2117 | TransferNamedProperties(from, to); |
| 2118 | TransferIndexedProperties(from, to); |
| 2119 | |
| 2120 | // Transfer the prototype (new map is needed). |
| 2121 | Handle<Map> old_to_map = Handle<Map>(to->map()); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2122 | Handle<Map> new_to_map = factory->CopyMapDropTransitions(old_to_map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2123 | new_to_map->set_prototype(from->map()->prototype()); |
| 2124 | to->set_map(*new_to_map); |
| 2125 | } |
| 2126 | |
| 2127 | |
| 2128 | void Genesis::MakeFunctionInstancePrototypeWritable() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2129 | // The maps with writable prototype are created in CreateEmptyFunction |
| 2130 | // and CreateStrictModeFunctionMaps respectively. Initially the maps are |
| 2131 | // created with read-only prototype for JS builtins processing. |
| 2132 | ASSERT(!function_instance_map_writable_prototype_.is_null()); |
| 2133 | ASSERT(!strict_mode_function_instance_map_writable_prototype_.is_null()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2134 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2135 | // Replace function instance maps to make prototype writable. |
| 2136 | global_context()->set_function_map( |
| 2137 | *function_instance_map_writable_prototype_); |
| 2138 | global_context()->set_strict_mode_function_map( |
| 2139 | *strict_mode_function_instance_map_writable_prototype_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2143 | Genesis::Genesis(Isolate* isolate, |
| 2144 | Handle<Object> global_object, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2145 | v8::Handle<v8::ObjectTemplate> global_template, |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2146 | v8::ExtensionConfiguration* extensions) : isolate_(isolate) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2147 | result_ = Handle<Context>::null(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2148 | // If V8 isn't running and cannot be initialized, just return. |
| 2149 | if (!V8::IsRunning() && !V8::Initialize(NULL)) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2150 | |
| 2151 | // Before creating the roots we must save the context and restore it |
| 2152 | // on all function exits. |
| 2153 | HandleScope scope; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2154 | SaveContext saved_context(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2155 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2156 | Handle<Context> new_context = Snapshot::NewContextFromSnapshot(); |
| 2157 | if (!new_context.is_null()) { |
| 2158 | global_context_ = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2159 | Handle<Context>::cast(isolate->global_handles()->Create(*new_context)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2160 | AddToWeakGlobalContextList(*global_context_); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2161 | isolate->set_context(*global_context_); |
| 2162 | isolate->counters()->contexts_created_by_snapshot()->Increment(); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2163 | Handle<GlobalObject> inner_global; |
| 2164 | Handle<JSGlobalProxy> global_proxy = |
| 2165 | CreateNewGlobals(global_template, |
| 2166 | global_object, |
| 2167 | &inner_global); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2168 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2169 | HookUpGlobalProxy(inner_global, global_proxy); |
| 2170 | HookUpInnerGlobal(inner_global); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2171 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2172 | if (!ConfigureGlobalObjects(global_template)) return; |
| 2173 | } else { |
| 2174 | // We get here if there was no context snapshot. |
| 2175 | CreateRoots(); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2176 | Handle<JSFunction> empty_function = CreateEmptyFunction(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2177 | CreateStrictModeFunctionMaps(empty_function); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2178 | Handle<GlobalObject> inner_global; |
| 2179 | Handle<JSGlobalProxy> global_proxy = |
| 2180 | CreateNewGlobals(global_template, global_object, &inner_global); |
| 2181 | HookUpGlobalProxy(inner_global, global_proxy); |
| 2182 | InitializeGlobal(inner_global, empty_function); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 2183 | InstallJSFunctionResultCaches(); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 2184 | InitializeNormalizedMapCaches(); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 2185 | if (!InstallNatives()) return; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2186 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2187 | MakeFunctionInstancePrototypeWritable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2188 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2189 | if (!ConfigureGlobalObjects(global_template)) return; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2190 | isolate->counters()->contexts_created_from_scratch()->Increment(); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2191 | } |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 2192 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 2193 | // Initialize experimental globals and install experimental natives. |
| 2194 | InitializeExperimentalGlobal(); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 2195 | if (!InstallExperimentalNatives()) return; |
| 2196 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2197 | result_ = global_context_; |
| 2198 | } |
| 2199 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 2200 | |
| 2201 | // Support for thread preemption. |
| 2202 | |
| 2203 | // Reserve space for statics needing saving and restoring. |
| 2204 | int Bootstrapper::ArchiveSpacePerThread() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2205 | return sizeof(NestingCounterType); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
| 2208 | |
| 2209 | // Archive statics that are thread local. |
| 2210 | char* Bootstrapper::ArchiveState(char* to) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2211 | *reinterpret_cast<NestingCounterType*>(to) = nesting_; |
| 2212 | nesting_ = 0; |
| 2213 | return to + sizeof(NestingCounterType); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
| 2216 | |
| 2217 | // Restore statics that are thread local. |
| 2218 | char* Bootstrapper::RestoreState(char* from) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2219 | nesting_ = *reinterpret_cast<NestingCounterType*>(from); |
| 2220 | return from + sizeof(NestingCounterType); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2224 | // Called when the top-level V8 mutex is destroyed. |
| 2225 | void Bootstrapper::FreeThreadResources() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2226 | ASSERT(!IsActive()); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2229 | } } // namespace v8::internal |