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

LibJS: Support mapFn argument of Array.from

This commit is contained in:
tuqqu 2021-04-06 23:12:38 +03:00 committed by Andreas Kling
parent bb9cd13a56
commit 7bd0384fa6
2 changed files with 115 additions and 33 deletions

View file

@ -95,15 +95,37 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from)
auto* array = Array::create(global_object); auto* array = Array::create(global_object);
Function* map_fn = nullptr;
if (!vm.argument(1).is_undefined()) {
auto callback = vm.argument(1);
if (!callback.is_function()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, callback.to_string_without_side_effects());
return {};
}
map_fn = &callback.as_function();
}
// Array.from() lets you create Arrays from: // Array.from() lets you create Arrays from:
if (auto size = object->indexed_properties().array_like_size()) { if (auto size = object->indexed_properties().array_like_size()) {
// * array-like objects (objects with a length property and indexed elements) // * array-like objects (objects with a length property and indexed elements)
MarkedValueList elements(vm.heap()); MarkedValueList elements(vm.heap());
elements.ensure_capacity(size); elements.ensure_capacity(size);
for (size_t i = 0; i < size; ++i) { for (size_t i = 0; i < size; ++i) {
elements.append(object->get(i)); if (map_fn) {
if (vm.exception()) auto element = object->get(i);
return {}; if (vm.exception())
return {};
auto map_fn_result = vm.call(*map_fn, value, element);
if (vm.exception())
return {};
elements.append(map_fn_result);
} else {
elements.append(object->get(i));
if (vm.exception())
return {};
}
} }
array->set_indexed_property_elements(move(elements)); array->set_indexed_property_elements(move(elements));
} else { } else {
@ -111,14 +133,23 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from)
get_iterator_values(global_object, value, [&](Value element) { get_iterator_values(global_object, value, [&](Value element) {
if (vm.exception()) if (vm.exception())
return IterationDecision::Break; return IterationDecision::Break;
array->indexed_properties().append(element);
if (map_fn) {
auto map_fn_result = vm.call(*map_fn, value, element);
if (vm.exception())
return IterationDecision::Break;
array->indexed_properties().append(map_fn_result);
} else {
array->indexed_properties().append(element);
}
return IterationDecision::Continue; return IterationDecision::Continue;
}); });
if (vm.exception()) if (vm.exception())
return {}; return {};
} }
// FIXME: if interpreter.argument_count() >= 2: mapFn
// FIXME: if interpreter.argument_count() >= 3: thisArg // FIXME: if interpreter.argument_count() >= 3: thisArg
return array; return array;

View file

@ -3,20 +3,32 @@ test("length is 1", () => {
}); });
describe("normal behavior", () => { describe("normal behavior", () => {
test("empty array", () => { test("empty array, no mapFn", () => {
var a = Array.from([]); const a = Array.from([]);
expect(a instanceof Array).toBeTrue(); expect(a instanceof Array).toBeTrue();
expect(a).toHaveLength(0); expect(a).toHaveLength(0);
}); });
test("empty string", () => { test("empty array, with mapFn", () => {
var a = Array.from(""); const a = Array.from([], n => n);
expect(a instanceof Array).toBeTrue(); expect(a instanceof Array).toBeTrue();
expect(a).toHaveLength(0); expect(a).toHaveLength(0);
}); });
test("non-empty array", () => { test("empty string, no mapFn", () => {
var a = Array.from([5, 8, 1]); const a = Array.from("");
expect(a instanceof Array).toBeTrue();
expect(a).toHaveLength(0);
});
test("empty string, with mapFn", () => {
const a = Array.from("", n => n);
expect(a instanceof Array).toBeTrue();
expect(a).toHaveLength(0);
});
test("non-empty array, no mapFn", () => {
const a = Array.from([5, 8, 1]);
expect(a instanceof Array).toBeTrue(); expect(a instanceof Array).toBeTrue();
expect(a).toHaveLength(3); expect(a).toHaveLength(3);
expect(a[0]).toBe(5); expect(a[0]).toBe(5);
@ -24,8 +36,17 @@ describe("normal behavior", () => {
expect(a[2]).toBe(1); expect(a[2]).toBe(1);
}); });
test("non-empty string", () => { test("non-empty array, with mapFn", () => {
var a = Array.from("what"); const a = Array.from([5, 8, 1], n => ++n);
expect(a instanceof Array).toBeTrue();
expect(a).toHaveLength(3);
expect(a[0]).toBe(6);
expect(a[1]).toBe(9);
expect(a[2]).toBe(2);
});
test("non-empty string, no mapFn", () => {
const a = Array.from("what");
expect(a instanceof Array).toBeTrue(); expect(a instanceof Array).toBeTrue();
expect(a).toHaveLength(4); expect(a).toHaveLength(4);
expect(a[0]).toBe("w"); expect(a[0]).toBe("w");
@ -34,36 +55,66 @@ describe("normal behavior", () => {
expect(a[3]).toBe("t"); expect(a[3]).toBe("t");
}); });
test("shallow array copy", () => { test("non-empty string, with mapFn", () => {
var a = [1, 2, 3]; const a = Array.from("what", n => n + n);
var b = Array.from([a]); expect(a instanceof Array).toBeTrue();
expect(a).toHaveLength(4);
expect(a[0]).toBe("ww");
expect(a[1]).toBe("hh");
expect(a[2]).toBe("aa");
expect(a[3]).toBe("tt");
});
test("shallow array copy, no mapFn", () => {
const a = [1, 2, 3];
const b = Array.from([a]);
expect(b instanceof Array).toBeTrue(); expect(b instanceof Array).toBeTrue();
expect(b).toHaveLength(1); expect(b).toHaveLength(1);
b[0][0] = 4; b[0][0] = 4;
expect(a[0]).toBe(4); expect(a[0]).toBe(4);
}); });
test("from iterator", () => { test("shallow array copy, with mapFn", () => {
function rangeIterator(begin, end) { const a = [1, 2, 3];
return { const b = Array.from([a], n => n.map(n => n + 2));
[Symbol.iterator]() { expect(b instanceof Array).toBeTrue();
let value = begin - 1; expect(b).toHaveLength(1);
return { b[0][0] = 10;
next() { expect(a[0]).toBe(1);
if (value < end) { expect(b[0][0]).toBe(10);
value += 1; expect(b[0][1]).toBe(4);
} expect(b[0][2]).toBe(5);
return { value: value, done: value >= end }; });
},
};
},
};
}
var a = Array.from(rangeIterator(8, 10)); const rangeIterator = function (begin, end) {
return {
[Symbol.iterator]() {
let value = begin - 1;
return {
next() {
if (value < end) {
value += 1;
}
return { value: value, done: value >= end };
},
};
},
};
};
test("from iterator, no mapFn", () => {
const a = Array.from(rangeIterator(8, 10));
expect(a instanceof Array).toBeTrue(); expect(a instanceof Array).toBeTrue();
expect(a).toHaveLength(2); expect(a).toHaveLength(2);
expect(a[0]).toBe(8); expect(a[0]).toBe(8);
expect(a[1]).toBe(9); expect(a[1]).toBe(9);
}); });
test("from iterator, with mapFn", () => {
const a = Array.from(rangeIterator(8, 10), n => --n);
expect(a instanceof Array).toBeTrue();
expect(a).toHaveLength(2);
expect(a[0]).toBe(7);
expect(a[1]).toBe(8);
});
}); });