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