United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-8160034 : The `this` value in the `with` is broken by the repetition of a function call

Details
Type:
Bug
Submit Date:
2016-06-21
Status:
Resolved
Updated Date:
2016-08-04
Project Name:
JDK
Resolved Date:
2016-07-28
Component:
core-libs
OS:
generic
Sub-Component:
jdk.nashorn
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
8u20,9
Fixed Versions:

Related Reports

Sub Tasks

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
URL:   http://hg.openjdk.java.net/jdk9/jdk9/nashorn/rev/9940204f504c
User:  amurillo
Date:  2016-08-03 16:27:55 +0000

                                     
2016-08-03
URL:   http://hg.openjdk.java.net/jdk9/dev/nashorn/rev/9940204f504c
User:  hannesw
Date:  2016-07-28 14:27:13 +0000

                                     
2016-07-28
Found this to be a regression, so increased the priority and added the regression label.
                                     
2016-06-22
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



Hardware and Software, Engineered to Work Together