| 1787 | }}} |
| 1788 | |
| 1789 | lua_State *DFHack::Lua::Open(color_ostream &out, lua_State *state) |
| 1790 | { |
| 1791 | if (!state) |
| 1792 | state = luaL_newstate(); |
| 1793 | |
| 1794 | interrupt_init(state); |
| 1795 | |
| 1796 | luaL_openlibs(state); |
| 1797 | AttachDFGlobals(state); |
| 1798 | |
| 1799 | // Table of query coroutines |
| 1800 | lua_newtable(state); |
| 1801 | lua_rawsetp(state, LUA_REGISTRYINDEX, &DFHACK_QUERY_COROTABLE_TOKEN); |
| 1802 | |
| 1803 | // Replace the print function of the standard library |
| 1804 | lua_pushcfunction(state, lua_dfhack_println); |
| 1805 | lua_setglobal(state, "print"); |
| 1806 | |
| 1807 | lua_getglobal(state, "require"); |
| 1808 | lua_rawsetp(state, LUA_REGISTRYINDEX, &DFHACK_REQUIRE_TOKEN); |
| 1809 | lua_getglobal(state, "tostring"); |
| 1810 | lua_rawsetp(state, LUA_REGISTRYINDEX, &DFHACK_TOSTRING_TOKEN); |
| 1811 | |
| 1812 | // Create the dfhack global |
| 1813 | lua_newtable(state); |
| 1814 | |
| 1815 | lua_dup(state); |
| 1816 | lua_rawsetp(state, LUA_REGISTRYINDEX, &DFHACK_DFHACK_TOKEN); |
| 1817 | |
| 1818 | lua_rawgeti(state, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS); |
| 1819 | lua_dup(state); |
| 1820 | lua_rawsetp(state, LUA_REGISTRYINDEX, &DFHACK_BASE_G_TOKEN); |
| 1821 | lua_setfield(state, -2, "BASE_G"); |
| 1822 | |
| 1823 | lua_pushstring(state, Version::dfhack_version()); |
| 1824 | lua_setfield(state, -2, "VERSION"); |
| 1825 | lua_pushstring(state, Version::df_version()); |
| 1826 | lua_setfield(state, -2, "DF_VERSION"); |
| 1827 | lua_pushstring(state, Version::dfhack_release()); |
| 1828 | lua_setfield(state, -2, "RELEASE"); |
| 1829 | |
| 1830 | lua_pushboolean(state, IsCoreContext(state)); |
| 1831 | lua_setfield(state, -2, "is_core_context"); |
| 1832 | |
| 1833 | // Create the metatable for exceptions |
| 1834 | lua_newtable(state); |
| 1835 | lua_pushcfunction(state, dfhack_exception_tostring); |
| 1836 | lua_setfield(state, -2, "__tostring"); |
| 1837 | lua_pushcfunction(state, dfhack_exception_tostring); |
| 1838 | lua_setfield(state, -2, "tostring"); |
| 1839 | lua_dup(state); |
| 1840 | lua_rawsetp(state, LUA_REGISTRYINDEX, &DFHACK_EXCEPTION_META_TOKEN); |
| 1841 | lua_setfield(state, -2, "exception"); |
| 1842 | |
| 1843 | lua_newtable(state); |
| 1844 | lua_pushcfunction(state, dfhack_event_call); |
| 1845 | lua_setfield(state, -2, "__call"); |
| 1846 | lua_pushcfunction(state, dfhack_event_len); |
no test coverage detected