From f7df52107365ed5defd604545b5c8dc8348165ee Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 13 Apr 2020 20:09:56 +0100 Subject: [PATCH] LibJS: Add Array.prototype.map() --- Libraries/LibJS/Runtime/ArrayPrototype.cpp | 26 ++++++++++ Libraries/LibJS/Runtime/ArrayPrototype.h | 1 + Libraries/LibJS/Tests/Array.prototype.map.js | 51 ++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 Libraries/LibJS/Tests/Array.prototype.map.js diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp index bca562e9af..f1c726269e 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -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(); + 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); diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.h b/Libraries/LibJS/Runtime/ArrayPrototype.h index 69effbfcd2..efe81b8821 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.h +++ b/Libraries/LibJS/Runtime/ArrayPrototype.h @@ -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&); diff --git a/Libraries/LibJS/Tests/Array.prototype.map.js b/Libraries/LibJS/Tests/Array.prototype.map.js new file mode 100644 index 0000000000..ee72b19ff4 --- /dev/null +++ b/Libraries/LibJS/Tests/Array.prototype.map.js @@ -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); +}