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