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

LibJS: Array.from mapFn fixes + thisArg support

* Callback mapFn now properly supports second argument (index)
* Support of thisArg to be passed as "this" in vm.call
* Tests for all cases
This commit is contained in:
tuqqu 2021-04-12 11:59:26 +03:00 committed by Andreas Kling
parent 2ed5d19407
commit c8ad1df143
2 changed files with 108 additions and 19 deletions

View file

@ -105,6 +105,8 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from)
map_fn = &callback.as_function();
}
auto this_arg = vm.argument(2);
// 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)
@ -116,7 +118,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from)
if (vm.exception())
return {};
auto map_fn_result = vm.call(*map_fn, value, element);
auto map_fn_result = vm.call(*map_fn, this_arg, element, Value((i32)i));
if (vm.exception())
return {};
@ -130,12 +132,14 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from)
array->set_indexed_property_elements(move(elements));
} else {
// * iterable objects
i32 i = 0;
get_iterator_values(global_object, value, [&](Value element) {
if (vm.exception())
return IterationDecision::Break;
if (map_fn) {
auto map_fn_result = vm.call(*map_fn, value, element);
auto map_fn_result = vm.call(*map_fn, this_arg, element, Value(i));
i++;
if (vm.exception())
return IterationDecision::Break;
@ -150,8 +154,6 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from)
return {};
}
// FIXME: if interpreter.argument_count() >= 3: thisArg
return array;
}