From f4e2286da2b0212b0a7ac76821203bd398cfac5b Mon Sep 17 00:00:00 2001 From: Li Jin Date: Tue, 15 Feb 2022 09:55:18 +0800 Subject: fix a global variable check case. --- spec/outputs/backcall.lua | 19 +++-- spec/outputs/destructure.lua | 7 +- spec/outputs/existential.lua | 161 +++++++++++++++++++++++++-------------- spec/outputs/multiline_chain.lua | 7 +- spec/outputs/pipe.lua | 50 +++++++----- spec/outputs/vararg.lua | 10 ++- src/yuescript/yue_compiler.cpp | 22 +++++- 7 files changed, 183 insertions(+), 93 deletions(-) diff --git a/spec/outputs/backcall.lua b/spec/outputs/backcall.lua index 9f7ec29..3a4c5e9 100644 --- a/spec/outputs/backcall.lua +++ b/spec/outputs/backcall.lua @@ -31,16 +31,19 @@ do end)()) end do - if http ~= nil then - http.get("ajaxtest", function(data) - body[".result"]:html(data) - return http.post("ajaxprocess", data, function(processed) - body[".result"]:append(processed) - return setTimeout(1000, function() - return print("done") + do + local _obj_0 = http + if _obj_0 ~= nil then + _obj_0.get("ajaxtest", function(data) + body[".result"]:html(data) + return http.post("ajaxprocess", data, function(processed) + body[".result"]:append(processed) + return setTimeout(1000, function() + return print("done") + end) end) end) - end) + end end end do diff --git a/spec/outputs/destructure.lua b/spec/outputs/destructure.lua index 9ca13c8..1862b85 100644 --- a/spec/outputs/destructure.lua +++ b/spec/outputs/destructure.lua @@ -298,8 +298,11 @@ do end local _obj_1 = getmetatable(tb).func if _obj_1 == nil then - if item ~= nil then - _obj_1 = item.defVal + do + local _obj_2 = item + if _obj_2 ~= nil then + _obj_1 = _obj_2.defVal + end end end a.b(function() diff --git a/spec/outputs/existential.lua b/spec/outputs/existential.lua index b594218..b241b3b 100644 --- a/spec/outputs/existential.lua +++ b/spec/outputs/existential.lua @@ -1,18 +1,28 @@ -if f1 ~= nil then - f1() +do + local _obj_0 = f1 + if _obj_0 ~= nil then + _obj_0() + end end -if f2 ~= nil then - f2("arg0", 123) +do + local _obj_0 = f2 + if _obj_0 ~= nil then + _obj_0("arg0", 123) + end end local x -if tab ~= nil then - x = tab.value +do + local _obj_0 = tab + if _obj_0 ~= nil then + x = _obj_0.value + end end print((function() - if abc ~= nil then - local _obj_0 = abc["hello world"] - if _obj_0 ~= nil then - return _obj_0.xyz + local _obj_0 = abc + if _obj_0 ~= nil then + local _obj_1 = _obj_0["hello world"] + if _obj_1 ~= nil then + return _obj_1.xyz end return nil end @@ -21,16 +31,24 @@ end)()) if print and (x ~= nil) then print(x) end -if self ~= nil then - self:func(998) +do + local _obj_0 = self + if _obj_0 ~= nil then + _obj_0:func(998) + end end do local _with_0 - if abc ~= nil then - local _obj_0 = abc() - local _obj_1 = _obj_0.func - if _obj_1 ~= nil then - _with_0 = _obj_1(_obj_0) + do + local _obj_0 = abc + if _obj_0 ~= nil then + do + local _obj_1 = _obj_0() + local _obj_2 = _obj_1.func + if _obj_2 ~= nil then + _with_0 = _obj_2(_obj_1) + end + end end end if (function() @@ -45,19 +63,29 @@ do end do local _des_0 - if a ~= nil then - local _obj_0 = a["if"] + do + local _obj_0 = a if _obj_0 ~= nil then - local _obj_1 = _obj_0["then"] - if _obj_1 ~= nil then - local _obj_2 = _obj_1(_obj_0, 123) - if _obj_2 ~= nil then - _des_0 = _obj_2((function() - if self ~= nil then - return self["function"](self, 998) + do + local _obj_1 = _obj_0["if"] + if _obj_1 ~= nil then + do + local _obj_2 = _obj_1["then"] + if _obj_2 ~= nil then + do + local _obj_3 = _obj_2(_obj_1, 123) + if _obj_3 ~= nil then + _des_0 = _obj_3((function() + local _obj_4 = self + if _obj_4 ~= nil then + return _obj_4["function"](_obj_4, 998) + end + return nil + end)()) + end + end end - return nil - end)()) + end end end end @@ -97,41 +125,54 @@ if (window ~= nil) then local environment = 'browser (probably)' end local zip -local _obj_0 = lottery.drawWinner -if _obj_0 ~= nil then - local _obj_1 = _obj_0().address - if _obj_1 ~= nil then - zip = _obj_1.zipcode +do + local _obj_0 = lottery.drawWinner + if _obj_0 ~= nil then + do + local _obj_1 = _obj_0().address + if _obj_1 ~= nil then + zip = _obj_1.zipcode + end + end end end local len = (function() - if utf8 ~= nil then - return utf8.len + local _obj_0 = utf8 + if _obj_0 ~= nil then + return _obj_0.len end return nil end)() or (function() - if string ~= nil then - return string.len + local _obj_0 = string + if _obj_0 ~= nil then + return _obj_0.len end return nil end)() or function(o) return #o end local a -if tb1 ~= nil then - local _obj_1 = tb1["end"] - if _obj_1 ~= nil then - a = _obj_1(tb1, 123 + (function() - if tb2 ~= nil then - return tb2["then"](tb2, 456) +do + local _obj_0 = tb1 + if _obj_0 ~= nil then + do + local _obj_1 = _obj_0["end"] + if _obj_1 ~= nil then + a = _obj_1(_obj_0, 123 + (function() + local _obj_2 = tb2 + if _obj_2 ~= nil then + return _obj_2["then"](_obj_2, 456) + end + return nil + end)()) end - return nil - end)()) + end end end local b = ((function() - if tb1 ~= nil then - local _base_0 = tb1 + local _obj_0 = tb1 + if _obj_0 ~= nil then + local _base_0 = _obj_0 local _fn_0 = _base_0["end"] return _fn_0 and function(...) return _fn_0(_base_0, ...) @@ -139,8 +180,9 @@ local b = ((function() end return nil end)() ~= nil) or (function() - if tb2 ~= nil then - local _base_0 = tb2 + local _obj_0 = tb2 + if _obj_0 ~= nil then + local _base_0 = _obj_0 local _fn_0 = _base_0["then"] return _fn_0 and function(...) return _fn_0(_base_0, ...) @@ -155,17 +197,24 @@ do _with_0:close() end end -if tb ~= nil then - local _obj_1 = getmetatable(tb).__a - if _obj_1 ~= nil then - _obj_1(123) +do + local _obj_0 = tb + if _obj_0 ~= nil then + do + local _obj_1 = getmetatable(_obj_0).__a + if _obj_1 ~= nil then + _obj_1(123) + end + end end end do local _with_0 - local _obj_1 = getmetatable(tb) - if _obj_1 ~= nil then - _with_0 = getmetatable(_obj_1).__index + do + local _obj_0 = getmetatable(tb) + if _obj_0 ~= nil then + _with_0 = getmetatable(_obj_0).__index + end end if _with_0 ~= nil then _with_0.a = 1 diff --git a/spec/outputs/multiline_chain.lua b/spec/outputs/multiline_chain.lua index ecd2ca8..5edad88 100644 --- a/spec/outputs/multiline_chain.lua +++ b/spec/outputs/multiline_chain.lua @@ -21,8 +21,11 @@ local tbb = { } } local tb -if f2 ~= nil then - tb = f2("abc", f1({ }).a) +do + local _obj_0 = f2 + if _obj_0 ~= nil then + tb = _obj_0("abc", f1({ }).a) + end end local f f = function() diff --git a/spec/outputs/pipe.lua b/spec/outputs/pipe.lua index bf517ae..345f8b0 100644 --- a/spec/outputs/pipe.lua +++ b/spec/outputs/pipe.lua @@ -32,31 +32,45 @@ do end do local _with_0 - if create ~= nil then - _with_0 = create(b, "new") + do + local _obj_0 = create + if _obj_0 ~= nil then + _with_0 = _obj_0(b, "new") + end end _with_0.value = 123 print(_with_0:work()) end -if f ~= nil then - f(123) +do + local _obj_0 = f + if _obj_0 ~= nil then + _obj_0(123) + end end -if f2 ~= nil then - f2((function() - if f1 ~= nil then - return f1("abc") - end - return nil - end)()) +do + local _obj_0 = f2 + if _obj_0 ~= nil then + _obj_0((function() + local _obj_1 = f1 + if _obj_1 ~= nil then + return _obj_1("abc") + end + return nil + end)()) + end end local c -if f2 ~= nil then - c = f2((function() - if f1 ~= nil then - return f1("abc") - end - return nil - end)()) +do + local _obj_0 = f2 + if _obj_0 ~= nil then + c = _obj_0((function() + local _obj_1 = f1 + if _obj_1 ~= nil then + return _obj_1("abc") + end + return nil + end)()) + end end local f f = function() diff --git a/spec/outputs/vararg.lua b/spec/outputs/vararg.lua index e799a94..052fce3 100644 --- a/spec/outputs/vararg.lua +++ b/spec/outputs/vararg.lua @@ -189,14 +189,16 @@ join = function(...) end end)(...)) f_eop((function() - if func ~= nil then - return func() + local _obj_0 = func + if _obj_0 ~= nil then + return _obj_0() end return nil end)()) f_eop((function(...) - if func ~= nil then - return func(...) + local _obj_0 = func + if _obj_0 ~= nil then + return _obj_0(...) end return nil end)(...)) diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 6bc9fe8..6c8e167 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.9.10"sv; +const std::string_view version = "0.9.11"sv; const std::string_view extension = "yue"sv; class YueCompilerImpl { @@ -3539,7 +3539,19 @@ private: } BLOCK_END auto objVar = singleVariableFrom(partOne); - if (objVar.empty()) { + bool isScoped = false; + if (objVar.empty() || !isLocal(objVar)) { + switch (usage) { + case ExpUsage::Common: + case ExpUsage::Assignment: + isScoped = true; + break; + default: break; + } + if (isScoped) { + temp.push_back(indent() + "do"s + nll(x)); + pushScope(); + } objVar = getUnusedName("_obj_"sv); if (auto colonItem = ast_cast(partOne->items.back())) { auto chainValue = x->new_ptr(); @@ -3549,7 +3561,7 @@ private: if (_withVars.empty()) { throw std::logic_error(_info.errorMessage("short dot/colon syntax must be called within a with block"sv, x)); } - chainValue->items.push_back(toAst(_withVars.top(), x)); + chainValue->items.push_back(toAst(_withVars.top(), x)); } auto newObj = singleVariableFrom(chainValue); if (!newObj.empty()) { @@ -3650,6 +3662,10 @@ private: default: break; } + if (isScoped) { + popScope(); + temp.push_back(indent() + "end"s + nlr(x)); + } out.push_back(join(temp)); return true; } -- cgit v1.2.3-55-g6feb