Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5 | #include "src/bootstrapper.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "src/accessors.h" |
| 8 | #include "src/code-stubs.h" |
| 9 | #include "src/extensions/externalize-string-extension.h" |
| 10 | #include "src/extensions/free-buffer-extension.h" |
| 11 | #include "src/extensions/gc-extension.h" |
| 12 | #include "src/extensions/statistics-extension.h" |
| 13 | #include "src/extensions/trigger-failure-extension.h" |
| 14 | #include "src/isolate-inl.h" |
| 15 | #include "src/natives.h" |
| 16 | #include "src/snapshot.h" |
| 17 | #include "third_party/fdlibm/fdlibm.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18 | |
| 19 | namespace v8 { |
| 20 | namespace internal { |
| 21 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 22 | NativesExternalStringResource::NativesExternalStringResource( |
| 23 | Bootstrapper* bootstrapper, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 24 | const char* source, |
| 25 | size_t length) |
| 26 | : data_(source), length_(length) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 27 | if (bootstrapper->delete_these_non_arrays_on_tear_down_ == NULL) { |
| 28 | bootstrapper->delete_these_non_arrays_on_tear_down_ = new List<char*>(2); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 29 | } |
| 30 | // The resources are small objects and we only make a fixed number of |
| 31 | // them, but let's clean them up on exit for neatness. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 32 | bootstrapper->delete_these_non_arrays_on_tear_down_-> |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 33 | Add(reinterpret_cast<char*>(this)); |
| 34 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 35 | |
| 36 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 37 | Bootstrapper::Bootstrapper(Isolate* isolate) |
| 38 | : isolate_(isolate), |
| 39 | nesting_(0), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 40 | extensions_cache_(Script::TYPE_EXTENSION), |
| 41 | delete_these_non_arrays_on_tear_down_(NULL), |
| 42 | delete_these_arrays_on_tear_down_(NULL) { |
| 43 | } |
| 44 | |
| 45 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 46 | Handle<String> Bootstrapper::NativesSourceLookup(int index) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 47 | DCHECK(0 <= index && index < Natives::GetBuiltinsCount()); |
| 48 | Heap* heap = isolate_->heap(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 49 | if (heap->natives_source_cache()->get(index)->IsUndefined()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 50 | // We can use external strings for the natives. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 51 | Vector<const char> source = Natives::GetScriptSource(index); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 52 | NativesExternalStringResource* resource = |
| 53 | new NativesExternalStringResource(this, |
| 54 | source.start(), |
| 55 | source.length()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 56 | // We do not expect this to throw an exception. Change this if it does. |
| 57 | Handle<String> source_code = isolate_->factory() |
| 58 | ->NewExternalStringFromOneByte(resource) |
| 59 | .ToHandleChecked(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 60 | // Mark this external string with a special map. |
| 61 | source_code->set_map(isolate_->heap()->native_source_string_map()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 62 | heap->natives_source_cache()->set(index, *source_code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 63 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 64 | Handle<Object> cached_source(heap->natives_source_cache()->get(index), |
| 65 | isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 66 | return Handle<String>::cast(cached_source); |
| 67 | } |
| 68 | |
| 69 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 70 | void Bootstrapper::Initialize(bool create_heap_objects) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 71 | extensions_cache_.Initialize(isolate_, create_heap_objects); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | static const char* GCFunctionName() { |
| 76 | bool flag_given = FLAG_expose_gc_as != NULL && strlen(FLAG_expose_gc_as) != 0; |
| 77 | return flag_given ? FLAG_expose_gc_as : "gc"; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | v8::Extension* Bootstrapper::free_buffer_extension_ = NULL; |
| 82 | v8::Extension* Bootstrapper::gc_extension_ = NULL; |
| 83 | v8::Extension* Bootstrapper::externalize_string_extension_ = NULL; |
| 84 | v8::Extension* Bootstrapper::statistics_extension_ = NULL; |
| 85 | v8::Extension* Bootstrapper::trigger_failure_extension_ = NULL; |
| 86 | |
| 87 | |
| 88 | void Bootstrapper::InitializeOncePerProcess() { |
| 89 | free_buffer_extension_ = new FreeBufferExtension; |
| 90 | v8::RegisterExtension(free_buffer_extension_); |
| 91 | gc_extension_ = new GCExtension(GCFunctionName()); |
| 92 | v8::RegisterExtension(gc_extension_); |
| 93 | externalize_string_extension_ = new ExternalizeStringExtension; |
| 94 | v8::RegisterExtension(externalize_string_extension_); |
| 95 | statistics_extension_ = new StatisticsExtension; |
| 96 | v8::RegisterExtension(statistics_extension_); |
| 97 | trigger_failure_extension_ = new TriggerFailureExtension; |
| 98 | v8::RegisterExtension(trigger_failure_extension_); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | void Bootstrapper::TearDownExtensions() { |
| 103 | delete free_buffer_extension_; |
| 104 | free_buffer_extension_ = NULL; |
| 105 | delete gc_extension_; |
| 106 | gc_extension_ = NULL; |
| 107 | delete externalize_string_extension_; |
| 108 | externalize_string_extension_ = NULL; |
| 109 | delete statistics_extension_; |
| 110 | statistics_extension_ = NULL; |
| 111 | delete trigger_failure_extension_; |
| 112 | trigger_failure_extension_ = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 116 | char* Bootstrapper::AllocateAutoDeletedArray(int bytes) { |
| 117 | char* memory = new char[bytes]; |
| 118 | if (memory != NULL) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 119 | if (delete_these_arrays_on_tear_down_ == NULL) { |
| 120 | delete_these_arrays_on_tear_down_ = new List<char*>(2); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 121 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 122 | delete_these_arrays_on_tear_down_->Add(memory); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 123 | } |
| 124 | return memory; |
| 125 | } |
| 126 | |
| 127 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 128 | void Bootstrapper::TearDown() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 129 | if (delete_these_non_arrays_on_tear_down_ != NULL) { |
| 130 | int len = delete_these_non_arrays_on_tear_down_->length(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 131 | DCHECK(len < 1000); // Don't use this mechanism for unbounded allocations. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 132 | for (int i = 0; i < len; i++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 133 | delete delete_these_non_arrays_on_tear_down_->at(i); |
| 134 | delete_these_non_arrays_on_tear_down_->at(i) = NULL; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 135 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 136 | delete delete_these_non_arrays_on_tear_down_; |
| 137 | delete_these_non_arrays_on_tear_down_ = NULL; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 140 | if (delete_these_arrays_on_tear_down_ != NULL) { |
| 141 | int len = delete_these_arrays_on_tear_down_->length(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 142 | DCHECK(len < 1000); // Don't use this mechanism for unbounded allocations. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 143 | for (int i = 0; i < len; i++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 144 | delete[] delete_these_arrays_on_tear_down_->at(i); |
| 145 | delete_these_arrays_on_tear_down_->at(i) = NULL; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 146 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 147 | delete delete_these_arrays_on_tear_down_; |
| 148 | delete_these_arrays_on_tear_down_ = NULL; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 151 | extensions_cache_.Initialize(isolate_, false); // Yes, symmetrical |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 155 | class Genesis BASE_EMBEDDED { |
| 156 | public: |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 157 | Genesis(Isolate* isolate, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 158 | MaybeHandle<JSGlobalProxy> maybe_global_proxy, |
| 159 | v8::Handle<v8::ObjectTemplate> global_proxy_template, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 160 | v8::ExtensionConfiguration* extensions); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 161 | ~Genesis() { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 162 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 163 | Isolate* isolate() const { return isolate_; } |
| 164 | Factory* factory() const { return isolate_->factory(); } |
| 165 | Heap* heap() const { return isolate_->heap(); } |
| 166 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 167 | Handle<Context> result() { return result_; } |
| 168 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 169 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 170 | Handle<Context> native_context() { return native_context_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 171 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 172 | // Creates some basic objects. Used for creating a context from scratch. |
| 173 | void CreateRoots(); |
| 174 | // Creates the empty function. Used for creating a context from scratch. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 175 | Handle<JSFunction> CreateEmptyFunction(Isolate* isolate); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 176 | // Creates the ThrowTypeError function. ECMA 5th Ed. 13.2.3 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 177 | Handle<JSFunction> GetStrictPoisonFunction(); |
| 178 | // Poison for sloppy generator function arguments/callee. |
| 179 | Handle<JSFunction> GetGeneratorPoisonFunction(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 180 | |
| 181 | void CreateStrictModeFunctionMaps(Handle<JSFunction> empty); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 182 | |
| 183 | // Make the "arguments" and "caller" properties throw a TypeError on access. |
| 184 | void PoisonArgumentsAndCaller(Handle<Map> map); |
| 185 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 186 | // Creates the global objects using the global and the template passed in |
| 187 | // through the API. We call this regardless of whether we are building a |
| 188 | // context from scratch or using a deserialized one from the partial snapshot |
| 189 | // but in the latter case we don't use the objects it produces directly, as |
| 190 | // we have to used the deserialized ones that are linked together with the |
| 191 | // rest of the context snapshot. |
| 192 | Handle<JSGlobalProxy> CreateNewGlobals( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 193 | v8::Handle<v8::ObjectTemplate> global_proxy_template, |
| 194 | MaybeHandle<JSGlobalProxy> maybe_global_proxy, |
| 195 | Handle<GlobalObject>* global_object_out); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 196 | // Hooks the given global proxy into the context. If the context was created |
| 197 | // by deserialization then this will unhook the global proxy that was |
| 198 | // deserialized, leaving the GC to pick it up. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 199 | void HookUpGlobalProxy(Handle<GlobalObject> global_object, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 200 | Handle<JSGlobalProxy> global_proxy); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 201 | // Similarly, we want to use the global that has been created by the templates |
| 202 | // passed through the API. The global from the snapshot is detached from the |
| 203 | // other objects in the snapshot. |
| 204 | void HookUpGlobalObject(Handle<GlobalObject> global_object); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 205 | // New context initialization. Used for creating a context from scratch. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 206 | void InitializeGlobal(Handle<GlobalObject> global_object, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 207 | Handle<JSFunction> empty_function); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 208 | void InitializeExperimentalGlobal(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 209 | // Installs the contents of the native .js files on the global objects. |
| 210 | // Used for creating a context from scratch. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 211 | void InstallNativeFunctions(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 212 | void InstallExperimentalNativeFunctions(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 213 | |
| 214 | #define DECLARE_FEATURE_INITIALIZATION(id, descr) \ |
| 215 | void InstallNativeFunctions_##id(); \ |
| 216 | void InitializeGlobal_##id(); |
| 217 | |
| 218 | HARMONY_INPROGRESS(DECLARE_FEATURE_INITIALIZATION) |
| 219 | HARMONY_STAGED(DECLARE_FEATURE_INITIALIZATION) |
| 220 | HARMONY_SHIPPING(DECLARE_FEATURE_INITIALIZATION) |
| 221 | #undef DECLARE_FEATURE_INITIALIZATION |
| 222 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 223 | Handle<JSFunction> InstallInternalArray(Handle<JSBuiltinsObject> builtins, |
| 224 | const char* name, |
| 225 | ElementsKind elements_kind); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 226 | bool InstallNatives(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 227 | |
| 228 | void InstallTypedArray( |
| 229 | const char* name, |
| 230 | ElementsKind elements_kind, |
| 231 | Handle<JSFunction>* fun, |
| 232 | Handle<Map>* external_map); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 233 | bool InstallExperimentalNatives(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 234 | void InstallBuiltinFunctionIds(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 235 | void InstallJSFunctionResultCaches(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 236 | void InitializeNormalizedMapCaches(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 237 | |
| 238 | enum ExtensionTraversalState { |
| 239 | UNVISITED, VISITED, INSTALLED |
| 240 | }; |
| 241 | |
| 242 | class ExtensionStates { |
| 243 | public: |
| 244 | ExtensionStates(); |
| 245 | ExtensionTraversalState get_state(RegisteredExtension* extension); |
| 246 | void set_state(RegisteredExtension* extension, |
| 247 | ExtensionTraversalState state); |
| 248 | private: |
| 249 | HashMap map_; |
| 250 | DISALLOW_COPY_AND_ASSIGN(ExtensionStates); |
| 251 | }; |
| 252 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 253 | // Used both for deserialized and from-scratch contexts to add the extensions |
| 254 | // provided. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 255 | static bool InstallExtensions(Handle<Context> native_context, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 256 | v8::ExtensionConfiguration* extensions); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 257 | static bool InstallAutoExtensions(Isolate* isolate, |
| 258 | ExtensionStates* extension_states); |
| 259 | static bool InstallRequestedExtensions(Isolate* isolate, |
| 260 | v8::ExtensionConfiguration* extensions, |
| 261 | ExtensionStates* extension_states); |
| 262 | static bool InstallExtension(Isolate* isolate, |
| 263 | const char* name, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 264 | ExtensionStates* extension_states); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 265 | static bool InstallExtension(Isolate* isolate, |
| 266 | v8::RegisteredExtension* current, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 267 | ExtensionStates* extension_states); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 268 | static bool InstallSpecialObjects(Handle<Context> native_context); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 269 | bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 270 | bool ConfigureApiObject(Handle<JSObject> object, |
| 271 | Handle<ObjectTemplateInfo> object_template); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 272 | bool ConfigureGlobalObjects( |
| 273 | v8::Handle<v8::ObjectTemplate> global_proxy_template); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 274 | |
| 275 | // Migrates all properties from the 'from' object to the 'to' |
| 276 | // object and overrides the prototype in 'to' with the one from |
| 277 | // 'from'. |
| 278 | void TransferObject(Handle<JSObject> from, Handle<JSObject> to); |
| 279 | void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to); |
| 280 | void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to); |
| 281 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 282 | enum FunctionMode { |
| 283 | // With prototype. |
| 284 | FUNCTION_WITH_WRITEABLE_PROTOTYPE, |
| 285 | FUNCTION_WITH_READONLY_PROTOTYPE, |
| 286 | // Without prototype. |
| 287 | FUNCTION_WITHOUT_PROTOTYPE, |
| 288 | BOUND_FUNCTION |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 289 | }; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 290 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 291 | static bool IsFunctionModeWithPrototype(FunctionMode function_mode) { |
| 292 | return (function_mode == FUNCTION_WITH_WRITEABLE_PROTOTYPE || |
| 293 | function_mode == FUNCTION_WITH_READONLY_PROTOTYPE); |
| 294 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 295 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 296 | Handle<Map> CreateFunctionMap(FunctionMode function_mode); |
| 297 | |
| 298 | void SetFunctionInstanceDescriptor(Handle<Map> map, |
| 299 | FunctionMode function_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 300 | void MakeFunctionInstancePrototypeWritable(); |
| 301 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 302 | Handle<Map> CreateStrictFunctionMap( |
| 303 | FunctionMode function_mode, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 304 | Handle<JSFunction> empty_function); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 305 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 306 | void SetStrictFunctionInstanceDescriptor(Handle<Map> map, |
| 307 | FunctionMode function_mode); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 308 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 309 | static bool CompileBuiltin(Isolate* isolate, int index); |
| 310 | static bool CompileExperimentalBuiltin(Isolate* isolate, int index); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 311 | static bool CompileNative(Isolate* isolate, |
| 312 | Vector<const char> name, |
| 313 | Handle<String> source); |
| 314 | static bool CompileScriptCached(Isolate* isolate, |
| 315 | Vector<const char> name, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 316 | Handle<String> source, |
| 317 | SourceCodeCache* cache, |
| 318 | v8::Extension* extension, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 319 | Handle<Context> top_context, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 320 | bool use_runtime_context); |
| 321 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 322 | Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 323 | Handle<Context> result_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 324 | Handle<Context> native_context_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 325 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 326 | // Function maps. Function maps are created initially with a read only |
| 327 | // prototype for the processing of JS builtins. Later the function maps are |
| 328 | // replaced in order to make prototype writable. These are the final, writable |
| 329 | // prototype, maps. |
| 330 | Handle<Map> sloppy_function_map_writable_prototype_; |
| 331 | Handle<Map> strict_function_map_writable_prototype_; |
| 332 | Handle<JSFunction> strict_poison_function; |
| 333 | Handle<JSFunction> generator_poison_function; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 334 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 335 | BootstrapperActive active_; |
| 336 | friend class Bootstrapper; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 337 | }; |
| 338 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 339 | |
| 340 | void Bootstrapper::Iterate(ObjectVisitor* v) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 341 | extensions_cache_.Iterate(v); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 342 | v->Synchronize(VisitorSynchronization::kExtensions); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 346 | Handle<Context> Bootstrapper::CreateEnvironment( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 347 | MaybeHandle<JSGlobalProxy> maybe_global_proxy, |
| 348 | v8::Handle<v8::ObjectTemplate> global_proxy_template, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 349 | v8::ExtensionConfiguration* extensions) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 350 | HandleScope scope(isolate_); |
| 351 | Genesis genesis( |
| 352 | isolate_, maybe_global_proxy, global_proxy_template, extensions); |
| 353 | Handle<Context> env = genesis.result(); |
| 354 | if (env.is_null() || !InstallExtensions(env, extensions)) { |
| 355 | return Handle<Context>(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 356 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 357 | return scope.CloseAndEscape(env); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | |
| 361 | static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) { |
| 362 | // object.__proto__ = proto; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 363 | Handle<Map> old_map = Handle<Map>(object->map()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 364 | Handle<Map> new_map = Map::Copy(old_map, "SetObjectPrototype"); |
| 365 | new_map->SetPrototype(proto, FAST_PROTOTYPE); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 366 | JSObject::MigrateToMap(object, new_map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | |
| 370 | void Bootstrapper::DetachGlobal(Handle<Context> env) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 371 | Factory* factory = env->GetIsolate()->factory(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 372 | Handle<JSGlobalProxy> global_proxy(JSGlobalProxy::cast(env->global_proxy())); |
| 373 | global_proxy->set_native_context(*factory->null_value()); |
| 374 | SetObjectPrototype(global_proxy, factory->null_value()); |
| 375 | global_proxy->map()->set_constructor(*factory->null_value()); |
Andrei Popescu | 74b3c14 | 2010-03-29 12:03:09 +0100 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 379 | static Handle<JSFunction> InstallFunction(Handle<JSObject> target, |
| 380 | const char* name, |
| 381 | InstanceType type, |
| 382 | int instance_size, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 383 | MaybeHandle<JSObject> maybe_prototype, |
| 384 | Builtins::Name call) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 385 | Isolate* isolate = target->GetIsolate(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 386 | Factory* factory = isolate->factory(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 387 | Handle<String> internalized_name = factory->InternalizeUtf8String(name); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 388 | Handle<Code> call_code = Handle<Code>(isolate->builtins()->builtin(call)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 389 | Handle<JSObject> prototype; |
| 390 | Handle<JSFunction> function = maybe_prototype.ToHandle(&prototype) |
| 391 | ? factory->NewFunction(internalized_name, call_code, prototype, |
| 392 | type, instance_size) |
| 393 | : factory->NewFunctionWithoutPrototype(internalized_name, call_code); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 394 | PropertyAttributes attributes; |
| 395 | if (target->IsJSBuiltinsObject()) { |
| 396 | attributes = |
| 397 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 398 | } else { |
| 399 | attributes = DONT_ENUM; |
| 400 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 401 | JSObject::AddProperty(target, internalized_name, function, attributes); |
| 402 | if (target->IsJSGlobalObject()) { |
| 403 | function->shared()->set_instance_class_name(*internalized_name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 404 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 405 | function->shared()->set_native(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 406 | return function; |
| 407 | } |
| 408 | |
| 409 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 410 | void Genesis::SetFunctionInstanceDescriptor( |
| 411 | Handle<Map> map, FunctionMode function_mode) { |
| 412 | int size = IsFunctionModeWithPrototype(function_mode) ? 5 : 4; |
| 413 | Map::EnsureDescriptorSlack(map, size); |
| 414 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 415 | PropertyAttributes attribs = static_cast<PropertyAttributes>( |
| 416 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 417 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 418 | Handle<AccessorInfo> length = |
| 419 | Accessors::FunctionLengthInfo(isolate(), attribs); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 420 | { // Add length. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 421 | CallbacksDescriptor d(Handle<Name>(Name::cast(length->name())), |
| 422 | length, attribs); |
| 423 | map->AppendDescriptor(&d); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 424 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 425 | Handle<AccessorInfo> name = |
| 426 | Accessors::FunctionNameInfo(isolate(), attribs); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 427 | { // Add name. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 428 | CallbacksDescriptor d(Handle<Name>(Name::cast(name->name())), |
| 429 | name, attribs); |
| 430 | map->AppendDescriptor(&d); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 431 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 432 | Handle<AccessorInfo> args = |
| 433 | Accessors::FunctionArgumentsInfo(isolate(), attribs); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 434 | { // Add arguments. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 435 | CallbacksDescriptor d(Handle<Name>(Name::cast(args->name())), |
| 436 | args, attribs); |
| 437 | map->AppendDescriptor(&d); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 438 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 439 | Handle<AccessorInfo> caller = |
| 440 | Accessors::FunctionCallerInfo(isolate(), attribs); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 441 | { // Add caller. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 442 | CallbacksDescriptor d(Handle<Name>(Name::cast(caller->name())), |
| 443 | caller, attribs); |
| 444 | map->AppendDescriptor(&d); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 445 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 446 | if (IsFunctionModeWithPrototype(function_mode)) { |
| 447 | if (function_mode == FUNCTION_WITH_WRITEABLE_PROTOTYPE) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 448 | attribs = static_cast<PropertyAttributes>(attribs & ~READ_ONLY); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 449 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 450 | Handle<AccessorInfo> prototype = |
| 451 | Accessors::FunctionPrototypeInfo(isolate(), attribs); |
| 452 | CallbacksDescriptor d(Handle<Name>(Name::cast(prototype->name())), |
| 453 | prototype, attribs); |
| 454 | map->AppendDescriptor(&d); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 455 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 456 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 457 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 458 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 459 | Handle<Map> Genesis::CreateFunctionMap(FunctionMode function_mode) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 460 | Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 461 | SetFunctionInstanceDescriptor(map, function_mode); |
| 462 | map->set_function_with_prototype(IsFunctionModeWithPrototype(function_mode)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 463 | return map; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 467 | Handle<JSFunction> Genesis::CreateEmptyFunction(Isolate* isolate) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 468 | // Allocate the map for function instances. Maps are allocated first and their |
| 469 | // prototypes patched later, once empty function is created. |
| 470 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 471 | // Functions with this map will not have a 'prototype' property, and |
| 472 | // can not be used as constructors. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 473 | Handle<Map> function_without_prototype_map = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 474 | CreateFunctionMap(FUNCTION_WITHOUT_PROTOTYPE); |
| 475 | native_context()->set_sloppy_function_without_prototype_map( |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 476 | *function_without_prototype_map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 477 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 478 | // Allocate the function map. This map is temporary, used only for processing |
| 479 | // of builtins. |
| 480 | // Later the map is replaced with writable prototype map, allocated below. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 481 | Handle<Map> function_map = |
| 482 | CreateFunctionMap(FUNCTION_WITH_READONLY_PROTOTYPE); |
| 483 | native_context()->set_sloppy_function_map(*function_map); |
| 484 | native_context()->set_sloppy_function_with_readonly_prototype_map( |
| 485 | *function_map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 486 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 487 | // The final map for functions. Writeable prototype. |
| 488 | // This map is installed in MakeFunctionInstancePrototypeWritable. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 489 | sloppy_function_map_writable_prototype_ = |
| 490 | CreateFunctionMap(FUNCTION_WITH_WRITEABLE_PROTOTYPE); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 491 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 492 | Factory* factory = isolate->factory(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 493 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 494 | Handle<String> object_name = factory->Object_string(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 495 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 496 | Handle<JSObject> object_function_prototype; |
| 497 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 498 | { // --- O b j e c t --- |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 499 | Handle<JSFunction> object_fun = factory->NewFunction(object_name); |
| 500 | int unused = JSObject::kInitialGlobalObjectUnusedPropertiesCount; |
| 501 | int instance_size = JSObject::kHeaderSize + kPointerSize * unused; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 502 | Handle<Map> object_function_map = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 503 | factory->NewMap(JS_OBJECT_TYPE, instance_size); |
| 504 | object_function_map->set_inobject_properties(unused); |
| 505 | JSFunction::SetInitialMap(object_fun, object_function_map, |
| 506 | isolate->factory()->null_value()); |
| 507 | object_function_map->set_unused_property_fields(unused); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 508 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 509 | native_context()->set_object_function(*object_fun); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 510 | |
| 511 | // Allocate a new prototype for the object function. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 512 | object_function_prototype = |
| 513 | factory->NewJSObject(isolate->object_function(), TENURED); |
| 514 | Handle<Map> map = Map::Copy(handle(object_function_prototype->map()), |
| 515 | "EmptyObjectPrototype"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 516 | map->set_is_prototype_map(true); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 517 | object_function_prototype->set_map(*map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 518 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 519 | native_context()->set_initial_object_prototype(*object_function_prototype); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 520 | // For bootstrapping set the array prototype to be the same as the object |
| 521 | // prototype, otherwise the missing initial_array_prototype will cause |
| 522 | // assertions during startup. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 523 | native_context()->set_initial_array_prototype(*object_function_prototype); |
| 524 | Accessors::FunctionSetPrototype(object_fun, object_function_prototype) |
| 525 | .Assert(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | // Allocate the empty function as the prototype for function ECMAScript |
| 529 | // 262 15.3.4. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 530 | Handle<String> empty_string = |
| 531 | factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("Empty")); |
| 532 | Handle<Code> code(isolate->builtins()->builtin(Builtins::kEmptyFunction)); |
| 533 | Handle<JSFunction> empty_function = factory->NewFunctionWithoutPrototype( |
| 534 | empty_string, code); |
| 535 | |
| 536 | // Allocate the function map first and then patch the prototype later |
| 537 | Handle<Map> empty_function_map = |
| 538 | CreateFunctionMap(FUNCTION_WITHOUT_PROTOTYPE); |
| 539 | DCHECK(!empty_function_map->is_dictionary_map()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 540 | empty_function_map->SetPrototype(object_function_prototype); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 541 | empty_function_map->set_is_prototype_map(true); |
| 542 | empty_function->set_map(*empty_function_map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 543 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 544 | // --- E m p t y --- |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 545 | Handle<String> source = factory->NewStringFromStaticChars("() {}"); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 546 | Handle<Script> script = factory->NewScript(source); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 547 | script->set_type(Smi::FromInt(Script::TYPE_NATIVE)); |
| 548 | empty_function->shared()->set_script(*script); |
| 549 | empty_function->shared()->set_start_position(0); |
| 550 | empty_function->shared()->set_end_position(source->length()); |
| 551 | empty_function->shared()->DontAdaptArguments(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 552 | |
| 553 | // Set prototypes for the function maps. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 554 | native_context()->sloppy_function_map()->SetPrototype(empty_function); |
| 555 | native_context()->sloppy_function_without_prototype_map()->SetPrototype( |
| 556 | empty_function); |
| 557 | sloppy_function_map_writable_prototype_->SetPrototype(empty_function); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 558 | return empty_function; |
| 559 | } |
| 560 | |
| 561 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 562 | void Genesis::SetStrictFunctionInstanceDescriptor( |
| 563 | Handle<Map> map, FunctionMode function_mode) { |
| 564 | int size = IsFunctionModeWithPrototype(function_mode) ? 5 : 4; |
| 565 | Map::EnsureDescriptorSlack(map, size); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 566 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 567 | Handle<AccessorPair> arguments(factory()->NewAccessorPair()); |
| 568 | Handle<AccessorPair> caller(factory()->NewAccessorPair()); |
| 569 | PropertyAttributes rw_attribs = |
| 570 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE); |
| 571 | PropertyAttributes ro_attribs = |
| 572 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 573 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 574 | // Add length. |
| 575 | if (function_mode == BOUND_FUNCTION) { |
| 576 | Handle<String> length_string = isolate()->factory()->length_string(); |
| 577 | FieldDescriptor d(length_string, 0, ro_attribs, Representation::Tagged()); |
| 578 | map->AppendDescriptor(&d); |
| 579 | } else { |
| 580 | DCHECK(function_mode == FUNCTION_WITH_WRITEABLE_PROTOTYPE || |
| 581 | function_mode == FUNCTION_WITH_READONLY_PROTOTYPE || |
| 582 | function_mode == FUNCTION_WITHOUT_PROTOTYPE); |
| 583 | Handle<AccessorInfo> length = |
| 584 | Accessors::FunctionLengthInfo(isolate(), ro_attribs); |
| 585 | CallbacksDescriptor d(Handle<Name>(Name::cast(length->name())), |
| 586 | length, ro_attribs); |
| 587 | map->AppendDescriptor(&d); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 588 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 589 | Handle<AccessorInfo> name = |
| 590 | Accessors::FunctionNameInfo(isolate(), ro_attribs); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 591 | { // Add name. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 592 | CallbacksDescriptor d(Handle<Name>(Name::cast(name->name())), |
| 593 | name, ro_attribs); |
| 594 | map->AppendDescriptor(&d); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 595 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 596 | { // Add arguments. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 597 | CallbacksDescriptor d(factory()->arguments_string(), arguments, |
| 598 | rw_attribs); |
| 599 | map->AppendDescriptor(&d); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 600 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 601 | { // Add caller. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 602 | CallbacksDescriptor d(factory()->caller_string(), caller, rw_attribs); |
| 603 | map->AppendDescriptor(&d); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 604 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 605 | if (IsFunctionModeWithPrototype(function_mode)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 606 | // Add prototype. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 607 | PropertyAttributes attribs = |
| 608 | function_mode == FUNCTION_WITH_WRITEABLE_PROTOTYPE ? rw_attribs |
| 609 | : ro_attribs; |
| 610 | Handle<AccessorInfo> prototype = |
| 611 | Accessors::FunctionPrototypeInfo(isolate(), attribs); |
| 612 | CallbacksDescriptor d(Handle<Name>(Name::cast(prototype->name())), |
| 613 | prototype, attribs); |
| 614 | map->AppendDescriptor(&d); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 615 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | |
| 619 | // ECMAScript 5th Edition, 13.2.3 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 620 | Handle<JSFunction> Genesis::GetStrictPoisonFunction() { |
| 621 | if (strict_poison_function.is_null()) { |
| 622 | Handle<String> name = factory()->InternalizeOneByteString( |
| 623 | STATIC_CHAR_VECTOR("ThrowTypeError")); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 624 | Handle<Code> code(isolate()->builtins()->builtin( |
| 625 | Builtins::kStrictModePoisonPill)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 626 | strict_poison_function = factory()->NewFunctionWithoutPrototype(name, code); |
| 627 | strict_poison_function->set_map(native_context()->sloppy_function_map()); |
| 628 | strict_poison_function->shared()->DontAdaptArguments(); |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 629 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 630 | JSObject::PreventExtensions(strict_poison_function).Assert(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 631 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 632 | return strict_poison_function; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 636 | Handle<JSFunction> Genesis::GetGeneratorPoisonFunction() { |
| 637 | if (generator_poison_function.is_null()) { |
| 638 | Handle<String> name = factory()->InternalizeOneByteString( |
| 639 | STATIC_CHAR_VECTOR("ThrowTypeError")); |
| 640 | Handle<Code> code(isolate()->builtins()->builtin( |
| 641 | Builtins::kGeneratorPoisonPill)); |
| 642 | generator_poison_function = factory()->NewFunctionWithoutPrototype( |
| 643 | name, code); |
| 644 | generator_poison_function->set_map(native_context()->sloppy_function_map()); |
| 645 | generator_poison_function->shared()->DontAdaptArguments(); |
| 646 | |
| 647 | JSObject::PreventExtensions(generator_poison_function).Assert(); |
| 648 | } |
| 649 | return generator_poison_function; |
| 650 | } |
| 651 | |
| 652 | |
| 653 | Handle<Map> Genesis::CreateStrictFunctionMap( |
| 654 | FunctionMode function_mode, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 655 | Handle<JSFunction> empty_function) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 656 | Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 657 | SetStrictFunctionInstanceDescriptor(map, function_mode); |
| 658 | map->set_function_with_prototype(IsFunctionModeWithPrototype(function_mode)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 659 | map->SetPrototype(empty_function); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 660 | return map; |
| 661 | } |
| 662 | |
| 663 | |
| 664 | void Genesis::CreateStrictModeFunctionMaps(Handle<JSFunction> empty) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 665 | // Allocate map for the prototype-less strict mode instances. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 666 | Handle<Map> strict_function_without_prototype_map = |
| 667 | CreateStrictFunctionMap(FUNCTION_WITHOUT_PROTOTYPE, empty); |
| 668 | native_context()->set_strict_function_without_prototype_map( |
| 669 | *strict_function_without_prototype_map); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 670 | |
| 671 | // Allocate map for the strict mode functions. This map is temporary, used |
| 672 | // only for processing of builtins. |
| 673 | // Later the map is replaced with writable prototype map, allocated below. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 674 | Handle<Map> strict_function_map = |
| 675 | CreateStrictFunctionMap(FUNCTION_WITH_READONLY_PROTOTYPE, empty); |
| 676 | native_context()->set_strict_function_map(*strict_function_map); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 677 | |
| 678 | // The final map for the strict mode functions. Writeable prototype. |
| 679 | // This map is installed in MakeFunctionInstancePrototypeWritable. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 680 | strict_function_map_writable_prototype_ = |
| 681 | CreateStrictFunctionMap(FUNCTION_WITH_WRITEABLE_PROTOTYPE, empty); |
| 682 | // Special map for bound functions. |
| 683 | Handle<Map> bound_function_map = |
| 684 | CreateStrictFunctionMap(BOUND_FUNCTION, empty); |
| 685 | native_context()->set_bound_function_map(*bound_function_map); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 686 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 687 | // Complete the callbacks. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 688 | PoisonArgumentsAndCaller(strict_function_without_prototype_map); |
| 689 | PoisonArgumentsAndCaller(strict_function_map); |
| 690 | PoisonArgumentsAndCaller(strict_function_map_writable_prototype_); |
| 691 | PoisonArgumentsAndCaller(bound_function_map); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 692 | } |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 693 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 694 | |
| 695 | static void SetAccessors(Handle<Map> map, |
| 696 | Handle<String> name, |
| 697 | Handle<JSFunction> func) { |
| 698 | DescriptorArray* descs = map->instance_descriptors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 699 | int number = descs->SearchWithCache(*name, *map); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 700 | AccessorPair* accessors = AccessorPair::cast(descs->GetValue(number)); |
| 701 | accessors->set_getter(*func); |
| 702 | accessors->set_setter(*func); |
| 703 | } |
| 704 | |
| 705 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 706 | static void ReplaceAccessors(Handle<Map> map, |
| 707 | Handle<String> name, |
| 708 | PropertyAttributes attributes, |
| 709 | Handle<AccessorPair> accessor_pair) { |
| 710 | DescriptorArray* descriptors = map->instance_descriptors(); |
| 711 | int idx = descriptors->SearchWithCache(*name, *map); |
| 712 | CallbacksDescriptor descriptor(name, accessor_pair, attributes); |
| 713 | descriptors->Replace(idx, &descriptor); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 717 | void Genesis::PoisonArgumentsAndCaller(Handle<Map> map) { |
| 718 | SetAccessors(map, factory()->arguments_string(), GetStrictPoisonFunction()); |
| 719 | SetAccessors(map, factory()->caller_string(), GetStrictPoisonFunction()); |
| 720 | } |
| 721 | |
| 722 | |
| 723 | static void AddToWeakNativeContextList(Context* context) { |
| 724 | DCHECK(context->IsNativeContext()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 725 | Heap* heap = context->GetIsolate()->heap(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 726 | #ifdef DEBUG |
| 727 | { // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 728 | DCHECK(context->get(Context::NEXT_CONTEXT_LINK)->IsUndefined()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 729 | // Check that context is not in the list yet. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 730 | for (Object* current = heap->native_contexts_list(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 731 | !current->IsUndefined(); |
| 732 | current = Context::cast(current)->get(Context::NEXT_CONTEXT_LINK)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 733 | DCHECK(current != context); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 737 | context->set(Context::NEXT_CONTEXT_LINK, heap->native_contexts_list()); |
| 738 | heap->set_native_contexts_list(context); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 742 | void Genesis::CreateRoots() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 743 | // Allocate the native context FixedArray first and then patch the |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 744 | // closure and extension object later (we need the empty function |
| 745 | // and the global object, but in order to create those, we need the |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 746 | // native context). |
| 747 | native_context_ = factory()->NewNativeContext(); |
| 748 | AddToWeakNativeContextList(*native_context()); |
| 749 | isolate()->set_context(*native_context()); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 750 | |
| 751 | // Allocate the message listeners object. |
| 752 | { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 753 | v8::NeanderArray listeners(isolate()); |
| 754 | native_context()->set_message_listeners(*listeners.value()); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
| 758 | |
| 759 | Handle<JSGlobalProxy> Genesis::CreateNewGlobals( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 760 | v8::Handle<v8::ObjectTemplate> global_proxy_template, |
| 761 | MaybeHandle<JSGlobalProxy> maybe_global_proxy, |
| 762 | Handle<GlobalObject>* global_object_out) { |
| 763 | // The argument global_proxy_template aka data is an ObjectTemplateInfo. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 764 | // It has a constructor pointer that points at global_constructor which is a |
| 765 | // FunctionTemplateInfo. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 766 | // The global_proxy_constructor is used to create or reinitialize the |
| 767 | // global_proxy. The global_proxy_constructor also has a prototype_template |
| 768 | // pointer that points at js_global_object_template which is an |
| 769 | // ObjectTemplateInfo. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 770 | // That in turn has a constructor pointer that points at |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 771 | // js_global_object_constructor which is a FunctionTemplateInfo. |
| 772 | // js_global_object_constructor is used to make js_global_object_function |
| 773 | // js_global_object_function is used to make the new global_object. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 774 | // |
| 775 | // --- G l o b a l --- |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 776 | // Step 1: Create a fresh JSGlobalObject. |
| 777 | Handle<JSFunction> js_global_object_function; |
| 778 | Handle<ObjectTemplateInfo> js_global_object_template; |
| 779 | if (!global_proxy_template.IsEmpty()) { |
| 780 | // Get prototype template of the global_proxy_template. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 781 | Handle<ObjectTemplateInfo> data = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 782 | v8::Utils::OpenHandle(*global_proxy_template); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 783 | Handle<FunctionTemplateInfo> global_constructor = |
| 784 | Handle<FunctionTemplateInfo>( |
| 785 | FunctionTemplateInfo::cast(data->constructor())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 786 | Handle<Object> proto_template(global_constructor->prototype_template(), |
| 787 | isolate()); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 788 | if (!proto_template->IsUndefined()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 789 | js_global_object_template = |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 790 | Handle<ObjectTemplateInfo>::cast(proto_template); |
| 791 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 794 | if (js_global_object_template.is_null()) { |
| 795 | Handle<String> name = Handle<String>(heap()->empty_string()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 796 | Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 797 | Builtins::kIllegal)); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 798 | Handle<JSObject> prototype = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 799 | factory()->NewFunctionPrototype(isolate()->object_function()); |
| 800 | js_global_object_function = factory()->NewFunction( |
| 801 | name, code, prototype, JS_GLOBAL_OBJECT_TYPE, JSGlobalObject::kSize); |
| 802 | #ifdef DEBUG |
| 803 | LookupIterator it(prototype, factory()->constructor_string(), |
| 804 | LookupIterator::OWN_SKIP_INTERCEPTOR); |
| 805 | Handle<Object> value = JSReceiver::GetProperty(&it).ToHandleChecked(); |
| 806 | DCHECK(it.IsFound()); |
| 807 | DCHECK_EQ(*isolate()->object_function(), *value); |
| 808 | #endif |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 809 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 810 | Handle<FunctionTemplateInfo> js_global_object_constructor( |
| 811 | FunctionTemplateInfo::cast(js_global_object_template->constructor())); |
| 812 | js_global_object_function = |
| 813 | factory()->CreateApiFunction(js_global_object_constructor, |
| 814 | factory()->the_hole_value(), |
| 815 | factory()->GlobalObjectType); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 818 | js_global_object_function->initial_map()->set_is_hidden_prototype(); |
| 819 | js_global_object_function->initial_map()->set_dictionary_map(true); |
| 820 | Handle<GlobalObject> global_object = |
| 821 | factory()->NewGlobalObject(js_global_object_function); |
| 822 | if (global_object_out != NULL) { |
| 823 | *global_object_out = global_object; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | // Step 2: create or re-initialize the global proxy object. |
| 827 | Handle<JSFunction> global_proxy_function; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 828 | if (global_proxy_template.IsEmpty()) { |
| 829 | Handle<String> name = Handle<String>(heap()->empty_string()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 830 | Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 831 | Builtins::kIllegal)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 832 | global_proxy_function = factory()->NewFunction( |
| 833 | name, code, JS_GLOBAL_PROXY_TYPE, JSGlobalProxy::kSize); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 834 | } else { |
| 835 | Handle<ObjectTemplateInfo> data = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 836 | v8::Utils::OpenHandle(*global_proxy_template); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 837 | Handle<FunctionTemplateInfo> global_constructor( |
| 838 | FunctionTemplateInfo::cast(data->constructor())); |
| 839 | global_proxy_function = |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 840 | factory()->CreateApiFunction(global_constructor, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 841 | factory()->the_hole_value(), |
| 842 | factory()->GlobalProxyType); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 845 | Handle<String> global_name = factory()->global_string(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 846 | global_proxy_function->shared()->set_instance_class_name(*global_name); |
| 847 | global_proxy_function->initial_map()->set_is_access_check_needed(true); |
| 848 | |
| 849 | // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects |
| 850 | // Return the global proxy. |
| 851 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 852 | Handle<JSGlobalProxy> global_proxy; |
| 853 | if (maybe_global_proxy.ToHandle(&global_proxy)) { |
| 854 | factory()->ReinitializeJSGlobalProxy(global_proxy, global_proxy_function); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 855 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 856 | global_proxy = Handle<JSGlobalProxy>::cast( |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 857 | factory()->NewJSObject(global_proxy_function, TENURED)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 858 | global_proxy->set_hash(heap()->undefined_value()); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 859 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 860 | return global_proxy; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 864 | void Genesis::HookUpGlobalProxy(Handle<GlobalObject> global_object, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 865 | Handle<JSGlobalProxy> global_proxy) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 866 | // Set the native context for the global object. |
| 867 | global_object->set_native_context(*native_context()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 868 | global_object->set_global_proxy(*global_proxy); |
| 869 | global_proxy->set_native_context(*native_context()); |
| 870 | native_context()->set_global_proxy(*global_proxy); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 874 | void Genesis::HookUpGlobalObject(Handle<GlobalObject> global_object) { |
| 875 | Handle<GlobalObject> global_object_from_snapshot( |
| 876 | GlobalObject::cast(native_context()->extension())); |
| 877 | Handle<JSBuiltinsObject> builtins_global(native_context()->builtins()); |
| 878 | native_context()->set_extension(*global_object); |
| 879 | native_context()->set_global_object(*global_object); |
| 880 | native_context()->set_security_token(*global_object); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 881 | static const PropertyAttributes attributes = |
| 882 | static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 883 | Runtime::DefineObjectProperty(builtins_global, factory()->global_string(), |
| 884 | global_object, attributes).Assert(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 885 | // Set up the reference from the global object to the builtins object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 886 | JSGlobalObject::cast(*global_object)->set_builtins(*builtins_global); |
| 887 | TransferNamedProperties(global_object_from_snapshot, global_object); |
| 888 | TransferIndexedProperties(global_object_from_snapshot, global_object); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | |
| 892 | // This is only called if we are not using snapshots. The equivalent |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 893 | // work in the snapshot case is done in HookUpGlobalObject. |
| 894 | void Genesis::InitializeGlobal(Handle<GlobalObject> global_object, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 895 | Handle<JSFunction> empty_function) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 896 | // --- N a t i v e C o n t e x t --- |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 897 | // Use the empty function as closure (no scope info). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 898 | native_context()->set_closure(*empty_function); |
| 899 | native_context()->set_previous(NULL); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 900 | // Set extension and global object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 901 | native_context()->set_extension(*global_object); |
| 902 | native_context()->set_global_object(*global_object); |
| 903 | // Security setup: Set the security token of the native context to the global |
| 904 | // object. This makes the security check between two different contexts fail |
| 905 | // by default even in case of global object reinitialization. |
| 906 | native_context()->set_security_token(*global_object); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 907 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 908 | Isolate* isolate = global_object->GetIsolate(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 909 | Factory* factory = isolate->factory(); |
| 910 | Heap* heap = isolate->heap(); |
| 911 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 912 | Handle<ScriptContextTable> script_context_table = |
| 913 | factory->NewScriptContextTable(); |
| 914 | native_context()->set_script_context_table(*script_context_table); |
| 915 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 916 | Handle<String> object_name = factory->Object_string(); |
| 917 | JSObject::AddProperty( |
| 918 | global_object, object_name, isolate->object_function(), DONT_ENUM); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 919 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 920 | Handle<JSObject> global(native_context()->global_object()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 921 | |
| 922 | // Install global Function object |
| 923 | InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 924 | empty_function, Builtins::kIllegal); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 925 | |
| 926 | { // --- A r r a y --- |
| 927 | Handle<JSFunction> array_function = |
| 928 | InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 929 | isolate->initial_object_prototype(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 930 | Builtins::kArrayCode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 931 | array_function->shared()->DontAdaptArguments(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 932 | array_function->shared()->set_function_data(Smi::FromInt(kArrayCode)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 933 | |
| 934 | // This seems a bit hackish, but we need to make sure Array.length |
| 935 | // is 1. |
| 936 | array_function->shared()->set_length(1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 937 | |
| 938 | Handle<Map> initial_map(array_function->initial_map()); |
| 939 | |
| 940 | // This assert protects an optimization in |
| 941 | // HGraphBuilder::JSArrayBuilder::EmitMapCode() |
| 942 | DCHECK(initial_map->elements_kind() == GetInitialFastElementsKind()); |
| 943 | Map::EnsureDescriptorSlack(initial_map, 1); |
| 944 | |
| 945 | PropertyAttributes attribs = static_cast<PropertyAttributes>( |
| 946 | DONT_ENUM | DONT_DELETE); |
| 947 | |
| 948 | Handle<AccessorInfo> array_length = |
| 949 | Accessors::ArrayLengthInfo(isolate, attribs); |
| 950 | { // Add length. |
| 951 | CallbacksDescriptor d( |
| 952 | Handle<Name>(Name::cast(array_length->name())), |
| 953 | array_length, attribs); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 954 | initial_map->AppendDescriptor(&d); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 955 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 956 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 957 | // array_function is used internally. JS code creating array object should |
| 958 | // search for the 'Array' property on the global object and use that one |
| 959 | // as the constructor. 'Array' property on a global object can be |
| 960 | // overwritten by JS code. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 961 | native_context()->set_array_function(*array_function); |
| 962 | |
| 963 | // Cache the array maps, needed by ArrayConstructorStub |
| 964 | CacheInitialJSArrayMaps(native_context(), initial_map); |
| 965 | ArrayConstructorStub array_constructor_stub(isolate); |
| 966 | Handle<Code> code = array_constructor_stub.GetCode(); |
| 967 | array_function->shared()->set_construct_stub(*code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | { // --- N u m b e r --- |
| 971 | Handle<JSFunction> number_fun = |
| 972 | InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 973 | isolate->initial_object_prototype(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 974 | Builtins::kIllegal); |
| 975 | native_context()->set_number_function(*number_fun); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | { // --- B o o l e a n --- |
| 979 | Handle<JSFunction> boolean_fun = |
| 980 | InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 981 | isolate->initial_object_prototype(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 982 | Builtins::kIllegal); |
| 983 | native_context()->set_boolean_function(*boolean_fun); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | { // --- S t r i n g --- |
| 987 | Handle<JSFunction> string_fun = |
| 988 | InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 989 | isolate->initial_object_prototype(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 990 | Builtins::kIllegal); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 991 | string_fun->shared()->set_construct_stub( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 992 | isolate->builtins()->builtin(Builtins::kStringConstructCode)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 993 | native_context()->set_string_function(*string_fun); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 994 | |
| 995 | Handle<Map> string_map = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 996 | Handle<Map>(native_context()->string_function()->initial_map()); |
| 997 | Map::EnsureDescriptorSlack(string_map, 1); |
| 998 | |
| 999 | PropertyAttributes attribs = static_cast<PropertyAttributes>( |
| 1000 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 1001 | Handle<AccessorInfo> string_length( |
| 1002 | Accessors::StringLengthInfo(isolate, attribs)); |
| 1003 | |
| 1004 | { // Add length. |
| 1005 | CallbacksDescriptor d(factory->length_string(), string_length, attribs); |
| 1006 | string_map->AppendDescriptor(&d); |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | { |
| 1011 | // --- S y m b o l --- |
| 1012 | Handle<JSFunction> symbol_fun = InstallFunction( |
| 1013 | global, "Symbol", JS_VALUE_TYPE, JSValue::kSize, |
| 1014 | isolate->initial_object_prototype(), Builtins::kIllegal); |
| 1015 | native_context()->set_symbol_function(*symbol_fun); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | { // --- D a t e --- |
| 1019 | // Builtin functions for Date.prototype. |
| 1020 | Handle<JSFunction> date_fun = |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1021 | InstallFunction(global, "Date", JS_DATE_TYPE, JSDate::kSize, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1022 | isolate->initial_object_prototype(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1023 | Builtins::kIllegal); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1024 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1025 | native_context()->set_date_function(*date_fun); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | |
| 1029 | { // -- R e g E x p |
| 1030 | // Builtin functions for RegExp.prototype. |
| 1031 | Handle<JSFunction> regexp_fun = |
| 1032 | InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1033 | isolate->initial_object_prototype(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1034 | Builtins::kIllegal); |
| 1035 | native_context()->set_regexp_function(*regexp_fun); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1036 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1037 | DCHECK(regexp_fun->has_initial_map()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1038 | Handle<Map> initial_map(regexp_fun->initial_map()); |
| 1039 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1040 | DCHECK_EQ(0, initial_map->inobject_properties()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1041 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1042 | PropertyAttributes final = |
| 1043 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1044 | Map::EnsureDescriptorSlack(initial_map, 5); |
| 1045 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1046 | { |
| 1047 | // ECMA-262, section 15.10.7.1. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1048 | Handle<AccessorInfo> regexp_source( |
| 1049 | Accessors::RegExpSourceInfo(isolate, final)); |
| 1050 | CallbacksDescriptor d(factory->source_string(), regexp_source, final); |
| 1051 | initial_map->AppendDescriptor(&d); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1052 | } |
| 1053 | { |
| 1054 | // ECMA-262, section 15.10.7.2. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1055 | FieldDescriptor field(factory->global_string(), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1056 | JSRegExp::kGlobalFieldIndex, |
| 1057 | final, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1058 | Representation::Tagged()); |
| 1059 | initial_map->AppendDescriptor(&field); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1060 | } |
| 1061 | { |
| 1062 | // ECMA-262, section 15.10.7.3. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1063 | FieldDescriptor field(factory->ignore_case_string(), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1064 | JSRegExp::kIgnoreCaseFieldIndex, |
| 1065 | final, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1066 | Representation::Tagged()); |
| 1067 | initial_map->AppendDescriptor(&field); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1068 | } |
| 1069 | { |
| 1070 | // ECMA-262, section 15.10.7.4. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1071 | FieldDescriptor field(factory->multiline_string(), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1072 | JSRegExp::kMultilineFieldIndex, |
| 1073 | final, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1074 | Representation::Tagged()); |
| 1075 | initial_map->AppendDescriptor(&field); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1076 | } |
| 1077 | { |
| 1078 | // ECMA-262, section 15.10.7.5. |
| 1079 | PropertyAttributes writable = |
| 1080 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1081 | FieldDescriptor field(factory->last_index_string(), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1082 | JSRegExp::kLastIndexFieldIndex, |
| 1083 | writable, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1084 | Representation::Tagged()); |
| 1085 | initial_map->AppendDescriptor(&field); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1086 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1087 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1088 | static const int num_fields = JSRegExp::kInObjectFieldCount; |
| 1089 | initial_map->set_inobject_properties(num_fields); |
| 1090 | initial_map->set_pre_allocated_property_fields(num_fields); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1091 | initial_map->set_unused_property_fields(0); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1092 | initial_map->set_instance_size(initial_map->instance_size() + |
| 1093 | num_fields * kPointerSize); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1094 | |
| 1095 | // RegExp prototype object is itself a RegExp. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1096 | Handle<Map> proto_map = Map::Copy(initial_map, "RegExpPrototype"); |
| 1097 | DCHECK(proto_map->prototype() == *isolate->initial_object_prototype()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1098 | Handle<JSObject> proto = factory->NewJSObjectFromMap(proto_map); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1099 | proto->InObjectPropertyAtPut(JSRegExp::kGlobalFieldIndex, |
| 1100 | heap->false_value()); |
| 1101 | proto->InObjectPropertyAtPut(JSRegExp::kIgnoreCaseFieldIndex, |
| 1102 | heap->false_value()); |
| 1103 | proto->InObjectPropertyAtPut(JSRegExp::kMultilineFieldIndex, |
| 1104 | heap->false_value()); |
| 1105 | proto->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex, |
| 1106 | Smi::FromInt(0), |
| 1107 | SKIP_WRITE_BARRIER); // It's a Smi. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1108 | proto_map->set_is_prototype_map(true); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1109 | initial_map->SetPrototype(proto); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1110 | factory->SetRegExpIrregexpData(Handle<JSRegExp>::cast(proto), |
| 1111 | JSRegExp::IRREGEXP, factory->empty_string(), |
| 1112 | JSRegExp::Flags(0), 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | { // -- J S O N |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1116 | Handle<String> name = factory->InternalizeUtf8String("JSON"); |
| 1117 | Handle<JSFunction> cons = factory->NewFunction(name); |
| 1118 | JSFunction::SetInstancePrototype(cons, |
| 1119 | Handle<Object>(native_context()->initial_object_prototype(), isolate)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1120 | cons->SetInstanceClassName(*name); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1121 | Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1122 | DCHECK(json_object->IsJSObject()); |
| 1123 | JSObject::AddProperty(global, name, json_object, DONT_ENUM); |
| 1124 | native_context()->set_json_object(*json_object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1127 | { // -- A r r a y B u f f e r |
| 1128 | Handle<JSFunction> array_buffer_fun = |
| 1129 | InstallFunction( |
| 1130 | global, "ArrayBuffer", JS_ARRAY_BUFFER_TYPE, |
| 1131 | JSArrayBuffer::kSizeWithInternalFields, |
| 1132 | isolate->initial_object_prototype(), |
| 1133 | Builtins::kIllegal); |
| 1134 | native_context()->set_array_buffer_fun(*array_buffer_fun); |
| 1135 | } |
| 1136 | |
| 1137 | { // -- T y p e d A r r a y s |
| 1138 | #define INSTALL_TYPED_ARRAY(Type, type, TYPE, ctype, size) \ |
| 1139 | { \ |
| 1140 | Handle<JSFunction> fun; \ |
| 1141 | Handle<Map> external_map; \ |
| 1142 | InstallTypedArray(#Type "Array", \ |
| 1143 | TYPE##_ELEMENTS, \ |
| 1144 | &fun, \ |
| 1145 | &external_map); \ |
| 1146 | native_context()->set_##type##_array_fun(*fun); \ |
| 1147 | native_context()->set_##type##_array_external_map(*external_map); \ |
| 1148 | } |
| 1149 | TYPED_ARRAYS(INSTALL_TYPED_ARRAY) |
| 1150 | #undef INSTALL_TYPED_ARRAY |
| 1151 | |
| 1152 | Handle<JSFunction> data_view_fun = |
| 1153 | InstallFunction( |
| 1154 | global, "DataView", JS_DATA_VIEW_TYPE, |
| 1155 | JSDataView::kSizeWithInternalFields, |
| 1156 | isolate->initial_object_prototype(), |
| 1157 | Builtins::kIllegal); |
| 1158 | native_context()->set_data_view_fun(*data_view_fun); |
| 1159 | } |
| 1160 | |
| 1161 | // -- M a p |
| 1162 | InstallFunction(global, "Map", JS_MAP_TYPE, JSMap::kSize, |
| 1163 | isolate->initial_object_prototype(), Builtins::kIllegal); |
| 1164 | |
| 1165 | // -- S e t |
| 1166 | InstallFunction(global, "Set", JS_SET_TYPE, JSSet::kSize, |
| 1167 | isolate->initial_object_prototype(), Builtins::kIllegal); |
| 1168 | |
| 1169 | { // Set up the iterator result object |
| 1170 | STATIC_ASSERT(JSGeneratorObject::kResultPropertyCount == 2); |
| 1171 | Handle<JSFunction> object_function(native_context()->object_function()); |
| 1172 | Handle<Map> iterator_result_map = |
| 1173 | Map::Create(isolate, JSGeneratorObject::kResultPropertyCount); |
| 1174 | DCHECK_EQ(JSGeneratorObject::kResultSize, |
| 1175 | iterator_result_map->instance_size()); |
| 1176 | DCHECK_EQ(JSGeneratorObject::kResultPropertyCount, |
| 1177 | iterator_result_map->inobject_properties()); |
| 1178 | Map::EnsureDescriptorSlack(iterator_result_map, |
| 1179 | JSGeneratorObject::kResultPropertyCount); |
| 1180 | |
| 1181 | FieldDescriptor value_descr(factory->value_string(), |
| 1182 | JSGeneratorObject::kResultValuePropertyIndex, |
| 1183 | NONE, Representation::Tagged()); |
| 1184 | iterator_result_map->AppendDescriptor(&value_descr); |
| 1185 | |
| 1186 | FieldDescriptor done_descr(factory->done_string(), |
| 1187 | JSGeneratorObject::kResultDonePropertyIndex, |
| 1188 | NONE, Representation::Tagged()); |
| 1189 | iterator_result_map->AppendDescriptor(&done_descr); |
| 1190 | |
| 1191 | iterator_result_map->set_unused_property_fields(0); |
| 1192 | iterator_result_map->set_pre_allocated_property_fields( |
| 1193 | JSGeneratorObject::kResultPropertyCount); |
| 1194 | DCHECK_EQ(JSGeneratorObject::kResultSize, |
| 1195 | iterator_result_map->instance_size()); |
| 1196 | native_context()->set_iterator_result_map(*iterator_result_map); |
| 1197 | } |
| 1198 | |
| 1199 | // -- W e a k M a p |
| 1200 | InstallFunction(global, "WeakMap", JS_WEAK_MAP_TYPE, JSWeakMap::kSize, |
| 1201 | isolate->initial_object_prototype(), Builtins::kIllegal); |
| 1202 | // -- W e a k S e t |
| 1203 | InstallFunction(global, "WeakSet", JS_WEAK_SET_TYPE, JSWeakSet::kSize, |
| 1204 | isolate->initial_object_prototype(), Builtins::kIllegal); |
| 1205 | |
| 1206 | { // --- sloppy arguments map |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1207 | // Make sure we can recognize argument objects at runtime. |
| 1208 | // This is done by introducing an anonymous function with |
| 1209 | // class_name equals 'Arguments'. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1210 | Handle<String> arguments_string = factory->Arguments_string(); |
| 1211 | Handle<Code> code(isolate->builtins()->builtin(Builtins::kIllegal)); |
| 1212 | Handle<JSFunction> function = factory->NewFunctionWithoutPrototype( |
| 1213 | arguments_string, code); |
| 1214 | function->shared()->set_instance_class_name(*arguments_string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1215 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1216 | Handle<Map> map = |
| 1217 | factory->NewMap(JS_OBJECT_TYPE, Heap::kSloppyArgumentsObjectSize); |
| 1218 | // Create the descriptor array for the arguments object. |
| 1219 | Map::EnsureDescriptorSlack(map, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1220 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1221 | { // length |
| 1222 | FieldDescriptor d(factory->length_string(), Heap::kArgumentsLengthIndex, |
| 1223 | DONT_ENUM, Representation::Tagged()); |
| 1224 | map->AppendDescriptor(&d); |
| 1225 | } |
| 1226 | { // callee |
| 1227 | FieldDescriptor d(factory->callee_string(), Heap::kArgumentsCalleeIndex, |
| 1228 | DONT_ENUM, Representation::Tagged()); |
| 1229 | map->AppendDescriptor(&d); |
| 1230 | } |
| 1231 | // @@iterator method is added later. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1232 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1233 | map->set_function_with_prototype(true); |
| 1234 | map->set_pre_allocated_property_fields(2); |
| 1235 | map->set_inobject_properties(2); |
| 1236 | native_context()->set_sloppy_arguments_map(*map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1237 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1238 | DCHECK(!function->has_initial_map()); |
| 1239 | JSFunction::SetInitialMap(function, map, |
| 1240 | isolate->initial_object_prototype()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1241 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1242 | DCHECK(map->inobject_properties() > Heap::kArgumentsCalleeIndex); |
| 1243 | DCHECK(map->inobject_properties() > Heap::kArgumentsLengthIndex); |
| 1244 | DCHECK(!map->is_dictionary_map()); |
| 1245 | DCHECK(IsFastObjectElementsKind(map->elements_kind())); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1246 | } |
| 1247 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1248 | { // --- aliased arguments map |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1249 | Handle<Map> map = |
| 1250 | Map::Copy(isolate->sloppy_arguments_map(), "AliasedArguments"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1251 | map->set_elements_kind(SLOPPY_ARGUMENTS_ELEMENTS); |
| 1252 | DCHECK_EQ(2, map->pre_allocated_property_fields()); |
| 1253 | native_context()->set_aliased_arguments_map(*map); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1256 | { // --- strict mode arguments map |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1257 | const PropertyAttributes attributes = |
| 1258 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 1259 | |
| 1260 | // Create the ThrowTypeError functions. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1261 | Handle<AccessorPair> callee = factory->NewAccessorPair(); |
| 1262 | Handle<AccessorPair> caller = factory->NewAccessorPair(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1263 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1264 | Handle<JSFunction> poison = GetStrictPoisonFunction(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1265 | |
| 1266 | // Install the ThrowTypeError functions. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1267 | callee->set_getter(*poison); |
| 1268 | callee->set_setter(*poison); |
| 1269 | caller->set_getter(*poison); |
| 1270 | caller->set_setter(*poison); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1271 | |
| 1272 | // Create the map. Allocate one in-object field for length. |
| 1273 | Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1274 | Heap::kStrictArgumentsObjectSize); |
| 1275 | // Create the descriptor array for the arguments object. |
| 1276 | Map::EnsureDescriptorSlack(map, 3); |
| 1277 | |
| 1278 | { // length |
| 1279 | FieldDescriptor d(factory->length_string(), Heap::kArgumentsLengthIndex, |
| 1280 | DONT_ENUM, Representation::Tagged()); |
| 1281 | map->AppendDescriptor(&d); |
| 1282 | } |
| 1283 | { // callee |
| 1284 | CallbacksDescriptor d(factory->callee_string(), callee, attributes); |
| 1285 | map->AppendDescriptor(&d); |
| 1286 | } |
| 1287 | { // caller |
| 1288 | CallbacksDescriptor d(factory->caller_string(), caller, attributes); |
| 1289 | map->AppendDescriptor(&d); |
| 1290 | } |
| 1291 | // @@iterator method is added later. |
| 1292 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1293 | map->set_function_with_prototype(true); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1294 | DCHECK_EQ(native_context()->object_function()->prototype(), |
| 1295 | *isolate->initial_object_prototype()); |
| 1296 | map->SetPrototype(isolate->initial_object_prototype()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1297 | map->set_pre_allocated_property_fields(1); |
| 1298 | map->set_inobject_properties(1); |
| 1299 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1300 | // Copy constructor from the sloppy arguments boilerplate. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1301 | map->set_constructor( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1302 | native_context()->sloppy_arguments_map()->constructor()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1303 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1304 | native_context()->set_strict_arguments_map(*map); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1305 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1306 | DCHECK(map->inobject_properties() > Heap::kArgumentsLengthIndex); |
| 1307 | DCHECK(!map->is_dictionary_map()); |
| 1308 | DCHECK(IsFastObjectElementsKind(map->elements_kind())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | { // --- context extension |
| 1312 | // Create a function for the context extension objects. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1313 | Handle<Code> code = Handle<Code>( |
| 1314 | isolate->builtins()->builtin(Builtins::kIllegal)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1315 | Handle<JSFunction> context_extension_fun = factory->NewFunction( |
| 1316 | factory->empty_string(), code, JS_CONTEXT_EXTENSION_OBJECT_TYPE, |
| 1317 | JSObject::kHeaderSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1318 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1319 | Handle<String> name = factory->InternalizeOneByteString( |
| 1320 | STATIC_CHAR_VECTOR("context_extension")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1321 | context_extension_fun->shared()->set_instance_class_name(*name); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1322 | native_context()->set_context_extension_function(*context_extension_fun); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | |
| 1326 | { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1327 | // Set up the call-as-function delegate. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1328 | Handle<Code> code = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1329 | Handle<Code>(isolate->builtins()->builtin( |
| 1330 | Builtins::kHandleApiCallAsFunction)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1331 | Handle<JSFunction> delegate = factory->NewFunction( |
| 1332 | factory->empty_string(), code, JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 1333 | native_context()->set_call_as_function_delegate(*delegate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1334 | delegate->shared()->DontAdaptArguments(); |
| 1335 | } |
| 1336 | |
| 1337 | { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1338 | // Set up the call-as-constructor delegate. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1339 | Handle<Code> code = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1340 | Handle<Code>(isolate->builtins()->builtin( |
| 1341 | Builtins::kHandleApiCallAsConstructor)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1342 | Handle<JSFunction> delegate = factory->NewFunction( |
| 1343 | factory->empty_string(), code, JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 1344 | native_context()->set_call_as_constructor_delegate(*delegate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1345 | delegate->shared()->DontAdaptArguments(); |
| 1346 | } |
| 1347 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1348 | // Initialize the embedder data slot. |
| 1349 | Handle<FixedArray> embedder_data = factory->NewFixedArray(3); |
| 1350 | native_context()->set_embedder_data(*embedder_data); |
| 1351 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1352 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1353 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1354 | void Genesis::InstallTypedArray( |
| 1355 | const char* name, |
| 1356 | ElementsKind elements_kind, |
| 1357 | Handle<JSFunction>* fun, |
| 1358 | Handle<Map>* external_map) { |
| 1359 | Handle<JSObject> global = Handle<JSObject>(native_context()->global_object()); |
| 1360 | Handle<JSFunction> result = InstallFunction( |
| 1361 | global, name, JS_TYPED_ARRAY_TYPE, JSTypedArray::kSize, |
| 1362 | isolate()->initial_object_prototype(), Builtins::kIllegal); |
| 1363 | |
| 1364 | Handle<Map> initial_map = isolate()->factory()->NewMap( |
| 1365 | JS_TYPED_ARRAY_TYPE, |
| 1366 | JSTypedArray::kSizeWithInternalFields, |
| 1367 | elements_kind); |
| 1368 | JSFunction::SetInitialMap(result, initial_map, |
| 1369 | handle(initial_map->prototype(), isolate())); |
| 1370 | *fun = result; |
| 1371 | |
| 1372 | ElementsKind external_kind = GetNextTransitionElementsKind(elements_kind); |
| 1373 | *external_map = Map::AsElementsKind(initial_map, external_kind); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1377 | void Genesis::InitializeExperimentalGlobal() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1378 | #define FEATURE_INITIALIZE_GLOBAL(id, descr) InitializeGlobal_##id(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1379 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1380 | HARMONY_INPROGRESS(FEATURE_INITIALIZE_GLOBAL) |
| 1381 | HARMONY_STAGED(FEATURE_INITIALIZE_GLOBAL) |
| 1382 | HARMONY_SHIPPING(FEATURE_INITIALIZE_GLOBAL) |
| 1383 | #undef FEATURE_INITIALIZE_GLOBAL |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1387 | bool Genesis::CompileBuiltin(Isolate* isolate, int index) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1388 | Vector<const char> name = Natives::GetScriptName(index); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1389 | Handle<String> source_code = |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1390 | isolate->bootstrapper()->NativesSourceLookup(index); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1391 | return CompileNative(isolate, name, source_code); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | |
| 1395 | bool Genesis::CompileExperimentalBuiltin(Isolate* isolate, int index) { |
| 1396 | Vector<const char> name = ExperimentalNatives::GetScriptName(index); |
| 1397 | Factory* factory = isolate->factory(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1398 | Handle<String> source_code; |
| 1399 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1400 | isolate, source_code, |
| 1401 | factory->NewStringFromAscii(ExperimentalNatives::GetScriptSource(index)), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1402 | false); |
| 1403 | return CompileNative(isolate, name, source_code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1407 | bool Genesis::CompileNative(Isolate* isolate, |
| 1408 | Vector<const char> name, |
| 1409 | Handle<String> source) { |
| 1410 | HandleScope scope(isolate); |
| 1411 | SuppressDebug compiling_natives(isolate->debug()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1412 | // During genesis, the boilerplate for stack overflow won't work until the |
| 1413 | // environment has been at least partially initialized. Add a stack check |
| 1414 | // before entering JS code to catch overflow early. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1415 | StackLimitCheck check(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1416 | if (check.HasOverflowed()) return false; |
| 1417 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1418 | bool result = CompileScriptCached(isolate, |
| 1419 | name, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1420 | source, |
| 1421 | NULL, |
| 1422 | NULL, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1423 | Handle<Context>(isolate->context()), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1424 | true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1425 | DCHECK(isolate->has_pending_exception() != result); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1426 | if (!result) isolate->clear_pending_exception(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1427 | return result; |
| 1428 | } |
| 1429 | |
| 1430 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1431 | bool Genesis::CompileScriptCached(Isolate* isolate, |
| 1432 | Vector<const char> name, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1433 | Handle<String> source, |
| 1434 | SourceCodeCache* cache, |
| 1435 | v8::Extension* extension, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1436 | Handle<Context> top_context, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1437 | bool use_runtime_context) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1438 | Factory* factory = isolate->factory(); |
| 1439 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1440 | Handle<SharedFunctionInfo> function_info; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1441 | |
| 1442 | // If we can't find the function in the cache, we compile a new |
| 1443 | // function and insert it into the cache. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1444 | if (cache == NULL || !cache->Lookup(name, &function_info)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1445 | DCHECK(source->IsOneByteRepresentation()); |
| 1446 | Handle<String> script_name = |
| 1447 | factory->NewStringFromUtf8(name).ToHandleChecked(); |
| 1448 | function_info = Compiler::CompileScript( |
| 1449 | source, script_name, 0, 0, false, top_context, extension, NULL, |
| 1450 | ScriptCompiler::kNoCompileOptions, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1451 | use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1452 | if (function_info.is_null()) return false; |
| 1453 | if (cache != NULL) cache->Add(name, function_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1456 | // Set up the function context. Conceptually, we should clone the |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1457 | // function before overwriting the context but since we're in a |
| 1458 | // single-threaded environment it is not strictly necessary. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1459 | DCHECK(top_context->IsNativeContext()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1460 | Handle<Context> context = |
| 1461 | Handle<Context>(use_runtime_context |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1462 | ? Handle<Context>(top_context->runtime_context()) |
| 1463 | : top_context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1464 | Handle<JSFunction> fun = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1465 | factory->NewFunctionFromSharedFunctionInfo(function_info, context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1466 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 1467 | // Call function using either the runtime object or the global |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1468 | // object as the receiver. Provide no parameters. |
| 1469 | Handle<Object> receiver = |
| 1470 | Handle<Object>(use_runtime_context |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1471 | ? top_context->builtins() |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1472 | : top_context->global_object(), |
| 1473 | isolate); |
| 1474 | return !Execution::Call( |
| 1475 | isolate, fun, receiver, 0, NULL).is_null(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1479 | static Handle<JSObject> ResolveBuiltinIdHolder(Handle<Context> native_context, |
| 1480 | const char* holder_expr) { |
| 1481 | Isolate* isolate = native_context->GetIsolate(); |
| 1482 | Factory* factory = isolate->factory(); |
| 1483 | Handle<GlobalObject> global(native_context->global_object()); |
| 1484 | const char* period_pos = strchr(holder_expr, '.'); |
| 1485 | if (period_pos == NULL) { |
| 1486 | return Handle<JSObject>::cast( |
| 1487 | Object::GetPropertyOrElement( |
| 1488 | global, factory->InternalizeUtf8String(holder_expr)) |
| 1489 | .ToHandleChecked()); |
| 1490 | } |
| 1491 | const char* inner = period_pos + 1; |
| 1492 | DCHECK_EQ(NULL, strchr(inner, '.')); |
| 1493 | Vector<const char> property(holder_expr, |
| 1494 | static_cast<int>(period_pos - holder_expr)); |
| 1495 | Handle<String> property_string = factory->InternalizeUtf8String(property); |
| 1496 | DCHECK(!property_string.is_null()); |
| 1497 | Handle<JSObject> object = Handle<JSObject>::cast( |
| 1498 | Object::GetProperty(global, property_string).ToHandleChecked()); |
| 1499 | if (strcmp("prototype", inner) == 0) { |
| 1500 | Handle<JSFunction> function = Handle<JSFunction>::cast(object); |
| 1501 | return Handle<JSObject>(JSObject::cast(function->prototype())); |
| 1502 | } |
| 1503 | Handle<String> inner_string = factory->InternalizeUtf8String(inner); |
| 1504 | DCHECK(!inner_string.is_null()); |
| 1505 | Handle<Object> value = |
| 1506 | Object::GetProperty(object, inner_string).ToHandleChecked(); |
| 1507 | return Handle<JSObject>::cast(value); |
| 1508 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1509 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1510 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1511 | #define INSTALL_NATIVE(Type, name, var) \ |
| 1512 | Handle<String> var##_name = \ |
| 1513 | factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR(name)); \ |
| 1514 | Handle<Object> var##_native = \ |
| 1515 | Object::GetProperty(handle(native_context()->builtins()), var##_name) \ |
| 1516 | .ToHandleChecked(); \ |
| 1517 | native_context()->set_##var(Type::cast(*var##_native)); |
| 1518 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1519 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1520 | void Genesis::InstallNativeFunctions() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1521 | HandleScope scope(isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1522 | INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1523 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1524 | INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun); |
| 1525 | INSTALL_NATIVE(JSFunction, "ToString", to_string_fun); |
| 1526 | INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun); |
| 1527 | INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun); |
| 1528 | INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun); |
| 1529 | INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun); |
| 1530 | INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1531 | INSTALL_NATIVE(JSFunction, "ToLength", to_length_fun); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1532 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1533 | INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1534 | INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun); |
| 1535 | INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance", |
| 1536 | configure_instance_fun); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1537 | INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun); |
| 1538 | INSTALL_NATIVE(JSObject, "functionCache", function_cache); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1539 | INSTALL_NATIVE(JSFunction, "ToCompletePropertyDescriptor", |
| 1540 | to_complete_property_descriptor); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1541 | |
| 1542 | INSTALL_NATIVE(JSFunction, "IsPromise", is_promise); |
| 1543 | INSTALL_NATIVE(JSFunction, "PromiseCreate", promise_create); |
| 1544 | INSTALL_NATIVE(JSFunction, "PromiseResolve", promise_resolve); |
| 1545 | INSTALL_NATIVE(JSFunction, "PromiseReject", promise_reject); |
| 1546 | INSTALL_NATIVE(JSFunction, "PromiseChain", promise_chain); |
| 1547 | INSTALL_NATIVE(JSFunction, "PromiseCatch", promise_catch); |
| 1548 | INSTALL_NATIVE(JSFunction, "PromiseThen", promise_then); |
| 1549 | |
| 1550 | INSTALL_NATIVE(JSFunction, "NotifyChange", observers_notify_change); |
| 1551 | INSTALL_NATIVE(JSFunction, "EnqueueSpliceRecord", observers_enqueue_splice); |
| 1552 | INSTALL_NATIVE(JSFunction, "BeginPerformSplice", |
| 1553 | observers_begin_perform_splice); |
| 1554 | INSTALL_NATIVE(JSFunction, "EndPerformSplice", |
| 1555 | observers_end_perform_splice); |
| 1556 | INSTALL_NATIVE(JSFunction, "NativeObjectObserve", |
| 1557 | native_object_observe); |
| 1558 | INSTALL_NATIVE(JSFunction, "NativeObjectGetNotifier", |
| 1559 | native_object_get_notifier); |
| 1560 | INSTALL_NATIVE(JSFunction, "NativeObjectNotifierPerformChange", |
| 1561 | native_object_notifier_perform_change); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1562 | INSTALL_NATIVE(JSFunction, "ArrayValues", array_values_iterator); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1565 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1566 | void Genesis::InstallExperimentalNativeFunctions() { |
| 1567 | if (FLAG_harmony_proxies) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1568 | INSTALL_NATIVE(JSFunction, "DerivedHasTrap", derived_has_trap); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1569 | INSTALL_NATIVE(JSFunction, "DerivedGetTrap", derived_get_trap); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1570 | INSTALL_NATIVE(JSFunction, "DerivedSetTrap", derived_set_trap); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1571 | INSTALL_NATIVE(JSFunction, "ProxyEnumerate", proxy_enumerate); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1572 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1573 | |
| 1574 | #define INSTALL_NATIVE_FUNCTIONS_FOR(id, descr) InstallNativeFunctions_##id(); |
| 1575 | HARMONY_INPROGRESS(INSTALL_NATIVE_FUNCTIONS_FOR) |
| 1576 | HARMONY_STAGED(INSTALL_NATIVE_FUNCTIONS_FOR) |
| 1577 | HARMONY_SHIPPING(INSTALL_NATIVE_FUNCTIONS_FOR) |
| 1578 | #undef INSTALL_NATIVE_FUNCTIONS_FOR |
| 1579 | } |
| 1580 | |
| 1581 | |
| 1582 | #define EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(id) \ |
| 1583 | void Genesis::InstallNativeFunctions_##id() {} |
| 1584 | |
| 1585 | EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_scoping) |
| 1586 | EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_modules) |
| 1587 | EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_strings) |
| 1588 | EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_arrays) |
| 1589 | EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_array_includes) |
| 1590 | EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_classes) |
| 1591 | EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_object_literals) |
| 1592 | EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_regexps) |
| 1593 | EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_arrow_functions) |
| 1594 | EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_numeric_literals) |
| 1595 | EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_tostring) |
| 1596 | EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_templates) |
| 1597 | EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_sloppy) |
| 1598 | EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_unicode) |
| 1599 | |
| 1600 | |
| 1601 | void Genesis::InstallNativeFunctions_harmony_proxies() { |
| 1602 | if (FLAG_harmony_proxies) { |
| 1603 | INSTALL_NATIVE(JSFunction, "DerivedHasTrap", derived_has_trap); |
| 1604 | INSTALL_NATIVE(JSFunction, "DerivedGetTrap", derived_get_trap); |
| 1605 | INSTALL_NATIVE(JSFunction, "DerivedSetTrap", derived_set_trap); |
| 1606 | INSTALL_NATIVE(JSFunction, "ProxyEnumerate", proxy_enumerate); |
| 1607 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1610 | #undef INSTALL_NATIVE |
| 1611 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1612 | #define EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(id) \ |
| 1613 | void Genesis::InitializeGlobal_##id() {} |
| 1614 | |
| 1615 | EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_scoping) |
| 1616 | EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_modules) |
| 1617 | EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_strings) |
| 1618 | EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_arrays) |
| 1619 | EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_array_includes) |
| 1620 | EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_classes) |
| 1621 | EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_object_literals) |
| 1622 | EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_arrow_functions) |
| 1623 | EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_numeric_literals) |
| 1624 | EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_tostring) |
| 1625 | EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_proxies) |
| 1626 | EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_templates) |
| 1627 | EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_sloppy) |
| 1628 | EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_unicode) |
| 1629 | |
| 1630 | void Genesis::InitializeGlobal_harmony_regexps() { |
| 1631 | Handle<JSObject> builtins(native_context()->builtins()); |
| 1632 | |
| 1633 | Handle<HeapObject> flag(FLAG_harmony_regexps ? heap()->true_value() |
| 1634 | : heap()->false_value()); |
| 1635 | PropertyAttributes attributes = |
| 1636 | static_cast<PropertyAttributes>(DONT_DELETE | READ_ONLY); |
| 1637 | Runtime::DefineObjectProperty(builtins, factory()->harmony_regexps_string(), |
| 1638 | flag, attributes).Assert(); |
| 1639 | } |
| 1640 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1641 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1642 | Handle<JSFunction> Genesis::InstallInternalArray( |
| 1643 | Handle<JSBuiltinsObject> builtins, |
| 1644 | const char* name, |
| 1645 | ElementsKind elements_kind) { |
| 1646 | // --- I n t e r n a l A r r a y --- |
| 1647 | // An array constructor on the builtins object that works like |
| 1648 | // the public Array constructor, except that its prototype |
| 1649 | // doesn't inherit from Object.prototype. |
| 1650 | // To be used only for internal work by builtins. Instances |
| 1651 | // must not be leaked to user code. |
| 1652 | Handle<JSObject> prototype = |
| 1653 | factory()->NewJSObject(isolate()->object_function(), TENURED); |
| 1654 | Handle<JSFunction> array_function = InstallFunction( |
| 1655 | builtins, name, JS_ARRAY_TYPE, JSArray::kSize, |
| 1656 | prototype, Builtins::kInternalArrayCode); |
| 1657 | |
| 1658 | InternalArrayConstructorStub internal_array_constructor_stub(isolate()); |
| 1659 | Handle<Code> code = internal_array_constructor_stub.GetCode(); |
| 1660 | array_function->shared()->set_construct_stub(*code); |
| 1661 | array_function->shared()->DontAdaptArguments(); |
| 1662 | |
| 1663 | Handle<Map> original_map(array_function->initial_map()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1664 | Handle<Map> initial_map = Map::Copy(original_map, "InternalArray"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1665 | initial_map->set_elements_kind(elements_kind); |
| 1666 | JSFunction::SetInitialMap(array_function, initial_map, prototype); |
| 1667 | |
| 1668 | // Make "length" magic on instances. |
| 1669 | Map::EnsureDescriptorSlack(initial_map, 1); |
| 1670 | |
| 1671 | PropertyAttributes attribs = static_cast<PropertyAttributes>( |
| 1672 | DONT_ENUM | DONT_DELETE); |
| 1673 | |
| 1674 | Handle<AccessorInfo> array_length = |
| 1675 | Accessors::ArrayLengthInfo(isolate(), attribs); |
| 1676 | { // Add length. |
| 1677 | CallbacksDescriptor d( |
| 1678 | Handle<Name>(Name::cast(array_length->name())), array_length, attribs); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1679 | initial_map->AppendDescriptor(&d); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | return array_function; |
| 1683 | } |
| 1684 | |
| 1685 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1686 | bool Genesis::InstallNatives() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1687 | HandleScope scope(isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1688 | |
| 1689 | // Create a function for the builtins object. Allocate space for the |
| 1690 | // JavaScript builtins, a reference to the builtins object |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1691 | // (itself) and a reference to the native_context directly in the object. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1692 | Handle<Code> code = Handle<Code>( |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1693 | isolate()->builtins()->builtin(Builtins::kIllegal)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1694 | Handle<JSFunction> builtins_fun = factory()->NewFunction( |
| 1695 | factory()->empty_string(), code, JS_BUILTINS_OBJECT_TYPE, |
| 1696 | JSBuiltinsObject::kSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1697 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1698 | Handle<String> name = |
| 1699 | factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("builtins")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1700 | builtins_fun->shared()->set_instance_class_name(*name); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1701 | builtins_fun->initial_map()->set_dictionary_map(true); |
| 1702 | builtins_fun->initial_map()->set_prototype(heap()->null_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1703 | |
| 1704 | // Allocate the builtins object. |
| 1705 | Handle<JSBuiltinsObject> builtins = |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1706 | Handle<JSBuiltinsObject>::cast(factory()->NewGlobalObject(builtins_fun)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1707 | builtins->set_builtins(*builtins); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1708 | builtins->set_native_context(*native_context()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1709 | builtins->set_global_proxy(native_context()->global_proxy()); |
| 1710 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1711 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1712 | // Set up the 'global' properties of the builtins object. The |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1713 | // 'global' property that refers to the global object is the only |
| 1714 | // way to get from code running in the builtins context to the |
| 1715 | // global object. |
| 1716 | static const PropertyAttributes attributes = |
| 1717 | static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1718 | Handle<String> global_string = |
| 1719 | factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("global")); |
| 1720 | Handle<Object> global_obj(native_context()->global_object(), isolate()); |
| 1721 | JSObject::AddProperty(builtins, global_string, global_obj, attributes); |
| 1722 | Handle<String> builtins_string = |
| 1723 | factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("builtins")); |
| 1724 | JSObject::AddProperty(builtins, builtins_string, builtins, attributes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1725 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1726 | // Set up the reference from the global object to the builtins object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1727 | JSGlobalObject::cast(native_context()->global_object())-> |
| 1728 | set_builtins(*builtins); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1729 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1730 | // Create a bridge function that has context in the native context. |
| 1731 | Handle<JSFunction> bridge = factory()->NewFunction(factory()->empty_string()); |
| 1732 | DCHECK(bridge->context() == *isolate()->native_context()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1733 | |
| 1734 | // Allocate the builtins context. |
| 1735 | Handle<Context> context = |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1736 | factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1737 | context->set_global_object(*builtins); // override builtins global object |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1738 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1739 | native_context()->set_runtime_context(*context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1740 | |
| 1741 | { // -- S c r i p t |
| 1742 | // Builtin functions for Script. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1743 | Handle<JSFunction> script_fun = InstallFunction( |
| 1744 | builtins, "Script", JS_VALUE_TYPE, JSValue::kSize, |
| 1745 | isolate()->initial_object_prototype(), Builtins::kIllegal); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1746 | Handle<JSObject> prototype = |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1747 | factory()->NewJSObject(isolate()->object_function(), TENURED); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1748 | Accessors::FunctionSetPrototype(script_fun, prototype).Assert(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1749 | native_context()->set_script_function(*script_fun); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1750 | |
| 1751 | Handle<Map> script_map = Handle<Map>(script_fun->initial_map()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1752 | Map::EnsureDescriptorSlack(script_map, 14); |
| 1753 | |
| 1754 | PropertyAttributes attribs = |
| 1755 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 1756 | |
| 1757 | Handle<AccessorInfo> script_column = |
| 1758 | Accessors::ScriptColumnOffsetInfo(isolate(), attribs); |
| 1759 | { |
| 1760 | CallbacksDescriptor d(Handle<Name>(Name::cast(script_column->name())), |
| 1761 | script_column, attribs); |
| 1762 | script_map->AppendDescriptor(&d); |
| 1763 | } |
| 1764 | |
| 1765 | Handle<AccessorInfo> script_id = |
| 1766 | Accessors::ScriptIdInfo(isolate(), attribs); |
| 1767 | { |
| 1768 | CallbacksDescriptor d(Handle<Name>(Name::cast(script_id->name())), |
| 1769 | script_id, attribs); |
| 1770 | script_map->AppendDescriptor(&d); |
| 1771 | } |
| 1772 | |
| 1773 | |
| 1774 | Handle<AccessorInfo> script_name = |
| 1775 | Accessors::ScriptNameInfo(isolate(), attribs); |
| 1776 | { |
| 1777 | CallbacksDescriptor d(Handle<Name>(Name::cast(script_name->name())), |
| 1778 | script_name, attribs); |
| 1779 | script_map->AppendDescriptor(&d); |
| 1780 | } |
| 1781 | |
| 1782 | Handle<AccessorInfo> script_line = |
| 1783 | Accessors::ScriptLineOffsetInfo(isolate(), attribs); |
| 1784 | { |
| 1785 | CallbacksDescriptor d(Handle<Name>(Name::cast(script_line->name())), |
| 1786 | script_line, attribs); |
| 1787 | script_map->AppendDescriptor(&d); |
| 1788 | } |
| 1789 | |
| 1790 | Handle<AccessorInfo> script_source = |
| 1791 | Accessors::ScriptSourceInfo(isolate(), attribs); |
| 1792 | { |
| 1793 | CallbacksDescriptor d(Handle<Name>(Name::cast(script_source->name())), |
| 1794 | script_source, attribs); |
| 1795 | script_map->AppendDescriptor(&d); |
| 1796 | } |
| 1797 | |
| 1798 | Handle<AccessorInfo> script_type = |
| 1799 | Accessors::ScriptTypeInfo(isolate(), attribs); |
| 1800 | { |
| 1801 | CallbacksDescriptor d(Handle<Name>(Name::cast(script_type->name())), |
| 1802 | script_type, attribs); |
| 1803 | script_map->AppendDescriptor(&d); |
| 1804 | } |
| 1805 | |
| 1806 | Handle<AccessorInfo> script_compilation_type = |
| 1807 | Accessors::ScriptCompilationTypeInfo(isolate(), attribs); |
| 1808 | { |
| 1809 | CallbacksDescriptor d( |
| 1810 | Handle<Name>(Name::cast(script_compilation_type->name())), |
| 1811 | script_compilation_type, attribs); |
| 1812 | script_map->AppendDescriptor(&d); |
| 1813 | } |
| 1814 | |
| 1815 | Handle<AccessorInfo> script_line_ends = |
| 1816 | Accessors::ScriptLineEndsInfo(isolate(), attribs); |
| 1817 | { |
| 1818 | CallbacksDescriptor d(Handle<Name>(Name::cast(script_line_ends->name())), |
| 1819 | script_line_ends, attribs); |
| 1820 | script_map->AppendDescriptor(&d); |
| 1821 | } |
| 1822 | |
| 1823 | Handle<AccessorInfo> script_context_data = |
| 1824 | Accessors::ScriptContextDataInfo(isolate(), attribs); |
| 1825 | { |
| 1826 | CallbacksDescriptor d( |
| 1827 | Handle<Name>(Name::cast(script_context_data->name())), |
| 1828 | script_context_data, attribs); |
| 1829 | script_map->AppendDescriptor(&d); |
| 1830 | } |
| 1831 | |
| 1832 | Handle<AccessorInfo> script_eval_from_script = |
| 1833 | Accessors::ScriptEvalFromScriptInfo(isolate(), attribs); |
| 1834 | { |
| 1835 | CallbacksDescriptor d( |
| 1836 | Handle<Name>(Name::cast(script_eval_from_script->name())), |
| 1837 | script_eval_from_script, attribs); |
| 1838 | script_map->AppendDescriptor(&d); |
| 1839 | } |
| 1840 | |
| 1841 | Handle<AccessorInfo> script_eval_from_script_position = |
| 1842 | Accessors::ScriptEvalFromScriptPositionInfo(isolate(), attribs); |
| 1843 | { |
| 1844 | CallbacksDescriptor d( |
| 1845 | Handle<Name>(Name::cast(script_eval_from_script_position->name())), |
| 1846 | script_eval_from_script_position, attribs); |
| 1847 | script_map->AppendDescriptor(&d); |
| 1848 | } |
| 1849 | |
| 1850 | Handle<AccessorInfo> script_eval_from_function_name = |
| 1851 | Accessors::ScriptEvalFromFunctionNameInfo(isolate(), attribs); |
| 1852 | { |
| 1853 | CallbacksDescriptor d( |
| 1854 | Handle<Name>(Name::cast(script_eval_from_function_name->name())), |
| 1855 | script_eval_from_function_name, attribs); |
| 1856 | script_map->AppendDescriptor(&d); |
| 1857 | } |
| 1858 | |
| 1859 | Handle<AccessorInfo> script_source_url = |
| 1860 | Accessors::ScriptSourceUrlInfo(isolate(), attribs); |
| 1861 | { |
| 1862 | CallbacksDescriptor d(Handle<Name>(Name::cast(script_source_url->name())), |
| 1863 | script_source_url, attribs); |
| 1864 | script_map->AppendDescriptor(&d); |
| 1865 | } |
| 1866 | |
| 1867 | Handle<AccessorInfo> script_source_mapping_url = |
| 1868 | Accessors::ScriptSourceMappingUrlInfo(isolate(), attribs); |
| 1869 | { |
| 1870 | CallbacksDescriptor d( |
| 1871 | Handle<Name>(Name::cast(script_source_mapping_url->name())), |
| 1872 | script_source_mapping_url, attribs); |
| 1873 | script_map->AppendDescriptor(&d); |
| 1874 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1875 | |
| 1876 | // Allocate the empty script. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1877 | Handle<Script> script = factory()->NewScript(factory()->empty_string()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1878 | script->set_type(Smi::FromInt(Script::TYPE_NATIVE)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1879 | heap()->public_set_empty_script(*script); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1880 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1881 | { |
| 1882 | // Builtin function for OpaqueReference -- a JSValue-based object, |
| 1883 | // that keeps its field isolated from JavaScript code. It may store |
| 1884 | // objects, that JavaScript code may not access. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1885 | Handle<JSFunction> opaque_reference_fun = InstallFunction( |
| 1886 | builtins, "OpaqueReference", JS_VALUE_TYPE, JSValue::kSize, |
| 1887 | isolate()->initial_object_prototype(), Builtins::kIllegal); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1888 | Handle<JSObject> prototype = |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1889 | factory()->NewJSObject(isolate()->object_function(), TENURED); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1890 | Accessors::FunctionSetPrototype(opaque_reference_fun, prototype).Assert(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1891 | native_context()->set_opaque_reference_function(*opaque_reference_fun); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1892 | } |
| 1893 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1894 | // InternalArrays should not use Smi-Only array optimizations. There are too |
| 1895 | // many places in the C++ runtime code (e.g. RegEx) that assume that |
| 1896 | // elements in InternalArrays can be set to non-Smi values without going |
| 1897 | // through a common bottleneck that would make the SMI_ONLY -> FAST_ELEMENT |
| 1898 | // transition easy to trap. Moreover, they rarely are smi-only. |
| 1899 | { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1900 | Handle<JSFunction> array_function = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1901 | InstallInternalArray(builtins, "InternalArray", FAST_HOLEY_ELEMENTS); |
| 1902 | native_context()->set_internal_array_function(*array_function); |
| 1903 | } |
| 1904 | |
| 1905 | { |
| 1906 | InstallInternalArray(builtins, "InternalPackedArray", FAST_ELEMENTS); |
| 1907 | } |
| 1908 | |
| 1909 | { // -- S e t I t e r a t o r |
| 1910 | Handle<JSFunction> set_iterator_function = InstallFunction( |
| 1911 | builtins, "SetIterator", JS_SET_ITERATOR_TYPE, JSSetIterator::kSize, |
| 1912 | isolate()->initial_object_prototype(), Builtins::kIllegal); |
| 1913 | native_context()->set_set_iterator_map( |
| 1914 | set_iterator_function->initial_map()); |
| 1915 | } |
| 1916 | |
| 1917 | { // -- M a p I t e r a t o r |
| 1918 | Handle<JSFunction> map_iterator_function = InstallFunction( |
| 1919 | builtins, "MapIterator", JS_MAP_ITERATOR_TYPE, JSMapIterator::kSize, |
| 1920 | isolate()->initial_object_prototype(), Builtins::kIllegal); |
| 1921 | native_context()->set_map_iterator_map( |
| 1922 | map_iterator_function->initial_map()); |
| 1923 | } |
| 1924 | |
| 1925 | { |
| 1926 | // Create generator meta-objects and install them on the builtins object. |
| 1927 | Handle<JSObject> builtins(native_context()->builtins()); |
| 1928 | Handle<JSObject> generator_object_prototype = |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1929 | factory()->NewJSObject(isolate()->object_function(), TENURED); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1930 | Handle<JSFunction> generator_function_prototype = |
| 1931 | InstallFunction(builtins, "GeneratorFunctionPrototype", |
| 1932 | JS_FUNCTION_TYPE, JSFunction::kHeaderSize, |
| 1933 | generator_object_prototype, Builtins::kIllegal); |
| 1934 | InstallFunction(builtins, "GeneratorFunction", JS_FUNCTION_TYPE, |
| 1935 | JSFunction::kSize, generator_function_prototype, |
| 1936 | Builtins::kIllegal); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1937 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1938 | // Create maps for generator functions and their prototypes. Store those |
| 1939 | // maps in the native context. |
| 1940 | Handle<Map> generator_function_map = |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1941 | Map::Copy(sloppy_function_map_writable_prototype_, "GeneratorFunction"); |
| 1942 | generator_function_map->SetPrototype(generator_function_prototype); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1943 | native_context()->set_sloppy_generator_function_map( |
| 1944 | *generator_function_map); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1945 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1946 | // The "arguments" and "caller" instance properties aren't specified, so |
| 1947 | // technically we could leave them out. They make even less sense for |
| 1948 | // generators than for functions. Still, the same argument that it makes |
| 1949 | // sense to keep them around but poisoned in strict mode applies to |
| 1950 | // generators as well. With poisoned accessors, naive callers can still |
| 1951 | // iterate over the properties without accessing them. |
| 1952 | // |
| 1953 | // We can't use PoisonArgumentsAndCaller because that mutates accessor pairs |
| 1954 | // in place, and the initial state of the generator function map shares the |
| 1955 | // accessor pair with sloppy functions. Also the error message should be |
| 1956 | // different. Also unhappily, we can't use the API accessors to implement |
| 1957 | // poisoning, because API accessors present themselves as data properties, |
| 1958 | // not accessor properties, and so getOwnPropertyDescriptor raises an |
| 1959 | // exception as it tries to get the values. Sadness. |
| 1960 | Handle<AccessorPair> poison_pair(factory()->NewAccessorPair()); |
| 1961 | PropertyAttributes rw_attribs = |
| 1962 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE); |
| 1963 | Handle<JSFunction> poison_function = GetGeneratorPoisonFunction(); |
| 1964 | poison_pair->set_getter(*poison_function); |
| 1965 | poison_pair->set_setter(*poison_function); |
| 1966 | ReplaceAccessors(generator_function_map, factory()->arguments_string(), |
| 1967 | rw_attribs, poison_pair); |
| 1968 | ReplaceAccessors(generator_function_map, factory()->caller_string(), |
| 1969 | rw_attribs, poison_pair); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1970 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1971 | Handle<Map> strict_function_map(native_context()->strict_function_map()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1972 | Handle<Map> strict_generator_function_map = |
| 1973 | Map::Copy(strict_function_map, "StrictGeneratorFunction"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1974 | // "arguments" and "caller" already poisoned. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1975 | strict_generator_function_map->SetPrototype(generator_function_prototype); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1976 | native_context()->set_strict_generator_function_map( |
| 1977 | *strict_generator_function_map); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1978 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1979 | Handle<JSFunction> object_function(native_context()->object_function()); |
| 1980 | Handle<Map> generator_object_prototype_map = Map::Create(isolate(), 0); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1981 | generator_object_prototype_map->SetPrototype(generator_object_prototype); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1982 | native_context()->set_generator_object_prototype_map( |
| 1983 | *generator_object_prototype_map); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1984 | } |
| 1985 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1986 | if (FLAG_disable_native_files) { |
| 1987 | PrintF("Warning: Running without installed natives!\n"); |
| 1988 | return true; |
| 1989 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1990 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1991 | // Install public symbols. |
| 1992 | { |
| 1993 | static const PropertyAttributes attributes = |
| 1994 | static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE); |
| 1995 | #define INSTALL_PUBLIC_SYMBOL(name, varname, description) \ |
| 1996 | Handle<String> varname = factory()->NewStringFromStaticChars(#varname); \ |
| 1997 | JSObject::AddProperty(builtins, varname, factory()->name(), attributes); |
| 1998 | PUBLIC_SYMBOL_LIST(INSTALL_PUBLIC_SYMBOL) |
| 1999 | #undef INSTALL_PUBLIC_SYMBOL |
| 2000 | } |
| 2001 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2002 | // Install natives. |
| 2003 | for (int i = Natives::GetDebuggerCount(); |
| 2004 | i < Natives::GetBuiltinsCount(); |
| 2005 | i++) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2006 | if (!CompileBuiltin(isolate(), i)) return false; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2007 | // TODO(ager): We really only need to install the JS builtin |
| 2008 | // functions on the builtins object after compiling and running |
| 2009 | // runtime.js. |
| 2010 | if (!InstallJSBuiltins(builtins)) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | InstallNativeFunctions(); |
| 2014 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2015 | // Store the map for the string prototype after the natives has been compiled |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2016 | // and the String function has been set up. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2017 | Handle<JSFunction> string_function(native_context()->string_function()); |
| 2018 | DCHECK(JSObject::cast( |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2019 | string_function->initial_map()->prototype())->HasFastProperties()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2020 | native_context()->set_string_function_prototype_map( |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2021 | HeapObject::cast(string_function->initial_map()->prototype())->map()); |
| 2022 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2023 | // Install Function.prototype.call and apply. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2024 | { |
| 2025 | Handle<String> key = factory()->Function_string(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2026 | Handle<JSFunction> function = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2027 | Handle<JSFunction>::cast(Object::GetProperty( |
| 2028 | handle(native_context()->global_object()), key).ToHandleChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2029 | Handle<JSObject> proto = |
| 2030 | Handle<JSObject>(JSObject::cast(function->instance_prototype())); |
| 2031 | |
| 2032 | // Install the call and the apply functions. |
| 2033 | Handle<JSFunction> call = |
| 2034 | InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2035 | MaybeHandle<JSObject>(), Builtins::kFunctionCall); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2036 | Handle<JSFunction> apply = |
| 2037 | InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2038 | MaybeHandle<JSObject>(), Builtins::kFunctionApply); |
| 2039 | if (FLAG_vector_ics) { |
| 2040 | // Apply embeds an IC, so we need a type vector of size 1 in the shared |
| 2041 | // function info. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2042 | FeedbackVectorSpec spec(0, 1); |
| 2043 | spec.SetKind(0, Code::CALL_IC); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2044 | Handle<TypeFeedbackVector> feedback_vector = |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2045 | factory()->NewTypeFeedbackVector(spec); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2046 | apply->shared()->set_feedback_vector(*feedback_vector); |
| 2047 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2048 | |
| 2049 | // Make sure that Function.prototype.call appears to be compiled. |
| 2050 | // The code will never be called, but inline caching for call will |
| 2051 | // only work if it appears to be compiled. |
| 2052 | call->shared()->DontAdaptArguments(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2053 | DCHECK(call->is_compiled()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2054 | |
| 2055 | // Set the expected parameters for apply to 2; required by builtin. |
| 2056 | apply->shared()->set_formal_parameter_count(2); |
| 2057 | |
| 2058 | // Set the lengths for the functions to satisfy ECMA-262. |
| 2059 | call->shared()->set_length(1); |
| 2060 | apply->shared()->set_length(2); |
| 2061 | } |
| 2062 | |
Ben Murdoch | 42effa5 | 2011-08-19 16:40:31 +0100 | [diff] [blame] | 2063 | InstallBuiltinFunctionIds(); |
| 2064 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2065 | // Create a constructor for RegExp results (a variant of Array that |
| 2066 | // predefines the two properties index and match). |
| 2067 | { |
| 2068 | // RegExpResult initial map. |
| 2069 | |
| 2070 | // Find global.Array.prototype to inherit from. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2071 | Handle<JSFunction> array_constructor(native_context()->array_function()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2072 | Handle<JSObject> array_prototype( |
| 2073 | JSObject::cast(array_constructor->instance_prototype())); |
| 2074 | |
| 2075 | // Add initial map. |
| 2076 | Handle<Map> initial_map = |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2077 | factory()->NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2078 | initial_map->set_constructor(*array_constructor); |
| 2079 | |
| 2080 | // Set prototype on map. |
| 2081 | initial_map->set_non_instance_prototype(false); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2082 | initial_map->SetPrototype(array_prototype); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2083 | |
| 2084 | // Update map with length accessor from Array and add "index" and "input". |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2085 | Map::EnsureDescriptorSlack(initial_map, 3); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 2086 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2087 | { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2088 | JSFunction* array_function = native_context()->array_function(); |
| 2089 | Handle<DescriptorArray> array_descriptors( |
| 2090 | array_function->initial_map()->instance_descriptors()); |
| 2091 | Handle<String> length = factory()->length_string(); |
| 2092 | int old = array_descriptors->SearchWithCache( |
| 2093 | *length, array_function->initial_map()); |
| 2094 | DCHECK(old != DescriptorArray::kNotFound); |
| 2095 | CallbacksDescriptor desc(length, |
| 2096 | handle(array_descriptors->GetValue(old), |
| 2097 | isolate()), |
| 2098 | array_descriptors->GetDetails(old).attributes()); |
| 2099 | initial_map->AppendDescriptor(&desc); |
| 2100 | } |
| 2101 | { |
| 2102 | FieldDescriptor index_field(factory()->index_string(), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2103 | JSRegExpResult::kIndexIndex, |
| 2104 | NONE, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2105 | Representation::Tagged()); |
| 2106 | initial_map->AppendDescriptor(&index_field); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2110 | FieldDescriptor input_field(factory()->input_string(), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2111 | JSRegExpResult::kInputIndex, |
| 2112 | NONE, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2113 | Representation::Tagged()); |
| 2114 | initial_map->AppendDescriptor(&input_field); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2115 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2116 | |
| 2117 | initial_map->set_inobject_properties(2); |
| 2118 | initial_map->set_pre_allocated_property_fields(2); |
| 2119 | initial_map->set_unused_property_fields(0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2120 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2121 | native_context()->set_regexp_result_map(*initial_map); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2122 | } |
| 2123 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2124 | // Add @@iterator method to the arguments object maps. |
| 2125 | { |
| 2126 | PropertyAttributes attribs = DONT_ENUM; |
| 2127 | Handle<AccessorInfo> arguments_iterator = |
| 2128 | Accessors::ArgumentsIteratorInfo(isolate(), attribs); |
| 2129 | { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2130 | CallbacksDescriptor d(factory()->iterator_symbol(), arguments_iterator, |
| 2131 | attribs); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2132 | Handle<Map> map(native_context()->sloppy_arguments_map()); |
| 2133 | Map::EnsureDescriptorSlack(map, 1); |
| 2134 | map->AppendDescriptor(&d); |
| 2135 | } |
| 2136 | { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2137 | CallbacksDescriptor d(factory()->iterator_symbol(), arguments_iterator, |
| 2138 | attribs); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2139 | Handle<Map> map(native_context()->aliased_arguments_map()); |
| 2140 | Map::EnsureDescriptorSlack(map, 1); |
| 2141 | map->AppendDescriptor(&d); |
| 2142 | } |
| 2143 | { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2144 | CallbacksDescriptor d(factory()->iterator_symbol(), arguments_iterator, |
| 2145 | attribs); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2146 | Handle<Map> map(native_context()->strict_arguments_map()); |
| 2147 | Map::EnsureDescriptorSlack(map, 1); |
| 2148 | map->AppendDescriptor(&d); |
| 2149 | } |
| 2150 | } |
| 2151 | |
| 2152 | #ifdef VERIFY_HEAP |
| 2153 | builtins->ObjectVerify(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2154 | #endif |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2155 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2156 | return true; |
| 2157 | } |
| 2158 | |
| 2159 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2160 | bool Genesis::InstallExperimentalNatives() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2161 | static const char* harmony_arrays_natives[] = { |
| 2162 | "native harmony-array.js", "native harmony-typedarray.js", NULL}; |
| 2163 | static const char* harmony_array_includes_natives[] = { |
| 2164 | "native harmony-array-includes.js", NULL}; |
| 2165 | static const char* harmony_proxies_natives[] = {"native proxy.js", NULL}; |
| 2166 | static const char* harmony_strings_natives[] = {"native harmony-string.js", |
| 2167 | NULL}; |
| 2168 | static const char* harmony_classes_natives[] = {"native harmony-classes.js", |
| 2169 | NULL}; |
| 2170 | static const char* harmony_modules_natives[] = {NULL}; |
| 2171 | static const char* harmony_scoping_natives[] = {NULL}; |
| 2172 | static const char* harmony_object_literals_natives[] = {NULL}; |
| 2173 | static const char* harmony_regexps_natives[] = { |
| 2174 | "native harmony-regexp.js", NULL}; |
| 2175 | static const char* harmony_arrow_functions_natives[] = {NULL}; |
| 2176 | static const char* harmony_numeric_literals_natives[] = {NULL}; |
| 2177 | static const char* harmony_tostring_natives[] = {"native harmony-tostring.js", |
| 2178 | NULL}; |
| 2179 | static const char* harmony_templates_natives[] = { |
| 2180 | "native harmony-templates.js", NULL}; |
| 2181 | static const char* harmony_sloppy_natives[] = {NULL}; |
| 2182 | static const char* harmony_unicode_natives[] = {NULL}; |
| 2183 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2184 | for (int i = ExperimentalNatives::GetDebuggerCount(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2185 | i < ExperimentalNatives::GetBuiltinsCount(); i++) { |
| 2186 | #define INSTALL_EXPERIMENTAL_NATIVES(id, desc) \ |
| 2187 | if (FLAG_##id) { \ |
| 2188 | for (size_t j = 0; id##_natives[j] != NULL; j++) { \ |
| 2189 | Vector<const char> script_name = ExperimentalNatives::GetScriptName(i); \ |
| 2190 | if (strncmp(script_name.start(), id##_natives[j], \ |
| 2191 | script_name.length()) == 0) { \ |
| 2192 | if (!CompileExperimentalBuiltin(isolate(), i)) return false; \ |
| 2193 | } \ |
| 2194 | } \ |
| 2195 | } |
| 2196 | HARMONY_INPROGRESS(INSTALL_EXPERIMENTAL_NATIVES); |
| 2197 | HARMONY_STAGED(INSTALL_EXPERIMENTAL_NATIVES); |
| 2198 | HARMONY_SHIPPING(INSTALL_EXPERIMENTAL_NATIVES); |
| 2199 | #undef INSTALL_EXPERIMENTAL_NATIVES |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | InstallExperimentalNativeFunctions(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2203 | return true; |
| 2204 | } |
| 2205 | |
| 2206 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2207 | static void InstallBuiltinFunctionId(Handle<JSObject> holder, |
| 2208 | const char* function_name, |
| 2209 | BuiltinFunctionId id) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2210 | Isolate* isolate = holder->GetIsolate(); |
| 2211 | Handle<Object> function_object = |
| 2212 | Object::GetProperty(isolate, holder, function_name).ToHandleChecked(); |
| 2213 | Handle<JSFunction> function = Handle<JSFunction>::cast(function_object); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2214 | function->shared()->set_function_data(Smi::FromInt(id)); |
| 2215 | } |
| 2216 | |
| 2217 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2218 | void Genesis::InstallBuiltinFunctionIds() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2219 | HandleScope scope(isolate()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2220 | #define INSTALL_BUILTIN_ID(holder_expr, fun_name, name) \ |
| 2221 | { \ |
| 2222 | Handle<JSObject> holder = ResolveBuiltinIdHolder( \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2223 | native_context(), #holder_expr); \ |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2224 | BuiltinFunctionId id = k##name; \ |
| 2225 | InstallBuiltinFunctionId(holder, #fun_name, id); \ |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2226 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2227 | FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID) |
| 2228 | #undef INSTALL_BUILTIN_ID |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2232 | // Do not forget to update macros.py with named constant |
| 2233 | // of cache id. |
| 2234 | #define JSFUNCTION_RESULT_CACHE_LIST(F) \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2235 | F(16, native_context()->regexp_function()) |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2236 | |
| 2237 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2238 | static FixedArray* CreateCache(int size, Handle<JSFunction> factory_function) { |
| 2239 | Factory* factory = factory_function->GetIsolate()->factory(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2240 | // Caches are supposed to live for a long time, allocate in old space. |
| 2241 | int array_size = JSFunctionResultCache::kEntriesIndex + 2 * size; |
| 2242 | // Cannot use cast as object is not fully initialized yet. |
| 2243 | JSFunctionResultCache* cache = reinterpret_cast<JSFunctionResultCache*>( |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2244 | *factory->NewFixedArrayWithHoles(array_size, TENURED)); |
| 2245 | cache->set(JSFunctionResultCache::kFactoryIndex, *factory_function); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2246 | cache->MakeZeroSize(); |
| 2247 | return cache; |
| 2248 | } |
| 2249 | |
| 2250 | |
| 2251 | void Genesis::InstallJSFunctionResultCaches() { |
| 2252 | const int kNumberOfCaches = 0 + |
| 2253 | #define F(size, func) + 1 |
| 2254 | JSFUNCTION_RESULT_CACHE_LIST(F) |
| 2255 | #undef F |
| 2256 | ; |
| 2257 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2258 | Handle<FixedArray> caches = |
| 2259 | factory()->NewFixedArray(kNumberOfCaches, TENURED); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2260 | |
| 2261 | int index = 0; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 2262 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2263 | #define F(size, func) do { \ |
| 2264 | FixedArray* cache = CreateCache((size), Handle<JSFunction>(func)); \ |
| 2265 | caches->set(index++, cache); \ |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 2266 | } while (false) |
| 2267 | |
| 2268 | JSFUNCTION_RESULT_CACHE_LIST(F); |
| 2269 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2270 | #undef F |
| 2271 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2272 | native_context()->set_jsfunction_result_caches(*caches); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2273 | } |
| 2274 | |
| 2275 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2276 | void Genesis::InitializeNormalizedMapCaches() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2277 | Handle<NormalizedMapCache> cache = NormalizedMapCache::New(isolate()); |
| 2278 | native_context()->set_normalized_map_cache(*cache); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2279 | } |
| 2280 | |
| 2281 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2282 | bool Bootstrapper::InstallExtensions(Handle<Context> native_context, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2283 | v8::ExtensionConfiguration* extensions) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2284 | BootstrapperActive active(this); |
| 2285 | SaveContext saved_context(isolate_); |
| 2286 | isolate_->set_context(*native_context); |
| 2287 | return Genesis::InstallExtensions(native_context, extensions) && |
| 2288 | Genesis::InstallSpecialObjects(native_context); |
| 2289 | } |
| 2290 | |
| 2291 | |
| 2292 | bool Genesis::InstallSpecialObjects(Handle<Context> native_context) { |
| 2293 | Isolate* isolate = native_context->GetIsolate(); |
| 2294 | // Don't install extensions into the snapshot. |
| 2295 | if (isolate->serializer_enabled()) return true; |
| 2296 | |
| 2297 | Factory* factory = isolate->factory(); |
| 2298 | HandleScope scope(isolate); |
| 2299 | Handle<JSGlobalObject> global(JSGlobalObject::cast( |
| 2300 | native_context->global_object())); |
| 2301 | |
| 2302 | Handle<JSObject> Error = Handle<JSObject>::cast( |
| 2303 | Object::GetProperty(isolate, global, "Error").ToHandleChecked()); |
| 2304 | Handle<String> name = |
| 2305 | factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("stackTraceLimit")); |
| 2306 | Handle<Smi> stack_trace_limit(Smi::FromInt(FLAG_stack_trace_limit), isolate); |
| 2307 | JSObject::AddProperty(Error, name, stack_trace_limit, NONE); |
| 2308 | |
| 2309 | // Expose the natives in global if a name for it is specified. |
| 2310 | if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) { |
| 2311 | Handle<String> natives = |
| 2312 | factory->InternalizeUtf8String(FLAG_expose_natives_as); |
| 2313 | uint32_t dummy_index; |
| 2314 | if (natives->AsArrayIndex(&dummy_index)) return true; |
| 2315 | JSObject::AddProperty(global, natives, handle(global->builtins()), |
| 2316 | DONT_ENUM); |
| 2317 | } |
| 2318 | |
| 2319 | // Expose the stack trace symbol to native JS. |
| 2320 | RETURN_ON_EXCEPTION_VALUE(isolate, |
| 2321 | JSObject::SetOwnPropertyIgnoreAttributes( |
| 2322 | handle(native_context->builtins(), isolate), |
| 2323 | factory->InternalizeOneByteString( |
| 2324 | STATIC_CHAR_VECTOR("stack_trace_symbol")), |
| 2325 | factory->stack_trace_symbol(), NONE), |
| 2326 | false); |
| 2327 | |
| 2328 | // Expose the debug global object in global if a name for it is specified. |
| 2329 | if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) { |
| 2330 | // If loading fails we just bail out without installing the |
| 2331 | // debugger but without tanking the whole context. |
| 2332 | Debug* debug = isolate->debug(); |
| 2333 | if (!debug->Load()) return true; |
| 2334 | Handle<Context> debug_context = debug->debug_context(); |
| 2335 | // Set the security token for the debugger context to the same as |
| 2336 | // the shell native context to allow calling between these (otherwise |
| 2337 | // exposing debug global object doesn't make much sense). |
| 2338 | debug_context->set_security_token(native_context->security_token()); |
| 2339 | Handle<String> debug_string = |
| 2340 | factory->InternalizeUtf8String(FLAG_expose_debug_as); |
| 2341 | uint32_t index; |
| 2342 | if (debug_string->AsArrayIndex(&index)) return true; |
| 2343 | Handle<Object> global_proxy(debug_context->global_proxy(), isolate); |
| 2344 | JSObject::AddProperty(global, debug_string, global_proxy, DONT_ENUM); |
| 2345 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2346 | return true; |
| 2347 | } |
| 2348 | |
| 2349 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2350 | static uint32_t Hash(RegisteredExtension* extension) { |
| 2351 | return v8::internal::ComputePointerHash(extension); |
| 2352 | } |
| 2353 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2354 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2355 | Genesis::ExtensionStates::ExtensionStates() : map_(HashMap::PointersMatch, 8) {} |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2356 | |
| 2357 | Genesis::ExtensionTraversalState Genesis::ExtensionStates::get_state( |
| 2358 | RegisteredExtension* extension) { |
| 2359 | i::HashMap::Entry* entry = map_.Lookup(extension, Hash(extension), false); |
| 2360 | if (entry == NULL) { |
| 2361 | return UNVISITED; |
| 2362 | } |
| 2363 | return static_cast<ExtensionTraversalState>( |
| 2364 | reinterpret_cast<intptr_t>(entry->value)); |
| 2365 | } |
| 2366 | |
| 2367 | void Genesis::ExtensionStates::set_state(RegisteredExtension* extension, |
| 2368 | ExtensionTraversalState state) { |
| 2369 | map_.Lookup(extension, Hash(extension), true)->value = |
| 2370 | reinterpret_cast<void*>(static_cast<intptr_t>(state)); |
| 2371 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2372 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2373 | |
| 2374 | bool Genesis::InstallExtensions(Handle<Context> native_context, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2375 | v8::ExtensionConfiguration* extensions) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2376 | Isolate* isolate = native_context->GetIsolate(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2377 | ExtensionStates extension_states; // All extensions have state UNVISITED. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2378 | return InstallAutoExtensions(isolate, &extension_states) && |
| 2379 | (!FLAG_expose_free_buffer || |
| 2380 | InstallExtension(isolate, "v8/free-buffer", &extension_states)) && |
| 2381 | (!FLAG_expose_gc || |
| 2382 | InstallExtension(isolate, "v8/gc", &extension_states)) && |
| 2383 | (!FLAG_expose_externalize_string || |
| 2384 | InstallExtension(isolate, "v8/externalize", &extension_states)) && |
| 2385 | (!FLAG_track_gc_object_stats || |
| 2386 | InstallExtension(isolate, "v8/statistics", &extension_states)) && |
| 2387 | (!FLAG_expose_trigger_failure || |
| 2388 | InstallExtension(isolate, "v8/trigger-failure", &extension_states)) && |
| 2389 | InstallRequestedExtensions(isolate, extensions, &extension_states); |
| 2390 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2391 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2392 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2393 | bool Genesis::InstallAutoExtensions(Isolate* isolate, |
| 2394 | ExtensionStates* extension_states) { |
| 2395 | for (v8::RegisteredExtension* it = v8::RegisteredExtension::first_extension(); |
| 2396 | it != NULL; |
| 2397 | it = it->next()) { |
| 2398 | if (it->extension()->auto_enable() && |
| 2399 | !InstallExtension(isolate, it, extension_states)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2400 | return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2401 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2402 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2403 | return true; |
| 2404 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2405 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2406 | |
| 2407 | bool Genesis::InstallRequestedExtensions(Isolate* isolate, |
| 2408 | v8::ExtensionConfiguration* extensions, |
| 2409 | ExtensionStates* extension_states) { |
| 2410 | for (const char** it = extensions->begin(); it != extensions->end(); ++it) { |
| 2411 | if (!InstallExtension(isolate, *it, extension_states)) return false; |
| 2412 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2413 | return true; |
| 2414 | } |
| 2415 | |
| 2416 | |
| 2417 | // Installs a named extension. This methods is unoptimized and does |
| 2418 | // not scale well if we want to support a large number of extensions. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2419 | bool Genesis::InstallExtension(Isolate* isolate, |
| 2420 | const char* name, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2421 | ExtensionStates* extension_states) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2422 | for (v8::RegisteredExtension* it = v8::RegisteredExtension::first_extension(); |
| 2423 | it != NULL; |
| 2424 | it = it->next()) { |
| 2425 | if (strcmp(name, it->extension()->name()) == 0) { |
| 2426 | return InstallExtension(isolate, it, extension_states); |
| 2427 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2428 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2429 | return Utils::ApiCheck(false, |
| 2430 | "v8::Context::New()", |
| 2431 | "Cannot find required extension"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2435 | bool Genesis::InstallExtension(Isolate* isolate, |
| 2436 | v8::RegisteredExtension* current, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2437 | ExtensionStates* extension_states) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2438 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2439 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2440 | if (extension_states->get_state(current) == INSTALLED) return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2441 | // The current node has already been visited so there must be a |
| 2442 | // cycle in the dependency graph; fail. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2443 | if (!Utils::ApiCheck(extension_states->get_state(current) != VISITED, |
| 2444 | "v8::Context::New()", |
| 2445 | "Circular extension dependency")) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2446 | return false; |
| 2447 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2448 | DCHECK(extension_states->get_state(current) == UNVISITED); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2449 | extension_states->set_state(current, VISITED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2450 | v8::Extension* extension = current->extension(); |
| 2451 | // Install the extension's dependencies |
| 2452 | for (int i = 0; i < extension->dependency_count(); i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2453 | if (!InstallExtension(isolate, |
| 2454 | extension->dependencies()[i], |
| 2455 | extension_states)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2456 | return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2457 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2458 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2459 | // We do not expect this to throw an exception. Change this if it does. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2460 | Handle<String> source_code = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2461 | isolate->factory() |
| 2462 | ->NewExternalStringFromOneByte(extension->source()) |
| 2463 | .ToHandleChecked(); |
| 2464 | bool result = CompileScriptCached(isolate, |
| 2465 | CStrVector(extension->name()), |
| 2466 | source_code, |
| 2467 | isolate->bootstrapper()->extensions_cache(), |
| 2468 | extension, |
| 2469 | Handle<Context>(isolate->context()), |
| 2470 | false); |
| 2471 | DCHECK(isolate->has_pending_exception() != result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2472 | if (!result) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2473 | // We print out the name of the extension that fail to install. |
| 2474 | // When an error is thrown during bootstrapping we automatically print |
| 2475 | // the line number at which this happened to the console in the isolate |
| 2476 | // error throwing functionality. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2477 | base::OS::PrintError("Error installing extension '%s'.\n", |
| 2478 | current->extension()->name()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2479 | isolate->clear_pending_exception(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2480 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2481 | extension_states->set_state(current, INSTALLED); |
| 2482 | isolate->NotifyExtensionInstalled(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2483 | return result; |
| 2484 | } |
| 2485 | |
| 2486 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2487 | bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2488 | HandleScope scope(isolate()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2489 | for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) { |
| 2490 | Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2491 | Handle<Object> function_object = Object::GetProperty( |
| 2492 | isolate(), builtins, Builtins::GetName(id)).ToHandleChecked(); |
| 2493 | Handle<JSFunction> function = Handle<JSFunction>::cast(function_object); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2494 | builtins->set_javascript_builtin(id, *function); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2495 | // TODO(mstarzinger): This is just a temporary hack to make TurboFan work, |
| 2496 | // the correct solution is to restore the context register after invoking |
| 2497 | // builtins from full-codegen. |
| 2498 | function->shared()->set_optimization_disabled(true); |
| 2499 | if (!Compiler::EnsureCompiled(function, CLEAR_EXCEPTION)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2500 | return false; |
| 2501 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2502 | builtins->set_javascript_builtin_code(id, function->shared()->code()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2503 | } |
| 2504 | return true; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
| 2507 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2508 | bool Genesis::ConfigureGlobalObjects( |
| 2509 | v8::Handle<v8::ObjectTemplate> global_proxy_template) { |
| 2510 | Handle<JSObject> global_proxy( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2511 | JSObject::cast(native_context()->global_proxy())); |
| 2512 | Handle<JSObject> global_object( |
| 2513 | JSObject::cast(native_context()->global_object())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2514 | |
| 2515 | if (!global_proxy_template.IsEmpty()) { |
| 2516 | // Configure the global proxy object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2517 | Handle<ObjectTemplateInfo> global_proxy_data = |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2518 | v8::Utils::OpenHandle(*global_proxy_template); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2519 | if (!ConfigureApiObject(global_proxy, global_proxy_data)) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2520 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2521 | // Configure the global object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2522 | Handle<FunctionTemplateInfo> proxy_constructor( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2523 | FunctionTemplateInfo::cast(global_proxy_data->constructor())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2524 | if (!proxy_constructor->prototype_template()->IsUndefined()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2525 | Handle<ObjectTemplateInfo> global_object_data( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2526 | ObjectTemplateInfo::cast(proxy_constructor->prototype_template())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2527 | if (!ConfigureApiObject(global_object, global_object_data)) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2528 | } |
| 2529 | } |
| 2530 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2531 | SetObjectPrototype(global_proxy, global_object); |
| 2532 | |
| 2533 | native_context()->set_initial_array_prototype( |
| 2534 | JSArray::cast(native_context()->array_function()->prototype())); |
| 2535 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2536 | return true; |
| 2537 | } |
| 2538 | |
| 2539 | |
| 2540 | bool Genesis::ConfigureApiObject(Handle<JSObject> object, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2541 | Handle<ObjectTemplateInfo> object_template) { |
| 2542 | DCHECK(!object_template.is_null()); |
| 2543 | DCHECK(FunctionTemplateInfo::cast(object_template->constructor()) |
| 2544 | ->IsTemplateFor(object->map()));; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2545 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2546 | MaybeHandle<JSObject> maybe_obj = |
| 2547 | Execution::InstantiateObject(object_template); |
| 2548 | Handle<JSObject> obj; |
| 2549 | if (!maybe_obj.ToHandle(&obj)) { |
| 2550 | DCHECK(isolate()->has_pending_exception()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2551 | isolate()->clear_pending_exception(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2552 | return false; |
| 2553 | } |
| 2554 | TransferObject(obj, object); |
| 2555 | return true; |
| 2556 | } |
| 2557 | |
| 2558 | |
| 2559 | void Genesis::TransferNamedProperties(Handle<JSObject> from, |
| 2560 | Handle<JSObject> to) { |
| 2561 | if (from->HasFastProperties()) { |
| 2562 | Handle<DescriptorArray> descs = |
| 2563 | Handle<DescriptorArray>(from->map()->instance_descriptors()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2564 | for (int i = 0; i < from->map()->NumberOfOwnDescriptors(); i++) { |
| 2565 | PropertyDetails details = descs->GetDetails(i); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2566 | switch (details.type()) { |
| 2567 | case FIELD: { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2568 | HandleScope inner(isolate()); |
| 2569 | Handle<Name> key = Handle<Name>(descs->GetKey(i)); |
| 2570 | FieldIndex index = FieldIndex::ForDescriptor(from->map(), i); |
| 2571 | DCHECK(!descs->GetDetails(i).representation().IsDouble()); |
| 2572 | Handle<Object> value = Handle<Object>(from->RawFastPropertyAt(index), |
| 2573 | isolate()); |
| 2574 | JSObject::AddProperty(to, key, value, details.attributes()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2575 | break; |
| 2576 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2577 | case CONSTANT: { |
| 2578 | HandleScope inner(isolate()); |
| 2579 | Handle<Name> key = Handle<Name>(descs->GetKey(i)); |
| 2580 | Handle<Object> constant(descs->GetConstant(i), isolate()); |
| 2581 | JSObject::AddProperty(to, key, constant, details.attributes()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2582 | break; |
| 2583 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2584 | case ACCESSOR_FIELD: |
| 2585 | UNREACHABLE(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2586 | case CALLBACKS: { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2587 | Handle<Name> key(descs->GetKey(i)); |
| 2588 | LookupIterator it(to, key, LookupIterator::OWN_SKIP_INTERCEPTOR); |
| 2589 | CHECK_NE(LookupIterator::ACCESS_CHECK, it.state()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2590 | // If the property is already there we skip it |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2591 | if (it.IsFound()) continue; |
| 2592 | HandleScope inner(isolate()); |
| 2593 | DCHECK(!to->HasFastProperties()); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2594 | // Add to dictionary. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2595 | Handle<Object> callbacks(descs->GetCallbacksObject(i), isolate()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2596 | PropertyDetails d(details.attributes(), CALLBACKS, i + 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2597 | JSObject::SetNormalizedProperty(to, key, callbacks, d); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2598 | break; |
| 2599 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2600 | } |
| 2601 | } |
| 2602 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2603 | Handle<NameDictionary> properties = |
| 2604 | Handle<NameDictionary>(from->property_dictionary()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2605 | int capacity = properties->Capacity(); |
| 2606 | for (int i = 0; i < capacity; i++) { |
| 2607 | Object* raw_key(properties->KeyAt(i)); |
| 2608 | if (properties->IsKey(raw_key)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2609 | DCHECK(raw_key->IsName()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2610 | // If the property is already there we skip it. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2611 | Handle<Name> key(Name::cast(raw_key)); |
| 2612 | LookupIterator it(to, key, LookupIterator::OWN_SKIP_INTERCEPTOR); |
| 2613 | CHECK_NE(LookupIterator::ACCESS_CHECK, it.state()); |
| 2614 | if (it.IsFound()) continue; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2615 | // Set the property. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2616 | Handle<Object> value = Handle<Object>(properties->ValueAt(i), |
| 2617 | isolate()); |
| 2618 | DCHECK(!value->IsCell()); |
| 2619 | if (value->IsPropertyCell()) { |
| 2620 | value = Handle<Object>(PropertyCell::cast(*value)->value(), |
| 2621 | isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2622 | } |
| 2623 | PropertyDetails details = properties->DetailsAt(i); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2624 | DCHECK_EQ(DATA, details.kind()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2625 | JSObject::AddProperty(to, key, value, details.attributes()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2626 | } |
| 2627 | } |
| 2628 | } |
| 2629 | } |
| 2630 | |
| 2631 | |
| 2632 | void Genesis::TransferIndexedProperties(Handle<JSObject> from, |
| 2633 | Handle<JSObject> to) { |
| 2634 | // Cloning the elements array is sufficient. |
| 2635 | Handle<FixedArray> from_elements = |
| 2636 | Handle<FixedArray>(FixedArray::cast(from->elements())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2637 | Handle<FixedArray> to_elements = factory()->CopyFixedArray(from_elements); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2638 | to->set_elements(*to_elements); |
| 2639 | } |
| 2640 | |
| 2641 | |
| 2642 | void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2643 | HandleScope outer(isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2644 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2645 | DCHECK(!from->IsJSArray()); |
| 2646 | DCHECK(!to->IsJSArray()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2647 | |
| 2648 | TransferNamedProperties(from, to); |
| 2649 | TransferIndexedProperties(from, to); |
| 2650 | |
| 2651 | // Transfer the prototype (new map is needed). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2652 | Handle<Object> proto(from->map()->prototype(), isolate()); |
| 2653 | SetObjectPrototype(to, proto); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | |
| 2657 | void Genesis::MakeFunctionInstancePrototypeWritable() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2658 | // The maps with writable prototype are created in CreateEmptyFunction |
| 2659 | // and CreateStrictModeFunctionMaps respectively. Initially the maps are |
| 2660 | // created with read-only prototype for JS builtins processing. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2661 | DCHECK(!sloppy_function_map_writable_prototype_.is_null()); |
| 2662 | DCHECK(!strict_function_map_writable_prototype_.is_null()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2663 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2664 | // Replace function instance maps to make prototype writable. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2665 | native_context()->set_sloppy_function_map( |
| 2666 | *sloppy_function_map_writable_prototype_); |
| 2667 | native_context()->set_strict_function_map( |
| 2668 | *strict_function_map_writable_prototype_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2669 | } |
| 2670 | |
| 2671 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2672 | class NoTrackDoubleFieldsForSerializerScope { |
| 2673 | public: |
| 2674 | explicit NoTrackDoubleFieldsForSerializerScope(Isolate* isolate) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2675 | : flag_(FLAG_track_double_fields), enabled_(false) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2676 | if (isolate->serializer_enabled()) { |
| 2677 | // Disable tracking double fields because heap numbers treated as |
| 2678 | // immutable by the serializer. |
| 2679 | FLAG_track_double_fields = false; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2680 | enabled_ = true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2681 | } |
| 2682 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2683 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2684 | ~NoTrackDoubleFieldsForSerializerScope() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2685 | if (enabled_) { |
| 2686 | FLAG_track_double_fields = flag_; |
| 2687 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
| 2690 | private: |
| 2691 | bool flag_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2692 | bool enabled_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2693 | }; |
| 2694 | |
| 2695 | |
| 2696 | Genesis::Genesis(Isolate* isolate, |
| 2697 | MaybeHandle<JSGlobalProxy> maybe_global_proxy, |
| 2698 | v8::Handle<v8::ObjectTemplate> global_proxy_template, |
| 2699 | v8::ExtensionConfiguration* extensions) |
| 2700 | : isolate_(isolate), |
| 2701 | active_(isolate->bootstrapper()) { |
| 2702 | NoTrackDoubleFieldsForSerializerScope disable_scope(isolate); |
| 2703 | result_ = Handle<Context>::null(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2704 | // Before creating the roots we must save the context and restore it |
| 2705 | // on all function exits. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2706 | SaveContext saved_context(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2707 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2708 | // During genesis, the boilerplate for stack overflow won't work until the |
| 2709 | // environment has been at least partially initialized. Add a stack check |
| 2710 | // before entering JS code to catch overflow early. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2711 | StackLimitCheck check(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2712 | if (check.HasOverflowed()) return; |
| 2713 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2714 | // We can only de-serialize a context if the isolate was initialized from |
| 2715 | // a snapshot. Otherwise we have to build the context from scratch. |
| 2716 | if (isolate->initialized_from_snapshot()) { |
| 2717 | native_context_ = Snapshot::NewContextFromSnapshot(isolate); |
| 2718 | } else { |
| 2719 | native_context_ = Handle<Context>(); |
| 2720 | } |
| 2721 | |
| 2722 | if (!native_context().is_null()) { |
| 2723 | AddToWeakNativeContextList(*native_context()); |
| 2724 | isolate->set_context(*native_context()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2725 | isolate->counters()->contexts_created_by_snapshot()->Increment(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2726 | #if TRACE_MAPS |
| 2727 | if (FLAG_trace_maps) { |
| 2728 | Handle<JSFunction> object_fun = isolate->object_function(); |
| 2729 | PrintF("[TraceMap: InitialMap map= %p SFI= %d_Object ]\n", |
| 2730 | reinterpret_cast<void*>(object_fun->initial_map()), |
| 2731 | object_fun->shared()->unique_id()); |
| 2732 | Map::TraceAllTransitions(object_fun->initial_map()); |
| 2733 | } |
| 2734 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2735 | Handle<GlobalObject> global_object; |
| 2736 | Handle<JSGlobalProxy> global_proxy = CreateNewGlobals( |
| 2737 | global_proxy_template, maybe_global_proxy, &global_object); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2738 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2739 | HookUpGlobalProxy(global_object, global_proxy); |
| 2740 | HookUpGlobalObject(global_object); |
| 2741 | native_context()->builtins()->set_global_proxy( |
| 2742 | native_context()->global_proxy()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2743 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2744 | if (!ConfigureGlobalObjects(global_proxy_template)) return; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2745 | } else { |
| 2746 | // We get here if there was no context snapshot. |
| 2747 | CreateRoots(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2748 | Handle<JSFunction> empty_function = CreateEmptyFunction(isolate); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2749 | CreateStrictModeFunctionMaps(empty_function); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2750 | Handle<GlobalObject> global_object; |
| 2751 | Handle<JSGlobalProxy> global_proxy = CreateNewGlobals( |
| 2752 | global_proxy_template, maybe_global_proxy, &global_object); |
| 2753 | HookUpGlobalProxy(global_object, global_proxy); |
| 2754 | InitializeGlobal(global_object, empty_function); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2755 | InstallJSFunctionResultCaches(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2756 | InitializeNormalizedMapCaches(); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2757 | if (!InstallNatives()) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2758 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2759 | MakeFunctionInstancePrototypeWritable(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2760 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2761 | if (!ConfigureGlobalObjects(global_proxy_template)) return; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2762 | isolate->counters()->contexts_created_from_scratch()->Increment(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2763 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2764 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2765 | // Install experimental natives. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2766 | if (!InstallExperimentalNatives()) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2767 | InitializeExperimentalGlobal(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2768 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2769 | // We can't (de-)serialize typed arrays currently, but we are lucky: The state |
| 2770 | // of the random number generator needs no initialization during snapshot |
| 2771 | // creation time and we don't need trigonometric functions then. |
| 2772 | if (!isolate->serializer_enabled()) { |
| 2773 | // Initially seed the per-context random number generator using the |
| 2774 | // per-isolate random number generator. |
| 2775 | const int num_elems = 2; |
| 2776 | const int num_bytes = num_elems * sizeof(uint32_t); |
| 2777 | uint32_t* state = reinterpret_cast<uint32_t*>(malloc(num_bytes)); |
| 2778 | |
| 2779 | do { |
| 2780 | isolate->random_number_generator()->NextBytes(state, num_bytes); |
| 2781 | } while (state[0] == 0 || state[1] == 0); |
| 2782 | |
| 2783 | v8::Local<v8::ArrayBuffer> buffer = v8::ArrayBuffer::New( |
| 2784 | reinterpret_cast<v8::Isolate*>(isolate), state, num_bytes); |
| 2785 | Utils::OpenHandle(*buffer)->set_should_be_freed(true); |
| 2786 | v8::Local<v8::Uint32Array> ta = v8::Uint32Array::New(buffer, 0, num_elems); |
| 2787 | Handle<JSBuiltinsObject> builtins(native_context()->builtins()); |
| 2788 | Runtime::DefineObjectProperty(builtins, factory()->InternalizeOneByteString( |
| 2789 | STATIC_CHAR_VECTOR("rngstate")), |
| 2790 | Utils::OpenHandle(*ta), NONE).Assert(); |
| 2791 | |
| 2792 | // Initialize trigonometric lookup tables and constants. |
| 2793 | const int constants_size = arraysize(fdlibm::MathConstants::constants); |
| 2794 | const int table_num_bytes = constants_size * kDoubleSize; |
| 2795 | v8::Local<v8::ArrayBuffer> trig_buffer = v8::ArrayBuffer::New( |
| 2796 | reinterpret_cast<v8::Isolate*>(isolate), |
| 2797 | const_cast<double*>(fdlibm::MathConstants::constants), table_num_bytes); |
| 2798 | v8::Local<v8::Float64Array> trig_table = |
| 2799 | v8::Float64Array::New(trig_buffer, 0, constants_size); |
| 2800 | |
| 2801 | Runtime::DefineObjectProperty( |
| 2802 | builtins, |
| 2803 | factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("kMath")), |
| 2804 | Utils::OpenHandle(*trig_table), NONE).Assert(); |
| 2805 | } |
| 2806 | |
| 2807 | result_ = native_context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | |
| 2811 | // Support for thread preemption. |
| 2812 | |
| 2813 | // Reserve space for statics needing saving and restoring. |
| 2814 | int Bootstrapper::ArchiveSpacePerThread() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2815 | return sizeof(NestingCounterType); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
| 2818 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2819 | // Archive statics that are thread-local. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2820 | char* Bootstrapper::ArchiveState(char* to) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2821 | *reinterpret_cast<NestingCounterType*>(to) = nesting_; |
| 2822 | nesting_ = 0; |
| 2823 | return to + sizeof(NestingCounterType); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
| 2826 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2827 | // Restore statics that are thread-local. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2828 | char* Bootstrapper::RestoreState(char* from) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2829 | nesting_ = *reinterpret_cast<NestingCounterType*>(from); |
| 2830 | return from + sizeof(NestingCounterType); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
| 2833 | |
| 2834 | // Called when the top-level V8 mutex is destroyed. |
| 2835 | void Bootstrapper::FreeThreadResources() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2836 | DCHECK(!IsActive()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
| 2839 | } } // namespace v8::internal |