Changeset View
Changeset View
Standalone View
Standalone View
source/gui/Scripting/JSInterface_IGUIObject.cpp
/* Copyright (C) 2020 Wildfire Games. | /* Copyright (C) 2020 Wildfire Games. | ||||
Stan: 2020 | |||||
* 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 All 9 Lines | |||||
#include "JSInterface_IGUIObject.h" | #include "JSInterface_IGUIObject.h" | ||||
#include <type_traits> | #include <type_traits> | ||||
#include "gui/CGUI.h" | #include "gui/CGUI.h" | ||||
#include "gui/CGUISetting.h" | #include "gui/CGUISetting.h" | ||||
#include "gui/ObjectBases/IGUIObject.h" | #include "gui/ObjectBases/IGUIObject.h" | ||||
#include "gui/ObjectTypes/CList.h" | |||||
#include "gui/ObjectTypes/CText.h" | #include "gui/ObjectTypes/CText.h" | ||||
#include "scriptinterface/ScriptExtraHeaders.h" | #include "scriptinterface/ScriptExtraHeaders.h" | ||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
/** | /** | ||||
* Convenient struct to get info on a [const] function pointer. | * Convenient struct to get info on a [const] function pointer. | ||||
*/ | */ | ||||
template<typename ptr> | template<typename ptr> | ||||
struct args_info; | struct args_info; | ||||
template<typename C, typename R, typename ...Types> | template<typename C, typename R, typename ...Types> | ||||
struct args_info<R(C::*)(Types ...)> | struct args_info<R(C::*)(Types ...)> | ||||
{ | { | ||||
static const size_t nb_args = sizeof...(Types); | static const size_t nb_args = sizeof...(Types); | ||||
using return_type = R; | using return_type = R; | ||||
using object_type = C; | using object_type = C; | ||||
using args = std::tuple<typename std::remove_const<typename std::remove_reference<Types>::type>::type...>; | using args = std::tuple<typename std::remove_const<typename std::remove_reference<Types>::type>::type...>; | ||||
Done Inline ActionsCome C++14, I think these all become: wraitii: Come C++14, I think these all become:
`SetupHandler<&CText::GetTextSize>(scriptInterface. | |||||
Not Done Inline ActionsYou should put this comment with the future code instead of the vague TODO ? Itms: You should put this comment with the future code instead of the vague TODO ? | |||||
}; | }; | ||||
// TODO: would be nice to find a way around the duplication here. | // TODO: would be nice to find a way around the duplication here. | ||||
template<typename C, typename R, typename ...Types> | template<typename C, typename R, typename ...Types> | ||||
struct args_info<R(C::*)(Types ...) const> | struct args_info<R(C::*)(Types ...) const> | ||||
{ | { | ||||
static const size_t nb_args = sizeof...(Types); | static const size_t nb_args = sizeof...(Types); | ||||
using return_type = R; | using return_type = R; | ||||
Show All 21 Lines | |||||
JSI_GUI::TextObjectFactory::TextObjectFactory(ScriptInterface& scriptInterface) : JSI_GUI::GUIObjectFactory(scriptInterface) | JSI_GUI::TextObjectFactory::TextObjectFactory(ScriptInterface& scriptInterface) : JSI_GUI::GUIObjectFactory(scriptInterface) | ||||
{ | { | ||||
JSAutoRequest rq(scriptInterface.GetContext()); | JSAutoRequest rq(scriptInterface.GetContext()); | ||||
SetupHandler(&CText::GetTextSize, "getTextSize"); | SetupHandler(&CText::GetTextSize, "getTextSize"); | ||||
} | } | ||||
JSI_GUI::ListObjectFactory::ListObjectFactory(ScriptInterface& scriptInterface) : JSI_GUI::GUIObjectFactory(scriptInterface) | |||||
{ | |||||
JSAutoRequest rq(scriptInterface.GetContext()); | |||||
SetupHandler(&CList::AddItem, "pushItem"); | |||||
} | |||||
#undef SetupHandler | #undef SetupHandler | ||||
/** | /** | ||||
* Based on https://stackoverflow.com/a/32223343 | * Based on https://stackoverflow.com/a/32223343 | ||||
* make_index_sequence is not defined in C++11... Only C++14. | * make_index_sequence is not defined in C++11... Only C++14. | ||||
*/ | */ | ||||
template <size_t... Ints> | template <size_t... Ints> | ||||
struct index_sequence | struct index_sequence | ||||
▲ Show 20 Lines • Show All 79 Lines • ▼ Show 20 Lines | bool JSToCppCall(T* object, JSContext* cx, JS::CallArgs& args) | ||||
// We could either try-catch and throw exceptions, | // We could either try-catch and throw exceptions, | ||||
// or come C++17 return an std::optional/maybe or some kind of [bool, val] structured binding. | // or come C++17 return an std::optional/maybe or some kind of [bool, val] structured binding. | ||||
using returns_void = std::is_same<typename args_info<funcPtr>::return_type, void>; | using returns_void = std::is_same<typename args_info<funcPtr>::return_type, void>; | ||||
call<funcPtr, callable, T>(object, scriptInterface, args, outs, returns_void{}, make_index_sequence<N>{}); | call<funcPtr, callable, T>(object, scriptInterface, args, outs, returns_void{}, make_index_sequence<N>{}); | ||||
return true; | return true; | ||||
} | } | ||||
// TODO: this can get rewritten as <auto> and deduced come c++14 | // TODO: this can get rewritten as <auto> and deduced come c++14 | ||||
template <typename objType, typename funcPtr, funcPtr callable> | template <typename objType, typename funcPtr, funcPtr callable> | ||||
Done Inline Actions#include <string> Stan: ```lang=cpp
#include <string>
``` | |||||
bool JSI_GUI::GUIObjectFactory::scriptMethod(JSContext* cx, unsigned argc, JS::Value* vp) | bool JSI_GUI::GUIObjectFactory::scriptMethod(JSContext* cx, unsigned argc, JS::Value* vp) | ||||
{ | { | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
JS::CallArgs args = JS::CallArgsFromVp(argc, vp); | JS::CallArgs args = JS::CallArgsFromVp(argc, vp); | ||||
static_assert(std::is_same<objType, typename args_info<funcPtr>::object_type>::value, | static_assert(std::is_same<objType, typename args_info<funcPtr>::object_type>::value, | ||||
"The called method is not defined on the factory's cppType. You most likely forgot to define 'using cppType = ...'"); | "The called method is not defined on the factory's cppType. You most likely forgot to define 'using cppType = ...'"); | ||||
objType* thisObj = static_cast<objType*>(JS_GetPrivate(args.thisv().toObjectOrNull())); | objType* thisObj = static_cast<objType*>(JS_GetPrivate(args.thisv().toObjectOrNull())); | ||||
▲ Show 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | bool JSI_GUI::GUIProxy::get(JSContext* cx, JS::HandleObject proxy, JS::HandleValue UNUSED(receiver), JS::HandleId id, JS::MutableHandleValue vp) const | ||||
LOGERROR("Property '%s' does not exist!", propName.c_str()); | LOGERROR("Property '%s' does not exist!", propName.c_str()); | ||||
return false; | return false; | ||||
} | } | ||||
bool JSI_GUI::GUIProxy::set(JSContext* cx, JS::HandleObject proxy, JS::HandleId id, JS::HandleValue vp, | bool JSI_GUI::GUIProxy::set(JSContext* cx, JS::HandleObject proxy, JS::HandleId id, JS::HandleValue vp, | ||||
JS::HandleValue UNUSED(receiver), JS::ObjectOpResult& result) const | JS::HandleValue UNUSED(receiver), JS::ObjectOpResult& result) const | ||||
{ | { | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
IGUIObject* e = static_cast<IGUIObject*>(JS_GetPrivate(proxy.get())); | IGUIObject* e = static_cast<IGUIObject*>(JS_GetPrivate(proxy.get())); | ||||
Not Done Inline ActionsI think you should add a new static method to ScriptInterface for the new use. The new calls to the proxy private and the static casts are a bit ugly. Itms: I think you should add a new static method to ScriptInterface for the new use. The new calls to… | |||||
if (!e) | if (!e) | ||||
return result.fail(JSMSG_NOT_NONNULL_OBJECT); | return result.fail(JSMSG_NOT_NONNULL_OBJECT); | ||||
JS::RootedValue idval(cx); | JS::RootedValue idval(cx); | ||||
if (!JS_IdToValue(cx, id, &idval)) | if (!JS_IdToValue(cx, id, &idval)) | ||||
return result.fail(JSMSG_NOT_NONNULL_OBJECT); | return result.fail(JSMSG_NOT_NONNULL_OBJECT); | ||||
std::string propName; | std::string propName; | ||||
▲ Show 20 Lines • Show All 65 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
2020