diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp index b8b1f32a72..a5e52642db 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -37,9 +37,9 @@ namespace JS { ArrayPrototype::ArrayPrototype() { - put_native_function("shift", shift); - put_native_function("pop", pop); + put_native_function("pop", pop, 0); put_native_function("push", push, 1); + put_native_function("shift", shift, 0); put_native_function("toString", to_string, 0); put("length", Value(0)); } @@ -65,9 +65,8 @@ Value ArrayPrototype::push(Interpreter& interpreter) auto* array = array_from(interpreter); if (!array) return {}; - if (!interpreter.argument_count()) - return js_undefined(); - array->elements().append(interpreter.argument(0)); + for (size_t i = 0; i < interpreter.argument_count(); ++i) + array->elements().append(interpreter.argument(i)); return Value(array->length()); } diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.h b/Libraries/LibJS/Runtime/ArrayPrototype.h index bed19807a5..d4df4858ff 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.h +++ b/Libraries/LibJS/Runtime/ArrayPrototype.h @@ -38,9 +38,9 @@ public: private: virtual const char* class_name() const override { return "ArrayPrototype"; } + static Value pop(Interpreter&); static Value push(Interpreter&); static Value shift(Interpreter&); - static Value pop(Interpreter&); static Value to_string(Interpreter&); }; diff --git a/Libraries/LibJS/Tests/Array.prototype.push.js b/Libraries/LibJS/Tests/Array.prototype.push.js new file mode 100644 index 0000000000..f739aec463 --- /dev/null +++ b/Libraries/LibJS/Tests/Array.prototype.push.js @@ -0,0 +1,30 @@ +load("test-common.js"); + +try { + assert(Array.prototype.push.length === 1); + + var a = ["hello"]; + var length = a.push(); + assert(length === 1); + assert(a.length === 1); + assert(a[0] === "hello"); + + length = a.push("friends"); + assert(length === 2); + assert(a.length === 2); + assert(a[0] === "hello"); + assert(a[1] === "friends"); + + length = a.push(1, 2, 3); + assert(length === 5); + assert(a.length === 5); + assert(a[0] === "hello"); + assert(a[1] === "friends"); + assert(a[2] === 1); + assert(a[3] === 2); + assert(a[4] === 3); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +}