OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/bootstrapper.h" | 5 #include "src/bootstrapper.h" |
6 | 6 |
7 #include "src/accessors.h" | 7 #include "src/accessors.h" |
8 #include "src/api-natives.h" | 8 #include "src/api-natives.h" |
9 #include "src/base/utils/random-number-generator.h" | 9 #include "src/base/utils/random-number-generator.h" |
10 #include "src/code-stubs.h" | 10 #include "src/code-stubs.h" |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 // made in script scopes. Add a "this" binding to that table pointing to the | 192 // made in script scopes. Add a "this" binding to that table pointing to the |
193 // global proxy. | 193 // global proxy. |
194 void InstallGlobalThisBinding(); | 194 void InstallGlobalThisBinding(); |
195 void HookUpGlobalThisBinding(Handle<FixedArray> outdated_contexts); | 195 void HookUpGlobalThisBinding(Handle<FixedArray> outdated_contexts); |
196 // New context initialization. Used for creating a context from scratch. | 196 // New context initialization. Used for creating a context from scratch. |
197 void InitializeGlobal(Handle<GlobalObject> global_object, | 197 void InitializeGlobal(Handle<GlobalObject> global_object, |
198 Handle<JSFunction> empty_function, | 198 Handle<JSFunction> empty_function, |
199 ContextType context_type); | 199 ContextType context_type); |
200 void InitializeExperimentalGlobal(); | 200 void InitializeExperimentalGlobal(); |
201 // Typed arrays are not serializable and have to initialized afterwards. | 201 // Typed arrays are not serializable and have to initialized afterwards. |
202 void InitializeBuiltinTypedArrays(); | 202 bool InitializeBuiltinTypedArrays(); |
203 // Depending on the situation, expose and/or get rid of the utils object. | 203 // Depending on the situation, expose and/or get rid of the utils object. |
204 void ConfigureUtilsObject(ContextType context_type); | 204 void ConfigureUtilsObject(ContextType context_type); |
205 | 205 |
206 #define DECLARE_FEATURE_INITIALIZATION(id, descr) \ | 206 #define DECLARE_FEATURE_INITIALIZATION(id, descr) \ |
207 void InitializeGlobal_##id(); | 207 void InitializeGlobal_##id(); |
208 | 208 |
209 HARMONY_INPROGRESS(DECLARE_FEATURE_INITIALIZATION) | 209 HARMONY_INPROGRESS(DECLARE_FEATURE_INITIALIZATION) |
210 HARMONY_STAGED(DECLARE_FEATURE_INITIALIZATION) | 210 HARMONY_STAGED(DECLARE_FEATURE_INITIALIZATION) |
211 HARMONY_SHIPPING(DECLARE_FEATURE_INITIALIZATION) | 211 HARMONY_SHIPPING(DECLARE_FEATURE_INITIALIZATION) |
212 #undef DECLARE_FEATURE_INITIALIZATION | 212 #undef DECLARE_FEATURE_INITIALIZATION |
(...skipping 1570 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1783 } | 1783 } |
1784 Handle<String> inner_string = factory->InternalizeUtf8String(inner); | 1784 Handle<String> inner_string = factory->InternalizeUtf8String(inner); |
1785 DCHECK(!inner_string.is_null()); | 1785 DCHECK(!inner_string.is_null()); |
1786 Handle<Object> value = | 1786 Handle<Object> value = |
1787 Object::GetProperty(object, inner_string).ToHandleChecked(); | 1787 Object::GetProperty(object, inner_string).ToHandleChecked(); |
1788 return Handle<JSObject>::cast(value); | 1788 return Handle<JSObject>::cast(value); |
1789 } | 1789 } |
1790 | 1790 |
1791 | 1791 |
1792 template <typename Data> | 1792 template <typename Data> |
1793 Data* SetBuiltinTypedArray(Isolate* isolate, Handle<JSBuiltinsObject> builtins, | 1793 Handle<JSTypedArray> CreateTypedArray(Isolate* isolate, ExternalArrayType type, |
1794 ExternalArrayType type, Data* data, | 1794 size_t num_elements, Data** data) { |
1795 size_t num_elements, const char* name, | 1795 size_t byte_length = num_elements * sizeof(**data); |
1796 const SharedFlag shared = SharedFlag::kNotShared, | |
1797 const PretenureFlag pretenure = TENURED) { | |
1798 size_t byte_length = num_elements * sizeof(*data); | |
1799 Handle<JSArrayBuffer> buffer = | 1796 Handle<JSArrayBuffer> buffer = |
1800 isolate->factory()->NewJSArrayBuffer(shared, pretenure); | 1797 isolate->factory()->NewJSArrayBuffer(SharedFlag::kNotShared, TENURED); |
1801 bool is_external = data != nullptr; | 1798 bool is_external = (*data != nullptr); |
1802 if (!is_external) { | 1799 if (!is_external) { |
1803 data = reinterpret_cast<Data*>( | 1800 *data = reinterpret_cast<Data*>( |
1804 isolate->array_buffer_allocator()->Allocate(byte_length)); | 1801 isolate->array_buffer_allocator()->Allocate(byte_length)); |
1805 } | 1802 } |
1806 JSArrayBuffer::Setup(buffer, isolate, is_external, data, byte_length, shared); | 1803 JSArrayBuffer::Setup(buffer, isolate, is_external, *data, byte_length, |
1807 | 1804 SharedFlag::kNotShared); |
1808 Handle<JSTypedArray> typed_array = isolate->factory()->NewJSTypedArray( | 1805 return isolate->factory()->NewJSTypedArray(type, buffer, 0, num_elements, |
1809 type, buffer, 0, num_elements, pretenure); | 1806 TENURED); |
1810 Handle<String> name_string = isolate->factory()->InternalizeUtf8String(name); | |
1811 // Reset property cell type before (re)initializing. | |
1812 JSBuiltinsObject::InvalidatePropertyCell(builtins, name_string); | |
1813 JSObject::SetOwnPropertyIgnoreAttributes(builtins, name_string, typed_array, | |
1814 FROZEN) | |
1815 .Assert(); | |
1816 return data; | |
1817 } | 1807 } |
1818 | 1808 |
1819 | 1809 |
1820 void Genesis::InitializeBuiltinTypedArrays() { | 1810 bool Genesis::InitializeBuiltinTypedArrays() { |
1821 Handle<JSBuiltinsObject> builtins(native_context()->builtins()); | 1811 HandleScope scope(isolate()); |
1822 { // Initially seed the per-context random number generator using the | 1812 Handle<JSTypedArray> rng_state; |
1823 // per-isolate random number generator. | 1813 Handle<JSTypedArray> math_constants; |
| 1814 Handle<JSTypedArray> rempio2result; |
| 1815 |
| 1816 { |
| 1817 // Seed the per-context RNG using the per-isolate RNG. |
1824 const size_t num_elements = 2; | 1818 const size_t num_elements = 2; |
1825 const size_t num_bytes = num_elements * sizeof(uint32_t); | 1819 const size_t num_bytes = num_elements * sizeof(uint32_t); |
1826 uint32_t* state = SetBuiltinTypedArray<uint32_t>(isolate(), builtins, | 1820 uint32_t* state = NULL; |
1827 kExternalUint32Array, NULL, | 1821 rng_state = |
1828 num_elements, "rngstate"); | 1822 CreateTypedArray(isolate(), kExternalUint32Array, num_elements, &state); |
1829 do { | 1823 do { |
1830 isolate()->random_number_generator()->NextBytes(state, num_bytes); | 1824 isolate()->random_number_generator()->NextBytes(state, num_bytes); |
1831 } while (state[0] == 0 || state[1] == 0); | 1825 } while (state[0] == 0 || state[1] == 0); |
1832 } | 1826 } |
1833 | 1827 |
1834 { // Initialize trigonometric lookup tables and constants. | 1828 { // Initialize trigonometric lookup tables and constants. |
1835 const size_t num_elements = arraysize(fdlibm::MathConstants::constants); | 1829 const size_t num_elements = arraysize(fdlibm::MathConstants::constants); |
1836 double* data = const_cast<double*>(fdlibm::MathConstants::constants); | 1830 double* constants = const_cast<double*>(fdlibm::MathConstants::constants); |
1837 SetBuiltinTypedArray<double>(isolate(), builtins, kExternalFloat64Array, | 1831 math_constants = CreateTypedArray(isolate(), kExternalFloat64Array, |
1838 data, num_elements, "kMath"); | 1832 num_elements, &constants); |
1839 } | 1833 } |
1840 | 1834 |
1841 { // Initialize a result array for rempio2 calculation | 1835 { // Initialize a result array for rempio2 calculation |
1842 const size_t num_elements = 2; | 1836 const size_t num_elements = 2; |
1843 double* data = | 1837 double* data = NULL; |
1844 SetBuiltinTypedArray<double>(isolate(), builtins, kExternalFloat64Array, | 1838 rempio2result = |
1845 NULL, num_elements, "rempio2result"); | 1839 CreateTypedArray(isolate(), kExternalFloat64Array, num_elements, &data); |
1846 for (size_t i = 0; i < num_elements; i++) data[i] = 0; | 1840 for (size_t i = 0; i < num_elements; i++) data[i] = 0; |
1847 } | 1841 } |
| 1842 |
| 1843 Handle<JSObject> utils = |
| 1844 Handle<JSObject>::cast(isolate()->natives_utils_object()); |
| 1845 Handle<String> name_string = isolate()->factory()->NewStringFromAsciiChecked( |
| 1846 "InitializeBuiltinTypedArrays"); |
| 1847 Handle<Object> fun = JSObject::GetDataProperty(utils, name_string); |
| 1848 Handle<Object> receiver = isolate()->factory()->undefined_value(); |
| 1849 Handle<Object> args[] = {utils, rng_state, math_constants, rempio2result}; |
| 1850 return !Execution::Call(isolate(), fun, receiver, arraysize(args), args) |
| 1851 .is_null(); |
1848 } | 1852 } |
1849 | 1853 |
1850 | 1854 |
1851 void Genesis::ConfigureUtilsObject(ContextType context_type) { | 1855 void Genesis::ConfigureUtilsObject(ContextType context_type) { |
1852 switch (context_type) { | 1856 switch (context_type) { |
1853 // We still need the utils object to find debug functions. | 1857 // We still need the utils object to find debug functions. |
1854 case DEBUG_CONTEXT: | 1858 case DEBUG_CONTEXT: |
1855 return; | 1859 return; |
1856 // Expose the natives in global if a valid name for it is specified. | 1860 // Expose the natives in global if a valid name for it is specified. |
1857 case FULL_CONTEXT: { | 1861 case FULL_CONTEXT: { |
(...skipping 1396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3254 if (!InstallExtraNatives()) return; | 3258 if (!InstallExtraNatives()) return; |
3255 if (!ConfigureGlobalObjects(global_proxy_template)) return; | 3259 if (!ConfigureGlobalObjects(global_proxy_template)) return; |
3256 } | 3260 } |
3257 isolate->counters()->contexts_created_from_scratch()->Increment(); | 3261 isolate->counters()->contexts_created_from_scratch()->Increment(); |
3258 } | 3262 } |
3259 | 3263 |
3260 // Install experimental natives. Do not include them into the | 3264 // Install experimental natives. Do not include them into the |
3261 // snapshot as we should be able to turn them off at runtime. Re-installing | 3265 // snapshot as we should be able to turn them off at runtime. Re-installing |
3262 // them after they have already been deserialized would also fail. | 3266 // them after they have already been deserialized would also fail. |
3263 if (context_type == FULL_CONTEXT) { | 3267 if (context_type == FULL_CONTEXT) { |
| 3268 if (!InitializeBuiltinTypedArrays()) return; |
3264 if (!isolate->serializer_enabled()) { | 3269 if (!isolate->serializer_enabled()) { |
3265 InitializeExperimentalGlobal(); | 3270 InitializeExperimentalGlobal(); |
3266 if (!InstallExperimentalNatives()) return; | 3271 if (!InstallExperimentalNatives()) return; |
3267 | 3272 |
3268 if (FLAG_experimental_extras) { | 3273 if (FLAG_experimental_extras) { |
3269 if (!InstallExperimentalExtraNatives()) return; | 3274 if (!InstallExperimentalExtraNatives()) return; |
3270 } | 3275 } |
3271 } | 3276 } |
3272 // The serializer cannot serialize typed arrays. Reset those typed arrays | 3277 // The serializer cannot serialize typed arrays. Reset those typed arrays |
3273 // for each new context. | 3278 // for each new context. |
3274 InitializeBuiltinTypedArrays(); | |
3275 } else if (context_type == DEBUG_CONTEXT) { | 3279 } else if (context_type == DEBUG_CONTEXT) { |
3276 DCHECK(!isolate->serializer_enabled()); | 3280 DCHECK(!isolate->serializer_enabled()); |
| 3281 if (!InitializeBuiltinTypedArrays()) return; |
3277 InitializeExperimentalGlobal(); | 3282 InitializeExperimentalGlobal(); |
3278 if (!InstallDebuggerNatives()) return; | 3283 if (!InstallDebuggerNatives()) return; |
3279 } | 3284 } |
3280 | 3285 |
3281 ConfigureUtilsObject(context_type); | 3286 ConfigureUtilsObject(context_type); |
3282 | 3287 |
3283 // Check that the script context table is empty except for the 'this' binding. | 3288 // Check that the script context table is empty except for the 'this' binding. |
3284 // We do not need script contexts for native scripts. | 3289 // We do not need script contexts for native scripts. |
3285 if (!FLAG_global_var_shortcuts) { | 3290 if (!FLAG_global_var_shortcuts) { |
3286 DCHECK_EQ(1, native_context()->script_context_table()->used()); | 3291 DCHECK_EQ(1, native_context()->script_context_table()->used()); |
(...skipping 26 matching lines...) Expand all Loading... |
3313 } | 3318 } |
3314 | 3319 |
3315 | 3320 |
3316 // Called when the top-level V8 mutex is destroyed. | 3321 // Called when the top-level V8 mutex is destroyed. |
3317 void Bootstrapper::FreeThreadResources() { | 3322 void Bootstrapper::FreeThreadResources() { |
3318 DCHECK(!IsActive()); | 3323 DCHECK(!IsActive()); |
3319 } | 3324 } |
3320 | 3325 |
3321 } // namespace internal | 3326 } // namespace internal |
3322 } // namespace v8 | 3327 } // namespace v8 |
OLD | NEW |