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: var BUGNUMBER = 501739; michael@0: var summary = michael@0: "String.prototype.replace should throw when called with a global RegExp " + michael@0: "whose .lastIndex is non-writable"; michael@0: michael@0: print(BUGNUMBER + ": " + summary); michael@0: michael@0: /************** michael@0: * BEGIN TEST * michael@0: **************/ michael@0: michael@0: var s = '0x2x4x6x8'; michael@0: michael@0: // First time with .lastIndex === 0, replacing to '' michael@0: michael@0: var p1 = /x/g; michael@0: Object.defineProperty(p1, "lastIndex", { writable: false }); michael@0: michael@0: try michael@0: { michael@0: s.replace(p1, ''); michael@0: throw "didn't throw"; michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, michael@0: "should have thrown a TypeError, instead got: " + e); michael@0: assertEq(p1.lastIndex, 0); michael@0: } michael@0: michael@0: // Second time with .lastIndex !== 0, replacing to '' michael@0: michael@0: var p2 = /x/g; michael@0: Object.defineProperty(p2, "lastIndex", { writable: false, value: 3 }); michael@0: michael@0: try michael@0: { michael@0: s.replace(p2, ''); michael@0: throw "didn't throw"; michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, michael@0: "should have thrown a TypeError, instead got: " + e); michael@0: assertEq(p2.lastIndex, 3); michael@0: } michael@0: michael@0: // Third time with .lastIndex === 0, replacing to 'y' michael@0: michael@0: var p3 = /x/g; michael@0: Object.defineProperty(p3, "lastIndex", { writable: false }); michael@0: michael@0: try michael@0: { michael@0: s.replace(p3, 'y'); michael@0: throw "didn't throw"; michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, michael@0: "should have thrown a TypeError, instead got: " + e); michael@0: assertEq(p3.lastIndex, 0); michael@0: } michael@0: michael@0: // Fourth time with .lastIndex !== 0, replacing to 'y' michael@0: michael@0: var p4 = /x/g; michael@0: Object.defineProperty(p4, "lastIndex", { writable: false, value: 3 }); michael@0: michael@0: try michael@0: { michael@0: s.replace(p4, ''); michael@0: throw "didn't throw"; michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, michael@0: "should have thrown a TypeError, instead got: " + e); michael@0: assertEq(p4.lastIndex, 3); michael@0: } michael@0: michael@0: // Fifth time with .lastIndex === 0, replacing to 'y', but no match michael@0: michael@0: var p5 = /q/g; michael@0: Object.defineProperty(p5, "lastIndex", { writable: false }); michael@0: michael@0: try michael@0: { michael@0: s.replace(p5, 'y'); michael@0: throw "didn't throw"; michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, michael@0: "should have thrown a TypeError, instead got: " + e); michael@0: assertEq(p5.lastIndex, 0); michael@0: } michael@0: michael@0: // Sixth time with .lastIndex !== 0, replacing to 'y', but no match michael@0: michael@0: var p6 = /q/g; michael@0: Object.defineProperty(p6, "lastIndex", { writable: false, value: 3 }); michael@0: michael@0: try michael@0: { michael@0: s.replace(p6, ''); michael@0: throw "didn't throw"; michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, michael@0: "should have thrown a TypeError, instead got: " + e); michael@0: assertEq(p6.lastIndex, 3); michael@0: } michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: if (typeof reportCompare === "function") michael@0: reportCompare(true, true); michael@0: michael@0: print("Tests complete");