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