Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(444)

Unified Diff: test/mjsunit/harmony/arrow-functions-this.js

Issue 1097283003: Resolve references to "this" the same way as normal variables (Closed) Base URL: https://chromium.googlesource.com/v8/v8@master
Patch Set: Add tests for "this" scoping in arrow functions Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« src/x64/full-codegen-x64.cc ('K') | « test/mjsunit/debug-scopes.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/mjsunit/harmony/arrow-functions-this.js
diff --git a/test/mjsunit/harmony/arrow-functions-this.js b/test/mjsunit/harmony/arrow-functions-this.js
new file mode 100644
index 0000000000000000000000000000000000000000..38f29a28b8486b2191511d0d5e9db8b2af390326
--- /dev/null
+++ b/test/mjsunit/harmony/arrow-functions-this.js
@@ -0,0 +1,74 @@
+// Copyright 2015 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Flags: --harmony-arrow-functions
+
+var object = {};
+var global = this;
+var call = Function.call.bind(Function.call);
+
+var globalSloppyArrow = ()=>this;
+var globalStrictArrow = ()=>{ "use strict"; return this; };
+var globalSloppyArrowEval = (s)=>eval(s);
+var globalStrictArrowEval = (s)=>{ "use strict"; return eval(s); };
+
+var sloppyFunctionArrow = function(){ return (()=>this)(); };
+var strictFunctionArrow = function(){ "use strict";
+ return (()=>this)(); };
+var sloppyFunctionEvalArrow = function(){ return eval("(()=>this)()"); };
+var strictFunctionEvalArrow = function(){ "use strict";
+ return eval("(()=>this)()"); };
+var sloppyFunctionArrowEval = function(s) { return (()=>eval(s))(); };
+var strictFunctionArrowEval = function(s) { "use strict";
+ return (()=>eval(s))(); };
+
+var withObject = { ['this']: object }
rossberg 2015/04/23 13:31:45 Nit: you don't need the brackets
+var arrowInsideWith, arrowInsideWithEval;
+with (withObject) {
+ arrowInsideWith = ()=>this;
+ arrowInsideWithEval = (s)=>eval(s);
+}
+
+assertEquals(global, call(globalSloppyArrow, object));
+assertEquals(global, call(globalStrictArrow, object));
+assertEquals(global, call(globalSloppyArrowEval, object, "this"));
+assertEquals(global, call(globalStrictArrowEval, object, "this"));
+assertEquals(global, call(globalSloppyArrowEval, object, "(()=>this)()"));
+assertEquals(global, call(globalStrictArrowEval, object, "(()=>this)()"));
+
+assertEquals(object, call(sloppyFunctionArrow, object));
+assertEquals(global, call(sloppyFunctionArrow, undefined));
+assertEquals(object, call(strictFunctionArrow, object));
+assertEquals(undefined, call(strictFunctionArrow, undefined));
+
+assertEquals(object, call(sloppyFunctionEvalArrow, object));
+assertEquals(global, call(sloppyFunctionEvalArrow, undefined));
+assertEquals(object, call(strictFunctionEvalArrow, object));
+assertEquals(undefined, call(strictFunctionEvalArrow, undefined));
+
+assertEquals(object, call(sloppyFunctionArrowEval, object, "this"));
+assertEquals(global, call(sloppyFunctionArrowEval, undefined, "this"));
+// TODO(wingo): Scoping of "this" appears to be fine, but oddly the "s"
+// in function(s) { return ()=>eval(s) } doesn't resolve to the "s"
+// parameter in strict mode. Re-enable these tests once that bug is
+// fixed.
+//assertEquals(object, call(strictFunctionArrowEval, object, "this"));
+//assertEquals(undefined, call(strictFunctionArrowEval, undefined, "this"));
+
+assertEquals(object,
+ call(sloppyFunctionArrowEval, object, "(()=>this)()"));
+assertEquals(global,
+ call(sloppyFunctionArrowEval, undefined, "(()=>this)()"));
+// TODO(wingo): Same as above.
+//assertEquals(object,
+// call(strictFunctionArrowEval, object, "(()=>this)()"));
+//assertEquals(undefined,
+// call(strictFunctionArrowEval, undefined, "(()=>this)()"));
+
+assertEquals(global, call(arrowInsideWith, undefined));
+assertEquals(global, call(arrowInsideWith, object));
+assertEquals(global, call(arrowInsideWithEval, undefined, "this"));
+assertEquals(global, call(arrowInsideWithEval, object, "this"));
+assertEquals(global, call(arrowInsideWithEval, undefined, "(()=>this)()"));
+assertEquals(global, call(arrowInsideWithEval, object, "(()=>this)()"));
« src/x64/full-codegen-x64.cc ('K') | « test/mjsunit/debug-scopes.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698