Implement Array.prototype.splice
authorRoberto Raggi <roberto.raggi@nokia.com>
Mon, 21 May 2012 12:21:57 +0000 (14:21 +0200)
committerRoberto Raggi <roberto.raggi@nokia.com>
Mon, 21 May 2012 12:21:57 +0000 (14:21 +0200)
qv4ecmaobjects.cpp

index a6e9239..54bd0eb 100644 (file)
@@ -1329,8 +1329,21 @@ void ArrayPrototype::method_sort(Context *ctx)
 
 void ArrayPrototype::method_splice(Context *ctx)
 {
+    if (ctx->argumentCount < 2)
+        return;
+
+    double start = ctx->argument(0).toInteger(ctx);
+    double deleteCount = ctx->argument(1).toInteger(ctx);
+    Value a = Value::fromObject(ctx->engine->newArrayObject());
     Value self = ctx->thisObject;
     if (ArrayObject *instance = self.asArrayObject()) {
+        QVector<Value> items;
+        for (size_t i = 2; i < ctx->argumentCount; ++i)
+            items << ctx->argument(i);
+        ArrayObject *otherInstance = a.asArrayObject();
+        Q_ASSERT(otherInstance);
+        instance->value.splice(start, deleteCount, items, otherInstance->value);
+        ctx->result = a;
     } else {
         assert(!"generic implementation of Array.prototype.splice");
     }