<?xml version="1.0" encoding="UTF-8"?>
<!-- test that an the legal iterable collections are arrays, namely objects that satisfy instanceof(Array) in ECMAScript.
 	     the legal values for the 'item' attribute on foreach are legal ECMAScript variable names.. -->

<scxml xmlns="http://www.w3.org/2005/07/scxml" xmlns:conf="http://www.w3.org/2005/scxml-conformance" initial="s0" datamodel="ecmascript" version="1.0">
<datamodel>
  <data id="Var1" expr="0"/>
  <data id="Var2"/>
  <data id="Var3"/>
  <data id="Var4" expr="7"/>
  <data id="Var5" expr="[1,2,3]"/>
  <data id="Var6"/>
  </datamodel>
  
   <state id="s0">
      <onentry>
<!-- invalid array, legal item -->
        <foreach item="Var2" index="Var3" array="Var4">
          <assign location="Var1" expr="Var1 + 1"/>
          </foreach>
        <raise event="foo"/>
        </onentry>
   <transition event="error.execution" target="s1"/>
   <transition event="*" target="fail"/> 
   </state>
   
   <state id="s1">
      <onentry>
<!-- illegal item, legal array -->
        <foreach item="'continue'" index="Var3" array="Var5">
          <assign location="Var1" expr="Var1 + 1"/>
          </foreach>
        <raise event="bar"/>
        </onentry>
   <transition event="error.execution" target="s2"/>
   <transition event="bar" target="fail"/> 
   </state>

<state id="s2">
  <!-- check that var1 has its original value (so executable content never got executed -->
  <transition cond="Var1==0" target="s3"/>
  <transition target="fail"/>
  </state> 


<!-- finally check that a legal array works properly -->
<state id="s3">
	<onentry>
		<assign location="Var6" expr="0"/>
    <foreach item="Var2" array="Var5">
	  <assign location="Var6" expr="Var6 + Var2"/>
	</foreach>
	</onentry>
	  <transition cond="Var6==6" target="pass"/>
	  <transition target="fail"/>
 </state>   
   <final id="pass"><onentry><log label="Outcome" expr="'pass'"/></onentry></final>
   <final id="fail"><onentry><log label="Outcome" expr="'fail'"/></onentry></final>
   


</scxml>