This repository has been archived by the owner on Jan 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathluna.lua
270 lines (256 loc) · 7.47 KB
/
luna.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
--[[
luna class library https://github.com/BradSharp/luna
MIT License Copyright (c) 2016 Brad Sharp
]]
local createWrapper, getWrapper do
local indexObj = function (object, index)
return object[index]
end
local newIndexObj = function (object, index, value)
object[index] = value
end
local metatable = {
__call = function (this) return rawget(this, 1) end,
__index = function (this, index)
local value = rawget(this, 2)[index]
if value then
return value
else
local success, result = pcall(indexObj,
rawget(this, 1), index)
if success then
if type(result) == "function" then
-- Probably a better way to do this?
return function (...)
return result(this, ...)
end
else
return result
end
else
return nil
end
end
end,
__newindex = function (this, index, value)
local private = rawget(this, 2)
local currentValue = private[index]
if currentValue then -- Change an existing value
private[index] = value
else -- Attempt to set an existing value from definition
local success, result = pcall(
newIndexObj, rawget(this, 1), index, value)
if not success then
if result == "Incompatible type" then
error(result)
else -- Create a new value
private[index] = value
end
end
end
end,
__add = function (this, other) return rawget(this, 1) + other end,
__sub = function (this, other) return rawget(this, 1) - other end,
__mul = function (this, other) return rawget(this, 1) * other end,
__div = function (this, other) return rawget(this, 1) / other end,
__mod = function (this, other) return rawget(this, 1) % other end,
__pow = function (this, other) return rawget(this, 1) ^ other end,
__unm = function (this) return -rawget(this, 1) end,
__eq = function (this, other) return rawget(this, 1) == other end,
__lt = function (this, other) return rawget(this, 1) < other end,
__le = function (this, other) return rawget(this, 1) <= other end,
__concat = function (this, ...) return rawget(this, 1) .. ... end,
__tostring = function (this) return tostring(rawget(this, 1)) end,
__len = function (this) return #rawget(this, 1) end,
}
local wrappers = setmetatable({}, {__mode="k"})
function createWrapper(object)
local wrapper = {object, {}}
wrappers[object] = wrapper
return setmetatable(wrapper, metatable)
end
function getWrapper(object)
return wrappers[object]
end
end
function createObject(definition, ...)
local object = {__properties = {}}
local wrapper = createWrapper(object)
setmetatable(object, definition.__metatable)
local constructor = definition.__construct
if constructor then
constructor(wrapper, ...)
end
return object
end
function getIndex(definition, index)
local value = definition[index]
if value then
return value
else
for _, v in ipairs(definition.__inherits) do
value = getIndex(v, index)
if value then
return value
end
end
end
end
function getMetamethods(definition)
local methods = {}
local function recurse(def)
for i, v in pairs(def) do
if string.sub(i, 1, 2) == "__" then
if not methods[i] then
methods[i] = v
end
end
end
for _, v in ipairs(def.__inherits) do
recurse(v)
end
end
recurse(definition)
return methods
end
local constructClass do
local definitionMetatable = {
__call = createObject,
}
local function duplicateTable(t)
local t2 = {}
for i, v in pairs(t) do
t2[i] = v
end
return t2
end
local function convertType(value, _type)
local valueType = type(value)
if _type == valueType then
return value
elseif _type == "string" then
if valueType == "number" then
return tostring(value)
end
elseif _type == "number" then
if valueType == "string" then
return tonumber(value)
end
end
end
function constructClass(definition, inherits, base)
for i, v in pairs(definition) do
assert(type(i) == "string",
"Invalid index " .. tostring(i) .. " for " .. tostring(v))
assert(type(v) ~= "userdata", "Attempt to create property "
.. i .. " of type userdata use an accessor instead")
end
definition.__inherits = inherits or {}
definition.__sharp = true
local metamethods = getMetamethods(definition)
local metatable = {
__index = function (this, index)
assert(string.sub(index, 1, 2) ~= "__",
"Metaindexing is forbidden")
local public = rawget(this, "__properties")
local value = public[index]
if value then
return value
else -- It doesn't exist, it's a function or, an accessor
value = getIndex(definition, index)
if value then
local valueType = type(value)
if valueType == "function" then
local wrapper = getWrapper(this)
return function (this, ...)
return value(wrapper, ...)
end
elseif valueType == "table" then
if value.get then
return value.get(getWrapper(this))
else
local value = duplicateTable(value)
public[index] = value
return value
end
else
-- Assign it to the object
public[index] = value
return value
end
elseif metamethods.__index then
return metamethods.__index(getWrapper(this), index)
else
error("'" .. index .. "' is not a valid member of "
.. tostring(this))
end
end
end,
__newindex = function (this, index, value)
assert(string.sub(index, 1, 2) ~= "__",
"Metaindexing is forbidden")
if metamethods.__newindex then
metamethods.__newindex(getWrapper(this), index, value)
end
local public = rawget(this, "__properties")
local currentValue = public[index]
if currentValue then
local newValue = convertType(value, type(currentValue))
if newValue then
public[index] = newValue
else
error("Incompatible type")
end
else
currentValue = getIndex(definition, index)
if currentValue then
local currentValueType = type(currentValue)
local newValue = convertType(value, currentValueType)
local valueType = type(value)
if currentValueType == "table" and currentValue.set then
currentValue.set(getWrapper(this), value)
elseif valueType == "function" then
error(index .. " is not a valid member of "
.. tostring(this))
elseif newValue then
public[index] = newValue
else
error("Incompatible type")
end
else
error("'" .. index .. "' is not a valid member of "
.. tostring(this))
end
end
end,
__call = function ()
return definition
end
}
-- Wrap the metamethods
for i, v in pairs(metamethods) do
local method = string.sub(i, 3)
if not (method == "index" or method == "newindex"
or method == "call") then
metatable[i] = function (this, ...)
return v(getWrapper(this), ...)
end
end
end
definition.__construct = metamethods.__construct
definition.__metatable = metatable
return setmetatable(definition, definitionMetatable)
end
end
function class(...)
local parameters = {...}
if type(parameters[1]) == "table" and
rawget(parameters[1], "__sharp") then -- Inheritance
return function (definition)
return constructClass(definition, parameters)
end
else -- Definition
return constructClass(parameters[1], {})
end
end
return class