@@ -368,6 +368,16 @@ bool IsFilePath(const std::string& path) {
368
368
}
369
369
#endif // __POSIX__
370
370
371
+ void ThrowUninitializedInspectorError (Environment* env) {
372
+ HandleScope scope (env->isolate ());
373
+
374
+ const char * msg = " This Environment was initialized without a V8::Inspector" ;
375
+ Local<Value> exception =
376
+ v8::String::NewFromUtf8 (env->isolate (), msg).ToLocalChecked ();
377
+
378
+ env->isolate ()->ThrowException (exception );
379
+ }
380
+
371
381
} // namespace
372
382
373
383
class NodeInspectorClient : public V8InspectorClient {
@@ -728,6 +738,11 @@ bool Agent::StartIoThread() {
728
738
if (io_ != nullptr )
729
739
return true ;
730
740
741
+ if (!parent_env_->should_create_inspector () && !client_) {
742
+ ThrowUninitializedInspectorError (parent_env_);
743
+ return false ;
744
+ }
745
+
731
746
CHECK_NOT_NULL (client_);
732
747
733
748
io_ = InspectorIo::Start (client_->getThreadHandle (),
@@ -748,7 +763,13 @@ void Agent::Stop() {
748
763
std::unique_ptr<InspectorSession> Agent::Connect (
749
764
std::unique_ptr<InspectorSessionDelegate> delegate,
750
765
bool prevent_shutdown) {
766
+ if (!parent_env_->should_create_inspector () && !client_) {
767
+ ThrowUninitializedInspectorError (parent_env_);
768
+ return std::unique_ptr<InspectorSession>{};
769
+ }
770
+
751
771
CHECK_NOT_NULL (client_);
772
+
752
773
int session_id = client_->connectFrontend (std::move (delegate),
753
774
prevent_shutdown);
754
775
return std::unique_ptr<InspectorSession>(
@@ -758,6 +779,11 @@ std::unique_ptr<InspectorSession> Agent::Connect(
758
779
std::unique_ptr<InspectorSession> Agent::ConnectToMainThread (
759
780
std::unique_ptr<InspectorSessionDelegate> delegate,
760
781
bool prevent_shutdown) {
782
+ if (!parent_env_->should_create_inspector () && !client_) {
783
+ ThrowUninitializedInspectorError (parent_env_);
784
+ return std::unique_ptr<InspectorSession>{};
785
+ }
786
+
761
787
CHECK_NOT_NULL (parent_handle_);
762
788
CHECK_NOT_NULL (client_);
763
789
auto thread_safe_delegate =
@@ -767,6 +793,11 @@ std::unique_ptr<InspectorSession> Agent::ConnectToMainThread(
767
793
}
768
794
769
795
void Agent::WaitForDisconnect () {
796
+ if (!parent_env_->should_create_inspector () && !client_) {
797
+ ThrowUninitializedInspectorError (parent_env_);
798
+ return ;
799
+ }
800
+
770
801
CHECK_NOT_NULL (client_);
771
802
bool is_worker = parent_handle_ != nullptr ;
772
803
parent_handle_.reset ();
@@ -912,6 +943,12 @@ void Agent::SetParentHandle(
912
943
913
944
std::unique_ptr<ParentInspectorHandle> Agent::GetParentHandle (
914
945
uint64_t thread_id, const std::string& url) {
946
+ if (!parent_env_->should_create_inspector () && !client_) {
947
+ ThrowUninitializedInspectorError (parent_env_);
948
+ return std::unique_ptr<ParentInspectorHandle>{};
949
+ }
950
+
951
+ CHECK_NOT_NULL (client_);
915
952
if (!parent_handle_) {
916
953
return client_->getWorkerManager ()->NewParentHandle (thread_id, url);
917
954
} else {
@@ -920,11 +957,21 @@ std::unique_ptr<ParentInspectorHandle> Agent::GetParentHandle(
920
957
}
921
958
922
959
void Agent::WaitForConnect () {
960
+ if (!parent_env_->should_create_inspector () && !client_) {
961
+ ThrowUninitializedInspectorError (parent_env_);
962
+ return ;
963
+ }
964
+
923
965
CHECK_NOT_NULL (client_);
924
966
client_->waitForFrontend ();
925
967
}
926
968
927
969
std::shared_ptr<WorkerManager> Agent::GetWorkerManager () {
970
+ if (!parent_env_->should_create_inspector () && !client_) {
971
+ ThrowUninitializedInspectorError (parent_env_);
972
+ return std::unique_ptr<WorkerManager>{};
973
+ }
974
+
928
975
CHECK_NOT_NULL (client_);
929
976
return client_->getWorkerManager ();
930
977
}
0 commit comments