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

Unified Diff: src/builtins.cc

Issue 604033: Introduce builtin for Array.shift function. (Closed)
Patch Set: 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
« no previous file with comments | « src/builtins.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/builtins.cc
diff --git a/src/builtins.cc b/src/builtins.cc
index 18afe9119d634fe0001375f6f5dcdb4b9fd05bf3..854337784bb866494cf7aae7febe05fba88fff31 100644
--- a/src/builtins.cc
+++ b/src/builtins.cc
@@ -335,6 +335,41 @@ BUILTIN(ArrayPop) {
}
+BUILTIN(ArrayShift) {
+ JSArray* array = JSArray::cast(*args.receiver());
+ ASSERT(array->HasFastElements());
+ Object* undefined = Heap::undefined_value();
Mads Ager (chromium) 2010/02/12 13:27:51 Get rid of the local variable and just put in Heap
antonm 2010/02/12 14:05:55 Done.
+
+ int len = Smi::cast(array->length())->value();
+ if (len == 0) return undefined;
+
+ // 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);
+
+ // Set the length.
+ array->set_length(Smi::FromInt(len - 1));
+
+ if (first->IsTheHole())
Mads Ager (chromium) 2010/02/12 13:27:51 Please use braces around the body.
antonm 2010/02/12 14:05:55 Done.
+ first = prototype->GetElement(0);
+
+ for (int i = 0; i < len - 1; i++) {
+ Object* e = elms->get(i + 1);
+ if (e->IsTheHole())
Mads Ager (chromium) 2010/02/12 13:27:51 Please use braces around the body.
antonm 2010/02/12 14:05:55 Done.
+ e = prototype->GetElement(i + 1);
+ elms->set(i, e);
Mads Ager (chromium) 2010/02/12 13:27:51 Will this preserve array holes? Won't GetElement
antonm 2010/02/12 14:05:55 Nice catch, thanks a lot. Regression test added.
+ }
+ elms->set(len - 1, Heap::the_hole_value());
+
+ return first;
+}
+
+
// -----------------------------------------------------------------------------
//
« no previous file with comments | « src/builtins.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698