From 79ebd18f308cd86fa98784f14b5c3f5ac39d8c5f Mon Sep 17 00:00:00 2001 From: Osei Fortune Date: Mon, 8 Apr 2024 10:57:55 -0400 Subject: [PATCH] fix: inspector and globals (#1811) Co-authored-by: Igor Randjelovic --- .../src/main/cpp/JsV8InspectorClient.cpp | 38 +++++++++++++++---- test-app/runtime/src/main/cpp/Runtime.cpp | 11 ------ 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/test-app/runtime/src/main/cpp/JsV8InspectorClient.cpp b/test-app/runtime/src/main/cpp/JsV8InspectorClient.cpp index 48fcadf13..d82ffe96c 100644 --- a/test-app/runtime/src/main/cpp/JsV8InspectorClient.cpp +++ b/test-app/runtime/src/main/cpp/JsV8InspectorClient.cpp @@ -9,6 +9,7 @@ #include "Runtime.h" #include "NativeScriptException.h" +#include "NativeScriptAssert.h" #include "ArgConverter.h" #include "Util.h" @@ -271,6 +272,13 @@ void JsV8InspectorClient::init() { createInspectorSession(); tracing_agent_.reset(new tns::inspector::TracingAgentImpl()); + + try { + this->registerModules(); + } catch (NativeScriptException& e) { + // we don't want to throw if registering modules failed. + // e.ReThrowToV8(); + } } JsV8InspectorClient* JsV8InspectorClient::GetInstance() { @@ -290,12 +298,10 @@ void JsV8InspectorClient::inspectorSendEventCallback(const FunctionCallbackInfo< Local arg = args[0].As(); std::string message = ArgConverter::ConvertToString(arg); - /* JEnv env; // TODO: Pete: Check if we can use a wide (utf 16) string here JniLocalRef str(env.NewStringUTF(message.c_str())); env.CallStaticVoidMethod(instance->inspectorClass_, instance->sendMethod_, instance->connection_, (jstring) str); - */ // TODO: ios uses this method, but doesn't work on android // so I'm just sending directly to the socket (which seems to work) @@ -382,6 +388,7 @@ void JsV8InspectorClient::inspectorTimestampCallback(const FunctionCallbackInfo< } void JsV8InspectorClient::registerModules() { + DEBUG_WRITE("Registering inspector modules"); Isolate* isolate = isolate_; auto rt = Runtime::GetRuntime(isolate); v8::Locker l(isolate); @@ -394,21 +401,38 @@ void JsV8InspectorClient::registerModules() { Local global = context->Global(); Local inspectorObject = Object::New(isolate); - assert(global->Set(context, ArgConverter::ConvertToV8String(isolate, "__inspector"), inspectorObject).FromMaybe(false)); + bool success; Local func; - bool success = v8::Function::New(context, registerDomainDispatcherCallback).ToLocal(&func); - assert(success && global->Set(context, ArgConverter::ConvertToV8String(isolate, "__registerDomainDispatcher"), func).FromMaybe(false)); + // __inspector + success = global->Set(context, ArgConverter::ConvertToV8String(isolate, "__inspector"), inspectorObject).FromMaybe(false); + assert(success); + + // __registerDomainDispatcher + success = v8::Function::New(context, registerDomainDispatcherCallback).ToLocal(&func); + assert(success); + success = global->Set(context, ArgConverter::ConvertToV8String(isolate, "__registerDomainDispatcher"), func).FromMaybe(false); + assert(success); + + // __inspectorSendEvent Local data = External::New(isolate, this); success = v8::Function::New(context, inspectorSendEventCallback, data).ToLocal(&func); - assert(success && global->Set(context, ArgConverter::ConvertToV8String(isolate, "__inspectorSendEvent"), func).FromMaybe(false)); + assert(success); + success = global->Set(context, ArgConverter::ConvertToV8String(isolate, "__inspectorSendEvent"), func).FromMaybe(false); + assert(success); + // __inspectorTimestamp success = v8::Function::New(context, inspectorTimestampCallback).ToLocal(&func); - assert(success && global->Set(context, ArgConverter::ConvertToV8String(isolate, "__inspectorTimestamp"), func).FromMaybe(false)); + assert(success); + success = global->Set(context, ArgConverter::ConvertToV8String(isolate, "__inspectorTimestamp"), func).FromMaybe(false); + assert(success); TryCatch tc(isolate); Runtime::GetRuntime(isolate)->RunModule("inspector_modules"); + if(tc.HasCaught()) { + throw NativeScriptException(tc, "Error loading inspector modules"); + } } diff --git a/test-app/runtime/src/main/cpp/Runtime.cpp b/test-app/runtime/src/main/cpp/Runtime.cpp index adac3eb3c..6457527e4 100644 --- a/test-app/runtime/src/main/cpp/Runtime.cpp +++ b/test-app/runtime/src/main/cpp/Runtime.cpp @@ -201,16 +201,6 @@ void Runtime::Init(JNIEnv* env, jstring filesPath, jstring nativeLibDir, bool ve NativeScriptException::Init(); m_isolate = PrepareV8Runtime(filesRoot, nativeLibDirStr, packageNameStr, isDebuggable, callingDirStr, profilerOutputDirStr, maxLogcatObjectSize, forceLog); - -#ifdef APPLICATION_IN_DEBUG - /* - * Attach __inspector object with function callbacks that report to the Chrome DevTools frontend - */ - if (isDebuggable) { - JsV8InspectorClient::GetInstance()->registerModules(); - // JsV8InspectorClient::attachInspectorCallbacks(isolate, globalTemplate); - } -#endif } Runtime::~Runtime() { @@ -661,7 +651,6 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, const string& native }); )js"; - auto global = context->Global(); v8::Context::Scope contextScope{context};