1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 
4 #include "LuaScream.h"
5 
6 #include "LuaInclude.h"
7 
8 #include "LuaHashString.h"
9 
10 #include "System/Log/ILog.h"
11 
12 
13 /******************************************************************************/
14 /******************************************************************************/
15 
LuaScream()16 LuaScream::LuaScream()
17 {
18 }
19 
20 
~LuaScream()21 LuaScream::~LuaScream()
22 {
23 }
24 
25 
26 /******************************************************************************/
27 /******************************************************************************/
28 
PushEntries(lua_State * L)29 bool LuaScream::PushEntries(lua_State* L)
30 {
31 	CreateMetatable(L);
32 
33 #define REGISTER_LUA_CFUNC(x) \
34 	lua_pushstring(L, #x);      \
35 	lua_pushcfunction(L, x);    \
36 	lua_rawset(L, -3)
37 
38 	REGISTER_LUA_CFUNC(CreateScream);
39 
40 	return true;
41 }
42 
43 
44 /******************************************************************************/
45 /******************************************************************************/
46 
CreateMetatable(lua_State * L)47 bool LuaScream::CreateMetatable(lua_State* L)
48 {
49 	luaL_newmetatable(L, "Scream");
50 	HSTR_PUSH_CFUNC(L, "__gc",        meta_gc);
51 	HSTR_PUSH_CFUNC(L, "__index",     meta_index);
52 	HSTR_PUSH_CFUNC(L, "__newindex",  meta_newindex);
53 	lua_pop(L, 1);
54 	return true;
55 }
56 
57 
meta_gc(lua_State * L)58 int LuaScream::meta_gc(lua_State* L)
59 {
60 	int* refPtr = (int*)luaL_checkudata(L, 1, "Scream");
61 	lua_rawgeti(L, LUA_REGISTRYINDEX, *refPtr);
62 	if (lua_isfunction(L, -1)) {
63 		const int error = lua_pcall(L, 0, 0, 0);
64 		if (error != 0) {
65 			LOG_L(L_ERROR, "Scream: error(%i) = %s",
66 					error, lua_tostring(L, -1));
67 			lua_pop(L, 1);
68 		}
69 	}
70 	else if (lua_isstring(L, -1)) {
71 		LOG("SCREAM: %s", lua_tostring(L, -1));
72 	}
73 	luaL_unref(L, LUA_REGISTRYINDEX, *refPtr);
74 	return 0;
75 }
76 
77 
meta_index(lua_State * L)78 int LuaScream::meta_index(lua_State* L)
79 {
80 	int* refPtr = (int*)luaL_checkudata(L, 1, "Scream");
81 	const string key = luaL_checkstring(L, 2);
82 	if (key == "func") {
83 		lua_rawgeti(L, LUA_REGISTRYINDEX, *refPtr);
84 		return 1;
85 	}
86 	return 0;
87 }
88 
89 
meta_newindex(lua_State * L)90 int LuaScream::meta_newindex(lua_State* L)
91 {
92 	int* refPtr = (int*)luaL_checkudata(L, 1, "Scream");
93 	const string key = luaL_checkstring(L, 2);
94 	if (key == "func") {
95 		lua_pushvalue(L, 3);
96 		lua_rawseti(L, LUA_REGISTRYINDEX, *refPtr);
97 	}
98 	return 0;
99 }
100 
101 
102 /******************************************************************************/
103 /******************************************************************************/
104 
CreateScream(lua_State * L)105 int LuaScream::CreateScream(lua_State* L)
106 {
107 	int* refPtr = (int*)lua_newuserdata(L, sizeof(int));
108 	luaL_getmetatable(L, "Scream");
109 	lua_setmetatable(L, -2);
110 
111 	lua_pushvalue(L, 1);
112 	*refPtr = luaL_ref(L, LUA_REGISTRYINDEX);
113 
114 	return 1;
115 }
116 
117 
118 /******************************************************************************/
119 /******************************************************************************/
120