Skip to content

Commit d87ad9c

Browse files
committed
pump v8 runtime message loop on pause
1 parent 78ac59b commit d87ad9c

15 files changed

Lines changed: 361 additions & 116 deletions

File tree

runtime/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ model {
247247
toolchain = "clang"
248248
stl = "c++_static"
249249

250-
abiFilters.addAll(["armeabi-v7a"])
250+
abiFilters.addAll(["armeabi-v7a", "x86"])
251251

252252
if (System.getProperties()['idea.platform.prefix'] != null)
253253
{
@@ -257,6 +257,7 @@ model {
257257
if (!ndkDebuggable)
258258
{
259259
abiFilters.addAll(["x86", "arm64-v8a"])
260+
260261
}
261262
}
262263
else

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

Lines changed: 123 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,65 @@
11
package com.tns;
22

33
import android.content.Context;
4+
import android.os.Handler;
45
import android.util.Log;
56

67
import java.io.IOException;
8+
import java.util.concurrent.ConcurrentLinkedQueue;
9+
import java.util.concurrent.LinkedBlockingQueue;
10+
import java.util.concurrent.atomic.AtomicBoolean;
711

812
import fi.iki.elonen.NanoHTTPD;
913
import fi.iki.elonen.NanoWSD;
1014

1115
public class AndroidJsV8Inspector
1216
{
17+
public static final String DISCONNECT_MESSAGE = "nativescript-inspector-disconnect";
1318
private JsV8InspectorServer server;
1419
private Logger logger;
15-
private Context context;
20+
private Context context;
1621

1722
protected native final void init();
23+
1824
protected native final void connect(Object connection);
19-
protected native final void disconnect();
20-
protected native final void dispatchMessage(String message);
2125

26+
protected static native final void disconnect();
2227

23-
public AndroidJsV8Inspector(Context context, Logger logger)
24-
{
25-
this.context = context;
26-
this.logger = logger;
27-
}
28+
protected native final void dispatchMessage(String message);
29+
protected Handler mainHandler;
30+
private LinkedBlockingQueue<String> inspectorMessages = new LinkedBlockingQueue<String>();
31+
32+
public AndroidJsV8Inspector(Context context, Logger logger)
33+
{
34+
this.context = context;
35+
this.logger = logger;
36+
}
2837

2938
public void start() throws IOException
3039
{
3140
if (this.server == null)
3241
{
42+
Runtime currentRuntime = Runtime.getCurrentRuntime();
43+
44+
mainHandler = currentRuntime.getHandler();
45+
3346
this.server = new JsV8InspectorServer(context.getPackageName() + "-inspectorServer");
3447
this.server.start(-1);
3548

49+
Log.d("V8Inspector", "init ThreadId:" + Thread.currentThread().getId());
50+
3651
init();
3752
}
3853
}
3954

4055
private static void send(Object connection, String payload) throws IOException
4156
{
42-
((JsV8InspectorWebSocket)connection).send(payload);
57+
((JsV8InspectorWebSocket) connection).send(payload);
58+
}
59+
60+
private static String getInspectorMessage(Object connection)
61+
{
62+
return ((JsV8InspectorWebSocket) connection).getInspectorMessage();
4363
}
4464

4565
class JsV8InspectorServer extends NanoWSD
@@ -74,23 +94,89 @@ public JsV8InspectorWebSocket(NanoHTTPD.IHTTPSession handshakeRequest)
7494
@Override
7595
protected void onOpen()
7696
{
77-
Log.d("V8Inspector", "onOpen");
78-
connect(this);
97+
Log.d("V8Inspector", "onOpen: ThreadID: " + Thread.currentThread().getId());
98+
99+
final Object waitObject = new Object();
100+
101+
mainHandler.post(new Runnable()
102+
{
103+
@Override
104+
public void run()
105+
{
106+
try
107+
{
108+
Log.d("V8Inspector", "onOpen: runnable ThreadID : " + Thread.currentThread().getId());
109+
connect(JsV8InspectorWebSocket.this);
110+
}
111+
finally
112+
{
113+
synchronized (waitObject)
114+
{
115+
waitObject.notify();
116+
}
117+
}
118+
}
119+
});
120+
121+
try
122+
{
123+
synchronized (waitObject)
124+
{
125+
waitObject.wait();
126+
}
127+
}
128+
catch (InterruptedException e)
129+
{
130+
e.printStackTrace();
131+
}
79132
}
80133

81134
@Override
82135
protected void onClose(NanoWSD.WebSocketFrame.CloseCode code, String reason, boolean initiatedByRemote)
83136
{
84137
Log.d("V8Inspector", "onClose");
85-
disconnect();
138+
mainHandler.post(new Runnable()
139+
{
140+
@Override
141+
public void run()
142+
{
143+
disconnect();
144+
}
145+
});
146+
147+
inspectorMessages.offer(DISCONNECT_MESSAGE);
86148
}
87149

88150
@Override
89-
protected void onMessage(NanoWSD.WebSocketFrame message)
151+
protected void onMessage(final NanoWSD.WebSocketFrame message)
90152
{
91153
Log.d("V8Inspector", "onMessage");
92154
Log.d("V8Inspector", "onMessage TextPayload" + message.getTextPayload() + " ThreadId:" + Thread.currentThread().getId());
93-
dispatchMessage(message.getTextPayload());
155+
inspectorMessages.offer(message.getTextPayload());
156+
157+
mainHandler.post(new Runnable()
158+
{
159+
@Override
160+
public void run()
161+
{
162+
String nextMessage = inspectorMessages.poll();
163+
while (nextMessage != null)
164+
{
165+
dispatchMessage(nextMessage);
166+
nextMessage = inspectorMessages.poll();
167+
}
168+
}
169+
});
170+
171+
// mainHandler.post(new Runnable()
172+
// {
173+
// @Override
174+
// public void run()
175+
// {
176+
//
177+
// dispatchMessage(message.getTextPayload());
178+
// }
179+
// });
94180
}
95181

96182
@Override
@@ -100,6 +186,29 @@ public void send(String payload) throws IOException
100186
super.send(payload);
101187
}
102188

189+
public String getInspectorMessage()
190+
{
191+
try
192+
{
193+
String message = inspectorMessages.take();
194+
195+
if (message != null && message.equalsIgnoreCase(DISCONNECT_MESSAGE))
196+
{
197+
disconnect();
198+
return null;
199+
}
200+
201+
return message;
202+
}
203+
catch (InterruptedException e)
204+
{
205+
e.printStackTrace();
206+
}
207+
208+
return null;
209+
}
210+
211+
103212
@Override
104213
protected void onPong(NanoWSD.WebSocketFrame pong)
105214
{

runtime/src/main/jni/JsDebugger.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void JsDebugger::Disable()
5858
v8::Isolate::Scope isolate_scope(isolate);
5959
v8::HandleScope handleScope(isolate);
6060

61-
v8::Debug::SetMessageHandler(isolate, nullptr);
61+
//v8::Debug::SetMessageHandler(nullptr);
6262
}
6363

6464
/* *
@@ -70,7 +70,7 @@ void JsDebugger::DebugBreak()
7070
v8::Isolate::Scope isolate_scope(isolate);
7171
v8::HandleScope handleScope(isolate);
7272

73-
v8::Debug::DebugBreak(isolate);
73+
//v8::Debug::DebugBreak(isolate);
7474
}
7575

7676
bool JsDebugger::IsDebuggerActive()
@@ -84,7 +84,7 @@ void JsDebugger::ProcessDebugMessages()
8484
v8::Isolate::Scope isolate_scope(isolate);
8585
v8::HandleScope handleScope(isolate);
8686

87-
v8::Debug::ProcessDebugMessages(isolate);
87+
//v8::Debug::ProcessDebugMessages();
8888
}
8989

9090
void JsDebugger::SendCommand(JNIEnv *_env, jobject obj, jbyteArray command, jint length)
@@ -241,7 +241,7 @@ void JsDebugger::SendCommandToV8(uint16_t *cmd, int length)
241241
{
242242
auto isolate = s_isolate;
243243

244-
v8::Debug::SendCommand(isolate, cmd, length, nullptr);
244+
//v8::Debug::SendCommand(isolate, cmd, length, nullptr);
245245

246246
s_processedCommands = true;
247247
}

0 commit comments

Comments
 (0)