1 /* ScummVM - Graphic Adventure Engine 2 * 3 * ScummVM is the legal property of its developers, whose names 4 * are too numerous to list here. Please refer to the COPYRIGHT 5 * file distributed with this source distribution. 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License 9 * as published by the Free Software Foundation; either version 2 10 * of the License, or (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 * 21 */ 22 23 /* 24 * This code is based on Broken Sword 2.5 engine 25 * 26 * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer 27 * 28 * Licensed under GNU GPL v2 29 * 30 */ 31 32 #ifndef SWORD25_LUABINDHELPER_H 33 #define SWORD25_LUABINDHELPER_H 34 35 #include "sword25/kernel/common.h" 36 37 #include "common/lua/lua.h" 38 #include "common/lua/lauxlib.h" 39 40 namespace Sword25 { 41 42 #define lua_pushbooleancpp(L, b) (lua_pushboolean(L, b ? 1 : 0)) 43 #define lua_tobooleancpp(L, i) (lua_toboolean(L, i) != 0) 44 45 struct lua_constant_reg { 46 const char *Name; 47 lua_Number Value; 48 }; 49 50 class LuaBindhelper { 51 public: 52 /** 53 * Registers a set of functions into a Lua library. 54 * @param L A pointer to the Lua VM 55 * @param LibName The name of the library. 56 * If this is an empty string, the functions will be added to the global namespace. 57 * @param Functions An array of function pointers along with their names. 58 * The array must be terminated with the enry (0, 0) 59 * @return Returns true if successful, otherwise false. 60 */ 61 static bool addFunctionsToLib(lua_State *L, const Common::String &libName, const luaL_reg *functions); 62 63 /** 64 * Adds a set of constants to the Lua library 65 * @param L A pointer to the Lua VM 66 * @param LibName The name of the library. 67 * If this is an empty string, the functions will be added to the global namespace. 68 * @param Constants An array of the constant values along with their names. 69 * The array must be terminated with the enry (0, 0) 70 * @return Returns true if successful, otherwise false. 71 */ 72 static bool addConstantsToLib(lua_State *L, const Common::String &libName, const lua_constant_reg *constants); 73 74 /** 75 * Adds a set of methods to a Lua class 76 * @param L A pointer to the Lua VM 77 * @param ClassName The name of the class 78 * When the class name specified does not exist, it is created. 79 * @param Methods An array of function pointers along with their method names. 80 * The array must be terminated with the enry (0, 0) 81 * @return Returns true if successful, otherwise false. 82 */ 83 static bool addMethodsToClass(lua_State *L, const Common::String &className, const luaL_reg *methods); 84 85 /** 86 * Sets the garbage collector callback method when items of a particular class are deleted 87 * @param L A pointer to the Lua VM 88 * @param ClassName The name of the class 89 * When the class name specified does not exist, it is created. 90 * @param GCHandler A function pointer 91 * @return Returns true if successful, otherwise false. 92 */ 93 static bool setClassGCHandler(lua_State *L, const Common::String &className, lua_CFunction GCHandler); 94 95 /** 96 * Returns a string containing a stack dump of the Lua stack 97 * @param L A pointer to the Lua VM 98 */ 99 static Common::String stackDump(lua_State *L); 100 101 /** 102 * Returns a string that describes the contents of a table 103 * @param L A pointer to the Lua VM 104 * @remark The table must be on the Lua stack to be read out. 105 */ 106 static Common::String tableDump(lua_State *L); 107 108 static bool getMetatable(lua_State *L, const Common::String &tableName); 109 110 static void *my_checkudata(lua_State *L, int ud, const char *tname); 111 112 private: 113 static bool createTable(lua_State *L, const Common::String &tableName); 114 }; 115 116 } // End of namespace Sword25 117 118 #endif 119