From eceb244befb0992c47dd13b80bada954b4689053 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Wed, 5 Apr 2023 00:41:23 +0330 Subject: [PATCH] LibWasm: Allow Value::to() to perform statically valid conversions e.g. i32 -> i16 (if within range). --- .../LibWasm/AbstractMachine/AbstractMachine.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h index 4229198994..4c8ca6261f 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h +++ b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h @@ -126,10 +126,12 @@ public: Optional result; m_value.visit( [&](auto value) { - if constexpr (IsSame) - result = value; - else if constexpr (!IsFloatingPoint && IsSame>) - result = value; + if constexpr (IsSame || (!IsFloatingPoint && IsSame>)) { + result = static_cast(value); + } else if constexpr (!IsFloatingPoint && IsConvertible) { + if (AK::is_within_range(value)) + result = static_cast(value); + } }, [&](Reference const& value) { if constexpr (IsSame) {