mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 00:37:34 +00:00
LibJS: Implement Array length setter
This commit is contained in:
parent
d5d3e0b4ed
commit
418092a71a
4 changed files with 74 additions and 19 deletions
|
@ -49,19 +49,37 @@ Array::~Array()
|
|||
{
|
||||
}
|
||||
|
||||
Value Array::length_getter(Interpreter& interpreter)
|
||||
Array* array_from(Interpreter& interpreter)
|
||||
{
|
||||
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
|
||||
if (!this_object)
|
||||
return {};
|
||||
if (!this_object->is_array())
|
||||
return interpreter.throw_exception<TypeError>("Not an array");
|
||||
return Value(static_cast<const Array*>(this_object)->length());
|
||||
if (!this_object->is_array()) {
|
||||
interpreter.throw_exception<TypeError>("Not an Array");
|
||||
return nullptr;
|
||||
}
|
||||
return static_cast<Array*>(this_object);
|
||||
}
|
||||
|
||||
void Array::length_setter(Interpreter&, Value)
|
||||
Value Array::length_getter(Interpreter& interpreter)
|
||||
{
|
||||
ASSERT_NOT_REACHED();
|
||||
auto* array = array_from(interpreter);
|
||||
if (!array)
|
||||
return {};
|
||||
return Value(array->length());
|
||||
}
|
||||
|
||||
void Array::length_setter(Interpreter& interpreter, Value value)
|
||||
{
|
||||
auto* array = array_from(interpreter);
|
||||
if (!array)
|
||||
return;
|
||||
auto length = value.to_number();
|
||||
if (length.is_nan() || length.is_infinity() || length.as_double() < 0) {
|
||||
interpreter.throw_exception<RangeError>("Invalid array length");
|
||||
return;
|
||||
}
|
||||
array->elements().resize(length.as_double());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue