mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 19:37:34 +00:00
LibJS: Add Array.prototype.unshift()
This commit is contained in:
parent
5da1a40ccf
commit
29253bf932
3 changed files with 42 additions and 0 deletions
|
@ -41,6 +41,7 @@ ArrayPrototype::ArrayPrototype()
|
|||
put_native_function("push", push, 1);
|
||||
put_native_function("shift", shift, 0);
|
||||
put_native_function("toString", to_string, 0);
|
||||
put_native_function("unshift", unshift, 1);
|
||||
put("length", Value(0));
|
||||
}
|
||||
|
||||
|
@ -70,6 +71,16 @@ Value ArrayPrototype::push(Interpreter& interpreter)
|
|||
return Value(array->length());
|
||||
}
|
||||
|
||||
Value ArrayPrototype::unshift(Interpreter& interpreter)
|
||||
{
|
||||
auto* array = array_from(interpreter);
|
||||
if (!array)
|
||||
return {};
|
||||
for (size_t i = 0; i < interpreter.argument_count(); ++i)
|
||||
array->elements().insert(i, interpreter.argument(i));
|
||||
return Value(array->length());
|
||||
}
|
||||
|
||||
Value ArrayPrototype::pop(Interpreter& interpreter)
|
||||
{
|
||||
auto* array = array_from(interpreter);
|
||||
|
|
|
@ -42,6 +42,7 @@ private:
|
|||
static Value push(Interpreter&);
|
||||
static Value shift(Interpreter&);
|
||||
static Value to_string(Interpreter&);
|
||||
static Value unshift(Interpreter&);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue