js/src/tests/ecma_5/Function/function-call.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 /*
michael@0 2 * Any copyright is dedicated to the Public Domain.
michael@0 3 * http://creativecommons.org/licenses/publicdomain/
michael@0 4 * Contributor:
michael@0 5 * Jeff Walden <jwalden+code@mit.edu>
michael@0 6 */
michael@0 7
michael@0 8 //-----------------------------------------------------------------------------
michael@0 9 var BUGNUMBER = 575535;
michael@0 10 var summary = 'Function.prototype.call';
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 function expectTypeError(fun, msg)
michael@0 18 {
michael@0 19 try
michael@0 20 {
michael@0 21 fun();
michael@0 22 assertEq(true, false, "should have thrown a TypeError");
michael@0 23 }
michael@0 24 catch (e)
michael@0 25 {
michael@0 26 assertEq(e instanceof TypeError, true, msg + "; instead threw " + e);
michael@0 27 }
michael@0 28 }
michael@0 29
michael@0 30 function fun() { }
michael@0 31
michael@0 32 var global = this;
michael@0 33
michael@0 34 assertEq(Function.prototype.call.length, 1);
michael@0 35
michael@0 36
michael@0 37 /* Step 1. */
michael@0 38 var nonfuns = [null, 1, -1, 2.5, "[[Call]]", undefined, true, false, {}];
michael@0 39 for (var i = 0, sz = nonfuns.length; i < sz; i++)
michael@0 40 {
michael@0 41 var f = function()
michael@0 42 {
michael@0 43 Function.prototype.call.apply(nonfuns[i]);
michael@0 44 };
michael@0 45 var msg =
michael@0 46 "expected TypeError calling Function.prototype.call with uncallable this";
michael@0 47 expectTypeError(f, msg);
michael@0 48 }
michael@0 49
michael@0 50
michael@0 51 /* Steps 2-4. */
michael@0 52 function none()
michael@0 53 {
michael@0 54 assertEq(this, global, "bad this");
michael@0 55 assertEq(arguments.length, 0, "wrong arguments");
michael@0 56 }
michael@0 57
michael@0 58 none.call();
michael@0 59 none.call(undefined);
michael@0 60 none.call(null);
michael@0 61
michael@0 62 var seenThis;
michael@0 63 function strictNone()
michael@0 64 {
michael@0 65 "use strict";
michael@0 66 assertEq(this, seenThis, "bad this");
michael@0 67 assertEq(arguments.length, 0, "wrong arguments");
michael@0 68 }
michael@0 69
michael@0 70 seenThis = undefined;
michael@0 71 strictNone.call();
michael@0 72 strictNone.call(undefined);
michael@0 73
michael@0 74 seenThis = null;
michael@0 75 strictNone.call(null);
michael@0 76
michael@0 77 seenThis = 17;
michael@0 78 strictNone.call(17);
michael@0 79
michael@0 80 var seenThisBox, args;
michael@0 81 function some()
michael@0 82 {
michael@0 83 assertEq(this instanceof seenThisBox, true,
michael@0 84 "this not instanceof " + seenThisBox);
michael@0 85 assertEq(this.valueOf(), seenThis,
michael@0 86 "wrong this valueOf()");
michael@0 87 assertEq(arguments.length, args.length, "wrong arguments count");
michael@0 88 for (var i = 0; i < args.length; i++)
michael@0 89 assertEq(arguments[i], args[i], "wrong argument " + i);
michael@0 90 }
michael@0 91
michael@0 92 seenThis = false;
michael@0 93 seenThisBox = Boolean;
michael@0 94 args = [8, 6, 7, NaN, undefined, 0.3];
michael@0 95 some.call(false, 8, 6, 7, NaN, undefined, 0.3);
michael@0 96
michael@0 97 var obj = {};
michael@0 98
michael@0 99 seenThis = "foo";
michael@0 100 seenThisBox = String;
michael@0 101 args = [obj];
michael@0 102 some.call("foo", obj);
michael@0 103
michael@0 104 seenThis = obj;
michael@0 105 seenThisBox = Object;
michael@0 106 some.call(obj, obj);
michael@0 107
michael@0 108 function strictSome()
michael@0 109 {
michael@0 110 "use strict";
michael@0 111 assertEq(this, seenThis, "wrong this");
michael@0 112 assertEq(arguments.length, args.length, "wrong arguments count");
michael@0 113 for (var i = 0; i < args.length; i++)
michael@0 114 assertEq(arguments[i], args[i], "wrong argument " + i);
michael@0 115 }
michael@0 116
michael@0 117 seenThis = NaN;
michael@0 118 args = [8, 6, 7, NaN, undefined, 0.3];
michael@0 119 strictSome.call(NaN, 8, 6, 7, NaN, undefined, 0.3);
michael@0 120
michael@0 121 seenThis = "foo";
michael@0 122 args = [obj];
michael@0 123 strictSome.call("foo", obj);
michael@0 124
michael@0 125 seenThis = obj;
michael@0 126 strictSome.call(obj, obj);
michael@0 127
michael@0 128
michael@0 129 /******************************************************************************/
michael@0 130
michael@0 131 if (typeof reportCompare === "function")
michael@0 132 reportCompare(true, true);
michael@0 133
michael@0 134 print("All tests passed!");

mercurial