From 49f44f799b39dcc4ffe69502638108398732f8df Mon Sep 17 00:00:00 2001 From: Li Jin Date: Wed, 6 Apr 2022 17:21:36 +0800 Subject: adding support for fixing issue #85. --- spec/outputs/class.lua | 134 ++++++++++++++++++++++++++++++----------- spec/outputs/export.lua | 16 +++-- spec/outputs/global.lua | 4 +- spec/outputs/local.lua | 12 +++- spec/outputs/vararg.lua | 8 ++- src/yuescript/yue_compiler.cpp | 16 ++--- 6 files changed, 138 insertions(+), 52 deletions(-) diff --git a/spec/outputs/class.lua b/spec/outputs/class.lua index 929c243..8db5a8a 100644 --- a/spec/outputs/class.lua +++ b/spec/outputs/class.lua @@ -9,7 +9,9 @@ do return "hello world" end } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function(self, test, world) self.test = test @@ -40,7 +42,9 @@ do return print("cool") end } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -61,7 +65,9 @@ do local _class_0 local _parent_0 = Simple local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end setmetatable(_base_0, _parent_0.__base) _class_0 = setmetatable({ __init = function(self) @@ -104,7 +110,9 @@ do return print("num", num) end } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function(self, arg) return print("init arg", arg) @@ -130,7 +138,9 @@ do return _class_0.__parent.__base.cool(self, 120302) end } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end setmetatable(_base_0, _parent_0.__base) _class_0 = setmetatable({ __init = function(self) @@ -172,7 +182,9 @@ do local _base_0 = { something = 20323 } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -199,7 +211,9 @@ do return assert(_class_0.__parent == Okay) end } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end setmetatable(_base_0, _parent_0.__base) _class_0 = setmetatable({ __init = function(self, ...) @@ -240,7 +254,9 @@ do return _class_0.__parent.something(self, 1, 2, 3, 4) end } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -264,7 +280,9 @@ do return print("val:", self.val) end } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -293,7 +311,9 @@ do end end } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end setmetatable(_base_0, _parent_0.__base) _class_0 = setmetatable({ __init = function(self, ...) @@ -353,7 +373,9 @@ do return nil end } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -386,7 +408,9 @@ do blue = function(self) end, green = function(self) end } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -420,7 +444,9 @@ do local _class_0 local hello local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -453,7 +479,9 @@ yyy = function() do local _class_0 local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -476,7 +504,9 @@ end do local _class_0 local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -497,7 +527,9 @@ end do local _class_0 local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -519,7 +551,9 @@ do local _class_0 local _parent_0 = Hello.World local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end setmetatable(_base_0, _parent_0.__base) _class_0 = setmetatable({ __init = function(self, ...) @@ -560,7 +594,9 @@ local a do local _class_0 local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -581,7 +617,9 @@ local Something do local _class_0 local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -603,7 +641,9 @@ do local _class_0 local _parent_0 = Hello local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end setmetatable(_base_0, _parent_0.__base) _class_0 = setmetatable({ __init = function(self, ...) @@ -642,7 +682,9 @@ do local _class_0 local _parent_0 = World local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end setmetatable(_base_0, _parent_0.__base) _class_0 = setmetatable({ __init = function(self, ...) @@ -680,7 +722,9 @@ print(((function() do local _class_0 local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -701,7 +745,9 @@ end)()).__name) do local _class_0 local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -723,7 +769,9 @@ do local _class_0 local val, insert local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function(self) return print(insert, val) @@ -747,7 +795,9 @@ end do local _class_0 local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = hi, __base = _base_0, @@ -778,7 +828,9 @@ do } end } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end setmetatable(_base_0, _parent_0.__base) _class_0 = setmetatable({ __init = function(self, ...) @@ -819,7 +871,9 @@ do return _class_0.__parent.__base.dang(self) end) } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end setmetatable(_base_0, _parent_0.__base) _class_0 = setmetatable({ __init = function(self, ...) @@ -856,7 +910,9 @@ do local _class_0 local _parent_0 = Thing local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end setmetatable(_base_0, _parent_0.__base) _class_0 = setmetatable({ __init = function(self, ...) @@ -921,7 +977,9 @@ do return self.__class["if"] and self.__class["do"](self.__class) end } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function(self) self.__class["if"] = true @@ -958,7 +1016,9 @@ do return self["if"] and self["do"](self) end } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function(self) self["if"] = true @@ -991,7 +1051,9 @@ do } end } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end setmetatable(_base_0, _parent_0.__base) _class_0 = setmetatable({ __init = function(self, ...) @@ -1025,6 +1087,9 @@ end do local _class_0 local _base_0 = { } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end local _list_0 = { B, C, @@ -1033,12 +1098,11 @@ do for _index_0 = 1, #_list_0 do local _mixin_0 = _list_0[_index_0] for _key_0, _val_0 in pairs(_mixin_0.__base) do - if not _key_0:match("^__") and _base_0[_key_0] == nil then + if _key_0 ~= "__class" then _base_0[_key_0] = _val_0 end end end - _base_0.__index = _base_0 _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -1057,7 +1121,9 @@ end do local _class_0 local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, diff --git a/spec/outputs/export.lua b/spec/outputs/export.lua index 4d81cc0..84e5424 100644 --- a/spec/outputs/export.lua +++ b/spec/outputs/export.lua @@ -13,7 +13,9 @@ do local _base_0 = { umm = "cool" } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -168,7 +170,9 @@ f((function() do local _class_0 local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -264,7 +268,9 @@ _ = tostring((function() do local _class_0 local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -289,7 +295,9 @@ _module_0["v2"] = v2 do local _class_0 local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, diff --git a/spec/outputs/global.lua b/spec/outputs/global.lua index 1c9a3cf..ef73823 100644 --- a/spec/outputs/global.lua +++ b/spec/outputs/global.lua @@ -8,7 +8,9 @@ do local _base_0 = { umm = "cool" } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, diff --git a/spec/outputs/local.lua b/spec/outputs/local.lua index 288764a..524d9ef 100644 --- a/spec/outputs/local.lua +++ b/spec/outputs/local.lua @@ -78,7 +78,9 @@ do local _class_0 local Five local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -100,7 +102,9 @@ do local _class_0 local No local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -118,7 +122,9 @@ do do local _class_1 local _base_1 = { } - _base_1.__index = _base_1 + if _base_1.__index == nil then + _base_1.__index = _base_1 + end _class_1 = setmetatable({ __init = function() end, __base = _base_1, diff --git a/spec/outputs/vararg.lua b/spec/outputs/vararg.lua index 052fce3..56e2011 100644 --- a/spec/outputs/vararg.lua +++ b/spec/outputs/vararg.lua @@ -55,7 +55,9 @@ join = function(...) do local _class_0 local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, @@ -80,7 +82,9 @@ join = function(...) do local _class_0 local _base_0 = { } - _base_0.__index = _base_0 + if _base_0.__index == nil then + _base_0.__index = _base_0 + end _class_0 = setmetatable({ __init = function() end, __base = _base_0, diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 3720915..3394f1f 100755 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -60,7 +60,7 @@ using namespace parserlib; typedef std::list str_list; -const std::string_view version = "0.10.9"sv; +const std::string_view version = "0.10.10"sv; const std::string_view extension = "yue"sv; class YueCompilerImpl { @@ -5268,7 +5268,7 @@ private: if (clsDecl) { std::string clsName; bool newDefined = false; - std::tie(clsName,newDefined) = defineClassVariable(clsDecl->name); + std::tie(clsName, newDefined) = defineClassVariable(clsDecl->name); if (newDefined) varDefs.push_back(clsName); } } @@ -5337,17 +5337,17 @@ private: } else { temp.back() += "{ }"s + nll(classDecl); } + transformAssignment(toAst(baseVar + ".__index ?\?= "s + baseVar, classDecl), temp); if (classDecl->mixes) { - auto mixin = getUnusedName("_mixin_"); - auto key = getUnusedName("_key_"); - auto val = getUnusedName("_val_"); + auto mixin = getUnusedName("_mixin_"sv); + auto key = getUnusedName("_key_"sv); + auto val = getUnusedName("_val_"sv); auto mixins = _parser.toString(classDecl->mixes); _buf << "for "sv << mixin << " in *{"sv << mixins << "}\n"sv; _buf << "\tfor "sv << key << ',' << val << " in pairs "sv << mixin << ".__base\n"sv; - _buf << "\t\t"sv << baseVar << '[' << key << "]="sv << val << " if not "sv << key << "\\match\"^__\" and "sv << baseVar << '[' << key << "] == nil"sv; + _buf << "\t\t"sv << baseVar << '[' << key << "]="sv << val << " if "sv << key << "~=\"__class\""sv; transformBlock(toAst(clearBuf(), x), temp, ExpUsage::Common); } - temp.push_back(indent() + baseVar + ".__index = "s + baseVar + nll(classDecl)); str_list tmp; if (usage == ExpUsage::Assignment) { auto assign = x->new_ptr(); @@ -5366,7 +5366,7 @@ private: } else { if (extend) { _buf << indent(1) << "__init = function(self, ...)"sv << nll(classDecl); - _buf << indent(2) << "return _class_0.__parent.__init(self, ...)"sv << nll(classDecl); + _buf << indent(2) << "return "sv << classVar << ".__parent.__init(self, ...)"sv << nll(classDecl); _buf << indent(1) << "end,"sv << nll(classDecl); } else { _buf << indent(1) << "__init = function() end,"sv << nll(classDecl); -- cgit v1.2.3-55-g6feb