/**
 * Copyright 2017 The GreyCat Authors.  All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package greycat.internal.task;

import greycat.*;
import greycat.base.BaseNode;
import greycat.struct.Buffer;

class ActionCloneNodes implements Action {

    @Override
    public void eval(final TaskContext ctx) {

        TaskResult<Node> previousResult = ctx.resultAsNodes();
        Node[] previousNodes = (Node[]) previousResult.asArray();
        Node[] clones = new Node[previousNodes.length];
        for (int i = 0; i < previousNodes.length; i++) {
            clones[i] = ((BaseNode) previousNodes[i]).createClone();
        }
        previousResult.free();
        ctx.continueWith(ctx.wrap(clones));
    }

    @Override
    public void serialize(final Buffer builder) {
        builder.writeString(CoreActionNames.CLONE_NODES);
        builder.writeChar(Constants.TASK_PARAM_OPEN);
        builder.writeChar(Constants.TASK_PARAM_CLOSE);
    }


    @Override
    public final String name() {
        return CoreActionNames.CLONE_NODES;
    }

}

@@ -737,16 +737,46 @@
 .Buffer;
+%0Aimport greycat.utility.Tuple;
 %0A%0Aclass 
@@ -870,24 +870,100 @@
 ext ctx) %7B%0A%0A
+        if(ctx.result()==null) %7B%0A            ctx.continueTask();%0A        %7D%0A%0A
         Task
@@ -972,14 +972,8 @@
 sult
-%3CNode%3E
  pre
@@ -1000,15 +1000,8 @@
 sult
-AsNodes
 ();%0A
@@ -1012,231 +1012,271 @@
     
-Node%5B%5D previousNodes = (Node%5B%5D) previousResult.asArray();%0A        Node%5B%5D clones = new Node%5BpreviousNodes.length%5D;%0A        for (int i = 0; i %3C previousNodes.length; i++) %7B%0A            clones%5Bi%5D = ((BaseNode) previousNodes%5Bi%5D
+TaskResult nextResult = ctx.newResult();%0A        TaskResultIterator it = previousResult.iterator();%0A        while(it.hasNext()) %7B%0A            Object source = it.next();%0A            if(source instanceof BaseNode) %7B%0A                nextResult.add(((BaseNode) source
 ).cr
@@ -1286,16 +1286,17 @@
 eClone()
+)
 ;%0A      
@@ -1301,40 +1301,84 @@
     
+   
-%7D%0A        previousResult.free();
+ %7D else %7B%0A                nextResult.add(source);%0A            %7D%0A        %7D
 %0A   
@@ -1403,24 +1403,18 @@
 ith(
-ctx.wrap(clones)
+nextResult
 );%0A 

fix clone action