1 // Copyright JS Foundation and other contributors, http://js.foundation 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 var r = new RegExp('a', 'gimuy'); 16 assert (r.flags === 'gimuy'); 17 assert (r.toString() === '/a/gimuy'); 18 19 try { 20 Object.getOwnPropertyDescriptor(RegExp.prototype, 'flags').get.call(42); 21 assert (false); 22 } catch (e) { 23 assert (e instanceof TypeError); 24 } 25 26 var o = { 27 global: true, 28 unicode: true, 29 sticky: true, 30 source: "str" 31 } 32 33 Object.defineProperty(o, 'flags', Object.getOwnPropertyDescriptor(RegExp.prototype, 'flags')); 34 assert(o.flags === "guy"); 35 assert (RegExp.prototype.toString.call (o) === "/str/guy"); 36 37 Object.defineProperty(o, 'multiline', { 'get': function () {throw "abrupt flag get"; }}); 38 try { 39 o.flags 40 assert (false); 41 } catch (e) { 42 assert (e === "abrupt flag get"); 43 } 44 45 try { 46 RegExp.prototype.toString.call(42); 47 assert (false); 48 } catch (e) { 49 assert (e instanceof TypeError); 50 } 51 52 assert (RegExp.prototype.toString.call({}) === "/undefined/undefined"); 53 54 var o = {}; 55 Object.defineProperty (o, 'source', { 'get' : function () {throw "abrupt source get"; } }); 56 try { 57 RegExp.prototype.toString.call(o); 58 assert (false); 59 } catch (e) { 60 assert (e === "abrupt source get"); 61 } 62 63 var o = {source: {toString: function() {throw "abrupt source toString";}}}; 64 try { 65 RegExp.prototype.toString.call(o); 66 assert (false); 67 } catch (e) { 68 assert (e === "abrupt source toString"); 69 } 70 71 var o = {source: "str"}; 72 Object.defineProperty (o, 'flags', { 'get' : function () {throw "abrupt flags get"; } }); 73 try { 74 RegExp.prototype.toString.call(o); 75 assert (false); 76 } catch (e) { 77 assert (e === "abrupt flags get"); 78 } 79 80 var o = {source: "str", flags: {toString: function() {throw "abrupt flags toString";}}}; 81 try { 82 RegExp.prototype.toString.call(o); 83 assert (false); 84 } catch (e) { 85 assert (e === "abrupt flags toString"); 86 } 87 88 var o = { 89 global: true, 90 source: "str" 91 } 92 93 Object.defineProperty(o, 'unicode', { 'get': function () {throw "abrupt unicode get"; }}); 94 try { 95 RegExp.prototype[Symbol.match].call(o, "str"); 96 assert (false); 97 } catch (e) { 98 assert (e === "abrupt unicode get"); 99 } 100 101 assert ("str��fgh".replace(/(?:)/gu, "x") === 'xsxtxrx��xfxgxhx'); 102 assert ("str��fgh".replace(/(?:)/g, "x") === 'xsxtxrx\ud803x\udca1xfxgxhx'); 103 104 r = /(?:)/gu; 105 /* Disable fast path. */ 106 r.exec = function (s) { return RegExp.prototype.exec.call(this, s); }; 107 108 assert ("str��fgh".replace(r, "x") === 'xsxtxrx��xfxgxhx'); 109 Object.defineProperty(r, 'unicode', {value: false}); 110 assert ("str��fgh".replace(r, "x") === 'xsxtxrx\ud803x\udca1xfxgxhx'); 111 112 r = /(?:)/gu; 113 assert (RegExp.prototype[Symbol.match].call(r, "str��fgh").length === 8); 114 Object.defineProperty(r, 'unicode', {value: false}); 115 assert (RegExp.prototype[Symbol.match].call(r, "str��fgh").length === 9); 116 117 r = /(?:)/gy; 118 r.lastIndex = 2; 119 assert ("asd".replace(r, "x") === "xaxsxdx"); 120 assert (r.lastIndex === 0); 121 122 r.lastIndex = 5; 123 assert ("asd".replace(r, "x") === "xaxsxdx"); 124 assert (r.lastIndex === 0); 125 126 r = /(?:)/y; 127 r.lastIndex = 2; 128 assert ("asd".replace(r, "x") === "asxd"); 129 assert (r.lastIndex === 2); 130 131 r.lastIndex = 5; 132 assert ("asd".replace(r, "x") === "asd"); 133 assert (r.lastIndex === 0); 134 135 r.lastIndex = 2; 136 /* Disable fast path. */ 137 r.exec = function (s) { return RegExp.prototype.exec.call(this, s); }; 138 assert ("asd".replace(r, "x") === "asxd"); 139 assert (r.lastIndex === 2); 140 141 r.lastIndex = 5; 142 assert ("asd".replace(r, "x") === "asd"); 143 assert (r.lastIndex === 0); 144 145 assert (RegExp.prototype[Symbol.match].call(/a/y, "aaa").length === 1); 146 assert (RegExp.prototype[Symbol.match].call(/a/gy, "aaa").length === 3); 147