-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLuaHelp.cpp
More file actions
72 lines (58 loc) · 1.36 KB
/
LuaHelp.cpp
File metadata and controls
72 lines (58 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "LuaHelp.h"
#include "LuaBind.h"
namespace Aurora
{
int lua_setuserdata_metatable() {
lua_State* L = GetLuaState();
//if(!lua_isuserdata(L, -2)) LUA_ERROR(L, "");
//if(!lua_istable(L, -1)) LUA_ERROR(L, "");
lua_pushvalue(L, -2);
lua_pushvalue(L, -2);
return lua_setmetatable(L, -2);
}
static const char* _lua_pop_string(lua_State* L)
{
lua_pop(L, 1);
const char* str = lua_tostring(L, 0);
return str;
}
static int _lua_handle_error(lua_State* L)
{
lua_getglobal(L, "debug");
if(!lua_istable(L, -1))
{
lua_pop(L, 1);
return 1;
}
lua_getfield(L, -1, "traceback");
if(!lua_isfunction(L, -1))
{
lua_pop(L, 2);
return 1;
}
lua_pushvalue(L, 1);
lua_pushinteger(L, 2);
lua_call(L, 2, 1);
return 1;
}
int _lua_call(lua_State* L, int params_num, int result_count, int /*n*/)
{
int size = lua_gettop(L);
int handler = lua_gettop(L) - params_num;
lua_pushcfunction(L, _lua_handle_error);
lua_insert(L, handler);
int ret = 0;
if(lua_pcall(L, params_num, result_count, handler))
{
ret = LUA_ERRRUN;
LUA_ERROR(L, _lua_pop_string(L));
}
lua_remove(L, handler);
if(((lua_gettop(L) + (int)params_num + 1 - result_count) != size))
{
LUA_ERROR(L, "Lua error: lua stack size changed!");
}
return ret;
}
//////////////////////////////////////////////////////////////////////////
}