2026-02-02 09:24:57 -08:00
|
|
|
/* Copyright (C) 2026 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2020-11-21 09:49:06 -08:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2020-11-21 09:49:06 -08: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-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2020-11-21 09:49:06 -08: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-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2020-11-21 09:49:06 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// This file is included directly into actual implementation files.
|
|
|
|
|
|
2020-12-13 07:06:18 -08:00
|
|
|
#include "JSInterface_GUIProxy.h"
|
|
|
|
|
|
|
|
|
|
#include "gui/CGUI.h"
|
|
|
|
|
#include "gui/ObjectBases/IGUIObject.h"
|
|
|
|
|
#include "ps/CLogger.h"
|
2025-06-19 15:06:41 -07:00
|
|
|
#include "ps/CStr.h"
|
2021-04-09 11:01:47 -07:00
|
|
|
#include "scriptinterface/FunctionWrapper.h"
|
2023-07-07 13:12:16 -07:00
|
|
|
#include "scriptinterface/Object.h"
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
#include "scriptinterface/Conversions.h"
|
|
|
|
|
#include "scriptinterface/Request.h"
|
2020-12-13 07:06:18 -08:00
|
|
|
|
2025-06-19 15:06:41 -07:00
|
|
|
#include <js/CallAndConstruct.h>
|
|
|
|
|
#include <js/CallArgs.h>
|
|
|
|
|
#include <js/Class.h>
|
|
|
|
|
#include <js/Object.h>
|
|
|
|
|
#include <js/Proxy.h>
|
|
|
|
|
#include <js/RootingAPI.h>
|
|
|
|
|
#include <js/TypeDecls.h>
|
|
|
|
|
#include <js/Value.h>
|
|
|
|
|
#include <js/friend/ErrorMessages.h>
|
|
|
|
|
#include <jsapi.h>
|
|
|
|
|
#include <jspubtd.h>
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <cstddef>
|
2020-12-13 07:06:18 -08:00
|
|
|
#include <string>
|
2022-12-04 11:56:12 -08:00
|
|
|
#include <string_view>
|
2025-06-19 15:06:41 -07:00
|
|
|
#include <type_traits>
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
|
class JSObject;
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
namespace Script { class Interface; }
|
2020-12-13 07:06:18 -08:00
|
|
|
|
Allow setting CGUISize properties directly
Previously, modifying individual CGUISize properties (left, top, right,
bottom) via JavaScript had no effect unless the entire size object was
copied, modified, and reassigned—resulting in verbose and error-prone
code.
This update introduces proper JavaScript property accessors, enabling
direct reading and writing of CGUISize members. The result is a more
intuitive and scriptable API.
Key Changes:
- Implemented JavaScript get/set accessors for each CGUISize property,
directly interacting with the native object.
- Retained support for string assignments to CGUISize
- Maintained the GUISize JS object for backward compatibility, also
marked as deprecated.
- Improved CGUISimpleSetting<CGUISize> to allow partial updates via
direct property access.
- allow assigning size from object with pixel and percent values
You can now assign size using an object with properties like:
size = {
left: number, right: number, top: number, bottom: number,
rleft: number, rright: number, rtop: number, rbottom: number
}
- One or more properties can be specified, the missing properties with
fill as zero.
- Assignment triggers an immediate update event.
Delayed Setting Notifications:
- Introduced a mechanism for delayed setting notifications, currently
only used in CGUISimpleSetting<CGUISize>, to avoid redundant
recalculations.
- Ensured that getComputedSize triggers any pending delayed
notifications before returning the size.
This change significantly enhances the developer experience and brings
the behavior in line with typical expectations for direct property
manipulation in JavaScript.
2025-06-04 14:22:34 -07:00
|
|
|
#ifndef INCLUDED_JSI_GUIPROXY_IMP
|
|
|
|
|
#define INCLUDED_JSI_GUIPROXY_IMP
|
|
|
|
|
|
2020-11-21 09:49:06 -08:00
|
|
|
template <typename T>
|
|
|
|
|
JSI_GUIProxy<T>& JSI_GUIProxy<T>::Singleton()
|
|
|
|
|
{
|
|
|
|
|
static JSI_GUIProxy<T> s;
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-09 11:01:47 -07:00
|
|
|
// Call this for every specialised type. You will need to override IGUIObject::CreateJSObject() in your class interface.
|
|
|
|
|
#define DECLARE_GUIPROXY(Type) \
|
|
|
|
|
void Type::CreateJSObject() \
|
|
|
|
|
{ \
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
Script::Request rq(m_pGUI.GetScriptInterface()); \
|
2021-04-09 11:01:47 -07:00
|
|
|
using ProxyHandler = JSI_GUIProxy<std::remove_pointer_t<decltype(this)>>; \
|
|
|
|
|
m_JSObject = ProxyHandler::CreateJSObject(rq, this, GetGUI().GetProxyData(&ProxyHandler::Singleton())); \
|
|
|
|
|
} \
|
|
|
|
|
template class JSI_GUIProxy<Type>;
|
|
|
|
|
|
2020-12-13 07:06:18 -08:00
|
|
|
// Use a common namespace to avoid duplicating the symbols un-necessarily.
|
|
|
|
|
namespace JSInterface_GUIProxy
|
2020-11-21 09:49:06 -08:00
|
|
|
{
|
2021-04-30 06:20:43 -07:00
|
|
|
// All proxy objects share a class definition.
|
|
|
|
|
JSClass& ClassDefinition()
|
2020-11-21 09:49:06 -08:00
|
|
|
{
|
2021-04-30 06:20:43 -07:00
|
|
|
static JSClass c = PROXY_CLASS_DEF("GUIObjectProxy", JSCLASS_HAS_CACHED_PROTO(JSProto_Proxy) | JSCLASS_HAS_RESERVED_SLOTS(1));
|
|
|
|
|
return c;
|
2020-11-21 09:49:06 -08:00
|
|
|
}
|
2020-12-13 07:06:18 -08:00
|
|
|
|
|
|
|
|
// Default implementation of the cache via unordered_map
|
|
|
|
|
class MapCache : public GUIProxyProps
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
virtual ~MapCache() {};
|
|
|
|
|
|
2026-01-16 13:14:58 -08:00
|
|
|
bool has(const std::string& name) const override
|
2020-12-13 07:06:18 -08:00
|
|
|
{
|
|
|
|
|
return m_Functions.find(name) != m_Functions.end();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 13:14:58 -08:00
|
|
|
JSObject* get(const std::string& name) const override
|
2020-12-13 07:06:18 -08:00
|
|
|
{
|
|
|
|
|
return m_Functions.at(name).get();
|
|
|
|
|
}
|
|
|
|
|
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
bool setFunction(const Script::Request& rq, const std::string& name, JSFunction* function) override
|
2020-12-13 07:06:18 -08:00
|
|
|
{
|
2021-04-09 11:01:47 -07:00
|
|
|
m_Functions[name].init(rq.cx, JS_GetFunctionObject(function));
|
2020-12-13 07:06:18 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 13:14:58 -08:00
|
|
|
std::vector<std::string_view> getPropsNames() const override
|
2025-06-25 06:05:32 -07:00
|
|
|
{
|
|
|
|
|
std::vector<std::string_view> result;
|
|
|
|
|
result.reserve(m_Functions.size());
|
|
|
|
|
|
|
|
|
|
for (const auto& [key, value] : m_Functions)
|
|
|
|
|
result.emplace_back(key);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-13 07:06:18 -08:00
|
|
|
protected:
|
|
|
|
|
std::unordered_map<std::string, JS::PersistentRootedObject> m_Functions;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-30 06:20:43 -07:00
|
|
|
template<>
|
|
|
|
|
IGUIObject* IGUIProxyObject::FromPrivateSlot<IGUIObject>(JSObject* obj)
|
|
|
|
|
{
|
|
|
|
|
if (!obj)
|
|
|
|
|
return nullptr;
|
2023-01-10 09:06:47 -08:00
|
|
|
if (JS::GetClass(obj) != &JSInterface_GUIProxy::ClassDefinition())
|
2021-04-30 06:20:43 -07:00
|
|
|
return nullptr;
|
|
|
|
|
return UnsafeFromPrivateSlot<IGUIObject>(obj);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-13 07:06:18 -08:00
|
|
|
// The default propcache is a MapCache.
|
|
|
|
|
template<typename T>
|
|
|
|
|
struct JSI_GUIProxy<T>::PropCache
|
|
|
|
|
{
|
|
|
|
|
using type = JSInterface_GUIProxy::MapCache;
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-30 06:20:43 -07:00
|
|
|
template <typename T>
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
T* JSI_GUIProxy<T>::FromPrivateSlot(const Script::Request&, JS::CallArgs& args)
|
2021-04-30 06:20:43 -07:00
|
|
|
{
|
|
|
|
|
// Call the unsafe version - this is only ever called from actual proxy objects.
|
|
|
|
|
return IGUIProxyObject::UnsafeFromPrivateSlot<T>(args.thisv().toObjectOrNull());
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-13 07:06:18 -08:00
|
|
|
template <typename T>
|
|
|
|
|
bool JSI_GUIProxy<T>::PropGetter(JS::HandleObject proxy, const std::string& propName, JS::MutableHandleValue vp) const
|
|
|
|
|
{
|
|
|
|
|
using PropertyCache = typename PropCache::type;
|
|
|
|
|
// 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()));
|
|
|
|
|
if (data->has(propName))
|
|
|
|
|
{
|
|
|
|
|
vp.setObjectOrNull(data->get(propName));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
std::pair<const js::BaseProxyHandler*, GUIProxyProps*> JSI_GUIProxy<T>::CreateData(Script::Interface& scriptInterface)
|
2020-12-13 07:06:18 -08:00
|
|
|
{
|
|
|
|
|
using PropertyCache = typename PropCache::type;
|
|
|
|
|
PropertyCache* data = new PropertyCache();
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
Script::Request rq(scriptInterface);
|
2021-04-09 11:01:47 -07:00
|
|
|
|
|
|
|
|
// Functions common to all children of IGUIObject.
|
|
|
|
|
JSI_GUIProxy<IGUIObject>::CreateFunctions(rq, data);
|
|
|
|
|
|
|
|
|
|
// Let derived classes register their own interface.
|
|
|
|
|
if constexpr (!std::is_same_v<T, IGUIObject>)
|
|
|
|
|
CreateFunctions(rq, data);
|
2020-12-13 07:06:18 -08:00
|
|
|
return { &Singleton(), data };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2021-04-09 11:01:47 -07:00
|
|
|
template<auto callable>
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
void JSI_GUIProxy<T>::CreateFunction(const Script::Request& rq, GUIProxyProps* cache, const std::string& name)
|
2021-04-09 11:01:47 -07:00
|
|
|
{
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
cache->setFunction(rq, name, Script::Function::Create<callable, FromPrivateSlot>(rq, name.c_str()));
|
2021-04-09 11:01:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
std::unique_ptr<IGUIProxyObject> JSI_GUIProxy<T>::CreateJSObject(const Script::Request& rq, T* ptr, GUIProxyProps* dataPtr)
|
2020-12-13 07:06:18 -08:00
|
|
|
{
|
|
|
|
|
js::ProxyOptions options;
|
2021-04-30 06:20:43 -07:00
|
|
|
options.setClass(&JSInterface_GUIProxy::ClassDefinition());
|
2020-12-13 07:06:18 -08:00
|
|
|
|
2021-04-09 11:01:47 -07:00
|
|
|
auto ret = std::make_unique<IGUIProxyObject>();
|
|
|
|
|
ret->m_Ptr = static_cast<IGUIObject*>(ptr);
|
|
|
|
|
|
2020-12-13 07:06:18 -08:00
|
|
|
JS::RootedValue cppObj(rq.cx), data(rq.cx);
|
2021-04-09 11:01:47 -07:00
|
|
|
cppObj.get().setPrivate(ret->m_Ptr);
|
2020-12-13 07:06:18 -08:00
|
|
|
data.get().setPrivate(static_cast<void*>(dataPtr));
|
2021-04-09 11:01:47 -07:00
|
|
|
ret->m_Object.init(rq.cx, js::NewProxyObject(rq.cx, &Singleton(), cppObj, nullptr, options));
|
|
|
|
|
js::SetProxyReservedSlot(ret->m_Object, 0, data);
|
|
|
|
|
return ret;
|
2020-11-21 09:49:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2025-06-03 05:13:41 -07:00
|
|
|
bool JSI_GUIProxy<T>::get(JSContext* cx, JS::HandleObject proxy, JS::HandleValue /*receiver*/,
|
|
|
|
|
JS::HandleId id, JS::MutableHandleValue vp) const
|
2020-11-21 09:49:06 -08:00
|
|
|
{
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
Script::Request rq(cx);
|
2020-11-21 09:49:06 -08:00
|
|
|
|
2021-04-09 11:01:47 -07:00
|
|
|
T* e = IGUIProxyObject::FromPrivateSlot<T>(proxy.get());
|
2020-11-21 09:49:06 -08:00
|
|
|
if (!e)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
JS::RootedValue idval(rq.cx);
|
|
|
|
|
if (!JS_IdToValue(rq.cx, id, &idval))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
std::string propName;
|
2021-05-13 02:43:33 -07:00
|
|
|
if (!Script::FromJSVal(rq, idval, propName))
|
2020-11-21 09:49:06 -08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Return function properties. Specializable.
|
2020-12-13 07:06:18 -08:00
|
|
|
if (PropGetter(proxy, propName, vp))
|
2020-11-21 09:49:06 -08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
// Use onWhatever to access event handlers
|
|
|
|
|
if (propName.substr(0, 2) == "on")
|
|
|
|
|
{
|
|
|
|
|
CStr eventName(propName.substr(2));
|
|
|
|
|
std::map<CStr, JS::Heap<JSObject*>>::iterator it = e->m_ScriptHandlers.find(eventName);
|
|
|
|
|
if (it == e->m_ScriptHandlers.end())
|
|
|
|
|
vp.setNull();
|
|
|
|
|
else
|
|
|
|
|
vp.setObject(*it->second.get());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (propName == "parent")
|
|
|
|
|
{
|
|
|
|
|
IGUIObject* parent = e->GetParent();
|
|
|
|
|
|
|
|
|
|
if (parent)
|
|
|
|
|
vp.set(JS::ObjectValue(*parent->GetJSObject()));
|
|
|
|
|
else
|
|
|
|
|
vp.set(JS::NullValue());
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (propName == "children")
|
|
|
|
|
{
|
2026-02-02 09:24:57 -08:00
|
|
|
JS::RootedValueVector children{rq.cx};
|
|
|
|
|
|
|
|
|
|
for (const auto& child : e->m_Children)
|
|
|
|
|
{
|
|
|
|
|
JS::RootedValue rootedChild{rq.cx};
|
|
|
|
|
Script::ToJSVal(rq, &rootedChild, child);
|
|
|
|
|
if (!children.append(rootedChild))
|
|
|
|
|
throw std::runtime_error{"Append failed"};
|
|
|
|
|
}
|
|
|
|
|
vp.set(JS::ObjectValue(*JS::NewArrayObject(rq.cx, children)));
|
2020-11-21 09:49:06 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (propName == "name")
|
|
|
|
|
{
|
2021-05-13 02:43:33 -07:00
|
|
|
Script::ToJSVal(rq, vp, e->GetName());
|
2020-11-21 09:49:06 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (e->SettingExists(propName))
|
|
|
|
|
{
|
|
|
|
|
e->m_Settings[propName]->ToJSVal(rq, vp);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOGERROR("Property '%s' does not exist!", propName.c_str());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
bool JSI_GUIProxy<T>::set(JSContext* cx, JS::HandleObject proxy, JS::HandleId id, JS::HandleValue vp,
|
2025-06-03 05:13:41 -07:00
|
|
|
JS::HandleValue /*receiver*/, JS::ObjectOpResult& result) const
|
2020-11-21 09:49:06 -08:00
|
|
|
{
|
2021-04-09 11:01:47 -07:00
|
|
|
T* e = IGUIProxyObject::FromPrivateSlot<T>(proxy.get());
|
2020-11-21 09:49:06 -08:00
|
|
|
if (!e)
|
2020-12-06 06:03:02 -08:00
|
|
|
{
|
|
|
|
|
LOGERROR("C++ GUI Object could not be found");
|
|
|
|
|
return result.fail(JSMSG_OBJECT_REQUIRED);
|
|
|
|
|
}
|
2020-11-21 09:49:06 -08:00
|
|
|
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
Script::Request rq(cx);
|
2020-11-21 09:49:06 -08:00
|
|
|
|
|
|
|
|
JS::RootedValue idval(rq.cx);
|
|
|
|
|
if (!JS_IdToValue(rq.cx, id, &idval))
|
2020-12-06 06:03:02 -08:00
|
|
|
return result.fail(JSMSG_BAD_PROP_ID);
|
2020-11-21 09:49:06 -08:00
|
|
|
|
|
|
|
|
std::string propName;
|
2021-05-13 02:43:33 -07:00
|
|
|
if (!Script::FromJSVal(rq, idval, propName))
|
2020-12-06 06:03:02 -08:00
|
|
|
return result.fail(JSMSG_BAD_PROP_ID);
|
2020-11-21 09:49:06 -08:00
|
|
|
|
|
|
|
|
if (propName == "name")
|
|
|
|
|
{
|
|
|
|
|
std::string value;
|
2021-05-13 02:43:33 -07:00
|
|
|
if (!Script::FromJSVal(rq, vp, value))
|
2020-12-06 06:03:02 -08:00
|
|
|
return result.fail(JSMSG_BAD_PROP_ID);
|
2020-11-21 09:49:06 -08:00
|
|
|
e->SetName(value);
|
|
|
|
|
return result.succeed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JS::RootedObject vpObj(cx);
|
|
|
|
|
if (vp.isObject())
|
|
|
|
|
vpObj = &vp.toObject();
|
|
|
|
|
|
|
|
|
|
// Use onWhatever to set event handlers
|
|
|
|
|
if (propName.substr(0, 2) == "on")
|
|
|
|
|
{
|
2024-10-25 01:22:43 -07:00
|
|
|
if (vp.isPrimitive() || vp.isNull() || !JS::IsCallable(&vp.toObject()))
|
2020-11-21 09:49:06 -08:00
|
|
|
{
|
|
|
|
|
LOGERROR("on- event-handlers must be functions");
|
|
|
|
|
return result.fail(JSMSG_NOT_FUNCTION);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CStr eventName(propName.substr(2));
|
|
|
|
|
e->SetScriptHandler(eventName, vpObj);
|
|
|
|
|
|
|
|
|
|
return result.succeed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e->SettingExists(propName))
|
|
|
|
|
return e->m_Settings[propName]->FromJSVal(rq, vp, true) ? result.succeed() : result.fail(JSMSG_USER_DEFINED_ERROR);
|
|
|
|
|
|
|
|
|
|
LOGERROR("Property '%s' does not exist!", propName.c_str());
|
2020-12-06 06:03:02 -08:00
|
|
|
return result.fail(JSMSG_BAD_PROP_ID);
|
2020-11-21 09:49:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
bool JSI_GUIProxy<T>::delete_(JSContext* cx, JS::HandleObject proxy, JS::HandleId id, JS::ObjectOpResult& result) const
|
|
|
|
|
{
|
2021-04-09 11:01:47 -07:00
|
|
|
T* e = IGUIProxyObject::FromPrivateSlot<T>(proxy.get());
|
2020-11-21 09:49:06 -08:00
|
|
|
if (!e)
|
2020-12-06 06:03:02 -08:00
|
|
|
{
|
|
|
|
|
LOGERROR("C++ GUI Object could not be found");
|
|
|
|
|
return result.fail(JSMSG_OBJECT_REQUIRED);
|
|
|
|
|
}
|
2020-11-21 09:49:06 -08:00
|
|
|
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
Script::Request rq(cx);
|
2020-11-21 09:49:06 -08:00
|
|
|
|
|
|
|
|
JS::RootedValue idval(rq.cx);
|
|
|
|
|
if (!JS_IdToValue(rq.cx, id, &idval))
|
2020-12-06 06:03:02 -08:00
|
|
|
return result.fail(JSMSG_BAD_PROP_ID);
|
2020-11-21 09:49:06 -08:00
|
|
|
|
|
|
|
|
std::string propName;
|
2021-05-13 02:43:33 -07:00
|
|
|
if (!Script::FromJSVal(rq, idval, propName))
|
2020-12-06 06:03:02 -08:00
|
|
|
return result.fail(JSMSG_BAD_PROP_ID);
|
2020-11-21 09:49:06 -08:00
|
|
|
|
|
|
|
|
// event handlers
|
2022-12-04 11:56:12 -08:00
|
|
|
if (std::string_view{propName}.substr(0, 2) == "on")
|
2020-11-21 09:49:06 -08:00
|
|
|
{
|
|
|
|
|
CStr eventName(propName.substr(2));
|
|
|
|
|
e->UnsetScriptHandler(eventName);
|
|
|
|
|
return result.succeed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOGERROR("Only event handlers can be deleted from GUI objects!");
|
2020-12-06 06:03:02 -08:00
|
|
|
return result.fail(JSMSG_BAD_PROP_ID);
|
2020-11-21 09:49:06 -08:00
|
|
|
}
|
2025-06-25 06:05:32 -07:00
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
bool JSI_GUIProxy<T>::ownPropertyKeys(JSContext* cx, JS::HandleObject proxy, JS::MutableHandleIdVector props) const
|
|
|
|
|
{
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
Script::Request rq(cx);
|
2025-06-25 06:05:32 -07:00
|
|
|
|
|
|
|
|
T* e = IGUIProxyObject::FromPrivateSlot<T>(proxy.get());
|
|
|
|
|
if (!e)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Add common properties.
|
|
|
|
|
static constexpr std::array<const char*, 3> keys = {
|
|
|
|
|
"name",
|
|
|
|
|
"parent",
|
|
|
|
|
"children"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (const char* key : keys)
|
|
|
|
|
{
|
|
|
|
|
JS::RootedString str(cx, JS_NewStringCopyZ(cx, key));
|
|
|
|
|
if (!str)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
JS::RootedId id(cx);
|
|
|
|
|
if (!JS_StringToId(cx, str, &id))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (!props.append(id))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add settings.
|
|
|
|
|
for (const auto& [name, setting] : e->m_Settings)
|
|
|
|
|
{
|
|
|
|
|
JS::RootedString str(cx, JS_NewStringCopyZ(cx, name.c_str()));
|
|
|
|
|
JS::RootedId id(cx);
|
|
|
|
|
if (!str || !JS_StringToId(cx, str, &id) || !props.append(id))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add script handlers.
|
|
|
|
|
for (const auto& [name, scriptHandler] : e->m_ScriptHandlers)
|
|
|
|
|
{
|
|
|
|
|
JS::RootedString str(cx, JS_NewStringCopyZ(cx, fmt::format("on{}", name).c_str()));
|
|
|
|
|
JS::RootedId id(cx);
|
|
|
|
|
if (!str || !JS_StringToId(cx, str, &id) || !props.append(id))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add properties from the cache.
|
|
|
|
|
using PropertyCache = typename PropCache::type;
|
|
|
|
|
const PropertyCache* data = static_cast<const PropertyCache*>(static_cast<const GUIProxyProps*>(js::GetProxyReservedSlot(proxy, 0).toPrivate()));
|
|
|
|
|
for (const auto& key : data->getPropsNames()) {
|
|
|
|
|
JS::RootedString str(cx, JS_NewStringCopyZ(cx, key.data()));
|
|
|
|
|
JS::RootedId id(cx);
|
|
|
|
|
if (!str || !JS_StringToId(cx, str, &id) || !props.append(id))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
bool JSI_GUIProxy<T>::getOwnPropertyDescriptor(JSContext* cx, JS::HandleObject proxy, JS::HandleId id, JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc) const
|
|
|
|
|
{
|
|
|
|
|
JS::RootedValue value(cx);
|
|
|
|
|
|
|
|
|
|
if (!this->get(cx, proxy, JS::UndefinedHandleValue, id, &value))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (value.isUndefined())
|
|
|
|
|
{
|
|
|
|
|
desc.set(mozilla::Nothing());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
desc.set(mozilla::Some(JS::PropertyDescriptor::Data(value, JSPROP_ENUMERATE | JSPROP_READONLY)));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
Allow setting CGUISize properties directly
Previously, modifying individual CGUISize properties (left, top, right,
bottom) via JavaScript had no effect unless the entire size object was
copied, modified, and reassigned—resulting in verbose and error-prone
code.
This update introduces proper JavaScript property accessors, enabling
direct reading and writing of CGUISize members. The result is a more
intuitive and scriptable API.
Key Changes:
- Implemented JavaScript get/set accessors for each CGUISize property,
directly interacting with the native object.
- Retained support for string assignments to CGUISize
- Maintained the GUISize JS object for backward compatibility, also
marked as deprecated.
- Improved CGUISimpleSetting<CGUISize> to allow partial updates via
direct property access.
- allow assigning size from object with pixel and percent values
You can now assign size using an object with properties like:
size = {
left: number, right: number, top: number, bottom: number,
rleft: number, rright: number, rtop: number, rbottom: number
}
- One or more properties can be specified, the missing properties with
fill as zero.
- Assignment triggers an immediate update event.
Delayed Setting Notifications:
- Introduced a mechanism for delayed setting notifications, currently
only used in CGUISimpleSetting<CGUISize>, to avoid redundant
recalculations.
- Ensured that getComputedSize triggers any pending delayed
notifications before returning the size.
This change significantly enhances the developer experience and brings
the behavior in line with typical expectations for direct property
manipulation in JavaScript.
2025-06-04 14:22:34 -07:00
|
|
|
#endif // INCLUDED_JSI_GUIPROXY_IMP
|