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