7 #include "connections.h"
8 #include "lua_interface.h"
11 #define CONNECTION_META_TABLE "MetaConnection"
12 #define CONNECTION_TABLE "Connection"
13 #define CONNECTION_TABLE_UD_FIELD "core_"
16 * HARD: shared common server state, updatable commands table
17 * Interim: independent states, any global changes queued to all workers
18 * Idea: can a proxy table be crafted to access a shared table-like c entity
19 * Idea: eventual-consistency between lua states (somehow)
25 * Create a new referenced coroutine on lua state.
27 * luaL_unref(L, LUA_REGISTRYINDEX, refkey);
30 lua_new_coroutine_ref(lua_State
*L
, lua_State
**coL
, int *refkey
)
34 /* Create new coroutine on top of stack. */
35 *coL
= lua_newthread(L
);
37 NOTIFY_ERROR("%s:%s", "lua_newthread", "failed");
41 /* Store coroutine reference in state registry. */
42 *refkey
= luaL_ref(L
, LUA_REGISTRYINDEX
);
50 * is a table with metatable for methods and userdata field
56 * Add a connection reference to lua stack
60 lemu_connection_push(lua_State
*L
, struct connection
*c
)
62 connection_inc_ref(c
);
64 lua_newtable(L
); // new connection table
65 luaL_getmetatable(L
, CONNECTION_META_TABLE
); // locate meta
66 lua_setmetatable(L
, -2); // assign meta to connection table
68 /* keeping a pointer in userdata, rather than light userdata, to get metatable support */
69 struct connection
**cud
= lua_newuserdatauv(L
, sizeof c
, 0);
71 luaL_getmetatable(L
, CONNECTION_META_TABLE
);
72 lua_setmetatable(L
, -2);
74 lua_setfield(L
, -2, CONNECTION_TABLE_UD_FIELD
);
80 * I guess this one would take a stack value to find a connection
83 lemu_connection_create_(lua_State
*L
)
87 luaL_checktype(L
, 1, LUA_TSTRING
);
88 // c = connection_lookup(lua_tostring(L, 1));
91 return lemu_connection_push(L
, c
);
96 * this will only get called on userdata (how does that happen??)
99 lemu_connection_destroy_(lua_State
*L
)
101 struct connection
**cud
= luaL_checkudata(L
, 1, CONNECTION_META_TABLE
);
102 struct connection
*c
= *cud
;
111 * Send a fixed string to a connection.
114 lemu_connection_send_(lua_State
*L
)
116 if (lua_gettop(L
) < 2) {
117 lua_pushliteral(L
, "too few arguments");
120 luaL_checktype(L
, 1, LUA_TTABLE
);
121 lua_getfield(L
, 1, CONNECTION_TABLE_UD_FIELD
);
122 luaL_checktype(L
, -1, LUA_TUSERDATA
);
123 struct connection
**cud
= lua_touserdata(L
, -1);
124 struct connection
*c
= *cud
;
126 luaL_checktype(L
, 2, LUA_TSTRING
);
127 const char *message
= lua_tostring(L
, 2);
129 connection_printf(c
, "%s\n", message
);
135 static const luaL_Reg Connection_funcs
[] = {
136 { "create", lemu_connection_create_
},
141 static const luaL_Reg Connection_methods
[] = {
142 { "__gc", lemu_connection_destroy_
},
143 { "send", lemu_connection_send_
},
149 * Initialize the Connection object prototypes
152 lemu_connection_luainit(lua_State
*L
)
154 luaL_newmetatable(L
, CONNECTION_META_TABLE
); // new userdata metatable, __name = CONNECTION_META_TABLE
155 lua_pushstring(L
, "__index");
156 lua_pushvalue(L
, -2); // meta table
157 lua_settable(L
, -3); // becomes its own index table
158 luaL_setfuncs(L
, Connection_methods
, 0); // add methods to metatable
161 luaL_newlibtable(L
, Connection_funcs
); // new table with functions
162 luaL_setfuncs(L
, Connection_funcs
, 0);
163 lua_setglobal(L
, CONNECTION_TABLE
); // make it available
168 // int luaopen_lemu(lua_State *L);