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