LCOV - code coverage report
Current view: top level - source/scriptinterface - Exceptions.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 42 54 77.8 %
Date: 2026-12-03 01:19:39 Functions: 4 4 100.0 %

          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             : 
      19             : #include "precompiled.h"
      20             : 
      21             : #include "Exceptions.h"
      22             : 
      23             : #include "lib/code_annotation.h"
      24             : #include "lib/debug.h"
      25             : #include "lib/secure_crt.h"
      26             : #include "ps/CLogger.h"
      27             : #include "ps/CStr.h"
      28             : #include "scriptinterface/FunctionWrapper.h"
      29             : #include "scriptinterface/Conversions.h"
      30             : #include "scriptinterface/Request.h"
      31             : 
      32             : #include <cstdarg>
      33             : #include <js/CharacterEncoding.h>
      34             : #include <js/ErrorReport.h>
      35             : #include <js/Exception.h>
      36             : #include <js/RootingAPI.h>
      37             : #include <js/TypeDecls.h>
      38             : #include <js/Value.h>
      39             : #include <sstream>
      40             : #include <string>
      41             : 
      42             : namespace Script {
      43             : 
      44      155194 : bool Exception::IsPending(const Request& rq)
      45             : {
      46      155194 :     return JS_IsExceptionPending(rq.cx);
      47             : }
      48             : 
      49       32219 : bool Exception::CatchPending(const Request& rq)
      50             : {
      51       32219 :     if (!JS_IsExceptionPending(rq.cx))
      52             :         return false;
      53             : 
      54          16 :     JS::RootedValue excn(rq.cx);
      55          16 :     ENSURE(JS_GetPendingException(rq.cx, &excn));
      56          16 :     JS_ClearPendingException(rq.cx);
      57             : 
      58          16 :     if (excn.isUndefined())
      59             :     {
      60           0 :         LOGERROR("JavaScript error: (undefined)");
      61           0 :         return true;
      62             :     }
      63             : 
      64             :     // As of SM45/52, there is no way to recover a stack in case the thrown thing is not an Error object.
      65          16 :     if (!excn.isObject())
      66             :     {
      67           0 :         CStr error;
      68           0 :         FromJSVal(rq, excn, error);
      69           0 :         LOGERROR("JavaScript error: {}", error);
      70           0 :         return true;
      71           0 :     }
      72             : 
      73          32 :     JS::RootedObject excnObj(rq.cx, &excn.toObject());
      74          16 :     JSErrorReport* report = JS_ErrorFromException(rq.cx, excnObj);
      75             : 
      76          16 :     if (!report)
      77             :     {
      78           0 :         CStr error;
      79           0 :         FromJSVal(rq, excn, error);
      80           0 :         LOGERROR("JavaScript error: {}", error);
      81           0 :         return true;
      82           0 :     }
      83             : 
      84          16 :     std::stringstream msg;
      85          16 :     msg << "JavaScript error: ";
      86          16 :     if (report->filename)
      87             :     {
      88           8 :         msg << report->filename.c_str();
      89           8 :         msg << " line " << report->lineno << "\n";
      90             :     }
      91             : 
      92          16 :     msg << report->message().c_str();
      93             : 
      94          16 :     JS::RootedObject stackObj(rq.cx, ExceptionStackOrNull(excnObj));
      95          36 :     JS::RootedValue stackVal(rq.cx, JS::ObjectOrNullValue(stackObj));
      96             : 
      97          16 :     if (!stackVal.isNull())
      98             :     {
      99           4 :         std::string stackText;
     100           4 :         Script::Function::Call(rq, stackVal, "toString", stackText);
     101             : 
     102           4 :         std::istringstream stream(stackText);
     103          12 :         for (std::string line; std::getline(stream, line);)
     104           8 :             msg << "\n  " << line;
     105           8 :     }
     106             : 
     107          48 :     LOGERROR("{}", msg.str());
     108          16 :     return true;
     109       32219 : }
     110             : 
     111           4 : void Exception::ReportUncatchable(const Request& rq)
     112             : {
     113           4 :     if (!JS_IsExceptionPending(rq.cx))
     114           4 :         JS::ReportUncatchableException(rq.cx);
     115           4 : }
     116             : 
     117           5 : void Exception::Raise(const Request& rq, const char* format, ...)
     118             : {
     119           5 :     va_list ap;
     120           5 :     va_start(ap, format);
     121             :     // SM is single-threaded, so a static thread_local buffer needs no locking.
     122           5 :     thread_local static char buffer[256];
     123           5 :     vsprintf_s(buffer, ARRAY_SIZE(buffer), format, ap);
     124           5 :     va_end(ap);
     125             :     // Rather annoyingly, there are no va_list versions of this function, hence the preformatting above.
     126           5 :     JS_ReportErrorUTF8(rq.cx, "%s", buffer);
     127           5 : }
     128             : 
     129             : } // namespace Script

Generated by: LCOV version 1.16