OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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/api.h" | 5 #include "src/api.h" |
6 | 6 |
7 #include <string.h> // For memcpy, strlen. | 7 #include <string.h> // For memcpy, strlen. |
8 #ifdef V8_USE_ADDRESS_SANITIZER | 8 #ifdef V8_USE_ADDRESS_SANITIZER |
9 #include <sanitizer/asan_interface.h> | 9 #include <sanitizer/asan_interface.h> |
10 #endif // V8_USE_ADDRESS_SANITIZER | 10 #endif // V8_USE_ADDRESS_SANITIZER |
(...skipping 5612 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5623 void v8::V8::InitializeExternalStartupData(const char* natives_blob, | 5623 void v8::V8::InitializeExternalStartupData(const char* natives_blob, |
5624 const char* snapshot_blob) { | 5624 const char* snapshot_blob) { |
5625 i::InitializeExternalStartupData(natives_blob, snapshot_blob); | 5625 i::InitializeExternalStartupData(natives_blob, snapshot_blob); |
5626 } | 5626 } |
5627 | 5627 |
5628 | 5628 |
5629 const char* v8::V8::GetVersion() { | 5629 const char* v8::V8::GetVersion() { |
5630 return i::Version::GetVersion(); | 5630 return i::Version::GetVersion(); |
5631 } | 5631 } |
5632 | 5632 |
5633 static i::Handle<i::Context> CreateEnvironment( | 5633 template <typename ObjectType> |
| 5634 struct InvokeBootstrapper; |
| 5635 |
| 5636 template <> |
| 5637 struct InvokeBootstrapper<i::Context> { |
| 5638 i::Handle<i::Context> Invoke( |
| 5639 i::Isolate* isolate, i::MaybeHandle<i::JSGlobalProxy> maybe_global_proxy, |
| 5640 v8::Local<v8::ObjectTemplate> global_object_template, |
| 5641 v8::ExtensionConfiguration* extensions, size_t context_snapshot_index) { |
| 5642 return isolate->bootstrapper()->CreateEnvironment( |
| 5643 maybe_global_proxy, global_object_template, extensions, |
| 5644 context_snapshot_index); |
| 5645 } |
| 5646 }; |
| 5647 |
| 5648 template <> |
| 5649 struct InvokeBootstrapper<i::JSGlobalProxy> { |
| 5650 i::Handle<i::JSGlobalProxy> Invoke( |
| 5651 i::Isolate* isolate, i::MaybeHandle<i::JSGlobalProxy> maybe_global_proxy, |
| 5652 v8::Local<v8::ObjectTemplate> global_object_template, |
| 5653 v8::ExtensionConfiguration* extensions, size_t context_snapshot_index) { |
| 5654 USE(extensions); |
| 5655 USE(context_snapshot_index); |
| 5656 return isolate->bootstrapper()->NewRemoteContext(maybe_global_proxy, |
| 5657 global_object_template); |
| 5658 } |
| 5659 }; |
| 5660 |
| 5661 template <typename ObjectType> |
| 5662 static i::Handle<ObjectType> CreateEnvironment( |
5634 i::Isolate* isolate, v8::ExtensionConfiguration* extensions, | 5663 i::Isolate* isolate, v8::ExtensionConfiguration* extensions, |
5635 v8::Local<ObjectTemplate> global_template, | 5664 v8::MaybeLocal<ObjectTemplate> maybe_global_template, |
5636 v8::Local<Value> maybe_global_proxy, size_t context_snapshot_index) { | 5665 v8::MaybeLocal<Value> maybe_global_proxy, size_t context_snapshot_index) { |
5637 i::Handle<i::Context> env; | 5666 i::Handle<ObjectType> result; |
5638 | 5667 |
5639 // Enter V8 via an ENTER_V8 scope. | 5668 // Enter V8 via an ENTER_V8 scope. |
5640 { | 5669 { |
5641 ENTER_V8(isolate); | 5670 ENTER_V8(isolate); |
5642 v8::Local<ObjectTemplate> proxy_template = global_template; | 5671 v8::Local<ObjectTemplate> proxy_template; |
5643 i::Handle<i::FunctionTemplateInfo> proxy_constructor; | 5672 i::Handle<i::FunctionTemplateInfo> proxy_constructor; |
5644 i::Handle<i::FunctionTemplateInfo> global_constructor; | 5673 i::Handle<i::FunctionTemplateInfo> global_constructor; |
5645 | 5674 |
5646 if (!global_template.IsEmpty()) { | 5675 if (!maybe_global_template.IsEmpty()) { |
| 5676 v8::Local<v8::ObjectTemplate> global_template = |
| 5677 maybe_global_template.ToLocalChecked(); |
5647 // Make sure that the global_template has a constructor. | 5678 // Make sure that the global_template has a constructor. |
5648 global_constructor = EnsureConstructor(isolate, *global_template); | 5679 global_constructor = EnsureConstructor(isolate, *global_template); |
5649 | 5680 |
5650 // Create a fresh template for the global proxy object. | 5681 // Create a fresh template for the global proxy object. |
5651 proxy_template = ObjectTemplate::New( | 5682 proxy_template = ObjectTemplate::New( |
5652 reinterpret_cast<v8::Isolate*>(isolate)); | 5683 reinterpret_cast<v8::Isolate*>(isolate)); |
5653 proxy_constructor = EnsureConstructor(isolate, *proxy_template); | 5684 proxy_constructor = EnsureConstructor(isolate, *proxy_template); |
5654 | 5685 |
5655 // Set the global template to be the prototype template of | 5686 // Set the global template to be the prototype template of |
5656 // global proxy template. | 5687 // global proxy template. |
5657 proxy_constructor->set_prototype_template( | 5688 proxy_constructor->set_prototype_template( |
5658 *Utils::OpenHandle(*global_template)); | 5689 *Utils::OpenHandle(*global_template)); |
5659 | 5690 |
5660 // Migrate security handlers from global_template to | 5691 // Migrate security handlers from global_template to |
5661 // proxy_template. Temporarily removing access check | 5692 // proxy_template. Temporarily removing access check |
5662 // information from the global template. | 5693 // information from the global template. |
5663 if (!global_constructor->access_check_info()->IsUndefined(isolate)) { | 5694 if (!global_constructor->access_check_info()->IsUndefined(isolate)) { |
5664 proxy_constructor->set_access_check_info( | 5695 proxy_constructor->set_access_check_info( |
5665 global_constructor->access_check_info()); | 5696 global_constructor->access_check_info()); |
5666 proxy_constructor->set_needs_access_check( | 5697 proxy_constructor->set_needs_access_check( |
5667 global_constructor->needs_access_check()); | 5698 global_constructor->needs_access_check()); |
5668 global_constructor->set_needs_access_check(false); | 5699 global_constructor->set_needs_access_check(false); |
5669 global_constructor->set_access_check_info( | 5700 global_constructor->set_access_check_info( |
5670 isolate->heap()->undefined_value()); | 5701 isolate->heap()->undefined_value()); |
5671 } | 5702 } |
5672 } | 5703 } |
5673 | 5704 |
5674 i::Handle<i::Object> proxy = Utils::OpenHandle(*maybe_global_proxy, true); | |
5675 i::MaybeHandle<i::JSGlobalProxy> maybe_proxy; | 5705 i::MaybeHandle<i::JSGlobalProxy> maybe_proxy; |
5676 if (!proxy.is_null()) { | 5706 if (!maybe_global_proxy.IsEmpty()) { |
5677 maybe_proxy = i::Handle<i::JSGlobalProxy>::cast(proxy); | 5707 maybe_proxy = i::Handle<i::JSGlobalProxy>::cast( |
| 5708 Utils::OpenHandle(*maybe_global_proxy.ToLocalChecked())); |
5678 } | 5709 } |
5679 // Create the environment. | 5710 // Create the environment. |
5680 env = isolate->bootstrapper()->CreateEnvironment( | 5711 InvokeBootstrapper<ObjectType> invoke; |
5681 maybe_proxy, proxy_template, extensions, context_snapshot_index); | 5712 result = invoke.Invoke(isolate, maybe_proxy, proxy_template, extensions, |
| 5713 context_snapshot_index); |
5682 | 5714 |
5683 // Restore the access check info on the global template. | 5715 // Restore the access check info on the global template. |
5684 if (!global_template.IsEmpty()) { | 5716 if (!maybe_global_template.IsEmpty()) { |
5685 DCHECK(!global_constructor.is_null()); | 5717 DCHECK(!global_constructor.is_null()); |
5686 DCHECK(!proxy_constructor.is_null()); | 5718 DCHECK(!proxy_constructor.is_null()); |
5687 global_constructor->set_access_check_info( | 5719 global_constructor->set_access_check_info( |
5688 proxy_constructor->access_check_info()); | 5720 proxy_constructor->access_check_info()); |
5689 global_constructor->set_needs_access_check( | 5721 global_constructor->set_needs_access_check( |
5690 proxy_constructor->needs_access_check()); | 5722 proxy_constructor->needs_access_check()); |
5691 } | 5723 } |
5692 } | 5724 } |
5693 // Leave V8. | 5725 // Leave V8. |
5694 | 5726 |
5695 return env; | 5727 return result; |
5696 } | 5728 } |
5697 | 5729 |
5698 Local<Context> NewContext(v8::Isolate* external_isolate, | 5730 Local<Context> NewContext(v8::Isolate* external_isolate, |
5699 v8::ExtensionConfiguration* extensions, | 5731 v8::ExtensionConfiguration* extensions, |
5700 v8::Local<ObjectTemplate> global_template, | 5732 v8::MaybeLocal<ObjectTemplate> global_template, |
5701 v8::Local<Value> global_object, | 5733 v8::MaybeLocal<Value> global_object, |
5702 size_t context_snapshot_index) { | 5734 size_t context_snapshot_index) { |
5703 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate); | 5735 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate); |
5704 LOG_API(isolate, Context, New); | 5736 LOG_API(isolate, Context, New); |
5705 i::HandleScope scope(isolate); | 5737 i::HandleScope scope(isolate); |
5706 ExtensionConfiguration no_extensions; | 5738 ExtensionConfiguration no_extensions; |
5707 if (extensions == NULL) extensions = &no_extensions; | 5739 if (extensions == NULL) extensions = &no_extensions; |
5708 i::Handle<i::Context> env = | 5740 i::Handle<i::Context> env = |
5709 CreateEnvironment(isolate, extensions, global_template, global_object, | 5741 CreateEnvironment<i::Context>(isolate, extensions, global_template, |
5710 context_snapshot_index); | 5742 global_object, context_snapshot_index); |
5711 if (env.is_null()) { | 5743 if (env.is_null()) { |
5712 if (isolate->has_pending_exception()) { | 5744 if (isolate->has_pending_exception()) { |
5713 isolate->OptionalRescheduleException(true); | 5745 isolate->OptionalRescheduleException(true); |
5714 } | 5746 } |
5715 return Local<Context>(); | 5747 return Local<Context>(); |
5716 } | 5748 } |
5717 return Utils::ToLocal(scope.CloseAndEscape(env)); | 5749 return Utils::ToLocal(scope.CloseAndEscape(env)); |
5718 } | 5750 } |
5719 | 5751 |
5720 Local<Context> v8::Context::New(v8::Isolate* external_isolate, | 5752 Local<Context> v8::Context::New(v8::Isolate* external_isolate, |
5721 v8::ExtensionConfiguration* extensions, | 5753 v8::ExtensionConfiguration* extensions, |
5722 v8::Local<ObjectTemplate> global_template, | 5754 v8::MaybeLocal<ObjectTemplate> global_template, |
5723 v8::Local<Value> global_object) { | 5755 v8::MaybeLocal<Value> global_object) { |
5724 return NewContext(external_isolate, extensions, global_template, | 5756 return NewContext(external_isolate, extensions, global_template, |
5725 global_object, 0); | 5757 global_object, 0); |
5726 } | 5758 } |
5727 | 5759 |
5728 MaybeLocal<Context> v8::Context::FromSnapshot( | 5760 MaybeLocal<Context> v8::Context::FromSnapshot( |
5729 v8::Isolate* external_isolate, size_t context_snapshot_index, | 5761 v8::Isolate* external_isolate, size_t context_snapshot_index, |
5730 v8::ExtensionConfiguration* extensions, | 5762 v8::ExtensionConfiguration* extensions, |
5731 v8::Local<ObjectTemplate> global_template, v8::Local<Value> global_object) { | 5763 v8::MaybeLocal<ObjectTemplate> global_template, |
| 5764 v8::MaybeLocal<Value> global_object) { |
5732 if (!i::Snapshot::HasContextSnapshot( | 5765 if (!i::Snapshot::HasContextSnapshot( |
5733 reinterpret_cast<i::Isolate*>(external_isolate), | 5766 reinterpret_cast<i::Isolate*>(external_isolate), |
5734 context_snapshot_index)) { | 5767 context_snapshot_index)) { |
5735 return MaybeLocal<Context>(); | 5768 return MaybeLocal<Context>(); |
5736 } | 5769 } |
5737 return NewContext(external_isolate, extensions, global_template, | 5770 return NewContext(external_isolate, extensions, global_template, |
5738 global_object, context_snapshot_index); | 5771 global_object, context_snapshot_index); |
5739 } | 5772 } |
5740 | 5773 |
| 5774 MaybeLocal<Object> v8::Context::NewRemoteContext( |
| 5775 v8::Isolate* external_isolate, v8::Local<ObjectTemplate> global_template, |
| 5776 v8::MaybeLocal<v8::Value> global_object) { |
| 5777 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate); |
| 5778 LOG_API(isolate, Context, NewRemoteContext); |
| 5779 i::HandleScope scope(isolate); |
| 5780 i::Handle<i::FunctionTemplateInfo> global_constructor = |
| 5781 EnsureConstructor(isolate, *global_template); |
| 5782 Utils::ApiCheck(global_constructor->needs_access_check(), |
| 5783 "v8::Context::NewRemoteContext", |
| 5784 "Global template needs to have access checks enabled."); |
| 5785 i::Handle<i::AccessCheckInfo> access_check_info = i::handle( |
| 5786 i::AccessCheckInfo::cast(global_constructor->access_check_info()), |
| 5787 isolate); |
| 5788 Utils::ApiCheck(access_check_info->named_interceptor() != nullptr, |
| 5789 "v8::Context::NewRemoteContext", |
| 5790 "Global template needs to have access check handlers."); |
| 5791 i::Handle<i::JSGlobalProxy> global_proxy = |
| 5792 CreateEnvironment<i::JSGlobalProxy>(isolate, nullptr, global_template, |
| 5793 global_object, 0); |
| 5794 if (global_proxy.is_null()) { |
| 5795 if (isolate->has_pending_exception()) { |
| 5796 isolate->OptionalRescheduleException(true); |
| 5797 } |
| 5798 return MaybeLocal<Object>(); |
| 5799 } |
| 5800 return Utils::ToLocal( |
| 5801 scope.CloseAndEscape(i::Handle<i::JSObject>::cast(global_proxy))); |
| 5802 } |
| 5803 |
5741 void v8::Context::SetSecurityToken(Local<Value> token) { | 5804 void v8::Context::SetSecurityToken(Local<Value> token) { |
5742 i::Handle<i::Context> env = Utils::OpenHandle(this); | 5805 i::Handle<i::Context> env = Utils::OpenHandle(this); |
5743 i::Handle<i::Object> token_handle = Utils::OpenHandle(*token); | 5806 i::Handle<i::Object> token_handle = Utils::OpenHandle(*token); |
5744 env->set_security_token(*token_handle); | 5807 env->set_security_token(*token_handle); |
5745 } | 5808 } |
5746 | 5809 |
5747 | 5810 |
5748 void v8::Context::UseDefaultSecurityToken() { | 5811 void v8::Context::UseDefaultSecurityToken() { |
5749 i::Handle<i::Context> env = Utils::OpenHandle(this); | 5812 i::Handle<i::Context> env = Utils::OpenHandle(this); |
5750 env->set_security_token(env->global_object()); | 5813 env->set_security_token(env->global_object()); |
(...skipping 3186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
8937 Address callback_address = | 9000 Address callback_address = |
8938 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); | 9001 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); |
8939 VMState<EXTERNAL> state(isolate); | 9002 VMState<EXTERNAL> state(isolate); |
8940 ExternalCallbackScope call_scope(isolate, callback_address); | 9003 ExternalCallbackScope call_scope(isolate, callback_address); |
8941 callback(info); | 9004 callback(info); |
8942 } | 9005 } |
8943 | 9006 |
8944 | 9007 |
8945 } // namespace internal | 9008 } // namespace internal |
8946 } // namespace v8 | 9009 } // namespace v8 |
OLD | NEW |