From 47ef63d725d7daa2562703e0179ebe4b15a705bc Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sat, 21 Jul 2012 17:00:28 +0100 Subject: SUPPLE.OBJECTS: Everything tested and managing of local and remote objects looks right --- lib/supple/objects.lua | 8 ++-- test/test-supple.objects.lua | 112 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 116 insertions(+), 4 deletions(-) diff --git a/lib/supple/objects.lua b/lib/supple/objects.lua index 67cb71b..6c28cae 100644 --- a/lib/supple/objects.lua +++ b/lib/supple/objects.lua @@ -96,9 +96,10 @@ local function receive(obj) obj.methods[#obj.methods+1] = "__newindex" end for _, name in ipairs(obj.methods or {}) do - mt[name] = function(...) - proc_call(tag, name, ...) - end + local function meta_func(...) + return proc_call(tag, name, ...) + end + mt[name] = meta_func end -- And return the proxy object return proxy @@ -108,4 +109,5 @@ return { set_name = set_name, set_proc_call = set_proc_call, give = give, + receive = receive, } diff --git a/test/test-supple.objects.lua b/test/test-supple.objects.lua index cb81420..829de47 100644 --- a/test/test-supple.objects.lua +++ b/test/test-supple.objects.lua @@ -12,6 +12,7 @@ local luacov = require 'luacov' local objects = require 'supple.objects' +local capi = require 'supple.capi' local testnames = {} @@ -47,9 +48,118 @@ end function suite.give_table() local tab = {} + objects.set_name("give_table") local expn = objects.give(tab) assert(tab ~= expn, "Table passed through") - + assert(expn.tag:match("^give_table:"), "Tag didn't come through") + local expn2 = objects.give(tab) + assert(expn ~= expn2, "Expansion was the same") + assert(expn.tag == expn2.tag, "Expansion tags did not match") +end + +function suite.receive_integral_types() + assert(objects.receive(nil) == nil, "Nil didn't pass through") + assert(objects.receive(true) == true, "True didn't pass through") + assert(objects.receive(false) == false, "False didn't pass through") + assert(objects.receive(123) == 123, "Number didn't pass through") + assert(objects.receive("") == "", "String didn't pass through") +end + +function suite.receive_function() + local last_cb = nil + local function cb(tag, name, ...) + last_cb = { + tag = tag, + name = name, + args = {...} + } + return last_cb + end + local unique_id = tostring({}) + objects.set_proc_call(cb) + local obj = objects.receive { + tag = unique_id, + type = "function" + } + assert(capi.rawtype(obj) == "userdata", "Not a proxy") + assert(capi.type(obj) == "function", "Not a proxied function") + assert(obj("fish") == last_cb, "Call method not propagated") + assert(type(last_cb) == "table", "Call didn't reach callback") + assert(last_cb.tag == unique_id, "Tag not propagated") + assert(last_cb.name == "__call", "__call not propagated") + assert(last_cb.args[1] == obj, "obj not propagated") + assert(last_cb.args[2] == "fish", "args not propagated") + local obj2 = objects.receive { + tag = unique_id + } + assert(obj == obj2, "Same object should be same object") +end + +function suite.receive_one_of_ours() + local tab = {} + local ours = objects.give(tab) + local back = objects.receive({tag=ours.tag}) + assert(tab == back, "Reception of one of ours didn't work") +end + +function suite.give_one_of_theirs() + local function cb(...) + end + objects.set_proc_call(cb) + local unique_id = "give_one_of_theirs:"..tostring({}) + local obj = objects.receive { + tag = unique_id, + type = "function" + } + local out = objects.give(obj) + assert(type(out) == "table", "giving a proxy didn't tabulate") + assert(out.tag == unique_id, "giving back one of theirs wasn't right") +end + +function suite.receive_table() + local last_cb = nil + local function cb(tag, name, ...) + last_cb = { + tag = tag, + name = name, + args = {...} + } + return last_cb + end + local unique_id = tostring({}) + objects.set_proc_call(cb) + local obj = objects.receive { + tag = unique_id, + type = "table" + } + assert(capi.rawtype(obj) == "userdata", "Not a proxy") + assert(capi.type(obj) == "table", "Not a proxied function") + assert(obj.thingy == last_cb, "Didn't proxy index") + assert(last_cb.name == "__index", "didn't proxy") + assert(last_cb.args[2] == "thingy", "proxy wasn't right") +end + +function suite.received_object_gc() + local last_cb = nil + local function cb(tag, name, ...) + last_cb = { + tag = tag, + name = name, + } + return last_cb + end + local unique_id = tostring({}) + objects.set_proc_call(cb) + local obj = objects.receive { + tag = unique_id, + type = "table" + } + assert(capi.rawtype(obj) == "userdata", "Not a proxy") + assert(capi.type(obj) == "table", "Not a proxied function") + -- deliberately lose the object + obj = nil + collectgarbage() + assert(last_cb.name == "__gc", "GC didn't go through") end local count_ok = 0 -- cgit v1.2.1