MCPcopy Create free account
hub / github.com/NetHack/NetHack / nhl_variable

Function nhl_variable

src/nhlua.c:1220–1293  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1218 nh.variable("test", 10);
1219 local ten = nh.variable("test"); */
1220staticfn int
1221nhl_variable(lua_State *L)
1222{
1223 int argc = lua_gettop(L);
1224 int typ;
1225 const char *key;
1226
1227 if (!gl.luacore) {
1228 panic("nh luacore not inited");
1229 /*NOTREACHED*/
1230 return 0;
1231 }
1232
1233 lua_getglobal(gl.luacore, "nh_lua_variables");
1234 if (!lua_istable(gl.luacore, -1)) {
1235 impossible("nh_lua_variables is not a lua table");
1236 return 0;
1237 }
1238
1239 if (argc == 1) {
1240 key = luaL_checkstring(L, 1);
1241
1242 lua_getfield(gl.luacore, -1, key);
1243 typ = lua_type(gl.luacore, -1);
1244 if (typ == LUA_TSTRING)
1245 lua_pushstring(L, lua_tostring(gl.luacore, -1));
1246 else if (typ == LUA_TNIL)
1247 lua_pushnil(L);
1248 else if (typ == LUA_TBOOLEAN)
1249 lua_pushboolean(L, lua_toboolean(gl.luacore, -1));
1250 else if (typ == LUA_TNUMBER)
1251 lua_pushinteger(L, lua_tointeger(gl.luacore, -1));
1252 else if (typ == LUA_TTABLE) {
1253 lua_getglobal(gl.luacore, "nh_get_variables_string");
1254 lua_pushvalue(gl.luacore, -2);
1255 nhl_pcall_handle(gl.luacore, 1, 1, "nhl_variable", NHLpa_panic);
1256 luaL_loadstring(L, lua_tostring(gl.luacore, -1));
1257 nhl_pcall_handle(L, 0, 1, "nhl_variable-1", NHLpa_panic);
1258 } else
1259 nhl_error(L, "Cannot get variable of that type");
1260 return 1;
1261 } else if (argc == 2) {
1262 /* set nh_lua_variables[key] = value;
1263 nh.variable("key", value); */
1264 key = luaL_checkstring(L, 1);
1265 //pline("SETVAR:%s", key);
1266 typ = lua_type(L, -1);
1267
1268 if (typ == LUA_TSTRING) {
1269 lua_pushstring(gl.luacore, lua_tostring(L, -1));
1270 lua_setfield(gl.luacore, -2, key);
1271 } else if (typ == LUA_TNIL) {
1272 lua_pushnil(gl.luacore);
1273 lua_setfield(gl.luacore, -2, key);
1274 } else if (typ == LUA_TBOOLEAN) {
1275 lua_pushboolean(gl.luacore, lua_toboolean(L, -1));
1276 lua_setfield(gl.luacore, -2, key);
1277 } else if (typ == LUA_TNUMBER) {

Callers

nothing calls this directly

Calls 4

nhl_pcall_handleFunction · 0.85
nhl_errorFunction · 0.85
impossibleFunction · 0.70
panicFunction · 0.50

Tested by

no test coverage detected