moonbridge

annotate moonbridge_io.c @ 92:111b2469b753

Always re-fill immediate send buffer in write_nb method
author jbe
date Tue Apr 07 04:07:37 2015 +0200 (2015-04-07)
parents 6b26783f9323
children de3982f17d05
rev   line source
jbe@79 1
jbe@79 2 #include <stdlib.h>
jbe@79 3 #include <unistd.h>
jbe@79 4 #include <stdint.h>
jbe@79 5 #include <errno.h>
jbe@79 6 #include <string.h>
jbe@79 7 #include <sys/socket.h>
jbe@79 8 #include <sys/select.h>
jbe@81 9 #include <fcntl.h>
jbe@79 10
jbe@79 11 #include <lua.h>
jbe@79 12 #include <lauxlib.h>
jbe@79 13 #include <lualib.h>
jbe@79 14
jbe@80 15 #define MOONBR_IO_MAXSTRERRORLEN 80
jbe@85 16 #define MOONBR_IO_READBUFLEN 4096
jbe@80 17 #define MOONBR_IO_WRITEBUFLEN 4096
jbe@80 18
jbe@80 19 #define moonbr_io_errmsg() \
jbe@80 20 char errmsg[MOONBR_IO_MAXSTRERRORLEN]; \
jbe@80 21 strerror_r(errno, errmsg, MOONBR_IO_MAXSTRERRORLEN)
jbe@80 22
jbe@79 23 #define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle"
jbe@79 24 #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public"
jbe@79 25
jbe@79 26 typedef struct {
jbe@79 27 int fd;
jbe@84 28 int issocket;
jbe@88 29 int canshutdown;
jbe@88 30 int shutwr;
jbe@81 31 int nonblocking;
jbe@85 32 int readerr;
jbe@85 33 int readbufcnt;
jbe@81 34 int writeerr;
jbe@83 35 size_t writeleft;
jbe@83 36 #if LUA_VERSION_NUM >= 503
jbe@83 37 lua_Integer writeqin;
jbe@83 38 lua_Integer writeqout;
jbe@83 39 #else
jbe@83 40 int writeqin;
jbe@83 41 int writeqout;
jbe@83 42 #endif
jbe@83 43 size_t writeqoff;
jbe@81 44 int writebufcnt;
jbe@85 45 char readbuf[MOONBR_IO_READBUFLEN];
jbe@80 46 char writebuf[MOONBR_IO_WRITEBUFLEN];
jbe@79 47 } moonbr_io_handle_t;
jbe@79 48
jbe@81 49 static void moonbr_io_handle_set_nonblocking(lua_State *L, moonbr_io_handle_t *handle, int nonblocking) {
jbe@81 50 if (handle->nonblocking != nonblocking) {
jbe@81 51 int flags;
jbe@81 52 flags = fcntl(handle->fd, F_GETFL, 0);
jbe@81 53 if (flags == -1) {
jbe@81 54 moonbr_io_errmsg();
jbe@81 55 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
jbe@81 56 }
jbe@81 57 if (nonblocking) flags |= O_NONBLOCK;
jbe@81 58 else flags &= ~O_NONBLOCK;
jbe@81 59 if (fcntl(handle->fd, F_SETFL, flags) == -1) {
jbe@81 60 moonbr_io_errmsg();
jbe@81 61 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
jbe@81 62 }
jbe@81 63 }
jbe@81 64 }
jbe@81 65
jbe@87 66 static void moonbr_io_handle_set_linger(lua_State *L, moonbr_io_handle_t *handle, int timeout) {
jbe@87 67 struct linger lingerval = { 0, };
jbe@87 68 if (!handle->issocket) return;
jbe@87 69 if (timeout >= 0) {
jbe@87 70 lingerval.l_onoff = 1;
jbe@87 71 lingerval.l_linger = timeout;
jbe@87 72 }
jbe@87 73 if (setsockopt(handle->fd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval))) {
jbe@87 74 moonbr_io_errmsg();
jbe@87 75 luaL_error(L, "Unexpected error in setsockopt call: %s", errmsg);
jbe@87 76 }
jbe@87 77 }
jbe@87 78
jbe@86 79 static int moonbr_io_read_impl(lua_State *L, int nonblocking, int drain) {
jbe@85 80 moonbr_io_handle_t *handle;
jbe@85 81 lua_Integer maxread;
jbe@85 82 const char *terminatorstr;
jbe@85 83 size_t terminatorlen;
jbe@85 84 char terminator;
jbe@85 85 luaL_Buffer luabuf;
jbe@85 86 size_t luabufcnt = 0;
jbe@86 87 int endcnt;
jbe@85 88 char *terminatorpos;
jbe@85 89 ssize_t result;
jbe@85 90 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@85 91 maxread = luaL_optinteger(L, 2, 0);
jbe@85 92 terminatorstr = luaL_optlstring(L, 3, "", &terminatorlen);
jbe@85 93 if (terminatorlen) {
jbe@85 94 luaL_argcheck(L, terminatorlen == 1, 3, "single byte expected");
jbe@85 95 terminator = terminatorstr[0];
jbe@85 96 }
jbe@86 97 lua_settop(L, 1); /* return handle on drain, terminator string may be garbage collected */
jbe@85 98 if (handle->fd < 0) luaL_error(L, "Attempt to read from a closed I/O handle");
jbe@85 99 if (handle->readerr) {
jbe@85 100 lua_pushnil(L);
jbe@85 101 lua_pushliteral(L, "Previous read error");
jbe@85 102 return 2;
jbe@85 103 }
jbe@85 104 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
jbe@86 105 if (!drain) luaL_buffinit(L, &luabuf);
jbe@85 106 while (1) {
jbe@86 107 endcnt = -1;
jbe@85 108 if (maxread > 0 && handle->readbufcnt >= maxread - luabufcnt) {
jbe@86 109 endcnt = maxread - luabufcnt;
jbe@85 110 } else if (terminatorlen) {
jbe@85 111 terminatorpos = memchr(handle->readbuf, terminator, handle->readbufcnt);
jbe@86 112 if (terminatorpos) endcnt = 1 + (terminatorpos - handle->readbuf);
jbe@85 113 }
jbe@86 114 if (endcnt >= 0) {
jbe@86 115 if (!drain) {
jbe@86 116 luaL_addlstring(&luabuf, handle->readbuf, endcnt);
jbe@86 117 luaL_pushresult(&luabuf);
jbe@90 118 } else {
jbe@90 119 luabufcnt += handle->readbufcnt;
jbe@90 120 lua_pushinteger(L, luabufcnt);
jbe@86 121 }
jbe@86 122 handle->readbufcnt -= endcnt;
jbe@86 123 memmove(handle->readbuf, handle->readbuf + endcnt, handle->readbufcnt);
jbe@85 124 return 1;
jbe@85 125 }
jbe@86 126 if (!drain) luaL_addlstring(&luabuf, handle->readbuf, handle->readbufcnt);
jbe@85 127 luabufcnt += handle->readbufcnt;
jbe@85 128 handle->readbufcnt = 0;
jbe@85 129 do {
jbe@85 130 result = read(handle->fd, handle->readbuf, MOONBR_IO_READBUFLEN);
jbe@85 131 } while (result < 0 && (errno == EINTR));
jbe@85 132 if (result == 0 || (nonblocking && result < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))) break;
jbe@85 133 if (result < 0) {
jbe@85 134 moonbr_io_errmsg();
jbe@85 135 handle->readerr = 1;
jbe@85 136 lua_pushnil(L);
jbe@85 137 lua_pushstring(L, errmsg);
jbe@85 138 return 2;
jbe@85 139 }
jbe@85 140 handle->readbufcnt += result;
jbe@85 141 }
jbe@86 142 if (!drain) {
jbe@86 143 luaL_addlstring(&luabuf, handle->readbuf, handle->readbufcnt);
jbe@86 144 luaL_pushresult(&luabuf);
jbe@86 145 }
jbe@90 146 luabufcnt += handle->readbufcnt;
jbe@85 147 handle->readbufcnt = 0;
jbe@90 148 if (!drain) {
jbe@90 149 if (!luabufcnt && result == 0) {
jbe@90 150 lua_pushboolean(L, 0);
jbe@90 151 lua_pushliteral(L, "End of file");
jbe@90 152 return 2;
jbe@90 153 }
jbe@90 154 } else {
jbe@90 155 if (!luabufcnt && result == 0) lua_pushboolean(L, 1);
jbe@90 156 else lua_pushboolean(L, luabufcnt);
jbe@90 157 }
jbe@85 158 return 1;
jbe@85 159 }
jbe@85 160
jbe@85 161 static int moonbr_io_read(lua_State *L) {
jbe@86 162 return moonbr_io_read_impl(L, 0, 0);
jbe@85 163 }
jbe@85 164
jbe@85 165 static int moonbr_io_read_nb(lua_State *L) {
jbe@86 166 return moonbr_io_read_impl(L, 1, 0);
jbe@86 167 }
jbe@86 168
jbe@86 169 static int moonbr_io_drain(lua_State *L) {
jbe@86 170 return moonbr_io_read_impl(L, 0, 1);
jbe@86 171 }
jbe@86 172
jbe@86 173 static int moonbr_io_drain_nb(lua_State *L) {
jbe@86 174 return moonbr_io_read_impl(L, 1, 1);
jbe@85 175 }
jbe@85 176
jbe@81 177 static int moonbr_io_write_impl(lua_State *L, int nonblocking, int flush) {
jbe@80 178 moonbr_io_handle_t *handle;
jbe@80 179 int i, top;
jbe@80 180 const char *str;
jbe@92 181 size_t strlen;
jbe@80 182 size_t written;
jbe@80 183 ssize_t result;
jbe@80 184 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@83 185 if (handle->fd < 0) luaL_error(L, "Attempt to write to a closed I/O handle");
jbe@88 186 if (handle->shutwr) luaL_error(L, "Attempt to write to a finished I/O handle");
jbe@81 187 if (handle->writeerr) {
jbe@80 188 lua_pushnil(L);
jbe@80 189 lua_pushliteral(L, "Previous write error");
jbe@80 190 return 2;
jbe@80 191 }
jbe@81 192 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
jbe@84 193 top = lua_gettop(L);
jbe@81 194 lua_getuservalue(L, 1);
jbe@81 195 lua_getfield(L, -1, "writebuf");
jbe@84 196 for (i=2; i<=top; i++) {
jbe@84 197 luaL_checklstring(L, i, &strlen);
jbe@84 198 lua_pushvalue(L, i);
jbe@84 199 lua_rawseti(L, -2, handle->writeqin++);
jbe@84 200 handle->writeleft += strlen;
jbe@81 201 }
jbe@83 202 while (handle->writeqout != handle->writeqin) {
jbe@83 203 lua_rawgeti(L, -1, handle->writeqout);
jbe@81 204 str = lua_tolstring(L, -1, &strlen);
jbe@92 205 while (handle->writeqoff < strlen) {
jbe@92 206 if (strlen - handle->writeqoff < MOONBR_IO_WRITEBUFLEN - handle->writebufcnt) {
jbe@92 207 memcpy(handle->writebuf + handle->writebufcnt, str + handle->writeqoff, strlen - handle->writeqoff);
jbe@92 208 handle->writebufcnt += strlen - handle->writeqoff;
jbe@80 209 break;
jbe@80 210 } else {
jbe@80 211 written = 0;
jbe@92 212 memcpy(handle->writebuf + handle->writebufcnt, str + handle->writeqoff, MOONBR_IO_WRITEBUFLEN - handle->writebufcnt);
jbe@92 213 handle->writeqoff += MOONBR_IO_WRITEBUFLEN - handle->writebufcnt;
jbe@80 214 while (written < MOONBR_IO_WRITEBUFLEN) {
jbe@80 215 result = write(handle->fd, handle->writebuf + written, MOONBR_IO_WRITEBUFLEN - written);
jbe@80 216 if (result < 0) {
jbe@81 217 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
jbe@81 218 if (written) {
jbe@81 219 handle->writebufcnt -= written;
jbe@81 220 memmove(handle->writebuf, handle->writebuf + written, handle->writebufcnt);
jbe@92 221 goto moonbr_io_write_impl_continue;
jbe@81 222 }
jbe@81 223 goto moonbr_io_write_impl_block;
jbe@81 224 } else if (errno != EINTR) {
jbe@80 225 moonbr_io_errmsg();
jbe@81 226 handle->writeerr = 1;
jbe@80 227 lua_pushnil(L);
jbe@80 228 lua_pushstring(L, errmsg);
jbe@80 229 return 2;
jbe@80 230 }
jbe@80 231 }
jbe@92 232 written += result;
jbe@92 233 handle->writeleft -= result;
jbe@80 234 }
jbe@81 235 handle->writebufcnt = 0;
jbe@80 236 }
jbe@92 237 moonbr_io_write_impl_continue:;
jbe@80 238 }
jbe@81 239 handle->writeqoff = 0;
jbe@81 240 lua_pop(L, 1);
jbe@81 241 lua_pushnil(L);
jbe@83 242 lua_rawseti(L, -2, handle->writeqout++);
jbe@80 243 }
jbe@81 244 if (flush) {
jbe@81 245 written = 0;
jbe@81 246 while (written < handle->writebufcnt) {
jbe@81 247 result = write(handle->fd, handle->writebuf + written, handle->writebufcnt - written);
jbe@81 248 if (result < 0) {
jbe@81 249 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
jbe@81 250 if (written) {
jbe@81 251 handle->writebufcnt -= written;
jbe@81 252 memmove(handle->writebuf, handle->writebuf + written, handle->writebufcnt);
jbe@81 253 }
jbe@81 254 goto moonbr_io_write_impl_block;
jbe@81 255 } else if (errno != EINTR) {
jbe@81 256 moonbr_io_errmsg();
jbe@81 257 handle->writeerr = -1;
jbe@81 258 lua_pushnil(L);
jbe@81 259 lua_pushstring(L, errmsg);
jbe@81 260 return 2;
jbe@81 261 }
jbe@81 262 } else {
jbe@81 263 written += result;
jbe@81 264 handle->writeleft -= result;
jbe@81 265 }
jbe@81 266 }
jbe@81 267 handle->writebufcnt = 0;
jbe@82 268 if (nonblocking) lua_pushinteger(L, 0);
jbe@81 269 } else {
jbe@91 270 if (nonblocking) lua_pushinteger(L, handle->writeleft);
jbe@81 271 }
jbe@82 272 if (!nonblocking) lua_pushvalue(L, 1);
jbe@80 273 return 1;
jbe@81 274 moonbr_io_write_impl_block:
jbe@91 275 lua_pushinteger(L, handle->writeleft);
jbe@81 276 return 1;
jbe@81 277 }
jbe@81 278
jbe@81 279 static int moonbr_io_write(lua_State *L) {
jbe@81 280 return moonbr_io_write_impl(L, 0, 0);
jbe@81 281 }
jbe@81 282
jbe@81 283 static int moonbr_io_write_nb(lua_State *L) {
jbe@81 284 return moonbr_io_write_impl(L, 1, 0);
jbe@80 285 }
jbe@80 286
jbe@80 287 static int moonbr_io_flush(lua_State *L) {
jbe@81 288 return moonbr_io_write_impl(L, 0, 1);
jbe@81 289 }
jbe@81 290
jbe@81 291 static int moonbr_io_flush_nb(lua_State *L) {
jbe@81 292 return moonbr_io_write_impl(L, 1, 1);
jbe@80 293 }
jbe@80 294
jbe@88 295 static int moonbr_io_finish(lua_State *L) {
jbe@88 296 moonbr_io_handle_t *handle;
jbe@88 297 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@88 298 if (handle->fd < 0) luaL_error(L, "Attempt to finish a closed I/O handle");
jbe@88 299 if (handle->shutwr) luaL_error(L, "Attempt to finish a finished I/O handle");
jbe@88 300 handle->shutwr = 1;
jbe@88 301 if (handle->canshutdown) {
jbe@88 302 if (handle->writeleft) {
jbe@88 303 lua_pushcfunction(L, moonbr_io_flush);
jbe@88 304 lua_pushvalue(L, 1);
jbe@88 305 lua_call(L, 1, 2);
jbe@88 306 if (!lua_toboolean(L, -2)) return 2;
jbe@88 307 }
jbe@88 308 if (shutdown(handle->fd, SHUT_WR)) {
jbe@88 309 moonbr_io_errmsg();
jbe@88 310 lua_pushnil(L);
jbe@88 311 lua_pushstring(L, errmsg);
jbe@88 312 return 2;
jbe@88 313 }
jbe@88 314 }
jbe@88 315 lua_pushboolean(L, 1);
jbe@88 316 return 1;
jbe@88 317 }
jbe@88 318
jbe@87 319 static int moonbr_io_close(lua_State *L) {
jbe@83 320 moonbr_io_handle_t *handle;
jbe@87 321 int timeout;
jbe@83 322 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@87 323 timeout = luaL_optinteger(L, 2, -1);
jbe@83 324 if (handle->fd < 0) luaL_error(L, "Attempt to close a closed I/O handle");
jbe@87 325 if (timeout != 0) {
jbe@87 326 if (handle->writeleft) {
jbe@87 327 lua_pushcfunction(L, moonbr_io_flush);
jbe@87 328 lua_pushvalue(L, 1);
jbe@87 329 lua_call(L, 1, 2);
jbe@87 330 if (!lua_toboolean(L, -2)) {
jbe@87 331 close(handle->fd);
jbe@87 332 handle->fd = -1;
jbe@87 333 return 2;
jbe@87 334 }
jbe@83 335 }
jbe@87 336 moonbr_io_handle_set_linger(L, handle, timeout);
jbe@83 337 }
jbe@83 338 if (close(handle->fd)) {
jbe@83 339 moonbr_io_errmsg();
jbe@83 340 handle->fd = -1;
jbe@83 341 lua_pushnil(L);
jbe@83 342 lua_pushstring(L, errmsg);
jbe@83 343 return 2;
jbe@83 344 }
jbe@83 345 handle->fd = -1;
jbe@83 346 lua_pushboolean(L, 1);
jbe@83 347 return 1;
jbe@84 348
jbe@83 349 }
jbe@83 350
jbe@84 351 static int moonbr_io_reset(lua_State *L) {
jbe@87 352 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@87 353 lua_settop(L, 1);
jbe@87 354 lua_pushcfunction(L, moonbr_io_close);
jbe@87 355 lua_insert(L, 1);
jbe@87 356 lua_pushinteger(L, 0);
jbe@87 357 lua_call(L, 2, LUA_MULTRET);
jbe@87 358 return lua_gettop(L);
jbe@84 359 }
jbe@84 360
jbe@88 361 static int moonbr_io_gc(lua_State *L) {
jbe@88 362 moonbr_io_handle_t *handle;
jbe@88 363 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@88 364 if (handle->fd >= 0) {
jbe@88 365 lua_pushcfunction(L, moonbr_io_close);
jbe@88 366 lua_pushvalue(L, 1);
jbe@88 367 lua_pushinteger(L, 0);
jbe@88 368 lua_call(L, 2, 0);
jbe@88 369 }
jbe@88 370 return 0;
jbe@88 371 }
jbe@88 372
jbe@88 373 void moonbr_io_closehandle(lua_State *L, int idx, int timeout) {
jbe@88 374 moonbr_io_handle_t *handle;
jbe@88 375 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@88 376 if (handle->fd >= 0) {
jbe@88 377 lua_pushcfunction(L, moonbr_io_close);
jbe@88 378 lua_pushvalue(L, idx);
jbe@88 379 lua_pushinteger(L, timeout);
jbe@88 380 lua_call(L, 2, 0);
jbe@88 381 }
jbe@88 382 }
jbe@88 383
jbe@88 384 void moonbr_io_pushhandle(lua_State *L, int fd, int issocket, int canshutdown) {
jbe@79 385 moonbr_io_handle_t *handle;
jbe@79 386 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
jbe@79 387 handle->fd = fd;
jbe@84 388 handle->issocket = issocket;
jbe@88 389 handle->canshutdown = canshutdown;
jbe@88 390 handle->shutwr = 0;
jbe@81 391 handle->nonblocking = -1;
jbe@85 392 handle->readerr = 0;
jbe@85 393 handle->readbufcnt = 0;
jbe@81 394 handle->writeerr = 0;
jbe@81 395 handle->writeleft = 0;
jbe@83 396 handle->writeqin = 0;
jbe@83 397 handle->writeqout = 0;
jbe@81 398 handle->writeqoff = 0;
jbe@81 399 handle->writebufcnt = 0;
jbe@87 400 moonbr_io_handle_set_linger(L, handle, 0);
jbe@79 401 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 402 lua_setmetatable(L, -2);
jbe@79 403 lua_newtable(L); // uservalue
jbe@81 404 lua_newtable(L);
jbe@81 405 lua_setfield(L, -2, "writebuf");
jbe@79 406 lua_newtable(L); // public
jbe@79 407 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
jbe@79 408 lua_setmetatable(L, -2);
jbe@79 409 lua_setfield(L, -2, "public");
jbe@79 410 lua_setuservalue(L, -2);
jbe@79 411 }
jbe@79 412
jbe@79 413 static int moonbr_io_handleindex(lua_State *L) {
jbe@80 414 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 415 lua_getuservalue(L, 1);
jbe@79 416 lua_getfield(L, -1, "public");
jbe@79 417 lua_pushvalue(L, 2);
jbe@79 418 lua_gettable(L, -2);
jbe@79 419 return 1;
jbe@79 420 }
jbe@79 421
jbe@79 422 static int moonbr_io_handlenewindex(lua_State *L) {
jbe@80 423 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 424 lua_getuservalue(L, 1);
jbe@79 425 lua_getfield(L, -1, "public");
jbe@79 426 lua_pushvalue(L, 2);
jbe@79 427 lua_pushvalue(L, 3);
jbe@79 428 lua_settable(L, -3);
jbe@79 429 return 0;
jbe@79 430 }
jbe@79 431
jbe@79 432 static const struct luaL_Reg moonbr_io_handle_methods[] = {
jbe@85 433 {"read", moonbr_io_read},
jbe@85 434 {"read_nb", moonbr_io_read_nb},
jbe@86 435 {"drain", moonbr_io_drain},
jbe@86 436 {"drain_nb", moonbr_io_drain_nb},
jbe@80 437 {"write", moonbr_io_write},
jbe@81 438 {"write_nb", moonbr_io_write_nb},
jbe@80 439 {"flush", moonbr_io_flush},
jbe@81 440 {"flush_nb", moonbr_io_flush_nb},
jbe@88 441 {"finish", moonbr_io_finish},
jbe@87 442 {"close", moonbr_io_close},
jbe@85 443 {"reset", moonbr_io_reset},
jbe@79 444 {NULL, NULL}
jbe@79 445 };
jbe@79 446
jbe@79 447 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
jbe@79 448 {"__index", moonbr_io_handleindex},
jbe@79 449 {"__newindex", moonbr_io_handlenewindex},
jbe@88 450 {"__gc", moonbr_io_gc},
jbe@79 451 {NULL, NULL}
jbe@79 452 };
jbe@79 453
jbe@79 454 static const struct luaL_Reg moonbr_io_module_funcs[] = {
jbe@79 455 {NULL, NULL}
jbe@79 456 };
jbe@79 457
jbe@79 458 int luaopen_moonbridge_io(lua_State *L) {
jbe@79 459
jbe@80 460 lua_newtable(L); // module
jbe@80 461
jbe@79 462 lua_newtable(L); // public metatable
jbe@79 463 lua_newtable(L); // handle methods
jbe@79 464 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
jbe@80 465 lua_pushvalue(L, -1);
jbe@80 466 lua_setfield(L, -4, "handle");
jbe@79 467 lua_setfield(L, -2, "__index");
jbe@79 468 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
jbe@79 469
jbe@79 470 lua_newtable(L); // handle metatable
jbe@79 471 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
jbe@79 472 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 473
jbe@79 474 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
jbe@79 475 return 1;
jbe@79 476
jbe@79 477 }
jbe@79 478

Impressum / About Us