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.
This commit is contained in:
Linus Groh 2020-05-18 08:59:35 +01:00 committed by Andreas Kling
parent 014cb1a55b
commit 36996bd720
Notes: sideshowbarker 2024-07-19 06:32:06 +09:00
6 changed files with 28 additions and 25 deletions

View File

@ -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())

View File

@ -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;
}

View File

@ -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");

View File

@ -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);

View File

@ -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<i32>(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<i32>(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)

View File

@ -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;