mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 11:58:13 +00:00
LibJS: Add Array.prototype.filter()
This commit is contained in:
parent
866172a721
commit
f03d005bc4
3 changed files with 89 additions and 0 deletions
|
@ -39,6 +39,7 @@ namespace JS {
|
|||
|
||||
ArrayPrototype::ArrayPrototype()
|
||||
{
|
||||
put_native_function("filter", filter, 1);
|
||||
put_native_function("forEach", for_each, 1);
|
||||
put_native_function("pop", pop, 0);
|
||||
put_native_function("push", push, 1);
|
||||
|
@ -78,6 +79,32 @@ static Function* callback_from_args(Interpreter& interpreter, const String& name
|
|||
return static_cast<Function*>(&callback.as_object());
|
||||
}
|
||||
|
||||
Value ArrayPrototype::filter(Interpreter& interpreter)
|
||||
{
|
||||
auto* array = array_from(interpreter);
|
||||
if (!array)
|
||||
return {};
|
||||
auto* callback = callback_from_args(interpreter, "filter");
|
||||
if (!callback)
|
||||
return {};
|
||||
auto this_value = interpreter.argument(1);
|
||||
auto initial_array_size = array->elements().size();
|
||||
auto* new_array = interpreter.heap().allocate<Array>();
|
||||
for (size_t i = 0; i < initial_array_size; ++i) {
|
||||
if (i >= array->elements().size())
|
||||
break;
|
||||
auto value = array->elements()[i];
|
||||
if (value.is_empty())
|
||||
continue;
|
||||
auto result = interpreter.call(callback, this_value, { value, Value((i32)i), array });
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
if (result.to_boolean())
|
||||
new_array->elements().append(value);
|
||||
}
|
||||
return Value(new_array);
|
||||
}
|
||||
|
||||
Value ArrayPrototype::for_each(Interpreter& interpreter)
|
||||
{
|
||||
auto* array = array_from(interpreter);
|
||||
|
|
|
@ -39,6 +39,7 @@ public:
|
|||
private:
|
||||
virtual const char* class_name() const override { return "ArrayPrototype"; }
|
||||
|
||||
static Value filter(Interpreter&);
|
||||
static Value for_each(Interpreter&);
|
||||
static Value pop(Interpreter&);
|
||||
static Value push(Interpreter&);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue