| 1137 | } |
| 1138 | |
| 1139 | bool DFHack::Lua::SafeCallString(color_ostream &out, lua_State *state, const std::string &code, |
| 1140 | int nargs, int nres, bool perr, |
| 1141 | const char *debug_tag, int env_idx) |
| 1142 | { |
| 1143 | AssertCoreSuspend(state); |
| 1144 | |
| 1145 | if (!debug_tag) |
| 1146 | debug_tag = code.c_str(); |
| 1147 | if (env_idx) |
| 1148 | env_idx = lua_absindex(state, env_idx); |
| 1149 | |
| 1150 | int base = lua_gettop(state); |
| 1151 | (void)base; // used in assert() |
| 1152 | |
| 1153 | // Parse the code |
| 1154 | if (luaL_loadbuffer(state, code.data(), code.size(), debug_tag) != LUA_OK) |
| 1155 | { |
| 1156 | if (perr) |
| 1157 | report_error(state, &out, true); |
| 1158 | |
| 1159 | return false; |
| 1160 | } |
| 1161 | |
| 1162 | // Replace _ENV |
| 1163 | if (env_idx) |
| 1164 | { |
| 1165 | lua_pushvalue(state, env_idx); |
| 1166 | lua_setupvalue(state, -2, 1); |
| 1167 | assert(lua_gettop(state) == base+1); |
| 1168 | } |
| 1169 | |
| 1170 | if (nargs > 0) |
| 1171 | lua_insert(state, -1-nargs); |
| 1172 | |
| 1173 | return Lua::SafeCall(out, state, nargs, nres, perr); |
| 1174 | } |
| 1175 | |
| 1176 | /* |
| 1177 | * Coroutine interactive query loop |
nothing calls this directly
no test coverage detected