mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-28 13:43:45 +03:00
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:
parent
014cb1a55b
commit
36996bd720
Notes:
sideshowbarker
2024-07-19 06:32:06 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/36996bd720e Pull-request: https://github.com/SerenityOS/serenity/pull/2275 Reviewed-by: https://github.com/awesomekling
@ -1189,8 +1189,8 @@ PropertyName MemberExpression::computed_property_name(Interpreter& interpreter)
|
|||||||
|
|
||||||
ASSERT(!index.is_empty());
|
ASSERT(!index.is_empty());
|
||||||
|
|
||||||
if (index.is_integer() && index.to_i32() >= 0)
|
if (index.is_integer() && index.as_i32() >= 0)
|
||||||
return PropertyName(index.to_i32());
|
return PropertyName(index.as_i32());
|
||||||
|
|
||||||
auto index_string = index.to_string(interpreter);
|
auto index_string = index.to_string(interpreter);
|
||||||
if (interpreter.exception())
|
if (interpreter.exception())
|
||||||
|
@ -58,12 +58,12 @@ Value ArrayConstructor::call(Interpreter& interpreter)
|
|||||||
|
|
||||||
if (interpreter.argument_count() == 1 && interpreter.argument(0).is_number()) {
|
if (interpreter.argument_count() == 1 && interpreter.argument(0).is_number()) {
|
||||||
auto array_length_value = interpreter.argument(0);
|
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");
|
interpreter.throw_exception<TypeError>("Invalid array length");
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
auto* array = Array::create(interpreter.global_object());
|
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;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ BoundFunction* Function::bind(Value bound_this_value, Vector<Value> arguments)
|
|||||||
if (interpreter().exception())
|
if (interpreter().exception())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
if (length_property.is_number())
|
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;
|
Object* constructor_prototype = nullptr;
|
||||||
auto prototype_property = target_function.get("prototype");
|
auto prototype_property = target_function.get("prototype");
|
||||||
|
@ -118,7 +118,7 @@ Value StringPrototype::repeat(Interpreter& interpreter)
|
|||||||
return interpreter.throw_exception<RangeError>("repeat count must be a positive number");
|
return interpreter.throw_exception<RangeError>("repeat count must be a positive number");
|
||||||
if (count_value.is_infinity())
|
if (count_value.is_infinity())
|
||||||
return interpreter.throw_exception<RangeError>("repeat count must be a finite number");
|
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;
|
StringBuilder builder;
|
||||||
for (size_t i = 0; i < count; ++i)
|
for (size_t i = 0; i < count; ++i)
|
||||||
builder.append(string);
|
builder.append(string);
|
||||||
|
@ -82,7 +82,7 @@ String Value::to_string_without_side_effects() const
|
|||||||
return as_double() < 0 ? "-Infinity" : "Infinity";
|
return as_double() < 0 ? "-Infinity" : "Infinity";
|
||||||
|
|
||||||
if (is_integer())
|
if (is_integer())
|
||||||
return String::number(to_i32());
|
return String::number(as_i32());
|
||||||
return String::format("%.4f", as_double());
|
return String::format("%.4f", as_double());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,7 +125,7 @@ String Value::to_string(Interpreter& interpreter) const
|
|||||||
return as_double() < 0 ? "-Infinity" : "Infinity";
|
return as_double() < 0 ? "-Infinity" : "Infinity";
|
||||||
|
|
||||||
if (is_integer())
|
if (is_integer())
|
||||||
return String::number(to_i32());
|
return String::number(as_i32());
|
||||||
return String::format("%.4f", as_double());
|
return String::format("%.4f", as_double());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -242,6 +242,19 @@ Value Value::to_number(Interpreter& interpreter) const
|
|||||||
ASSERT_NOT_REACHED();
|
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
|
double Value::to_double(Interpreter& interpreter) const
|
||||||
{
|
{
|
||||||
auto number = to_number(interpreter);
|
auto number = to_number(interpreter);
|
||||||
@ -250,25 +263,12 @@ double Value::to_double(Interpreter& interpreter) const
|
|||||||
return number.as_double();
|
return number.as_double();
|
||||||
}
|
}
|
||||||
|
|
||||||
i32 Value::to_i32() const
|
|
||||||
{
|
|
||||||
return static_cast<i32>(as_double());
|
|
||||||
}
|
|
||||||
|
|
||||||
i32 Value::to_i32(Interpreter& interpreter) const
|
i32 Value::to_i32(Interpreter& interpreter) const
|
||||||
{
|
{
|
||||||
auto number = to_number(interpreter);
|
auto number = to_number(interpreter);
|
||||||
if (interpreter.exception())
|
if (interpreter.exception())
|
||||||
return 0;
|
return 0;
|
||||||
return number.to_i32();
|
return number.as_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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Value::to_size_t(Interpreter& interpreter) const
|
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);
|
auto number = to_number(interpreter);
|
||||||
if (interpreter.exception())
|
if (interpreter.exception())
|
||||||
return 0;
|
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)
|
Value greater_than(Interpreter& interpreter, Value lhs, Value rhs)
|
||||||
|
@ -184,15 +184,16 @@ public:
|
|||||||
|
|
||||||
Function& as_function();
|
Function& as_function();
|
||||||
|
|
||||||
|
i32 as_i32() const;
|
||||||
|
size_t as_size_t() const;
|
||||||
|
|
||||||
String to_string(Interpreter&) const;
|
String to_string(Interpreter&) const;
|
||||||
PrimitiveString* to_primitive_string(Interpreter&);
|
PrimitiveString* to_primitive_string(Interpreter&);
|
||||||
Value to_primitive(Interpreter&) const;
|
Value to_primitive(Interpreter&) const;
|
||||||
Object* to_object(Interpreter&) const;
|
Object* to_object(Interpreter&) const;
|
||||||
Value to_number(Interpreter&) const;
|
Value to_number(Interpreter&) const;
|
||||||
double to_double(Interpreter&) const;
|
double to_double(Interpreter&) const;
|
||||||
i32 to_i32() const;
|
|
||||||
i32 to_i32(Interpreter&) const;
|
i32 to_i32(Interpreter&) const;
|
||||||
size_t to_size_t() const;
|
|
||||||
size_t to_size_t(Interpreter&) const;
|
size_t to_size_t(Interpreter&) const;
|
||||||
bool to_boolean() const;
|
bool to_boolean() const;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user