fix logical operators evaluating their LHS twice

pull/21/head
teoxoy 2 years ago
parent 3d4f9e2865
commit ca94a68d03

@ -36,6 +36,10 @@ const binaryBooleanArithmetic = (
// extra
const bool = (value: LuaType): boolean => coerceToBoolean(value)
// logical
const and = (l: LuaType, r: LuaType): LuaType => coerceToBoolean(l) ? r : l
const or = (l: LuaType, r: LuaType): LuaType => coerceToBoolean(l) ? l : r
// unary
const not = (value: LuaType): boolean => !bool(value)
@ -156,6 +160,8 @@ const ge = (left: LuaType, right: LuaType): boolean => !lt(left, right)
const operators = {
bool,
and,
or,
not,
unm,
bnot,
@ -178,7 +184,7 @@ const operators = {
lt,
le,
gt,
ge
ge,
}
export { operators }

@ -303,10 +303,10 @@ const generate = (node: luaparse.Node): string | MemExpr => {
const operator = node.operator
if (operator === 'and') {
return `(!__lua.bool(${left})?${left}:${right})`
return `__lua.and(${left},${right})`
}
if (operator === 'or') {
return `(__lua.bool(${left})?${left}:${right})`
return `__lua.or(${left},${right})`
}
throw new Error(`Unhandled logical operator: ${node.operator}`)
}

Loading…
Cancel
Save