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
15var r = new RegExp('a', 'gimuy');
16assert (r.flags === 'gimuy');
17assert (r.toString() === '/a/gimuy');
18
19try {
20  Object.getOwnPropertyDescriptor(RegExp.prototype, 'flags').get.call(42);
21  assert (false);
22} catch (e) {
23  assert (e instanceof TypeError);
24}
25
26var o = {
27  global: true,
28  unicode: true,
29  sticky: true,
30  source: "str"
31}
32
33Object.defineProperty(o, 'flags', Object.getOwnPropertyDescriptor(RegExp.prototype, 'flags'));
34assert(o.flags === "guy");
35assert (RegExp.prototype.toString.call (o) === "/str/guy");
36
37Object.defineProperty(o, 'multiline', { 'get': function () {throw "abrupt flag get"; }});
38try {
39  o.flags
40  assert (false);
41} catch (e) {
42  assert (e === "abrupt flag get");
43}
44
45try {
46  RegExp.prototype.toString.call(42);
47  assert (false);
48} catch (e) {
49  assert (e instanceof TypeError);
50}
51
52assert (RegExp.prototype.toString.call({}) === "/undefined/undefined");
53
54var o = {};
55Object.defineProperty (o, 'source', { 'get' : function () {throw "abrupt source get"; } });
56try {
57  RegExp.prototype.toString.call(o);
58  assert (false);
59} catch (e) {
60  assert (e === "abrupt source get");
61}
62
63var o = {source: {toString: function() {throw "abrupt source toString";}}};
64try {
65  RegExp.prototype.toString.call(o);
66  assert (false);
67} catch (e) {
68  assert (e === "abrupt source toString");
69}
70
71var o = {source: "str"};
72Object.defineProperty (o, 'flags', { 'get' : function () {throw "abrupt flags get"; } });
73try {
74  RegExp.prototype.toString.call(o);
75  assert (false);
76} catch (e) {
77  assert (e === "abrupt flags get");
78}
79
80var o = {source: "str", flags: {toString: function() {throw "abrupt flags toString";}}};
81try {
82  RegExp.prototype.toString.call(o);
83  assert (false);
84} catch (e) {
85  assert (e === "abrupt flags toString");
86}
87
88var o = {
89  global: true,
90  source: "str"
91}
92
93Object.defineProperty(o, 'unicode', { 'get': function () {throw "abrupt unicode get"; }});
94try {
95  RegExp.prototype[Symbol.match].call(o, "str");
96  assert (false);
97} catch (e) {
98  assert (e === "abrupt unicode get");
99}
100
101assert ("str�fgh".replace(/(?:)/gu, "x") === 'xsxtxrx�xfxgxhx');
102assert ("str�fgh".replace(/(?:)/g, "x") === 'xsxtxrx\ud803x\udca1xfxgxhx');
103
104r = /(?:)/gu;
105/* Disable fast path. */
106r.exec = function (s) { return RegExp.prototype.exec.call(this, s); };
107
108assert ("str�fgh".replace(r, "x") === 'xsxtxrx�xfxgxhx');
109Object.defineProperty(r, 'unicode', {value: false});
110assert ("str�fgh".replace(r, "x") === 'xsxtxrx\ud803x\udca1xfxgxhx');
111
112r = /(?:)/gu;
113assert (RegExp.prototype[Symbol.match].call(r, "str�fgh").length === 8);
114Object.defineProperty(r, 'unicode', {value: false});
115assert (RegExp.prototype[Symbol.match].call(r, "str�fgh").length === 9);
116
117r = /(?:)/gy;
118r.lastIndex = 2;
119assert ("asd".replace(r, "x") === "xaxsxdx");
120assert (r.lastIndex === 0);
121
122r.lastIndex = 5;
123assert ("asd".replace(r, "x") === "xaxsxdx");
124assert (r.lastIndex === 0);
125
126r = /(?:)/y;
127r.lastIndex = 2;
128assert ("asd".replace(r, "x") === "asxd");
129assert (r.lastIndex === 2);
130
131r.lastIndex = 5;
132assert ("asd".replace(r, "x") === "asd");
133assert (r.lastIndex === 0);
134
135r.lastIndex = 2;
136/* Disable fast path. */
137r.exec = function (s) { return RegExp.prototype.exec.call(this, s); };
138assert ("asd".replace(r, "x") === "asxd");
139assert (r.lastIndex === 2);
140
141r.lastIndex = 5;
142assert ("asd".replace(r, "x") === "asd");
143assert (r.lastIndex === 0);
144
145assert (RegExp.prototype[Symbol.match].call(/a/y, "aaa").length === 1);
146assert (RegExp.prototype[Symbol.match].call(/a/gy, "aaa").length === 3);
147