instances store class in __class

This commit is contained in:
leaf corcoran 2011-07-04 11:36:26 -07:00
parent 883ae0b027
commit a80f8f875e
5 changed files with 29 additions and 9 deletions

View File

@ -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()

View File

@ -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

View File

@ -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 != ""

View File

@ -36,4 +36,5 @@ class Simple extends Hi
x = Simple()
x\cool()
print x.__class == Simple

View File

@ -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()
x:cool()
print(x.__class == Simple)