-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocap_examples.lua
135 lines (124 loc) · 3.19 KB
/
ocap_examples.lua
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
-- This file is put here for now.
local packages = {}
packages["ocap/weakmap"] = function (require)
local iface = {}
local mt = {}
mt["__mode"] = "k"
iface["make"] = function ()
local ins = {}
setmetatable(ins, mt)
return ins
end
return iface
end
packages["ocap/shallow-read-only"] = function (require)
local proxy2tab = require("ocap/weakmap").make()
local mt = {}
mt["__newindex"] = function (proxy, idx, val)
-- simply ignored
end
mt["__index"] = function (proxy, idx)
return proxy2tab[proxy][idx]
end
local iface = {}
iface["make"] = function (tab)
local proxy = {}
setmetatable(proxy, mt)
proxy2tab[proxy] = tab
return proxy
end
return iface
end
packages["ocap/throwingTable"] = function (require)
local throwingTable = {}
local mt = {}
mt["__newindex"] = function (self, idx, val)
error("tried to assign to the throwingTable")
end
mt["__index"] = function (self, idx)
error("tried to index into the throwingTable")
end
setmetatable(throwingTable, mt)
local iface = {}
iface["make"] = function ()
return throwingTable
end
return iface
end
packages["ocap/care-taker"] = function (require)
local throwingTable = require("ocap/throwingTable").make()
local proxy2tab = require("ocap/weakmap").make()
local mt = {}
mt["__newindex"] = function (proxy, idx, val)
proxy2tab[proxy][idx] = val
end
mt["__index"] = function (proxy, idx)
return proxy2tab[proxy][idx]
end
local iface = {}
iface["make"] = function (tab)
local proxy = {}
setmetatable(proxy, mt)
proxy2tab[proxy] = tab
local revoker = function ()
proxy2tab[proxy] = throwingTable
end
return proxy, revoker
end
return iface
end
packages["ocap/revokable-membrane"] = function (require)
local throwingTable = require("ocap/throwingTable").make()
local iface = {}
iface["make"] = function (tabl)
local proxy2tab = require("ocap/weakmap").make()
local tab2proxy = require("ocap/weakmap").makeWeakKV()
local mt = {}
local makeProxy = function (tab)
if tab2proxy[tab] ~= nil then
return tab2proxy[tab]
end
if type(tab) == "Number" then
return tab
elseif type(tab) == "String" then
return tab
end
local proxy = {}
setmetatable(proxy, mt)
tab2proxy[tab] = proxy
proxy2tab[proxy] = tab
return proxy
end
local unwrap = function (proxy)
if proxy2tab[proxy] ~= nil then
return proxy2tab[proxy]
else
return proxy
end
end
local revoked = false
local revoker = function ()
revoked = true
end
mt["__newindex"] = function (proxy, idx, val)
if revoked == true then
return nil
end
local real_idx = unwrap(idx)
local real_val = unwrap(val)
local tab = unwrap(proxy)
tab[real_idx] = real_val
return nil
end
mt["__index"] = function (proxy, idx)
if revoked == true then
return throwingTable
end
local real_idx = unwrap(idx)
local tab = unwrap(proxy)
return makeProxy(tab[real_idx])
end
return makeProxy(tabl), revoker
end
return iface
end