diff options
Diffstat (limited to 'lauxlib.c')
| -rw-r--r-- | lauxlib.c | 17 |
1 files changed, 15 insertions, 2 deletions
| @@ -501,12 +501,25 @@ static const luaL_Reg boxmt[] = { /* box metamethods */ | |||
| 501 | }; | 501 | }; |
| 502 | 502 | ||
| 503 | 503 | ||
| 504 | /* | ||
| 505 | ** Get/create metatable (MT) for boxes | ||
| 506 | */ | ||
| 507 | static void getBoxMT (lua_State *L) { | ||
| 508 | const char *BOXMT = "_UBOX*"; /* key for the metatable */ | ||
| 509 | if (luaL_getmetatable(L, BOXMT) == LUA_TNIL) { /* MT not created yet? */ | ||
| 510 | luaL_newlibtable(L, boxmt); /* create it */ | ||
| 511 | luaL_setfuncs(L, boxmt, 0); /* initialize it */ | ||
| 512 | lua_copy(L, -1, -2); /* change stack from nil,MT to MT,MT */ | ||
| 513 | lua_setfield(L, LUA_REGISTRYINDEX, BOXMT); /* store MT in the registry */ | ||
| 514 | } | ||
| 515 | } | ||
| 516 | |||
| 517 | |||
| 504 | static void newbox (lua_State *L) { | 518 | static void newbox (lua_State *L) { |
| 505 | UBox *box = (UBox *)lua_newuserdatauv(L, sizeof(UBox), 0); | 519 | UBox *box = (UBox *)lua_newuserdatauv(L, sizeof(UBox), 0); |
| 506 | box->box = NULL; | 520 | box->box = NULL; |
| 507 | box->bsize = 0; | 521 | box->bsize = 0; |
| 508 | if (luaL_newmetatable(L, "_UBOX*")) /* creating metatable? */ | 522 | getBoxMT(L); |
| 509 | luaL_setfuncs(L, boxmt, 0); /* set its metamethods */ | ||
| 510 | lua_setmetatable(L, -2); | 523 | lua_setmetatable(L, -2); |
| 511 | } | 524 | } |
| 512 | 525 | ||
