Repository navigation
Expand file tree
/
Copy pathluv.c
More file actions
82 lines (63 loc) · 1.76 KB
/
Copy pathluv.c
File metadata and controls
82 lines (63 loc) · 1.76 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <string.h>
#include "luv.h"
#include "luv_functions.c"
#include "luv_handle.c"
#include "luv_timer.c"
#include "luv_stream.c"
#include "luv_tcp.c"
int luv_newindex(lua_State* L) {
lua_getfenv(L, 1);
lua_pushvalue(L, 2);
lua_pushvalue(L, 3);
lua_rawset(L, -3);
lua_pop(L, 1);
return 0;
}
LUALIB_API int luaopen_luv (lua_State *L) {
luv_main_thread = L;
// Create userdata metatables
luaL_newmetatable(L, "uv_handle");
luaL_newmetatable(L, "uv_timer");
luaL_newmetatable(L, "uv_stream");
luaL_newmetatable(L, "uv_tcp");
// uv_handle, uv_timer, uv_stream, uv_tcp
// Create prototypes (method tables)
lua_newtable(L);
luv_setfuncs(L, luv_handle_m);
lua_newtable(L);
luv_setfuncs(L, luv_timer_m);
lua_newtable(L);
luv_setfuncs(L, luv_stream_m);
lua_newtable(L);
luv_setfuncs(L, luv_tcp_m);
// uv_handle, uv_timer, uv_stream, uv_tcp
// handle_m, timer_m, stream_m, tcp_m
// timer_m > handle_m
lua_newtable(L); // timer_meta
lua_pushvalue(L, -5);
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -4);
// stream_m > handle_m
lua_newtable(L); // stream_meta
lua_pushvalue(L, -5);
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -3);
// tcp_m > stream_m
lua_newtable(L); // tcp_meta
lua_pushvalue(L, -3);
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2);
lua_setfield(L, -5, "__index"); // tcp
lua_pushcfunction(L, luv_newindex);
lua_setfield(L, -5, "__newindex");
lua_setfield(L, -5, "__index"); // stream
lua_setfield(L, -5, "__index"); // timer
lua_pushcfunction(L, luv_newindex);
lua_setfield(L, -5, "__newindex");
lua_setfield(L, -5, "__index"); // handle
lua_pop(L, 4);
// Module exports
lua_newtable (L);
luv_setfuncs(L, luv_functions);
return 1;
}