Skip to content

Instantly share code, notes, and snippets.

@DarkWiiPlayer
Last active November 27, 2020 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarkWiiPlayer/bb0d153827990b4781f4f743d2bc7a3a to your computer and use it in GitHub Desktop.
Save DarkWiiPlayer/bb0d153827990b4781f4f743d2bc7a3a to your computer and use it in GitHub Desktop.
local t = setmetatable({}, {__name="foobar"})
print(type(t)) --> foobar
print(rawtype(t)) --> table
diff --unified -r lua-5.4.0/src/lapi.c lua-5.4.0-rawtype/src/lapi.c
--- lua-5.4.0/src/lapi.c 2020-06-18 16:25:53.000000000 +0200
+++ lua-5.4.0-rawtype/src/lapi.c 2020-11-27 09:31:30.787330086 +0100
@@ -267,6 +267,13 @@
}
+LUA_API const char *lua_objtypename (lua_State *L, int t) {
+ api_check(L, LUA_TNONE <= t && t < LUA_NUMTYPES, "invalid type");
+ const TValue *o = index2value(L, t);
+ return luaT_objtypename(L, o);
+}
+
+
LUA_API int lua_iscfunction (lua_State *L, int idx) {
const TValue *o = index2value(L, idx);
return (ttislcf(o) || (ttisCclosure(o)));
diff --unified -r lua-5.4.0/src/lbaselib.c lua-5.4.0-rawtype/src/lbaselib.c
--- lua-5.4.0/src/lbaselib.c 2020-06-18 16:25:53.000000000 +0200
+++ lua-5.4.0-rawtype/src/lbaselib.c 2020-11-27 09:38:19.813573132 +0100
@@ -240,7 +240,7 @@
}
-static int luaB_type (lua_State *L) {
+static int luaB_rawtype (lua_State *L) {
int t = lua_type(L, 1);
luaL_argcheck(L, t != LUA_TNONE, 1, "value expected");
lua_pushstring(L, lua_typename(L, t));
@@ -248,6 +248,16 @@
}
+static int luaB_type (lua_State *L) {
+ int t = lua_type(L, 1);
+ luaL_argcheck(L, t != LUA_TNONE, 1, "value expected");
+ //lua_pushstring(L, lua_typename(L, t));
+ const char *s = lua_objtypename(L, -1);
+ lua_pushstring(L, s);
+ return 1;
+}
+
+
static int luaB_next (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
@@ -504,6 +514,7 @@
{"tonumber", luaB_tonumber},
{"tostring", luaB_tostring},
{"type", luaB_type},
+ {"rawtype", luaB_rawtype},
{"xpcall", luaB_xpcall},
/* placeholders */
{LUA_GNAME, NULL},
diff --unified -r lua-5.4.0/src/lua.h lua-5.4.0-rawtype/src/lua.h
--- lua-5.4.0/src/lua.h 2020-06-18 16:25:54.000000000 +0200
+++ lua-5.4.0-rawtype/src/lua.h 2020-11-27 09:33:22.347601208 +0100
@@ -186,6 +186,7 @@
LUA_API int (lua_isuserdata) (lua_State *L, int idx);
LUA_API int (lua_type) (lua_State *L, int idx);
LUA_API const char *(lua_typename) (lua_State *L, int tp);
+LUA_API const char *(lua_objtypename) (lua_State *L, int tp);
LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum);
LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum);
Only in lua-5.4.0-rawtype/src: tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment