summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Runtime
diff options
context:
space:
mode:
Diffstat (limited to 'Libraries/LibJS/Runtime')
-rw-r--r--Libraries/LibJS/Runtime/Array.cpp15
-rw-r--r--Libraries/LibJS/Runtime/Array.h2
-rw-r--r--Libraries/LibJS/Runtime/ArrayPrototype.cpp12
3 files changed, 28 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();