-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlshared.cpp
52 lines (46 loc) · 1.14 KB
/
lshared.cpp
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
#include "lshared.h"
int shared_t::meta_index(lua_State *L)
{
shared_t** shared = (shared_t**)luaL_checkudata(L, 1, SHARED_METATABLE);
if (*shared) {
const char* name = lua_tostring(L, 2);
if (name) {
lua_CFunction func = (*shared)->index_function(name);
if (func) {
lua_pushcfunction(L, func);
return 1;
}
}
}
return 0;
}
int shared_t::meta_gc(lua_State *L)
{
shared_t** shared = (shared_t**)luaL_checkudata(L, 1, SHARED_METATABLE);
if (*shared) {
(*shared)->release();
*shared = NULL;
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
shared_t** shared_t::create(lua_State* L)
{
shared_t** ret = (shared_t**)lua_newuserdata(L, sizeof(shared_t*));
*ret = NULL;
if (luaL_newmetatable(L, SHARED_METATABLE)) { /* create new metatable */
lua_pushcclosure(L, meta_index, 0);
lua_setfield(L, -2, "__index");
lua_pushcclosure(L, meta_gc, 0);
lua_setfield(L, -2, "__gc");
}
lua_setmetatable(L, -2);
return ret;
}
shared_t** shared_t::create(lua_State* L, shared_t* shared)
{
shared_t** ret = create(L);
shared->grab();
*ret = shared;
return ret;
}