1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com> |
1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com> |
2 * Licensed as LGPL - See doc/COPYING for details */ |
2 * Licensed as LGPL - See doc/COPYING for details */ |
3 #include "luaevent.h" |
3 |
|
4 #include "luaevent.h" |
4 |
5 |
5 #include <lua.h> |
6 #include <lua.h> |
6 #include <lauxlib.h> |
7 #include <lauxlib.h> |
7 #include <assert.h> |
8 #include <assert.h> |
8 |
9 |
9 #define EVENT_BASE_MT "EVENT_BASE_MT" |
10 #define EVENT_BASE_MT "EVENT_BASE_MT" |
10 #define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT" |
11 #define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT" |
11 #define MAIN_THREAD_LOCATION 1 |
|
12 |
|
13 void setMainThread(lua_State* L) { |
|
14 lua_pushthread(L); |
|
15 lua_rawseti(L, LUA_ENVIRONINDEX, MAIN_THREAD_LOCATION); |
|
16 } |
|
17 lua_State* getMainThread(lua_State* L) { |
|
18 lua_State* g_L; |
|
19 lua_rawgeti(L, LUA_ENVIRONINDEX, MAIN_THREAD_LOCATION); |
|
20 g_L = lua_tothread(L, -1); |
|
21 lua_pop(L, 1); |
|
22 return g_L; |
|
23 } |
|
24 |
12 |
25 int luaevent_newbase(lua_State* L) { |
13 int luaevent_newbase(lua_State* L) { |
26 le_base *base = (le_base*)lua_newuserdata(L, sizeof(le_base)); |
14 le_base *base = (le_base*)lua_newuserdata(L, sizeof(le_base)); |
27 base->loop_L = NULL; /* No running loop */ |
15 base->loop_L = NULL; /* No running loop */ |
28 base->base = event_init(); |
16 base->base = event_init(); |
156 } |
144 } |
157 } |
145 } |
158 |
146 |
159 /* Verified ok */ |
147 /* Verified ok */ |
160 int luaopen_luaevent_core(lua_State* L) { |
148 int luaopen_luaevent_core(lua_State* L) { |
161 /* Setup environ table */ |
|
162 lua_createtable(L, 1, 0); |
|
163 lua_replace(L, LUA_ENVIRONINDEX); |
|
164 /* Setup metatable */ |
149 /* Setup metatable */ |
165 luaL_newmetatable(L, EVENT_BASE_MT); |
150 luaL_newmetatable(L, EVENT_BASE_MT); |
166 lua_newtable(L); |
151 lua_newtable(L); |
167 luaL_register(L, NULL, base_funcs); |
152 luaL_register(L, NULL, base_funcs); |
168 lua_setfield(L, -2, "__index"); |
153 lua_setfield(L, -2, "__index"); |