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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma_5/Global/cross-global-implicit-this.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,86 @@
     1.4 +// |reftest| skip-if(!xulRuntime.shell)
     1.5 +// Any copyright is dedicated to the Public Domain.
     1.6 +// http://creativecommons.org/licenses/publicdomain/
     1.7 +
     1.8 +//-----------------------------------------------------------------------------
     1.9 +var BUGNUMBER = 671947;
    1.10 +var summary = "Unqualified function invocation uses the global object of the called property as |this|";
    1.11 +var actual = "------------------------";
    1.12 +var expect = "ooaoboabuuaubuabooaoboab";
    1.13 +
    1.14 +print(BUGNUMBER + ": " + summary);
    1.15 +
    1.16 +/**************
    1.17 + * BEGIN TEST *
    1.18 + **************/
    1.19 +
    1.20 +this.name = "o";
    1.21 +
    1.22 +function f() {
    1.23 +  return this ? this.name : "t";
    1.24 +}
    1.25 +function g() {
    1.26 +  "use strict";
    1.27 +  return this ? this.name : "u";
    1.28 +}
    1.29 +function h() {
    1.30 +  return this ? this.name : "v";
    1.31 +}
    1.32 +
    1.33 +var sb = newGlobal('same-compartment');
    1.34 +sb.parent = this;
    1.35 +
    1.36 +evalcx('\n' +
    1.37 +       ' this.name="i";\n' +
    1.38 +       ' this.f = parent.f;\n' +
    1.39 +       ' this.g = parent.g;\n' +
    1.40 +       ' this.a = { name:"a", f:parent.f, g:parent.g };\n' +
    1.41 +       ' this.b = { name:"b", f:parent.f, g:parent.g };\n' +
    1.42 +       ' Object.defineProperty(this, "h", { get: (function(){ return parent.h; })});\n' +
    1.43 +       ' Object.defineProperty(a, "h", { get: (function(){ return parent.h; })});\n' +
    1.44 +       ' Object.defineProperty(b, "h", { get: (function(){ return parent.h; })});\n' +
    1.45 +
    1.46 +       ' var results = "";\n' +
    1.47 +
    1.48 +       ' /* Three of the first four cases pass undefined (promoted inside the callee to the callee\'s global object). */\n' +
    1.49 +       ' /* a.f() is the one exception, which passes the base, a, as the this object. */\n' +
    1.50 +       ' results += (function(){return f();})();\n' +
    1.51 +       ' results += (function(){return (1,f)();})();\n' +
    1.52 +       ' results += (function(){return a.f();})();\n' +
    1.53 +       ' results += (function(){return eval("f()");})();\n' +
    1.54 +       ' /* Same cases as above, but wrapped in a with. The first & last of these cases pass b, */\n' +
    1.55 +       ' /* the object scoped by the with, as the this value. */\n' +
    1.56 +       ' /* a.f() still passes the explicit base, a. (1,f)() is a little tricksier - this passes */\n' +
    1.57 +       ' /* undefined (promoted to the callee global object) since the comma operator calles GetValue */\n' +
    1.58 +       ' /* on the reference (see ES5 11.14.) */\n' +
    1.59 +       ' results += (function(){with(b){ return (function(){ return f();})(); }})();\n' +
    1.60 +       ' results += (function(){with(b){ return (function(){ return (1,f)();})(); }})();\n' +
    1.61 +       ' results += (function(){with(b){ return (function(){ return a.f();})(); }})();\n' +
    1.62 +       ' results += (function(){with(b){ return (function(){ return eval("f()");})(); }})();\n' +
    1.63 +
    1.64 +       ' /* Same tests as above, but with a strict callee. */\n' +
    1.65 +       ' /* We expect the same results, except undefined this is not replaced with the global object. */\n' +
    1.66 +       ' results += (function(){return g();})();\n' +
    1.67 +       ' results += (function(){return (1,g)();})();\n' +
    1.68 +       ' results += (function(){return a.g();})();\n' +
    1.69 +       ' results += (function(){return eval("g()");})();\n' +
    1.70 +       ' results += (function(){with(b){ return g(); }})();\n' +
    1.71 +       ' results += (function(){with(b){ return (1,g)(); }})();\n' +
    1.72 +       ' results += (function(){with(b){ return a.g(); }})();\n' +
    1.73 +       ' results += (function(){with(b){ return (function(){ return eval("g()");})(); }})();\n' +
    1.74 +
    1.75 +       ' /* Same as the first set, but h is a getter property. */\n' +
    1.76 +       ' results += (function(){return h();})();\n' +
    1.77 +       ' results += (function(){return (1,h)();})();\n' +
    1.78 +       ' results += (function(){return a.h();})();\n' +
    1.79 +       ' results += (function(){return eval("h()");})();\n' +
    1.80 +       ' results += (function(){with(b){ return h(); }})();\n' +
    1.81 +       ' results += (function(){with(b){ return (1,h)(); }})();\n' +
    1.82 +       ' results += (function(){with(b){ return a.h(); }})();\n' +
    1.83 +       ' results += (function(){with(b){ return (function(){ return eval("h()");})(); }})();\n' +
    1.84 +
    1.85 +       ' parent.actual = results;\n' +
    1.86 +       '',
    1.87 +       sb);
    1.88 +
    1.89 +reportCompare(expect, actual, "ok");

mercurial