Implement effil.type

Implement effil.type and test
This commit is contained in:
Ilia 2017-05-23 13:13:11 +03:00 committed by mihacooper
parent 5057116136
commit 4e5e3e9c18
5 changed files with 37 additions and 2 deletions

View File

@ -29,6 +29,19 @@ sol::object createChannel(sol::optional<int> capacity, sol::this_state lua) {
SharedTable globalTable = GC::instance().create<SharedTable>();
std::string userdataType(const sol::object& something) {
assert(something.get_type() == sol::type::userdata);
if (something.template is<SharedTable>()) {
return "effil.table";
} else if (something.template is<Channel>()) {
return "effil.channel";
} else if (something.template is<std::shared_ptr<Thread>>()) {
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;

View File

@ -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());
}

View File

@ -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

View File

@ -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

11
tests/lua/type.lua Normal file
View File

@ -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