blob: ed460af30a6c4938dfab9190b3f21c2d1a96d891 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
--
-- OBJECTS.LUA
--
-- Tests that objects (metatables) can be passed between lanes.
--
local lanes = require "lanes"
local linda= lanes.linda()
local start_lane= lanes.gen("io",
function(obj1 )
assert(obj1.v )
assert(obj1.print )
assert(obj1 )
local mt1= getmetatable(obj1)
assert(mt1)
local k, obj2= linda:receive("")
assert(k == "" and obj2 )
local mt2= getmetatable(obj2)
assert(mt2)
assert(mt1==mt2 )
local v= obj1:print()
assert(v=="aaa" )
v = obj2:print()
assert(v=="bbb" )
return true
end
)
local WR= function(str)
io.stderr:write(tostring(str).."\n")
end
-- Lanes identifies metatables and copies them only once per each lane.
--
-- Having methods in the metatable will make passing objects lighter than
-- having the methods 'fixed' in the object tables themselves.
--
local o_mt= {
__index= function(me, key )
if key=="print" then
return function() WR(me.v); return me.v end
end
end
}
local function obj_gen(v)
local o= { v=v }
setmetatable(o, o_mt )
return o
end
local a= obj_gen("aaa")
local b= obj_gen("bbb")
assert(a and b )
local mt_a= getmetatable(a)
local mt_b= getmetatable(b)
assert(mt_a and mt_a==mt_b )
local h= start_lane(a) -- 1st object as argument
linda:send("", b ) -- 2nd object via Linda
assert(h[1]==true ) -- wait for return
|