Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/scriptinterface/FunctionWrapper.h
/* Copyright (C) 2021 Wildfire Games. | /* Copyright (C) 2023 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
▲ Show 20 Lines • Show All 219 Lines • ▼ Show 20 Lines | private: | ||||
*/ | */ | ||||
template<typename R, typename ...Args> | template<typename R, typename ...Args> | ||||
static bool Call_(const ScriptRequest& rq, JS::HandleValue val, const char* name, R& ret, const Args&... args) | static bool Call_(const ScriptRequest& rq, JS::HandleValue val, const char* name, R& ret, const Args&... args) | ||||
{ | { | ||||
JS::RootedObject obj(rq.cx); | JS::RootedObject obj(rq.cx); | ||||
if (!JS_ValueToObject(rq.cx, val, &obj) || !obj) | if (!JS_ValueToObject(rq.cx, val, &obj) || !obj) | ||||
return false; | return false; | ||||
// Check that the named function actually exists, to avoid ugly JS error reports | // Fetch the property explicitly - this avoids converting the arguments if it doesn't exist. | ||||
// when calling an undefined value | JS::RootedValue func(rq.cx); | ||||
bool found; | if (!JS_GetProperty(rq.cx, obj, name, &func) || func.isUndefined()) | ||||
if (!JS_HasProperty(rq.cx, obj, name, &found) || !found) | |||||
return false; | return false; | ||||
JS::RootedValueVector argv(rq.cx); | JS::RootedValueVector argv(rq.cx); | ||||
ignore_result(argv.resize(sizeof...(Args))); | ignore_result(argv.resize(sizeof...(Args))); | ||||
AssignOrToJSValHelper<0>(rq, &argv, args...); | AssignOrToJSValHelper<0>(rq, &argv, args...); | ||||
bool success; | bool success; | ||||
if constexpr (std::is_same_v<R, JS::MutableHandleValue>) | if constexpr (std::is_same_v<R, JS::MutableHandleValue>) | ||||
success = JS_CallFunctionName(rq.cx, obj, name, argv, ret); | success = JS_CallFunctionValue(rq.cx, obj, func, argv, ret); | ||||
else | else | ||||
{ | { | ||||
JS::RootedValue jsRet(rq.cx); | JS::RootedValue jsRet(rq.cx); | ||||
success = JS_CallFunctionName(rq.cx, obj, name, argv, &jsRet); | success = JS_CallFunctionValue(rq.cx, obj, func, argv, &jsRet); | ||||
if constexpr (!std::is_same_v<R, IgnoreResult_t>) | if constexpr (!std::is_same_v<R, IgnoreResult_t>) | ||||
{ | { | ||||
if (success) | if (success) | ||||
Script::FromJSVal(rq, jsRet, ret); | Script::FromJSVal(rq, jsRet, ret); | ||||
} | } | ||||
else | else | ||||
UNUSED2(ret); // VS2017 complains. | UNUSED2(ret); // VS2017 complains. | ||||
} | } | ||||
▲ Show 20 Lines • Show All 145 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator