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 // extra
const bool = (value: LuaType): boolean => coerceToBoolean(value) 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 // unary
const not = (value: LuaType): boolean => !bool(value) const not = (value: LuaType): boolean => !bool(value)
@ -156,6 +160,8 @@ const ge = (left: LuaType, right: LuaType): boolean => !lt(left, right)
const operators = { const operators = {
bool, bool,
and,
or,
not, not,
unm, unm,
bnot, bnot,
@ -178,7 +184,7 @@ const operators = {
lt, lt,
le, le,
gt, gt,
ge ge,
} }
export { operators } export { operators }

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

Loading…
Cancel
Save