LCOV - code coverage report
Current view: top level - source/scriptinterface - Conversions.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 95 147 64.6 %
Date: 2026-12-03 01:19:39 Functions: 28 49 57.1 %

          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 "Conversions.h"
      21             : 
      22             : #include "graphics/Entity.h"
      23             : #include "lib/path.h"
      24             : #include "ps/CStr.h"
      25             : #include "scriptinterface/Exceptions.h"
      26             : #include "scriptinterface/Request.h"
      27             : 
      28             : #include <cstddef>
      29             : #include <js/Conversions.h>
      30             : #include <js/GCAPI.h>
      31             : #include <js/String.h>
      32             : #include <jsapi.h>
      33             : #include <string>
      34             : 
      35             : namespace Script{
      36             : 
      37             : // Catch the raised exception right away to ensure the stack trace gets
      38             : // printed. The script is then aborted without a catchable exception which is
      39             : // required since ESR 140.
      40             : #define FAIL(msg) STMT(Exception::Raise(rq, msg); Exception::CatchPending(rq); Exception::ReportUncatchable(rq); return false)
      41             : 
      42             : // Implicit type conversions often hide bugs, so fail.
      43             : #define FAIL_IF_NOT(c, v) STMT(if (!(c)) { \
      44             :     Exception::Raise(rq, "Script value conversion check failed: %s (got type %s)", #c, JS::InformalValueTypeName(v)); \
      45             :     Exception::CatchPending(rq); \
      46             :     Exception::ReportUncatchable(rq); \
      47             :     return false; \
      48             : })
      49             : 
      50        3477 : template<> bool FromJSVal<bool>(const Request& rq, JS::HandleValue v, bool& out)
      51             : {
      52        3477 :     FAIL_IF_NOT(v.isBoolean(), v);
      53        3477 :     out = JS::ToBoolean(v);
      54        3477 :     return true;
      55             : }
      56             : 
      57        1425 : template<> bool FromJSVal<float>(const Request& rq, JS::HandleValue v, float& out)
      58             : {
      59        1425 :     double tmp;
      60        1425 :     FAIL_IF_NOT(v.isNumber(), v);
      61        1425 :     if (!JS::ToNumber(rq.cx, v, &tmp))
      62             :         return false;
      63        1425 :     out = tmp;
      64        1425 :     return true;
      65             : }
      66             : 
      67           6 : template<> bool FromJSVal<double>(const Request& rq,  JS::HandleValue v, double& out)
      68             : {
      69           6 :     FAIL_IF_NOT(v.isNumber(), v);
      70           6 :     if (!JS::ToNumber(rq.cx, v, &out))
      71             :         return false;
      72             :     return true;
      73             : }
      74             : 
      75      119236 : template<> bool FromJSVal<std::int32_t>(const Request& rq,  JS::HandleValue v, std::int32_t& out)
      76             : {
      77      119236 :     FAIL_IF_NOT(v.isNumber(), v);
      78      119235 :     if (!JS::ToInt32(rq.cx, v, &out))
      79             :         return false;
      80             :     return true;
      81             : }
      82             : 
      83      119313 : template<> bool FromJSVal<std::uint32_t>(const Request& rq,  JS::HandleValue v, std::uint32_t& out)
      84             : {
      85      119313 :     FAIL_IF_NOT(v.isNumber(), v);
      86      119314 :     if (!JS::ToUint32(rq.cx, v, &out))
      87             :         return false;
      88             :     return true;
      89             : }
      90             : 
      91           0 : template<> bool FromJSVal<std::uint16_t>(const Request& rq,  JS::HandleValue v, std::uint16_t& out)
      92             : {
      93           0 :     FAIL_IF_NOT(v.isNumber(), v);
      94           0 :     if (!JS::ToUint16(rq.cx, v, &out))
      95             :         return false;
      96             :     return true;
      97             : }
      98             : 
      99         162 : template<> bool FromJSVal<std::uint8_t>(const Request& rq,  JS::HandleValue v, std::uint8_t& out)
     100             : {
     101         162 :     std::uint16_t tmp;
     102         162 :     FAIL_IF_NOT(v.isNumber(), v);
     103         162 :     if (!JS::ToUint16(rq.cx, v, &tmp))
     104             :         return false;
     105         162 :     out = static_cast<std::uint8_t>(tmp);
     106         162 :     return true;
     107             : }
     108             : 
     109       62780 : template<> bool FromJSVal<std::wstring>(const Request& rq,  JS::HandleValue v, std::wstring& out)
     110             : {
     111       62780 :     FAIL_IF_NOT(v.isString() || v.isNumber() || v.isBoolean(), v); // allow implicit boolean/number conversions
     112      125556 :     JS::RootedString str(rq.cx, JS::ToString(rq.cx, v));
     113       62778 :     if (!str)
     114           0 :         FAIL("Argument must be convertible to a string");
     115             : 
     116       62778 :     if (JS::StringHasLatin1Chars(str))
     117             :     {
     118       62774 :         size_t length;
     119       62774 :         JS::AutoCheckCannotGC nogc;
     120       62774 :         const JS::Latin1Char* ch = JS_GetLatin1StringCharsAndLength(rq.cx, nogc, str, &length);
     121       62774 :         if (!ch)
     122           0 :             FAIL("JS_GetLatin1StringCharsAndLength failed");
     123             : 
     124       62774 :         out.assign(ch, ch + length);
     125             :     }
     126             :     else
     127             :     {
     128           4 :         size_t length;
     129           4 :         JS::AutoCheckCannotGC nogc;
     130           4 :         const char16_t* ch = JS_GetTwoByteStringCharsAndLength(rq.cx, nogc, str, &length);
     131           4 :         if (!ch)
     132           0 :             FAIL("JS_GetTwoByteStringsCharsAndLength failed"); // out of memory
     133             : 
     134           4 :         out.assign(ch, ch + length);
     135             :     }
     136             :     return true;
     137       62780 : }
     138             : 
     139         576 : template<> bool FromJSVal<Path>(const Request& rq,  JS::HandleValue v, Path& out)
     140             : {
     141         576 :     std::wstring string;
     142         576 :     if (!FromJSVal(rq, v, string))
     143             :         return false;
     144        1728 :     out = string;
     145         576 :     return true;
     146         576 : }
     147             : 
     148       16104 : template<> bool FromJSVal<std::string>(const Request& rq,  JS::HandleValue v, std::string& out)
     149             : {
     150       16104 :     std::wstring wideout;
     151       16104 :     if (!FromJSVal(rq, v, wideout))
     152             :         return false;
     153       32204 :     out = CStrW(wideout).ToUTF8();
     154       16102 :     return true;
     155       16104 : }
     156             : 
     157        1199 : template<> bool FromJSVal<CStr8>(const Request& rq,  JS::HandleValue v, CStr8& out)
     158             : {
     159        1199 :     return FromJSVal(rq, v, static_cast<std::string&>(out));
     160             : }
     161             : 
     162           0 : template<> bool FromJSVal<CStrW>(const Request& rq,  JS::HandleValue v, CStrW& out)
     163             : {
     164           0 :     return FromJSVal(rq, v, static_cast<std::wstring&>(out));
     165             : }
     166             : 
     167           0 : template<> bool FromJSVal<Entity>(const Request& rq,  JS::HandleValue v, Entity& out)
     168             : {
     169           0 :     if (!v.isObject())
     170           0 :         FAIL("Argument must be an object");
     171             : 
     172           0 :     JS::RootedObject obj(rq.cx, &v.toObject());
     173           0 :     JS::RootedValue templateName(rq.cx);
     174           0 :     JS::RootedValue id(rq.cx);
     175           0 :     JS::RootedValue player(rq.cx);
     176           0 :     JS::RootedValue position(rq.cx);
     177           0 :     JS::RootedValue rotation(rq.cx);
     178             : 
     179             :     // TODO: Report type errors
     180           0 :     if (!JS_GetProperty(rq.cx, obj, "player", &player) || !FromJSVal(rq, player, out.playerID))
     181           0 :         FAIL("Failed to read Entity.player property");
     182           0 :     if (!JS_GetProperty(rq.cx, obj, "templateName", &templateName) || !FromJSVal(rq, templateName, out.templateName))
     183           0 :         FAIL("Failed to read Entity.templateName property");
     184           0 :     if (!JS_GetProperty(rq.cx, obj, "id", &id) || !FromJSVal(rq, id, out.entityID))
     185           0 :         FAIL("Failed to read Entity.id property");
     186           0 :     if (!JS_GetProperty(rq.cx, obj, "position", &position) || !FromJSVal(rq, position, out.position))
     187           0 :         FAIL("Failed to read Entity.position property");
     188           0 :     if (!JS_GetProperty(rq.cx, obj, "rotation", &rotation) || !FromJSVal(rq, rotation, out.rotation))
     189           0 :         FAIL("Failed to read Entity.rotation property");
     190             : 
     191             :     return true;
     192           0 : }
     193             : 
     194             : ////////////////////////////////////////////////////////////////
     195             : // Primitive types:
     196             : 
     197       14337 : template<> void ToJSVal<bool>(const Request&, JS::MutableHandleValue ret, const bool& val)
     198             : {
     199       14337 :     ret.setBoolean(val);
     200       14337 : }
     201             : 
     202          12 : template<> void ToJSVal<float>(const Request&, JS::MutableHandleValue ret, const float& val)
     203             : {
     204          12 :     ret.set(JS::NumberValue(val));
     205          12 : }
     206             : 
     207           9 : template<> void ToJSVal<double>(const Request&, JS::MutableHandleValue ret, const double& val)
     208             : {
     209           9 :     ret.set(JS::NumberValue(val));
     210           9 : }
     211             : 
     212       37692 : template<> void ToJSVal<std::int32_t>(const Request&, JS::MutableHandleValue ret, const std::int32_t& val)
     213             : {
     214           0 :     ret.set(JS::NumberValue(val));
     215       37692 : }
     216             : 
     217          39 : template<> void ToJSVal<std::uint16_t>(const Request&, JS::MutableHandleValue ret, const std::uint16_t& val)
     218             : {
     219           0 :     ret.set(JS::NumberValue(val));
     220          39 : }
     221             : 
     222         228 : template<> void ToJSVal<std::uint8_t>(const Request&, JS::MutableHandleValue ret, const std::uint8_t& val)
     223             : {
     224         228 :     ret.set(JS::NumberValue(val));
     225         228 : }
     226             : 
     227        9792 : template<> void ToJSVal<std::uint32_t>(const Request&, JS::MutableHandleValue ret,
     228             :     const std::uint32_t& val)
     229             : {
     230       10044 :     ret.set(JS::NumberValue(val));
     231        9540 : }
     232             : 
     233        8672 : template<> void ToJSVal<std::wstring>(const Request& rq,  JS::MutableHandleValue ret, const std::wstring& val)
     234             : {
     235        8672 :     std::u16string utf16(val.begin(), val.end());
     236        8672 :     JS::RootedString str(rq.cx, JS_NewUCStringCopyN(rq.cx, utf16.c_str(), utf16.length()));
     237        8672 :     if (str)
     238        8672 :         ret.setString(str);
     239             :     else
     240        8672 :         ret.setUndefined();
     241       12785 : }
     242             : 
     243           0 : template<> void ToJSVal<Path>(const Request& rq,  JS::MutableHandleValue ret, const Path& val)
     244             : {
     245           0 :     ToJSVal(rq, ret, val.string());
     246           0 : }
     247             : 
     248        2057 : template<> void ToJSVal<std::string>(const Request& rq,  JS::MutableHandleValue ret, const std::string& val)
     249             : {
     250        4114 :     ToJSVal(rq, ret, static_cast<const std::wstring>(CStr(val).FromUTF8()));
     251        2057 : }
     252             : 
     253           0 : template<> void ToJSVal<const wchar_t*>(const Request& rq,  JS::MutableHandleValue ret, const wchar_t* const& val)
     254             : {
     255           0 :     ToJSVal(rq, ret, std::wstring(val));
     256           0 : }
     257             : 
     258          36 : template<> void ToJSVal<const char*>(const Request& rq,  JS::MutableHandleValue ret, const char* const& val)
     259             : {
     260          36 :     JS::RootedString str(rq.cx, JS_NewStringCopyZ(rq.cx, val));
     261          36 :     if (str)
     262          36 :         ret.setString(str);
     263             :     else
     264           0 :         ret.setUndefined();
     265          36 : }
     266             : 
     267        1452 : template<> void ToJSVal<CStrW>(const Request& rq,  JS::MutableHandleValue ret, const CStrW& val)
     268             : {
     269        1452 :     ToJSVal(rq, ret, static_cast<const std::wstring&>(val));
     270           0 : }
     271             : 
     272           0 : template<> void ToJSVal<CStr8>(const Request& rq,  JS::MutableHandleValue ret, const CStr8& val)
     273             : {
     274           0 :     ToJSVal(rq, ret, static_cast<const std::string&>(val));
     275           0 : }
     276             : 
     277             : } //namespace Script
     278             : 
     279             : ////////////////////////////////////////////////////////////////
     280             : // Compound types
     281             : // Instantiate various vector types:
     282             : 
     283         240 : JSVAL_VECTOR(int)
     284        1137 : JSVAL_VECTOR(std::uint32_t)
     285           0 : JSVAL_VECTOR(std::uint16_t)
     286           9 : JSVAL_VECTOR(std::string)
     287        1296 : JSVAL_VECTOR(std::wstring)
     288           0 : JSVAL_VECTOR(std::vector<std::wstring>)
     289           0 : JSVAL_VECTOR(CStr8)
     290           0 : JSVAL_VECTOR(CStrW)
     291           0 : JSVAL_VECTOR(std::vector<CStr8>)
     292           0 : JSVAL_VECTOR(std::vector<std::string>)
     293             : 
     294             : class IComponent;
     295           0 : template<> void Script::ToJSVal<std::vector<IComponent*>>(const Script::Request& rq,  JS::MutableHandleValue ret, const std::vector<IComponent*>& val)
     296             : {
     297           0 :     Script::ToJSVal_vector(rq, ret, val);
     298           0 : }
     299             : 
     300           0 : template<> bool Script::FromJSVal<std::vector<Entity>>(const Script::Request& rq,  JS::HandleValue v, std::vector<Entity>& out)
     301             : {
     302           0 :     return Script::FromJSVal_vector(rq, v, out);
     303             : }
     304             : 
     305             : #undef FAIL
     306             : #undef FAIL_IF_NOT

Generated by: LCOV version 1.16