-
Notifications
You must be signed in to change notification settings - Fork 18
/
object.lua
182 lines (156 loc) · 4.59 KB
/
object.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
local env = require("internal.env")
local data = require("internal.data")
local binser = require("thirdparty.binser")
local object = {}
-- Modification of penlight's algorithm to ignore class fields
local function cycle_aware_copy(t, cache)
if type(t) ~= 'table' then return t end
if cache[t] then return cache[t] end
local res = {}
cache[t] = res
local mt = getmetatable(t)
for k,v in pairs(t) do
-- TODO: standardize no-save fields
-- NOTE: preserves the UID for now.
if k ~= "location" then
k = cycle_aware_copy(k, cache)
v = cycle_aware_copy(v, cache)
res[k] = v
end
end
setmetatable(res,mt)
return res
end
-- ignores location field only, unlike Object.make_prototype
function object.make_prototype(obj)
local _type = obj.proto._type
local _id = obj.proto._id
local copy = cycle_aware_copy(obj, {})
setmetatable(copy, nil)
-- for deserialization, removed afterward
copy._type = _type
copy._id = _id
return copy
end
function object.__index(t, k)
if k == "__iface" then
local mt = getmetatable(t)
return mt.__iface
end
if k == "__mt" then
local mt = getmetatable(t)
return env.get_require_path(mt.__iface)
end
if k == "proto" then
local mt = getmetatable(t)
return mt.__proto
end
local v = rawget(t, k)
if v ~= nil then
return v
end
local mt = getmetatable(t)
-- v = mt.__proto[k]
-- if v ~= nil then
-- return v
-- end
return mt.__iface[k]
end
local function extract_functions(instance, serial, proto, cache)
if type(proto) ~= "table" then
return
end
-- protect against recursive tables
if cache[instance] or cache[proto] then
return
end
cache[instance] = true
cache[proto] = true
for k, v in pairs(instance) do
if type(k) == "string" then
-- Skip non-copiable fields (prefixed with '_')
if string.sub(k, 1, 1) ~= "_" then
if type(v) == "function" and proto[k] == v then
serial[k] = "copy_from_proto"
elseif type(v) == "table" and type(instance) == "table" then
serial[k] = {}
extract_functions(serial[k], v, proto[k], cache)
end
end
end
end
end
local function copy_functions(instance, serial, proto, cache)
if type(proto) ~= "table" then
return
end
-- protect against recursive tables
if cache[instance] or cache[proto] then
return
end
cache[instance] = true
cache[proto] = true
for k, v in pairs(serial) do
if v == "copy_from_proto" then
instance[k] = proto[k]
elseif type(v) == "table" and type(instance[k]) == "table" then
copy_functions(instance[k], v, proto[k], cache)
end
end
end
function object.serialize(self)
local proto = self.proto
local ret = object.make_prototype(self)
assert(ret._id, "serialization currently assumes there is a prototype in data[] to load")
local serial = {}
extract_functions(self, serial, proto, {})
ret.__serial = serial
return ret
end
function object.deserialize(self, _type, _id)
if self._type and self._id then
_type = self._type
_id = self._id
end
assert(type(_type) == "string")
assert(type(_id) == "string")
local proto = data[_type][_id]
if not proto then
error("no proto " .. tostring(_type) .. " " .. tostring(_id))
end
local iface = data[_type]:interface()
assert(iface)
assert(self.location == nil, ("%s %s"):format(_type, _id))
self._type = _type
self._id = _id
-- functions on the prototype table are not serialized, so they
-- must be copied from the prototype to the instance on
-- deserialization if they were unchanged from the prototype's
-- version on save.
local serial = self.__serial
if serial then
self.__serial = nil
copy_functions(self, serial, proto, {})
end
setmetatable(self,
{
__id = "object",
__index = object.__index,
__proto = proto,
__iface = iface,
__inspect = function(t)
local n = {}
for k, v in pairs(t) do
if not (type(k) == "string" and string.match(k, "^__")) then
n[k] = v
end
end
return inspect(n)
end
})
return self
end
if not binser.hasRegistry("object") then
binser.register("object", "object", object.serialize, object.deserialize)
end
return object