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