6 #include <lauxlib.h> |
6 #include <lauxlib.h> |
7 #include <malloc.h> |
7 #include <malloc.h> |
8 #include "event_buffer.h" |
8 #include "event_buffer.h" |
9 |
9 |
10 #define BUFFER_EVENT_MT "BUFFER_EVENT_MT" |
10 #define BUFFER_EVENT_MT "BUFFER_EVENT_MT" |
|
11 |
|
12 /* Locations of READ/WRITE buffers in the fenv */ |
|
13 #define READ_BUFFER_LOCATION 4 |
|
14 #define WRITE_BUFFER_LOCATION 5 |
11 |
15 |
12 /* Obtains an le_bufferevent structure from a given index */ |
16 /* Obtains an le_bufferevent structure from a given index */ |
13 static le_bufferevent* buffer_event_get(lua_State* L, int idx) { |
17 static le_bufferevent* buffer_event_get(lua_State* L, int idx) { |
14 return (le_bufferevent*)luaL_checkudata(L, idx, BUFFER_EVENT_MT); |
18 return (le_bufferevent*)luaL_checkudata(L, idx, BUFFER_EVENT_MT); |
15 } |
19 } |
84 lua_rawseti(L, -2, 2); // Write |
88 lua_rawseti(L, -2, 2); // Write |
85 lua_pushvalue(L, 5); |
89 lua_pushvalue(L, 5); |
86 lua_rawseti(L, -2, 3); // Err |
90 lua_rawseti(L, -2, 3); // Err |
87 |
91 |
88 event_buffer_push(L, ev->ev->input); |
92 event_buffer_push(L, ev->ev->input); |
89 lua_rawseti(L, -2, 4); |
93 lua_rawseti(L, -2, READ_BUFFER_LOCATION); |
90 event_buffer_push(L, ev->ev->output); |
94 event_buffer_push(L, ev->ev->output); |
91 lua_rawseti(L, -2, 5); |
95 lua_rawseti(L, -2, WRITE_BUFFER_LOCATION); |
92 lua_setfenv(L, -2); |
96 lua_setfenv(L, -2); |
93 ev->base = base; |
97 ev->base = base; |
94 return 1; |
98 return 1; |
95 } |
99 } |
96 |
100 |
104 bufferevent_free(ev->ev); |
108 bufferevent_free(ev->ev); |
105 ev->ev = NULL; |
109 ev->ev = NULL; |
106 /* Also clear out the associated input/output event_buffers |
110 /* Also clear out the associated input/output event_buffers |
107 * since they would have already been freed.. */ |
111 * since they would have already been freed.. */ |
108 lua_getfenv(L, 1); |
112 lua_getfenv(L, 1); |
109 lua_rawgeti(L, -1, 4); |
113 lua_rawgeti(L, -1, READ_BUFFER_LOCATION); |
110 lua_rawgeti(L, -2, 5); |
114 lua_rawgeti(L, -2, WRITE_BUFFER_LOCATION); |
111 read = event_buffer_check(L, -2); |
115 read = event_buffer_check(L, -2); |
112 write = event_buffer_check(L, -1); |
116 write = event_buffer_check(L, -1); |
113 /* Erase their knowledge of the buffer so that the GC won't try to double-free */ |
117 /* Erase their knowledge of the buffer so that the GC won't try to double-free */ |
114 read->buffer = NULL; |
118 read->buffer = NULL; |
115 write->buffer = NULL; |
119 write->buffer = NULL; |