diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 38e3a66..4021419 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -166,7 +166,7 @@ Block = (function(_parent_0) end } _base_0.__index = _base_0 - return setmetatable({ __init = function(self, parent) + local _class_0 = setmetatable({ __init = function(self, parent) self.parent = parent self:set_indent(self.parent and self.parent.indent + 1 or 0) self._lines = { } @@ -182,6 +182,8 @@ Block = (function(_parent_0) mt.__init(self, ...) return self end }) + _base_0.__class = _class_0 + return _class_0 end)() tree = function(tree) local scope = Block() diff --git a/moonscript/compile/line.lua b/moonscript/compile/line.lua index 7ad6f1c..1d5fe0c 100644 --- a/moonscript/compile/line.lua +++ b/moonscript/compile/line.lua @@ -307,7 +307,10 @@ line_compile = { { "dot", "__index" } } } } } } }) end - def_scope:add_line(("return setmetatable(%s, %s)"):format(cls, cls_mt)) + local cls_name = def_scope:free_name("class") + def_scope:add_line(("local %s = setmetatable(%s, %s)"):format(cls_name, cls, cls_mt)) + def_scope:add_line(("%s.__class = %s"):format(base_name, cls_name)) + def_scope:add_line("return", cls_name) if parent_val ~= "" then parent_val = self:value(parent_val) end diff --git a/moonscript/compile/line.moon b/moonscript/compile/line.moon index 7832eb7..0cccd34 100644 --- a/moonscript/compile/line.moon +++ b/moonscript/compile/line.moon @@ -237,7 +237,10 @@ line_compile = {base_name, {"chain", "getmetatable", {"call", {parent_loc}}, {"dot", "__index"}}}}}}} - def_scope\add_line ("return setmetatable(%s, %s)")\format(cls, cls_mt) + cls_name = def_scope\free_name "class" + def_scope\add_line ("local %s = setmetatable(%s, %s)")\format(cls_name, cls, cls_mt) + def_scope\add_line ("%s.__class = %s")\format base_name, cls_name + def_scope\add_line "return", cls_name parent_val = @value parent_val if parent_val != "" diff --git a/tests/inputs/class.moon b/tests/inputs/class.moon index 8016bda..6cbf22f 100644 --- a/tests/inputs/class.moon +++ b/tests/inputs/class.moon @@ -36,4 +36,5 @@ class Simple extends Hi x = Simple() x\cool() +print x.__class == Simple diff --git a/tests/outputs/class.lua b/tests/outputs/class.lua index e488db9..c24b11b 100644 --- a/tests/outputs/class.lua +++ b/tests/outputs/class.lua @@ -2,7 +2,7 @@ local Hello Hello = (function(_parent_0) local _base_0 = { hello = function(self) return print(self.test, self.world) end, __tostring = function(self) return "hello world" end } _base_0.__index = _base_0 - return setmetatable({ __init = function(self, test, world) + local _class_0 = setmetatable({ __init = function(self, test, world) self.test, self.world = test, world return print("creating object..") end }, { __index = _base_0, __call = function(mt, ...) @@ -10,6 +10,8 @@ Hello = (function(_parent_0) mt.__init(self, ...) return self end }) + _base_0.__class = _class_0 + return _class_0 end)() local x = Hello(1, 2) x:hello() @@ -18,7 +20,7 @@ local Simple Simple = (function(_parent_0) local _base_0 = { cool = function(self) return print("cool") end } _base_0.__index = _base_0 - return setmetatable({ __init = function(self, ...) + local _class_0 = setmetatable({ __init = function(self, ...) if _parent_0 then return _parent_0.__init(self, ...) end @@ -27,6 +29,8 @@ Simple = (function(_parent_0) mt.__init(self, ...) return self end }) + _base_0.__class = _class_0 + return _class_0 end)() local Yikes Yikes = (function(_parent_0) @@ -35,11 +39,13 @@ Yikes = (function(_parent_0) if _parent_0 then setmetatable(_base_0, getmetatable(_parent_0).__index) end - return setmetatable({ __init = function(self) return print("created hello") end }, { __index = _base_0, __call = function(mt, ...) + local _class_0 = setmetatable({ __init = function(self) return print("created hello") end }, { __index = _base_0, __call = function(mt, ...) local self = setmetatable({}, _base_0) mt.__init(self, ...) return self end }) + _base_0.__class = _class_0 + return _class_0 end)(Simple) x = Yikes() x:cool() @@ -47,11 +53,13 @@ local Hi Hi = (function(_parent_0) local _base_0 = { cool = function(self, num) return print("num", num) end } _base_0.__index = _base_0 - return setmetatable({ __init = function(self, arg) return print("init arg", arg) end }, { __index = _base_0, __call = function(mt, ...) + local _class_0 = setmetatable({ __init = function(self, arg) return print("init arg", arg) end }, { __index = _base_0, __call = function(mt, ...) local self = setmetatable({}, _base_0) mt.__init(self, ...) return self end }) + _base_0.__class = _class_0 + return _class_0 end)() local Simple Simple = (function(_parent_0) @@ -60,11 +68,14 @@ Simple = (function(_parent_0) if _parent_0 then setmetatable(_base_0, getmetatable(_parent_0).__index) end - return setmetatable({ __init = function(self) return _parent_0.__init(self, "man") end }, { __index = _base_0, __call = function(mt, ...) + local _class_0 = setmetatable({ __init = function(self) return _parent_0.__init(self, "man") end }, { __index = _base_0, __call = function(mt, ...) local self = setmetatable({}, _base_0) mt.__init(self, ...) return self end }) + _base_0.__class = _class_0 + return _class_0 end)(Hi) x = Simple() -x:cool() \ No newline at end of file +x:cool() +print(x.__class == Simple) \ No newline at end of file