Skip to content

Commit

Permalink
LibJS: Rename to_{i32,size_t}() to as_{i32,size_t}() for clarity
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
linusg authored and awesomekling committed May 18, 2020
1 parent 014cb1a commit 36996bd
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 24 deletions.
4 changes: 2 additions & 2 deletions Libraries/LibJS/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions Libraries/LibJS/Runtime/ArrayConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibJS/Runtime/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibJS/Runtime/StringPrototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 18 additions & 16 deletions Libraries/LibJS/Runtime/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions Libraries/LibJS/Runtime/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,16 @@ class Value {

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;

Expand Down

0 comments on commit 36996bd

Please sign in to comment.