1
/* ScummVM - Graphic Adventure Engine
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.
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.
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.
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.
27
* This code is based on Broken Sword 2.5 engine
29
* Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
31
* Licensed under GNU GPL v2
35
#include "sword25/kernel/kernel.h"
36
#include "sword25/script/luabindhelper.h"
37
#include "sword25/script/luascript.h"
40
const char *METATABLES_TABLE_NAME = "__METATABLES";
41
const char *PERMANENTS_TABLE_NAME = "Permanents";
43
bool registerPermanent(lua_State *L, const Common::String &name) {
44
// A C function has to be on the stack
45
if (!lua_iscfunction(L, -1))
48
// Make sure that the Permanents-Table is on top of the stack
49
lua_getfield(L, LUA_REGISTRYINDEX, PERMANENTS_TABLE_NAME);
50
if (lua_isnil(L, -1)) {
51
// Permanents-Table does not yet exist, so it has to be created
53
// Pop nil from the stack
56
// Create Permanents-Table and insert a second reference to it on the stack
60
// Store the Permanents-Table in the registry. The second reference is left
61
// on the stack to be used in the connection
62
lua_setfield(L, LUA_REGISTRYINDEX, PERMANENTS_TABLE_NAME);
65
// C function with the name of an index in the Permanents-Table
67
lua_setfield(L, -2, name.c_str());
69
// Remove the Permanents-Table from the stack
79
* Registers a set of functions into a Lua library.
80
* @param L A pointer to the Lua VM
81
* @param LibName The name of the library.
82
* If this is an empty string, the functions will be added to the global namespace.
83
* @param Functions An array of function pointers along with their names.
84
* The array must be terminated with the enry (0, 0)
85
* @return Returns true if successful, otherwise false.
87
bool LuaBindhelper::addFunctionsToLib(lua_State *L, const Common::String &libName, const luaL_reg *functions) {
89
int __startStackDepth = lua_gettop(L);
92
// If the table name is empty, the functions are to be added to the global namespace
93
if (libName.size() == 0) {
94
for (; functions->name; ++functions) {
95
lua_pushstring(L, functions->name);
96
lua_pushcclosure(L, functions->func, 0);
97
lua_settable(L, LUA_GLOBALSINDEX);
99
// Function is being permanently registed, so persistence can be ignored
100
lua_pushstring(L, functions->name);
101
lua_gettable(L, LUA_GLOBALSINDEX);
102
registerPermanent(L, functions->name);
104
} else { // If the table name is not empty, the functions are added to the given table
105
// Ensure that the library table exists
106
if (!createTable(L, libName)) return false;
108
// Register each function into the table
109
for (; functions->name; ++functions) {
110
// Function registration
111
lua_pushstring(L, functions->name);
112
lua_pushcclosure(L, functions->func, 0);
115
// Function is being permanently registed, so persistence can be ignored
116
lua_pushstring(L, functions->name);
118
registerPermanent(L, libName + "." + functions->name);
121
// Remove the library table from the Lua stack
126
assert(__startStackDepth == lua_gettop(L));
133
* Adds a set of constants to the Lua library
134
* @param L A pointer to the Lua VM
135
* @param LibName The name of the library.
136
* If this is an empty string, the functions will be added to the global namespace.
137
* @param Constants An array of the constant values along with their names.
138
* The array must be terminated with the enry (0, 0)
139
* @return Returns true if successful, otherwise false.
141
bool LuaBindhelper::addConstantsToLib(lua_State *L, const Common::String &libName, const lua_constant_reg *constants) {
143
int __startStackDepth = lua_gettop(L);
146
// If the table is empty, the constants are added to the global namespace
147
if (libName.size() == 0) {
148
for (; constants->Name; ++constants) {
149
lua_pushstring(L, constants->Name);
150
lua_pushnumber(L, constants->Value);
151
lua_settable(L, LUA_GLOBALSINDEX);
154
// If the table name is nto empty, the constants are added to that table
156
// Ensure that the library table exists
157
if (!createTable(L, libName)) return false;
159
// Register each constant in the table
160
for (; constants->Name; ++constants) {
161
lua_pushstring(L, constants->Name);
162
lua_pushnumber(L, constants->Value);
166
// Remove the library table from the Lua stack
171
assert(__startStackDepth == lua_gettop(L));
178
* Adds a set of methods to a Lua class
179
* @param L A pointer to the Lua VM
180
* @param ClassName The name of the class
181
* When the class name specified does not exist, it is created.
182
* @param Methods An array of function pointers along with their method names.
183
* The array must be terminated with the enry (0, 0)
184
* @return Returns true if successful, otherwise false.
186
bool LuaBindhelper::addMethodsToClass(lua_State *L, const Common::String &className, const luaL_reg *methods) {
188
int __startStackDepth = lua_gettop(L);
191
// Load the metatable onto the Lua stack
192
if (!getMetatable(L, className)) return false;
194
// Register each method in the Metatable
195
for (; methods->name; ++methods) {
196
lua_pushstring(L, methods->name);
197
lua_pushcclosure(L, methods->func, 0);
200
// Function is being permanently registed, so persistence can be ignored
201
lua_pushstring(L, methods->name);
203
registerPermanent(L, className + "." + methods->name);
206
// Remove the metatable from the stack
210
assert(__startStackDepth == lua_gettop(L));
217
* Sets the garbage collector callback method when items of a particular class are deleted
218
* @param L A pointer to the Lua VM
219
* @param ClassName The name of the class
220
* When the class name specified does not exist, it is created.
221
* @param GCHandler A function pointer
222
* @return Returns true if successful, otherwise false.
224
bool LuaBindhelper::setClassGCHandler(lua_State *L, const Common::String &className, lua_CFunction GCHandler) {
226
int __startStackDepth = lua_gettop(L);
229
// Load the metatable onto the Lua stack
230
if (!getMetatable(L, className)) return false;
232
// Add the GC handler to the Metatable
233
lua_pushstring(L, "__gc");
234
lua_pushcclosure(L, GCHandler, 0);
237
// Function is being permanently registed, so persistence can be ignored
238
lua_pushstring(L, "__gc");
240
registerPermanent(L, className + ".__gc");
242
// Remove the metatable from the stack
246
assert(__startStackDepth == lua_gettop(L));
252
} // End of namespace Sword25
255
void pushMetatableTable(lua_State *L) {
256
// Push the Metatable table onto the stack
257
lua_getglobal(L, METATABLES_TABLE_NAME);
259
// If the table doesn't yet exist, it must be created
260
if (lua_isnil(L, -1)) {
261
// Pop nil from stack
264
// New table has been created, so add it to the global table and leave reference on stack
266
lua_pushvalue(L, -1);
267
lua_setglobal(L, METATABLES_TABLE_NAME);
274
bool LuaBindhelper::getMetatable(lua_State *L, const Common::String &tableName) {
275
// Push the Metatable table onto the stack
276
pushMetatableTable(L);
278
// Versuchen, die gew�nschte Metatabelle auf den Stack zu legen. Wenn sie noch nicht existiert, muss sie erstellt werden.
279
lua_getfield(L, -1, tableName.c_str());
280
if (lua_isnil(L, -1)) {
281
// Pop nil from stack
287
// Set the __index field in the table
288
lua_pushvalue(L, -1);
289
lua_setfield(L, -2, "__index");
291
// Flag the table as persisted. This ensures that objects within this table get stored
292
lua_pushbooleancpp(L, true);
293
lua_setfield(L, -2, "__persist");
295
// Set the table name and push it onto the stack
296
lua_pushvalue(L, -1);
297
lua_setfield(L, -3, tableName.c_str());
300
// Remove the Metatable table from the stack
306
// Like luaL_checkudata, only without that no error is generated.
307
void *LuaBindhelper::my_checkudata(lua_State *L, int ud, const char *tname) {
308
int top = lua_gettop(L);
310
void *p = lua_touserdata(L, ud);
311
if (p != NULL) { /* value is a userdata? */
312
if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
313
// lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
314
LuaBindhelper::getMetatable(L, tname);
315
if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */
327
bool LuaBindhelper::createTable(lua_State *L, const Common::String &tableName) {
328
const char *partBegin = tableName.c_str();
330
while (partBegin - tableName.c_str() < (int)tableName.size()) {
331
const char *partEnd = strchr(partBegin, '.');
333
partEnd = partBegin + strlen(partBegin);
334
Common::String subTableName(partBegin, partEnd);
336
// Tables with an empty string as the name are not allowed
337
if (subTableName.size() == 0)
340
// Verify that the table with the name already exists
341
// The first round will be searched in the global namespace, with later passages
342
// in the corresponding parent table in the stack
343
if (partBegin == tableName.c_str()) {
344
lua_pushstring(L, subTableName.c_str());
345
lua_gettable(L, LUA_GLOBALSINDEX);
347
lua_pushstring(L, subTableName.c_str());
349
if (!lua_isnil(L, -1))
353
// If it doesn't exist, create table
354
if (lua_isnil(L, -1)) {
355
// Pop nil from stack
360
lua_pushstring(L, subTableName.c_str());
361
lua_pushvalue(L, -2);
362
if (partBegin == tableName.c_str())
363
lua_settable(L, LUA_GLOBALSINDEX);
370
partBegin = partEnd + 1;
376
} // End of namespace Sword25
379
Common::String getLuaValueInfo(lua_State *L, int stackIndex) {
380
switch (lua_type(L, stackIndex)) {
382
lua_pushstring(L, lua_tostring(L, stackIndex));
386
lua_pushfstring(L, "\"%s\"", lua_tostring(L, stackIndex));
390
lua_pushstring(L, (lua_toboolean(L, stackIndex) ? "true" : "false"));
394
lua_pushliteral(L, "nil");
398
lua_pushfstring(L, "%s: %p", luaL_typename(L, stackIndex), lua_topointer(L, stackIndex));
402
Common::String result(lua_tostring(L, -1));
411
Common::String LuaBindhelper::stackDump(lua_State *L) {
414
int i = lua_gettop(L);
415
oss += "------------------- Stack Dump -------------------\n";
418
oss += i + ": " + getLuaValueInfo(L, i) + "\n";
422
oss += "-------------- Stack Dump Finished ---------------\n";
427
Common::String LuaBindhelper::tableDump(lua_State *L) {
430
oss += "------------------- Table Dump -------------------\n";
433
while (lua_next(L, -2) != 0) {
434
// Get the value of the current element on top of the stack, including the index
435
oss += getLuaValueInfo(L, -2) + " : " + getLuaValueInfo(L, -1) + "\n";
437
// Pop value from the stack. The index is then ready for the next call to lua_next()
441
oss += "-------------- Table Dump Finished ---------------\n";
446
} // End of namespace Sword25