mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:37:46 +00:00
LibJS: Implement Array.prototype.flat
This commit is contained in:
parent
83d880180e
commit
3940635ed3
3 changed files with 50 additions and 0 deletions
|
@ -80,6 +80,7 @@ void ArrayPrototype::initialize(GlobalObject& global_object)
|
||||||
define_native_function(vm.names.splice, splice, 2, attr);
|
define_native_function(vm.names.splice, splice, 2, attr);
|
||||||
define_native_function(vm.names.fill, fill, 1, attr);
|
define_native_function(vm.names.fill, fill, 1, attr);
|
||||||
define_native_function(vm.names.values, values, 0, attr);
|
define_native_function(vm.names.values, values, 0, attr);
|
||||||
|
define_native_function(vm.names.flat, flat, 0, attr);
|
||||||
define_property(vm.names.length, Value(0), Attribute::Configurable);
|
define_property(vm.names.length, Value(0), Attribute::Configurable);
|
||||||
|
|
||||||
// Use define_property here instead of define_native_function so that
|
// Use define_property here instead of define_native_function so that
|
||||||
|
@ -1034,4 +1035,51 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::values)
|
||||||
return ArrayIterator::create(global_object, this_object, Object::PropertyKind::Value);
|
return ArrayIterator::create(global_object, this_object, Object::PropertyKind::Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void recursive_array_flat(VM& vm, GlobalObject& global_object, Array& new_array, Object& array, double depth)
|
||||||
|
{
|
||||||
|
auto array_length = length_of_array_like(global_object, array);
|
||||||
|
if (vm.exception())
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (size_t j = 0; j < array_length; ++j) {
|
||||||
|
auto value = array.get(j);
|
||||||
|
if (vm.exception())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (depth > 0 && value.is_array()) {
|
||||||
|
recursive_array_flat(vm, global_object, new_array, value.as_array(), depth - 1);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!value.is_empty()) {
|
||||||
|
new_array.indexed_properties().append(value);
|
||||||
|
if (vm.exception())
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::flat)
|
||||||
|
{
|
||||||
|
auto* this_object = vm.this_value(global_object).to_object(global_object);
|
||||||
|
if (!this_object)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
auto* new_array = Array::create(global_object);
|
||||||
|
|
||||||
|
double depth = 1;
|
||||||
|
if (vm.argument_count() > 0) {
|
||||||
|
auto depth_argument = vm.argument(0);
|
||||||
|
if (!depth_argument.is_undefined()) {
|
||||||
|
auto depth_num = depth_argument.to_integer_or_infinity(global_object);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
depth = max(depth_num, 0.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
recursive_array_flat(vm, global_object, *new_array, *this_object, depth);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
return new_array;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,6 +66,7 @@ private:
|
||||||
JS_DECLARE_NATIVE_FUNCTION(splice);
|
JS_DECLARE_NATIVE_FUNCTION(splice);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(fill);
|
JS_DECLARE_NATIVE_FUNCTION(fill);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(values);
|
JS_DECLARE_NATIVE_FUNCTION(values);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(flat);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,6 +108,7 @@ namespace JS {
|
||||||
P(find) \
|
P(find) \
|
||||||
P(findIndex) \
|
P(findIndex) \
|
||||||
P(flags) \
|
P(flags) \
|
||||||
|
P(flat) \
|
||||||
P(floor) \
|
P(floor) \
|
||||||
P(forEach) \
|
P(forEach) \
|
||||||
P(from) \
|
P(from) \
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue