diff options
author | Emil Renner Berthing <esmil@mailme.dk> | 2012-12-11 23:59:37 +0100 |
---|---|---|
committer | Emil Renner Berthing <esmil@mailme.dk> | 2012-12-17 10:11:06 +0100 |
commit | f25118928aa35861b370a6529c651a28f5c8859b (patch) | |
tree | 63f07f33cd207f947ea43bef5d92f5311b2e758b /lem/io/core.c | |
parent | b738f523cc7a56602e07ff54bd11203355a64af9 (diff) | |
download | lem-f25118928aa35861b370a6529c651a28f5c8859b.tar.gz lem-f25118928aa35861b370a6529c651a28f5c8859b.tar.xz lem-f25118928aa35861b370a6529c651a28f5c8859b.zip |
io: rework streams
Diffstat (limited to 'lem/io/core.c')
-rw-r--r-- | lem/io/core.c | 122 |
1 files changed, 42 insertions, 80 deletions
diff --git a/lem/io/core.c b/lem/io/core.c index da3241e..5f964be 100644 --- a/lem/io/core.c +++ b/lem/io/core.c @@ -20,6 +20,7 @@ #include <unistd.h> #include <errno.h> #include <string.h> +#include <assert.h> #include <fcntl.h> #include <sys/stat.h> @@ -83,16 +84,12 @@ module_index(lua_State *T) return 2; } - if (fd == 0) - (void)istream_new(T, fd, lua_upvalueindex(1)); - else - (void)ostream_new(T, fd, lua_upvalueindex(2)); + stream_new(T, fd, lua_upvalueindex(1)); /* save this object so we don't initialize it again */ lua_pushvalue(T, 2); lua_pushvalue(T, -2); lua_rawset(T, 1); - return 1; } @@ -124,52 +121,49 @@ luaopen_lem_io_core(lua_State *L) lua_pushcclosure(L, sendfile_open, 1); lua_setfield(L, -2, "sendfile"); - /* create metatable for input stream objects */ + /* create File metatable */ lua_newtable(L); /* mt.__index = mt */ lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); - /* mt.__gc = <stream_close> */ - lua_pushcfunction(L, istream_close); + /* mt.__gc = <file_gc> */ + lua_pushcfunction(L, file_gc); lua_setfield(L, -2, "__gc"); - /* mt.closed = <stream_closed> */ - lua_pushcfunction(L, stream_closed); + /* mt.closed = <file_closed> */ + lua_pushcfunction(L, file_closed); lua_setfield(L, -2, "closed"); - /* mt.busy = <stream_busy> */ - lua_pushcfunction(L, stream_busy); - lua_setfield(L, -2, "busy"); - /* mt.interrupt = <stream_interrupt> */ - lua_pushcfunction(L, stream_interrupt); - lua_setfield(L, -2, "interrupt"); - /* mt.close = <istream_close> */ - lua_pushcfunction(L, istream_close); + /* mt.close = <file_close> */ + lua_pushcfunction(L, file_close); lua_setfield(L, -2, "close"); - /* mt.readp = <stream_readp> */ - lua_pushcfunction(L, stream_readp); + /* mt.readp = <file_readp> */ + lua_pushcfunction(L, file_readp); lua_setfield(L, -2, "readp"); + /* mt.write = <file_write> */ + lua_pushcfunction(L, file_write); + lua_setfield(L, -2, "write"); + /* mt.seek = <file_seek> */ + lua_pushcfunction(L, file_seek); + lua_setfield(L, -2, "seek"); /* insert table */ - lua_setfield(L, -2, "IStream"); + lua_setfield(L, -2, "File"); - /* create metatable for output stream objects */ + /* create Stream metatable */ lua_newtable(L); /* mt.__index = mt */ lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); - /* mt.__gc = <ostream_close> */ - lua_pushcfunction(L, ostream_close); + /* mt.__gc = <stream_gc> */ + lua_pushcfunction(L, stream_gc); lua_setfield(L, -2, "__gc"); /* mt.closed = <stream_closed> */ lua_pushcfunction(L, stream_closed); lua_setfield(L, -2, "closed"); - /* mt.busy = <stream_busy> */ - lua_pushcfunction(L, stream_busy); - lua_setfield(L, -2, "busy"); - /* mt.interrupt = <stream_interrupt> */ - lua_pushcfunction(L, stream_interrupt); - lua_setfield(L, -2, "interrupt"); - /* mt.close = <ostream_close> */ - lua_pushcfunction(L, ostream_close); + /* mt.close = <stream_close> */ + lua_pushcfunction(L, stream_close); lua_setfield(L, -2, "close"); + /* mt.readp = <stream_readp> */ + lua_pushcfunction(L, stream_readp); + lua_setfield(L, -2, "readp"); /* mt.write = <stream_write> */ lua_pushcfunction(L, stream_write); lua_setfield(L, -2, "write"); @@ -179,39 +173,13 @@ luaopen_lem_io_core(lua_State *L) /* mt.uncork = <stream_uncork> */ lua_pushcfunction(L, stream_uncork); lua_setfield(L, -2, "uncork"); - /* mt.sendfile = <ostream_sendfile> */ + /* mt.sendfile = <stream_sendfile> */ lua_pushcfunction(L, stream_sendfile); lua_setfield(L, -2, "sendfile"); /* insert table */ - lua_setfield(L, -2, "OStream"); - - /* create File metatable */ - lua_newtable(L); - /* mt.__index = mt */ - lua_pushvalue(L, -1); - lua_setfield(L, -2, "__index"); - /* mt.__gc = <file_gc> */ - lua_pushcfunction(L, file_gc); - lua_setfield(L, -2, "__gc"); - /* mt.closed = <file_closed> */ - lua_pushcfunction(L, file_closed); - lua_setfield(L, -2, "closed"); - /* mt.close = <file_close> */ - lua_pushcfunction(L, file_close); - lua_setfield(L, -2, "close"); - /* mt.readp = <file_readp> */ - lua_pushcfunction(L, file_readp); - lua_setfield(L, -2, "readp"); - /* mt.write = <file_write> */ - lua_pushcfunction(L, file_write); - lua_setfield(L, -2, "write"); - /* mt.seek = <file_seek> */ - lua_pushcfunction(L, file_seek); - lua_setfield(L, -2, "seek"); - /* insert table */ - lua_setfield(L, -2, "File"); + lua_setfield(L, -2, "Stream"); - /* create metatable for server objects */ + /* create Server metatable */ lua_newtable(L); /* mt.__index = mt */ lua_pushvalue(L, -1); @@ -232,34 +200,29 @@ luaopen_lem_io_core(lua_State *L) lua_pushcfunction(L, server_interrupt); lua_setfield(L, -2, "interrupt"); /* mt.accept = <server_accept> */ - lua_getfield(L, -2, "IStream"); /* upvalue 1 = IStream */ - lua_getfield(L, -3, "OStream"); /* upvalue 2 = OStream */ - lua_pushcclosure(L, server_accept, 2); + lua_getfield(L, -2, "Stream"); /* upvalue 1 = Stream */ + lua_pushcclosure(L, server_accept, 1); lua_setfield(L, -2, "accept"); /* mt.autospawn = <server_autospawn> */ - lua_getfield(L, -2, "IStream"); /* upvalue 1 = IStream */ - lua_getfield(L, -3, "OStream"); /* upvalue 2 = OStream */ - lua_pushcclosure(L, server_autospawn, 2); + lua_getfield(L, -2, "Stream"); /* upvalue 1 = Stream */ + lua_pushcclosure(L, server_autospawn, 1); lua_setfield(L, -2, "autospawn"); /* insert table */ lua_setfield(L, -2, "Server"); /* insert open function */ - lua_getfield(L, -1, "IStream"); /* upvalue 1 = IStream */ - lua_getfield(L, -2, "OStream"); /* upvalue 2 = OStream */ - lua_getfield(L, -3, "File"); /* upvalue 3 = File */ - lua_pushcclosure(L, stream_open, 3); + lua_getfield(L, -1, "File"); /* upvalue 1 = File */ + lua_getfield(L, -2, "Stream"); /* upvalue 2 = Stream */ + lua_pushcclosure(L, stream_open, 2); lua_setfield(L, -2, "open"); /* insert popen function */ - lua_getfield(L, -1, "IStream"); /* upvalue 1 = IStream */ - lua_getfield(L, -2, "OStream"); /* upvalue 2 = OStream */ - lua_pushcclosure(L, stream_popen, 2); + lua_getfield(L, -1, "Stream"); /* upvalue 1 = Stream */ + lua_pushcclosure(L, stream_popen, 1); lua_setfield(L, -2, "popen"); /* insert the connect function */ - lua_getfield(L, -1, "IStream"); /* upvalue 1 = IStream */ - lua_getfield(L, -2, "OStream"); /* upvalue 2 = OStream */ - lua_pushcclosure(L, tcp_connect, 2); + lua_getfield(L, -1, "Stream"); /* upvalue 1 = Stream */ + lua_pushcclosure(L, tcp_connect, 1); lua_setfield(L, -2, "tcp_connect"); /* insert the tcp4_listen function */ lua_getfield(L, -1, "Server"); /* upvalue 1 = Server */ @@ -290,9 +253,8 @@ luaopen_lem_io_core(lua_State *L) /* create metatable for the module */ lua_newtable(L); /* insert the index function */ - lua_getfield(L, -2, "IStream"); /* upvalue 1 = IStream */ - lua_getfield(L, -3, "OStream"); /* upvalue 2 = OStream */ - lua_pushcclosure(L, module_index, 2); + lua_getfield(L, -2, "Stream"); /* upvalue 1 = Stream */ + lua_pushcclosure(L, module_index, 1); lua_setfield(L, -2, "__index"); /* set the metatable */ |