moonbridge

annotate moonbridge_io.c @ 90:54e6104c70a6

Proper return values for drain functions
author jbe
date Tue Apr 07 02:52:41 2015 +0200 (2015-04-07)
parents fca51922b708
children 6b26783f9323
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@82 271 if (nonblocking) lua_pushinteger(L, handle->writeleft - handle->writebufcnt);
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@81 276 if (flush) lua_pushinteger(L, handle->writeleft);
jbe@81 277 else lua_pushinteger(L, handle->writeleft - handle->writebufcnt);
jbe@81 278 return 1;
jbe@81 279 }
jbe@81 280
jbe@81 281 static int moonbr_io_write(lua_State *L) {
jbe@81 282 return moonbr_io_write_impl(L, 0, 0);
jbe@81 283 }
jbe@81 284
jbe@81 285 static int moonbr_io_write_nb(lua_State *L) {
jbe@81 286 return moonbr_io_write_impl(L, 1, 0);
jbe@80 287 }
jbe@80 288
jbe@80 289 static int moonbr_io_flush(lua_State *L) {
jbe@81 290 return moonbr_io_write_impl(L, 0, 1);
jbe@81 291 }
jbe@81 292
jbe@81 293 static int moonbr_io_flush_nb(lua_State *L) {
jbe@81 294 return moonbr_io_write_impl(L, 1, 1);
jbe@80 295 }
jbe@80 296
jbe@88 297 static int moonbr_io_finish(lua_State *L) {
jbe@88 298 moonbr_io_handle_t *handle;
jbe@88 299 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@88 300 if (handle->fd < 0) luaL_error(L, "Attempt to finish a closed I/O handle");
jbe@88 301 if (handle->shutwr) luaL_error(L, "Attempt to finish a finished I/O handle");
jbe@88 302 handle->shutwr = 1;
jbe@88 303 if (handle->canshutdown) {
jbe@88 304 if (handle->writeleft) {
jbe@88 305 lua_pushcfunction(L, moonbr_io_flush);
jbe@88 306 lua_pushvalue(L, 1);
jbe@88 307 lua_call(L, 1, 2);
jbe@88 308 if (!lua_toboolean(L, -2)) return 2;
jbe@88 309 }
jbe@88 310 if (shutdown(handle->fd, SHUT_WR)) {
jbe@88 311 moonbr_io_errmsg();
jbe@88 312 lua_pushnil(L);
jbe@88 313 lua_pushstring(L, errmsg);
jbe@88 314 return 2;
jbe@88 315 }
jbe@88 316 }
jbe@88 317 lua_pushboolean(L, 1);
jbe@88 318 return 1;
jbe@88 319 }
jbe@88 320
jbe@87 321 static int moonbr_io_close(lua_State *L) {
jbe@83 322 moonbr_io_handle_t *handle;
jbe@87 323 int timeout;
jbe@83 324 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@87 325 timeout = luaL_optinteger(L, 2, -1);
jbe@83 326 if (handle->fd < 0) luaL_error(L, "Attempt to close a closed I/O handle");
jbe@87 327 if (timeout != 0) {
jbe@87 328 if (handle->writeleft) {
jbe@87 329 lua_pushcfunction(L, moonbr_io_flush);
jbe@87 330 lua_pushvalue(L, 1);
jbe@87 331 lua_call(L, 1, 2);
jbe@87 332 if (!lua_toboolean(L, -2)) {
jbe@87 333 close(handle->fd);
jbe@87 334 handle->fd = -1;
jbe@87 335 return 2;
jbe@87 336 }
jbe@83 337 }
jbe@87 338 moonbr_io_handle_set_linger(L, handle, timeout);
jbe@83 339 }
jbe@83 340 if (close(handle->fd)) {
jbe@83 341 moonbr_io_errmsg();
jbe@83 342 handle->fd = -1;
jbe@83 343 lua_pushnil(L);
jbe@83 344 lua_pushstring(L, errmsg);
jbe@83 345 return 2;
jbe@83 346 }
jbe@83 347 handle->fd = -1;
jbe@83 348 lua_pushboolean(L, 1);
jbe@83 349 return 1;
jbe@84 350
jbe@83 351 }
jbe@83 352
jbe@84 353 static int moonbr_io_reset(lua_State *L) {
jbe@87 354 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@87 355 lua_settop(L, 1);
jbe@87 356 lua_pushcfunction(L, moonbr_io_close);
jbe@87 357 lua_insert(L, 1);
jbe@87 358 lua_pushinteger(L, 0);
jbe@87 359 lua_call(L, 2, LUA_MULTRET);
jbe@87 360 return lua_gettop(L);
jbe@84 361 }
jbe@84 362
jbe@88 363 static int moonbr_io_gc(lua_State *L) {
jbe@88 364 moonbr_io_handle_t *handle;
jbe@88 365 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@88 366 if (handle->fd >= 0) {
jbe@88 367 lua_pushcfunction(L, moonbr_io_close);
jbe@88 368 lua_pushvalue(L, 1);
jbe@88 369 lua_pushinteger(L, 0);
jbe@88 370 lua_call(L, 2, 0);
jbe@88 371 }
jbe@88 372 return 0;
jbe@88 373 }
jbe@88 374
jbe@88 375 void moonbr_io_closehandle(lua_State *L, int idx, int timeout) {
jbe@88 376 moonbr_io_handle_t *handle;
jbe@88 377 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@88 378 if (handle->fd >= 0) {
jbe@88 379 lua_pushcfunction(L, moonbr_io_close);
jbe@88 380 lua_pushvalue(L, idx);
jbe@88 381 lua_pushinteger(L, timeout);
jbe@88 382 lua_call(L, 2, 0);
jbe@88 383 }
jbe@88 384 }
jbe@88 385
jbe@88 386 void moonbr_io_pushhandle(lua_State *L, int fd, int issocket, int canshutdown) {
jbe@79 387 moonbr_io_handle_t *handle;
jbe@79 388 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
jbe@79 389 handle->fd = fd;
jbe@84 390 handle->issocket = issocket;
jbe@88 391 handle->canshutdown = canshutdown;
jbe@88 392 handle->shutwr = 0;
jbe@81 393 handle->nonblocking = -1;
jbe@85 394 handle->readerr = 0;
jbe@85 395 handle->readbufcnt = 0;
jbe@81 396 handle->writeerr = 0;
jbe@81 397 handle->writeleft = 0;
jbe@83 398 handle->writeqin = 0;
jbe@83 399 handle->writeqout = 0;
jbe@81 400 handle->writeqoff = 0;
jbe@81 401 handle->writebufcnt = 0;
jbe@87 402 moonbr_io_handle_set_linger(L, handle, 0);
jbe@79 403 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 404 lua_setmetatable(L, -2);
jbe@79 405 lua_newtable(L); // uservalue
jbe@81 406 lua_newtable(L);
jbe@81 407 lua_setfield(L, -2, "writebuf");
jbe@79 408 lua_newtable(L); // public
jbe@79 409 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
jbe@79 410 lua_setmetatable(L, -2);
jbe@79 411 lua_setfield(L, -2, "public");
jbe@79 412 lua_setuservalue(L, -2);
jbe@79 413 }
jbe@79 414
jbe@79 415 static int moonbr_io_handleindex(lua_State *L) {
jbe@80 416 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 417 lua_getuservalue(L, 1);
jbe@79 418 lua_getfield(L, -1, "public");
jbe@79 419 lua_pushvalue(L, 2);
jbe@79 420 lua_gettable(L, -2);
jbe@79 421 return 1;
jbe@79 422 }
jbe@79 423
jbe@79 424 static int moonbr_io_handlenewindex(lua_State *L) {
jbe@80 425 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 426 lua_getuservalue(L, 1);
jbe@79 427 lua_getfield(L, -1, "public");
jbe@79 428 lua_pushvalue(L, 2);
jbe@79 429 lua_pushvalue(L, 3);
jbe@79 430 lua_settable(L, -3);
jbe@79 431 return 0;
jbe@79 432 }
jbe@79 433
jbe@79 434 static const struct luaL_Reg moonbr_io_handle_methods[] = {
jbe@85 435 {"read", moonbr_io_read},
jbe@85 436 {"read_nb", moonbr_io_read_nb},
jbe@86 437 {"drain", moonbr_io_drain},
jbe@86 438 {"drain_nb", moonbr_io_drain_nb},
jbe@80 439 {"write", moonbr_io_write},
jbe@81 440 {"write_nb", moonbr_io_write_nb},
jbe@80 441 {"flush", moonbr_io_flush},
jbe@81 442 {"flush_nb", moonbr_io_flush_nb},
jbe@88 443 {"finish", moonbr_io_finish},
jbe@87 444 {"close", moonbr_io_close},
jbe@85 445 {"reset", moonbr_io_reset},
jbe@79 446 {NULL, NULL}
jbe@79 447 };
jbe@79 448
jbe@79 449 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
jbe@79 450 {"__index", moonbr_io_handleindex},
jbe@79 451 {"__newindex", moonbr_io_handlenewindex},
jbe@88 452 {"__gc", moonbr_io_gc},
jbe@79 453 {NULL, NULL}
jbe@79 454 };
jbe@79 455
jbe@79 456 static const struct luaL_Reg moonbr_io_module_funcs[] = {
jbe@79 457 {NULL, NULL}
jbe@79 458 };
jbe@79 459
jbe@79 460 int luaopen_moonbridge_io(lua_State *L) {
jbe@79 461
jbe@80 462 lua_newtable(L); // module
jbe@80 463
jbe@79 464 lua_newtable(L); // public metatable
jbe@79 465 lua_newtable(L); // handle methods
jbe@79 466 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
jbe@80 467 lua_pushvalue(L, -1);
jbe@80 468 lua_setfield(L, -4, "handle");
jbe@79 469 lua_setfield(L, -2, "__index");
jbe@79 470 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
jbe@79 471
jbe@79 472 lua_newtable(L); // handle metatable
jbe@79 473 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
jbe@79 474 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 475
jbe@79 476 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
jbe@79 477 return 1;
jbe@79 478
jbe@79 479 }
jbe@79 480

Impressum / About Us