Skip to content

Commit

Permalink
LibJS: Add Value::to_size_t()
Browse files Browse the repository at this point in the history
  • Loading branch information
linusg authored and awesomekling committed May 1, 2020
1 parent 62671be commit 65dbe17
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 1 addition & 3 deletions Libraries/LibJS/Runtime/FunctionPrototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,8 @@ Value FunctionPrototype::apply(Interpreter& interpreter)
return interpreter.call(function, this_arg);
if (!arg_array.is_object())
return interpreter.throw_exception<TypeError>("argument array must be an object");
size_t length = 0;
auto length_property = arg_array.as_object().get("length");
if (!length_property.is_empty())
length = length_property.to_number().to_i32();
auto length = length_property.to_size_t();
MarkedValueList arguments(interpreter.heap());
for (size_t i = 0; i < length; ++i) {
auto element = arg_array.as_object().get(String::number(i));
Expand Down
12 changes: 11 additions & 1 deletion Libraries/LibJS/Runtime/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ double Value::to_double() const
return to_number().as_double();
}

size_t Value::to_size_t() const
{
if (is_empty())
return 0;
auto number = to_number();
if (number.is_nan() || number.as_double() <= 0)
return 0;
return min(number.to_i32(), (i32)pow(2, 53) - 1);
}

Value greater_than(Interpreter&, Value lhs, Value rhs)
{
return Value(lhs.to_number().as_double() > rhs.to_number().as_double());
Expand Down Expand Up @@ -313,7 +323,7 @@ Value mod(Interpreter&, Value lhs, Value rhs)

double index = lhs.to_number().as_double();
double period = rhs.to_number().as_double();
double trunc = (double)(i32) (index / period);
double trunc = (double)(i32)(index / period);

return Value(index - trunc * period);
}
Expand Down
1 change: 1 addition & 0 deletions Libraries/LibJS/Runtime/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class Value {
Value to_number() const;
i32 to_i32() const;
double to_double() const;
size_t to_size_t() const;
Value to_primitive(Interpreter&) const;

Object* to_object(Heap&) const;
Expand Down

0 comments on commit 65dbe17

Please sign in to comment.