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_SCRIPT_REQUEST 19 : #define INCLUDED_SCRIPT_REQUEST 20 : 21 : #include "scriptinterface/ForwardDeclarations.h" 22 : 23 : #include <js/RootingAPI.h> 24 : #include <js/TypeDecls.h> 25 : #include <memory> 26 : 27 : namespace JS { class Realm; } 28 : namespace JS { class Value; } 29 : namespace Script { class Interface; } 30 : struct JSContext; 31 : 32 : /** 33 : * Spidermonkey maintains some 'local' state via the JSContext* object. 34 : * This object is an argument to most JSAPI functions. 35 : * Furthermore, this state is Realm (~ global) dependent. For many reasons, including GC safety, 36 : * The JSContext* Realm must be set up correctly when accessing it. 37 : * 'Entering' and 'Leaving' realms must be done in a LIFO manner. 38 : * SM recommends using JSAutoRealm, which provides an RAII option. 39 : * 40 : * Request combines both of the above in a single convenient package, 41 : * providing safe access to the JSContext*, the global object, and ensuring that the proper realm has been entered. 42 : * Most scriptinterface/ functions will take a Request, to ensure proper rooting. You may sometimes 43 : * have to create one from a Interface. 44 : * 45 : * Be particularly careful when manipulating several script interfaces. 46 : */ 47 : namespace Script 48 : { 49 : 50 : class Request 51 : { 52 : Request() = delete; 53 : Request(const Request& rq) = delete; 54 : Request& operator=(const Request& rq) = delete; 55 : public: 56 : /** 57 : * NB: the definitions are in scriptinterface.cpp, because these access members of the PImpled 58 : * implementation of Interface, and that seemed more convenient. 59 : */ 60 : Request(const Interface& scriptInterface); 61 43787 : Request(const Interface* scriptInterface) : Request(*scriptInterface) {} 62 90 : Request(std::shared_ptr<Interface> scriptInterface) : Request(*scriptInterface) {} 63 : ~Request(); 64 : 65 : /** 66 : * Create a script request from a JSContext. 67 : * This can be used to get the script interface in a JSNative function. 68 : * In general, you shouldn't have to rely on this otherwise. 69 : */ 70 : Request(JSContext* cx); 71 : 72 : /** 73 : * Return the scriptInterface active when creating this Request. 74 : * Note that this is multi-request safe: even if another Request is created, 75 : * it will point to the original scriptInterface, and thus can be used to re-enter the realm. 76 : */ 77 : const Interface& GetScriptInterface() const; 78 : 79 : JS::Value globalValue() const; 80 : 81 : // Note that JSContext actually changes behind the scenes when creating another Request for another realm, 82 : // so be _very_ careful when juggling between different realms. 83 : JSContext* cx; 84 : JS::HandleObject glob; 85 : JS::HandleObject nativeScope; 86 : private: 87 : const Interface& m_ScriptInterface; 88 : JS::Realm* m_FormerRealm; 89 : }; 90 : 91 : } 92 : 93 : #endif // INCLUDED_SCRIPT_REQUEST