Skip to content

Commit 78ac59b

Browse files
committed
debugger communication using v8 tasks
1 parent 8468a14 commit 78ac59b

27 files changed

Lines changed: 3799 additions & 896 deletions

runtime/build.gradle

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ model {
232232
//"-I${file("src/main/jni/v8_inspector/protocol/")}".toString(),
233233
])
234234

235-
cppFlags.addAll(["-std=c++11", "-fexceptions", "-fno-builtin-stpcpy", "-DHAVE_INSPECTOR"]) //, "-DV8_INSPECTOR_USE_STL=1", "-D__GXX_EXPERIMENTAL_CXX0X__=1"
235+
cppFlags.addAll(["-std=c++11", "-fexceptions", "-fno-builtin-stpcpy", "-DHAVE_INSPECTOR", "-D__ANDROID__"]) //, "-DV8_INSPECTOR_USE_STL=1", "-D__GXX_EXPERIMENTAL_CXX0X__=1"
236236

237237

238238
CFlags.addAll(["-Wno-error=format-security", "-g", "-fno-builtin-stpcpy"])
@@ -247,7 +247,25 @@ model {
247247
toolchain = "clang"
248248
stl = "c++_static"
249249

250-
abiFilters.addAll(["armeabi-v7a", "x86", "arm64-v8a"])
250+
abiFilters.addAll(["armeabi-v7a"])
251+
252+
if (System.getProperties()['idea.platform.prefix'] != null)
253+
{
254+
// Built from AndroidStudio
255+
println "Build from AndroidStudio"
256+
257+
if (!ndkDebuggable)
258+
{
259+
abiFilters.addAll(["x86", "arm64-v8a"])
260+
}
261+
}
262+
else
263+
{
264+
// Built from command line
265+
abiFilters.addAll(["x86", "arm64-v8a"])
266+
267+
println "Build from Command Line"
268+
}
251269
}
252270

253271
android.sources {

runtime/src/main/java/com/tns/AndroidJsV8Inspector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ protected void onClose(NanoWSD.WebSocketFrame.CloseCode code, String reason, boo
8989
protected void onMessage(NanoWSD.WebSocketFrame message)
9090
{
9191
Log.d("V8Inspector", "onMessage");
92-
Log.d("V8Inspector", "onMessage TextPayload" + message.getTextPayload());
92+
Log.d("V8Inspector", "onMessage TextPayload" + message.getTextPayload() + " ThreadId:" + Thread.currentThread().getId());
9393
dispatchMessage(message.getTextPayload());
9494
}
9595

runtime/src/main/jni/JsDebugger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void JsDebugger::Enable()
4444
v8::Isolate::Scope isolate_scope(isolate);
4545
v8::HandleScope handleScope(isolate);
4646

47-
v8::Debug::SetMessageHandler(isolate, MyMessageHandler);
47+
//v8::Debug::SetMessageHandler(MyMessageHandler);
4848
enabled = true;
4949
}
5050

runtime/src/main/jni/JsV8InspectorClient.cpp

Lines changed: 220 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,150 @@
99
#include "Runtime.h"
1010
#include "src/inspector/string-16.h"
1111

12+
#include "src/inspector/task-runner.h"
13+
1214
using namespace std;
1315
using namespace tns;
1416
using namespace v8;
1517

1618
using 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+
1890
JsV8InspectorClient::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+
35115
void 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

46153
void 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+
58166
void 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

104308
JsV8InspectorClient *JsV8InspectorClient::GetInstance()

runtime/src/main/jni/JsV8InspectorClient.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define JSV8INSPECTORCLIENT_H_
33

44
#include <string>
5+
#include <v8_inspector/src/inspector/task-runner.h>
56
#include "v8.h"
67
#include "v8-debug.h"
78
#include "JEnv.h"
@@ -16,28 +17,37 @@ using namespace v8_inspector;
1617

1718
namespace tns
1819
{
19-
class JsV8InspectorClient : V8InspectorClient //, InspectorClientImpl::FrontendChannel
20+
class JsV8InspectorClient : V8InspectorClient, v8_inspector::V8Inspector::Channel
2021
{
2122
public:
2223
static JsV8InspectorClient* GetInstance();
2324

2425
void init();
2526
void connect(jobject connection);
27+
void doConnect(v8::Isolate *isolate, const v8::Local<v8::Context>& context);
2628
void disconnect();
27-
void dispatchMessage(const std::string& message);
28-
//void sendProtocolResponse(int callId, const String16& message) override;
29+
void dispatchMessage(const std::string& message);
30+
void doDispatchMessage(v8::Isolate *isolate, const std::string& message);
31+
32+
//void sendProtocolResponse(int callId, const String16& message) override;
2933
//void sendProtocolNotification(const String16& message) override;
3034
//void flushProtocolNotifications() override;
3135

36+
void sendProtocolResponse(int callId, const v8_inspector::StringView &message) override;
37+
void sendProtocolNotification(const v8_inspector::StringView &message) override;
38+
void flushProtocolNotifications() override;
39+
3240
private:
3341
JsV8InspectorClient(v8::Isolate *isolate);
42+
~JsV8InspectorClient();
3443

3544
static JsV8InspectorClient* instance;
3645
static jclass inspectorClass;
3746
static jmethodID sendMethod;
3847

48+
TaskRunner* backend_runner;
3949
v8::Isolate* isolate_;
40-
std::unique_ptr<V8Inspector> inspector_ = nullptr;
50+
std::unique_ptr<V8Inspector> inspector_;
4151
std::unique_ptr<V8InspectorSession> session_;
4252
jobject connection;
4353
};

0 commit comments

Comments
 (0)