MCPcopy Create free account
hub / github.com/DFHack/dfhack / luaL_tolstring

Function luaL_tolstring

depends/lua/src/lauxlib.c:810–845  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

808
809
810LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
811 if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */
812 if (!lua_isstring(L, -1))
813 luaL_error(L, "'__tostring' must return a string");
814 }
815 else {
816 switch (lua_type(L, idx)) {
817 case LUA_TNUMBER: {
818 if (lua_isinteger(L, idx))
819 lua_pushfstring(L, "%I", (LUAI_UACINT)lua_tointeger(L, idx));
820 else
821 lua_pushfstring(L, "%f", (LUAI_UACNUMBER)lua_tonumber(L, idx));
822 break;
823 }
824 case LUA_TSTRING:
825 lua_pushvalue(L, idx);
826 break;
827 case LUA_TBOOLEAN:
828 lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false"));
829 break;
830 case LUA_TNIL:
831 lua_pushliteral(L, "nil");
832 break;
833 default: {
834 int tt = luaL_getmetafield(L, idx, "__name"); /* try name */
835 const char *kind = (tt == LUA_TSTRING) ? lua_tostring(L, -1) :
836 luaL_typename(L, idx);
837 lua_pushfstring(L, "%s: %p", kind, lua_topointer(L, idx));
838 if (tt != LUA_TNIL)
839 lua_remove(L, -2); /* remove '__name' */
840 break;
841 }
842 }
843 }
844 return lua_tolstring(L, -1, len);
845}
846
847
848/*

Callers 4

luaB_tostringFunction · 0.85
add_sFunction · 0.85
addliteralFunction · 0.85
str_formatFunction · 0.85

Calls 12

luaL_callmetaFunction · 0.85
lua_isstringFunction · 0.85
luaL_errorFunction · 0.85
lua_typeFunction · 0.85
lua_isintegerFunction · 0.85
lua_pushfstringFunction · 0.85
lua_pushvalueFunction · 0.85
lua_pushstringFunction · 0.85
lua_tobooleanFunction · 0.85
luaL_getmetafieldFunction · 0.85
lua_topointerFunction · 0.85
lua_tolstringFunction · 0.85

Tested by

no test coverage detected