jbeard4 / SCION

SCXML/Statecharts in JavaScript, moved to gitlab: https://gitlab.com/scion-scxml/scion
https://scion.scxml.io
Apache License 2.0
149 stars 29 forks source link

error test/scxml-test-framework/test/w3c-ecma/test152.txml.scxml #285

Closed feyzo closed 6 years ago

feyzo commented 10 years ago

https://github.com/jbeard4/scxml-test-framework/tree/master/test/scxml-test-framework/test/w3c-ecma/test152.txml.scxml

Error

null
Data:
"Unexpected string"
scxml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- test that an illegal array or item value causes error.execution and results in executable content
not being executed. -->
<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">
    [1,2,3]
    </data>
  </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="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>

JSON:

{
"initialConfiguration": [
"pass"
],
"events": []
}

jbeard4 commented 9 years ago

Error is in: <foreach item="'continue'" index="Var3" array="Var5"> @item and @index should be valid javascript identifiers. Use the same technique that you're using to lint <assign> @array should be valid js expression.

feyzo commented 9 years ago

I fixed the second foreach with illegal item. This test is now failing.

Cause of the failure is because array="Var4" on first foreach is not an array and test is expecting an error raised in that state. I think compiler is not going to be able to understand that completely. We can use dataModelAccumulators to check if the initial value is array or not.

But after datamodel declaration and before foreach, we can do <assign location="Var4" expr="[1,2,3]" /> and change Var4 to an array. I think that is why we need a check on the runtime.

I'm thinking of editing else here to raise error. https://github.com/jbeard4/SCION/blob/2.0.0-w3c-ecma/lib/compiler/scjson-to-module.js#L473-L485

What do you think?

jbeard4 commented 9 years ago

We need a runtime check. We implement a proprietary extension to the SCXML spec, where if an Array is not used as the array variable, the compiler assumes it is an object, and iterates over the object keys and values:

https://github.com/jbeard4/SCION/blob/2.0.0-w3c-ecma/lib/compiler/scjson-to-module.js#L479-L484

What we need is runtime logic that says that if the value for array is not an array, we raise an error event using this.raise.

feyzo commented 9 years ago

So are we going to do something like this?

if(Array.isArray(array))
    for(i=0;....)
else if(what to do here?)
   for(index in array)
else
    raise error.

How are we going to check the second if? I did some tests here: http://jsbin.com/futigi/7/edit?js,output

jbeard4 commented 9 years ago

Like:

if(Array.isArray(array))
    for(i=0;....)
else
    raise error.

I think we need this in order to be compliant with the spec.

feyzo commented 9 years ago

I did this: https://github.com/jbeard4/SCION/commit/191aa3b25ee95ec32fa4a06fcc07d70c7ee4e2f0#diff-a76ac3fcca872b7345475359ccdfbc50R479

Module: https://gist.github.com/feyzo/042ac93eb63637317d07 Log: https://gist.github.com/feyzo/0b437ff2f4eb7f58cc44

The test ends on s1 state without any transition or event on the queue i think.

jbeard4 commented 9 years ago

In the module, raise_line_27_column_61 is meant to queue up another error.execution event, but it looks like this isn't happening. Not sure why - next step would be to trace it in a debugger and look at the inner event queue at the end of the each big-step.