3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "cpp_api/s_base.h"
21 #include "cpp_api/s_internal.h"
22 #include "cpp_api/s_security.h"
23 #include "lua_api/l_object.h"
24 #include "serverobject.h"
30 #include "util/string.h"
49 ModNameStorer(lua_State *L_, const std::string &mod_name):
52 // Store current mod name in registry
53 lua_pushstring(L, mod_name.c_str());
54 lua_setfield(L, LUA_REGISTRYINDEX, SCRIPT_MOD_NAME_FIELD);
58 // Clear current mod name from registry
60 lua_setfield(L, LUA_REGISTRYINDEX, SCRIPT_MOD_NAME_FIELD);
69 ScriptApiBase::ScriptApiBase()
71 #ifdef SCRIPTAPI_LOCK_DEBUG
75 m_luastack = luaL_newstate();
76 FATAL_ERROR_IF(!m_luastack, "luaL_newstate() failed");
78 luaL_openlibs(m_luastack);
80 // Add and save an error handler
81 lua_pushcfunction(m_luastack, script_error_handler);
82 m_errorhandler = lua_gettop(m_luastack);
84 // Make the ScriptApiBase* accessible to ModApiBase
85 lua_pushlightuserdata(m_luastack, this);
86 lua_setfield(m_luastack, LUA_REGISTRYINDEX, "scriptapi");
88 // If we are using LuaJIT add a C++ wrapper function to catch
89 // exceptions thrown in Lua -> C++ calls
91 lua_pushlightuserdata(m_luastack, (void*) script_exception_wrapper);
92 luaJIT_setmode(m_luastack, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON);
93 lua_pop(m_luastack, 1);
97 lua_newtable(m_luastack);
98 lua_setglobal(m_luastack, "core");
100 lua_pushstring(m_luastack, DIR_DELIM);
101 lua_setglobal(m_luastack, "DIR_DELIM");
103 lua_pushstring(m_luastack, porting::getPlatformName());
104 lua_setglobal(m_luastack, "PLATFORM");
106 // m_secure gets set to true inside
107 // ScriptApiSecurity::initializeSecurity(), if neccessary.
108 // Default to false otherwise
112 m_environment = NULL;
116 ScriptApiBase::~ScriptApiBase()
118 lua_close(m_luastack);
121 bool ScriptApiBase::loadMod(const std::string &script_path,
122 const std::string &mod_name, std::string *error)
124 ModNameStorer mod_name_storer(getStack(), mod_name);
126 return loadScript(script_path, error);
129 bool ScriptApiBase::loadScript(const std::string &script_path, std::string *error)
131 verbosestream << "Loading and running script from " << script_path << std::endl;
133 lua_State *L = getStack();
137 ok = ScriptApiSecurity::safeLoadFile(L, script_path.c_str());
139 ok = !luaL_loadfile(L, script_path.c_str());
141 ok = ok && !lua_pcall(L, 0, 0, m_errorhandler);
143 std::string error_msg = lua_tostring(L, -1);
145 (*error) = error_msg;
146 errorstream << "========== ERROR FROM LUA ===========" << std::endl
147 << "Failed to load and run script from " << std::endl
148 << script_path << ":" << std::endl << std::endl
149 << error_msg << std::endl << std::endl
150 << "======= END OF ERROR FROM LUA ========" << std::endl;
151 lua_pop(L, 1); // Pop error message from stack
157 void ScriptApiBase::realityCheck()
159 int top = lua_gettop(m_luastack);
161 dstream<<"Stack is over 30:"<<std::endl;
163 std::string traceback = script_get_backtrace(m_luastack);
164 throw LuaError("Stack is over 30 (reality check)\n" + traceback);
168 void ScriptApiBase::scriptError()
170 throw LuaError(lua_tostring(m_luastack, -1));
173 void ScriptApiBase::stackDump(std::ostream &o)
176 int top = lua_gettop(m_luastack);
177 for (i = 1; i <= top; i++) { /* repeat for each level */
178 int t = lua_type(m_luastack, i);
181 case LUA_TSTRING: /* strings */
182 o<<"\""<<lua_tostring(m_luastack, i)<<"\"";
185 case LUA_TBOOLEAN: /* booleans */
186 o<<(lua_toboolean(m_luastack, i) ? "true" : "false");
189 case LUA_TNUMBER: /* numbers */ {
191 snprintf(buf, 10, "%g", lua_tonumber(m_luastack, i));
195 default: /* other values */
196 o<<lua_typename(m_luastack, t);
205 void ScriptApiBase::addObjectReference(ServerActiveObject *cobj)
207 SCRIPTAPI_PRECHECKHEADER
208 //infostream<<"scriptapi_add_object_reference: id="<<cobj->getId()<<std::endl;
210 // Create object on stack
211 ObjectRef::create(L, cobj); // Puts ObjectRef (as userdata) on stack
212 int object = lua_gettop(L);
214 // Get core.object_refs table
215 lua_getglobal(L, "core");
216 lua_getfield(L, -1, "object_refs");
217 luaL_checktype(L, -1, LUA_TTABLE);
218 int objectstable = lua_gettop(L);
220 // object_refs[id] = object
221 lua_pushnumber(L, cobj->getId()); // Push id
222 lua_pushvalue(L, object); // Copy object to top of stack
223 lua_settable(L, objectstable);
226 void ScriptApiBase::removeObjectReference(ServerActiveObject *cobj)
228 SCRIPTAPI_PRECHECKHEADER
229 //infostream<<"scriptapi_rm_object_reference: id="<<cobj->getId()<<std::endl;
231 // Get core.object_refs table
232 lua_getglobal(L, "core");
233 lua_getfield(L, -1, "object_refs");
234 luaL_checktype(L, -1, LUA_TTABLE);
235 int objectstable = lua_gettop(L);
237 // Get object_refs[id]
238 lua_pushnumber(L, cobj->getId()); // Push id
239 lua_gettable(L, objectstable);
240 // Set object reference to NULL
241 ObjectRef::set_null(L);
242 lua_pop(L, 1); // pop object
244 // Set object_refs[id] = nil
245 lua_pushnumber(L, cobj->getId()); // Push id
247 lua_settable(L, objectstable);
250 // Creates a new anonymous reference if cobj=NULL or id=0
251 void ScriptApiBase::objectrefGetOrCreate(lua_State *L,
252 ServerActiveObject *cobj)
254 if(cobj == NULL || cobj->getId() == 0){
255 ObjectRef::create(L, cobj);
257 objectrefGet(L, cobj->getId());
261 void ScriptApiBase::objectrefGet(lua_State *L, u16 id)
263 // Get core.object_refs[i]
264 lua_getglobal(L, "core");
265 lua_getfield(L, -1, "object_refs");
266 luaL_checktype(L, -1, LUA_TTABLE);
267 lua_pushnumber(L, id);
269 lua_remove(L, -2); // object_refs
270 lua_remove(L, -2); // core