diff --git a/src/cpp/lua-module.cpp b/src/cpp/lua-module.cpp index bfb0bf9..a5ffa3d 100644 --- a/src/cpp/lua-module.cpp +++ b/src/cpp/lua-module.cpp @@ -29,6 +29,19 @@ sol::object createChannel(sol::optional capacity, sol::this_state lua) { SharedTable globalTable = GC::instance().create(); +std::string userdataType(const sol::object& something) { + assert(something.get_type() == sol::type::userdata); + if (something.template is()) { + return "effil.table"; + } else if (something.template is()) { + return "effil.channel"; + } else if (something.template is>()) { + return "effil.thread"; + } else { + return "userdata"; + } +} + } // namespace extern "C" int luaopen_libeffil(lua_State* L) { @@ -50,7 +63,8 @@ extern "C" int luaopen_libeffil(lua_State* L) { "G", sol::make_object(lua, globalTable), "getmetatable", SharedTable::luaGetMetatable, "gc", GC::getLuaApi(lua), - "channel", createChannel + "channel", createChannel, + "userdata_type", userdataType ); sol::stack::push(lua, publicApi); return 1; diff --git a/src/cpp/shared-table.cpp b/src/cpp/shared-table.cpp index d752c57..6155be0 100644 --- a/src/cpp/shared-table.cpp +++ b/src/cpp/shared-table.cpp @@ -200,7 +200,7 @@ StoredArray SharedTable::luaCall(sol::this_state state, const sol::variadic_args sol::object SharedTable::luaToString(sol::this_state state) { DEFFINE_METAMETHOD_CALL_0("__tostring"); std::stringstream ss; - ss << "effil::table (0x" << std::hex << this << ")"; + ss << "effil.table: " << data_.get(); return sol::make_object(state, ss.str()); } diff --git a/src/lua/effil.lua b/src/lua/effil.lua index d3360e7..ef29b86 100644 --- a/src/lua/effil.lua +++ b/src/lua/effil.lua @@ -27,6 +27,15 @@ local api = { channel = capi.channel } +api.type = function (something) + local t = type(something) + if (t ~= "userdata") then + return t + else + return capi.userdata_type(something) + end +end + local function run_thread(config, f, ...) return capi.thread(config.path, config.cpath, config.managed, config.step, f, ...) end diff --git a/tests/lua/run_tests.lua b/tests/lua/run_tests.lua index 2d6bc18..fdc470e 100755 --- a/tests/lua/run_tests.lua +++ b/tests/lua/run_tests.lua @@ -36,6 +36,7 @@ require 'thread' require 'shared_table' require 'gc' require 'channel' +require 'type' -- Hack tests functions to print when test starts for suite_name, suite in pairs(_G) do diff --git a/tests/lua/type.lua b/tests/lua/type.lua new file mode 100644 index 0000000..749c471 --- /dev/null +++ b/tests/lua/type.lua @@ -0,0 +1,11 @@ +local effil = require 'effil' + +TestType = { tearDown = tearDown } + +function TestType:testType() + test.assertEquals(effil.type(1), "number") + test.assertEquals(effil.type("string"), "string") + test.assertEquals(effil.type(effil.table()), "effil.table") + test.assertEquals(effil.type(effil.channel()), "effil.channel") + test.assertEquals(effil.type(effil.thread(function() end)()), "effil.thread") +end