LCOV - code coverage report
Current view: top level - source/scriptinterface - Promises.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 43 51 84.3 %
Date: 2026-12-03 01:19:39 Functions: 7 9 77.8 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "Promises.h"
      21             : 
      22             : #include "lib/debug.h"
      23             : #include "ps/CLogger.h"
      24             : #include "scriptinterface/FunctionWrapper.h"
      25             : #include "scriptinterface/Object.h"
      26             : #include "scriptinterface/Request.h"
      27             : 
      28             : #include <js/CallAndConstruct.h>
      29             : #include <js/GlobalObject.h>
      30             : #include <js/Promise.h>
      31             : #include <js/Value.h>
      32             : #include <js/ValueArray.h>
      33             : #include <string>
      34             : #include <utility>
      35             : 
      36             : struct JSContext;
      37             : 
      38             : namespace Script
      39             : {
      40             : 
      41           8 : void UnhandledRejectedPromise(JSContext* cx, bool, JS::HandleObject promise,
      42             :     JS::PromiseRejectionHandlingState state, void*)
      43             : {
      44           8 :     if (state == JS::PromiseRejectionHandlingState::Handled)
      45           2 :         return;
      46             : 
      47           6 :     const Script::Request rq{cx};
      48           6 :     JS::RootedValue reason(cx, JS::GetPromiseResult(promise));
      49             : 
      50           6 :     std::string asString;
      51           6 :     Script::Function::Call(rq, reason, "toString", asString);
      52           6 :     std::string stack;
      53           6 :     Script::GetProperty(rq, reason, "stack", stack);
      54          18 :     LOGERROR("An unhandled promise got rejected:\n{}\n{}", asString, stack);
      55           7 : }
      56             : 
      57         645 : void JobQueue::runJobs(JSContext*)
      58             : {
      59         721 :     while (!m_Jobs.empty())
      60             :     {
      61          76 :         QueueElement& element = m_Jobs.front();
      62          76 :         Script::Request rq{element.scriptInterface};
      63          76 :         JS::RootedObject localJob{rq.cx, element.job};
      64          76 :         m_Jobs.pop();
      65             : 
      66          76 :         JS::RootedValue globV{rq.cx, rq.globalValue()};
      67          76 :         JS::RootedValue rval{rq.cx};
      68          76 :         JS::Call(rq.cx, globV, localJob, JS::HandleValueArray::empty(), &rval);
      69          76 :     }
      70         645 : }
      71             : 
      72          82 : bool JobQueue::getHostDefinedData(JSContext* cx, JS::MutableHandle<JSObject*> data) const
      73             : {
      74          82 :     data.set(JS::CurrentGlobalOrNull(cx));
      75          82 :     return true;
      76             : }
      77             : 
      78          76 : bool JobQueue::enqueuePromiseJob(JSContext* cx, JS::HandleObject, JS::HandleObject job, JS::HandleObject,
      79             :     JS::HandleObject)
      80             : {
      81          76 :     try
      82             :     {
      83         228 :         m_Jobs.push({Script::Request{cx}.GetScriptInterface(), JS::PersistentRootedObject{cx, job}});
      84          76 :         return true;
      85             :     }
      86           0 :     catch (...)
      87             :     {
      88           0 :         return false;
      89           0 :     }
      90          76 : }
      91             : 
      92           0 : bool JobQueue::empty() const
      93             : {
      94           0 :     return m_Jobs.empty();
      95             : }
      96             : 
      97           1 : js::UniquePtr<JS::JobQueue::SavedJobQueue> JobQueue::saveJobQueue(JSContext*)
      98             : {
      99           1 :     class SavedJobQueue final : public JS::JobQueue::SavedJobQueue
     100             :     {
     101             :     public:
     102           1 :         SavedJobQueue(QueueType& queue) :
     103           1 :             externQueue{queue},
     104           1 :             internQueue{std::move(queue)}
     105           1 :         {}
     106             : 
     107           1 :         ~SavedJobQueue() final
     108           1 :         {
     109           1 :             ENSURE(externQueue.empty());
     110           1 :             externQueue = std::move(internQueue);
     111           1 :         }
     112             : 
     113             :     private:
     114             :         QueueType& externQueue;
     115             :         QueueType internQueue;
     116             :     };
     117             : 
     118           1 :     try
     119             :     {
     120           1 :         return js::MakeUnique<SavedJobQueue>(m_Jobs);
     121             :     }
     122           0 :     catch (...)
     123             :     {
     124           0 :         return nullptr;
     125           0 :     }
     126             : }
     127             : 
     128             : } // namespace Script

Generated by: LCOV version 1.16