Reduce duplication in JSI GUI objects implementation.

- Store the functions in an unordered_map. A no-container implementation
is doable but likely not worth the added code complexity, maybe with
C++20
- Move more things into the _impl.h
- Clear out the un-necessary friends declaration in the specific types
by moving the functions to the public interface, which makes sense.
- Fix a memory leak (JS::PersistentRootedObject weren't deleted).

This doesn't change what one needs to do to add a new type, but it does
reduce the actual code that's necessary, and makes it less error prone.

Differential Revision: https://code.wildfiregames.com/D3214
This was SVN commit r24384.
This commit is contained in:
wraitii
2020-12-13 15:06:18 +00:00
parent 876f6d5e50
commit 1b67a079fb
15 changed files with 202 additions and 277 deletions
@@ -17,59 +17,19 @@
#include "precompiled.h"
#include "JSInterface_GUIProxy.h"
#include "gui/CGUI.h"
#include "gui/CGUISetting.h"
#include "gui/ObjectBases/IGUIObject.h"
#include "gui/ObjectTypes/CText.h"
#include "ps/CLogger.h"
#include "scriptinterface/ScriptExtraHeaders.h"
#include "scriptinterface/ScriptInterface.h"
#include <string>
// Include the definition of the generic templates.
#include "JSInterface_GUIProxy_impl.h"
namespace
{
struct SData
{
JS::PersistentRootedObject m_ToString;
JS::PersistentRootedObject m_Focus;
JS::PersistentRootedObject m_Blur;
JS::PersistentRootedObject m_GetComputedSize;
};
}
using GUIObjectType = IGUIObject;
template <>
bool JSI_GUIProxy<IGUIObject>::FuncGetter(JS::HandleObject proxy, const std::string& propName, JS::MutableHandleValue vp) const
template<>
void JSI_GUIProxy<GUIObjectType>::CreateFunctions(const ScriptRequest& rq, GUIProxyProps* cache)
{
const SData& data = *static_cast<const SData*>(js::GetProxyReservedSlot(proxy, 0).toPrivate());
if (propName == "toString")
return vp.setObjectOrNull(data.m_ToString), true;
if (propName == "focus")
return vp.setObjectOrNull(data.m_Focus), true;
if (propName == "blur")
return vp.setObjectOrNull(data.m_Blur), true;
if (propName == "getComputedSize")
return vp.setObjectOrNull(data.m_GetComputedSize), true;
return false;
}
template <>
std::pair<const js::BaseProxyHandler*, void*> JSI_GUIProxy<IGUIObject>::CreateData(ScriptInterface& scriptInterface)
{
SData* data = new SData();
ScriptRequest rq(scriptInterface);
#define func(class, func) &apply_to<IGUIObject, class, &class::func>
data->m_ToString.init(rq.cx, JS_GetFunctionObject(JS_NewFunction(rq.cx, func(IGUIObject, toString), 0, 0, "toString")));
data->m_Focus.init(rq.cx, JS_GetFunctionObject(JS_NewFunction(rq.cx, func(IGUIObject, focus), 0, 0, "focus")));
data->m_Blur.init(rq.cx, JS_GetFunctionObject(JS_NewFunction(rq.cx, func(IGUIObject, blur), 0, 0, "blur")));
data->m_GetComputedSize.init(rq.cx, JS_GetFunctionObject(JS_NewFunction(rq.cx, func(IGUIObject, getComputedSize), 0, 0, "getComputedSize")));
#define func(class, func) &JSInterface_GUIProxy::apply_to<GUIObjectType, class, &class::func>
cache->setFunction(rq, "toString", func(IGUIObject, toString), 0);
cache->setFunction(rq, "focus", func(IGUIObject, focus), 0);
cache->setFunction(rq, "blur", func(IGUIObject, blur), 0);
cache->setFunction(rq, "getComputedSize", func(IGUIObject, getComputedSize), 0);
#undef func
return { &Singleton(), data };
}
template class JSI_GUIProxy<IGUIObject>;
template class JSI_GUIProxy<GUIObjectType>;