js/src/tests/ecma_5/Global/cross-global-implicit-this.js

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

michael@0 1 // |reftest| skip-if(!xulRuntime.shell)
michael@0 2 // Any copyright is dedicated to the Public Domain.
michael@0 3 // http://creativecommons.org/licenses/publicdomain/
michael@0 4
michael@0 5 //-----------------------------------------------------------------------------
michael@0 6 var BUGNUMBER = 671947;
michael@0 7 var summary = "Unqualified function invocation uses the global object of the called property as |this|";
michael@0 8 var actual = "------------------------";
michael@0 9 var expect = "ooaoboabuuaubuabooaoboab";
michael@0 10
michael@0 11 print(BUGNUMBER + ": " + summary);
michael@0 12
michael@0 13 /**************
michael@0 14 * BEGIN TEST *
michael@0 15 **************/
michael@0 16
michael@0 17 this.name = "o";
michael@0 18
michael@0 19 function f() {
michael@0 20 return this ? this.name : "t";
michael@0 21 }
michael@0 22 function g() {
michael@0 23 "use strict";
michael@0 24 return this ? this.name : "u";
michael@0 25 }
michael@0 26 function h() {
michael@0 27 return this ? this.name : "v";
michael@0 28 }
michael@0 29
michael@0 30 var sb = newGlobal('same-compartment');
michael@0 31 sb.parent = this;
michael@0 32
michael@0 33 evalcx('\n' +
michael@0 34 ' this.name="i";\n' +
michael@0 35 ' this.f = parent.f;\n' +
michael@0 36 ' this.g = parent.g;\n' +
michael@0 37 ' this.a = { name:"a", f:parent.f, g:parent.g };\n' +
michael@0 38 ' this.b = { name:"b", f:parent.f, g:parent.g };\n' +
michael@0 39 ' Object.defineProperty(this, "h", { get: (function(){ return parent.h; })});\n' +
michael@0 40 ' Object.defineProperty(a, "h", { get: (function(){ return parent.h; })});\n' +
michael@0 41 ' Object.defineProperty(b, "h", { get: (function(){ return parent.h; })});\n' +
michael@0 42
michael@0 43 ' var results = "";\n' +
michael@0 44
michael@0 45 ' /* Three of the first four cases pass undefined (promoted inside the callee to the callee\'s global object). */\n' +
michael@0 46 ' /* a.f() is the one exception, which passes the base, a, as the this object. */\n' +
michael@0 47 ' results += (function(){return f();})();\n' +
michael@0 48 ' results += (function(){return (1,f)();})();\n' +
michael@0 49 ' results += (function(){return a.f();})();\n' +
michael@0 50 ' results += (function(){return eval("f()");})();\n' +
michael@0 51 ' /* Same cases as above, but wrapped in a with. The first & last of these cases pass b, */\n' +
michael@0 52 ' /* the object scoped by the with, as the this value. */\n' +
michael@0 53 ' /* a.f() still passes the explicit base, a. (1,f)() is a little tricksier - this passes */\n' +
michael@0 54 ' /* undefined (promoted to the callee global object) since the comma operator calles GetValue */\n' +
michael@0 55 ' /* on the reference (see ES5 11.14.) */\n' +
michael@0 56 ' results += (function(){with(b){ return (function(){ return f();})(); }})();\n' +
michael@0 57 ' results += (function(){with(b){ return (function(){ return (1,f)();})(); }})();\n' +
michael@0 58 ' results += (function(){with(b){ return (function(){ return a.f();})(); }})();\n' +
michael@0 59 ' results += (function(){with(b){ return (function(){ return eval("f()");})(); }})();\n' +
michael@0 60
michael@0 61 ' /* Same tests as above, but with a strict callee. */\n' +
michael@0 62 ' /* We expect the same results, except undefined this is not replaced with the global object. */\n' +
michael@0 63 ' results += (function(){return g();})();\n' +
michael@0 64 ' results += (function(){return (1,g)();})();\n' +
michael@0 65 ' results += (function(){return a.g();})();\n' +
michael@0 66 ' results += (function(){return eval("g()");})();\n' +
michael@0 67 ' results += (function(){with(b){ return g(); }})();\n' +
michael@0 68 ' results += (function(){with(b){ return (1,g)(); }})();\n' +
michael@0 69 ' results += (function(){with(b){ return a.g(); }})();\n' +
michael@0 70 ' results += (function(){with(b){ return (function(){ return eval("g()");})(); }})();\n' +
michael@0 71
michael@0 72 ' /* Same as the first set, but h is a getter property. */\n' +
michael@0 73 ' results += (function(){return h();})();\n' +
michael@0 74 ' results += (function(){return (1,h)();})();\n' +
michael@0 75 ' results += (function(){return a.h();})();\n' +
michael@0 76 ' results += (function(){return eval("h()");})();\n' +
michael@0 77 ' results += (function(){with(b){ return h(); }})();\n' +
michael@0 78 ' results += (function(){with(b){ return (1,h)(); }})();\n' +
michael@0 79 ' results += (function(){with(b){ return a.h(); }})();\n' +
michael@0 80 ' results += (function(){with(b){ return (function(){ return eval("h()");})(); }})();\n' +
michael@0 81
michael@0 82 ' parent.actual = results;\n' +
michael@0 83 '',
michael@0 84 sb);
michael@0 85
michael@0 86 reportCompare(expect, actual, "ok");

mercurial