1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 12:27:34 +00:00

LibJS: Add Array.prototype.some

This commit is contained in:
Kesse Jones 2020-04-27 20:41:57 -03:00 committed by Andreas Kling
parent c14fedd562
commit 1c4d776ccc
3 changed files with 74 additions and 0 deletions

View file

@ -61,6 +61,7 @@ ArrayPrototype::ArrayPrototype()
put_native_function("includes", includes, 1, attr);
put_native_function("find", find, 1, attr);
put_native_function("findIndex", find_index, 1, attr);
put_native_function("some", some, 1, attr);
put("length", Value(0), Attribute::Configurable);
}
@ -501,4 +502,41 @@ Value ArrayPrototype::find_index(Interpreter& interpreter)
return Value(-1);
}
Value ArrayPrototype::some(Interpreter& interpreter)
{
auto* array = array_from(interpreter);
if (!array)
return {};
auto* callback = callback_from_args(interpreter, "some");
if (!callback)
return {};
auto this_value = interpreter.argument(1);
auto array_size = array->elements().size();
for (size_t i = 0; i < array_size; ++i) {
if (i >= array->elements().size())
break;
auto value = array->elements().at(i);
if (value.is_empty())
continue;
MarkedValueList arguments(interpreter.heap());
arguments.append(value);
arguments.append(Value((i32)i));
arguments.append(array);
auto result = interpreter.call(callback, this_value, move(arguments));
if (interpreter.exception())
return {};
if (result.to_boolean())
return Value(true);
}
return Value(false);
}
}

View file

@ -56,5 +56,7 @@ private:
static Value includes(Interpreter&);
static Value find(Interpreter&);
static Value find_index(Interpreter&);
static Value some(Interpreter&);
};
}