michael@0: /* michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/licenses/publicdomain/ michael@0: * Contributor: michael@0: * Jeff Walden michael@0: */ michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = 575535; michael@0: var summary = 'Function.prototype.call'; michael@0: print(BUGNUMBER + ": " + summary); michael@0: michael@0: /************** michael@0: * BEGIN TEST * michael@0: **************/ michael@0: michael@0: function expectTypeError(fun, msg) michael@0: { michael@0: try michael@0: { michael@0: fun(); michael@0: assertEq(true, false, "should have thrown a TypeError"); michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, msg + "; instead threw " + e); michael@0: } michael@0: } michael@0: michael@0: function fun() { } michael@0: michael@0: var global = this; michael@0: michael@0: assertEq(Function.prototype.call.length, 1); michael@0: michael@0: michael@0: /* Step 1. */ michael@0: var nonfuns = [null, 1, -1, 2.5, "[[Call]]", undefined, true, false, {}]; michael@0: for (var i = 0, sz = nonfuns.length; i < sz; i++) michael@0: { michael@0: var f = function() michael@0: { michael@0: Function.prototype.call.apply(nonfuns[i]); michael@0: }; michael@0: var msg = michael@0: "expected TypeError calling Function.prototype.call with uncallable this"; michael@0: expectTypeError(f, msg); michael@0: } michael@0: michael@0: michael@0: /* Steps 2-4. */ michael@0: function none() michael@0: { michael@0: assertEq(this, global, "bad this"); michael@0: assertEq(arguments.length, 0, "wrong arguments"); michael@0: } michael@0: michael@0: none.call(); michael@0: none.call(undefined); michael@0: none.call(null); michael@0: michael@0: var seenThis; michael@0: function strictNone() michael@0: { michael@0: "use strict"; michael@0: assertEq(this, seenThis, "bad this"); michael@0: assertEq(arguments.length, 0, "wrong arguments"); michael@0: } michael@0: michael@0: seenThis = undefined; michael@0: strictNone.call(); michael@0: strictNone.call(undefined); michael@0: michael@0: seenThis = null; michael@0: strictNone.call(null); michael@0: michael@0: seenThis = 17; michael@0: strictNone.call(17); michael@0: michael@0: var seenThisBox, args; michael@0: function some() michael@0: { michael@0: assertEq(this instanceof seenThisBox, true, michael@0: "this not instanceof " + seenThisBox); michael@0: assertEq(this.valueOf(), seenThis, michael@0: "wrong this valueOf()"); michael@0: assertEq(arguments.length, args.length, "wrong arguments count"); michael@0: for (var i = 0; i < args.length; i++) michael@0: assertEq(arguments[i], args[i], "wrong argument " + i); michael@0: } michael@0: michael@0: seenThis = false; michael@0: seenThisBox = Boolean; michael@0: args = [8, 6, 7, NaN, undefined, 0.3]; michael@0: some.call(false, 8, 6, 7, NaN, undefined, 0.3); michael@0: michael@0: var obj = {}; michael@0: michael@0: seenThis = "foo"; michael@0: seenThisBox = String; michael@0: args = [obj]; michael@0: some.call("foo", obj); michael@0: michael@0: seenThis = obj; michael@0: seenThisBox = Object; michael@0: some.call(obj, obj); michael@0: michael@0: function strictSome() michael@0: { michael@0: "use strict"; michael@0: assertEq(this, seenThis, "wrong this"); michael@0: assertEq(arguments.length, args.length, "wrong arguments count"); michael@0: for (var i = 0; i < args.length; i++) michael@0: assertEq(arguments[i], args[i], "wrong argument " + i); michael@0: } michael@0: michael@0: seenThis = NaN; michael@0: args = [8, 6, 7, NaN, undefined, 0.3]; michael@0: strictSome.call(NaN, 8, 6, 7, NaN, undefined, 0.3); michael@0: michael@0: seenThis = "foo"; michael@0: args = [obj]; michael@0: strictSome.call("foo", obj); michael@0: michael@0: seenThis = obj; michael@0: strictSome.call(obj, obj); michael@0: michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: if (typeof reportCompare === "function") michael@0: reportCompare(true, true); michael@0: michael@0: print("All tests passed!");