Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 | */ |
michael@0 | 5 | |
michael@0 | 6 | //----------------------------------------------------------------------------- |
michael@0 | 7 | var BUGNUMBER = 619283; |
michael@0 | 8 | var summary = |
michael@0 | 9 | "ECMAScript built-in methods that immediately throw when |this| is " + |
michael@0 | 10 | "|undefined| or |null| (due to CheckObjectCoercible, ToObject, or ToString)"; |
michael@0 | 11 | |
michael@0 | 12 | print(BUGNUMBER + ": " + summary); |
michael@0 | 13 | |
michael@0 | 14 | /************** |
michael@0 | 15 | * BEGIN TEST * |
michael@0 | 16 | **************/ |
michael@0 | 17 | |
michael@0 | 18 | // This test fills out for the non-standard methods which |
michael@0 | 19 | // ecma_5/misc/builtin-methods-reject-null-undefined-this.js declines to test. |
michael@0 | 20 | |
michael@0 | 21 | var ClassToMethodMap = |
michael@0 | 22 | { |
michael@0 | 23 | Object: [/* |
michael@0 | 24 | * Don't box this just yet for these methods -- they're used too |
michael@0 | 25 | * much without qualification to do that. :-( |
michael@0 | 26 | */ |
michael@0 | 27 | /* "__defineGetter__", "__defineSetter__", */ |
michael@0 | 28 | "__lookupGetter__", "__lookupSetter__", "watch", "unwatch", |
michael@0 | 29 | "toSource"], |
michael@0 | 30 | Function: ["toSource"], |
michael@0 | 31 | Array: ["toSource"], |
michael@0 | 32 | String: ["toSource", "quote", "bold", "italics", "fixed", "fontsize", |
michael@0 | 33 | "fontcolor", "link", "anchor", "strike", "small", "big", "blink", |
michael@0 | 34 | "sup", "sub", "substr", "trimLeft", "trimRight", "toJSON"], |
michael@0 | 35 | Boolean: ["toSource", "toJSON"], |
michael@0 | 36 | Number: ["toSource", "toJSON"], |
michael@0 | 37 | Date: ["toSource", "toLocaleFormat", "getYear", "setYear", |
michael@0 | 38 | "toGMTString"], |
michael@0 | 39 | RegExp: ["toSource"], |
michael@0 | 40 | Error: ["toSource"], |
michael@0 | 41 | }; |
michael@0 | 42 | |
michael@0 | 43 | var badThisValues = [null, undefined]; |
michael@0 | 44 | |
michael@0 | 45 | function testMethod(Class, className, method) |
michael@0 | 46 | { |
michael@0 | 47 | var expr; |
michael@0 | 48 | |
michael@0 | 49 | // Try out explicit this values |
michael@0 | 50 | for (var i = 0, sz = badThisValues.length; i < sz; i++) |
michael@0 | 51 | { |
michael@0 | 52 | var badThis = badThisValues[i]; |
michael@0 | 53 | |
michael@0 | 54 | expr = className + ".prototype." + method + ".call(" + badThis + ")"; |
michael@0 | 55 | try |
michael@0 | 56 | { |
michael@0 | 57 | Class.prototype[method].call(badThis); |
michael@0 | 58 | throw new Error(expr + " didn't throw a TypeError"); |
michael@0 | 59 | } |
michael@0 | 60 | catch (e) |
michael@0 | 61 | { |
michael@0 | 62 | assertEq(e instanceof TypeError, true, |
michael@0 | 63 | "wrong error for " + expr + ", instead threw " + e); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | expr = className + ".prototype." + method + ".apply(" + badThis + ")"; |
michael@0 | 67 | try |
michael@0 | 68 | { |
michael@0 | 69 | Class.prototype[method].apply(badThis); |
michael@0 | 70 | throw new Error(expr + " didn't throw a TypeError"); |
michael@0 | 71 | } |
michael@0 | 72 | catch (e) |
michael@0 | 73 | { |
michael@0 | 74 | assertEq(e instanceof TypeError, true, |
michael@0 | 75 | "wrong error for " + expr + ", instead threw " + e); |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | // ..and for good measure.. |
michael@0 | 80 | |
michael@0 | 81 | expr = "(0, " + className + ".prototype." + method + ")()" |
michael@0 | 82 | try |
michael@0 | 83 | { |
michael@0 | 84 | // comma operator to call GetValue() on the method and de-Reference it |
michael@0 | 85 | (0, Class.prototype[method])(); |
michael@0 | 86 | throw new Error(expr + " didn't throw a TypeError"); |
michael@0 | 87 | } |
michael@0 | 88 | catch (e) |
michael@0 | 89 | { |
michael@0 | 90 | assertEq(e instanceof TypeError, true, |
michael@0 | 91 | "wrong error for " + expr + ", instead threw " + e); |
michael@0 | 92 | } |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | for (var className in ClassToMethodMap) |
michael@0 | 96 | { |
michael@0 | 97 | var Class = this[className]; |
michael@0 | 98 | |
michael@0 | 99 | var methodNames = ClassToMethodMap[className]; |
michael@0 | 100 | for (var i = 0, sz = methodNames.length; i < sz; i++) |
michael@0 | 101 | { |
michael@0 | 102 | var method = methodNames[i]; |
michael@0 | 103 | testMethod(Class, className, method); |
michael@0 | 104 | } |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | /******************************************************************************/ |
michael@0 | 108 | |
michael@0 | 109 | if (typeof reportCompare === "function") |
michael@0 | 110 | reportCompare(true, true); |
michael@0 | 111 | |
michael@0 | 112 | print("All tests passed!"); |