AssemblyScript / assemblyscript

A TypeScript-like language for WebAssembly.
https://www.assemblyscript.org
Apache License 2.0
16.6k stars 650 forks source link

The results of arithmetic operations with u8 are not truncated to 8 bits, when assigned to f64 #2812

Closed palisarbaro closed 5 months ago

palisarbaro commented 5 months ago

Bug description

export function add(a: u8): f64 {
  let x:u8 = 128
  x = x + a
  return x;
}

This function compiles to

(module
 (type $0 (func (param i32) (result f64)))
 (memory $0 0)
 (export "add" (func $assembly/index/add))
 (export "memory" (memory $0))
 (func $assembly/index/add (param $0 i32) (result f64)
  local.get $0
  i32.const 128
  i32.add
  ;; i32.const 255   missing cast to u8
  ;; i32.and             
  f64.convert_i32_u
 )
)

Steps to reproduce

export function add(a: u8): f64 {
  let x:u8 = 128
  x = x + a
  return x;
}

From js

add(129) // actual 257; expected 1

AssemblyScript version

0.27.22

CountBleck commented 5 months ago

I'd assume this is a problem involving a missing Constraints.MustWrap.