On Sep 29, 12:32 am, Stefan Weiss <[email protected]> wrote: > Hello again. > > I'm having major problems with the JavaScript engine which comes bundled > with Java 6 (version 1.6.0_10 here). After working my way through the > rather uncomfortable API provided by the javax.script package, I found > that some of the scripts we use were producing errors in unexpected > places. I was able to pinpoint the problem to assignments like this: > > var x = undefined || 0; > > This assigns undefined to x, instead of 0. Other, similar assignments > are also broken (see test class below). Unfortunately, this is a very > frequently used construct. Is (or was) this a known problem, and is > there a workaround? > > The engine identifies itself as "Mozilla Rhino, version 1.6 release 2". > I realize that's a very old version, and I've never had this problem > with more recent versions of the standalone Rhino package. > > Thanks in advance, > Stefan > > --------------------------------------------------- > The test class below produces the following output (for me): > > input expected actual result > > false number number 0.0 > undefined number undefined sun.org.j[...].undefi...@e3b895 > null number number 0.0 > '' number string > 0 number number 0.0 > -0 number number 0.0 > true boolean boolean true > ' ' string string > 1 number boolean true > {} object object [object Object] > > --------------------------------------------------- > > import java.util.*; > import javax.script.*; > > public final class RhinoTest3 { > > public static void main (String[] args) throws Exception > { > ScriptEngineManager mgr = new ScriptEngineManager(); > ScriptEngine engine = mgr.getEngineByName("JavaScript"); > Invocable inv = (Invocable) engine; > > Map<String, String> tests = new LinkedHashMap<String, String>(); > // input expected result type > tests.put("false", "number"); > tests.put("undefined", "number"); > tests.put("null", "number"); > tests.put("''", "number"); > tests.put("0", "number"); > tests.put("-0", "number"); > tests.put("true", "boolean"); > tests.put("' '", "string"); > tests.put("1", "number"); > tests.put("{}", "object"); > > System.out.println("input expected actual result"); > System.out.println(); > for (Map.Entry test : tests.entrySet()) { > String js = "var x = " + test.getKey() + " || 0; typeof x"; > String type = (String) engine.eval(js); > System.out.printf("%-10s %-10s %-10s %s%n", > test.getKey(), > test.getValue(), > type, > "" + engine.get("x")); > } > } > > > > }
The "undefined || 0" expression is evaluated successfully in mozilla.org Rhino. I only checked the latest version but I'd be surprised if Rhino ever did the wrong thing here as the functionality is so basic. The fact that in the example you cite that undefined is a sun.org.j[...].Undefined makes me wonder if there's some problem caused by the port of Rhino into the JDK. At any rate, I'd recommend using mozilla.org Rhino. If you want to keep using the javax.script interface, then you can use the adapter mentioned here: https://bugzilla.mozilla.org/show_bug.cgi?id=379385 --N _______________________________________________ dev-tech-js-engine-rhino mailing list [email protected] https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino
