moonbridge

annotate moonbridge_io.c @ 91:6b26783f9323

write_nb returns total number of bytes buffered in case of block; Updated reference
author jbe
date Tue Apr 07 03:50:28 2015 +0200 (2015-04-07)
parents 54e6104c70a6
children 111b2469b753
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@81 181 size_t strlen, strpos;
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@81 205 strpos = handle->writeqoff;
jbe@81 206 while (strpos < strlen) {
jbe@81 207 if (strlen - strpos < MOONBR_IO_WRITEBUFLEN - handle->writebufcnt) {
jbe@81 208 memcpy(handle->writebuf + handle->writebufcnt, str + strpos, strlen - strpos);
jbe@81 209 handle->writebufcnt += strlen - strpos;
jbe@80 210 break;
jbe@80 211 } else {
jbe@80 212 written = 0;
jbe@81 213 memcpy(handle->writebuf + handle->writebufcnt, str + strpos, MOONBR_IO_WRITEBUFLEN - handle->writebufcnt);
jbe@81 214 strpos += MOONBR_IO_WRITEBUFLEN - handle->writebufcnt;
jbe@80 215 while (written < MOONBR_IO_WRITEBUFLEN) {
jbe@80 216 result = write(handle->fd, handle->writebuf + written, MOONBR_IO_WRITEBUFLEN - written);
jbe@80 217 if (result < 0) {
jbe@81 218 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
jbe@81 219 if (written) {
jbe@81 220 handle->writebufcnt -= written;
jbe@81 221 memmove(handle->writebuf, handle->writebuf + written, handle->writebufcnt);
jbe@81 222 }
jbe@81 223 handle->writeqoff = strpos;
jbe@81 224 goto moonbr_io_write_impl_block;
jbe@81 225 } else if (errno != EINTR) {
jbe@80 226 moonbr_io_errmsg();
jbe@81 227 handle->writeerr = 1;
jbe@80 228 lua_pushnil(L);
jbe@80 229 lua_pushstring(L, errmsg);
jbe@80 230 return 2;
jbe@80 231 }
jbe@80 232 } else {
jbe@80 233 written += result;
jbe@81 234 handle->writeleft -= result;
jbe@80 235 }
jbe@80 236 }
jbe@81 237 handle->writebufcnt = 0;
jbe@80 238 }
jbe@80 239 }
jbe@81 240 handle->writeqoff = 0;
jbe@81 241 lua_pop(L, 1);
jbe@81 242 lua_pushnil(L);
jbe@83 243 lua_rawseti(L, -2, handle->writeqout++);
jbe@80 244 }
jbe@81 245 if (flush) {
jbe@81 246 written = 0;
jbe@81 247 while (written < handle->writebufcnt) {
jbe@81 248 result = write(handle->fd, handle->writebuf + written, handle->writebufcnt - written);
jbe@81 249 if (result < 0) {
jbe@81 250 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
jbe@81 251 if (written) {
jbe@81 252 handle->writebufcnt -= written;
jbe@81 253 memmove(handle->writebuf, handle->writebuf + written, handle->writebufcnt);
jbe@81 254 }
jbe@81 255 goto moonbr_io_write_impl_block;
jbe@81 256 } else if (errno != EINTR) {
jbe@81 257 moonbr_io_errmsg();
jbe@81 258 handle->writeerr = -1;
jbe@81 259 lua_pushnil(L);
jbe@81 260 lua_pushstring(L, errmsg);
jbe@81 261 return 2;
jbe@81 262 }
jbe@81 263 } else {
jbe@81 264 written += result;
jbe@81 265 handle->writeleft -= result;
jbe@81 266 }
jbe@81 267 }
jbe@81 268 handle->writebufcnt = 0;
jbe@82 269 if (nonblocking) lua_pushinteger(L, 0);
jbe@81 270 } else {
jbe@91 271 if (nonblocking) lua_pushinteger(L, handle->writeleft);
jbe@81 272 }
jbe@82 273 if (!nonblocking) lua_pushvalue(L, 1);
jbe@80 274 return 1;
jbe@81 275 moonbr_io_write_impl_block:
jbe@91 276 lua_pushinteger(L, handle->writeleft);
jbe@81 277 return 1;
jbe@81 278 }
jbe@81 279
jbe@81 280 static int moonbr_io_write(lua_State *L) {
jbe@81 281 return moonbr_io_write_impl(L, 0, 0);
jbe@81 282 }
jbe@81 283
jbe@81 284 static int moonbr_io_write_nb(lua_State *L) {
jbe@81 285 return moonbr_io_write_impl(L, 1, 0);
jbe@80 286 }
jbe@80 287
jbe@80 288 static int moonbr_io_flush(lua_State *L) {
jbe@81 289 return moonbr_io_write_impl(L, 0, 1);
jbe@81 290 }
jbe@81 291
jbe@81 292 static int moonbr_io_flush_nb(lua_State *L) {
jbe@81 293 return moonbr_io_write_impl(L, 1, 1);
jbe@80 294 }
jbe@80 295
jbe@88 296 static int moonbr_io_finish(lua_State *L) {
jbe@88 297 moonbr_io_handle_t *handle;
jbe@88 298 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@88 299 if (handle->fd < 0) luaL_error(L, "Attempt to finish a closed I/O handle");
jbe@88 300 if (handle->shutwr) luaL_error(L, "Attempt to finish a finished I/O handle");
jbe@88 301 handle->shutwr = 1;
jbe@88 302 if (handle->canshutdown) {
jbe@88 303 if (handle->writeleft) {
jbe@88 304 lua_pushcfunction(L, moonbr_io_flush);
jbe@88 305 lua_pushvalue(L, 1);
jbe@88 306 lua_call(L, 1, 2);
jbe@88 307 if (!lua_toboolean(L, -2)) return 2;
jbe@88 308 }
jbe@88 309 if (shutdown(handle->fd, SHUT_WR)) {
jbe@88 310 moonbr_io_errmsg();
jbe@88 311 lua_pushnil(L);
jbe@88 312 lua_pushstring(L, errmsg);
jbe@88 313 return 2;
jbe@88 314 }
jbe@88 315 }
jbe@88 316 lua_pushboolean(L, 1);
jbe@88 317 return 1;
jbe@88 318 }
jbe@88 319
jbe@87 320 static int moonbr_io_close(lua_State *L) {
jbe@83 321 moonbr_io_handle_t *handle;
jbe@87 322 int timeout;
jbe@83 323 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@87 324 timeout = luaL_optinteger(L, 2, -1);
jbe@83 325 if (handle->fd < 0) luaL_error(L, "Attempt to close a closed I/O handle");
jbe@87 326 if (timeout != 0) {
jbe@87 327 if (handle->writeleft) {
jbe@87 328 lua_pushcfunction(L, moonbr_io_flush);
jbe@87 329 lua_pushvalue(L, 1);
jbe@87 330 lua_call(L, 1, 2);
jbe@87 331 if (!lua_toboolean(L, -2)) {
jbe@87 332 close(handle->fd);
jbe@87 333 handle->fd = -1;
jbe@87 334 return 2;
jbe@87 335 }
jbe@83 336 }
jbe@87 337 moonbr_io_handle_set_linger(L, handle, timeout);
jbe@83 338 }
jbe@83 339 if (close(handle->fd)) {
jbe@83 340 moonbr_io_errmsg();
jbe@83 341 handle->fd = -1;
jbe@83 342 lua_pushnil(L);
jbe@83 343 lua_pushstring(L, errmsg);
jbe@83 344 return 2;
jbe@83 345 }
jbe@83 346 handle->fd = -1;
jbe@83 347 lua_pushboolean(L, 1);
jbe@83 348 return 1;
jbe@84 349
jbe@83 350 }
jbe@83 351
jbe@84 352 static int moonbr_io_reset(lua_State *L) {
jbe@87 353 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@87 354 lua_settop(L, 1);
jbe@87 355 lua_pushcfunction(L, moonbr_io_close);
jbe@87 356 lua_insert(L, 1);
jbe@87 357 lua_pushinteger(L, 0);
jbe@87 358 lua_call(L, 2, LUA_MULTRET);
jbe@87 359 return lua_gettop(L);
jbe@84 360 }
jbe@84 361
jbe@88 362 static int moonbr_io_gc(lua_State *L) {
jbe@88 363 moonbr_io_handle_t *handle;
jbe@88 364 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@88 365 if (handle->fd >= 0) {
jbe@88 366 lua_pushcfunction(L, moonbr_io_close);
jbe@88 367 lua_pushvalue(L, 1);
jbe@88 368 lua_pushinteger(L, 0);
jbe@88 369 lua_call(L, 2, 0);
jbe@88 370 }
jbe@88 371 return 0;
jbe@88 372 }
jbe@88 373
jbe@88 374 void moonbr_io_closehandle(lua_State *L, int idx, int timeout) {
jbe@88 375 moonbr_io_handle_t *handle;
jbe@88 376 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@88 377 if (handle->fd >= 0) {
jbe@88 378 lua_pushcfunction(L, moonbr_io_close);
jbe@88 379 lua_pushvalue(L, idx);
jbe@88 380 lua_pushinteger(L, timeout);
jbe@88 381 lua_call(L, 2, 0);
jbe@88 382 }
jbe@88 383 }
jbe@88 384
jbe@88 385 void moonbr_io_pushhandle(lua_State *L, int fd, int issocket, int canshutdown) {
jbe@79 386 moonbr_io_handle_t *handle;
jbe@79 387 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
jbe@79 388 handle->fd = fd;
jbe@84 389 handle->issocket = issocket;
jbe@88 390 handle->canshutdown = canshutdown;
jbe@88 391 handle->shutwr = 0;
jbe@81 392 handle->nonblocking = -1;
jbe@85 393 handle->readerr = 0;
jbe@85 394 handle->readbufcnt = 0;
jbe@81 395 handle->writeerr = 0;
jbe@81 396 handle->writeleft = 0;
jbe@83 397 handle->writeqin = 0;
jbe@83 398 handle->writeqout = 0;
jbe@81 399 handle->writeqoff = 0;
jbe@81 400 handle->writebufcnt = 0;
jbe@87 401 moonbr_io_handle_set_linger(L, handle, 0);
jbe@79 402 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 403 lua_setmetatable(L, -2);
jbe@79 404 lua_newtable(L); // uservalue
jbe@81 405 lua_newtable(L);
jbe@81 406 lua_setfield(L, -2, "writebuf");
jbe@79 407 lua_newtable(L); // public
jbe@79 408 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
jbe@79 409 lua_setmetatable(L, -2);
jbe@79 410 lua_setfield(L, -2, "public");
jbe@79 411 lua_setuservalue(L, -2);
jbe@79 412 }
jbe@79 413
jbe@79 414 static int moonbr_io_handleindex(lua_State *L) {
jbe@80 415 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 416 lua_getuservalue(L, 1);
jbe@79 417 lua_getfield(L, -1, "public");
jbe@79 418 lua_pushvalue(L, 2);
jbe@79 419 lua_gettable(L, -2);
jbe@79 420 return 1;
jbe@79 421 }
jbe@79 422
jbe@79 423 static int moonbr_io_handlenewindex(lua_State *L) {
jbe@80 424 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 425 lua_getuservalue(L, 1);
jbe@79 426 lua_getfield(L, -1, "public");
jbe@79 427 lua_pushvalue(L, 2);
jbe@79 428 lua_pushvalue(L, 3);
jbe@79 429 lua_settable(L, -3);
jbe@79 430 return 0;
jbe@79 431 }
jbe@79 432
jbe@79 433 static const struct luaL_Reg moonbr_io_handle_methods[] = {
jbe@85 434 {"read", moonbr_io_read},
jbe@85 435 {"read_nb", moonbr_io_read_nb},
jbe@86 436 {"drain", moonbr_io_drain},
jbe@86 437 {"drain_nb", moonbr_io_drain_nb},
jbe@80 438 {"write", moonbr_io_write},
jbe@81 439 {"write_nb", moonbr_io_write_nb},
jbe@80 440 {"flush", moonbr_io_flush},
jbe@81 441 {"flush_nb", moonbr_io_flush_nb},
jbe@88 442 {"finish", moonbr_io_finish},
jbe@87 443 {"close", moonbr_io_close},
jbe@85 444 {"reset", moonbr_io_reset},
jbe@79 445 {NULL, NULL}
jbe@79 446 };
jbe@79 447
jbe@79 448 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
jbe@79 449 {"__index", moonbr_io_handleindex},
jbe@79 450 {"__newindex", moonbr_io_handlenewindex},
jbe@88 451 {"__gc", moonbr_io_gc},
jbe@79 452 {NULL, NULL}
jbe@79 453 };
jbe@79 454
jbe@79 455 static const struct luaL_Reg moonbr_io_module_funcs[] = {
jbe@79 456 {NULL, NULL}
jbe@79 457 };
jbe@79 458
jbe@79 459 int luaopen_moonbridge_io(lua_State *L) {
jbe@79 460
jbe@80 461 lua_newtable(L); // module
jbe@80 462
jbe@79 463 lua_newtable(L); // public metatable
jbe@79 464 lua_newtable(L); // handle methods
jbe@79 465 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
jbe@80 466 lua_pushvalue(L, -1);
jbe@80 467 lua_setfield(L, -4, "handle");
jbe@79 468 lua_setfield(L, -2, "__index");
jbe@79 469 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
jbe@79 470
jbe@79 471 lua_newtable(L); // handle metatable
jbe@79 472 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
jbe@79 473 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 474
jbe@79 475 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
jbe@79 476 return 1;
jbe@79 477
jbe@79 478 }
jbe@79 479

Impressum / About Us