aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCourtney Bane <git-1691@cbane.org>2017-01-25 18:04:35 -0600
committerCourtney Bane <git-1691@cbane.org>2017-01-25 18:04:35 -0600
commitea0064625b2a4cf279977477fe2ca627afbd5ad0 (patch)
tree6ac59aa0fd0dc74d3d78cc989fa4973317a015c4
parent843fe9b65fd93b015e5fe3e457c98fb806cd8c79 (diff)
downloadluasocket-ea0064625b2a4cf279977477fe2ca627afbd5ad0.tar.gz
luasocket-ea0064625b2a4cf279977477fe2ca627afbd5ad0.tar.bz2
luasocket-ea0064625b2a4cf279977477fe2ca627afbd5ad0.zip
Add backwards compatibility wrappers for socket.unix
Add backwards compatibility aliases "tcp" and "udp" for the recently renamed "stream" and "dgram" functions, as well as a wrapper function and metatable setup so that socket.unix() calls socket.unix.stream().
-rw-r--r--src/unix.c50
1 files changed, 45 insertions, 5 deletions
diff --git a/src/unix.c b/src/unix.c
index e604733..dbc8710 100644
--- a/src/unix.c
+++ b/src/unix.c
@@ -17,14 +17,54 @@ static const luaL_Reg mod[] = {
17 {NULL, NULL} 17 {NULL, NULL}
18}; 18};
19 19
20static void add_alias(lua_State *L, int index, const char *name, const char *target)
21{
22 lua_getfield(L, index, target);
23 lua_setfield(L, index, name);
24}
25
26static int compat_socket_unix_call(lua_State *L)
27{
28 /* Look up socket.unix.stream in the socket.unix table (which is the first
29 * argument). */
30 lua_getfield(L, 1, "stream");
31
32 /* Replace the stack entry for the socket.unix table with the
33 * socket.unix.stream function. */
34 lua_replace(L, 1);
35
36 /* Call socket.unix.stream, passing along any arguments. */
37 int n = lua_gettop(L);
38 lua_call(L, n-1, LUA_MULTRET);
39
40 /* Pass along the return values from socket.unix.stream. */
41 n = lua_gettop(L);
42 return n;
43}
44
20/*-------------------------------------------------------------------------*\ 45/*-------------------------------------------------------------------------*\
21* Initializes module 46* Initializes module
22\*-------------------------------------------------------------------------*/ 47\*-------------------------------------------------------------------------*/
23int luaopen_socket_unix(lua_State *L) 48int luaopen_socket_unix(lua_State *L)
24{ 49{
25 int i; 50 int i;
26 lua_newtable(L); 51 lua_newtable(L);
27 for (i = 0; mod[i].name; i++) mod[i].func(L); 52 int socket_unix_table = lua_gettop(L);
28 return 1; 53
29} 54 for (i = 0; mod[i].name; i++)
55 mod[i].func(L);
30 56
57 /* Add backwards compatibility aliases "tcp" and "udp" for the "stream" and
58 * "dgram" functions. */
59 add_alias(L, socket_unix_table, "tcp", "stream");
60 add_alias(L, socket_unix_table, "udp", "dgram");
61
62 /* Add a backwards compatibility function and a metatable setup to call it
63 * for the old socket.unix() interface. */
64 lua_pushcfunction(L, compat_socket_unix_call);
65 lua_setfield(L, socket_unix_table, "__call");
66 lua_pushvalue(L, socket_unix_table);
67 lua_setmetatable(L, socket_unix_table);
68
69 return 1;
70}