diff options
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibJS/Runtime/Array.cpp | 15 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Array.h | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/ArrayPrototype.cpp | 12 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/Array.prototype.pop.js | 19 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/Array.prototype.shift.js | 19 |
5 files changed, 66 insertions, 1 deletions
diff --git a/Libraries/LibJS/Runtime/Array.cpp b/Libraries/LibJS/Runtime/Array.cpp index df048c2389..6d3149f423 100644 --- a/Libraries/LibJS/Runtime/Array.cpp +++ b/Libraries/LibJS/Runtime/Array.cpp @@ -47,6 +47,20 @@ Array::~Array() { } +Value Array::shift() +{ + if (m_elements.size() == 0) + return js_undefined(); + return Value(m_elements.take_first()); +} + +Value Array::pop() +{ + if (m_elements.size() == 0) + return js_undefined(); + return Value(m_elements.take_last()); +} + void Array::push(Value value) { m_elements.append(value); @@ -82,5 +96,4 @@ bool Array::put_own_property(Object& this_object, const FlyString& property_name } return Object::put_own_property(this_object, property_name, value); } - } diff --git a/Libraries/LibJS/Runtime/Array.h b/Libraries/LibJS/Runtime/Array.h index 4e9a56abae..e43e845526 100644 --- a/Libraries/LibJS/Runtime/Array.h +++ b/Libraries/LibJS/Runtime/Array.h @@ -39,6 +39,8 @@ public: const Vector<Value>& elements() const { return m_elements; } Vector<Value>& elements() { return m_elements; } + Value shift(); + Value pop(); void push(Value); private: diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp index 2aed718142..996d28096d 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -35,6 +35,18 @@ namespace JS { ArrayPrototype::ArrayPrototype() { + put_native_function("shift", [](Object* this_object, const Vector<Value>&) -> Value { + ASSERT(this_object); + ASSERT(this_object->is_array()); + return static_cast<Array*>(this_object)->shift(); + }); + + put_native_function("pop", [](Object* this_object, const Vector<Value>&) -> Value { + ASSERT(this_object); + ASSERT(this_object->is_array()); + return static_cast<Array*>(this_object)->pop(); + }); + put_native_function("push", [](Object* this_object, const Vector<Value>& arguments) -> Value { if (arguments.is_empty()) return js_undefined(); diff --git a/Libraries/LibJS/Tests/Array.prototype.pop.js b/Libraries/LibJS/Tests/Array.prototype.pop.js new file mode 100644 index 0000000000..4cb895f5ab --- /dev/null +++ b/Libraries/LibJS/Tests/Array.prototype.pop.js @@ -0,0 +1,19 @@ +function assert(x) { if (!x) throw 1; } + +try { + var a = [1, 2, 3]; + var value = a.pop(); + assert(value === 3); + assert(a.length === 2); + assert(a[0] === 1); + assert(a[1] === 2); + + var a = []; + var value = a.pop(); + assert(value === undefined); + assert(a.length === 0); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} diff --git a/Libraries/LibJS/Tests/Array.prototype.shift.js b/Libraries/LibJS/Tests/Array.prototype.shift.js new file mode 100644 index 0000000000..1494556299 --- /dev/null +++ b/Libraries/LibJS/Tests/Array.prototype.shift.js @@ -0,0 +1,19 @@ +function assert(x) { if (!x) throw 1; } + +try { + var a = [1, 2, 3]; + var value = a.shift(); + assert(value === 1); + assert(a.length === 2); + assert(a[0] === 2); + assert(a[1] === 3); + + var a = []; + var value = a.shift(); + assert(value === undefined); + assert(a.length === 0); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} |