-
Notifications
You must be signed in to change notification settings - Fork 0
/
gelib.c
202 lines (198 loc) · 5.1 KB
/
gelib.c
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "gelib.h"
int index_self(lua_State* L,const char* classname)
{
lua_pushstring(L,classname);
lua_gettable(L,LUA_REGISTRYINDEX);/* t = registry[classname] */
if(lua_isnil(L,-1)){
char buf[1024] = {0};
sprintf(buf,"Not found register[%s].",classname);
lua_pushstring(L,buf);
lua_error(L);
}
lua_pushvalue(L,-1);
lua_setfield(L,-2,"__index"); /* t.__index = t */
lua_pop(L,1);
return 0;
}
static int glibHandle_gc(lua_State* L)
{
Ghandle* gh = checkhandle(L,1);
if(gh->flag){
/*
lua_rawgeti(L,LUA_REGISTRYINDEX,gh->ref);
lua_pushstring(L,"ghandle");
lua_pushnil(L);
lua_settable(L,-3);
lua_pop(L,1);
*/
luaL_unref(L,LUA_REGISTRYINDEX,gh->ref);
gh->flag = 0;
}
if(gh->gc)
gh->gc(gh);
return 0;
}
int new_class(lua_State* L,const char* classname)
{
lua_newtable(L); /* push t */
lua_pushstring(L,"class"); /* push key="class" */
lua_pushstring(L,classname); /* push value = classname */
lua_settable(L,-3); /* t.class = class_name */
//lua_pushstring(L,classname); /* push key = classname */
lua_setfield(L,LUA_REGISTRYINDEX,classname); /* registry[classname] = t */
return 0;
}
/* userdata or table on stack */
int set_class(lua_State* L,const char* classname)
{
if(lua_istable(L,-1) || lua_isuserdata(L,-1)){
lua_pushstring(L,classname);
lua_gettable(L,LUA_REGISTRYINDEX);/* t2 = registry[classname] */
if(lua_isnil(L,-1)){
lua_pushstring(L,"Invalid class name.");
lua_error(L);
}
lua_setmetatable(L,-2); /* metatable(t) = t2 */
}
else{
lua_pushstring(L,"not userdata or table");
lua_error(L);
}
return 0;
}
int set_method(lua_State* L,const char* classname,const char* funcname,lua_CFunction f)
{
lua_pushstring(L,classname);
lua_gettable(L,LUA_REGISTRYINDEX);
if(lua_isnil(L,-1)){
lua_pushstring(L,"Invalid class name.");
lua_error(L);
}
lua_pushstring(L,funcname);
lua_pushcfunction(L,f);
lua_settable(L,-3);
return 0;
}
/*
iup.NewClass("iupHandle") -- this is an Ihandle* with enhancements
iup.NewClass("iupWidget") -- this is a Lua object for control construction, see iup.WIDGET and iup.BOX
*/
static int NewClass(lua_State *L)
{
const char* classname = lua_tostring(L,1);
lua_newtable(L); /* push t */
lua_pushstring(L, "class"); /* push "class" */
lua_pushvalue(L, 1); /* push again the class_name to the stack, because settable will remove it */
lua_settable(L, -3); /* t.class = class_name */
lua_settable(L, LUA_REGISTRYINDEX); /* registry[class_name] = t */
index_self(L,classname);
//set_method(L,classname,"__gc",glibHandle_gc);
return 0;
}
/* iup.SetClass(t, class_name)
* Calls:
* iup.SetClass(ih, "iupHandle") --Called only in iup.RegisterHandle and WIDGET.constructor
* iup.SetClass(widget, "iupWidget") --Called whenever a new control class is created.
* */
static int SetClass(lua_State *L)
{
lua_gettable(L, LUA_REGISTRYINDEX); /* t2 = registry[class_name] */
if (lua_isnil(L, -1)) {
lua_pushstring(L, "Invalid class name.");
lua_error(L);
}
lua_setmetatable(L, -2); /* metatable(t) = t2 */
return 0;
}
/* class_name = iup.GetClass(t) */
static int GetClass(lua_State *L)
{
if (lua_istable(L, 1) || lua_isuserdata(L, 1))
{
lua_getmetatable(L, 1); /* t2 = metatable(t) */
if (lua_istable(L, -1))
{
lua_pushstring(L, "class");
lua_gettable(L, -2); /* class_name = t2.class */
return 1;
}
}
lua_pushnil(L);
return 1;
}
/* iup.SetMethod(class_name, method, function)
* For ex:
* iup.SetMethod("iupHandle", "__index", ihandle_gettable)
* */
static int SetMethod(lua_State *L)
{
lua_pushvalue(L, 1); /* push class_name */
lua_gettable(L, LUA_REGISTRYINDEX); /* t = registry[class_name] */
if (lua_isnil(L, -1)) {
lua_pushstring(L, "Invalid class name.");
lua_error(L);
}
lua_pushvalue(L, -3); /* push method */
lua_pushvalue(L, -3); /* push function */
lua_settable(L, -3); /* t.method = function */
return 0;
}
int get_link(lua_State* L)
{
Ghandle* gh = checkhandle(L,-1);
if(gh->flag)
lua_rawgeti(L,LUA_REGISTRYINDEX,gh->ref);
else
lua_pushnil(L);
return 0;
}
/* local link = glib.GetLink(gh) */
static int GetLink(lua_State* L)
{
Ghandle* gh = checkhandle(L,1);
if(gh->flag)
lua_rawgeti(L,LUA_REGISTRYINDEX,gh->ref);
else
lua_pushnil(L);
return 1;
}
/* glib.SetLink(gh,link) */
static int SetLink(lua_State* L)
{
Ghandle* gh = checkhandle(L,1);
if(!gh->flag){
gh->flag = 1;
gh->ref = luaL_ref(L,LUA_REGISTRYINDEX);
}
return 0;
}
static int new_ghandle(lua_State* L)
{
void* handle = lua_touserdata(L,1);
DESTROY_CB gc = (DESTROY_CB)lua_touserdata(L,2);
Ghandle* gh = (Ghandle*)lua_newuserdata(L,sizeof(Ghandle));
gh->handle = handle;
gh->gc = gc;
gh->flag = 0;
gh->ref = 0;
set_class(L,GLIB_HANDLE_NAME);
return 1;
}
static const struct luaL_Reg luaglib_m[] ={
{"NewClass",NewClass},
{"SetClass",SetClass},
{"GetClass",GetClass},
{"SetMethod",SetMethod},
{"GetLink",GetLink},
{"SetLink",SetLink},
{"Ghandle",new_ghandle},
{NULL,NULL}
};
int luaopen_luaext_glib(lua_State* L)
{
new_class(L,GLIB_HANDLE_NAME);
index_self(L,GLIB_HANDLE_NAME);
set_method(L,GLIB_HANDLE_NAME,"__gc",glibHandle_gc);
luaL_newlib(L,luaglib_m);
return 1;
}