Repository: commons-scxml Updated Branches: refs/heads/master 2a9e83027 -> 4f0841e04
Support Javascript (Nashorn) based arrays for foreach Project: http://git-wip-us.apache.org/repos/asf/commons-scxml/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-scxml/commit/b24e7f33 Tree: http://git-wip-us.apache.org/repos/asf/commons-scxml/tree/b24e7f33 Diff: http://git-wip-us.apache.org/repos/asf/commons-scxml/diff/b24e7f33 Branch: refs/heads/master Commit: b24e7f33aa4c19352a5aca076dc3880b3924ed61 Parents: 2a9e830 Author: Ate Douma <a...@apache.org> Authored: Mon Dec 28 21:43:26 2015 +0100 Committer: Ate Douma <a...@apache.org> Committed: Mon Dec 28 21:43:26 2015 +0100 ---------------------------------------------------------------------- .../java/org/apache/commons/scxml2/model/Foreach.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-scxml/blob/b24e7f33/src/main/java/org/apache/commons/scxml2/model/Foreach.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/scxml2/model/Foreach.java b/src/main/java/org/apache/commons/scxml2/model/Foreach.java index 0be121e..1325f79 100644 --- a/src/main/java/org/apache/commons/scxml2/model/Foreach.java +++ b/src/main/java/org/apache/commons/scxml2/model/Foreach.java @@ -19,6 +19,7 @@ package org.apache.commons.scxml2.model; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.List; +import java.util.Map; import org.apache.commons.scxml2.ActionExecutionContext; import org.apache.commons.scxml2.Context; @@ -49,7 +50,7 @@ public class Foreach extends Action implements ActionsContainer { public Foreach() { super(); - this.actions = new ArrayList<Action>(); + this.actions = new ArrayList<>(); } @Override @@ -103,7 +104,7 @@ public class Foreach extends Action implements ActionsContainer { ctx.setLocal(getNamespacesKey(), getNamespaces()); try { Object arrayObject = eval.eval(ctx,array); - if (arrayObject != null && (arrayObject instanceof Iterable || arrayObject.getClass().isArray())) { + if (arrayObject != null && (arrayObject.getClass().isArray() || arrayObject instanceof Iterable || arrayObject instanceof Map)) { if (arrayObject.getClass().isArray()) { for (int currentIndex = 0, size = Array.getLength(arrayObject); currentIndex < size; currentIndex++) { ctx.setLocal(item, Array.get(arrayObject, currentIndex)); @@ -117,12 +118,16 @@ public class Foreach extends Action implements ActionsContainer { } } else { + // In case of Javascript based arrays, the (Nashorn) engine returns a ScriptObjectMirror + // which (also) implements Map<String, Object), so then we can/must use the map values as Iterable + Iterable iterable = arrayObject instanceof Iterable ? (Iterable)arrayObject : ((Map)arrayObject).values(); + // Spec requires to iterate over a shallow copy of underlying array in a way that modifications to // the collection during the execution of <foreach> must not affect the iteration behavior. // For array objects (see above) this isn't needed, but for Iterables we don't have that guarantee // so we make a copy first - ArrayList<Object> arrayList = new ArrayList<Object>(); - for (Object value: (Iterable)arrayObject) { + ArrayList<Object> arrayList = new ArrayList<>(); + for (Object value: iterable) { arrayList.add(value); } int currentIndex = 0;