1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_5/String/match-throws-nonwritable-lastIndex-global.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,88 @@ 1.4 +/* 1.5 + * Any copyright is dedicated to the Public Domain. 1.6 + * http://creativecommons.org/licenses/publicdomain/ 1.7 + */ 1.8 + 1.9 +var BUGNUMBER = 501739; 1.10 +var summary = 1.11 + "String.prototype.match should throw when called with a global RegExp " + 1.12 + "whose .lastIndex is non-writable"; 1.13 + 1.14 +print(BUGNUMBER + ": " + summary); 1.15 + 1.16 +/************** 1.17 + * BEGIN TEST * 1.18 + **************/ 1.19 + 1.20 +var s = '0x2x4x6x8'; 1.21 + 1.22 +// First time with .lastIndex === 0 1.23 + 1.24 +var p1 = /x/g; 1.25 +Object.defineProperty(p1, "lastIndex", { writable: false }); 1.26 + 1.27 +try 1.28 +{ 1.29 + s.match(p1); 1.30 + throw "didn't throw"; 1.31 +} 1.32 +catch (e) 1.33 +{ 1.34 + assertEq(e instanceof TypeError, true, 1.35 + "should have thrown a TypeError, instead got: " + e); 1.36 +} 1.37 + 1.38 +// Second time with .lastIndex !== 0 1.39 + 1.40 +var p2 = /x/g; 1.41 +Object.defineProperty(p2, "lastIndex", { writable: false, value: 3 }); 1.42 + 1.43 +try 1.44 +{ 1.45 + s.match(p2); 1.46 + throw "didn't throw"; 1.47 +} 1.48 +catch (e) 1.49 +{ 1.50 + assertEq(e instanceof TypeError, true, 1.51 + "should have thrown a TypeError, instead got: " + e); 1.52 +} 1.53 + 1.54 +// Third time with .lastIndex === 0, no matches 1.55 + 1.56 +var p3 = /q/g; 1.57 +Object.defineProperty(p3, "lastIndex", { writable: false }); 1.58 + 1.59 +try 1.60 +{ 1.61 + s.match(p3); 1.62 + throw "didn't throw"; 1.63 +} 1.64 +catch (e) 1.65 +{ 1.66 + assertEq(e instanceof TypeError, true, 1.67 + "should have thrown a TypeError, instead got: " + e); 1.68 +} 1.69 + 1.70 +// Fourth time with .lastIndex !== 0, no matches 1.71 + 1.72 +var p4 = /q/g; 1.73 +Object.defineProperty(p4, "lastIndex", { writable: false, value: 3 }); 1.74 + 1.75 +try 1.76 +{ 1.77 + s.match(p4); 1.78 + throw "didn't throw"; 1.79 +} 1.80 +catch (e) 1.81 +{ 1.82 + assertEq(e instanceof TypeError, true, 1.83 + "should have thrown a TypeError, instead got: " + e); 1.84 +} 1.85 + 1.86 +/******************************************************************************/ 1.87 + 1.88 +if (typeof reportCompare === "function") 1.89 + reportCompare(true, true); 1.90 + 1.91 +print("Tests complete");