JDK-8134731 : Function.prototype.apply interacts incorrectly with 'arguments'
  • Type: Bug
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Affected Version: 8u60,9
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: os_x
  • CPU: x86
  • Submitted: 2015-08-24
  • Updated: 2016-02-02
  • Resolved: 2015-08-31
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 8 JDK 9
8u72Fixed 9 b81Fixed
Description
FULL PRODUCT VERSION :
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Darwin c056.local 14.5.0 Darwin Kernel Version 14.5.0: Wed Jul 29 02:26:53 PDT 2015; root:xnu-2782.40.9~1/RELEASE_X86_64 x86_64

A DESCRIPTION OF THE PROBLEM :
The following code incorrectly produces `4` in Nashorn (it should produce `2`):

(function(f){
  return function(a1, a2, a3, a4){
    return f.apply(this, arguments);
  }
})
(function(){
  return arguments.length;
})
(1, 2);

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Observe that the code provided in the description produces an incorrect result when run through Nashorn.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
2, the number of arguments with which the function is called.
ACTUAL -
4, the number of formal parameters of the function.

REPRODUCIBILITY :
This bug can be reproduced always.


Comments
Fix suggestion: limit apply2call only to functions that don't explicitly declare parameters.
31-08-2015

Reproduced with jdk9-dev nashorn tip. Modified script to print the apply the result. (function(f){ return function(a1, a2, a3, a4){ print(f.apply(this, arguments)); } }) (function(){ return arguments.length; }) (234, 435); Prints 4 instead of 2. But if script is run with -J-Dnashorn.apply2call=false, then it prints 2 as expected. => This is an issue with Apply specialization optimization.
31-08-2015

Added the correct test case after getting clarification from the submitter. Tested in JDK 8u51 - Fail JDK 8u60 - Fail JDK 9EA b78- Fail -When the number of formal parameters is less than the number of arguments passed, correct output is generated. It gives the output as the number of arguments passed. -When the number of formal parameters is more than the number of arguments passed, incorrect output is generated i.e. it gives the output as number of formal parameters. Moving to dev-team to look into it.
31-08-2015

Attached the mail sent to user for clarification.
28-08-2015

Could not reproduce the issue with the test case provided. The attached test case is returning expected results in Windows on JDK 8u51, 8u60 and 9 . Tested on JDK 9 in Mac OS also, that also returned expected results. Sending an email to user to confirm.
28-08-2015