PDA

View Full Version : Limitations of 'eval'?


bjaspan
11-14-2005, 07:20 PM
I want to use the JavaScript eval function to evaluate expressions I get as strings from XML (specifically, my XML needs to refer to objects within my program that it operates on, and while could write code to explicitly translate from the strings to objects, I'd rather not).

It *almost* works, but not completely. In the "evals" dataset below, all the expressions evaluate correctly except the last two. I think both of the last two expressions should evaluate to the LzView containing Monday, but they evaluate to null instead. Why? Does eval not support [] or function calls?

Thanks,

Barry

<canvas>
<dataset name="days">
<day d="Mon"/>
<day d="Tue"/>
<day d="Wed"/>
<day d="Thu"/>
<day d="Fri"/>
<day d="Sat"/>
<day d="Sun"/>
</dataset>

<dataset name="evals">
<eval e="canvas"/>
<eval e="canvas.datasets"/>
<eval e="canvas.datasets.days"/>
<eval e="canvas.one"/>
<eval e="canvas.one.two"/>
<eval e="canvas.one.two.clones"/>
<eval e="canvas.one.two.clones[0]"/>
<eval e="canvas.one.two.getCloneNumber(0)"/>
</dataset>

<view name="one">
<simplelayout axis="y"/>
<view name="two" datapath="days:/day">
<text name="three" datapath="@d"/>
</view>
</view>

<view datapath="evals:/eval">
<view datapath="@e">
<method event="ondata">
<![CDATA[
Debug.write(this.data + " = " + eval(this.data));
]]>
</method>
</view>
</view>
</canvas>

bbarkley
11-15-2005, 06:56 AM
Eval is crippled in Laszlo.

See:
http://www.openlaszlo.org/lps-latest/docs/guide/ecmascript-and-lzx.html#ftn.d0e29393

bjaspan
11-15-2005, 07:10 AM
Well, that explains the situation perfectly. :-) I've worked around the problem. Thanks,

Barry

hqm
11-15-2005, 08:46 AM
The eval is crippled because Flash doesn't implement it fully. It is possible if you are desperate to implement an Javascript evaluator in Javascript, but it would add some size to your app.

bjaspan
11-15-2005, 10:35 AM
That is basically what I did, in a very small way. If I want to refer to canvas.one.two.clones[0].col1, I write in XML

<ref to="canvas.one.two.clones">
<sub sub="0"/>
<sub sub="col1"/>
</ref>

My evaluator then looks like this (ev is a datapointer pointing to the <ref> tag):

var ref = eval(ev.getAttr("to"));
if (ev.hasChildNodes()) {
var kid = ev.getFirstChild();
do {
if (kid.nodeName == "sub") {
ref = ref[kid.getAttr("sub")];
}
} while (kid = kid.getNextSibling());
}

If I need more functionality than [], I can create new tags for inside the <ref>, such as <call func="name"><arg .../></call>.

Thanks,

Barry

bjaspan
11-15-2005, 10:37 AM
By the way, note that adding support for function calls could be a HUGE cross-site scripting security hole if your app receives the XML data to be eval'ed from an untrusted source.

Barry