diff options
author | Linus Groh <mail@linusgroh.de> | 2020-05-18 08:59:35 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-18 10:21:51 +0200 |
commit | 36996bd720e0ead315dea5057be9f5551af4a787 (patch) | |
tree | d669c5fac275bca007d2be5477c8554f3bd350cd /Libraries | |
parent | 014cb1a55b70bfc2c5defbdeee3e13827f463e32 (diff) | |
download | serenity-36996bd720e0ead315dea5057be9f5551af4a787.zip |
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.
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibJS/AST.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/ArrayConstructor.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Function.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/StringPrototype.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Value.cpp | 34 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Value.h | 5 |
6 files changed, 27 insertions, 24 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<TypeError>("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<Value> arguments) if (interpreter().exception()) return nullptr; if (length_property.is_number()) - computed_length = max(0, length_property.to_i32() - static_cast<i32>(arguments.size())); + computed_length = max(0, length_property.as_i32() - static_cast<i32>(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<RangeError>("repeat count must be a positive number"); if (count_value.is_infinity()) return interpreter.throw_exception<RangeError>("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,33 +242,33 @@ Value Value::to_number(Interpreter& interpreter) const ASSERT_NOT_REACHED(); } -double Value::to_double(Interpreter& interpreter) const +i32 Value::as_i32() const { - auto number = to_number(interpreter); - if (interpreter.exception()) - return 0; - return number.as_double(); + return static_cast<i32>(as_double()); } -i32 Value::to_i32() const +size_t Value::as_size_t() const { - return static_cast<i32>(as_double()); + ASSERT(as_double() >= 0); + if (is_nan()) + return 0; + return min((double)(i32)as_double(), MAX_ARRAY_LIKE_INDEX); } -i32 Value::to_i32(Interpreter& interpreter) const +double Value::to_double(Interpreter& interpreter) const { auto number = to_number(interpreter); if (interpreter.exception()) return 0; - return number.to_i32(); + return number.as_double(); } -size_t Value::to_size_t() const +i32 Value::to_i32(Interpreter& interpreter) const { - ASSERT(type() == Type::Number); - if (is_nan() || as_double() <= 0) + auto number = to_number(interpreter); + if (interpreter.exception()) 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; |