Skip to content

Commit fa815b5

Browse files
committed
Fix #423
1 parent 1a665bc commit fa815b5

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

_glua-tests/issues.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,15 @@ function test()
445445
assert(f..", "..a.d == "1, e")
446446
end
447447
test()
448+
449+
-- issue #423
450+
function test()
451+
local a, b, c = "1", "3", "1"
452+
a, b, c= tonumber(a), tonumber(b) or a, tonumber(c)
453+
assert(a == 1)
454+
assert(type(a) == "number")
455+
assert(b == 3)
456+
assert(type(b) == "number")
457+
assert(c == 1)
458+
assert(type(c) == "number")
459+
end

compile.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,13 +1650,13 @@ func compileLogicalOpExprAux(context *funcContext, reg int, expr ast.Expr, ec *e
16501650
isLastAnd := elselabel == lb.e && thenlabel != elselabel
16511651
isLastOr := thenlabel == lb.e && hasnextcond
16521652

1653-
if ident, ok := expr.(*ast.IdentExpr); ok && getIdentRefType(context, context, ident) == ecLocal && (isLastAnd || isLastOr) {
1653+
if ident, ok := expr.(*ast.IdentExpr); ok && (isLastAnd || isLastOr) && getIdentRefType(context, context, ident) == ecLocal {
16541654
b := context.FindLocalVar(ident.Value)
1655-
if sreg != b {
1656-
code.AddABC(OP_TESTSET, sreg, b, 0^flip, sline(expr))
1657-
} else {
1658-
code.AddABC(OP_TEST, sreg, b, 0^flip, sline(expr))
1655+
op := OP_TESTSET
1656+
if sreg == b {
1657+
op = OP_TEST
16591658
}
1659+
code.AddABC(op, sreg, b, 0^flip, sline(expr))
16601660
} else if !hasnextcond && thenlabel == elselabel {
16611661
reg += compileExpr(context, reg, expr, &expcontext{ec.ctype, intMax(a, sreg), ec.varargopt})
16621662
last := context.Code.Last()
@@ -1667,7 +1667,11 @@ func compileLogicalOpExprAux(context *funcContext, reg int, expr ast.Expr, ec *e
16671667
}
16681668
} else {
16691669
reg += compileExpr(context, reg, expr, ecnone(0))
1670-
code.AddABC(OP_TEST, a, 0, 0^flip, sline(expr))
1670+
if !hasnextcond {
1671+
code.AddABC(OP_TEST, a, 0, 0^flip, sline(expr))
1672+
} else {
1673+
code.AddABC(OP_TESTSET, sreg, a, 0^flip, sline(expr))
1674+
}
16711675
}
16721676
code.AddASbx(OP_JMP, 0, jumplabel, sline(expr))
16731677
} // }}}

0 commit comments

Comments
 (0)