Skip to content

Commit

Permalink
Fix #423
Browse files Browse the repository at this point in the history
  • Loading branch information
yuin committed Jan 22, 2023
1 parent 1a665bc commit fa815b5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
12 changes: 12 additions & 0 deletions _glua-tests/issues.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 10 additions & 6 deletions compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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))
} // }}}
Expand Down

0 comments on commit fa815b5

Please sign in to comment.