1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-09 17:44:56 +09:00

LibJS: Add Array.prototype.map()

This commit is contained in:
Linus Groh 2020-04-13 20:09:56 +01:00 committed by Andreas Kling
parent f03d005bc4
commit f7df521073
Notes: sideshowbarker 2024-07-19 07:36:17 +09:00
3 changed files with 78 additions and 0 deletions

View file

@ -41,6 +41,7 @@ ArrayPrototype::ArrayPrototype()
{
put_native_function("filter", filter, 1);
put_native_function("forEach", for_each, 1);
put_native_function("map", map, 1);
put_native_function("pop", pop, 0);
put_native_function("push", push, 1);
put_native_function("shift", shift, 0);
@ -128,6 +129,31 @@ Value ArrayPrototype::for_each(Interpreter& interpreter)
return js_undefined();
}
Value ArrayPrototype::map(Interpreter& interpreter)
{
auto* array = array_from(interpreter);
if (!array)
return {};
auto* callback = callback_from_args(interpreter, "map");
if (!callback)
return {};
auto this_value = interpreter.argument(1);
auto initial_array_size = array->elements().size();
auto* new_array = interpreter.heap().allocate<Array>();
for (size_t i = 0; i < initial_array_size; ++i) {
if (i >= array->elements().size())
break;
auto value = array->elements()[i];
if (value.is_empty())
continue;
auto result = interpreter.call(callback, this_value, { value, Value((i32)i), array });
if (interpreter.exception())
return {};
new_array->elements().append(result);
}
return Value(new_array);
}
Value ArrayPrototype::push(Interpreter& interpreter)
{
auto* array = array_from(interpreter);

View file

@ -41,6 +41,7 @@ private:
static Value filter(Interpreter&);
static Value for_each(Interpreter&);
static Value map(Interpreter&);
static Value pop(Interpreter&);
static Value push(Interpreter&);
static Value shift(Interpreter&);

View file

@ -0,0 +1,51 @@
load("test-common.js");
try {
assert(Array.prototype.map.length === 1);
try {
[].map();
assertNotReached();
} catch (e) {
assert(e.name === "TypeError");
assert(e.message === "Array.prototype.map() requires at least one argument");
}
try {
[].map(undefined);
assertNotReached();
} catch (e) {
assert(e.name === "TypeError");
assert(e.message === "undefined is not a function");
}
var callbackCalled = 0;
var callback = () => { callbackCalled++; };
assert([].map(callback).length === 0);
assert(callbackCalled === 0);
assert([1, 2, 3].map(callback).length === 3);
assert(callbackCalled === 3);
var results = [undefined, null, true, "foo", 42, {}].map((value, index) => "" + index + " -> " + value);
assert(results.length === 6);
assert(results[0] === "0 -> undefined");
assert(results[1] === "1 -> null");
assert(results[2] === "2 -> true");
assert(results[3] === "3 -> foo");
assert(results[4] === "4 -> 42");
assert(results[5] === "5 -> [object Object]");
var squaredNumbers = [0, 1, 2, 3, 4].map(x => x ** 2);
assert(squaredNumbers.length === 5);
assert(squaredNumbers[0] === 0);
assert(squaredNumbers[1] === 1);
assert(squaredNumbers[2] === 4);
assert(squaredNumbers[3] === 9);
assert(squaredNumbers[4] === 16);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}