From 8a8f6a59da1d01e596a7bf64366481325db0b235 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Tue, 1 Dec 2026 22:11:02 -0300 Subject: [PATCH 1/3] fix(inspector): console timestamps, and errors from JS domain handlers Every Runtime.consoleAPICalled and Runtime.exceptionThrown carried timestamp 0, so DevTools showed console times in 1970: V8 stamps them with V8InspectorClient::currentTimeMS, whose default returns 0. The main and worker clients now return the wall clock in milliseconds. A JS domain handler that threw had its command passed on to V8, which does not know the domain and answered "wasn't found". The frontend now gets the handler's error, code -32000 with the error's message, as for a failed command in Chromium. The "may be enabled at a time" error from core's domains still counts as success. --- NativeScript/inspector/JsV8InspectorClient.h | 4 +- NativeScript/inspector/JsV8InspectorClient.mm | 40 ++++++++++++++++++- .../inspector/WorkerInspectorClient.h | 1 + .../inspector/WorkerInspectorClient.mm | 8 ++++ 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/NativeScript/inspector/JsV8InspectorClient.h b/NativeScript/inspector/JsV8InspectorClient.h index 64b61bcc..7a4022d7 100644 --- a/NativeScript/inspector/JsV8InspectorClient.h +++ b/NativeScript/inspector/JsV8InspectorClient.h @@ -63,6 +63,7 @@ class JsV8InspectorClient : V8InspectorClient { // Overrides of V8InspectorClient void runMessageLoopOnPause(int contextGroupId) override; void quitMessageLoopOnPause() override; + double currentTimeMS() override; void registerModules(); @@ -241,7 +242,8 @@ class JsV8InspectorClient : V8InspectorClient { v8::Local domainMethodFunc, const v8::Local& arg, v8::Local& domainDebugger, - v8::Local& result); + v8::Local& result, + std::string& thrownMessage); std::string GetReturnMessageFromDomainHandlerResult( const v8::Local& result, const nlohmann::json& requestId); }; diff --git a/NativeScript/inspector/JsV8InspectorClient.mm b/NativeScript/inspector/JsV8InspectorClient.mm index c5e39c34..50c0f4c0 100644 --- a/NativeScript/inspector/JsV8InspectorClient.mm +++ b/NativeScript/inspector/JsV8InspectorClient.mm @@ -782,6 +782,13 @@ bool GetInt32(const json& value, long long& out) { void JsV8InspectorClient::quitMessageLoopOnPause() { pauseTerminated_ = true; } +// V8 stamps console messages and exceptions with it; its default is 0. +double JsV8InspectorClient::currentTimeMS() { + return std::chrono::duration( + std::chrono::system_clock::now().time_since_epoch()) + .count(); +} + void JsV8InspectorClient::SendToConnection(uint64_t connectionId, const std::string& message) { this->SendRawToConnection(connectionId, MaybeRewriteSourceMapURL(message)); } @@ -935,8 +942,9 @@ bool GetInt32(const json& value, long long& out) { } Local result; + std::string thrownMessage; success = this->CallDomainHandlerFunction(context, domainMethodFunc, argObject, domainDebugger, - result); + result, thrownMessage); if (enabling) { auto& enablers = this->jsDomainEnablers_[domain]; auto it = enablers.find(connectionId); @@ -967,6 +975,15 @@ bool GetInt32(const json& value, long long& out) { return; } this->pendingJsReplies_.erase(handlerId); + + // The handler owns the command, so V8, which does not know the domain, + // must not answer it instead. + if (!thrownMessage.empty()) { + json error = {{"id", json_message["id"]}, + {"error", {{"code", -32000}, {"message", thrownMessage}}}}; + this->SendRawToConnection(connectionId, error.dump()); + return; + } } // if no handler handled the message successfully, fall-through to the default V8 implementation @@ -1421,7 +1438,8 @@ bool GetInt32(const json& value, long long& out) { Local domainMethodFunc, const Local& arg, Local& domainDebugger, - Local& result) { + Local& result, + std::string& thrownMessage) { if (domainMethodFunc.IsEmpty() || !domainMethodFunc->IsFunction()) { return false; } @@ -1454,6 +1472,24 @@ bool GetInt32(const json& value, long long& out) { // log any other errors - they are caught, but still make them visible to the user. tns::LogError(isolate, tc); + // The error's message, as Chromium reports a failed command; the whole + // text for anything else that was thrown. + if (!tc.HasTerminated()) { + Local exception = tc.Exception(); + TryCatch messageTc(isolate); + Local message; + if (exception->IsNativeError() && + exception.As()->Get(context, tns::ToV8String(isolate, "message")).ToLocal(&message) && + message->IsString()) { + thrownMessage = tns::ToString(isolate, message); + } else if (exception->ToString(context).ToLocal(&message)) { + thrownMessage = tns::ToString(isolate, message); + } + if (thrownMessage.empty()) { + thrownMessage = "Internal error"; + } + } + return false; } diff --git a/NativeScript/inspector/WorkerInspectorClient.h b/NativeScript/inspector/WorkerInspectorClient.h index e6b97ffd..55d380d5 100644 --- a/NativeScript/inspector/WorkerInspectorClient.h +++ b/NativeScript/inspector/WorkerInspectorClient.h @@ -65,6 +65,7 @@ class WorkerInspectorClient final : public V8InspectorClient { // Overrides of V8InspectorClient void runMessageLoopOnPause(int contextGroupId) override; void quitMessageLoopOnPause() override; + double currentTimeMS() override; private: static constexpr int contextGroupId = 1; diff --git a/NativeScript/inspector/WorkerInspectorClient.mm b/NativeScript/inspector/WorkerInspectorClient.mm index 25f422ad..9b9430cb 100644 --- a/NativeScript/inspector/WorkerInspectorClient.mm +++ b/NativeScript/inspector/WorkerInspectorClient.mm @@ -1,5 +1,7 @@ #include "WorkerInspectorClient.h" +#include + #include "src/inspector/v8-inspector-impl.h" #include "src/inspector/v8-inspector-session-impl.h" #include "src/inspector/v8-runtime-agent-impl.h" @@ -196,6 +198,12 @@ StringView Make8BitStringView(const std::string& value) { void WorkerInspectorClient::quitMessageLoopOnPause() { pauseTerminated_ = true; } +double WorkerInspectorClient::currentTimeMS() { + return std::chrono::duration( + std::chrono::system_clock::now().time_since_epoch()) + .count(); +} + void WorkerInspectorClient::NotifyTerminating() { dying_ = true; pauseTerminated_ = true; From 219b505c59769732a85521ace09d2088559bf3d2 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Tue, 1 Dec 2026 22:20:13 -0300 Subject: [PATCH 2/3] fix(inspector): don't run the app's promise callbacks while paused Every command a frontend sent while the app was paused ended with a microtask checkpoint, which ran the app's own pending promise callbacks in the middle of the paused code. Chrome runs them only after the resume. The checkpoint is now skipped while paused, on the main isolate and in workers. --- NativeScript/inspector/JsV8InspectorClient.mm | 7 +++++-- NativeScript/inspector/WorkerInspectorClient.mm | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/NativeScript/inspector/JsV8InspectorClient.mm b/NativeScript/inspector/JsV8InspectorClient.mm index 50c0f4c0..bab4a33c 100644 --- a/NativeScript/inspector/JsV8InspectorClient.mm +++ b/NativeScript/inspector/JsV8InspectorClient.mm @@ -994,8 +994,11 @@ bool GetInt32(const json& value, long long& out) { this->StopHolding(connectionId, true); } - // TODO: check why this is needed (it should trigger automatically when script depth is 0) - isolate->PerformMicrotaskCheckpoint(); + // Runs what the command queued. Not while paused, as in Chrome: it would + // also run the app's own promise callbacks in the middle of paused JS. + if (!runningPauseLoop_) { + isolate->PerformMicrotaskCheckpoint(); + } } void JsV8InspectorClient::HandleLoadNetworkResource(uint64_t connectionId, int msgId, diff --git a/NativeScript/inspector/WorkerInspectorClient.mm b/NativeScript/inspector/WorkerInspectorClient.mm index 9b9430cb..63595143 100644 --- a/NativeScript/inspector/WorkerInspectorClient.mm +++ b/NativeScript/inspector/WorkerInspectorClient.mm @@ -158,6 +158,10 @@ StringView Make8BitStringView(const std::string& value) { sessions_->Connect(inspector_.get(), item.connectionId); sessions_->Dispatch(item.connectionId, item.method, item.message); + // Not while paused; see JsV8InspectorClient::DispatchFrontendMessage. + if (runningPauseLoop_) { + return; + } Isolate::Scope isolate_scope(isolate_); HandleScope handle_scope(isolate_); isolate_->PerformMicrotaskCheckpoint(); From e16ce71420560a0066b36877d68d718d7899ba95 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Tue, 1 Dec 2026 23:23:05 -0300 Subject: [PATCH 3/3] fix(inspector): keep an empty error message from a domain handler --- NativeScript/inspector/JsV8InspectorClient.h | 2 +- NativeScript/inspector/JsV8InspectorClient.mm | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/NativeScript/inspector/JsV8InspectorClient.h b/NativeScript/inspector/JsV8InspectorClient.h index 7a4022d7..dd1c2dd0 100644 --- a/NativeScript/inspector/JsV8InspectorClient.h +++ b/NativeScript/inspector/JsV8InspectorClient.h @@ -243,7 +243,7 @@ class JsV8InspectorClient : V8InspectorClient { const v8::Local& arg, v8::Local& domainDebugger, v8::Local& result, - std::string& thrownMessage); + std::optional& thrownMessage); std::string GetReturnMessageFromDomainHandlerResult( const v8::Local& result, const nlohmann::json& requestId); }; diff --git a/NativeScript/inspector/JsV8InspectorClient.mm b/NativeScript/inspector/JsV8InspectorClient.mm index bab4a33c..82743339 100644 --- a/NativeScript/inspector/JsV8InspectorClient.mm +++ b/NativeScript/inspector/JsV8InspectorClient.mm @@ -942,7 +942,7 @@ bool GetInt32(const json& value, long long& out) { } Local result; - std::string thrownMessage; + std::optional thrownMessage; success = this->CallDomainHandlerFunction(context, domainMethodFunc, argObject, domainDebugger, result, thrownMessage); if (enabling) { @@ -978,9 +978,9 @@ bool GetInt32(const json& value, long long& out) { // The handler owns the command, so V8, which does not know the domain, // must not answer it instead. - if (!thrownMessage.empty()) { + if (thrownMessage.has_value()) { json error = {{"id", json_message["id"]}, - {"error", {{"code", -32000}, {"message", thrownMessage}}}}; + {"error", {{"code", -32000}, {"message", *thrownMessage}}}}; this->SendRawToConnection(connectionId, error.dump()); return; } @@ -1442,7 +1442,7 @@ bool GetInt32(const json& value, long long& out) { const Local& arg, Local& domainDebugger, Local& result, - std::string& thrownMessage) { + std::optional& thrownMessage) { if (domainMethodFunc.IsEmpty() || !domainMethodFunc->IsFunction()) { return false; } @@ -1488,8 +1488,8 @@ bool GetInt32(const json& value, long long& out) { } else if (exception->ToString(context).ToLocal(&message)) { thrownMessage = tns::ToString(isolate, message); } - if (thrownMessage.empty()) { - thrownMessage = "Internal error"; + if (!thrownMessage.has_value()) { + thrownMessage = std::string(); } }