From f8f8a9843fbcccfb5fb3f50bb3defe88191226e1 Mon Sep 17 00:00:00 2001 From: Josh Yelon Date: Mon, 19 Jul 2021 16:15:22 -0400 Subject: [PATCH] Add isclass operator, make inspector print classes nicely --- luprex/core/cpp/source.cpp | 24 ++++++++++++++++++++++++ luprex/core/cpp/source.hpp | 3 +++ luprex/core/lua/inspect.lua | 8 +++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/luprex/core/cpp/source.cpp b/luprex/core/cpp/source.cpp index 9d1c730d..29a967a8 100644 --- a/luprex/core/cpp/source.cpp +++ b/luprex/core/cpp/source.cpp @@ -40,6 +40,30 @@ LuaDefine(source_makeclass, "f") { return LS.result(); } +LuaDefine(source_isclass, "f") { + LuaArg table; + LuaVar classname, globtab, classtab; + LuaRet result; + LuaStack LS(L, table, classname, globtab, classtab, result); + if (!LS.istable(table)) { + LS.set(result, false); + return LS.result(); + } + LS.rawget(classname, table, "__class"); + if (!LS.isstring(classname)) { + LS.set(result, false); + return LS.result(); + } + LS.getglobaltable(globtab); + LS.rawget(classtab, globtab, classname); + if (!LS.rawequal(classtab, table)) { + LS.set(result, false); + return LS.result(); + } + LS.set(result, true); + return LS.result(); +} + LuaDefine(source_maketangible, "f") { LuaArg classname; LuaRet classtab; diff --git a/luprex/core/cpp/source.hpp b/luprex/core/cpp/source.hpp index e185e21d..fcfb628e 100644 --- a/luprex/core/cpp/source.hpp +++ b/luprex/core/cpp/source.hpp @@ -165,6 +165,9 @@ public: // int source_makeclass(lua_State *L); +// Return true if the specified table is a class created by 'makeclass' +// +int source_isclass(lua_State *L); #endif // SOURCE_HPP diff --git a/luprex/core/lua/inspect.lua b/luprex/core/lua/inspect.lua index 218cf53f..2e30dcb2 100644 --- a/luprex/core/lua/inspect.lua +++ b/luprex/core/lua/inspect.lua @@ -271,7 +271,13 @@ function Inspector:putTable(t) if count > 0 then self:puts(',') end self:tabify() self:puts(' = ') - self:putValue(mt) + if isclass(mt) then + self:puts('') + else + self:putValue(mt) + end end end)