Line data Source code
1 : /* Copyright (C) 2026 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_INTERFACE_SCRIPTED 19 : #define INCLUDED_INTERFACE_SCRIPTED 20 : 21 : #include "scriptinterface/FunctionWrapper.h" 22 : #include "scriptinterface/Object.h" 23 : #include "scriptinterface/Conversions.h" 24 : #include "scriptinterface/Interface.h" 25 : #include "simulation2/system/ComponentManager.h" 26 : 27 : #define BEGIN_INTERFACE_WRAPPER(iname) \ 28 : JSClass class_ICmp##iname = { \ 29 : "ICmp" #iname, JSCLASS_HAS_RESERVED_SLOTS( Script::Interface::JSObjectReservedSlots::PRIVATE+1 ) \ 30 : }; \ 31 : static JSFunctionSpec methods_ICmp##iname[] = { 32 : 33 : #define END_INTERFACE_WRAPPER(iname) \ 34 : JS_FS_END \ 35 : }; \ 36 : void ICmp##iname::InterfaceInit(Script::Interface& scriptInterface) { \ 37 : scriptInterface.DefineCustomObjectType(&class_ICmp##iname, NULL, 0, NULL, methods_ICmp##iname, NULL, NULL); \ 38 : } \ 39 : bool ICmp##iname::NewJSObject(const Script::Interface& scriptInterface, JS::MutableHandleObject out) const\ 40 : { \ 41 : out.set(scriptInterface.CreateCustomObject("ICmp" #iname)); \ 42 : IComponent* comp = const_cast<IComponent*>(static_cast<const IComponent*>(this)); \ 43 : JS::SetReservedSlot(out, Script::Interface::JSObjectReservedSlots::PRIVATE, JS::PrivateValue(comp)); \ 44 : return true; \ 45 : } \ 46 : JS::HandleValue ICmp##iname::GetJSInstance() const \ 47 : { \ 48 : if (m_CachedInstance) \ 49 : return JS::HandleValue::fromMarkedLocation(m_CachedInstance.unsafeAddress()); \ 50 : \ 51 : const Script::Interface& si = GetSimContext().GetScriptInterface(); \ 52 : Script::Request rq(si); \ 53 : JS::RootedObject obj(rq.cx); \ 54 : NewJSObject(GetSimContext().GetScriptInterface(), &obj); \ 55 : m_CachedInstance.setObject(*obj); \ 56 : \ 57 : GetSimContext().GetComponentManager().RegisterTrace(GetEntityId(), m_CachedInstance); \ 58 : return JS::HandleValue::fromMarkedLocation(m_CachedInstance.unsafeAddress()); \ 59 : } \ 60 : void RegisterComponentInterface_##iname(Script::Interface& scriptInterface) { \ 61 : ICmp##iname::InterfaceInit(scriptInterface); \ 62 : } 63 : 64 : template <typename T> 65 37723 : inline T* ComponentGetter(const Script::Request& rq, JS::CallArgs& args) 66 : { 67 37723 : return Script::Interface::GetPrivate<T>(rq, args); 68 : } 69 : 70 : #define DEFINE_INTERFACE_METHOD(scriptname, classname, methodname) \ 71 : Script::Function::Wrap<&classname::methodname, ComponentGetter<classname>>(scriptname), 72 : 73 : #endif // INCLUDED_INTERFACE_SCRIPTED