Fix conversion to from float to int

Round the value.

The previous behavior is that

 - For the interpreter, we were rounding (same as new behavior)
 - for Rust and C++ we were truncating, unless the properties were
   inlinined and then we were keeping it as float
This commit is contained in:
Olivier Goffart 2024-07-23 14:06:23 +02:00
parent ef92c1a161
commit f5d003d1e2
5 changed files with 59 additions and 27 deletions

View file

@ -1987,6 +1987,9 @@ fn compile_expression(expr: &Expression, ctx: &EvaluationContext) -> TokenStream
Expression::Cast { from, to } => {
let f = compile_expression(from, ctx);
match (from.ty(ctx), to) {
(Type::Float32, Type::Int32) => {
quote!(((#f as f32).round() as i32))
}
(from, Type::String) if from.as_unit_product().is_some() => {
quote!(sp::SharedString::from(sp::format!("{}", #f).as_str()))
}
@ -2645,9 +2648,9 @@ fn compile_builtin_function_call(
let (r, g, b, a) =
(a.next().unwrap(), a.next().unwrap(), a.next().unwrap(), a.next().unwrap());
quote!({
let r: u8 = (#r as u32).max(0).min(255) as u8;
let g: u8 = (#g as u32).max(0).min(255) as u8;
let b: u8 = (#b as u32).max(0).min(255) as u8;
let r: u8 = (#r as u32).min(255) as u8;
let g: u8 = (#g as u32).min(255) as u8;
let b: u8 = (#b as u32).min(255) as u8;
let a: u8 = (255. * (#a as f32)).max(0.).min(255.) as u8;
sp::Color::from_argb_u8(a, r, g, b)
})