2024-05-22 17:52:12 +02:00
|
|
|
/* Copyright (C) 2024 Wildfire Games.
|
2023-12-03 01:30:12 +01:00
|
|
|
* This file is part of 0 A.D.
|
2021-03-01 21:52:24 +01:00
|
|
|
*
|
2023-12-03 01:30:12 +01:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2021-03-01 21:52:24 +01: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-03 01:30:12 +01:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2021-03-01 21:52:24 +01: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-03 01:30:12 +01:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2021-03-01 21:52:24 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_FUNCTIONWRAPPER
|
|
|
|
#define INCLUDED_FUNCTIONWRAPPER
|
|
|
|
|
2021-05-13 11:43:33 +02:00
|
|
|
#include "ScriptConversions.h"
|
2021-03-01 21:52:24 +01:00
|
|
|
#include "ScriptExceptions.h"
|
2021-05-13 19:23:52 +02:00
|
|
|
#include "ScriptRequest.h"
|
2021-03-01 21:52:24 +01:00
|
|
|
|
2024-05-22 17:52:12 +02:00
|
|
|
#include <fmt/format.h>
|
2021-05-15 15:54:58 +02:00
|
|
|
#include <tuple>
|
|
|
|
#include <type_traits>
|
2024-05-22 17:52:12 +02:00
|
|
|
#include <stdexcept>
|
2023-09-02 16:20:25 +02:00
|
|
|
#include <utility>
|
2021-05-15 15:54:58 +02:00
|
|
|
|
|
|
|
class ScriptInterface;
|
|
|
|
|
2021-03-01 21:52:24 +01:00
|
|
|
/**
|
|
|
|
* This class introduces templates to conveniently wrap C++ functions in JSNative functions.
|
|
|
|
* This _is_ rather template heavy, so compilation times beware.
|
|
|
|
* The C++ code can have arbitrary arguments and arbitrary return types, so long
|
2021-05-13 11:43:33 +02:00
|
|
|
* as they can be converted to/from JS using Script::ToJSVal (FromJSVal respectively),
|
2021-03-01 21:52:24 +01:00
|
|
|
* and they are default-constructible (TODO: that can probably changed).
|
|
|
|
* (This could be a namespace, but I like being able to specify public/private).
|
|
|
|
*/
|
2023-07-07 22:12:16 +02:00
|
|
|
class ScriptFunction
|
|
|
|
{
|
2021-03-01 21:52:24 +01:00
|
|
|
private:
|
|
|
|
ScriptFunction() = delete;
|
|
|
|
ScriptFunction(const ScriptFunction&) = delete;
|
|
|
|
ScriptFunction(ScriptFunction&&) = delete;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In JS->C++ calls, types are converted using FromJSVal,
|
|
|
|
* and this requires them to be default-constructible (as that function takes an out parameter)
|
2021-03-02 17:55:22 +01:00
|
|
|
* thus constref needs to be removed when defining the tuple.
|
2021-03-01 21:52:24 +01:00
|
|
|
* Exceptions are:
|
|
|
|
* - const ScriptRequest& (as the first argument only, for implementation simplicity).
|
2021-03-02 17:55:22 +01:00
|
|
|
* - const ScriptInterface& (as the first argument only, for implementation simplicity).
|
2021-03-01 21:52:24 +01:00
|
|
|
* - JS::HandleValue
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2021-03-02 17:55:22 +01:00
|
|
|
using type_transform = std::conditional_t<
|
|
|
|
std::is_same_v<const ScriptRequest&, T> || std::is_same_v<const ScriptInterface&, T>,
|
|
|
|
T,
|
|
|
|
std::remove_const_t<typename std::remove_reference_t<T>>
|
|
|
|
>;
|
2021-03-01 21:52:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenient struct to get info on a [class] [const] function pointer.
|
|
|
|
* TODO VS19: I ran into a really weird bug with an auto specialisation on this taking function pointers.
|
|
|
|
* It'd be good to add it back once we upgrade.
|
|
|
|
*/
|
|
|
|
template <class T> struct args_info;
|
|
|
|
|
|
|
|
template<typename R, typename ...Types>
|
|
|
|
struct args_info<R(*)(Types ...)>
|
|
|
|
{
|
|
|
|
static constexpr const size_t nb_args = sizeof...(Types);
|
|
|
|
using return_type = R;
|
|
|
|
using object_type = void;
|
|
|
|
using arg_types = std::tuple<type_transform<Types>...>;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename C, typename R, typename ...Types>
|
|
|
|
struct args_info<R(C::*)(Types ...)> : public args_info<R(*)(Types ...)> { using object_type = C; };
|
|
|
|
template<typename C, typename R, typename ...Types>
|
|
|
|
struct args_info<R(C::*)(Types ...) const> : public args_info<R(C::*)(Types ...)> {};
|
|
|
|
|
2024-05-22 17:52:12 +02:00
|
|
|
struct IteratorResultError : std::runtime_error
|
|
|
|
{
|
|
|
|
IteratorResultError(const std::string& property) :
|
|
|
|
IteratorResultError{property.c_str()}
|
|
|
|
{}
|
|
|
|
IteratorResultError(const char* property) :
|
|
|
|
std::runtime_error{fmt::format("Failed to get `{}` from an `IteratorResult`.", property)}
|
|
|
|
{}
|
|
|
|
using std::runtime_error::runtime_error;
|
|
|
|
};
|
|
|
|
|
2021-03-01 21:52:24 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DoConvertFromJS takes a type, a JS argument, and converts.
|
|
|
|
* The type T must be default constructible (except for HandleValue, which is handled specially).
|
|
|
|
* (possible) TODO: this could probably be changed if FromJSVal had a different signature.
|
2023-09-02 16:20:25 +02:00
|
|
|
* @param wentOk - true if the conversion succeeded and wentOk was true before, false otherwise.
|
2021-03-01 21:52:24 +01:00
|
|
|
*/
|
|
|
|
template<size_t idx, typename T>
|
2023-09-02 16:20:25 +02:00
|
|
|
static T DoConvertFromJS(const ScriptRequest& rq, JS::CallArgs& args, bool& wentOk)
|
2021-03-01 21:52:24 +01:00
|
|
|
{
|
|
|
|
// No need to convert JS values.
|
|
|
|
if constexpr (std::is_same_v<T, JS::HandleValue>)
|
|
|
|
{
|
2021-03-02 17:55:22 +01:00
|
|
|
// Default-construct values that aren't passed by JS.
|
|
|
|
// TODO: this should perhaps be removed, as it's distinct from C++ default values and kind of tricky.
|
|
|
|
if (idx >= args.length())
|
2023-09-02 16:20:25 +02:00
|
|
|
return JS::UndefinedHandleValue;
|
2021-03-02 17:55:22 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// GCC (at least < 9) & VS17 prints warnings if arguments are not used in some constexpr branch.
|
2023-09-02 16:20:25 +02:00
|
|
|
UNUSED2(rq); UNUSED2(args); UNUSED2(wentOk);
|
|
|
|
return args[idx]; // This passes the null handle value if idx is beyond the length of args.
|
2021-03-02 17:55:22 +01:00
|
|
|
}
|
2021-03-01 21:52:24 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Default-construct values that aren't passed by JS.
|
|
|
|
// TODO: this should perhaps be removed, as it's distinct from C++ default values and kind of tricky.
|
|
|
|
if (idx >= args.length())
|
2023-09-02 16:20:25 +02:00
|
|
|
return {};
|
2021-03-01 21:52:24 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
T ret;
|
2023-09-02 16:20:25 +02:00
|
|
|
wentOk &= Script::FromJSVal<T>(rq, args[idx], ret);
|
|
|
|
return ret;
|
2021-03-01 21:52:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-09-02 16:20:25 +02:00
|
|
|
* Wrapper: calls DoConvertFromJS for each element in T.
|
2021-03-01 21:52:24 +01:00
|
|
|
*/
|
2023-09-02 16:20:25 +02:00
|
|
|
template<typename... T, size_t... idx>
|
|
|
|
static std::tuple<T...> DoConvertFromJS(std::index_sequence<idx...>, const ScriptRequest& rq,
|
|
|
|
JS::CallArgs& args, bool& wentOk)
|
2021-03-01 21:52:24 +01:00
|
|
|
{
|
2023-09-02 16:20:25 +02:00
|
|
|
return {DoConvertFromJS<idx, T>(rq, args, wentOk)...};
|
2021-03-01 21:52:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-09-02 16:20:25 +02:00
|
|
|
* ConvertFromJS is a wrapper around DoConvertFromJS, and handles specific cases for the
|
|
|
|
* first argument (ScriptRequest, ...).
|
2021-03-01 21:52:24 +01:00
|
|
|
*
|
|
|
|
* Trick: to unpack the types of the tuple as a parameter pack, we deduce them from the function signature.
|
|
|
|
* To do that, we want the tuple in the arguments, but we don't want to actually have to default-instantiate,
|
|
|
|
* so we'll pass a nullptr that's static_cast to what we want.
|
|
|
|
*/
|
|
|
|
template<typename ...Types>
|
2023-09-02 16:20:25 +02:00
|
|
|
static std::tuple<Types...> ConvertFromJS(const ScriptRequest& rq, JS::CallArgs& args, bool& wentOk,
|
|
|
|
std::tuple<Types...>*)
|
2021-03-01 21:52:24 +01:00
|
|
|
{
|
2023-09-02 16:20:25 +02:00
|
|
|
return DoConvertFromJS<Types...>(std::index_sequence_for<Types...>(), rq, args, wentOk);
|
2021-03-01 21:52:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overloads for ScriptRequest& first argument.
|
|
|
|
template<typename ...Types>
|
2023-09-02 16:20:25 +02:00
|
|
|
static std::tuple<const ScriptRequest&, Types...> ConvertFromJS(const ScriptRequest& rq,
|
|
|
|
JS::CallArgs& args, bool& wentOk, std::tuple<const ScriptRequest&, Types...>*)
|
2021-03-01 21:52:24 +01:00
|
|
|
{
|
2023-09-02 16:20:25 +02:00
|
|
|
return std::tuple_cat(std::tie(rq), DoConvertFromJS<Types...>(
|
|
|
|
std::index_sequence_for<Types...>(), rq, args, wentOk));
|
2021-03-01 21:52:24 +01:00
|
|
|
}
|
|
|
|
|
2021-03-02 17:55:22 +01:00
|
|
|
// Overloads for ScriptInterface& first argument.
|
|
|
|
template<typename ...Types>
|
2023-09-02 16:20:25 +02:00
|
|
|
static std::tuple<const ScriptInterface&, Types...> ConvertFromJS(const ScriptRequest& rq,
|
|
|
|
JS::CallArgs& args, bool& wentOk, std::tuple<const ScriptInterface&, Types...>*)
|
2021-03-02 17:55:22 +01:00
|
|
|
{
|
2023-09-02 16:20:25 +02:00
|
|
|
return std::tuple_cat(std::tie(rq.GetScriptInterface()),
|
|
|
|
DoConvertFromJS<Types...>(std::index_sequence_for<Types...>(), rq, args, wentOk));
|
2021-03-02 17:55:22 +01:00
|
|
|
}
|
|
|
|
|
2021-03-01 21:52:24 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrap std::apply for the case where we have an object method or a regular function.
|
|
|
|
*/
|
|
|
|
template <auto callable, typename T, typename tuple>
|
|
|
|
static typename args_info<decltype(callable)>::return_type call(T* object, tuple& args)
|
|
|
|
{
|
|
|
|
if constexpr(std::is_same_v<T, void>)
|
|
|
|
{
|
|
|
|
// GCC (at least < 9) & VS17 prints warnings if arguments are not used in some constexpr branch.
|
|
|
|
UNUSED2(object);
|
|
|
|
return std::apply(callable, args);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return std::apply(callable, std::tuple_cat(std::forward_as_tuple(*object), args));
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2021-05-01 16:04:53 +02:00
|
|
|
|
|
|
|
struct IgnoreResult_t {};
|
|
|
|
static inline IgnoreResult_t IgnoreResult;
|
|
|
|
|
|
|
|
/**
|
2023-09-02 16:20:25 +02:00
|
|
|
* Converts any number of arguments to a `JS::MutableHandleValueVector`.
|
|
|
|
* If `idx` is empty this function does nothing. For that case there is a
|
|
|
|
* `[[maybe_unused]]` on `argv`. GCC would issue a
|
|
|
|
* "-Wunused-but-set-parameter" warning.
|
|
|
|
* For references like `rq` this warning isn't issued.
|
2021-05-01 16:04:53 +02:00
|
|
|
*/
|
2023-09-02 16:20:25 +02:00
|
|
|
template<typename... Types, size_t... idx>
|
|
|
|
static void ToJSValVector(std::index_sequence<idx...>, const ScriptRequest& rq,
|
|
|
|
[[maybe_unused]] JS::MutableHandleValueVector argv, const Types&... params)
|
2021-05-01 16:04:53 +02:00
|
|
|
{
|
2023-09-02 16:20:25 +02:00
|
|
|
(Script::ToJSVal(rq, argv[idx], params), ...);
|
2021-05-01 16:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper around calling a JS function from C++.
|
|
|
|
* Arguments are const& to avoid lvalue/rvalue issues, and so can't be used as out-parameters.
|
|
|
|
* In particular, the problem is that Rooted are deduced as Rooted, not Handle, and so can't be copied.
|
|
|
|
* This could be worked around with more templates, but it doesn't seem particularly worth doing.
|
|
|
|
*/
|
|
|
|
template<typename R, typename ...Args>
|
|
|
|
static bool Call_(const ScriptRequest& rq, JS::HandleValue val, const char* name, R& ret, const Args&... args)
|
|
|
|
{
|
|
|
|
JS::RootedObject obj(rq.cx);
|
|
|
|
if (!JS_ValueToObject(rq.cx, val, &obj) || !obj)
|
|
|
|
return false;
|
|
|
|
|
2023-06-20 13:40:18 +02:00
|
|
|
// Fetch the property explicitly - this avoids converting the arguments if it doesn't exist.
|
|
|
|
JS::RootedValue func(rq.cx);
|
|
|
|
if (!JS_GetProperty(rq.cx, obj, name, &func) || func.isUndefined())
|
2021-05-01 16:04:53 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
JS::RootedValueVector argv(rq.cx);
|
|
|
|
ignore_result(argv.resize(sizeof...(Args)));
|
2023-09-02 16:20:25 +02:00
|
|
|
ToJSValVector(std::index_sequence_for<Args...>{}, rq, &argv, args...);
|
2021-05-01 16:04:53 +02:00
|
|
|
|
|
|
|
bool success;
|
|
|
|
if constexpr (std::is_same_v<R, JS::MutableHandleValue>)
|
2023-06-20 13:40:18 +02:00
|
|
|
success = JS_CallFunctionValue(rq.cx, obj, func, argv, ret);
|
2021-05-01 16:04:53 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
JS::RootedValue jsRet(rq.cx);
|
2023-06-20 13:40:18 +02:00
|
|
|
success = JS_CallFunctionValue(rq.cx, obj, func, argv, &jsRet);
|
2021-05-01 16:04:53 +02:00
|
|
|
if constexpr (!std::is_same_v<R, IgnoreResult_t>)
|
|
|
|
{
|
|
|
|
if (success)
|
2021-05-13 11:43:33 +02:00
|
|
|
Script::FromJSVal(rq, jsRet, ret);
|
2021-05-01 16:04:53 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
UNUSED2(ret); // VS2017 complains.
|
|
|
|
}
|
|
|
|
// Even if everything succeeded, there could be pending exceptions
|
|
|
|
return !ScriptException::CatchPending(rq) && success;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2021-03-01 21:52:24 +01:00
|
|
|
public:
|
|
|
|
template <typename T>
|
|
|
|
using ObjectGetter = T*(*)(const ScriptRequest&, JS::CallArgs&);
|
|
|
|
|
|
|
|
// TODO: the fact that this takes class and not auto is to work around an odd VS17 bug.
|
|
|
|
// It can be removed with VS19.
|
|
|
|
template <class callableType>
|
|
|
|
using GetterFor = ObjectGetter<typename args_info<callableType>::object_type>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The meat of this file. This wraps a C++ function into a JSNative,
|
|
|
|
* so that it can be called from JS and manipulated in Spidermonkey.
|
|
|
|
* Most C++ functions can be directly wrapped, so long as their arguments are
|
|
|
|
* convertible from JS::Value and their return value is convertible to JS::Value (or void)
|
2021-05-15 15:54:58 +02:00
|
|
|
* The C++ function may optionally take const ScriptRequest& or ScriptInterface& as its first argument.
|
2021-03-01 21:52:24 +01:00
|
|
|
* The function may be an object method, in which case you need to pass an appropriate getter
|
|
|
|
*
|
|
|
|
* Optimisation note: the ScriptRequest object is created even without arguments,
|
|
|
|
* as it's necessary for IsExceptionPending.
|
|
|
|
*
|
|
|
|
* @param thisGetter to get the object, if necessary.
|
|
|
|
*/
|
|
|
|
template <auto callable, GetterFor<decltype(callable)> thisGetter = nullptr>
|
|
|
|
static bool ToJSNative(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
|
|
|
using ObjType = typename args_info<decltype(callable)>::object_type;
|
|
|
|
|
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
2021-05-15 15:54:58 +02:00
|
|
|
ScriptRequest rq(cx);
|
2021-03-01 21:52:24 +01:00
|
|
|
|
2021-03-02 17:55:22 +01:00
|
|
|
// If the callable is an object method, we must specify how to fetch the object.
|
|
|
|
static_assert(std::is_same_v<typename args_info<decltype(callable)>::object_type, void> || thisGetter != nullptr,
|
|
|
|
"ScriptFunction::Register - No getter specified for object method");
|
|
|
|
|
2021-03-01 21:52:24 +01:00
|
|
|
// GCC 7 triggers spurious warnings
|
2021-03-02 16:00:33 +01:00
|
|
|
#ifdef __GNUC__
|
2021-03-01 21:52:24 +01:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Waddress"
|
2021-03-02 16:00:33 +01:00
|
|
|
#endif
|
2021-03-01 21:52:24 +01:00
|
|
|
ObjType* obj = nullptr;
|
|
|
|
if constexpr (thisGetter != nullptr)
|
|
|
|
{
|
|
|
|
obj = thisGetter(rq, args);
|
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-02 16:00:33 +01:00
|
|
|
#ifdef __GNUC__
|
2021-03-01 21:52:24 +01:00
|
|
|
#pragma GCC diagnostic pop
|
2021-03-02 16:00:33 +01:00
|
|
|
#endif
|
2021-03-01 21:52:24 +01:00
|
|
|
|
2023-09-02 16:20:25 +02:00
|
|
|
bool wentOk = true;
|
|
|
|
typename args_info<decltype(callable)>::arg_types outs = ConvertFromJS(rq, args, wentOk,
|
|
|
|
static_cast<typename args_info<decltype(callable)>::arg_types*>(nullptr));
|
|
|
|
if (!wentOk)
|
2021-03-01 21:52:24 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: error handling isn't standard, and since this can call any C++ function,
|
|
|
|
* there's no simple obvious way to deal with it.
|
|
|
|
* For now we check for pending JS exceptions, but it would probably be nicer
|
|
|
|
* to standardise on something, or perhaps provide an "errorHandler" here.
|
|
|
|
*/
|
|
|
|
if constexpr (std::is_same_v<void, typename args_info<decltype(callable)>::return_type>)
|
|
|
|
call<callable>(obj, outs);
|
|
|
|
else if constexpr (std::is_same_v<JS::Value, typename args_info<decltype(callable)>::return_type>)
|
|
|
|
args.rval().set(call<callable>(obj, outs));
|
|
|
|
else
|
2021-05-13 11:43:33 +02:00
|
|
|
Script::ToJSVal(rq, args.rval(), call<callable>(obj, outs));
|
2021-03-01 21:52:24 +01:00
|
|
|
|
|
|
|
return !ScriptException::IsPending(rq);
|
|
|
|
}
|
|
|
|
|
2021-05-01 16:04:53 +02:00
|
|
|
/**
|
|
|
|
* Call a JS function @a name, property of object @a val, with the arguments @a args.
|
|
|
|
* @a ret will be updated with the return value, if any.
|
|
|
|
* @return the success (or failure) thereof.
|
|
|
|
*/
|
|
|
|
template<typename R, typename ...Args>
|
|
|
|
static bool Call(const ScriptRequest& rq, JS::HandleValue val, const char* name, R& ret, const Args&... args)
|
|
|
|
{
|
|
|
|
return Call_(rq, val, name, ret, std::forward<const Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Specialisation for MutableHandleValue return.
|
|
|
|
template<typename ...Args>
|
|
|
|
static bool Call(const ScriptRequest& rq, JS::HandleValue val, const char* name, JS::MutableHandleValue ret, const Args&... args)
|
|
|
|
{
|
|
|
|
return Call_(rq, val, name, ret, std::forward<const Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call a JS function @a name, property of object @a val, with the arguments @a args.
|
|
|
|
* @return the success (or failure) thereof.
|
|
|
|
*/
|
|
|
|
template<typename ...Args>
|
|
|
|
static bool CallVoid(const ScriptRequest& rq, JS::HandleValue val, const char* name, const Args&... args)
|
|
|
|
{
|
|
|
|
return Call(rq, val, name, IgnoreResult, std::forward<const Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2024-05-22 17:52:12 +02:00
|
|
|
/**
|
|
|
|
* Call a JS function @a name, property of object @a val, with the argument @a args. Repeatetly
|
|
|
|
* invokes @a yieldCallback with the yielded value.
|
|
|
|
* @return the final value of the generator.
|
|
|
|
*/
|
|
|
|
template<typename Callback>
|
|
|
|
static JS::Value RunGenerator(const ScriptRequest& rq, JS::HandleValue val, const char* name,
|
|
|
|
JS::HandleValue arg, Callback yieldCallback)
|
|
|
|
{
|
|
|
|
JS::RootedValue generator{rq.cx};
|
|
|
|
if (!ScriptFunction::Call(rq, val, name, &generator, arg))
|
|
|
|
throw std::runtime_error{fmt::format("Failed to call the generator `{}`.", name)};
|
|
|
|
|
|
|
|
const auto continueGenerator = [&](const char* property, auto... args) -> JS::Value
|
|
|
|
{
|
|
|
|
JS::RootedValue iteratorResult{rq.cx};
|
|
|
|
if (!ScriptFunction::Call(rq, generator, property, &iteratorResult, args...))
|
|
|
|
throw std::runtime_error{fmt::format("Failed to call `{}`.", name)};
|
|
|
|
return iteratorResult;
|
|
|
|
};
|
|
|
|
|
|
|
|
JS::PersistentRootedValue error{rq.cx, JS::UndefinedValue()};
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
JS::RootedValue iteratorResult{rq.cx, error.isUndefined() ? continueGenerator("next") :
|
|
|
|
continueGenerator("throw", std::exchange(error, JS::UndefinedValue()))};
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
JS::RootedObject iteratorResultObject{rq.cx, &iteratorResult.toObject()};
|
|
|
|
|
|
|
|
bool done;
|
|
|
|
if (!Script::FromJSProperty(rq, iteratorResult, "done", done, true))
|
|
|
|
throw IteratorResultError{"done"};
|
|
|
|
|
|
|
|
JS::RootedValue value{rq.cx};
|
|
|
|
if (!JS_GetProperty(rq.cx, iteratorResultObject, "value", &value))
|
|
|
|
throw IteratorResultError{"value"};
|
|
|
|
|
|
|
|
if (done)
|
|
|
|
return value;
|
|
|
|
|
|
|
|
yieldCallback(value);
|
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
|
|
|
JS::RootedValue global{rq.cx, rq.globalValue()};
|
|
|
|
if (!ScriptFunction::Call(rq, global, "Error", &error, e.what()))
|
|
|
|
throw std::runtime_error{"Failed to construct `Error`."};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 21:52:24 +01:00
|
|
|
/**
|
|
|
|
* Return a function spec from a C++ function.
|
|
|
|
*/
|
2023-11-19 20:19:32 +01:00
|
|
|
template <auto callable, GetterFor<decltype(callable)> thisGetter = nullptr>
|
|
|
|
static JSFunctionSpec Wrap(const char* name,
|
|
|
|
const u16 flags = JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT)
|
2021-03-01 21:52:24 +01:00
|
|
|
{
|
2021-04-09 20:01:47 +02:00
|
|
|
return JS_FN(name, (&ToJSNative<callable, thisGetter>), args_info<decltype(callable)>::nb_args, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a JSFunction from a C++ function.
|
|
|
|
*/
|
2023-11-19 20:19:32 +01:00
|
|
|
template <auto callable, GetterFor<decltype(callable)> thisGetter = nullptr>
|
|
|
|
static JSFunction* Create(const ScriptRequest& rq, const char* name,
|
|
|
|
const u16 flags = JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT)
|
2021-04-09 20:01:47 +02:00
|
|
|
{
|
|
|
|
return JS_NewFunction(rq.cx, &ToJSNative<callable, thisGetter>, args_info<decltype(callable)>::nb_args, flags, name);
|
2021-03-01 21:52:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a function on the native scope (usually 'Engine').
|
|
|
|
*/
|
2023-11-19 20:19:32 +01:00
|
|
|
template <auto callable, GetterFor<decltype(callable)> thisGetter = nullptr>
|
|
|
|
static void Register(const ScriptRequest& rq, const char* name,
|
|
|
|
const u16 flags = JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT)
|
2021-03-01 21:52:24 +01:00
|
|
|
{
|
|
|
|
JS_DefineFunction(rq.cx, rq.nativeScope, name, &ToJSNative<callable, thisGetter>, args_info<decltype(callable)>::nb_args, flags);
|
|
|
|
}
|
|
|
|
|
2021-03-02 17:55:22 +01:00
|
|
|
/**
|
|
|
|
* Register a function on @param scope.
|
|
|
|
* Prefer the version taking ScriptRequest unless you have a good reason not to.
|
|
|
|
* @see Register
|
|
|
|
*/
|
2023-11-19 20:19:32 +01:00
|
|
|
template <auto callable, GetterFor<decltype(callable)> thisGetter = nullptr>
|
|
|
|
static void Register(JSContext* cx, JS::HandleObject scope, const char* name,
|
|
|
|
const u16 flags = JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT)
|
2021-03-02 17:55:22 +01:00
|
|
|
{
|
|
|
|
JS_DefineFunction(cx, scope, name, &ToJSNative<callable, thisGetter>, args_info<decltype(callable)>::nb_args, flags);
|
|
|
|
}
|
2021-03-01 21:52:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // INCLUDED_FUNCTIONWRAPPER
|