| 1644 | } |
| 1645 | |
| 1646 | static void dfhack_event_invoke(lua_State *L, int base, bool from_c) |
| 1647 | { |
| 1648 | int event = base+1; |
| 1649 | int num_args = lua_gettop(L)-event; |
| 1650 | |
| 1651 | int errorfun = base+2; |
| 1652 | lua_pushcfunction(L, dfhack_onerror); |
| 1653 | lua_insert(L, errorfun); |
| 1654 | |
| 1655 | int argbase = base+3; |
| 1656 | |
| 1657 | // stack: |base| event errorfun (args) |
| 1658 | |
| 1659 | if (!from_c) |
| 1660 | { |
| 1661 | // Invoke the NULL key first |
| 1662 | lua_rawgetp(L, event, NULL); |
| 1663 | |
| 1664 | if (lua_isnil(L, -1)) |
| 1665 | lua_pop(L, 1); |
| 1666 | else |
| 1667 | do_invoke_event(L, argbase, num_args, errorfun); |
| 1668 | } |
| 1669 | |
| 1670 | lua_pushnil(L); |
| 1671 | |
| 1672 | // stack: |base| event errorfun (args) key || cb (args) |
| 1673 | |
| 1674 | while (lua_next(L, event)) |
| 1675 | { |
| 1676 | // Skip the NULL key in the main loop |
| 1677 | if (is_null_userdata(L, -2)) |
| 1678 | lua_pop(L, 1); |
| 1679 | else |
| 1680 | do_invoke_event(L, argbase, num_args, errorfun); |
| 1681 | } |
| 1682 | |
| 1683 | lua_settop(L, base); |
| 1684 | } |
| 1685 | |
| 1686 | static int dfhack_event_call(lua_State *state) |
| 1687 | { |
no test coverage detected