blob: a322e2b7b1a9d68f7c7925f8dc37304d6e580794 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#include "stream.h"
#include <stdlib.h>
#include <lauxlib.h>
#include <lua.h>
/// 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;
}
|