Skip to content

Commit

Permalink
fix: inspector and globals (#1811)
Browse files Browse the repository at this point in the history
Co-authored-by: Igor Randjelovic <[email protected]>
  • Loading branch information
triniwiz and rigor789 authored Apr 8, 2024
1 parent 9faa25d commit 79ebd18
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
38 changes: 31 additions & 7 deletions test-app/runtime/src/main/cpp/JsV8InspectorClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "Runtime.h"
#include "NativeScriptException.h"
#include "NativeScriptAssert.h"

#include "ArgConverter.h"
#include "Util.h"
Expand Down Expand Up @@ -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() {
Expand All @@ -290,12 +298,10 @@ void JsV8InspectorClient::inspectorSendEventCallback(const FunctionCallbackInfo<
Local<v8::String> arg = args[0].As<v8::String>();
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)
Expand Down Expand Up @@ -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);
Expand All @@ -394,21 +401,38 @@ void JsV8InspectorClient::registerModules() {
Local<Object> global = context->Global();
Local<Object> inspectorObject = Object::New(isolate);

assert(global->Set(context, ArgConverter::ConvertToV8String(isolate, "__inspector"), inspectorObject).FromMaybe(false));
bool success;
Local<v8::Function> 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<External> 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");
}
}


Expand Down
11 changes: 0 additions & 11 deletions test-app/runtime/src/main/cpp/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -661,7 +651,6 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, const string& native
});
)js";


auto global = context->Global();

v8::Context::Scope contextScope{context};
Expand Down

0 comments on commit 79ebd18

Please sign in to comment.