From 36996bd720e0ead315dea5057be9f5551af4a787 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 18 May 2020 08:59:35 +0100 Subject: [PATCH] LibJS: Rename to_{i32,size_t}() to as_{i32,size_t}() for clarity As these parameter-less overloads don't change the value's type and just assume Type::Number, naming them as_i32() and as_size_t() is more appropriate. --- Libraries/LibJS/AST.cpp | 4 +-- Libraries/LibJS/Runtime/ArrayConstructor.cpp | 4 +-- Libraries/LibJS/Runtime/Function.cpp | 2 +- Libraries/LibJS/Runtime/StringPrototype.cpp | 2 +- Libraries/LibJS/Runtime/Value.cpp | 36 +++++++++++--------- Libraries/LibJS/Runtime/Value.h | 5 +-- 6 files changed, 28 insertions(+), 25 deletions(-) diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index 1811e8ab84..034a55ab8f 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -1189,8 +1189,8 @@ PropertyName MemberExpression::computed_property_name(Interpreter& interpreter) ASSERT(!index.is_empty()); - if (index.is_integer() && index.to_i32() >= 0) - return PropertyName(index.to_i32()); + if (index.is_integer() && index.as_i32() >= 0) + return PropertyName(index.as_i32()); auto index_string = index.to_string(interpreter); if (interpreter.exception()) diff --git a/Libraries/LibJS/Runtime/ArrayConstructor.cpp b/Libraries/LibJS/Runtime/ArrayConstructor.cpp index 3c5b7c07b6..d1423009ff 100644 --- a/Libraries/LibJS/Runtime/ArrayConstructor.cpp +++ b/Libraries/LibJS/Runtime/ArrayConstructor.cpp @@ -58,12 +58,12 @@ Value ArrayConstructor::call(Interpreter& interpreter) if (interpreter.argument_count() == 1 && interpreter.argument(0).is_number()) { auto array_length_value = interpreter.argument(0); - if (!array_length_value.is_integer() || array_length_value.to_i32() < 0) { + if (!array_length_value.is_integer() || array_length_value.as_i32() < 0) { interpreter.throw_exception("Invalid array length"); return {}; } auto* array = Array::create(interpreter.global_object()); - array->elements().resize(array_length_value.to_i32()); + array->elements().resize(array_length_value.as_i32()); return array; } diff --git a/Libraries/LibJS/Runtime/Function.cpp b/Libraries/LibJS/Runtime/Function.cpp index 24ce9dc26f..e09e197758 100644 --- a/Libraries/LibJS/Runtime/Function.cpp +++ b/Libraries/LibJS/Runtime/Function.cpp @@ -69,7 +69,7 @@ BoundFunction* Function::bind(Value bound_this_value, Vector arguments) if (interpreter().exception()) return nullptr; if (length_property.is_number()) - computed_length = max(0, length_property.to_i32() - static_cast(arguments.size())); + computed_length = max(0, length_property.as_i32() - static_cast(arguments.size())); Object* constructor_prototype = nullptr; auto prototype_property = target_function.get("prototype"); diff --git a/Libraries/LibJS/Runtime/StringPrototype.cpp b/Libraries/LibJS/Runtime/StringPrototype.cpp index 5b3847d1e3..3da7fbe542 100644 --- a/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -118,7 +118,7 @@ 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.to_size_t(); + auto count = count_value.as_size_t(); 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 1dc4746fd2..194998d903 100644 --- a/Libraries/LibJS/Runtime/Value.cpp +++ b/Libraries/LibJS/Runtime/Value.cpp @@ -82,7 +82,7 @@ String Value::to_string_without_side_effects() const return as_double() < 0 ? "-Infinity" : "Infinity"; if (is_integer()) - return String::number(to_i32()); + return String::number(as_i32()); return String::format("%.4f", as_double()); } @@ -125,7 +125,7 @@ String Value::to_string(Interpreter& interpreter) const return as_double() < 0 ? "-Infinity" : "Infinity"; if (is_integer()) - return String::number(to_i32()); + return String::number(as_i32()); return String::format("%.4f", as_double()); } @@ -242,6 +242,19 @@ Value Value::to_number(Interpreter& interpreter) const ASSERT_NOT_REACHED(); } +i32 Value::as_i32() const +{ + return static_cast(as_double()); +} + +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); +} + double Value::to_double(Interpreter& interpreter) const { auto number = to_number(interpreter); @@ -250,25 +263,12 @@ double Value::to_double(Interpreter& interpreter) const return number.as_double(); } -i32 Value::to_i32() const -{ - return static_cast(as_double()); -} - i32 Value::to_i32(Interpreter& interpreter) const { auto number = to_number(interpreter); if (interpreter.exception()) return 0; - return number.to_i32(); -} - -size_t Value::to_size_t() const -{ - ASSERT(type() == Type::Number); - if (is_nan() || as_double() <= 0) - return 0; - return min((double)(i32)as_double(), MAX_ARRAY_LIKE_INDEX); + return number.as_i32(); } size_t Value::to_size_t(Interpreter& interpreter) const @@ -278,7 +278,9 @@ size_t Value::to_size_t(Interpreter& interpreter) const auto number = to_number(interpreter); if (interpreter.exception()) return 0; - return number.to_size_t(); + if (as_double() <= 0) + return 0; + return number.as_size_t(); } Value greater_than(Interpreter& interpreter, Value lhs, Value rhs) diff --git a/Libraries/LibJS/Runtime/Value.h b/Libraries/LibJS/Runtime/Value.h index 4a04caa627..fe1793f912 100644 --- a/Libraries/LibJS/Runtime/Value.h +++ b/Libraries/LibJS/Runtime/Value.h @@ -184,15 +184,16 @@ public: Function& as_function(); + i32 as_i32() const; + size_t as_size_t() const; + String to_string(Interpreter&) const; PrimitiveString* to_primitive_string(Interpreter&); Value to_primitive(Interpreter&) const; Object* to_object(Interpreter&) const; Value to_number(Interpreter&) const; double to_double(Interpreter&) const; - i32 to_i32() const; i32 to_i32(Interpreter&) const; - size_t to_size_t() const; size_t to_size_t(Interpreter&) const; bool to_boolean() const;