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