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