| 651 | }; |
| 652 | |
| 653 | int |
| 654 | l_obj_register(lua_State *L) |
| 655 | { |
| 656 | /* Table of instance methods (e.g. an_object:isnull()) |
| 657 | and static methods (e.g. obj.new("dagger")). */ |
| 658 | luaL_newlib(L, l_obj_methods); |
| 659 | |
| 660 | /* metatable = { __name = "obj", __gc = l_obj_gc } */ |
| 661 | luaL_newmetatable(L, "obj"); |
| 662 | luaL_setfuncs(L, l_obj_meta, 0); |
| 663 | /* metatable.__index points at the object method table. */ |
| 664 | lua_pushvalue(L, -2); |
| 665 | lua_setfield(L, -2, "__index"); |
| 666 | |
| 667 | /* Don't let lua code mess with the real metatable. |
| 668 | Instead offer a fake one that only contains __gc. */ |
| 669 | luaL_newlib(L, l_obj_meta); |
| 670 | lua_setfield(L, -2, "__metatable"); |
| 671 | |
| 672 | /* We don't need the metatable anymore. It's safe in the |
| 673 | Lua registry for use by luaL_setmetatable. */ |
| 674 | lua_pop(L, 1); |
| 675 | |
| 676 | /* global obj = the method table we created at the start */ |
| 677 | lua_setglobal(L, "obj"); |
| 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | /* for 'onefile' processing where end of this file isn't necessarily the |
| 682 | end of the source code seen by the compiler */ |