Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Unified Diff: src/builtins.cc

Issue 606017: Introduce builtin for Array.shift function. (Closed)
Patch Set: Next round of Mads' comments Created 10 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/builtins.cc
diff --git a/src/builtins.cc b/src/builtins.cc
index 9c4cdace1d445912f3160530ee10c891fd971312..1159a213dce7b3961b8f58117371b34f42a28243 100644
--- a/src/builtins.cc
+++ b/src/builtins.cc
@@ -313,6 +313,43 @@ BUILTIN(ArrayPop) {
}
+BUILTIN(ArrayShift) {
+ JSArray* array = JSArray::cast(*args.receiver());
+ ASSERT(array->HasFastElements());
+
+ int len = Smi::cast(array->length())->value();
+ if (len == 0) return Heap::undefined_value();
+
+ // Fetch the prototype.
+ JSFunction* array_function =
+ Top::context()->global_context()->array_function();
+ JSObject* prototype = JSObject::cast(array_function->prototype());
+
+ // Get first element
+ FixedArray* elms = FixedArray::cast(array->elements());
+ Object* first = elms->get(0);
+
+ if (first->IsTheHole()) {
+ first = prototype->GetElement(0);
+ }
+
+ // Shift the elements.
+ for (int i = 0; i < len - 1; i++) {
+ Object* e = elms->get(i + 1);
+ if (e->IsTheHole() && prototype->HasElement(i + 1)) {
+ e = prototype->GetElement(i + 1);
+ }
+ elms->set(i, e);
+ }
+ elms->set(len - 1, Heap::the_hole_value());
+
+ // Set the length.
+ array->set_length(Smi::FromInt(len - 1));
+
+ return first;
+}
+
+
// -----------------------------------------------------------------------------
//
« no previous file with comments | « src/builtins.h ('k') | test/mjsunit/array-shift.js » ('j') | test/mjsunit/array-shift.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698