michael@0: /* michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/licenses/publicdomain/ michael@0: */ michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = 619283; michael@0: var summary = michael@0: "ECMAScript built-in methods that immediately throw when |this| is " + michael@0: "|undefined| or |null| (due to CheckObjectCoercible, ToObject, or ToString)"; michael@0: michael@0: print(BUGNUMBER + ": " + summary); michael@0: michael@0: /************** michael@0: * BEGIN TEST * michael@0: **************/ michael@0: michael@0: // This test fills out for the non-standard methods which michael@0: // ecma_5/misc/builtin-methods-reject-null-undefined-this.js declines to test. michael@0: michael@0: var ClassToMethodMap = michael@0: { michael@0: Object: [/* michael@0: * Don't box this just yet for these methods -- they're used too michael@0: * much without qualification to do that. :-( michael@0: */ michael@0: /* "__defineGetter__", "__defineSetter__", */ michael@0: "__lookupGetter__", "__lookupSetter__", "watch", "unwatch", michael@0: "toSource"], michael@0: Function: ["toSource"], michael@0: Array: ["toSource"], michael@0: String: ["toSource", "quote", "bold", "italics", "fixed", "fontsize", michael@0: "fontcolor", "link", "anchor", "strike", "small", "big", "blink", michael@0: "sup", "sub", "substr", "trimLeft", "trimRight", "toJSON"], michael@0: Boolean: ["toSource", "toJSON"], michael@0: Number: ["toSource", "toJSON"], michael@0: Date: ["toSource", "toLocaleFormat", "getYear", "setYear", michael@0: "toGMTString"], michael@0: RegExp: ["toSource"], michael@0: Error: ["toSource"], michael@0: }; michael@0: michael@0: var badThisValues = [null, undefined]; michael@0: michael@0: function testMethod(Class, className, method) michael@0: { michael@0: var expr; michael@0: michael@0: // Try out explicit this values michael@0: for (var i = 0, sz = badThisValues.length; i < sz; i++) michael@0: { michael@0: var badThis = badThisValues[i]; michael@0: michael@0: expr = className + ".prototype." + method + ".call(" + badThis + ")"; michael@0: try michael@0: { michael@0: Class.prototype[method].call(badThis); michael@0: throw new Error(expr + " didn't throw a TypeError"); michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, michael@0: "wrong error for " + expr + ", instead threw " + e); michael@0: } michael@0: michael@0: expr = className + ".prototype." + method + ".apply(" + badThis + ")"; michael@0: try michael@0: { michael@0: Class.prototype[method].apply(badThis); michael@0: throw new Error(expr + " didn't throw a TypeError"); michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, michael@0: "wrong error for " + expr + ", instead threw " + e); michael@0: } michael@0: } michael@0: michael@0: // ..and for good measure.. michael@0: michael@0: expr = "(0, " + className + ".prototype." + method + ")()" michael@0: try michael@0: { michael@0: // comma operator to call GetValue() on the method and de-Reference it michael@0: (0, Class.prototype[method])(); michael@0: throw new Error(expr + " didn't throw a TypeError"); michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, michael@0: "wrong error for " + expr + ", instead threw " + e); michael@0: } michael@0: } michael@0: michael@0: for (var className in ClassToMethodMap) michael@0: { michael@0: var Class = this[className]; michael@0: michael@0: var methodNames = ClassToMethodMap[className]; michael@0: for (var i = 0, sz = methodNames.length; i < sz; i++) michael@0: { michael@0: var method = methodNames[i]; michael@0: testMethod(Class, className, method); michael@0: } 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!");