United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6346732 can't assign undefined or null value to JavaScript global variables
JDK-6346732 : can't assign undefined or null value to JavaScript global variables

Details
Type:
Bug
Submit Date:
2005-11-07
Status:
Resolved
Updated Date:
2012-12-14
Project Name:
JDK
Resolved Date:
2005-12-03
Component:
core-libs
OS:
generic
Sub-Component:
javax.script
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:

Related Reports

Sub Tasks

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



Hardware and Software, Engineered to Work Together