js/src/tests/ecma_3/Function/call-001.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 * Date: 2001-07-13
michael@0 8 *
michael@0 9 * SUMMARY: Applying Function.prototype.call to the Function object itself
michael@0 10 *
michael@0 11 *
michael@0 12 * ECMA-262 15.3.4.4 Function.prototype.call (thisArg [,arg1 [,arg2,…] ] )
michael@0 13 *
michael@0 14 * When applied to the Function object itself, thisArg should be ignored.
michael@0 15 * As explained by Waldemar (waldemar@netscape.com):
michael@0 16 *
michael@0 17 * Function.call(obj, "print(this)") is equivalent to invoking
michael@0 18 * Function("print(this)") with this set to obj. Now, Function("print(this)")
michael@0 19 * is equivalent to new Function("print(this)") (see 15.3.1.1), and the latter
michael@0 20 * ignores the this value that you passed it and constructs a function
michael@0 21 * (which we'll call F) which will print the value of the this that will be
michael@0 22 * passed in when F will be invoked.
michael@0 23 *
michael@0 24 * With the last set of () you're invoking F(), which means you're calling it
michael@0 25 * with no this value. When you don't provide a this value, it defaults to the
michael@0 26 * global object.
michael@0 27 *
michael@0 28 */
michael@0 29
michael@0 30 //-----------------------------------------------------------------------------
michael@0 31 var UBound = 0;
michael@0 32 var BUGNUMBER = '(none)';
michael@0 33 var summary = 'Applying Function.prototype.call to the Function object itself';
michael@0 34 var status = '';
michael@0 35 var statusitems = [];
michael@0 36 var actual = '';
michael@0 37 var actualvalues = [];
michael@0 38 var expect= '';
michael@0 39 var expectedvalues = [];
michael@0 40 var self = this; // capture a reference to the global object
michael@0 41 var cnOBJECT_GLOBAL = self.toString();
michael@0 42 var cnOBJECT_OBJECT = (new Object).toString();
michael@0 43 var cnHello = 'Hello';
michael@0 44 var cnRed = 'red';
michael@0 45 var objTEST = {color:cnRed};
michael@0 46 var f = new Function();
michael@0 47 var g = new Function();
michael@0 48
michael@0 49
michael@0 50 f = Function.call(self, 'return cnHello');
michael@0 51 g = Function.call(objTEST, 'return cnHello');
michael@0 52
michael@0 53 status = 'Section A of test';
michael@0 54 actual = f();
michael@0 55 expect = cnHello;
michael@0 56 captureThis();
michael@0 57
michael@0 58 status = 'Section B of test';
michael@0 59 actual = g();
michael@0 60 expect = cnHello;
michael@0 61 captureThis();
michael@0 62
michael@0 63
michael@0 64 f = Function.call(self, 'return this.toString()');
michael@0 65 g = Function.call(objTEST, 'return this.toString()');
michael@0 66
michael@0 67 status = 'Section C of test';
michael@0 68 actual = f();
michael@0 69 expect = cnOBJECT_GLOBAL;
michael@0 70 captureThis();
michael@0 71
michael@0 72 status = 'Section D of test';
michael@0 73 actual = g();
michael@0 74 expect = cnOBJECT_GLOBAL;
michael@0 75 captureThis();
michael@0 76
michael@0 77
michael@0 78 f = Function.call(self, 'return this.color');
michael@0 79 g = Function.call(objTEST, 'return this.color');
michael@0 80
michael@0 81 status = 'Section E of test';
michael@0 82 actual = f();
michael@0 83 expect = undefined;
michael@0 84 captureThis();
michael@0 85
michael@0 86 status = 'Section F of test';
michael@0 87 actual = g();
michael@0 88 expect = undefined;
michael@0 89 captureThis();
michael@0 90
michael@0 91
michael@0 92
michael@0 93 //-----------------------------------------------------------------------------
michael@0 94 test();
michael@0 95 //-----------------------------------------------------------------------------
michael@0 96
michael@0 97
michael@0 98 function captureThis()
michael@0 99 {
michael@0 100 statusitems[UBound] = status;
michael@0 101 actualvalues[UBound] = actual;
michael@0 102 expectedvalues[UBound] = expect;
michael@0 103 UBound++;
michael@0 104 }
michael@0 105
michael@0 106
michael@0 107 function test()
michael@0 108 {
michael@0 109 enterFunc ('test');
michael@0 110 printBugNumber(BUGNUMBER);
michael@0 111 printStatus (summary);
michael@0 112
michael@0 113 for (var i = 0; i < UBound; i++)
michael@0 114 {
michael@0 115 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
michael@0 116 }
michael@0 117
michael@0 118 exitFunc ('test');
michael@0 119 }

mercurial