-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathinit.lua
More file actions
174 lines (174 loc) · 3.5 KB
/
init.lua
File metadata and controls
174 lines (174 loc) · 3.5 KB
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
local lua = {
debug = debug,
type = type
}
local getfenv, setfenv, dump
do
local _obj_0 = require("moonscript.util")
getfenv, setfenv, dump = _obj_0.getfenv, _obj_0.setfenv, _obj_0.dump
end
local p, is_object, type, debug, run_with_scope, bind_methods, defaultbl, extend, copy, mixin, mixin_object, mixin_table, fold
p = function(...)
return print(dump(...))
end
is_object = function(value)
return lua.type(value) == "table" and value.__class
end
type = function(value)
local base_type = lua.type(value)
if base_type == "table" then
local cls = value.__class
if cls then
return cls
end
end
return base_type
end
debug = setmetatable({
upvalue = function(fn, k, ...)
local value = nil
local i = 1
while true do
local name
name, value = lua.debug.getupvalue(fn, i)
if name == nil then
error("Failed to find upvalue: " .. tostring(k))
end
if name == k then
break
end
i = i + 1
end
if select("#", ...) == 0 then
return value
else
return lua.debug.setupvalue(fn, i, ...)
end
end
}, {
__index = lua.debug
})
run_with_scope = function(fn, scope, ...)
local old_env = getfenv(fn)
local env = setmetatable({ }, {
__index = function(self, name)
local val = scope[name]
if val ~= nil then
return val
else
return old_env[name]
end
end
})
setfenv(fn, env)
return fn(...)
end
bind_methods = function(obj)
return setmetatable({ }, {
__index = function(self, name)
local val = obj[name]
if val and lua.type(val) == "function" then
local bound
bound = function(...)
return val(obj, ...)
end
self[name] = bound
return bound
else
return val
end
end
})
end
defaultbl = function(t, fn)
if not fn then
fn = t
t = { }
end
return setmetatable(t, {
__index = function(self, name)
local val = fn(self, name)
rawset(self, name, val)
return val
end
})
end
extend = function(...)
local tbls = {
...
}
if #tbls < 2 then
return
end
for i = 1, #tbls - 1 do
local a = tbls[i]
local b = tbls[i + 1]
setmetatable(a, {
__index = b
})
end
return tbls[1]
end
copy = function(self)
local _tbl_0 = { }
for key, val in pairs(self) do
_tbl_0[key] = val
end
return _tbl_0
end
mixin = function(self, cls, ...)
for key, val in pairs(cls.__base) do
if not key:match("^__") then
self[key] = val
end
end
return cls.__init(self, ...)
end
mixin_object = function(self, object, methods)
for _index_0 = 1, #methods do
local name = methods[_index_0]
self[name] = function(parent, ...)
return object[name](object, ...)
end
end
end
mixin_table = function(self, tbl, keys)
if keys then
for _index_0 = 1, #keys do
local key = keys[_index_0]
self[key] = tbl[key]
end
else
for key, val in pairs(tbl) do
self[key] = val
end
end
end
fold = function(items, fn)
local len = #items
if len > 1 then
local accum = fn(items[1], items[2])
for i = 3, len do
accum = fn(accum, items[i])
end
return accum
else
return items[1]
end
end
return {
dump = dump,
p = p,
is_object = is_object,
type = type,
debug = debug,
run_with_scope = run_with_scope,
bind_methods = bind_methods,
defaultbl = defaultbl,
extend = extend,
copy = copy,
mixin = mixin,
mixin_object = mixin_object,
mixin_table = mixin_table,
fold = fold
}