diff --git a/NativeScript/inspector/JsV8InspectorClient.h b/NativeScript/inspector/JsV8InspectorClient.h index 64b61bcc..dd1c2dd0 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::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 c5e39c34..82743339 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::optional 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.has_value()) { + 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 @@ -977,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, @@ -1421,7 +1441,8 @@ bool GetInt32(const json& value, long long& out) { Local domainMethodFunc, const Local& arg, Local& domainDebugger, - Local& result) { + Local& result, + std::optional& thrownMessage) { if (domainMethodFunc.IsEmpty() || !domainMethodFunc->IsFunction()) { return false; } @@ -1454,6 +1475,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.has_value()) { + thrownMessage = std::string(); + } + } + 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..63595143 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" @@ -156,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(); @@ -196,6 +202,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;