Menu

#7 Fix function parameter (JSRSriptEngine) variable scopes

Unstable_(example)
open
nobody
None
5
2013-04-27
2013-04-27
No

Don't know in which ticket category this should go, but since I have a patch that 'worksforme' I'm putting it here.

There are to (related) points I'm interested in solving:

1) Scope behavior of parameters/variables inside function calls differs from the 2.0 version.

2) Because of that when calling functions, omitting some parameters can lead to unexpected result.

Demonstration:

<function name="function1">
    <return>
        <template>${param1}</template>
    </return>
</function>

<var-def name="var1"><template>1</template></var-def>

<var-def name="newvar1">
    <call name="function1">
        <call-param name="param1"><var name="var1"/></call-param>
    </call>
</var-def>

<var-def name="newvar2">
    <call name="function1">
    </call>
</var-def>

The question is what would be the expected value of newvar2?

In previous (2.0) version the answer is nothing (""), but in 2.1 it is "1".
To me the first behavior seems right and logical. It would be excellent if the first behavior could be returned.

I've digged into the code for the last couple of hours and found out why this is happening - it's because when something (like variables/attributes) are passed to JSRScriptEngine they stay there 'forever'. I've made a simple patch that just before some processor is evaluated clears script scope so that it only contains variables from the current context (DynamicScopeContext).

One more example:

<function name="function1">
    <return>
        <case>
            <if condition="${p_var2 != void}">
                ...do something...
            </if>
            <else>
                ...do something else...                 
            </else>
        </case>
    </return>
</function>

<call name="function1">
    <call-param name="p_var1"><var name="var1"/></call-param>
    <call-param name="p_var2"><var name="var2"/></call-param>       
</call>

<call name="function1">
    <call-param name="p_var1"><var name="var1"/></call-param>
</call>

Currently the second invocation of function1 will have the value of p_var2 from previous function call and that to me is not logical (it should be void).
True, there is a workaround - one can always pass second parameter and not set anything ie. do something like this:

<call name="function1">
    <call-param name="p_var1"><var name="var1"/></call-param>
    <call-param name="p_var2"></call-param>
</call>

But when having complex functions (for me) that is to verbose and not to elegant.

Hope you guys feel the same way.

Btw. thanks for maintaining and improving this excellent project!
As someone mentioned - where is the "donate" button?! :)

1 Attachments

Discussion


Log in to post a comment.