js/src/tests/ecma_5/String/replace-throws-nonwritable-lastIndex-global.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.

     1 /*
     2  * Any copyright is dedicated to the Public Domain.
     3  * http://creativecommons.org/licenses/publicdomain/
     4  */
     6 var BUGNUMBER = 501739;
     7 var summary =
     8   "String.prototype.replace should throw when called with a global RegExp " +
     9   "whose .lastIndex is non-writable";
    11 print(BUGNUMBER + ": " + summary);
    13 /**************
    14  * BEGIN TEST *
    15  **************/
    17 var s = '0x2x4x6x8';
    19 // First time with .lastIndex === 0, replacing to ''
    21 var p1 = /x/g;
    22 Object.defineProperty(p1, "lastIndex", { writable: false });
    24 try
    25 {
    26   s.replace(p1, '');
    27   throw "didn't throw";
    28 }
    29 catch (e)
    30 {
    31   assertEq(e instanceof TypeError, true,
    32            "should have thrown a TypeError, instead got: " + e);
    33   assertEq(p1.lastIndex, 0);
    34 }
    36 // Second time with .lastIndex !== 0, replacing to ''
    38 var p2 = /x/g;
    39 Object.defineProperty(p2, "lastIndex", { writable: false, value: 3 });
    41 try
    42 {
    43   s.replace(p2, '');
    44   throw "didn't throw";
    45 }
    46 catch (e)
    47 {
    48   assertEq(e instanceof TypeError, true,
    49            "should have thrown a TypeError, instead got: " + e);
    50   assertEq(p2.lastIndex, 3);
    51 }
    53 // Third time with .lastIndex === 0, replacing to 'y'
    55 var p3 = /x/g;
    56 Object.defineProperty(p3, "lastIndex", { writable: false });
    58 try
    59 {
    60   s.replace(p3, 'y');
    61   throw "didn't throw";
    62 }
    63 catch (e)
    64 {
    65   assertEq(e instanceof TypeError, true,
    66            "should have thrown a TypeError, instead got: " + e);
    67   assertEq(p3.lastIndex, 0);
    68 }
    70 // Fourth time with .lastIndex !== 0, replacing to 'y'
    72 var p4 = /x/g;
    73 Object.defineProperty(p4, "lastIndex", { writable: false, value: 3 });
    75 try
    76 {
    77   s.replace(p4, '');
    78   throw "didn't throw";
    79 }
    80 catch (e)
    81 {
    82   assertEq(e instanceof TypeError, true,
    83            "should have thrown a TypeError, instead got: " + e);
    84   assertEq(p4.lastIndex, 3);
    85 }
    87 // Fifth time with .lastIndex === 0, replacing to 'y', but no match
    89 var p5 = /q/g;
    90 Object.defineProperty(p5, "lastIndex", { writable: false });
    92 try
    93 {
    94   s.replace(p5, 'y');
    95   throw "didn't throw";
    96 }
    97 catch (e)
    98 {
    99   assertEq(e instanceof TypeError, true,
   100            "should have thrown a TypeError, instead got: " + e);
   101   assertEq(p5.lastIndex, 0);
   102 }
   104 // Sixth time with .lastIndex !== 0, replacing to 'y', but no match
   106 var p6 = /q/g;
   107 Object.defineProperty(p6, "lastIndex", { writable: false, value: 3 });
   109 try
   110 {
   111   s.replace(p6, '');
   112   throw "didn't throw";
   113 }
   114 catch (e)
   115 {
   116   assertEq(e instanceof TypeError, true,
   117            "should have thrown a TypeError, instead got: " + e);
   118   assertEq(p6.lastIndex, 3);
   119 }
   121 /******************************************************************************/
   123 if (typeof reportCompare === "function")
   124   reportCompare(true, true);
   126 print("Tests complete");

mercurial