regexp_test.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. package goja
  2. import (
  3. "testing"
  4. )
  5. func TestRegexp1(t *testing.T) {
  6. const SCRIPT = `
  7. var r = new RegExp("(['\"])(.*?)\\1");
  8. var m = r.exec("'test'");
  9. m !== null && m.length == 3 && m[2] === "test";
  10. `
  11. testScript1(SCRIPT, valueTrue, t)
  12. }
  13. func TestRegexp2(t *testing.T) {
  14. const SCRIPT = `
  15. var r = new RegExp("(['\"])(.*?)['\"]");
  16. var m = r.exec("'test'");
  17. m !== null && m.length == 3 && m[2] === "test";
  18. `
  19. testScript1(SCRIPT, valueTrue, t)
  20. }
  21. func TestRegexpLiteral(t *testing.T) {
  22. const SCRIPT = `
  23. var r = /(['\"])(.*?)\1/;
  24. var m = r.exec("'test'");
  25. m !== null && m.length == 3 && m[2] === "test";
  26. `
  27. testScript1(SCRIPT, valueTrue, t)
  28. }
  29. func TestRegexpRe2Unicode(t *testing.T) {
  30. const SCRIPT = `
  31. var r = /(тест)/i;
  32. var m = r.exec("'Тест'");
  33. m !== null && m.length == 2 && m[1] === "Тест";
  34. `
  35. testScript1(SCRIPT, valueTrue, t)
  36. }
  37. func TestRegexpRe2UnicodeTarget(t *testing.T) {
  38. const SCRIPT = `
  39. var r = /(['\"])(.*?)['\"]/i;
  40. var m = r.exec("'Тест'");
  41. m !== null && m.length == 3 && m[2] === "Тест";
  42. `
  43. testScript1(SCRIPT, valueTrue, t)
  44. }
  45. func TestRegexpRegexp2Unicode(t *testing.T) {
  46. const SCRIPT = `
  47. var r = /(['\"])(тест)\1/i;
  48. var m = r.exec("'Тест'");
  49. m !== null && m.length == 3 && m[2] === "Тест";
  50. `
  51. testScript1(SCRIPT, valueTrue, t)
  52. }
  53. func TestRegexpRegexp2UnicodeTarget(t *testing.T) {
  54. const SCRIPT = `
  55. var r = /(['\"])(.*?)\1/;
  56. var m = r.exec("'Тест'");
  57. m !== null && m.length == 3 && m[2] === "Тест";
  58. `
  59. testScript1(SCRIPT, valueTrue, t)
  60. }
  61. func TestRegexpRe2Whitespace(t *testing.T) {
  62. const SCRIPT = `
  63. "\u2000\u2001\u2002\u200b".replace(/\s+/g, "") === "\u200b";
  64. `
  65. testScript1(SCRIPT, valueTrue, t)
  66. }
  67. func TestRegexpRegexp2Whitespace(t *testing.T) {
  68. const SCRIPT = `
  69. "A\u2000\u2001\u2002A\u200b".replace(/(A)\s+\1/g, "") === "\u200b"
  70. `
  71. testScript1(SCRIPT, valueTrue, t)
  72. }
  73. func TestEmptyCharClassRe2(t *testing.T) {
  74. const SCRIPT = `
  75. /[]/.test("\u0000");
  76. `
  77. testScript1(SCRIPT, valueFalse, t)
  78. }
  79. func TestNegatedEmptyCharClassRe2(t *testing.T) {
  80. const SCRIPT = `
  81. /[^]/.test("\u0000");
  82. `
  83. testScript1(SCRIPT, valueTrue, t)
  84. }
  85. func TestEmptyCharClassRegexp2(t *testing.T) {
  86. const SCRIPT = `
  87. /([])\1/.test("\u0000\u0000");
  88. `
  89. testScript1(SCRIPT, valueFalse, t)
  90. }
  91. func TestRegexp2Negate(t *testing.T) {
  92. const SCRIPT = `
  93. /([\D1])\1/.test("aa");
  94. `
  95. testScript1(SCRIPT, valueTrue, t)
  96. }
  97. func TestAlternativeRe2(t *testing.T) {
  98. const SCRIPT = `
  99. /()|/.exec("") !== null;
  100. `
  101. testScript1(SCRIPT, valueTrue, t)
  102. }
  103. func TestRegexpReplaceGlobal(t *testing.T) {
  104. const SCRIPT = `
  105. "QBZPbage\ny_cynprubyqre".replace(/^\s*|\s*$/g, '')
  106. `
  107. testScript1(SCRIPT, asciiString("QBZPbage\ny_cynprubyqre"), t)
  108. }
  109. func TestRegexpNumCaptures(t *testing.T) {
  110. const SCRIPT = `
  111. "Fubpxjnir Synfu 9.0 e115".replace(/([a-zA-Z]|\s)+/, '')
  112. `
  113. testScript1(SCRIPT, asciiString("9.0 e115"), t)
  114. }
  115. func TestRegexpNumCaptures1(t *testing.T) {
  116. const SCRIPT = `
  117. "Fubpxjnir Sy\tfu 9.0 e115".replace(/^.*\s+(\S+\s+\S+$)/, '')
  118. `
  119. testScript1(SCRIPT, asciiString(""), t)
  120. }
  121. func TestRegexpSInClass(t *testing.T) {
  122. const SCRIPT = `
  123. /[\S]/.test("\u2028");
  124. `
  125. testScript1(SCRIPT, valueFalse, t)
  126. }
  127. func TestRegexpDotMatchCR(t *testing.T) {
  128. const SCRIPT = `
  129. /./.test("\r");
  130. `
  131. testScript1(SCRIPT, valueFalse, t)
  132. }
  133. func TestRegexpDotMatchCRInGroup(t *testing.T) {
  134. const SCRIPT = `
  135. /(.)/.test("\r");
  136. `
  137. testScript1(SCRIPT, valueFalse, t)
  138. }
  139. func TestRegexpDotMatchLF(t *testing.T) {
  140. const SCRIPT = `
  141. /./.test("\n");
  142. `
  143. testScript1(SCRIPT, valueFalse, t)
  144. }
  145. func TestRegexpSplitWithBackRef(t *testing.T) {
  146. const SCRIPT = `
  147. "a++b+-c".split(/([+-])\1/).join(" $$ ")
  148. `
  149. testScript1(SCRIPT, asciiString("a $$ + $$ b+-c"), t)
  150. }
  151. func TestEscapeNonASCII(t *testing.T) {
  152. const SCRIPT = `
  153. /\⩓/.test("⩓")
  154. `
  155. testScript1(SCRIPT, valueTrue, t)
  156. }
  157. func TestRegexpUTF16(t *testing.T) {
  158. const SCRIPT = `
  159. var str = "\uD800\uDC00";
  160. assert(/\uD800/g.test(str), "#1");
  161. assert(/\uD800/.test(str), "#2");
  162. assert(/𐀀/.test(str), "#3");
  163. var re = /\uD800/;
  164. assert(compareArray(str.replace(re, "X"), ["X", "\uDC00"]), "#4");
  165. assert(compareArray(str.split(re), ["", "\uDC00"]), "#5");
  166. assert(compareArray("a\uD800\uDC00b".split(/\uD800/g), ["a", "\uDC00b"]), "#6");
  167. assert(compareArray("a\uD800\uDC00b".split(/(?:)/g), ["a", "\uD800", "\uDC00", "b"]), "#7");
  168. assert(compareArray("0\x80".split(/(0){0}/g), ["0", undefined, "\x80"]), "#7+");
  169. re = /(?=)a/; // a hack to use regexp2
  170. assert.sameValue(re.exec('\ud83d\ude02a').index, 2, "#8");
  171. assert.sameValue(/./.exec('\ud83d\ude02')[0], '\ud83d', "#9");
  172. assert(RegExp("\uD800").test("\uD800"), "#10");
  173. var cu = 0xD800;
  174. var xx = "a\\" + String.fromCharCode(cu);
  175. var pattern = eval("/" + xx + "/");
  176. assert.sameValue(pattern.source, "a\\\\\\ud800", "Code unit: " + cu.toString(16), "#11");
  177. assert(pattern.test("a\\\uD800"), "#12");
  178. `
  179. testScript1(TESTLIB+SCRIPT, _undefined, t)
  180. }
  181. func TestRegexpUnicode(t *testing.T) {
  182. const SCRIPT = `
  183. assert(!/\uD800/u.test("\uD800\uDC00"), "#1");
  184. assert(!/\uFFFD/u.test("\uD800\uDC00"), "#2");
  185. assert(/\uD800\uDC00/u.test("\uD800\uDC00"), "#3");
  186. assert(/\uD800/u.test("\uD800"), "#4");
  187. assert(compareArray("a\uD800\uDC00b".split(/\uD800/gu), ["a\uD800\uDC00b"]), "#5");
  188. assert(compareArray("a\uD800\uDC00b".split(/(?:)/gu), ["a", "𐀀", "b"]), "#6");
  189. assert(compareArray("0\x80".split(/(0){0}/gu), ["0", undefined, "\x80"]), "#7");
  190. var re = eval('/' + /\ud834\udf06/u.source + '/u');
  191. assert(re.test('\ud834\udf06'), "#9");
  192. /*re = RegExp("\\p{L}", "u");
  193. if (!re.test("A")) {
  194. throw new Error("Test 9 failed");
  195. }*/
  196. `
  197. testScript1(TESTLIB+SCRIPT, _undefined, t)
  198. }
  199. func TestConvertRegexpToUnicode(t *testing.T) {
  200. if s := convertRegexpToUnicode(`test\uD800\u0C00passed`); s != `test\uD800\u0C00passed` {
  201. t.Fatal(s)
  202. }
  203. if s := convertRegexpToUnicode(`test\uD800\uDC00passed`); s != `test𐀀passed` {
  204. t.Fatal(s)
  205. }
  206. if s := convertRegexpToUnicode(`test\u0023passed`); s != `test\u0023passed` {
  207. t.Fatal(s)
  208. }
  209. if s := convertRegexpToUnicode(`test\u0passed`); s != `test\u0passed` {
  210. t.Fatal(s)
  211. }
  212. if s := convertRegexpToUnicode(`test\uD800passed`); s != `test\uD800passed` {
  213. t.Fatal(s)
  214. }
  215. if s := convertRegexpToUnicode(`test\uD800`); s != `test\uD800` {
  216. t.Fatal(s)
  217. }
  218. if s := convertRegexpToUnicode(`test\uD80`); s != `test\uD80` {
  219. t.Fatal(s)
  220. }
  221. if s := convertRegexpToUnicode(`\\uD800\uDC00passed`); s != `\\uD800\uDC00passed` {
  222. t.Fatal(s)
  223. }
  224. if s := convertRegexpToUnicode(`testpassed`); s != `testpassed` {
  225. t.Fatal(s)
  226. }
  227. }
  228. func TestConvertRegexpToUtf16(t *testing.T) {
  229. if s := convertRegexpToUtf16(`𐀀`); s != `\ud800\udc00` {
  230. t.Fatal(s)
  231. }
  232. if s := convertRegexpToUtf16(`\𐀀`); s != `\\\ud800\udc00` {
  233. t.Fatal(s)
  234. }
  235. }
  236. func TestEscapeInvalidUtf16(t *testing.T) {
  237. if s := escapeInvalidUtf16(asciiString("test")); s != "test" {
  238. t.Fatal(s)
  239. }
  240. if s := escapeInvalidUtf16(newStringValue("test\U00010000")); s != "test\U00010000" {
  241. t.Fatal(s)
  242. }
  243. if s := escapeInvalidUtf16(unicodeStringFromRunes([]rune{'t', 0xD800})); s != "t\\ud800" {
  244. t.Fatal(s)
  245. }
  246. if s := escapeInvalidUtf16(unicodeStringFromRunes([]rune{'t', 0xD800, 'p'})); s != "t\\ud800p" {
  247. t.Fatal(s)
  248. }
  249. if s := escapeInvalidUtf16(unicodeStringFromRunes([]rune{0xD800, 'p'})); s != "\\ud800p" {
  250. t.Fatal(s)
  251. }
  252. if s := escapeInvalidUtf16(unicodeStringFromRunes([]rune{'t', '\\', 0xD800, 'p'})); s != `t\\\ud800p` {
  253. t.Fatal(s)
  254. }
  255. }
  256. func TestRegexpAssertion(t *testing.T) {
  257. const SCRIPT = `
  258. var res = 'aaa'.match(/^a/g);
  259. res.length === 1 || res[0] === 'a';
  260. `
  261. testScript1(SCRIPT, valueTrue, t)
  262. }
  263. func TestRegexpUnicodeAdvanceStringIndex(t *testing.T) {
  264. const SCRIPT = `
  265. // deoptimise RegExp
  266. var origExec = RegExp.prototype.exec;
  267. RegExp.prototype.exec = function(s) {
  268. return origExec.call(this, s);
  269. };
  270. var re = /(?:)/gu;
  271. var str = "a\uD800\uDC00b";
  272. assert(compareArray(str.split(re), ["a", "𐀀", "b"]), "#1");
  273. re.lastIndex = 3;
  274. assert.sameValue(re.exec(str).index, 3, "#2");
  275. re.lastIndex = 2;
  276. assert.sameValue(re.exec(str).index, 1, "#3");
  277. re.lastIndex = 4;
  278. assert.sameValue(re.exec(str).index, 4, "#4");
  279. re.lastIndex = 5;
  280. assert.sameValue(re.exec(str), null, "#5");
  281. var iterator = str.matchAll(re); // regexp is copied by matchAll, but resets lastIndex
  282. var matches = [];
  283. for (var v of iterator) {matches.push(v);}
  284. assert.sameValue(matches.length, 4, "#6");
  285. assert.sameValue(matches[0].index, 0, "#7 index");
  286. assert.sameValue(matches[0][0], "", "#7 value");
  287. assert.sameValue(matches[1].index, 1, "#8 index");
  288. assert.sameValue(matches[1][0], "", "#8 value");
  289. assert.sameValue(matches[2].index, 3, "#9 index");
  290. assert.sameValue(matches[2][0], "", "#9 value");
  291. assert.sameValue(matches[3].index, 4, "#10 index");
  292. assert.sameValue(matches[3][0], "", "#10 value");
  293. `
  294. testScript1(TESTLIB+SCRIPT, _undefined, t)
  295. }
  296. func TestRegexpInit(t *testing.T) {
  297. const SCRIPT = `
  298. RegExp(".").lastIndex;
  299. `
  300. testScript1(SCRIPT, intToValue(0), t)
  301. }
  302. func TestRegexpToString(t *testing.T) {
  303. const SCRIPT = `
  304. RegExp.prototype.toString.call({
  305. source: 'foo',
  306. flags: 'bar'});
  307. `
  308. testScript1(SCRIPT, asciiString("/foo/bar"), t)
  309. }
  310. func TestRegexpEscapeSource(t *testing.T) {
  311. const SCRIPT = `
  312. /href="(.+?)(\/.*\/\S+?)\/"/.source;
  313. `
  314. testScript1(SCRIPT, asciiString(`href="(.+?)(\/.*\/\S+?)\/"`), t)
  315. }
  316. func TestRegexpConsecutiveMatchCache(t *testing.T) {
  317. const SCRIPT = `
  318. (function test(unicode) {
  319. var regex = new RegExp('t(e)(st(\\d?))', unicode?'gu':'g');
  320. var string = 'test1test2';
  321. var match;
  322. var matches = [];
  323. while (match = regex.exec(string)) {
  324. matches.push(match);
  325. }
  326. var expectedMatches = [
  327. [
  328. 'test1',
  329. 'e',
  330. 'st1',
  331. '1'
  332. ],
  333. [
  334. 'test2',
  335. 'e',
  336. 'st2',
  337. '2'
  338. ]
  339. ];
  340. expectedMatches[0].index = 0;
  341. expectedMatches[0].input = 'test1test2';
  342. expectedMatches[1].index = 5;
  343. expectedMatches[1].input = 'test1test2';
  344. assert(deepEqual(matches, expectedMatches), "#1");
  345. // try the same regexp with a different string
  346. regex.lastIndex = 0;
  347. match = regex.exec(' test5');
  348. var expectedMatch = [
  349. 'test5',
  350. 'e',
  351. 'st5',
  352. '5'
  353. ];
  354. expectedMatch.index = 1;
  355. expectedMatch.input = ' test5';
  356. assert(deepEqual(match, expectedMatch), "#2");
  357. assert.sameValue(regex.lastIndex, 6, "#3");
  358. // continue matching with a different string
  359. match = regex.exec(' test5test6');
  360. expectedMatch = [
  361. 'test6',
  362. 'e',
  363. 'st6',
  364. '6'
  365. ];
  366. expectedMatch.index = 6;
  367. expectedMatch.input = ' test5test6';
  368. assert(deepEqual(match, expectedMatch), "#4");
  369. assert.sameValue(regex.lastIndex, 11, "#5");
  370. match = regex.exec(' test5test6');
  371. assert.sameValue(match, null, "#6");
  372. return regex;
  373. });
  374. `
  375. vm := New()
  376. v, err := vm.RunString(TESTLIBX + SCRIPT)
  377. if err != nil {
  378. t.Fatal(err)
  379. }
  380. var f func(bool) (*Object, error)
  381. err = vm.ExportTo(v, &f)
  382. if err != nil {
  383. t.Fatal(err)
  384. }
  385. regex, err := f(false)
  386. if err != nil {
  387. t.Fatal(err)
  388. }
  389. if regex.self.(*regexpObject).pattern.regexp2Wrapper.cache != nil {
  390. t.Fatal("Cache is not nil (non-unicode)")
  391. }
  392. regex, err = f(true)
  393. if err != nil {
  394. t.Fatal(err)
  395. }
  396. if regex.self.(*regexpObject).pattern.regexp2Wrapper.cache != nil {
  397. t.Fatal("Cache is not nil (unicode)")
  398. }
  399. }
  400. func TestRegexpMatchAll(t *testing.T) {
  401. const SCRIPT = `
  402. (function test(unicode) {
  403. var regex = new RegExp('t(e)(st(\\d?))', unicode?'gu':'g');
  404. var string = 'test1test2';
  405. var matches = [];
  406. for (var match of string.matchAll(regex)) {
  407. matches.push(match);
  408. }
  409. var expectedMatches = [
  410. [
  411. 'test1',
  412. 'e',
  413. 'st1',
  414. '1'
  415. ],
  416. [
  417. 'test2',
  418. 'e',
  419. 'st2',
  420. '2'
  421. ]
  422. ];
  423. expectedMatches[0].index = 0;
  424. expectedMatches[0].input = 'test1test2';
  425. expectedMatches[1].index = 5;
  426. expectedMatches[1].input = 'test1test2';
  427. assert(deepEqual(matches, expectedMatches), "#1");
  428. assert.sameValue(regex.lastIndex, 0, "#1 lastIndex");
  429. // try the same regexp with a different string
  430. string = ' test5';
  431. matches = [];
  432. for (var match of string.matchAll(regex)) {
  433. matches.push(match);
  434. }
  435. expectedMatches = [
  436. [
  437. 'test5',
  438. 'e',
  439. 'st5',
  440. '5'
  441. ]
  442. ];
  443. expectedMatches[0].index = 1;
  444. expectedMatches[0].input = ' test5';
  445. assert(deepEqual(matches, expectedMatches), "#2");
  446. assert.sameValue(regex.lastIndex, 0, "#2 lastIndex");
  447. // continue matching with a different string
  448. string = ' test5test6';
  449. matches = [];
  450. for (var match of string.matchAll(regex)) {
  451. matches.push(match);
  452. }
  453. var expectedMatches = [
  454. [
  455. 'test5',
  456. 'e',
  457. 'st5',
  458. '5'
  459. ],
  460. [
  461. 'test6',
  462. 'e',
  463. 'st6',
  464. '6'
  465. ]
  466. ];
  467. expectedMatches[0].index = 1;
  468. expectedMatches[0].input = ' test5test6';
  469. expectedMatches[1].index = 6;
  470. expectedMatches[1].input = ' test5test6';
  471. assert(deepEqual(matches, expectedMatches), "#3");
  472. assert.sameValue(regex.lastIndex, 0, "#3 lastindex");
  473. });
  474. `
  475. vm := New()
  476. v, err := vm.RunString(TESTLIBX + SCRIPT)
  477. if err != nil {
  478. t.Fatal(err)
  479. }
  480. var f func(bool) (*Object, error)
  481. err = vm.ExportTo(v, &f)
  482. if err != nil {
  483. t.Fatal(err)
  484. }
  485. _, err = f(false)
  486. if err != nil {
  487. t.Fatal(err)
  488. }
  489. _, err = f(true)
  490. if err != nil {
  491. t.Fatal(err)
  492. }
  493. }
  494. func TestRegexpOverrideSpecies(t *testing.T) {
  495. const SCRIPT = `
  496. Object.defineProperty(RegExp, Symbol.species, {
  497. configurable: true,
  498. value: function() {
  499. throw "passed";
  500. }
  501. });
  502. try {
  503. "ab".split(/a/);
  504. throw new Error("Expected error");
  505. } catch(e) {
  506. if (e !== "passed") {
  507. throw e;
  508. }
  509. }
  510. `
  511. testScript1(SCRIPT, _undefined, t)
  512. }
  513. func TestRegexpSymbolMatchAllCallsIsRegexp(t *testing.T) {
  514. // This is tc39's test/built-ins/RegExp/prototype/Symbol.matchAll/isregexp-this-throws.js
  515. const SCRIPT = `
  516. var a = new Object();
  517. Object.defineProperty(a, Symbol.match, {
  518. get: function() {
  519. throw "passed";
  520. }
  521. });
  522. try {
  523. RegExp.prototype[Symbol.matchAll].call(a, '');
  524. throw new Error("Expected error");
  525. } catch(e) {
  526. if (e !== "passed") {
  527. throw e;
  528. }
  529. }
  530. `
  531. testScript1(SCRIPT, _undefined, t)
  532. }
  533. func TestRegexpMatchAllConstructor(t *testing.T) {
  534. // This is tc39's test/built-ins/RegExp/prototype/Symbol.matchAll/species-constuctor.js
  535. const SCRIPT = `
  536. var callCount = 0;
  537. var callArgs;
  538. var regexp = /\d/u;
  539. var obj = {}
  540. Object.defineProperty(obj, Symbol.species, {
  541. value: function() {
  542. callCount++;
  543. callArgs = arguments;
  544. return /\w/g;
  545. }
  546. });
  547. regexp.constructor = obj;
  548. var str = 'a*b';
  549. var iter = regexp[Symbol.matchAll](str);
  550. assert.sameValue(callCount, 1);
  551. assert.sameValue(callArgs.length, 2);
  552. assert.sameValue(callArgs[0], regexp);
  553. assert.sameValue(callArgs[1], 'u');
  554. var first = iter.next()
  555. assert.sameValue(first.done, false);
  556. assert.sameValue(first.value.length, 1);
  557. assert.sameValue(first.value[0], "a");
  558. var second = iter.next()
  559. assert.sameValue(second.done, true);
  560. `
  561. testScript1(TESTLIB+SCRIPT, _undefined, t)
  562. }
  563. func TestRegexp2InvalidEscape(t *testing.T) {
  564. testScript1(`/(?=)\x0/.test("x0")`, valueTrue, t)
  565. }
  566. func TestRegexpUnicodeEmptyMatch(t *testing.T) {
  567. testScript1(`/(0)0|/gu.exec("0\xef").length === 2`, valueTrue, t)
  568. }
  569. func TestRegexpInvalidGroup(t *testing.T) {
  570. const SCRIPT = `
  571. ["?", "(?)"].forEach(function(s) {
  572. assert.throws(SyntaxError, function() {new RegExp(s)}, s);
  573. });
  574. `
  575. testScript1(TESTLIB+SCRIPT, _undefined, t)
  576. }
  577. func TestRegexpLookbehindAssertion(t *testing.T) {
  578. const SCRIPT = `
  579. var re = /(?<=Jack|Tom)Sprat/;
  580. assert(re.test("JackSprat"), "#1");
  581. assert(!re.test("JohnSprat"), "#2");
  582. re = /(?<!-)\d+/;
  583. assert(re.test("3"), "#3");
  584. assert(!re.test("-3"), "#4");
  585. `
  586. testScript1(TESTLIB+SCRIPT, _undefined, t)
  587. }
  588. func TestRegexpInvalidUTF8(t *testing.T) {
  589. vm := New()
  590. // Note that normally vm.ToValue() would replace invalid UTF-8 sequences with RuneError
  591. _, err := vm.New(vm.Get("RegExp"), asciiString([]byte{0xAD}))
  592. if err == nil {
  593. t.Fatal("Expected error")
  594. }
  595. }
  596. // this should not cause data races when run with -race
  597. func TestRegexpConcurrentLiterals(t *testing.T) {
  598. prg := MustCompile("test.js", `var r = /(?<!-)\d+/; r.test("");`, false)
  599. go func() {
  600. vm := New()
  601. _, err := vm.RunProgram(prg)
  602. if err != nil {
  603. panic(err)
  604. }
  605. }()
  606. vm := New()
  607. _, _ = vm.RunProgram(prg)
  608. }
  609. func BenchmarkRegexpSplitWithBackRef(b *testing.B) {
  610. const SCRIPT = `
  611. "aaaaaaaaaaaaaaaaaaaaaaaaa++bbbbbbbbbbbbbbbbbbbbbb+-ccccccccccccccccccccccc".split(/([+-])\1/)
  612. `
  613. b.StopTimer()
  614. prg, err := Compile("test.js", SCRIPT, false)
  615. if err != nil {
  616. b.Fatal(err)
  617. }
  618. vm := New()
  619. b.StartTimer()
  620. for i := 0; i < b.N; i++ {
  621. vm.RunProgram(prg)
  622. }
  623. }
  624. func BenchmarkRegexpMatch(b *testing.B) {
  625. const SCRIPT = `
  626. "a\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\r\
  627. a\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\r\
  628. a\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\r\
  629. a\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\r\
  630. a\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\r\
  631. a\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\r\
  632. a\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\r\
  633. a\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\r\
  634. a\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\r\
  635. a\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\r\
  636. ".match(/[^\r\n]+/g)
  637. `
  638. b.StopTimer()
  639. prg, err := Compile("test.js", SCRIPT, false)
  640. if err != nil {
  641. b.Fatal(err)
  642. }
  643. vm := New()
  644. b.StartTimer()
  645. for i := 0; i < b.N; i++ {
  646. vm.RunProgram(prg)
  647. }
  648. }
  649. func BenchmarkRegexpMatchCache(b *testing.B) {
  650. const SCRIPT = `
  651. (function() {
  652. var s = "a\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\r\
  653. a\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\r\
  654. a\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\r\
  655. a\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\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c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\r\
  684. a\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\r\
  685. a\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\r\
  686. a\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\r\
  687. a\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\r\
  688. a\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\r\
  689. "
  690. var r = /[^\r\n]+/g
  691. while(r.exec(s)) {};
  692. });
  693. `
  694. vm := New()
  695. v, err := vm.RunString(SCRIPT)
  696. if err != nil {
  697. b.Fatal(err)
  698. }
  699. if fn, ok := AssertFunction(v); ok {
  700. b.ResetTimer()
  701. b.ReportAllocs()
  702. for i := 0; i < b.N; i++ {
  703. fn(_undefined)
  704. }
  705. } else {
  706. b.Fatal("not a function")
  707. }
  708. }
  709. func BenchmarkRegexpMatchAll(b *testing.B) {
  710. const SCRIPT = `
  711. (function() {
  712. var s = "a\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\f\rg\nh\ra\nb\r\c\nd\r\e\n\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\rg\nh\r\
  749. "
  750. var r = /[^\r\n]+/g
  751. for (var v of s.matchAll(r)) {}
  752. });
  753. `
  754. vm := New()
  755. v, err := vm.RunString(SCRIPT)
  756. if err != nil {
  757. b.Fatal(err)
  758. }
  759. if fn, ok := AssertFunction(v); ok {
  760. b.ResetTimer()
  761. b.ReportAllocs()
  762. for i := 0; i < b.N; i++ {
  763. fn(_undefined)
  764. }
  765. } else {
  766. b.Fatal("not a function")
  767. }
  768. }
  769. func BenchmarkRegexpSingleExec(b *testing.B) {
  770. vm := New()
  771. regexp := vm.Get("RegExp")
  772. f := func(reStr, str string, b *testing.B) {
  773. r, err := vm.New(regexp, vm.ToValue(reStr))
  774. if err != nil {
  775. b.Fatal(err)
  776. }
  777. exec, ok := AssertFunction(r.Get("exec"))
  778. if !ok {
  779. b.Fatal("RegExp.exec is not a function")
  780. }
  781. arg := vm.ToValue(str)
  782. b.ResetTimer()
  783. b.ReportAllocs()
  784. for i := 0; i < b.N; i++ {
  785. _, err := exec(r, arg)
  786. if err != nil {
  787. b.Fatal(err)
  788. }
  789. }
  790. }
  791. b.Run("Re-ASCII", func(b *testing.B) {
  792. f("test", "aaaaaaaaaaaaaaaaaaaaaaaaa testing", b)
  793. })
  794. b.Run("Re2-ASCII", func(b *testing.B) {
  795. f("(?=)test", "aaaaaaaaaaaaaaaaaaaaaaaaa testing", b)
  796. })
  797. b.Run("Re-Unicode", func(b *testing.B) {
  798. f("test", "aaaaaaaaaaaaaaaaaaaaaaaaa testing 😀", b)
  799. })
  800. b.Run("Re2-Unicode", func(b *testing.B) {
  801. f("(?=)test", "aaaaaaaaaaaaaaaaaaaaaaaaa testing 😀", b)
  802. })
  803. }