summaryrefslogtreecommitdiff
path: root/lib/supple/objects.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lib/supple/objects.lua')
-rw-r--r--lib/supple/objects.lua35
1 files changed, 32 insertions, 3 deletions
diff --git a/lib/supple/objects.lua b/lib/supple/objects.lua
index 6c28cae..78f5788 100644
--- a/lib/supple/objects.lua
+++ b/lib/supple/objects.lua
@@ -9,6 +9,8 @@
-- For licence terms, see COPYING
--
+local gc = collectgarbage
+
local capi = require 'supple.capi'
local my_objects_by_obj = {}
@@ -21,10 +23,24 @@ local proc_call = nil
local type = capi.rawtype
+local function clean_down()
+ -- And force a full GC
+ gc "collect"
+ gc "collect"
+ gc "collect"
+ -- And forget all our local objects
+ my_objects_by_obj = {}
+ my_objects_by_tag = {}
+end
+
local function set_name(newname)
my_name = newname
end
+local function get_name()
+ return my_name
+end
+
local function set_proc_call(pc)
proc_call = pc
end
@@ -36,7 +52,7 @@ local integral = {
number = true,
}
-local function give(obj)
+local function give(obj, special_tag)
-- If the object is integral, return it directly
if integral[type(obj)] then
return obj
@@ -54,7 +70,9 @@ local function give(obj)
return { tag = tag }
end
-- otherwise wrap it freshly for us and return that.
- local tag = ("%s:%d"):format(my_name, my_counter)
+ local tag = (special_tag and special_tag or
+ ("%s:%d"):format(my_name, my_counter))
+ my_counter = my_counter + 1
local expn = capi.explain(obj, tag)
my_objects_by_obj[obj] = tag
my_objects_by_tag[tag] = obj
@@ -83,6 +101,7 @@ local function receive(obj)
-- Okay, prepare a proxy?
assert(type(obj.type) == "string")
local proxy, mt = capi.new_proxy(obj.type)
+ assert(capi.type(proxy) == obj.type)
their_objects_by_tag[tag] = proxy
their_objects_by_obj[proxy] = tag
-- Fill out the metatable
@@ -92,11 +111,12 @@ local function receive(obj)
obj.methods[#obj.methods+1] = "__call"
end
if obj.type == "table" then
+ obj.methods[#obj.methods+1] = "__len"
obj.methods[#obj.methods+1] = "__index"
obj.methods[#obj.methods+1] = "__newindex"
end
for _, name in ipairs(obj.methods or {}) do
- local function meta_func(...)
+ local function meta_func(mobj, ...)
return proc_call(tag, name, ...)
end
mt[name] = meta_func
@@ -105,9 +125,18 @@ local function receive(obj)
return proxy
end
+local function forget_mine(tag)
+ local obj = my_objects_by_tag[tag]
+ my_objects_by_tag[tag] = nil
+ my_objects_by_obj[obj] = nil
+end
+
return {
set_name = set_name,
+ get_name = get_name,
set_proc_call = set_proc_call,
give = give,
receive = receive,
+ clean_down = clean_down,
+ forget_mine = forget_mine,
}