1 | /* Copyright (C) 2021 Wildfire Games.
|
---|
2 | * This file is part of 0 A.D.
|
---|
3 | *
|
---|
4 | * 0 A.D. is free software: you can redistribute it and/or modify
|
---|
5 | * it under the terms of the GNU General Public License as published by
|
---|
6 | * the Free Software Foundation, either version 2 of the License, or
|
---|
7 | * (at your option) any later version.
|
---|
8 | *
|
---|
9 | * 0 A.D. is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | * GNU General Public License for more details.
|
---|
13 | *
|
---|
14 | * You should have received a copy of the GNU General Public License
|
---|
15 | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef INCLUDED_JSI_GUIPROXY
|
---|
19 | #define INCLUDED_JSI_GUIPROXY
|
---|
20 |
|
---|
21 | #include "gui/ObjectBases/IGUIObject.h"
|
---|
22 | #include "scriptinterface/ScriptExtraHeaders.h"
|
---|
23 |
|
---|
24 | #include <memory>
|
---|
25 | #include <utility>
|
---|
26 |
|
---|
27 | class ScriptInterface;
|
---|
28 | class ScriptRequest;
|
---|
29 |
|
---|
30 | template <typename T>
|
---|
31 | class JSI_GUIProxy;
|
---|
32 |
|
---|
33 | // See JSI_GuiProxy below
|
---|
34 | #if GCC_VERSION
|
---|
35 | # pragma GCC diagnostic push
|
---|
36 | # pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
|
---|
37 | #elif MSC_VERSION
|
---|
38 | # pragma warning(push, 1)
|
---|
39 | # pragma warning(disable: 4265)
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * JS GUI proxies need to store some private data.
|
---|
44 | * This class is responsible for deleting that data.
|
---|
45 | */
|
---|
46 | class IGUIProxyObject final
|
---|
47 | {
|
---|
48 | template<typename T>
|
---|
49 | friend class JSI_GUIProxy;
|
---|
50 | friend std::unique_ptr<IGUIProxyObject> std::make_unique<IGUIProxyObject>();
|
---|
51 | public:
|
---|
52 | JSObject* Get() const
|
---|
53 | {
|
---|
54 | return m_Object.get();
|
---|
55 | }
|
---|
56 |
|
---|
57 | using PrivateData = IGUIObject*;
|
---|
58 | template<typename T>
|
---|
59 | static T* FromPrivateSlot(JSObject* obj)
|
---|
60 | {
|
---|
61 | return static_cast<T*>(static_cast<PrivateData>(js::GetProxyPrivate(obj).toPrivate()));
|
---|
62 | }
|
---|
63 |
|
---|
64 | protected:
|
---|
65 | IGUIProxyObject() = default;
|
---|
66 | IGUIProxyObject(const IGUIProxyObject&) = delete;
|
---|
67 | IGUIProxyObject(IGUIProxyObject&&) = delete;
|
---|
68 |
|
---|
69 | JS::PersistentRootedObject m_Object;
|
---|
70 | PrivateData m_Ptr;
|
---|
71 | };
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Proxies need to store some data whose lifetime is tied to an interface.
|
---|
75 | * This is the virtual interface of that data.
|
---|
76 | */
|
---|
77 | class GUIProxyProps
|
---|
78 | {
|
---|
79 | public:
|
---|
80 | virtual ~GUIProxyProps() {};
|
---|
81 |
|
---|
82 | // @return true if @param name exists in this cache.
|
---|
83 | virtual bool has(const std::string& name) const = 0;
|
---|
84 | // @return the JSFunction matching @param name. Must call has() first as it can assume existence.
|
---|
85 | virtual JSObject* get(const std::string& name) const = 0;
|
---|
86 | virtual bool setFunction(const ScriptRequest& rq, const std::string& name, JSFunction* function) = 0;
|
---|
87 | };
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Handles the js interface with C++ GUI objects.
|
---|
91 | * Proxy handlers must live for at least as long as the JS runtime
|
---|
92 | * where a proxy object with that handler was created. The reason is that
|
---|
93 | * proxy handlers are called during GC, such as on runtime destruction.
|
---|
94 | * In practical terms, this means "just keep them static and store no data".
|
---|
95 | *
|
---|
96 | * GUI Objects only exist in C++ and have no JS-only properties.
|
---|
97 | * As such, there is no "target" JS object that this proxy could point to,
|
---|
98 | * and thus we should inherit from BaseProxyHandler and not js::Wrapper.
|
---|
99 | *
|
---|
100 | * Proxies can be used with prototypes and almost treated like regular JS objects.
|
---|
101 | * However, the default implementation embarks a lot of code that we don't really need here,
|
---|
102 | * since the only fanciness is that we cache JSFunction* properties.
|
---|
103 | * As such, these GUI proxies don't have one and instead override get/set directly.
|
---|
104 | *
|
---|
105 | * To add a new JSI_GUIProxy, you'll need to:
|
---|
106 | * - overload CreateJSObject in your class header.
|
---|
107 | * - change the CGUI::AddObjectTypes method.
|
---|
108 | * - explicitly instantiate the template & CreateJSObject via DECLARE_GUIPROXY.
|
---|
109 | *
|
---|
110 | */
|
---|
111 | template<typename GUIObjectType>
|
---|
112 | class JSI_GUIProxy : public js::BaseProxyHandler
|
---|
113 | {
|
---|
114 | // Need to friend other specializations so CreateFunctions() can call the IGUIObject version in all codepaths.
|
---|
115 | template<typename T>
|
---|
116 | friend class JSI_GUIProxy;
|
---|
117 | public:
|
---|
118 | // Access the js::Class of the Proxy.
|
---|
119 | static JSClass& ClassDefinition();
|
---|
120 |
|
---|
121 | // For convenience, this is the single instantiated JSI_GUIProxy.
|
---|
122 | static JSI_GUIProxy& Singleton();
|
---|
123 |
|
---|
124 | // Call this in CGUI::AddObjectTypes.
|
---|
125 | static std::pair<const js::BaseProxyHandler*, GUIProxyProps*> CreateData(ScriptInterface& scriptInterface);
|
---|
126 |
|
---|
127 | // Create the JS object, the proxy, the data and wrap it in a convenient unique_ptr.
|
---|
128 | static std::unique_ptr<IGUIProxyObject> CreateJSObject(const ScriptRequest& rq, GUIObjectType* ptr, GUIProxyProps* data);
|
---|
129 | protected:
|
---|
130 | // @param family can't be nullptr because that's used for some DOM object and it crashes.
|
---|
131 | JSI_GUIProxy() : BaseProxyHandler(this, false, false) {};
|
---|
132 |
|
---|
133 | // Note: SM provides no virtual destructor for baseProxyHandler.
|
---|
134 | // This also enforces making proxy handlers dataless static variables.
|
---|
135 | ~JSI_GUIProxy() {};
|
---|
136 |
|
---|
137 | // The default implementations need to know the type of the GUIProxyProps for this proxy type.
|
---|
138 | // This is done by specializing this struct's alias type.
|
---|
139 | struct PropCache;
|
---|
140 |
|
---|
141 | // Specialize this to define the custom properties of this type.
|
---|
142 | static void CreateFunctions(const ScriptRequest& rq, GUIProxyProps* cache);
|
---|
143 |
|
---|
144 | // Convenience helper for the above.
|
---|
145 | template<auto callable>
|
---|
146 | static void CreateFunction(const ScriptRequest& rq, GUIProxyProps* cache, const std::string& name);
|
---|
147 |
|
---|
148 | // This handles returning custom properties. Specialize this if needed.
|
---|
149 | bool PropGetter(JS::HandleObject proxy, const std::string& propName, JS::MutableHandleValue vp) const;
|
---|
150 | protected:
|
---|
151 | // BaseProxyHandler interface below
|
---|
152 |
|
---|
153 | // Handler for `object.x`
|
---|
154 | virtual bool get(JSContext* cx, JS::HandleObject proxy, JS::HandleValue receiver, JS::HandleId id, JS::MutableHandleValue vp) const override final;
|
---|
155 | // Handler for `object.x = y;`
|
---|
156 | virtual bool set(JSContext* cx, JS::HandleObject proxy, JS::HandleId id, JS::HandleValue vp,
|
---|
157 | JS::HandleValue receiver, JS::ObjectOpResult& result) const final;
|
---|
158 | // Handler for `delete object.x;`
|
---|
159 | virtual bool delete_(JSContext* cx, JS::HandleObject proxy, JS::HandleId id, JS::ObjectOpResult& result) const override final;
|
---|
160 |
|
---|
161 | // The following methods are not provided by BaseProxyHandler.
|
---|
162 | // We provide defaults that do nothing (some raise JS exceptions).
|
---|
163 |
|
---|
164 | // The JS code will see undefined when querying a property descriptor.
|
---|
165 | virtual bool getOwnPropertyDescriptor(JSContext* UNUSED(cx), JS::HandleObject UNUSED(proxy), JS::HandleId UNUSED(id),
|
---|
166 | JS::MutableHandle<JS::PropertyDescriptor> UNUSED(desc)) const override
|
---|
167 | {
|
---|
168 | return true;
|
---|
169 | }
|
---|
170 | // Throw an exception is JS code attempts defining a property.
|
---|
171 | virtual bool defineProperty(JSContext* UNUSED(cx), JS::HandleObject UNUSED(proxy), JS::HandleId UNUSED(id),
|
---|
172 | JS::Handle<JS::PropertyDescriptor> UNUSED(desc), JS::ObjectOpResult& UNUSED(result)) const override
|
---|
173 | {
|
---|
174 | return false;
|
---|
175 | }
|
---|
176 | // No accessible properties.
|
---|
177 | virtual bool ownPropertyKeys(JSContext* UNUSED(cx), JS::HandleObject UNUSED(proxy), JS::MutableHandleIdVector UNUSED(props)) const override
|
---|
178 | {
|
---|
179 | return true;
|
---|
180 | }
|
---|
181 | // Nothing to enumerate.
|
---|
182 | virtual bool enumerate(JSContext* UNUSED(cx), JS::HandleObject UNUSED(proxy), JS::MutableHandleIdVector UNUSED(props)) const override
|
---|
183 | {
|
---|
184 | return true;
|
---|
185 | }
|
---|
186 | // Throw an exception is JS attempts to query the prototype.
|
---|
187 | virtual bool getPrototypeIfOrdinary(JSContext* UNUSED(cx), JS::HandleObject UNUSED(proxy), bool* UNUSED(isOrdinary), JS::MutableHandleObject UNUSED(protop)) const override
|
---|
188 | {
|
---|
189 | return false;
|
---|
190 | }
|
---|
191 | // Throw an exception - no prototype to set.
|
---|
192 | virtual bool setImmutablePrototype(JSContext* UNUSED(cx), JS::HandleObject UNUSED(proxy), bool* UNUSED(succeeded)) const override
|
---|
193 | {
|
---|
194 | return false;
|
---|
195 | }
|
---|
196 | // We are not extensible.
|
---|
197 | virtual bool preventExtensions(JSContext* UNUSED(cx), JS::HandleObject UNUSED(proxy), JS::ObjectOpResult& UNUSED(result)) const override
|
---|
198 | {
|
---|
199 | return true;
|
---|
200 | }
|
---|
201 | virtual bool isExtensible(JSContext* UNUSED(cx), JS::HandleObject UNUSED(proxy), bool* extensible) const override
|
---|
202 | {
|
---|
203 | *extensible = false;
|
---|
204 | return true;
|
---|
205 | }
|
---|
206 | };
|
---|
207 |
|
---|
208 | #if GCC_VERSION
|
---|
209 | # pragma GCC diagnostic pop
|
---|
210 | #elif MSC_VERSION
|
---|
211 | # pragma warning(pop)
|
---|
212 | #endif
|
---|
213 |
|
---|
214 |
|
---|
215 | #endif // INCLUDED_JSI_GUIPROXY
|
---|