|
1 /* |
|
2 * Any copyright is dedicated to the Public Domain. |
|
3 * http://creativecommons.org/licenses/publicdomain/ |
|
4 */ |
|
5 |
|
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"; |
|
10 |
|
11 print(BUGNUMBER + ": " + summary); |
|
12 |
|
13 /************** |
|
14 * BEGIN TEST * |
|
15 **************/ |
|
16 |
|
17 var s = '0x2x4x6x8'; |
|
18 |
|
19 // First time with .lastIndex === 0, replacing to '' |
|
20 |
|
21 var p1 = /x/g; |
|
22 Object.defineProperty(p1, "lastIndex", { writable: false }); |
|
23 |
|
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 } |
|
35 |
|
36 // Second time with .lastIndex !== 0, replacing to '' |
|
37 |
|
38 var p2 = /x/g; |
|
39 Object.defineProperty(p2, "lastIndex", { writable: false, value: 3 }); |
|
40 |
|
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 } |
|
52 |
|
53 // Third time with .lastIndex === 0, replacing to 'y' |
|
54 |
|
55 var p3 = /x/g; |
|
56 Object.defineProperty(p3, "lastIndex", { writable: false }); |
|
57 |
|
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 } |
|
69 |
|
70 // Fourth time with .lastIndex !== 0, replacing to 'y' |
|
71 |
|
72 var p4 = /x/g; |
|
73 Object.defineProperty(p4, "lastIndex", { writable: false, value: 3 }); |
|
74 |
|
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 } |
|
86 |
|
87 // Fifth time with .lastIndex === 0, replacing to 'y', but no match |
|
88 |
|
89 var p5 = /q/g; |
|
90 Object.defineProperty(p5, "lastIndex", { writable: false }); |
|
91 |
|
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 } |
|
103 |
|
104 // Sixth time with .lastIndex !== 0, replacing to 'y', but no match |
|
105 |
|
106 var p6 = /q/g; |
|
107 Object.defineProperty(p6, "lastIndex", { writable: false, value: 3 }); |
|
108 |
|
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 } |
|
120 |
|
121 /******************************************************************************/ |
|
122 |
|
123 if (typeof reportCompare === "function") |
|
124 reportCompare(true, true); |
|
125 |
|
126 print("Tests complete"); |