1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 05:18:12 +00:00

LibJS: Implement basic functionality of Array.from()

The optional 2nd and 3rd arguments are not yet implemented.

This assumes that `this` is the Array constructor and doesn't yet
implement the more general behavior in the ES6 spec that allows
transferring this method to other constructors.
This commit is contained in:
Nico Weber 2020-08-17 09:52:24 -04:00 committed by Andreas Kling
parent a50a9d67ee
commit 8ebef785eb
3 changed files with 109 additions and 0 deletions

View file

@ -32,6 +32,7 @@
#include <LibJS/Runtime/ArrayConstructor.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Shape.h>
namespace JS {
@ -53,6 +54,7 @@ void ArrayConstructor::initialize(GlobalObject& global_object)
define_property("length", Value(1), Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function("from", from, 1, attr);
define_native_function("isArray", is_array, 1, attr);
define_native_function("of", of, 0, attr);
}
@ -84,6 +86,44 @@ Value ArrayConstructor::construct(Interpreter& interpreter, Function&)
return call(interpreter);
}
JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from)
{
auto value = interpreter.argument(0);
auto object = value.to_object(interpreter, global_object);
if (!object)
return {};
auto* array = Array::create(global_object);
// Array.from() lets you create Arrays from:
if (auto size = object->indexed_properties().array_like_size()) {
// * array-like objects (objects with a length property and indexed elements)
Vector<Value> elements;
elements.ensure_capacity(size);
for (size_t i = 0; i < size; ++i) {
elements.append(object->get(i));
if (interpreter.exception())
return {};
}
array->set_indexed_property_elements(move(elements));
} else {
// * iterable objects
get_iterator_values(global_object, value, [&](Value& element) {
if (interpreter.exception())
return IterationDecision::Break;
array->indexed_properties().append(element);
return IterationDecision::Continue;
});
if (interpreter.exception())
return {};
}
// FIXME: if interpreter.argument_count() >= 2: mapFn
// FIXME: if interpreter.argument_count() >= 3: thisArg
return array;
}
JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::is_array)
{
auto value = interpreter.argument(0);