Skip to content

Commit cadc39f

Browse files
authored
Merge pull request #652 from NativeScript/v8-inspector-5.5
V8 inspector 5.5
2 parents e6951da + b8a867c commit cadc39f

127 files changed

Lines changed: 33877 additions & 284 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

build-artifacts/project-template-gradle/src/main/java/com/tns/AndroidJsDebugger.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
public class AndroidJsDebugger implements Debugger
2828
{
29+
private AndroidJsV8Inspector v8Inspector;
30+
2931
@Override
3032
public void onConnect(JsDebugger context)
3133
{
@@ -456,6 +458,17 @@ public void start()
456458
{
457459
AndroidJsDebugger.this.debugContext.enableAgent();
458460

461+
v8Inspector = new AndroidJsV8Inspector(context, logger);
462+
try
463+
{
464+
v8Inspector.start();
465+
}
466+
catch (IOException e)
467+
{
468+
e.printStackTrace();
469+
}
470+
471+
459472
handlerThread = new HandlerThread("debugAgentBroadCastReceiverHandler");
460473
handlerThread.start();
461474
Handler handler = new Handler(handlerThread.getLooper());

build-artifacts/project-template-gradle/src/main/java/com/tns/Util.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ private static void silentClose(Closeable closeable)
114114

115115
public static Boolean isPositive(String value)
116116
{
117+
if(value == null) {
118+
return false;
119+
}
120+
117121
return (value.equals("true") || value.equals("TRUE") ||
118122
value.equals("yes") || value.equals("YES") ||
119123
value.equals("enabled") || value.equals("ENABLED"));

runtime/build.gradle

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,22 @@ model {
219219
moduleName = "NativeScript"
220220

221221
cppFlags.addAll(["-I${file("src/main/jni")}".toString(),
222-
"-I${file("src/main/jni/include")}".toString()
222+
"-I${file("src/main/jni/include")}".toString(),
223+
"-I${file("src/main/jni/v8_inspector/")}".toString(),
224+
//"-I${file("src/main/jni/v8_inspector/platform/inspector_protocol/")}".toString(),
225+
//"-I${file("src/main/jni/v8_inspector/protocol/")}".toString(),
223226
])
224227

225-
cppFlags.addAll(["-std=c++11", "-fexceptions", "-fno-builtin-stpcpy"])
228+
CFlags.addAll(["-I${file("src/main/jni")}".toString(),
229+
"-I${file("src/main/jni/include")}".toString(),
230+
"-I${file("src/main/jni/v8_inspector")}".toString(),
231+
//"-I${file("src/main/jni/v8_inspector/platform/inspector_protocol/")}".toString(),
232+
//"-I${file("src/main/jni/v8_inspector/protocol/")}".toString(),
233+
])
234+
235+
cppFlags.addAll(["-std=c++11", "-fexceptions", "-fno-builtin-stpcpy", "-DHAVE_INSPECTOR", "-D__ANDROID__"]) //, "-DV8_INSPECTOR_USE_STL=1", "-D__GXX_EXPERIMENTAL_CXX0X__=1"
236+
237+
226238
CFlags.addAll(["-Wno-error=format-security", "-g", "-fno-builtin-stpcpy"])
227239

228240
ldLibs.addAll(["android", "dl", "log", "atomic", "z"])
@@ -235,7 +247,26 @@ model {
235247
toolchain = "clang"
236248
stl = "c++_static"
237249

238-
abiFilters.addAll(["armeabi-v7a", "x86", "arm64-v8a"])
250+
abiFilters.addAll(["armeabi-v7a", "x86"])
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+
}
263+
else
264+
{
265+
// Built from command line
266+
abiFilters.addAll(["x86", "arm64-v8a"])
267+
268+
println "Build from Command Line"
269+
}
239270
}
240271

241272
android.sources {
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
package com.tns;
2+
3+
import android.content.Context;
4+
import android.os.Handler;
5+
import android.util.Log;
6+
7+
import java.io.IOException;
8+
import java.util.concurrent.ConcurrentLinkedQueue;
9+
import java.util.concurrent.LinkedBlockingQueue;
10+
import java.util.concurrent.atomic.AtomicBoolean;
11+
12+
import fi.iki.elonen.NanoHTTPD;
13+
import fi.iki.elonen.NanoWSD;
14+
15+
public class AndroidJsV8Inspector
16+
{
17+
public static final String DISCONNECT_MESSAGE = "nativescript-inspector-disconnect";
18+
private JsV8InspectorServer server;
19+
private Logger logger;
20+
private Context context;
21+
22+
protected native final void init();
23+
24+
protected native final void connect(Object connection);
25+
26+
protected static native final void disconnect();
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+
}
37+
38+
public void start() throws IOException
39+
{
40+
if (this.server == null)
41+
{
42+
Runtime currentRuntime = Runtime.getCurrentRuntime();
43+
44+
mainHandler = currentRuntime.getHandler();
45+
46+
this.server = new JsV8InspectorServer(context.getPackageName() + "-inspectorServer");
47+
this.server.start(-1);
48+
49+
//Log.d("V8Inspector", "init ThreadId:" + Thread.currentThread().getId());
50+
51+
init();
52+
}
53+
}
54+
55+
private static void send(Object connection, String payload) throws IOException
56+
{
57+
((JsV8InspectorWebSocket) connection).send(payload);
58+
}
59+
60+
private static String getInspectorMessage(Object connection)
61+
{
62+
return ((JsV8InspectorWebSocket) connection).getInspectorMessage();
63+
}
64+
65+
class JsV8InspectorServer extends NanoWSD
66+
{
67+
public JsV8InspectorServer(String name)
68+
{
69+
super(name);
70+
}
71+
72+
@Override
73+
protected Response serveHttp(IHTTPSession session)
74+
{
75+
//Log.d("{N}.v8-inspector", "http request for " + session.getUri());
76+
return super.serveHttp(session);
77+
}
78+
79+
@Override
80+
protected WebSocket openWebSocket(IHTTPSession handshake)
81+
{
82+
return new JsV8InspectorWebSocket(handshake);
83+
}
84+
}
85+
86+
class JsV8InspectorWebSocket extends NanoWSD.WebSocket
87+
{
88+
89+
public JsV8InspectorWebSocket(NanoHTTPD.IHTTPSession handshakeRequest)
90+
{
91+
super(handshakeRequest);
92+
}
93+
94+
@Override
95+
protected void onOpen()
96+
{
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+
}
132+
}
133+
134+
@Override
135+
protected void onClose(NanoWSD.WebSocketFrame.CloseCode code, String reason, boolean initiatedByRemote)
136+
{
137+
//Log.d("V8Inspector", "onClose");
138+
mainHandler.post(new Runnable()
139+
{
140+
@Override
141+
public void run()
142+
{
143+
disconnect();
144+
}
145+
});
146+
147+
inspectorMessages.offer(DISCONNECT_MESSAGE);
148+
}
149+
150+
@Override
151+
protected void onMessage(final NanoWSD.WebSocketFrame message)
152+
{
153+
//Log.d("V8Inspector", "onMessage");
154+
//Log.d("V8Inspector", "onMessage TextPayload" + message.getTextPayload() + " ThreadId:" + Thread.currentThread().getId());
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+
172+
@Override
173+
public void send(String payload) throws IOException
174+
{
175+
//Log.d("V8Inspector", "send " + payload);
176+
super.send(payload);
177+
}
178+
179+
public String getInspectorMessage()
180+
{
181+
try
182+
{
183+
String message = inspectorMessages.take();
184+
185+
if (message != null && message.equalsIgnoreCase(DISCONNECT_MESSAGE))
186+
{
187+
disconnect();
188+
return null;
189+
}
190+
191+
return message;
192+
}
193+
catch (InterruptedException e)
194+
{
195+
e.printStackTrace();
196+
}
197+
198+
return null;
199+
}
200+
201+
202+
@Override
203+
protected void onPong(NanoWSD.WebSocketFrame pong)
204+
{
205+
//Log.d("V8Inspector", "onPong");
206+
}
207+
208+
@Override
209+
protected void onException(IOException exception)
210+
{
211+
exception.printStackTrace();
212+
disconnect();
213+
}
214+
}
215+
}

0 commit comments

Comments
 (0)