JDK-8182996 : Incorrect mapping Long type to JavaScript equivalent
  • Type: Bug
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Affected Version: 8u131,9
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2017-06-27
  • Updated: 2018-03-02
  • Resolved: 2017-06-29
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 10 JDK 8
10 b14Fixed 8u171Fixed
Related Reports
Relates :  
Description
When passing variables from Java to JavaScript (Nashorn) using the 
javax.script.Bindings API, variables of Long type are not always mapped to a 
JavaScript number. In some cases they shows up as a plain JavaScript Object 
instead. 
This happens if the javax.script.Bindings object already contains a variable 
of the same name. 
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM : 
1) Pass a Long variable to JavaScript via javax.script.Bindings API 
2) Verify the type in the JavaScript end by evaluating "typeof". Should 
correctly return "number" 
3) Pass a new Long value for the same variables via javax.script.Bindings API 

4) Verify the type in the JavaScript end again. Should correctly return 
"number", but returns "object" now. 

EXPECTED VERSUS ACTUAL BEHAVIOR : 
EXPECTED - 
Data type of passed variable should always be "number". 

Attached test case SHOULD produce the following printout: 
undefined 
number 
number 
ACTUAL - 
 Datatype but is now "object" instead" 

Attached test case PRODUCES the following printout instead: 
undefined 
number 
object 



REPRODUCIBILITY : 
This bug can be reproduced always. 

---------- BEGIN SOURCE ---------- 
import javax.script.*; 

public class JavaScriptTest { 

    ScriptEngine engine; 
    Bindings bindings; 

    public JavaScriptTest() throws ScriptException { 
        ScriptEngineManager mgr = new ScriptEngineManager(); 
        engine = mgr.getEngineByName("javascript"); 
        bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); 
         
        testType(); 
        bindings.put("fileSize", new Long(1000000000L)); 
        testType(); 
        bindings.put("fileSize", new Long(2000000000L)); 
        testType(); 
    } 

    public void testType() throws ScriptException { 
        engine.eval("print(typeof fileSize);"); 
    } 

    public static void main(String[] args) throws ScriptException { 
        new JavaScriptTest(); 
    } 
} 
Comments
After JDK-8144020 Longs are expected to be exposed as objects in Nashorn. However, the first time a binding is created, long values are converted to doubles, which are exposed as numbers. Only when an existing binding is updated with a new value longs are exposed as objects. The problem is in AccessorProperty#setInitialValue which still contains special logic for longs.
28-06-2017