Changeset View
Changeset View
Standalone View
Standalone View
source/gui/Scripting/JSInterface_GUIProxy_impl.h
Show All 24 Lines | |||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include "scriptinterface/FunctionWrapper.h" | #include "scriptinterface/FunctionWrapper.h" | ||||
#include "scriptinterface/ScriptExtraHeaders.h" | #include "scriptinterface/ScriptExtraHeaders.h" | ||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include <string> | #include <string> | ||||
template <typename T> | template <typename T> | ||||
JSClass& JSI_GUIProxy<T>::ClassDefinition() | |||||
{ | |||||
static JSClass c = PROXY_CLASS_DEF("GUIObjectProxy", JSCLASS_HAS_CACHED_PROTO(JSProto_Proxy) | JSCLASS_HAS_RESERVED_SLOTS(1)); | |||||
return c; | |||||
} | |||||
template <typename T> | |||||
JSI_GUIProxy<T>& JSI_GUIProxy<T>::Singleton() | JSI_GUIProxy<T>& JSI_GUIProxy<T>::Singleton() | ||||
{ | { | ||||
static JSI_GUIProxy<T> s; | static JSI_GUIProxy<T> s; | ||||
return s; | return s; | ||||
} | } | ||||
// Call this for every specialised type. You will need to override IGUIObject::CreateJSObject() in your class interface. | // Call this for every specialised type. You will need to override IGUIObject::CreateJSObject() in your class interface. | ||||
#define DECLARE_GUIPROXY(Type) \ | #define DECLARE_GUIPROXY(Type) \ | ||||
void Type::CreateJSObject() \ | void Type::CreateJSObject() \ | ||||
{ \ | { \ | ||||
ScriptRequest rq(m_pGUI.GetScriptInterface()); \ | ScriptRequest rq(m_pGUI.GetScriptInterface()); \ | ||||
using ProxyHandler = JSI_GUIProxy<std::remove_pointer_t<decltype(this)>>; \ | using ProxyHandler = JSI_GUIProxy<std::remove_pointer_t<decltype(this)>>; \ | ||||
m_JSObject = ProxyHandler::CreateJSObject(rq, this, GetGUI().GetProxyData(&ProxyHandler::Singleton())); \ | m_JSObject = ProxyHandler::CreateJSObject(rq, this, GetGUI().GetProxyData(&ProxyHandler::Singleton())); \ | ||||
} \ | } \ | ||||
template class JSI_GUIProxy<Type>; | template class JSI_GUIProxy<Type>; | ||||
// Use a common namespace to avoid duplicating the symbols un-necessarily. | // Use a common namespace to avoid duplicating the symbols un-necessarily. | ||||
namespace JSInterface_GUIProxy | namespace JSInterface_GUIProxy | ||||
{ | { | ||||
template<typename T> | // All proxy objects share a class definition. | ||||
inline T* GuiObjectGetter(const ScriptRequest&, JS::CallArgs& args) | JSClass& ClassDefinition() | ||||
{ | { | ||||
return IGUIProxyObject::FromPrivateSlot<T>(args.thisv().toObjectOrNull()); | static JSClass c = PROXY_CLASS_DEF("GUIObjectProxy", JSCLASS_HAS_CACHED_PROTO(JSProto_Proxy) | JSCLASS_HAS_RESERVED_SLOTS(1)); | ||||
return c; | |||||
} | } | ||||
// Default implementation of the cache via unordered_map | // Default implementation of the cache via unordered_map | ||||
class MapCache : public GUIProxyProps | class MapCache : public GUIProxyProps | ||||
{ | { | ||||
public: | public: | ||||
virtual ~MapCache() {}; | virtual ~MapCache() {}; | ||||
Show All 13 Lines | virtual bool setFunction(const ScriptRequest& rq, const std::string& name, JSFunction* function) override | ||||
return true; | return true; | ||||
} | } | ||||
protected: | protected: | ||||
std::unordered_map<std::string, JS::PersistentRootedObject> m_Functions; | std::unordered_map<std::string, JS::PersistentRootedObject> m_Functions; | ||||
}; | }; | ||||
} | } | ||||
template<> | |||||
IGUIObject* IGUIProxyObject::FromPrivateSlot<IGUIObject>(JSObject* obj) | |||||
{ | |||||
if (!obj) | |||||
return nullptr; | |||||
if (JS_GetClass(obj) != &JSInterface_GUIProxy::ClassDefinition()) | |||||
return nullptr; | |||||
return UnsafeFromPrivateSlot<IGUIObject>(obj); | |||||
} | |||||
// The default propcache is a MapCache. | // The default propcache is a MapCache. | ||||
template<typename T> | template<typename T> | ||||
struct JSI_GUIProxy<T>::PropCache | struct JSI_GUIProxy<T>::PropCache | ||||
{ | { | ||||
using type = JSInterface_GUIProxy::MapCache; | using type = JSInterface_GUIProxy::MapCache; | ||||
}; | }; | ||||
template <typename T> | template <typename T> | ||||
T* JSI_GUIProxy<T>::FromPrivateSlot(const ScriptRequest&, JS::CallArgs& args) | |||||
{ | |||||
// Call the unsafe version - this is only ever called from actual proxy objects. | |||||
return IGUIProxyObject::UnsafeFromPrivateSlot<T>(args.thisv().toObjectOrNull()); | |||||
} | |||||
template <typename T> | |||||
bool JSI_GUIProxy<T>::PropGetter(JS::HandleObject proxy, const std::string& propName, JS::MutableHandleValue vp) const | bool JSI_GUIProxy<T>::PropGetter(JS::HandleObject proxy, const std::string& propName, JS::MutableHandleValue vp) const | ||||
{ | { | ||||
using PropertyCache = typename PropCache::type; | using PropertyCache = typename PropCache::type; | ||||
// Since we know at compile time what the type actually is, avoid the virtual call. | // Since we know at compile time what the type actually is, avoid the virtual call. | ||||
const PropertyCache* data = static_cast<const PropertyCache*>(static_cast<const GUIProxyProps*>(js::GetProxyReservedSlot(proxy, 0).toPrivate())); | const PropertyCache* data = static_cast<const PropertyCache*>(static_cast<const GUIProxyProps*>(js::GetProxyReservedSlot(proxy, 0).toPrivate())); | ||||
if (data->has(propName)) | if (data->has(propName)) | ||||
{ | { | ||||
vp.setObjectOrNull(data->get(propName)); | vp.setObjectOrNull(data->get(propName)); | ||||
Show All 17 Lines | if constexpr (!std::is_same_v<T, IGUIObject>) | ||||
CreateFunctions(rq, data); | CreateFunctions(rq, data); | ||||
return { &Singleton(), data }; | return { &Singleton(), data }; | ||||
} | } | ||||
template<typename T> | template<typename T> | ||||
template<auto callable> | template<auto callable> | ||||
void JSI_GUIProxy<T>::CreateFunction(const ScriptRequest& rq, GUIProxyProps* cache, const std::string& name) | void JSI_GUIProxy<T>::CreateFunction(const ScriptRequest& rq, GUIProxyProps* cache, const std::string& name) | ||||
{ | { | ||||
cache->setFunction(rq, name, ScriptFunction::Create<callable, JSInterface_GUIProxy::GuiObjectGetter<T>>(rq, name.c_str())); | cache->setFunction(rq, name, ScriptFunction::Create<callable, FromPrivateSlot>(rq, name.c_str())); | ||||
} | } | ||||
template<typename T> | template<typename T> | ||||
std::unique_ptr<IGUIProxyObject> JSI_GUIProxy<T>::CreateJSObject(const ScriptRequest& rq, T* ptr, GUIProxyProps* dataPtr) | std::unique_ptr<IGUIProxyObject> JSI_GUIProxy<T>::CreateJSObject(const ScriptRequest& rq, T* ptr, GUIProxyProps* dataPtr) | ||||
{ | { | ||||
js::ProxyOptions options; | js::ProxyOptions options; | ||||
options.setClass(&ClassDefinition()); | options.setClass(&JSInterface_GUIProxy::ClassDefinition()); | ||||
auto ret = std::make_unique<IGUIProxyObject>(); | auto ret = std::make_unique<IGUIProxyObject>(); | ||||
ret->m_Ptr = static_cast<IGUIObject*>(ptr); | ret->m_Ptr = static_cast<IGUIObject*>(ptr); | ||||
JS::RootedValue cppObj(rq.cx), data(rq.cx); | JS::RootedValue cppObj(rq.cx), data(rq.cx); | ||||
cppObj.get().setPrivate(ret->m_Ptr); | cppObj.get().setPrivate(ret->m_Ptr); | ||||
data.get().setPrivate(static_cast<void*>(dataPtr)); | data.get().setPrivate(static_cast<void*>(dataPtr)); | ||||
ret->m_Object.init(rq.cx, js::NewProxyObject(rq.cx, &Singleton(), cppObj, nullptr, options)); | ret->m_Object.init(rq.cx, js::NewProxyObject(rq.cx, &Singleton(), cppObj, nullptr, options)); | ||||
▲ Show 20 Lines • Show All 161 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator