JDK-8160034 : The `this` value in the `with` is broken by the repetition of a function call
  • Type: Bug
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Affected Version: 8u20,9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2016-06-21
  • Updated: 2016-08-04
  • Resolved: 2016-07-28
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 9
9 b130Fixed
Description
FULL PRODUCT VERSION :
java version "1.8.0_77"
Java(TM) SE Runtime Environment (build 1.8.0_77-b03)
Java HotSpot(TM) 64-Bit Server VM (build 25.77-b03, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Darwin Kernel Version 15.3.0

A DESCRIPTION OF THE PROBLEM :
The `this` value in the `with` is broken by the repetition of a function call.
This is a code that the reproduces the problem.

```
function Foo() {
  this.bar = "bar";
  this.baz = "baz";
}

Foo.prototype._h = function(str) {
  print("expect `bar`: " + this.bar);
}

Foo.prototype._e = function() {
  print("expect `baz`: " + this.baz);
}

function makeFunction() {
  return new Function("with(this) { return _h(_e()); }");
}

var fn = makeFunction();
var foo = null;
for (var i=0; i < 20; i++) {
  foo = new Foo();
  fn.call(foo);
}

// jjs sample.js
// Output:
// expect `baz`: baz
// expect `bar`: bar
// expect `baz`: baz
// expect `bar`: bar
// expect `baz`: baz
// expect `bar`: bar
// expect `baz`: baz
// expect `bar`: bar
// expect `baz`: baz
// expect `bar`: bar
// expect `baz`: baz
// expect `bar`: bar
// expect `baz`: baz
// expect `bar`: bar
// expect `baz`: baz
// expect `bar`: bar
// expect `baz`: baz
// expect `bar`: bar
// expect `baz`: baz
// expect `bar`: bar
// expect `baz`: baz
// expect `bar`: bar
// expect `baz`: baz
// expect `bar`: bar
// expect `baz`: baz
// expect `bar`: bar
// expect `baz`: baz
// expect `bar`: bar
// expect `baz`: baz
// expect `bar`: bar
// expect `baz`: baz
// expect `bar`: bar
// expect `baz`: undefined
// expect `bar`: undefined
// expect `baz`: undefined
// expect `bar`: undefined
// expect `baz`: undefined
// expect `bar`: undefined
// expect `baz`: undefined
// expect `bar`: undefined
```

As can be seen from the reproduction code, by calling the function with the `with` statement more than 17 times , it seems the scope is broken.


REPRODUCIBILITY :
This bug can be reproduced always.


Comments
Ran the attached test case with: JDK 8 - Pass JDK 8u11 - Pass JDK 8u20 - Fail ( Failed after first few iterations) JDK 8u92 - Fail (Failed in last few iterations) JDK 9eab122 - Fail Output on JDK 8u11 (Passed Test case): ----------------------------------------------------- >java -version java version "1.8.0_11" Java(TM) SE Runtime Environment (build 1.8.0_11-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode) >jjs sample.js expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar Output on JDK 8u 20 (Failed Test case within first few iterations) ---------------------------------------------------------------------------------- >java -version java version "1.8.0_20" Java(TM) SE Runtime Environment (build 1.8.0_20-b26) Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode) >jjs sample.js expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: undefined expect `bar`: undefined expect `baz`: undefined expect `bar`: undefined expect `baz`: undefined expect `bar`: undefined expect `baz`: undefined expect `bar`: undefined expect `baz`: undefined expect `bar`: undefined expect `baz`: undefined expect `bar`: undefined expect `baz`: undefined expect `bar`: undefined expect `baz`: undefined expect `bar`: undefined expect `baz`: undefined expect `bar`: undefined expect `baz`: undefined expect `bar`: undefined expect `baz`: undefined expect `bar`: undefined expect `baz`: undefined expect `bar`: undefined Output on JDK 8u92 and 9ea (failed test case in last few iterations): -------------------------------------------------------------------------------------- >jjs sample.js expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: baz expect `bar`: bar expect `baz`: undefined expect `bar`: undefined expect `baz`: undefined expect `bar`: undefined expect `baz`: undefined expect `bar`: undefined expect `baz`: undefined expect `bar`: undefined
2016-06-22

Found this to be a regression, so increased the priority and added the regression label.
2016-06-22