js/src/tests/ecma_5/extensions/extension-methods-reject-null-undefined-this.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial