| 1682 | } |
| 1683 | |
| 1684 | LuaCallbackInfo* CreateCallback(lua_State* L, int callback_stack_index) |
| 1685 | { |
| 1686 | luaL_checktype(L, callback_stack_index, LUA_TFUNCTION); |
| 1687 | |
| 1688 | DM_LUA_STACK_CHECK(L, 0); |
| 1689 | |
| 1690 | GetInstance(L); |
| 1691 | // [-1] instance |
| 1692 | |
| 1693 | if (!GetMetaFunction(L, -1, META_GET_INSTANCE_CONTEXT_TABLE_REF, sizeof(META_GET_INSTANCE_CONTEXT_TABLE_REF) - 1)) |
| 1694 | { |
| 1695 | dmLogError("CreateCallback failed: missing %s meta function for instance context table", "INSTANCE_CONTEXT"); |
| 1696 | lua_pop(L, 1); |
| 1697 | return 0x0; |
| 1698 | } |
| 1699 | // [-2] instance |
| 1700 | // [-1] META_GET_INSTANCE_CONTEXT_TABLE_REF() |
| 1701 | |
| 1702 | lua_pushvalue(L, -2); |
| 1703 | // [-3] instance |
| 1704 | // [-2] META_GET_INSTANCE_CONTEXT_TABLE_REF() |
| 1705 | // [-1] instance |
| 1706 | |
| 1707 | lua_call(L, 1, 1); |
| 1708 | // [-2] instance |
| 1709 | // [-1] instance context table ref |
| 1710 | assert(lua_type(L, -1) == LUA_TNUMBER); |
| 1711 | |
| 1712 | int context_table_ref = lua_tonumber(L, -1); |
| 1713 | |
| 1714 | lua_pop(L, 1); |
| 1715 | // [-1] instance |
| 1716 | |
| 1717 | uint32_t unique_script_id = INVALID_SCRIPT_ID; |
| 1718 | if (!GetMetaFunction(L, -1, META_GET_UNIQUE_SCRIPT_ID, sizeof(META_GET_UNIQUE_SCRIPT_ID) - 1)) |
| 1719 | { |
| 1720 | dmLogError("CreateCallback failed: missing %s meta function for instance context table", "UNIQUE_SCRIPT"); |
| 1721 | lua_pop(L, 1); |
| 1722 | return 0x0; |
| 1723 | } |
| 1724 | // [-2] instance |
| 1725 | // [-1] META_GET_UNIQUE_SCRIPT_ID() |
| 1726 | lua_pushvalue(L, -2); // push instance |
| 1727 | // [-3] instance |
| 1728 | // [-2] META_GET_UNIQUE_SCRIPT_ID() |
| 1729 | // [-1] instance |
| 1730 | lua_call(L, 1, 1); // call __get_unique_script_id(self) |
| 1731 | // [-2] instance |
| 1732 | // [-1] unique script id |
| 1733 | unique_script_id = (uint32_t)lua_tointeger(L, -1); |
| 1734 | lua_pop(L, 1); |
| 1735 | // [-1] instance |
| 1736 | |
| 1737 | lua_pop(L, 1); |
| 1738 | |
| 1739 | lua_pushvalue(L, callback_stack_index); |
| 1740 | // [-1] callback |
| 1741 | |