Implement effil.type
Implement effil.type and test
This commit is contained in:
parent
5057116136
commit
4e5e3e9c18
@ -29,6 +29,19 @@ sol::object createChannel(sol::optional<int> capacity, sol::this_state lua) {
|
|||||||
|
|
||||||
SharedTable globalTable = GC::instance().create<SharedTable>();
|
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
|
} // namespace
|
||||||
|
|
||||||
extern "C" int luaopen_libeffil(lua_State* L) {
|
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),
|
"G", sol::make_object(lua, globalTable),
|
||||||
"getmetatable", SharedTable::luaGetMetatable,
|
"getmetatable", SharedTable::luaGetMetatable,
|
||||||
"gc", GC::getLuaApi(lua),
|
"gc", GC::getLuaApi(lua),
|
||||||
"channel", createChannel
|
"channel", createChannel,
|
||||||
|
"userdata_type", userdataType
|
||||||
);
|
);
|
||||||
sol::stack::push(lua, publicApi);
|
sol::stack::push(lua, publicApi);
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
@ -200,7 +200,7 @@ StoredArray SharedTable::luaCall(sol::this_state state, const sol::variadic_args
|
|||||||
sol::object SharedTable::luaToString(sol::this_state state) {
|
sol::object SharedTable::luaToString(sol::this_state state) {
|
||||||
DEFFINE_METAMETHOD_CALL_0("__tostring");
|
DEFFINE_METAMETHOD_CALL_0("__tostring");
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "effil::table (0x" << std::hex << this << ")";
|
ss << "effil.table: " << data_.get();
|
||||||
return sol::make_object(state, ss.str());
|
return sol::make_object(state, ss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -27,6 +27,15 @@ local api = {
|
|||||||
channel = capi.channel
|
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, ...)
|
local function run_thread(config, f, ...)
|
||||||
return capi.thread(config.path, config.cpath, config.managed, config.step, f, ...)
|
return capi.thread(config.path, config.cpath, config.managed, config.step, f, ...)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -36,6 +36,7 @@ require 'thread'
|
|||||||
require 'shared_table'
|
require 'shared_table'
|
||||||
require 'gc'
|
require 'gc'
|
||||||
require 'channel'
|
require 'channel'
|
||||||
|
require 'type'
|
||||||
|
|
||||||
-- Hack tests functions to print when test starts
|
-- Hack tests functions to print when test starts
|
||||||
for suite_name, suite in pairs(_G) do
|
for suite_name, suite in pairs(_G) do
|
||||||
|
|||||||
11
tests/lua/type.lua
Normal file
11
tests/lua/type.lua
Normal 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
|
||||||
Loading…
x
Reference in New Issue
Block a user