99#include " Runtime.h"
1010#include " src/inspector/string-16.h"
1111
12+ #include " src/inspector/task-runner.h"
13+
1214using namespace std ;
1315using namespace tns ;
1416using namespace v8 ;
1517
1618using namespace v8_inspector ;
1719
20+ class ConnectTask : public TaskRunner ::Task
21+ {
22+ public:
23+ ConnectTask (JsV8InspectorClient* client, v8::base_copied::Semaphore *ready_semaphore)
24+ : client_(client), ready_semaphore_(ready_semaphore)
25+ {
26+
27+ }
28+
29+ ~ConnectTask () = default ;
30+
31+ bool is_inspector_task () final
32+ {
33+ return true ;
34+ }
35+
36+ void Run (v8::Isolate *isolate, const v8::Global<v8::Context> &global_context)
37+ {
38+ v8::HandleScope handle_scope (isolate);
39+ v8::Local<v8::Context> context = global_context.Get (isolate);
40+ client_->doConnect (isolate, context);
41+ if (ready_semaphore_ != nullptr )
42+ {
43+ ready_semaphore_->Signal ();
44+ }
45+ }
46+
47+ private:
48+ JsV8InspectorClient* client_;
49+ v8::base_copied::Semaphore *ready_semaphore_;
50+ };
51+
52+
53+ class SendMessageToBackendTask : public TaskRunner ::Task
54+ {
55+ public:
56+ explicit SendMessageToBackendTask (JsV8InspectorClient* client, const std::string &message)
57+ : client_(client), message_(message)
58+ {
59+
60+ }
61+
62+ bool is_inspector_task () final
63+ {
64+ return true ;
65+ }
66+
67+ void Run (v8::Isolate *isolate, const v8::Global<v8::Context> &global_context) override
68+ {
69+ // v8_inspector::V8InspectorSession *session = nullptr;
70+ // {
71+ // v8::HandleScope handle_scope(isolate);
72+ // v8::Local<v8::Context> context = global_context.Get(isolate);
73+ // session = InspectorClientImpl::SessionFromContext(context);
74+ // CHECK(session);
75+ // }
76+ //
77+ // v8_inspector::StringView message_view(reinterpret_cast<const uint16_t *>(message_.characters16()), message_.length());
78+ // session->dispatchProtocolMessage(message_view);
79+
80+ v8::HandleScope handle_scope (isolate);
81+ v8::Local<v8::Context> context = global_context.Get (isolate);
82+ client_->doDispatchMessage (isolate, message_);
83+ }
84+
85+ private:
86+ JsV8InspectorClient* client_;
87+ std::string message_;
88+ };
89+
1890JsV8InspectorClient::JsV8InspectorClient (v8::Isolate *isolate)
19- : isolate_(nullptr ),
91+ : isolate_(isolate ),
2092 inspector_(nullptr ),
2193 session_(nullptr ),
22- connection(nullptr )
94+ connection(nullptr ),
95+ backend_runner(nullptr )
2396{
24- isolate_ = isolate;
25-
2697 JEnv env;
2798
99+
28100 inspectorClass = env.FindClass (" com/tns/AndroidJsV8Inspector" );
29101 assert (inspectorClass != nullptr );
30102
31103 sendMethod = env.GetStaticMethodID (inspectorClass, " send" , " (Ljava/lang/Object;Ljava/lang/String;)V" );
32104 assert (sendMethod != nullptr );
33105}
34106
107+ JsV8InspectorClient::~JsV8InspectorClient ()
108+ {
109+ if (backend_runner != nullptr )
110+ {
111+ delete backend_runner;
112+ }
113+ }
114+
35115void JsV8InspectorClient::connect (jobject connection)
36116{
37- v8:: Isolate::Scope isolate_scope (isolate_);
38- v8::HandleScope handleScope (isolate_);
117+ // Isolate::Scope isolate_scope(isolate_);
118+ // v8::HandleScope handleScope(isolate_);
39119
40- // session_ = inspector_->connect(1, this, nullptr );
120+ v8::base_copied::Semaphore ready_semaphore ( 0 );
41121
42122 JEnv env;
43123 this ->connection = env.NewGlobalRef (connection);
124+
125+ this ->backend_runner ->Append (new ConnectTask (this , &ready_semaphore));
126+
127+ ready_semaphore.Wait ();
128+
129+
130+
131+ // inspector_ = V8Inspector::create(isolate_, this);
132+
133+ // session_ = inspector_->connect(1, this, v8_inspector::StringView());
134+
135+ // inspector_->contextCreated(v8_inspector::V8ContextInfo(isolate_->GetCurrentContext(), 1, v8_inspector::StringView()));
136+
137+
138+ }
139+
140+ void JsV8InspectorClient::doConnect (v8::Isolate *isolate, const v8::Local<v8::Context>& context)
141+ {
142+ // Isolate::Scope isolate_scope(isolate_);
143+ // v8::HandleScope handleScope(isolate_);
144+
145+
146+ inspector_ = V8Inspector::create (isolate, this );
147+
148+ session_ = inspector_->connect (1 , this , v8_inspector::StringView ());
149+
150+ inspector_->contextCreated (v8_inspector::V8ContextInfo (/* isolate->GetCurrentContext()*/ context, 1 , v8_inspector::StringView ()));
44151}
45152
46153void JsV8InspectorClient::disconnect ()
47154{
48- v8:: Isolate::Scope isolate_scope (isolate_);
155+ Isolate::Scope isolate_scope (isolate_);
49156 v8::HandleScope handleScope (isolate_);
50157
51158 session_.reset ();
@@ -55,14 +162,105 @@ void JsV8InspectorClient::disconnect()
55162 this ->connection = nullptr ;
56163}
57164
165+
58166void JsV8InspectorClient::dispatchMessage (const std::string &message)
59167{
60- v8::Isolate::Scope isolate_scope (isolate_);
61- v8::HandleScope handleScope (isolate_);
168+ // Isolate::Scope isolate_scope(isolate_);
169+ // v8::HandleScope handleScope(isolate_);
170+
171+ // assert(session_ != nullptr);
172+ //
173+ // const String16 msg(message.c_str());
174+ // v8_inspector::StringView message_view(reinterpret_cast<const uint16_t *>(msg.characters16()), msg.length());
175+ // session_->dispatchProtocolMessage(message_view);
176+
177+ this ->backend_runner ->Append (new SendMessageToBackendTask (this , message));
178+
179+ }
180+
181+ void JsV8InspectorClient::doDispatchMessage (v8::Isolate *isolate, const std::string &message)
182+ {
183+ // Isolate::Scope isolate_scope(isolate_);
184+ // v8::HandleScope handleScope(isolate_);
62185
63186 assert (session_ != nullptr );
64- // const String16 protocolMessage(message.c_str());
65- // session_->dispatchProtocolMessage(protocolMessage);
187+
188+ const String16 msg (message.c_str ());
189+ v8_inspector::StringView message_view (reinterpret_cast <const uint16_t *>(msg.characters16 ()), msg.length ());
190+ session_->dispatchProtocolMessage (message_view);
191+ }
192+
193+ void JsV8InspectorClient::sendProtocolResponse (int callId, const v8_inspector::StringView &message)
194+ {
195+ // frontend_channel_->SendMessageToFrontend(message);
196+ sendProtocolNotification (message);
197+ }
198+
199+ static v8_inspector::String16 ToString16 (const v8_inspector::StringView &string)
200+ {
201+ if (string.is8Bit ())
202+ {
203+ return v8_inspector::String16 (reinterpret_cast <const char *>(string.characters8 ()), string.length ());
204+ }
205+
206+ return v8_inspector::String16 (reinterpret_cast <const uint16_t *>(string.characters16 ()), string.length ());
207+ }
208+
209+ void JsV8InspectorClient::sendProtocolNotification (const v8_inspector::StringView &message)
210+ {
211+ // frontend_channel_->SendMessageToFrontend(message);
212+
213+ if (inspectorClass == nullptr || this ->connection == nullptr )
214+ {
215+ return ;
216+ }
217+
218+ v8_inspector::String16 msg = ToString16 (message);
219+
220+ JEnv env;
221+ JniLocalRef string (env.NewStringUTF (msg.utf8 ().c_str ()));
222+ env.CallStaticVoidMethod (inspectorClass, sendMethod, this ->connection , (jstring) string);
223+ }
224+
225+ void JsV8InspectorClient::flushProtocolNotifications ()
226+ {
227+ }
228+
229+ void MessageHandler (v8::Local<v8::Message> message,
230+ v8::Local<v8::Value> exception)
231+ {
232+ v8::Isolate *isolate = v8::Isolate::GetCurrent ();
233+ v8::Local<v8::Context> context = isolate->GetEnteredContext ();
234+ // if (context.IsEmpty()) return;
235+ // v8_inspector::V8Inspector *inspector = InspectorClientImpl::InspectorFromContext(context);
236+ //
237+ // v8::Local<v8::StackTrace> stack = message->GetStackTrace();
238+ // int script_id = message->GetScriptOrigin().ScriptID()->Value();
239+ // if (!stack.IsEmpty() && stack->GetFrameCount() > 0)
240+ // {
241+ // int top_script_id = stack->GetFrame(0)->GetScriptId();
242+ // if (top_script_id == script_id) script_id = 0;
243+ // }
244+ // int line_number = message->GetLineNumber(context).FromMaybe(0);
245+ // int column_number = 0;
246+ // if (message->GetStartColumn(context).IsJust())
247+ // column_number = message->GetStartColumn(context).FromJust() + 1;
248+ //
249+ // v8_inspector::StringView detailed_message;
250+ // v8_inspector::String16 message_text_string = ToString16(message->Get());
251+ // v8_inspector::StringView message_text(message_text_string.characters16(),
252+ // message_text_string.length());
253+ // v8_inspector::String16 url_string;
254+ // if (message->GetScriptOrigin().ResourceName()->IsString())
255+ // {
256+ // url_string =
257+ // ToString16(message->GetScriptOrigin().ResourceName().As<v8::String>());
258+ // }
259+ // v8_inspector::StringView url(url_string.characters16(), url_string.length());
260+ //
261+ // inspector->exceptionThrown(context, message_text, exception, detailed_message,
262+ // url, line_number, column_number,
263+ // inspector->createStackTrace(stack), script_id);
66264}
67265
68266// void JsV8InspectorClient::sendProtocolResponse(int callId, const String16 &message)
@@ -94,11 +292,17 @@ void JsV8InspectorClient::init()
94292 return ;
95293 }
96294
97- v8::Isolate::Scope isolate_scope (isolate_);
98- v8::HandleScope handleScope (isolate_);
295+ // SendMessageToBackendExtension send_message_to_backend_extension;
296+ // v8::RegisterExtension(&send_message_to_backend_extension);
297+
298+ v8::base_copied::Semaphore ready_semaphore (0 );
299+
300+ // const char *backend_extensions[0] = {"v8_inspector/setTimeout"};
301+ // v8::ExtensionConfiguration backend_configuration(arraysize(backend_extensions), backend_extensions);
302+
303+ this ->backend_runner = new TaskRunner (/* &backend_configuration*/ nullptr , false , &ready_semaphore);
304+ ready_semaphore.Wait ();
99305
100- inspector_ = V8Inspector::create (isolate_, this );
101- // inspector_->contextCreated(v8_inspector::V8ContextInfo(isolate_->GetCurrentContext(), 1, "{N} Context"));
102306}
103307
104308JsV8InspectorClient *JsV8InspectorClient::GetInstance ()
0 commit comments