JDK-6346732 : can't assign undefined or null value to JavaScript global variables
  • Type: Bug
  • Component: core-libs
  • Sub-Component: javax.script
  • Affected Version: 6
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2005-11-07
  • Updated: 2012-12-14
  • Resolved: 2005-12-03
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b63Fixed
Description
JavaScript has two special values 'null' and 'undefined' respectively. It should be possible to assign these values for global (or local) variables. This can be seen by following jrunscript sessions:

./jrunscript
js> var v = null
js> v
script error: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "v" is not defined. (<STDIN>#1) in <STDIN> at line number 1
js>quit()

./jrunscript
js> var u = undefined
js> u
script error: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "u" is not defined. (<STDIN>#1) in <STDIN> at line number 1
js>quit()
In fact, these are allowed for local variables as shown by following jrunscript sessions:

./jrunscript
js> function f() { var v = null; print(v); }
js> f()
null
js>quit()

./jrunscript
js> function g() { var u = undefined; print(u); }
js> g()
undefined
js>quit()

These are problems with API implementation rather than jrunscript tool. This can be verified by a simple program as below:

import javax.script.*;

public class t {
        public static void main(String[] args) throws Exception {
                ScriptEngineManager m = new ScriptEngineManager();
                ScriptEngine e = m.getEngineByName("js");
                e.eval("x = null; print(x)");
        }
}


or

import javax.script.*;

public class t {
        public static void main(String[] args) throws Exception {
                ScriptEngineManager m = new ScriptEngineManager();
                ScriptEngine e = m.getEngineByName("js");
                e.eval("x = undefined; print(x);");
        }
}


Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "x" is not defined. (<Unknown source>#1) in <Unknown source> at line number 1

is thrown with these programs.

Comments
EVALUATION We translate 'undefined' as 'null'. This should be avoided. In JavaScript, 'undefined' is different from 'null'. Second problem is that in many places, we are interpreting null return from Map.get(String) method as "no such variable". Instead, we should be using Map.containsKey(String) so that null value is permitted for script variables.
2005-11-11