2025-06-03 23:10:15 -07:00
|
|
|
/* Copyright (C) 2025 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2017-01-06 03:14:03 -08:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2017-01-06 03:14:03 -08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2017-01-06 03:14:03 -08:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2017-01-06 03:14:03 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef INCLUDED_SCRIPTCONVERSIONS
|
|
|
|
|
#define INCLUDED_SCRIPTCONVERSIONS
|
|
|
|
|
|
2025-06-19 15:06:41 -07:00
|
|
|
#include "lib/debug.h"
|
2021-05-13 02:43:33 -07:00
|
|
|
#include "ScriptRequest.h"
|
|
|
|
|
#include "ScriptExceptions.h"
|
|
|
|
|
#include "ScriptExtraHeaders.h" // for typed arrays
|
2017-01-06 03:14:03 -08:00
|
|
|
|
|
|
|
|
#include <limits>
|
2025-06-18 10:53:49 -07:00
|
|
|
#include <optional>
|
|
|
|
|
#include <type_traits>
|
2021-05-15 07:50:24 -07:00
|
|
|
#include <vector>
|
2017-01-06 03:14:03 -08:00
|
|
|
|
2021-05-13 02:43:33 -07:00
|
|
|
namespace Script
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Convert a JS::Value to a C++ type. (This might trigger GC.)
|
|
|
|
|
*/
|
|
|
|
|
template<typename T> bool FromJSVal(const ScriptRequest& rq, const JS::HandleValue val, T& ret);
|
|
|
|
|
|
2025-06-18 10:53:49 -07:00
|
|
|
template<typename T>
|
|
|
|
|
bool FromJSVal(const ScriptRequest& rq, JS::HandleValue v, std::optional<T>& out)
|
|
|
|
|
{
|
|
|
|
|
if (v.isNullOrUndefined())
|
|
|
|
|
{
|
|
|
|
|
out = std::nullopt;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
T value;
|
|
|
|
|
if (!FromJSVal(rq, v, value))
|
|
|
|
|
return false;
|
|
|
|
|
out = std::move(value);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 02:43:33 -07:00
|
|
|
/**
|
|
|
|
|
* Convert a C++ type to a JS::Value. (This might trigger GC. The return
|
|
|
|
|
* value must be rooted if you don't want it to be collected.)
|
|
|
|
|
* NOTE: We are passing the JS::Value by reference instead of returning it by value.
|
|
|
|
|
* The reason is a memory corruption problem that appears to be caused by a bug in Visual Studio.
|
|
|
|
|
* Details here: http://www.wildfiregames.com/forum/index.php?showtopic=17289&p=285921
|
|
|
|
|
*/
|
|
|
|
|
template<typename T> void ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret, T const& val);
|
|
|
|
|
|
|
|
|
|
template<>
|
2025-06-03 23:10:15 -07:00
|
|
|
inline void ToJSVal<JS::PersistentRootedValue>(const ScriptRequest&, JS::MutableHandleValue handle,
|
|
|
|
|
const JS::PersistentRootedValue& a)
|
2021-05-13 02:43:33 -07:00
|
|
|
{
|
|
|
|
|
handle.set(a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
2025-06-03 23:10:15 -07:00
|
|
|
inline void ToJSVal<JS::Heap<JS::Value> >(const ScriptRequest&, JS::MutableHandleValue handle,
|
|
|
|
|
const JS::Heap<JS::Value>& a)
|
2021-05-13 02:43:33 -07:00
|
|
|
{
|
|
|
|
|
handle.set(a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
2025-06-03 23:10:15 -07:00
|
|
|
inline void ToJSVal<JS::RootedValue>(const ScriptRequest&, JS::MutableHandleValue handle,
|
|
|
|
|
const JS::RootedValue& a)
|
2021-05-13 02:43:33 -07:00
|
|
|
{
|
|
|
|
|
handle.set(a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
2025-06-03 23:10:15 -07:00
|
|
|
inline void ToJSVal<JS::HandleValue>(const ScriptRequest&, JS::MutableHandleValue handle,
|
|
|
|
|
const JS::HandleValue& a)
|
2021-05-13 02:43:33 -07:00
|
|
|
{
|
|
|
|
|
handle.set(a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert a named property of an object to a C++ type.
|
|
|
|
|
*/
|
|
|
|
|
template<typename T> inline bool FromJSProperty(const ScriptRequest& rq, const JS::HandleValue val, const char* name, T& ret, bool strict = false)
|
|
|
|
|
{
|
|
|
|
|
if (!val.isObject())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
JS::RootedObject obj(rq.cx, &val.toObject());
|
|
|
|
|
|
|
|
|
|
bool hasProperty;
|
|
|
|
|
if (!JS_HasProperty(rq.cx, obj, name, &hasProperty) || !hasProperty)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
JS::RootedValue value(rq.cx);
|
|
|
|
|
if (!JS_GetProperty(rq.cx, obj, name, &value))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (strict && value.isNull())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return FromJSVal(rq, value, ret);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T> inline void ToJSVal_vector(const ScriptRequest& rq, JS::MutableHandleValue ret, const std::vector<T>& val)
|
2017-01-06 03:14:03 -08:00
|
|
|
{
|
2020-12-06 06:03:02 -08:00
|
|
|
JS::RootedObject obj(rq.cx, JS::NewArrayObject(rq.cx, 0));
|
2017-01-06 03:14:03 -08:00
|
|
|
if (!obj)
|
|
|
|
|
{
|
|
|
|
|
ret.setUndefined();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ENSURE(val.size() <= std::numeric_limits<u32>::max());
|
|
|
|
|
for (u32 i = 0; i < val.size(); ++i)
|
|
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue el(rq.cx);
|
2021-05-13 02:43:33 -07:00
|
|
|
Script::ToJSVal<T>(rq, &el, val[i]);
|
2020-11-13 05:18:22 -08:00
|
|
|
JS_SetElement(rq.cx, obj, i, el);
|
2017-01-06 03:14:03 -08:00
|
|
|
}
|
|
|
|
|
ret.setObject(*obj);
|
|
|
|
|
}
|
|
|
|
|
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
#define FAIL(msg) STMT(ScriptException::Raise(rq, msg); return false)
|
2017-01-06 03:14:03 -08:00
|
|
|
|
2021-05-13 02:43:33 -07:00
|
|
|
template<typename T> inline bool FromJSVal_vector(const ScriptRequest& rq, JS::HandleValue v, std::vector<T>& out)
|
2017-01-06 03:14:03 -08:00
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedObject obj(rq.cx);
|
2017-01-06 03:14:03 -08:00
|
|
|
if (!v.isObject())
|
|
|
|
|
FAIL("Argument must be an array");
|
|
|
|
|
|
Upgrade SpiderMonkey to version 45.0.2, refs #4893.
- Various build changes, in particular NSPR is not needed on Unix
anymore
- Add js/Initialization.h to source/scriptinterface/ScriptEngine.h
- Use nullptr instead of JS::NullPtr(), see
https://bugzilla.mozilla.org/show_bug.cgi?id=1164602
- Remove `JS::RuntimeOptionsRef.varObjFix`, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1171177
- Remove uses of `AutoIdArray`, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1191529
- `JS_InternUCStringN` has been renamed, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1178581
- `JS::Evaluate` now takes scope chains explicitly, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1097987
- Array functions (such as `JS_IsArrayObject`) are fallible and output
to params, see https://bugzilla.mozilla.org/show_bug.cgi?id=f3d35d8
- Remove `JSCLASS_CACHED_PROTO_WIDTH` workaround in our code, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1236373
- Remove compile'n go (`setCompileAndGo`) and replace it by
`setIsRunOnce` which will become the default in the future, see
https://bugzilla.mozilla.org/show_bug.cgi?id=679939
- Mark shared memory in direct access operations
(`JS_GetUint16ArrayData` and `JS_GetUint8ArrayData`), see
https://bugzilla.mozilla.org/show_bug.cgi?id=1176214
- Use new `JS::ObjectOpResult`, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1113369
Thanks to wraitii, elexis, Krinkle and historic_bruno for contributions
and comments, and to gentz, madpilot, s0600204 and Stan for testing and
indirect contributions.
Differential Revision: https://code.wildfiregames.com/D1510
This was SVN commit r22627.
2019-08-07 15:37:43 -07:00
|
|
|
bool isArray;
|
2017-01-06 03:14:03 -08:00
|
|
|
obj = &v.toObject();
|
2020-12-06 06:03:02 -08:00
|
|
|
if ((!JS::IsArrayObject(rq.cx, obj, &isArray) || !isArray) && !JS_IsTypedArrayObject(obj))
|
2017-01-06 03:14:03 -08:00
|
|
|
FAIL("Argument must be an array");
|
|
|
|
|
|
|
|
|
|
u32 length;
|
2020-12-06 06:03:02 -08:00
|
|
|
if (!JS::GetArrayLength(rq.cx, obj, &length))
|
2017-01-06 03:14:03 -08:00
|
|
|
FAIL("Failed to get array length");
|
|
|
|
|
|
2021-05-06 01:22:37 -07:00
|
|
|
out.clear();
|
2017-01-06 03:14:03 -08:00
|
|
|
out.reserve(length);
|
|
|
|
|
for (u32 i = 0; i < length; ++i)
|
|
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue el(rq.cx);
|
|
|
|
|
if (!JS_GetElement(rq.cx, obj, i, &el))
|
2017-01-06 03:14:03 -08:00
|
|
|
FAIL("Failed to read array element");
|
2025-03-03 12:23:56 -08:00
|
|
|
T el2{};
|
2021-05-13 02:43:33 -07:00
|
|
|
if (!Script::FromJSVal<T>(rq, el, el2))
|
2017-01-06 03:14:03 -08:00
|
|
|
return false;
|
|
|
|
|
out.push_back(el2);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#undef FAIL
|
|
|
|
|
|
|
|
|
|
#define JSVAL_VECTOR(T) \
|
2021-05-13 02:43:33 -07:00
|
|
|
template<> void Script::ToJSVal<std::vector<T> >(const ScriptRequest& rq, JS::MutableHandleValue ret, const std::vector<T>& val) \
|
2017-01-06 03:14:03 -08:00
|
|
|
{ \
|
2020-11-13 05:18:22 -08:00
|
|
|
ToJSVal_vector(rq, ret, val); \
|
2017-01-06 03:14:03 -08:00
|
|
|
} \
|
2021-05-13 02:43:33 -07:00
|
|
|
template<> bool Script::FromJSVal<std::vector<T> >(const ScriptRequest& rq, JS::HandleValue v, std::vector<T>& out) \
|
2017-01-06 03:14:03 -08:00
|
|
|
{ \
|
2020-11-13 05:18:22 -08:00
|
|
|
return FromJSVal_vector(rq, v, out); \
|
2017-01-06 03:14:03 -08:00
|
|
|
}
|
|
|
|
|
|
2021-05-13 02:43:33 -07:00
|
|
|
} // namespace Script
|
2017-04-16 16:59:20 -07:00
|
|
|
|
2017-01-06 03:14:03 -08:00
|
|
|
#endif //INCLUDED_SCRIPTCONVERSIONS
|