| 920 | |
| 921 | |
| 922 | LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { |
| 923 | idx = lua_absindex(L,idx); |
| 924 | if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */ |
| 925 | if (!lua_isstring(L, -1)) |
| 926 | luaL_error(L, "'__tostring' must return a string"); |
| 927 | } |
| 928 | else { |
| 929 | switch (lua_type(L, idx)) { |
| 930 | case LUA_TNUMBER: { |
| 931 | char buff[LUA_N2SBUFFSZ]; |
| 932 | lua_numbertocstring(L, idx, buff); |
| 933 | lua_pushstring(L, buff); |
| 934 | break; |
| 935 | } |
| 936 | case LUA_TSTRING: |
| 937 | lua_pushvalue(L, idx); |
| 938 | break; |
| 939 | case LUA_TBOOLEAN: |
| 940 | lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false")); |
| 941 | break; |
| 942 | case LUA_TNIL: |
| 943 | lua_pushliteral(L, "nil"); |
| 944 | break; |
| 945 | default: { |
| 946 | int tt = luaL_getmetafield(L, idx, "__name"); /* try name */ |
| 947 | const char *kind = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : |
| 948 | luaL_typename(L, idx); |
| 949 | lua_pushfstring(L, "%s: %p", kind, lua_topointer(L, idx)); |
| 950 | if (tt != LUA_TNIL) |
| 951 | lua_remove(L, -2); /* remove '__name' */ |
| 952 | break; |
| 953 | } |
| 954 | } |
| 955 | } |
| 956 | return lua_tolstring(L, -1, len); |
| 957 | } |
| 958 | |
| 959 | |
| 960 | /* |
no test coverage detected