Skip to content

Commit e797114

Browse files
committed
is_object and type stdlib functions
1 parent 0ac471c commit e797114

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

moon/init.lua

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,27 @@ if not moon or not moon.inject then
33
end
44
local util = require("moonscript.util")
55
local lua = {
6-
debug = debug
6+
debug = debug,
7+
type = type
78
}
89
dump = util.dump
910
p = function(...)
1011
return print(dump(...))
1112
end
12-
debug = {
13+
is_object = function(value)
14+
return lua.type(value) == "table" and value.__class
15+
end
16+
type = function(value)
17+
local base_type = lua.type(value)
18+
if base_type == "table" then
19+
local cls = value.__class
20+
if cls then
21+
return cls
22+
end
23+
end
24+
return base_type
25+
end
26+
debug = setmetatable({
1327
upvalue = function(fn, k, v)
1428
local upvalues = { }
1529
local i = 1
@@ -31,7 +45,9 @@ debug = {
3145
return lua.debug.setupvalue(fn, upvalues[k], v)
3246
end
3347
end
34-
}
48+
}, {
49+
__index = lua.debug
50+
})
3551
run_with_scope = function(fn, scope, ...)
3652
local old_env = getfenv(fn)
3753
local env = setmetatable({ }, {
@@ -51,7 +67,7 @@ bind_methods = function(obj)
5167
return setmetatable({ }, {
5268
__index = function(self, name)
5369
local val = obj[name]
54-
if val and type(val) == "function" then
70+
if val and lua.type(val) == "function" then
5571
local bound
5672
bound = function(...)
5773
return val(obj, ...)
@@ -94,11 +110,13 @@ extend = function(...)
94110
return tbls[1]
95111
end
96112
copy = function(self)
97-
local t = { }
98-
for key, val in pairs(self) do
99-
t[key] = val
100-
end
101-
return t
113+
return (function()
114+
local _tbl_0 = { }
115+
for key, val in pairs(self) do
116+
_tbl_0[key] = val
117+
end
118+
return _tbl_0
119+
end)()
102120
end
103121
mixin = function(self, cls, ...)
104122
local meta = getmetatable(cls)
@@ -131,3 +149,15 @@ mixin_table = function(self, tbl, keys)
131149
end
132150
end
133151
end
152+
fold = function(items, fn)
153+
local len = #items
154+
if len > 1 then
155+
local accum = fn(items[1], items[2])
156+
for i = 3, len do
157+
accum = fn(acum, items[i])
158+
end
159+
return accum
160+
else
161+
return items[1]
162+
end
163+
end

moon/init.moon

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ if not moon or not moon.inject
44

55
util = require "moonscript.util"
66

7-
lua = { :debug }
7+
lua = { :debug, :type }
88

99
export *
1010

@@ -13,6 +13,16 @@ dump = util.dump
1313
p = (...) ->
1414
print dump ...
1515

16+
is_object = (value) -> -- is a moonscript object
17+
lua.type(value) == "table" and value.__class
18+
19+
type = (value) -> -- class aware type
20+
base_type = lua.type value
21+
if base_type == "table"
22+
cls = value.__class
23+
return cls if cls
24+
base_type
25+
1626
debug = setmetatable {
1727
upvalue: (fn, k, v) ->
1828
upvalues = {}
@@ -52,7 +62,7 @@ bind_methods = (obj) ->
5262
setmetatable {}, {
5363
__index: (name) =>
5464
val = obj[name]
55-
if val and type(val) == "function"
65+
if val and lua.type(val) == "function"
5666
bound = (...) -> val obj, ...
5767
self[name] = bound
5868
bound

0 commit comments

Comments
 (0)