From fa815b5cd712a146016c373261cda69942ec74bb Mon Sep 17 00:00:00 2001 From: yuin Date: Sun, 22 Jan 2023 22:28:43 +0900 Subject: [PATCH] Fix #423 --- _glua-tests/issues.lua | 12 ++++++++++++ compile.go | 16 ++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/_glua-tests/issues.lua b/_glua-tests/issues.lua index 274530c7..514d0b7a 100644 --- a/_glua-tests/issues.lua +++ b/_glua-tests/issues.lua @@ -445,3 +445,15 @@ function test() assert(f..", "..a.d == "1, e") end test() + +-- issue #423 +function test() + local a, b, c = "1", "3", "1" + a, b, c= tonumber(a), tonumber(b) or a, tonumber(c) + assert(a == 1) + assert(type(a) == "number") + assert(b == 3) + assert(type(b) == "number") + assert(c == 1) + assert(type(c) == "number") +end diff --git a/compile.go b/compile.go index 90863095..c3736777 100644 --- a/compile.go +++ b/compile.go @@ -1650,13 +1650,13 @@ func compileLogicalOpExprAux(context *funcContext, reg int, expr ast.Expr, ec *e isLastAnd := elselabel == lb.e && thenlabel != elselabel isLastOr := thenlabel == lb.e && hasnextcond - if ident, ok := expr.(*ast.IdentExpr); ok && getIdentRefType(context, context, ident) == ecLocal && (isLastAnd || isLastOr) { + if ident, ok := expr.(*ast.IdentExpr); ok && (isLastAnd || isLastOr) && getIdentRefType(context, context, ident) == ecLocal { b := context.FindLocalVar(ident.Value) - if sreg != b { - code.AddABC(OP_TESTSET, sreg, b, 0^flip, sline(expr)) - } else { - code.AddABC(OP_TEST, sreg, b, 0^flip, sline(expr)) + op := OP_TESTSET + if sreg == b { + op = OP_TEST } + code.AddABC(op, sreg, b, 0^flip, sline(expr)) } else if !hasnextcond && thenlabel == elselabel { reg += compileExpr(context, reg, expr, &expcontext{ec.ctype, intMax(a, sreg), ec.varargopt}) last := context.Code.Last() @@ -1667,7 +1667,11 @@ func compileLogicalOpExprAux(context *funcContext, reg int, expr ast.Expr, ec *e } } else { reg += compileExpr(context, reg, expr, ecnone(0)) - code.AddABC(OP_TEST, a, 0, 0^flip, sline(expr)) + if !hasnextcond { + code.AddABC(OP_TEST, a, 0, 0^flip, sline(expr)) + } else { + code.AddABC(OP_TESTSET, sreg, a, 0^flip, sline(expr)) + } } code.AddASbx(OP_JMP, 0, jumplabel, sline(expr)) } // }}}