Skip to content

Instantly share code, notes, and snippets.

@kitak
Last active June 21, 2016 15:38
Show Gist options
  • Save kitak/8b3ce24cc9e063994fefac11565f2980 to your computer and use it in GitHub Desktop.
Save kitak/8b3ce24cc9e063994fefac11565f2980 to your computer and use it in GitHub Desktop.
Nashorn's bug with using `with` statement
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment