Skip to content

Commit e24a3d4

Browse files
committed
rebase of Javascript debugger using named sockets that fixes
Fixes: refactored and fixed debugger for issue (issue #112) … fix debugger reattach (issue #266) fix debugger disconnect when client disconnects (issue #270) and fix for sdcard permission issues the original pull was here #273 the corresponding CLI changes are here NativeScript/nativescript-cli#1227
1 parent 531415b commit e24a3d4

11 files changed

+449
-489
lines changed

src/jni/JsDebugger.cpp

+18-14
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,21 @@ JsDebugger::JsDebugger()
1313
{
1414
}
1515

16-
void JsDebugger::Init(v8::Isolate *isolate, const string& packageName)
16+
void JsDebugger::Init(v8::Isolate *isolate, const string& packageName, jobject jsDebugger)
1717
{
1818
s_isolate = isolate;
1919
s_packageName = packageName;
2020

2121
JEnv env;
22+
s_jsDebugger = env.NewGlobalRef(jsDebugger);
23+
2224
s_JsDebuggerClass = env.FindClass("com/tns/JsDebugger");
2325
assert(s_JsDebuggerClass != nullptr);
2426

25-
s_EnqueueMessage = env.GetStaticMethodID(s_JsDebuggerClass, "enqueueMessage", "(Ljava/lang/String;)V");
27+
s_EnqueueMessage = env.GetMethodID(s_JsDebuggerClass, "enqueueMessage", "(Ljava/lang/String;)V");
2628
assert(s_EnqueueMessage != nullptr);
2729

28-
s_EnableAgent = env.GetStaticMethodID(s_JsDebuggerClass, "enableAgent", "(Ljava/lang/String;IZ)V");
30+
s_EnableAgent = env.GetMethodID(s_JsDebuggerClass, "enableAgent", "()V");
2931
assert(s_EnableAgent != nullptr);
3032
}
3133

@@ -95,19 +97,15 @@ void JsDebugger::SendCommand(JNIEnv *_env, jobject obj, jbyteArray command, jint
9597

9698
void JsDebugger::DebugBreakCallback(const v8::FunctionCallbackInfo<v8::Value>& args)
9799
{
100+
if (s_jsDebugger == nullptr)
101+
{
102+
return;
103+
}
104+
98105
try
99106
{
100107
JEnv env;
101-
JniLocalRef packageName(env.NewStringUTF(s_packageName.c_str()));
102-
103-
jint port = 8181;
104-
if ((args.Length() > 0) && args[0]->IsInt32())
105-
{
106-
port = args[0]->Int32Value();
107-
}
108-
jboolean jniFalse = JNI_FALSE;
109-
110-
env.CallStaticVoidMethod(s_JsDebuggerClass, s_EnableAgent, (jstring) packageName, port, jniFalse);
108+
env.CallStaticVoidMethod(s_JsDebuggerClass, s_EnableAgent);
111109

112110
DebugBreak();
113111
}
@@ -140,16 +138,22 @@ void JsDebugger::SendCommandToV8(uint16_t *cmd, int length)
140138
*/
141139
void JsDebugger::MyMessageHandler(const v8::Debug::Message& message)
142140
{
141+
if (s_jsDebugger == nullptr)
142+
{
143+
return;
144+
}
145+
143146
auto json = message.GetJSON();
144147
auto str = ConvertToString(json);
145148

146149
JEnv env;
147150
JniLocalRef s(env.NewStringUTF(str.c_str()));
148151

149-
env.CallStaticVoidMethod(s_JsDebuggerClass, s_EnqueueMessage, (jstring) s);
152+
env.CallVoidMethod(s_jsDebugger, s_EnqueueMessage, (jstring) s);
150153
}
151154

152155
v8::Isolate* JsDebugger::s_isolate = nullptr;
156+
jobject JsDebugger::s_jsDebugger = nullptr;
153157
string JsDebugger::s_packageName = "";
154158
jclass JsDebugger::s_JsDebuggerClass = nullptr;
155159
jmethodID JsDebugger::s_EnqueueMessage = nullptr;

src/jni/JsDebugger.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace tns
1111
class JsDebugger
1212
{
1313
public:
14-
static void Init(v8::Isolate *isolate, const std::string& packageName);
14+
static void Init(v8::Isolate *isolate, const std::string& packageName, jobject jsDebugger);
1515

1616
static void ProcessDebugMessages();
1717

@@ -35,6 +35,7 @@ namespace tns
3535

3636
static std::string s_packageName;
3737
static jclass s_JsDebuggerClass;
38+
static jobject s_jsDebugger;
3839
static jmethodID s_EnqueueMessage;
3940
static jmethodID s_EnableAgent;
4041
static v8::Isolate *s_isolate;

src/jni/NativePlatform.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jint NativePlatform::JNI_ON_LOAD(JavaVM *vm, void *reserved)
6060
return JNI_VERSION_1_6;
6161
}
6262

63-
Isolate* NativePlatform::InitNativeScript(JNIEnv *_env, jobject obj, jstring filesPath, jint appJavaObjectId, jboolean verboseLoggingEnabled, jstring packageName, jobjectArray args)
63+
Isolate* NativePlatform::InitNativeScript(JNIEnv *_env, jobject obj, jstring filesPath, jint appJavaObjectId, jboolean verboseLoggingEnabled, jstring packageName, jobjectArray args, jobject jsDebugger)
6464
{
6565
AppJavaObjectID = appJavaObjectId;
6666
tns::LogEnabled = verboseLoggingEnabled;
@@ -80,7 +80,7 @@ Isolate* NativePlatform::InitNativeScript(JNIEnv *_env, jobject obj, jstring fil
8080
DEBUG_WRITE("Initializing Telerik NativeScript: app instance id:%d", appJavaObjectId);
8181

8282
NativeScriptException::Init(g_jvm, g_objectManager);
83-
s_isolate = PrepareV8Runtime(env, filesRoot, packageName);
83+
s_isolate = PrepareV8Runtime(env, filesRoot, packageName, jsDebugger);
8484
return s_isolate;
8585
}
8686

@@ -323,7 +323,7 @@ void NativePlatform::AppInitCallback(const v8::FunctionCallbackInfo<v8::Value>&
323323
}
324324
}
325325

326-
Isolate* NativePlatform::PrepareV8Runtime(JEnv& env, const string& filesPath, jstring packageName)
326+
Isolate* NativePlatform::PrepareV8Runtime(JEnv& env, const string& filesPath, jstring packageName, jobject jsDebugger)
327327
{
328328
Platform* platform = v8::platform::CreateDefaultPlatform();
329329
V8::InitializePlatform(platform);
@@ -413,7 +413,7 @@ Isolate* NativePlatform::PrepareV8Runtime(JEnv& env, const string& filesPath, js
413413

414414
string pckName = ArgConverter::jstringToString(packageName);
415415
Profiler::Init(pckName);
416-
JsDebugger::Init(isolate, pckName);
416+
JsDebugger::Init(isolate, pckName, jsDebugger);
417417

418418
NativeScriptRuntime::BuildMetadata(env, filesPath);
419419

src/jni/NativePlatform.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace tns
1616
{
1717
public:
1818
static jint JNI_ON_LOAD(JavaVM *vm, void *reserved);
19-
static v8::Isolate* InitNativeScript(JNIEnv *_env, jobject obj, jstring filesPath, jint appJavaObjectId, jboolean verboseLoggingEnabled, jstring packageName, jobjectArray args);
19+
static v8::Isolate* InitNativeScript(JNIEnv *_env, jobject obj, jstring filesPath, jint appJavaObjectId, jboolean verboseLoggingEnabled, jstring packageName, jobjectArray args, jobject jsDebugger);
2020
static void RunModule(JNIEnv *_env, jobject obj, jstring scriptFile);
2121
static jobject RunScript(JNIEnv *_env, jobject obj, jstring scriptFile);
2222
static jobject CallJSMethodNative(JNIEnv *_env, jobject obj, jint javaObjectID, jstring methodName, jint retType, jboolean isConstructor, jobjectArray packagedArgs);
@@ -29,7 +29,7 @@ namespace tns
2929
private:
3030

3131
static void PrepareExtendFunction(v8::Isolate *isolate, jstring filesPath);
32-
static v8::Isolate* PrepareV8Runtime(JEnv& env, const std::string& filesPath, jstring packageName);
32+
static v8::Isolate* PrepareV8Runtime(JEnv& env, const std::string& filesPath, jstring packageName, jobject jsDebugger);
3333
static void AppInitCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
3434
static jobject ConvertJsValueToJavaObject(JEnv& env, const v8::Local<v8::Value>& value, int classReturnType);
3535

src/jni/com_tns_Platform.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
3131
}
3232
}
3333

34-
extern "C" void Java_com_tns_Platform_initNativeScript(JNIEnv *_env, jobject obj, jstring filesPath, jint appJavaObjectId, jboolean verboseLoggingEnabled, jstring packageName, jobjectArray args)
34+
extern "C" void Java_com_tns_Platform_initNativeScript(JNIEnv *_env, jobject obj, jstring filesPath, jint appJavaObjectId, jboolean verboseLoggingEnabled, jstring packageName, jobjectArray args, jobject jsDebugger)
3535
{
3636
try
3737
{
38-
g_isolate = NativePlatform::InitNativeScript(_env, obj, filesPath, appJavaObjectId, verboseLoggingEnabled, packageName, args);
38+
g_isolate = NativePlatform::InitNativeScript(_env, obj, filesPath, appJavaObjectId, verboseLoggingEnabled, packageName, args, jsDebugger);
3939
}
4040
catch (NativeScriptException& e)
4141
{

src/src/com/tns/ErrorReport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static Intent getIntent(Context context)
115115
Class<?> errorActivityClass = Platform.getErrorActivityClass(); // can be null or can be provided beforehand
116116

117117
// if in debug and errorActivityClass is not provided use ErrorReportActivity class
118-
if (errorActivityClass == null && Util.isDebuggableApp(context))
118+
if (errorActivityClass == null && JsDebugger.isDebuggableApp(context))
119119
{
120120
errorActivityClass = ErrorReportActivity.class;
121121
}

0 commit comments

Comments
 (0)