From eb72ba246631752c8a9f7de60749ff6898a5b5d0 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 18 May 2020 10:03:08 +0100 Subject: [PATCH] LibJS: Remove is_nan() check in as_size_t() and fix to_size_t() We need to call as_double() on the freshly converted number, not the value itself. --- Libraries/LibJS/Runtime/StringPrototype.cpp | 4 +++- Libraries/LibJS/Runtime/Value.cpp | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Libraries/LibJS/Runtime/StringPrototype.cpp b/Libraries/LibJS/Runtime/StringPrototype.cpp index 3da7fbe542..87a5f58b50 100644 --- a/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -118,7 +118,9 @@ Value StringPrototype::repeat(Interpreter& interpreter) return interpreter.throw_exception("repeat count must be a positive number"); if (count_value.is_infinity()) return interpreter.throw_exception("repeat count must be a finite number"); - auto count = count_value.as_size_t(); + auto count = count_value.to_size_t(interpreter); + if (interpreter.exception()) + return {}; StringBuilder builder; for (size_t i = 0; i < count; ++i) builder.append(string); diff --git a/Libraries/LibJS/Runtime/Value.cpp b/Libraries/LibJS/Runtime/Value.cpp index 194998d903..7c19e5529b 100644 --- a/Libraries/LibJS/Runtime/Value.cpp +++ b/Libraries/LibJS/Runtime/Value.cpp @@ -250,8 +250,6 @@ i32 Value::as_i32() const size_t Value::as_size_t() const { ASSERT(as_double() >= 0); - if (is_nan()) - return 0; return min((double)(i32)as_double(), MAX_ARRAY_LIKE_INDEX); } @@ -278,7 +276,9 @@ size_t Value::to_size_t(Interpreter& interpreter) const auto number = to_number(interpreter); if (interpreter.exception()) return 0; - if (as_double() <= 0) + if (number.is_nan()) + return 0; + if (number.as_double() <= 0) return 0; return number.as_size_t(); }