summaryrefslogtreecommitdiff
path: root/src/script/lua_api
diff options
context:
space:
mode:
authorPaul Ouellette <oue.paul18@gmail.com>2020-06-09 13:37:25 -0400
committerGitHub <noreply@github.com>2020-06-09 19:37:25 +0200
commitb16f841756ef86e83710ad2fddf2cd5bafdf4bcc (patch)
tree777e5dee13e1e2d20a7f8e4a5b8df19a7fad6b85 /src/script/lua_api
parent09e285f38cd96b4278b921ab82c5266082bb1a0d (diff)
downloadminetest-b16f841756ef86e83710ad2fddf2cd5bafdf4bcc.tar.gz
minetest-b16f841756ef86e83710ad2fddf2cd5bafdf4bcc.tar.bz2
minetest-b16f841756ef86e83710ad2fddf2cd5bafdf4bcc.zip
LuaItemStack: Add __tostring metamethod (#8785)
* LuaItemStack: Add __tostring metamethod * Clean up LuaItemStack::checkobject
Diffstat (limited to 'src/script/lua_api')
-rw-r--r--src/script/lua_api/l_item.cpp25
-rw-r--r--src/script/lua_api/l_item.h3
2 files changed, 21 insertions, 7 deletions
diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp
index 0a403acbd..d67cab76f 100644
--- a/src/script/lua_api/l_item.cpp
+++ b/src/script/lua_api/l_item.cpp
@@ -37,6 +37,15 @@ int LuaItemStack::gc_object(lua_State *L)
return 0;
}
+// __tostring metamethod
+int LuaItemStack::mt_tostring(lua_State *L)
+{
+ LuaItemStack *o = checkobject(L, 1);
+ std::string itemstring = o->m_stack.getItemString(false);
+ lua_pushfstring(L, "ItemStack(\"%s\")", itemstring.c_str());
+ return 1;
+}
+
// is_empty(self) -> true/false
int LuaItemStack::l_is_empty(lua_State *L)
{
@@ -433,12 +442,9 @@ int LuaItemStack::create(lua_State *L, const ItemStack &item)
return 1;
}
-LuaItemStack* LuaItemStack::checkobject(lua_State *L, int narg)
+LuaItemStack *LuaItemStack::checkobject(lua_State *L, int narg)
{
- luaL_checktype(L, narg, LUA_TUSERDATA);
- void *ud = luaL_checkudata(L, narg, className);
- if(!ud) luaL_typerror(L, narg, className);
- return *(LuaItemStack**)ud; // unbox pointer
+ return *(LuaItemStack **)luaL_checkudata(L, narg, className);
}
void LuaItemStack::Register(lua_State *L)
@@ -448,9 +454,10 @@ void LuaItemStack::Register(lua_State *L)
luaL_newmetatable(L, className);
int metatable = lua_gettop(L);
+ // hide metatable from Lua getmetatable()
lua_pushliteral(L, "__metatable");
lua_pushvalue(L, methodtable);
- lua_settable(L, metatable); // hide metatable from Lua getmetatable()
+ lua_settable(L, metatable);
lua_pushliteral(L, "__index");
lua_pushvalue(L, methodtable);
@@ -460,12 +467,16 @@ void LuaItemStack::Register(lua_State *L)
lua_pushcfunction(L, gc_object);
lua_settable(L, metatable);
+ lua_pushliteral(L, "__tostring");
+ lua_pushcfunction(L, mt_tostring);
+ lua_settable(L, metatable);
+
lua_pop(L, 1); // drop metatable
luaL_openlib(L, 0, methods, 0); // fill methodtable
lua_pop(L, 1); // drop methodtable
- // Can be created from Lua (LuaItemStack(itemstack or itemstring or table or nil))
+ // Can be created from Lua (ItemStack(itemstack or itemstring or table or nil))
lua_register(L, className, create_object);
}
diff --git a/src/script/lua_api/l_item.h b/src/script/lua_api/l_item.h
index 6fab58045..98744c071 100644
--- a/src/script/lua_api/l_item.h
+++ b/src/script/lua_api/l_item.h
@@ -34,6 +34,9 @@ private:
// garbage collector
static int gc_object(lua_State *L);
+ // __tostring metamethod
+ static int mt_tostring(lua_State *L);
+
// is_empty(self) -> true/false
static int l_is_empty(lua_State *L);