]> git.lizzy.rs Git - minetest.git/blob - src/script/cpp_api/s_base.h
Use C++11 mutexes only (remove compat code) (#5922)
[minetest.git] / src / script / cpp_api / s_base.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef S_BASE_H_
21 #define S_BASE_H_
22
23 #include <iostream>
24 #include <string>
25
26 extern "C" {
27 #include <lua.h>
28 }
29
30 #include "irrlichttypes.h"
31 #include "threads.h"
32 #include "threading/mutex_auto_lock.h"
33 #include "common/c_types.h"
34 #include "common/c_internal.h"
35
36 #define SCRIPTAPI_LOCK_DEBUG
37 #define SCRIPTAPI_DEBUG
38
39 // MUST be an invalid mod name so that mods can't
40 // use that name to bypass security!
41 #define BUILTIN_MOD_NAME "*builtin*"
42
43 #define PCALL_RES(RES) do {                 \
44         int result_ = (RES);                    \
45         if (result_ != 0) {                     \
46                 scriptError(result_, __FUNCTION__); \
47         }                                       \
48 } while (0)
49
50 #define runCallbacks(nargs, mode) \
51         runCallbacksRaw((nargs), (mode), __FUNCTION__)
52
53 #define setOriginFromTable(index) \
54         setOriginFromTableRaw(index, __FUNCTION__)
55
56 class Server;
57 #ifndef SERVER
58 class Client;
59 #endif
60 class IGameDef;
61 class Environment;
62 class GUIEngine;
63 class ServerActiveObject;
64
65 class ScriptApiBase {
66 public:
67         ScriptApiBase();
68         virtual ~ScriptApiBase();
69
70         // These throw a ModError on failure
71         void loadMod(const std::string &script_path, const std::string &mod_name);
72         void loadScript(const std::string &script_path);
73
74         void runCallbacksRaw(int nargs,
75                 RunCallbacksMode mode, const char *fxn);
76
77         /* object */
78         void addObjectReference(ServerActiveObject *cobj);
79         void removeObjectReference(ServerActiveObject *cobj);
80
81         IGameDef *getGameDef() { return m_gamedef; }
82         Server* getServer();
83 #ifndef SERVER
84         Client* getClient();
85 #endif
86
87         std::string getOrigin() { return m_last_run_mod; }
88         void setOriginDirect(const char *origin);
89         void setOriginFromTableRaw(int index, const char *fxn);
90
91 protected:
92         friend class LuaABM;
93         friend class LuaLBM;
94         friend class InvRef;
95         friend class ObjectRef;
96         friend class NodeMetaRef;
97         friend class ModApiBase;
98         friend class ModApiEnvMod;
99         friend class LuaVoxelManip;
100
101         lua_State* getStack()
102                 { return m_luastack; }
103
104         void realityCheck();
105         void scriptError(int result, const char *fxn);
106         void stackDump(std::ostream &o);
107
108         void setGameDef(IGameDef* gamedef) { m_gamedef = gamedef; }
109
110         Environment* getEnv() { return m_environment; }
111         void setEnv(Environment* env) { m_environment = env; }
112
113         GUIEngine* getGuiEngine() { return m_guiengine; }
114         void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
115
116         void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
117
118         std::recursive_mutex m_luastackmutex;
119         std::string     m_last_run_mod;
120         bool            m_secure;
121 #ifdef SCRIPTAPI_LOCK_DEBUG
122         int             m_lock_recursion_count;
123         threadid_t      m_owning_thread;
124 #endif
125
126 private:
127         static int luaPanic(lua_State *L);
128
129         lua_State*      m_luastack;
130
131         IGameDef*       m_gamedef;
132         Environment*    m_environment;
133         GUIEngine*      m_guiengine;
134 };
135
136 #endif /* S_BASE_H_ */