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