#include "stream.h" #include #include #include /// Creates and pushes new Stream into the Lua stack. /// \param L Lua state to push to /// \param fd File descriptor used by the Stream /// \return TODO int stream_push_new(lua_State * L, const int fd) { struct stream * s = lua_newuserdata(L, sizeof(struct stream)); s->fd = fd; if (1 == luaL_newmetatable(L, "stream")) { lua_pushstring(L, "__gc"); lua_pushcfunction(L, stream_gc); lua_rawset(L, -3); } lua_setmetatable(L, -2); return LUA_OK; } /// Metamethod to handle garbage collection of Stream userdata. /// \param L Lua state in which Stream resides /// \return Always zero, which is the number of the results pushed to the stack int stream_gc(lua_State * L) { struct stream * s = lua_touserdata(L, -1); if (NULL != s) { if (NULL != s->in.data) { free(s->in.data); } } return 0; }