diff --git a/src/compiler.ts b/src/compiler.ts index f84a31cd8b..0598097c8b 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -3394,6 +3394,14 @@ export class Compiler extends DiagnosticEmitter { var type = element.type; this.currentType = type; switch (type.kind) { + case TypeKind.BOOL: { + return this.module.i32( + element.constantValueKind == ConstantValueKind.INTEGER + // @ts-ignore + ? i64_ne(element.constantIntegerValue, i64_zero) + : 0 + ); + } case TypeKind.I8: case TypeKind.I16: { let shift = type.computeSmallIntegerShift(Type.i32); @@ -3404,8 +3412,7 @@ export class Compiler extends DiagnosticEmitter { ); // recognized by canOverflow } case TypeKind.U8: - case TypeKind.U16: - case TypeKind.BOOL: { + case TypeKind.U16: { let mask = element.type.computeSmallIntegerMask(Type.i32); return this.module.i32( element.constantValueKind == ConstantValueKind.INTEGER diff --git a/tests/compiler/overflow.ts b/tests/compiler/overflow.ts index 9ca7e3bf01..6cd77e35c7 100644 --- a/tests/compiler/overflow.ts +++ b/tests/compiler/overflow.ts @@ -129,6 +129,27 @@ assert(val - 1 == 0xffff); } +// special cases +{ + const b1 = 2; + assert(b1 == true); + + const b2 = -1; + assert(b2 == true); + + const b3 = 0; + assert(b3 == false); + + let b4 = 2; + assert(b4 == true); + + let b5 = -1; + assert(b5 == true); + + let b6 = 0; + assert(b6 == false); +} + { // regression #2131 const a: u32 = 65; diff --git a/tests/compiler/overflow.untouched.wat b/tests/compiler/overflow.untouched.wat index e4aa1a45e8..6586fcaf6f 100644 --- a/tests/compiler/overflow.untouched.wat +++ b/tests/compiler/overflow.untouched.wat @@ -677,6 +677,64 @@ call $~lib/builtins/abort unreachable end + i32.const 1 + i32.const 1 + i32.eq + drop + i32.const 1 + i32.const 1 + i32.eq + drop + i32.const 0 + i32.const 0 + i32.eq + drop + i32.const 2 + local.set $0 + local.get $0 + i32.const 0 + i32.ne + i32.const 1 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 144 + i32.const 3 + call $~lib/builtins/abort + unreachable + end + i32.const -1 + local.set $1 + local.get $1 + i32.const 0 + i32.ne + i32.const 1 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 147 + i32.const 3 + call $~lib/builtins/abort + unreachable + end + i32.const 0 + local.set $2 + local.get $2 + i32.const 0 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 150 + i32.const 3 + call $~lib/builtins/abort + unreachable + end i32.const 65 i32.const 63457 i32.const 504 @@ -690,15 +748,15 @@ i32.const 65535 i32.and i32.add - local.set $0 - local.get $0 + local.set $2 + local.get $2 i32.const 65597 i32.eq i32.eqz if i32.const 0 i32.const 32 - i32.const 138 + i32.const 159 i32.const 3 call $~lib/builtins/abort unreachable