2012-05-22 14:05:44 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2008-11-25 11:07:48 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2014-09-30 10:29:32 +00:00
|
|
|
#include <cstdlib>
|
2016-07-25 10:24:45 +00:00
|
|
|
#include <memory>
|
2014-09-30 10:29:32 +00:00
|
|
|
#include <sstream>
|
2008-11-25 11:07:48 +00:00
|
|
|
|
2015-12-22 09:16:30 +00:00
|
|
|
#include "include/v8.h"
|
2019-05-17 12:13:44 +00:00
|
|
|
#include "src/api/api-inl.h"
|
2015-11-26 16:22:34 +00:00
|
|
|
#include "src/ast/ast.h"
|
2019-05-21 09:30:15 +00:00
|
|
|
#include "src/codegen/assembler-arch.h"
|
|
|
|
#include "src/codegen/macro-assembler.h"
|
2019-05-24 13:51:59 +00:00
|
|
|
#include "src/init/v8.h"
|
2019-08-29 14:35:31 +00:00
|
|
|
#include "src/objects/js-regexp-inl.h"
|
2019-05-23 08:51:46 +00:00
|
|
|
#include "src/objects/objects-inl.h"
|
2019-07-09 11:56:47 +00:00
|
|
|
#include "src/regexp/regexp-bytecode-generator.h"
|
2019-10-01 11:55:16 +00:00
|
|
|
#include "src/regexp/regexp-bytecodes.h"
|
2019-06-13 16:00:50 +00:00
|
|
|
#include "src/regexp/regexp-compiler.h"
|
2019-06-12 08:44:08 +00:00
|
|
|
#include "src/regexp/regexp-interpreter.h"
|
2019-06-12 09:02:05 +00:00
|
|
|
#include "src/regexp/regexp-macro-assembler-arch.h"
|
2016-01-08 09:55:29 +00:00
|
|
|
#include "src/regexp/regexp-parser.h"
|
2019-06-17 15:16:39 +00:00
|
|
|
#include "src/regexp/regexp.h"
|
2019-05-21 06:38:38 +00:00
|
|
|
#include "src/strings/char-predicates-inl.h"
|
|
|
|
#include "src/strings/string-stream.h"
|
|
|
|
#include "src/strings/unicode-inl.h"
|
2019-05-23 13:27:57 +00:00
|
|
|
#include "src/utils/ostreams.h"
|
2019-02-14 21:10:30 +00:00
|
|
|
#include "src/zone/zone-list-inl.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
2020-04-29 06:31:16 +00:00
|
|
|
#include "test/common/wasm/flag-utils.h"
|
2008-11-25 11:07:48 +00:00
|
|
|
|
2017-08-11 11:22:28 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2017-09-21 03:29:52 +00:00
|
|
|
namespace test_regexp {
|
2008-11-25 11:07:48 +00:00
|
|
|
|
2010-01-07 19:01:23 +00:00
|
|
|
static bool CheckParse(const char* input) {
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2013-09-19 09:17:13 +00:00
|
|
|
FlatStringReader reader(CcTest::i_isolate(), CStrVector(input));
|
2010-01-07 19:01:23 +00:00
|
|
|
RegExpCompileData result;
|
2015-01-23 15:19:34 +00:00
|
|
|
return v8::internal::RegExpParser::ParseRegExp(
|
2016-01-21 13:33:02 +00:00
|
|
|
CcTest::i_isolate(), &zone, &reader, JSRegExp::kNone, &result);
|
2010-01-07 19:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-11 14:18:29 +00:00
|
|
|
static void CheckParseEq(const char* input, const char* expected,
|
|
|
|
bool unicode = false) {
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2013-09-19 09:17:13 +00:00
|
|
|
FlatStringReader reader(CcTest::i_isolate(), CStrVector(input));
|
2008-12-11 11:13:13 +00:00
|
|
|
RegExpCompileData result;
|
2016-01-21 13:33:02 +00:00
|
|
|
JSRegExp::Flags flags = JSRegExp::kNone;
|
|
|
|
if (unicode) flags |= JSRegExp::kUnicode;
|
|
|
|
CHECK(v8::internal::RegExpParser::ParseRegExp(CcTest::i_isolate(), &zone,
|
|
|
|
&reader, flags, &result));
|
2017-10-18 09:06:55 +00:00
|
|
|
CHECK_NOT_NULL(result.tree);
|
Reland "[regexp] Rewrite error handling"
This is a reland of e80ca24c80432f747c386da61459282d44ba7aaa
Original change's description:
> [regexp] Rewrite error handling
>
> This patch modifies irregexp's error handling. Instead of representing
> errors as C strings, they are represented as an enumeration value
> (RegExpError), and only converted to strings when throwing the error
> object in regexp.cc. This makes it significantly easier to integrate
> into SpiderMonkey. A few notes:
>
> 1. Depending on whether the stack overflows during parsing or
> analysis, the stack overflow message can vary ("Stack overflow" or
> "Maximum call stack size exceeded"). I kept that behaviour in this
> patch, under the assumption that stack overflow messages are
> (sadly) the sorts of things that real world code ends up depending
> on.
>
> 2. Depending on the point in code where the error was identified,
> invalid unicode escapes could be reported as "Invalid Unicode
> escape", "Invalid unicode escape", or "Invalid Unicode escape
> sequence". I fervently hope that nobody depends on the specific
> wording of a syntax error, so I standardized on the first one. (It
> was both the most common, and the most consistent with other
> "Invalid X escape" messages.)
>
> 3. In addition to changing the representation, this patch also adds an
> error_pos field to RegExpParser and RegExpCompileData, which stores
> the position at which an error occurred. This is used by
> SpiderMonkey to provide more helpful messages about where a syntax
> error occurred in large regular expressions.
>
> 4. This model is closer to V8's existing MessageTemplate
> infrastructure. I considered trying to integrate it more closely
> with MessageTemplate, but since one of our stated goals for this
> project was to make it easier to use irregexp outside of V8, I
> decided to hold off.
>
> R=jgruber@chromium.org
>
> Bug: v8:10303
> Change-Id: I62605fd2def2fc539f38a7e0eefa04d36e14bbde
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2091863
> Commit-Queue: Jakob Gruber <jgruber@chromium.org>
> Reviewed-by: Jakob Gruber <jgruber@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#66784}
R=jgruber@chromium.org
Bug: v8:10303
Change-Id: Iad1f11a0e0b9e525d7499aacb56c27eff9e7c7b5
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2109952
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Commit-Queue: Jakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66798}
2020-03-19 14:02:33 +00:00
|
|
|
CHECK(result.error == RegExpError::kNone);
|
2014-09-30 10:29:32 +00:00
|
|
|
std::ostringstream os;
|
2014-07-07 09:57:29 +00:00
|
|
|
result.tree->Print(os, &zone);
|
2015-12-16 06:58:36 +00:00
|
|
|
if (strcmp(expected, os.str().c_str()) != 0) {
|
|
|
|
printf("%s | %s\n", expected, os.str().c_str());
|
|
|
|
}
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK_EQ(0, strcmp(expected, os.str().c_str()));
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2008-12-12 10:22:56 +00:00
|
|
|
static bool CheckSimple(const char* input) {
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2013-09-19 09:17:13 +00:00
|
|
|
FlatStringReader reader(CcTest::i_isolate(), CStrVector(input));
|
2008-12-11 11:13:13 +00:00
|
|
|
RegExpCompileData result;
|
2015-01-23 15:19:34 +00:00
|
|
|
CHECK(v8::internal::RegExpParser::ParseRegExp(
|
2016-01-21 13:33:02 +00:00
|
|
|
CcTest::i_isolate(), &zone, &reader, JSRegExp::kNone, &result));
|
2017-10-18 09:06:55 +00:00
|
|
|
CHECK_NOT_NULL(result.tree);
|
Reland "[regexp] Rewrite error handling"
This is a reland of e80ca24c80432f747c386da61459282d44ba7aaa
Original change's description:
> [regexp] Rewrite error handling
>
> This patch modifies irregexp's error handling. Instead of representing
> errors as C strings, they are represented as an enumeration value
> (RegExpError), and only converted to strings when throwing the error
> object in regexp.cc. This makes it significantly easier to integrate
> into SpiderMonkey. A few notes:
>
> 1. Depending on whether the stack overflows during parsing or
> analysis, the stack overflow message can vary ("Stack overflow" or
> "Maximum call stack size exceeded"). I kept that behaviour in this
> patch, under the assumption that stack overflow messages are
> (sadly) the sorts of things that real world code ends up depending
> on.
>
> 2. Depending on the point in code where the error was identified,
> invalid unicode escapes could be reported as "Invalid Unicode
> escape", "Invalid unicode escape", or "Invalid Unicode escape
> sequence". I fervently hope that nobody depends on the specific
> wording of a syntax error, so I standardized on the first one. (It
> was both the most common, and the most consistent with other
> "Invalid X escape" messages.)
>
> 3. In addition to changing the representation, this patch also adds an
> error_pos field to RegExpParser and RegExpCompileData, which stores
> the position at which an error occurred. This is used by
> SpiderMonkey to provide more helpful messages about where a syntax
> error occurred in large regular expressions.
>
> 4. This model is closer to V8's existing MessageTemplate
> infrastructure. I considered trying to integrate it more closely
> with MessageTemplate, but since one of our stated goals for this
> project was to make it easier to use irregexp outside of V8, I
> decided to hold off.
>
> R=jgruber@chromium.org
>
> Bug: v8:10303
> Change-Id: I62605fd2def2fc539f38a7e0eefa04d36e14bbde
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2091863
> Commit-Queue: Jakob Gruber <jgruber@chromium.org>
> Reviewed-by: Jakob Gruber <jgruber@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#66784}
R=jgruber@chromium.org
Bug: v8:10303
Change-Id: Iad1f11a0e0b9e525d7499aacb56c27eff9e7c7b5
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2109952
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Commit-Queue: Jakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66798}
2020-03-19 14:02:33 +00:00
|
|
|
CHECK(result.error == RegExpError::kNone);
|
2008-12-12 10:22:56 +00:00
|
|
|
return result.simple;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
2008-12-17 10:59:14 +00:00
|
|
|
struct MinMaxPair {
|
|
|
|
int min_match;
|
|
|
|
int max_match;
|
|
|
|
};
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2008-12-17 10:59:14 +00:00
|
|
|
static MinMaxPair CheckMinMaxMatch(const char* input) {
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2013-09-19 09:17:13 +00:00
|
|
|
FlatStringReader reader(CcTest::i_isolate(), CStrVector(input));
|
2008-12-17 10:59:14 +00:00
|
|
|
RegExpCompileData result;
|
2015-01-23 15:19:34 +00:00
|
|
|
CHECK(v8::internal::RegExpParser::ParseRegExp(
|
2016-01-21 13:33:02 +00:00
|
|
|
CcTest::i_isolate(), &zone, &reader, JSRegExp::kNone, &result));
|
2017-10-18 09:06:55 +00:00
|
|
|
CHECK_NOT_NULL(result.tree);
|
Reland "[regexp] Rewrite error handling"
This is a reland of e80ca24c80432f747c386da61459282d44ba7aaa
Original change's description:
> [regexp] Rewrite error handling
>
> This patch modifies irregexp's error handling. Instead of representing
> errors as C strings, they are represented as an enumeration value
> (RegExpError), and only converted to strings when throwing the error
> object in regexp.cc. This makes it significantly easier to integrate
> into SpiderMonkey. A few notes:
>
> 1. Depending on whether the stack overflows during parsing or
> analysis, the stack overflow message can vary ("Stack overflow" or
> "Maximum call stack size exceeded"). I kept that behaviour in this
> patch, under the assumption that stack overflow messages are
> (sadly) the sorts of things that real world code ends up depending
> on.
>
> 2. Depending on the point in code where the error was identified,
> invalid unicode escapes could be reported as "Invalid Unicode
> escape", "Invalid unicode escape", or "Invalid Unicode escape
> sequence". I fervently hope that nobody depends on the specific
> wording of a syntax error, so I standardized on the first one. (It
> was both the most common, and the most consistent with other
> "Invalid X escape" messages.)
>
> 3. In addition to changing the representation, this patch also adds an
> error_pos field to RegExpParser and RegExpCompileData, which stores
> the position at which an error occurred. This is used by
> SpiderMonkey to provide more helpful messages about where a syntax
> error occurred in large regular expressions.
>
> 4. This model is closer to V8's existing MessageTemplate
> infrastructure. I considered trying to integrate it more closely
> with MessageTemplate, but since one of our stated goals for this
> project was to make it easier to use irregexp outside of V8, I
> decided to hold off.
>
> R=jgruber@chromium.org
>
> Bug: v8:10303
> Change-Id: I62605fd2def2fc539f38a7e0eefa04d36e14bbde
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2091863
> Commit-Queue: Jakob Gruber <jgruber@chromium.org>
> Reviewed-by: Jakob Gruber <jgruber@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#66784}
R=jgruber@chromium.org
Bug: v8:10303
Change-Id: Iad1f11a0e0b9e525d7499aacb56c27eff9e7c7b5
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2109952
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Commit-Queue: Jakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66798}
2020-03-19 14:02:33 +00:00
|
|
|
CHECK(result.error == RegExpError::kNone);
|
2008-12-17 10:59:14 +00:00
|
|
|
int min_match = result.tree->min_match();
|
|
|
|
int max_match = result.tree->max_match();
|
|
|
|
MinMaxPair pair = { min_match, max_match };
|
|
|
|
return pair;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-07 19:01:23 +00:00
|
|
|
#define CHECK_PARSE_ERROR(input) CHECK(!CheckParse(input))
|
2008-12-12 10:22:56 +00:00
|
|
|
#define CHECK_SIMPLE(input, simple) CHECK_EQ(simple, CheckSimple(input));
|
2008-12-17 10:59:14 +00:00
|
|
|
#define CHECK_MIN_MAX(input, min, max) \
|
|
|
|
{ MinMaxPair min_max = CheckMinMaxMatch(input); \
|
|
|
|
CHECK_EQ(min, min_max.min_match); \
|
|
|
|
CHECK_EQ(max, min_max.max_match); \
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
|
2017-11-17 00:45:55 +00:00
|
|
|
TEST(RegExpParser) {
|
2010-01-08 06:40:09 +00:00
|
|
|
CHECK_PARSE_ERROR("?");
|
|
|
|
|
2014-07-07 09:57:29 +00:00
|
|
|
CheckParseEq("abc", "'abc'");
|
|
|
|
CheckParseEq("", "%");
|
|
|
|
CheckParseEq("abc|def", "(| 'abc' 'def')");
|
|
|
|
CheckParseEq("abc|def|ghi", "(| 'abc' 'def' 'ghi')");
|
|
|
|
CheckParseEq("^xxx$", "(: @^i 'xxx' @$i)");
|
|
|
|
CheckParseEq("ab\\b\\d\\bcd", "(: 'ab' @b [0-9] @b 'cd')");
|
|
|
|
CheckParseEq("\\w|\\d", "(| [0-9 A-Z _ a-z] [0-9])");
|
|
|
|
CheckParseEq("a*", "(# 0 - g 'a')");
|
|
|
|
CheckParseEq("a*?", "(# 0 - n 'a')");
|
|
|
|
CheckParseEq("abc+", "(: 'ab' (# 1 - g 'c'))");
|
|
|
|
CheckParseEq("abc+?", "(: 'ab' (# 1 - n 'c'))");
|
|
|
|
CheckParseEq("xyz?", "(: 'xy' (# 0 1 g 'z'))");
|
|
|
|
CheckParseEq("xyz??", "(: 'xy' (# 0 1 n 'z'))");
|
|
|
|
CheckParseEq("xyz{0,1}", "(: 'xy' (# 0 1 g 'z'))");
|
|
|
|
CheckParseEq("xyz{0,1}?", "(: 'xy' (# 0 1 n 'z'))");
|
|
|
|
CheckParseEq("xyz{93}", "(: 'xy' (# 93 93 g 'z'))");
|
|
|
|
CheckParseEq("xyz{93}?", "(: 'xy' (# 93 93 n 'z'))");
|
|
|
|
CheckParseEq("xyz{1,32}", "(: 'xy' (# 1 32 g 'z'))");
|
|
|
|
CheckParseEq("xyz{1,32}?", "(: 'xy' (# 1 32 n 'z'))");
|
|
|
|
CheckParseEq("xyz{1,}", "(: 'xy' (# 1 - g 'z'))");
|
|
|
|
CheckParseEq("xyz{1,}?", "(: 'xy' (# 1 - n 'z'))");
|
|
|
|
CheckParseEq("a\\fb\\nc\\rd\\te\\vf", "'a\\x0cb\\x0ac\\x0dd\\x09e\\x0bf'");
|
|
|
|
CheckParseEq("a\\nb\\bc", "(: 'a\\x0ab' @b 'c')");
|
2017-01-18 08:14:59 +00:00
|
|
|
CheckParseEq("(?:foo)", "(?: 'foo')");
|
|
|
|
CheckParseEq("(?: foo )", "(?: ' foo ')");
|
2014-07-07 09:57:29 +00:00
|
|
|
CheckParseEq("(foo|bar|baz)", "(^ (| 'foo' 'bar' 'baz'))");
|
|
|
|
CheckParseEq("foo|(bar|baz)|quux", "(| 'foo' (^ (| 'bar' 'baz')) 'quux')");
|
|
|
|
CheckParseEq("foo(?=bar)baz", "(: 'foo' (-> + 'bar') 'baz')");
|
|
|
|
CheckParseEq("foo(?!bar)baz", "(: 'foo' (-> - 'bar') 'baz')");
|
2017-11-17 00:45:55 +00:00
|
|
|
CheckParseEq("foo(?<=bar)baz", "(: 'foo' (<- + 'bar') 'baz')");
|
|
|
|
CheckParseEq("foo(?<!bar)baz", "(: 'foo' (<- - 'bar') 'baz')");
|
2014-07-07 09:57:29 +00:00
|
|
|
CheckParseEq("()", "(^ %)");
|
|
|
|
CheckParseEq("(?=)", "(-> + %)");
|
2016-01-21 13:33:02 +00:00
|
|
|
CheckParseEq("[]", "^[\\x00-\\u{10ffff}]"); // Doesn't compile on windows
|
|
|
|
CheckParseEq("[^]", "[\\x00-\\u{10ffff}]"); // \uffff isn't in codepage 1252
|
2014-07-07 09:57:29 +00:00
|
|
|
CheckParseEq("[x]", "[x]");
|
|
|
|
CheckParseEq("[xyz]", "[x y z]");
|
|
|
|
CheckParseEq("[a-zA-Z0-9]", "[a-z A-Z 0-9]");
|
|
|
|
CheckParseEq("[-123]", "[- 1 2 3]");
|
|
|
|
CheckParseEq("[^123]", "^[1 2 3]");
|
|
|
|
CheckParseEq("]", "']'");
|
|
|
|
CheckParseEq("}", "'}'");
|
|
|
|
CheckParseEq("[a-b-c]", "[a-b - c]");
|
|
|
|
CheckParseEq("[\\d]", "[0-9]");
|
|
|
|
CheckParseEq("[x\\dz]", "[x 0-9 z]");
|
|
|
|
CheckParseEq("[\\d-z]", "[0-9 - z]");
|
2017-07-25 07:38:09 +00:00
|
|
|
CheckParseEq("[\\d-\\d]", "[0-9 0-9 -]");
|
|
|
|
CheckParseEq("[z-\\d]", "[0-9 z -]");
|
2011-01-07 12:35:42 +00:00
|
|
|
// Control character outside character class.
|
2014-07-07 09:57:29 +00:00
|
|
|
CheckParseEq("\\cj\\cJ\\ci\\cI\\ck\\cK", "'\\x0a\\x0a\\x09\\x09\\x0b\\x0b'");
|
|
|
|
CheckParseEq("\\c!", "'\\c!'");
|
|
|
|
CheckParseEq("\\c_", "'\\c_'");
|
|
|
|
CheckParseEq("\\c~", "'\\c~'");
|
|
|
|
CheckParseEq("\\c1", "'\\c1'");
|
2011-01-07 12:35:42 +00:00
|
|
|
// Control character inside character class.
|
2014-07-07 09:57:29 +00:00
|
|
|
CheckParseEq("[\\c!]", "[\\ c !]");
|
|
|
|
CheckParseEq("[\\c_]", "[\\x1f]");
|
|
|
|
CheckParseEq("[\\c~]", "[\\ c ~]");
|
|
|
|
CheckParseEq("[\\ca]", "[\\x01]");
|
|
|
|
CheckParseEq("[\\cz]", "[\\x1a]");
|
|
|
|
CheckParseEq("[\\cA]", "[\\x01]");
|
|
|
|
CheckParseEq("[\\cZ]", "[\\x1a]");
|
|
|
|
CheckParseEq("[\\c1]", "[\\x11]");
|
|
|
|
|
|
|
|
CheckParseEq("[a\\]c]", "[a ] c]");
|
|
|
|
CheckParseEq("\\[\\]\\{\\}\\(\\)\\%\\^\\#\\ ", "'[]{}()%^# '");
|
|
|
|
CheckParseEq("[\\[\\]\\{\\}\\(\\)\\%\\^\\#\\ ]", "[[ ] { } ( ) % ^ # ]");
|
|
|
|
CheckParseEq("\\0", "'\\x00'");
|
|
|
|
CheckParseEq("\\8", "'8'");
|
|
|
|
CheckParseEq("\\9", "'9'");
|
|
|
|
CheckParseEq("\\11", "'\\x09'");
|
|
|
|
CheckParseEq("\\11a", "'\\x09a'");
|
|
|
|
CheckParseEq("\\011", "'\\x09'");
|
|
|
|
CheckParseEq("\\00011", "'\\x0011'");
|
|
|
|
CheckParseEq("\\118", "'\\x098'");
|
|
|
|
CheckParseEq("\\111", "'I'");
|
|
|
|
CheckParseEq("\\1111", "'I1'");
|
|
|
|
CheckParseEq("(x)(x)(x)\\1", "(: (^ 'x') (^ 'x') (^ 'x') (<- 1))");
|
|
|
|
CheckParseEq("(x)(x)(x)\\2", "(: (^ 'x') (^ 'x') (^ 'x') (<- 2))");
|
|
|
|
CheckParseEq("(x)(x)(x)\\3", "(: (^ 'x') (^ 'x') (^ 'x') (<- 3))");
|
|
|
|
CheckParseEq("(x)(x)(x)\\4", "(: (^ 'x') (^ 'x') (^ 'x') '\\x04')");
|
|
|
|
CheckParseEq("(x)(x)(x)\\1*",
|
|
|
|
"(: (^ 'x') (^ 'x') (^ 'x')"
|
|
|
|
" (# 0 - g (<- 1)))");
|
|
|
|
CheckParseEq("(x)(x)(x)\\2*",
|
|
|
|
"(: (^ 'x') (^ 'x') (^ 'x')"
|
|
|
|
" (# 0 - g (<- 2)))");
|
|
|
|
CheckParseEq("(x)(x)(x)\\3*",
|
|
|
|
"(: (^ 'x') (^ 'x') (^ 'x')"
|
|
|
|
" (# 0 - g (<- 3)))");
|
|
|
|
CheckParseEq("(x)(x)(x)\\4*",
|
|
|
|
"(: (^ 'x') (^ 'x') (^ 'x')"
|
|
|
|
" (# 0 - g '\\x04'))");
|
|
|
|
CheckParseEq("(x)(x)(x)(x)(x)(x)(x)(x)(x)(x)\\10",
|
|
|
|
"(: (^ 'x') (^ 'x') (^ 'x') (^ 'x') (^ 'x') (^ 'x')"
|
|
|
|
" (^ 'x') (^ 'x') (^ 'x') (^ 'x') (<- 10))");
|
|
|
|
CheckParseEq("(x)(x)(x)(x)(x)(x)(x)(x)(x)(x)\\11",
|
|
|
|
"(: (^ 'x') (^ 'x') (^ 'x') (^ 'x') (^ 'x') (^ 'x')"
|
|
|
|
" (^ 'x') (^ 'x') (^ 'x') (^ 'x') '\\x09')");
|
|
|
|
CheckParseEq("(a)\\1", "(: (^ 'a') (<- 1))");
|
|
|
|
CheckParseEq("(a\\1)", "(^ 'a')");
|
|
|
|
CheckParseEq("(\\1a)", "(^ 'a')");
|
2015-12-16 06:58:36 +00:00
|
|
|
CheckParseEq("(\\2)(\\1)", "(: (^ (<- 2)) (^ (<- 1)))");
|
2014-07-07 09:57:29 +00:00
|
|
|
CheckParseEq("(?=a)?a", "'a'");
|
|
|
|
CheckParseEq("(?=a){0,10}a", "'a'");
|
|
|
|
CheckParseEq("(?=a){1,10}a", "(: (-> + 'a') 'a')");
|
|
|
|
CheckParseEq("(?=a){9,10}a", "(: (-> + 'a') 'a')");
|
|
|
|
CheckParseEq("(?!a)?a", "'a'");
|
2015-11-17 13:33:03 +00:00
|
|
|
CheckParseEq("\\1(a)", "(: (<- 1) (^ 'a'))");
|
2014-07-07 09:57:29 +00:00
|
|
|
CheckParseEq("(?!(a))\\1", "(: (-> - (^ 'a')) (<- 1))");
|
2015-11-17 13:33:03 +00:00
|
|
|
CheckParseEq("(?!\\1(a\\1)\\1)\\1",
|
|
|
|
"(: (-> - (: (<- 1) (^ 'a') (<- 1))) (<- 1))");
|
|
|
|
CheckParseEq("\\1\\2(a(?:\\1(b\\1\\2))\\2)\\1",
|
2017-01-18 08:14:59 +00:00
|
|
|
"(: (<- 1) (<- 2) (^ (: 'a' (?: (^ 'b')) (<- 2))) (<- 1))");
|
2017-11-17 00:45:55 +00:00
|
|
|
CheckParseEq("\\1\\2(a(?<=\\1(b\\1\\2))\\2)\\1",
|
|
|
|
"(: (<- 1) (<- 2) (^ (: 'a' (<- + (^ 'b')) (<- 2))) (<- 1))");
|
2014-07-07 09:57:29 +00:00
|
|
|
CheckParseEq("[\\0]", "[\\x00]");
|
|
|
|
CheckParseEq("[\\11]", "[\\x09]");
|
|
|
|
CheckParseEq("[\\11a]", "[\\x09 a]");
|
|
|
|
CheckParseEq("[\\011]", "[\\x09]");
|
|
|
|
CheckParseEq("[\\00011]", "[\\x00 1 1]");
|
|
|
|
CheckParseEq("[\\118]", "[\\x09 8]");
|
|
|
|
CheckParseEq("[\\111]", "[I]");
|
|
|
|
CheckParseEq("[\\1111]", "[I 1]");
|
|
|
|
CheckParseEq("\\x34", "'\x34'");
|
|
|
|
CheckParseEq("\\x60", "'\x60'");
|
|
|
|
CheckParseEq("\\x3z", "'x3z'");
|
|
|
|
CheckParseEq("\\c", "'\\c'");
|
|
|
|
CheckParseEq("\\u0034", "'\x34'");
|
|
|
|
CheckParseEq("\\u003z", "'u003z'");
|
|
|
|
CheckParseEq("foo[z]*", "(: 'foo' (# 0 - g [z]))");
|
2019-06-17 08:45:41 +00:00
|
|
|
CheckParseEq("^^^$$$\\b\\b\\b\\b", "(: @^i @^i @^i @$i @$i @$i @b @b @b @b)");
|
|
|
|
CheckParseEq("\\b\\b\\b\\b\\B\\B\\B\\B\\b\\b\\b\\b",
|
|
|
|
"(: @b @b @b @b @B @B @B @B @b @b @b @b)");
|
2017-03-22 09:28:28 +00:00
|
|
|
CheckParseEq("\\b\\B\\b", "(: @b @B @b)");
|
2008-11-25 11:07:48 +00:00
|
|
|
|
2016-01-11 14:18:29 +00:00
|
|
|
// Unicode regexps
|
|
|
|
CheckParseEq("\\u{12345}", "'\\ud808\\udf45'", true);
|
2016-01-11 15:06:57 +00:00
|
|
|
CheckParseEq("\\u{12345}\\u{23456}", "(! '\\ud808\\udf45' '\\ud84d\\udc56')",
|
|
|
|
true);
|
2016-01-11 14:18:29 +00:00
|
|
|
CheckParseEq("\\u{12345}|\\u{23456}", "(| '\\ud808\\udf45' '\\ud84d\\udc56')",
|
|
|
|
true);
|
2016-01-11 15:06:57 +00:00
|
|
|
CheckParseEq("\\u{12345}{3}", "(# 3 3 g '\\ud808\\udf45')", true);
|
|
|
|
CheckParseEq("\\u{12345}*", "(# 0 - g '\\ud808\\udf45')", true);
|
2016-01-11 14:18:29 +00:00
|
|
|
|
2016-01-21 13:33:02 +00:00
|
|
|
CheckParseEq("\\ud808\\udf45*", "(# 0 - g '\\ud808\\udf45')", true);
|
|
|
|
CheckParseEq("[\\ud808\\udf45-\\ud809\\udccc]", "[\\u{012345}-\\u{0124cc}]",
|
|
|
|
true);
|
|
|
|
|
2012-08-28 09:37:41 +00:00
|
|
|
CHECK_SIMPLE("", false);
|
2008-12-12 10:22:56 +00:00
|
|
|
CHECK_SIMPLE("a", true);
|
|
|
|
CHECK_SIMPLE("a|b", false);
|
|
|
|
CHECK_SIMPLE("a\\n", false);
|
|
|
|
CHECK_SIMPLE("^a", false);
|
|
|
|
CHECK_SIMPLE("a$", false);
|
|
|
|
CHECK_SIMPLE("a\\b!", false);
|
|
|
|
CHECK_SIMPLE("a\\Bb", false);
|
|
|
|
CHECK_SIMPLE("a*", false);
|
|
|
|
CHECK_SIMPLE("a*?", false);
|
|
|
|
CHECK_SIMPLE("a?", false);
|
|
|
|
CHECK_SIMPLE("a??", false);
|
|
|
|
CHECK_SIMPLE("a{0,1}?", false);
|
|
|
|
CHECK_SIMPLE("a{1,1}?", false);
|
|
|
|
CHECK_SIMPLE("a{1,2}?", false);
|
|
|
|
CHECK_SIMPLE("a+?", false);
|
|
|
|
CHECK_SIMPLE("(a)", false);
|
|
|
|
CHECK_SIMPLE("(a)\\1", false);
|
|
|
|
CHECK_SIMPLE("(\\1a)", false);
|
|
|
|
CHECK_SIMPLE("\\1(a)", false);
|
|
|
|
CHECK_SIMPLE("a\\s", false);
|
|
|
|
CHECK_SIMPLE("a\\S", false);
|
|
|
|
CHECK_SIMPLE("a\\d", false);
|
|
|
|
CHECK_SIMPLE("a\\D", false);
|
|
|
|
CHECK_SIMPLE("a\\w", false);
|
|
|
|
CHECK_SIMPLE("a\\W", false);
|
|
|
|
CHECK_SIMPLE("a.", false);
|
|
|
|
CHECK_SIMPLE("a\\q", false);
|
|
|
|
CHECK_SIMPLE("a[a]", false);
|
|
|
|
CHECK_SIMPLE("a[^a]", false);
|
|
|
|
CHECK_SIMPLE("a[a-z]", false);
|
|
|
|
CHECK_SIMPLE("a[\\q]", false);
|
|
|
|
CHECK_SIMPLE("a(?:b)", false);
|
|
|
|
CHECK_SIMPLE("a(?=b)", false);
|
|
|
|
CHECK_SIMPLE("a(?!b)", false);
|
|
|
|
CHECK_SIMPLE("\\x60", false);
|
|
|
|
CHECK_SIMPLE("\\u0060", false);
|
|
|
|
CHECK_SIMPLE("\\cA", false);
|
|
|
|
CHECK_SIMPLE("\\q", false);
|
|
|
|
CHECK_SIMPLE("\\1112", false);
|
|
|
|
CHECK_SIMPLE("\\0", false);
|
|
|
|
CHECK_SIMPLE("(a)\\1", false);
|
|
|
|
CHECK_SIMPLE("(?=a)?a", false);
|
|
|
|
CHECK_SIMPLE("(?!a)?a\\1", false);
|
|
|
|
CHECK_SIMPLE("(?:(?=a))a\\1", false);
|
2008-11-25 11:07:48 +00:00
|
|
|
|
2014-07-07 09:57:29 +00:00
|
|
|
CheckParseEq("a{}", "'a{}'");
|
|
|
|
CheckParseEq("a{,}", "'a{,}'");
|
|
|
|
CheckParseEq("a{", "'a{'");
|
|
|
|
CheckParseEq("a{z}", "'a{z}'");
|
|
|
|
CheckParseEq("a{1z}", "'a{1z}'");
|
|
|
|
CheckParseEq("a{12z}", "'a{12z}'");
|
|
|
|
CheckParseEq("a{12,", "'a{12,'");
|
|
|
|
CheckParseEq("a{12,3b", "'a{12,3b'");
|
|
|
|
CheckParseEq("{}", "'{}'");
|
|
|
|
CheckParseEq("{,}", "'{,}'");
|
|
|
|
CheckParseEq("{", "'{'");
|
|
|
|
CheckParseEq("{z}", "'{z}'");
|
|
|
|
CheckParseEq("{1z}", "'{1z}'");
|
|
|
|
CheckParseEq("{12z}", "'{12z}'");
|
|
|
|
CheckParseEq("{12,", "'{12,'");
|
|
|
|
CheckParseEq("{12,3b", "'{12,3b'");
|
2008-12-17 10:59:14 +00:00
|
|
|
|
|
|
|
CHECK_MIN_MAX("a", 1, 1);
|
|
|
|
CHECK_MIN_MAX("abc", 3, 3);
|
|
|
|
CHECK_MIN_MAX("a[bc]d", 3, 3);
|
|
|
|
CHECK_MIN_MAX("a|bc", 1, 2);
|
|
|
|
CHECK_MIN_MAX("ab|c", 1, 2);
|
|
|
|
CHECK_MIN_MAX("a||bc", 0, 2);
|
|
|
|
CHECK_MIN_MAX("|", 0, 0);
|
|
|
|
CHECK_MIN_MAX("(?:ab)", 2, 2);
|
|
|
|
CHECK_MIN_MAX("(?:ab|cde)", 2, 3);
|
|
|
|
CHECK_MIN_MAX("(?:ab)|cde", 2, 3);
|
|
|
|
CHECK_MIN_MAX("(ab)", 2, 2);
|
|
|
|
CHECK_MIN_MAX("(ab|cde)", 2, 3);
|
2015-12-16 06:58:36 +00:00
|
|
|
CHECK_MIN_MAX("(ab)\\1", 2, RegExpTree::kInfinity);
|
|
|
|
CHECK_MIN_MAX("(ab|cde)\\1", 2, RegExpTree::kInfinity);
|
2008-12-17 10:59:14 +00:00
|
|
|
CHECK_MIN_MAX("(?:ab)?", 0, 2);
|
|
|
|
CHECK_MIN_MAX("(?:ab)*", 0, RegExpTree::kInfinity);
|
|
|
|
CHECK_MIN_MAX("(?:ab)+", 2, RegExpTree::kInfinity);
|
|
|
|
CHECK_MIN_MAX("a?", 0, 1);
|
|
|
|
CHECK_MIN_MAX("a*", 0, RegExpTree::kInfinity);
|
|
|
|
CHECK_MIN_MAX("a+", 1, RegExpTree::kInfinity);
|
|
|
|
CHECK_MIN_MAX("a??", 0, 1);
|
|
|
|
CHECK_MIN_MAX("a*?", 0, RegExpTree::kInfinity);
|
|
|
|
CHECK_MIN_MAX("a+?", 1, RegExpTree::kInfinity);
|
|
|
|
CHECK_MIN_MAX("(?:a?)?", 0, 1);
|
|
|
|
CHECK_MIN_MAX("(?:a*)?", 0, RegExpTree::kInfinity);
|
|
|
|
CHECK_MIN_MAX("(?:a+)?", 0, RegExpTree::kInfinity);
|
|
|
|
CHECK_MIN_MAX("(?:a?)+", 0, RegExpTree::kInfinity);
|
|
|
|
CHECK_MIN_MAX("(?:a*)+", 0, RegExpTree::kInfinity);
|
|
|
|
CHECK_MIN_MAX("(?:a+)+", 1, RegExpTree::kInfinity);
|
|
|
|
CHECK_MIN_MAX("(?:a?)*", 0, RegExpTree::kInfinity);
|
|
|
|
CHECK_MIN_MAX("(?:a*)*", 0, RegExpTree::kInfinity);
|
|
|
|
CHECK_MIN_MAX("(?:a+)*", 0, RegExpTree::kInfinity);
|
|
|
|
CHECK_MIN_MAX("a{0}", 0, 0);
|
|
|
|
CHECK_MIN_MAX("(?:a+){0}", 0, 0);
|
|
|
|
CHECK_MIN_MAX("(?:a+){0,0}", 0, 0);
|
|
|
|
CHECK_MIN_MAX("a*b", 1, RegExpTree::kInfinity);
|
|
|
|
CHECK_MIN_MAX("a+b", 2, RegExpTree::kInfinity);
|
|
|
|
CHECK_MIN_MAX("a*b|c", 1, RegExpTree::kInfinity);
|
|
|
|
CHECK_MIN_MAX("a+b|c", 1, RegExpTree::kInfinity);
|
|
|
|
CHECK_MIN_MAX("(?:a{5,1000000}){3,1000000}", 15, RegExpTree::kInfinity);
|
|
|
|
CHECK_MIN_MAX("(?:ab){4,7}", 8, 14);
|
|
|
|
CHECK_MIN_MAX("a\\bc", 2, 2);
|
|
|
|
CHECK_MIN_MAX("a\\Bc", 2, 2);
|
|
|
|
CHECK_MIN_MAX("a\\sc", 3, 3);
|
|
|
|
CHECK_MIN_MAX("a\\Sc", 3, 3);
|
|
|
|
CHECK_MIN_MAX("a(?=b)c", 2, 2);
|
|
|
|
CHECK_MIN_MAX("a(?=bbb|bb)c", 2, 2);
|
|
|
|
CHECK_MIN_MAX("a(?!bbb|bb)c", 2, 2);
|
2016-06-15 06:48:31 +00:00
|
|
|
|
|
|
|
CheckParseEq("(?<a>x)(?<b>x)(?<c>x)\\k<a>",
|
|
|
|
"(: (^ 'x') (^ 'x') (^ 'x') (<- 1))", true);
|
|
|
|
CheckParseEq("(?<a>x)(?<b>x)(?<c>x)\\k<b>",
|
|
|
|
"(: (^ 'x') (^ 'x') (^ 'x') (<- 2))", true);
|
|
|
|
CheckParseEq("(?<a>x)(?<b>x)(?<c>x)\\k<c>",
|
|
|
|
"(: (^ 'x') (^ 'x') (^ 'x') (<- 3))", true);
|
|
|
|
CheckParseEq("(?<a>a)\\k<a>", "(: (^ 'a') (<- 1))", true);
|
|
|
|
CheckParseEq("(?<a>a\\k<a>)", "(^ 'a')", true);
|
|
|
|
CheckParseEq("(?<a>\\k<a>a)", "(^ 'a')", true);
|
|
|
|
CheckParseEq("(?<a>\\k<b>)(?<b>\\k<a>)", "(: (^ (<- 2)) (^ (<- 1)))", true);
|
|
|
|
CheckParseEq("\\k<a>(?<a>a)", "(: (<- 1) (^ 'a'))", true);
|
|
|
|
|
|
|
|
CheckParseEq("(?<\\u{03C0}>a)", "(^ 'a')", true);
|
|
|
|
CheckParseEq("(?<\\u03C0>a)", "(^ 'a')", true);
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ParserRegression) {
|
2014-07-07 09:57:29 +00:00
|
|
|
CheckParseEq("[A-Z$-][x]", "(! [A-Z $ -] [x])");
|
|
|
|
CheckParseEq("a{3,4*}", "(: 'a{3,' (# 0 - g '4') '}')");
|
|
|
|
CheckParseEq("{", "'{'");
|
|
|
|
CheckParseEq("a|", "(| 'a' %)");
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
2016-06-15 06:48:31 +00:00
|
|
|
static void ExpectError(const char* input, const char* expected,
|
|
|
|
bool unicode = false) {
|
2018-06-20 16:32:59 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2018-06-20 16:32:59 +00:00
|
|
|
Zone zone(isolate->allocator(), ZONE_NAME);
|
|
|
|
FlatStringReader reader(isolate, CStrVector(input));
|
2008-12-11 11:13:13 +00:00
|
|
|
RegExpCompileData result;
|
2016-06-15 06:48:31 +00:00
|
|
|
JSRegExp::Flags flags = JSRegExp::kNone;
|
|
|
|
if (unicode) flags |= JSRegExp::kUnicode;
|
2018-06-20 16:32:59 +00:00
|
|
|
CHECK(!v8::internal::RegExpParser::ParseRegExp(isolate, &zone, &reader, flags,
|
|
|
|
&result));
|
2017-10-18 09:06:55 +00:00
|
|
|
CHECK_NULL(result.tree);
|
Reland "[regexp] Rewrite error handling"
This is a reland of e80ca24c80432f747c386da61459282d44ba7aaa
Original change's description:
> [regexp] Rewrite error handling
>
> This patch modifies irregexp's error handling. Instead of representing
> errors as C strings, they are represented as an enumeration value
> (RegExpError), and only converted to strings when throwing the error
> object in regexp.cc. This makes it significantly easier to integrate
> into SpiderMonkey. A few notes:
>
> 1. Depending on whether the stack overflows during parsing or
> analysis, the stack overflow message can vary ("Stack overflow" or
> "Maximum call stack size exceeded"). I kept that behaviour in this
> patch, under the assumption that stack overflow messages are
> (sadly) the sorts of things that real world code ends up depending
> on.
>
> 2. Depending on the point in code where the error was identified,
> invalid unicode escapes could be reported as "Invalid Unicode
> escape", "Invalid unicode escape", or "Invalid Unicode escape
> sequence". I fervently hope that nobody depends on the specific
> wording of a syntax error, so I standardized on the first one. (It
> was both the most common, and the most consistent with other
> "Invalid X escape" messages.)
>
> 3. In addition to changing the representation, this patch also adds an
> error_pos field to RegExpParser and RegExpCompileData, which stores
> the position at which an error occurred. This is used by
> SpiderMonkey to provide more helpful messages about where a syntax
> error occurred in large regular expressions.
>
> 4. This model is closer to V8's existing MessageTemplate
> infrastructure. I considered trying to integrate it more closely
> with MessageTemplate, but since one of our stated goals for this
> project was to make it easier to use irregexp outside of V8, I
> decided to hold off.
>
> R=jgruber@chromium.org
>
> Bug: v8:10303
> Change-Id: I62605fd2def2fc539f38a7e0eefa04d36e14bbde
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2091863
> Commit-Queue: Jakob Gruber <jgruber@chromium.org>
> Reviewed-by: Jakob Gruber <jgruber@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#66784}
R=jgruber@chromium.org
Bug: v8:10303
Change-Id: Iad1f11a0e0b9e525d7499aacb56c27eff9e7c7b5
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2109952
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Commit-Queue: Jakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66798}
2020-03-19 14:02:33 +00:00
|
|
|
CHECK(result.error != RegExpError::kNone);
|
|
|
|
CHECK_EQ(0, strcmp(expected, RegExpErrorString(result.error)));
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-20 10:52:20 +00:00
|
|
|
TEST(Errors) {
|
2008-11-25 11:07:48 +00:00
|
|
|
const char* kEndBackslash = "\\ at end of pattern";
|
|
|
|
ExpectError("\\", kEndBackslash);
|
|
|
|
const char* kUnterminatedGroup = "Unterminated group";
|
|
|
|
ExpectError("(foo", kUnterminatedGroup);
|
|
|
|
const char* kInvalidGroup = "Invalid group";
|
|
|
|
ExpectError("(?", kInvalidGroup);
|
|
|
|
const char* kUnterminatedCharacterClass = "Unterminated character class";
|
|
|
|
ExpectError("[", kUnterminatedCharacterClass);
|
|
|
|
ExpectError("[a-", kUnterminatedCharacterClass);
|
|
|
|
const char* kNothingToRepeat = "Nothing to repeat";
|
|
|
|
ExpectError("*", kNothingToRepeat);
|
|
|
|
ExpectError("?", kNothingToRepeat);
|
|
|
|
ExpectError("+", kNothingToRepeat);
|
|
|
|
ExpectError("{1}", kNothingToRepeat);
|
|
|
|
ExpectError("{1,2}", kNothingToRepeat);
|
|
|
|
ExpectError("{1,}", kNothingToRepeat);
|
2009-01-30 10:38:25 +00:00
|
|
|
|
|
|
|
// Check that we don't allow more than kMaxCapture captures
|
|
|
|
const int kMaxCaptures = 1 << 16; // Must match RegExpParser::kMaxCaptures.
|
|
|
|
const char* kTooManyCaptures = "Too many captures";
|
2014-09-30 10:29:32 +00:00
|
|
|
std::ostringstream os;
|
2009-01-30 10:38:25 +00:00
|
|
|
for (int i = 0; i <= kMaxCaptures; i++) {
|
2014-07-07 09:57:29 +00:00
|
|
|
os << "()";
|
2009-01-30 10:38:25 +00:00
|
|
|
}
|
2014-09-30 10:29:32 +00:00
|
|
|
ExpectError(os.str().c_str(), kTooManyCaptures);
|
2016-06-15 06:48:31 +00:00
|
|
|
|
|
|
|
const char* kInvalidCaptureName = "Invalid capture group name";
|
|
|
|
ExpectError("(?<>.)", kInvalidCaptureName, true);
|
|
|
|
ExpectError("(?<1>.)", kInvalidCaptureName, true);
|
|
|
|
ExpectError("(?<_%>.)", kInvalidCaptureName, true);
|
|
|
|
ExpectError("\\k<a", kInvalidCaptureName, true);
|
|
|
|
const char* kDuplicateCaptureName = "Duplicate capture group name";
|
|
|
|
ExpectError("(?<a>.)(?<a>.)", kDuplicateCaptureName, true);
|
Reland "[regexp] Rewrite error handling"
This is a reland of e80ca24c80432f747c386da61459282d44ba7aaa
Original change's description:
> [regexp] Rewrite error handling
>
> This patch modifies irregexp's error handling. Instead of representing
> errors as C strings, they are represented as an enumeration value
> (RegExpError), and only converted to strings when throwing the error
> object in regexp.cc. This makes it significantly easier to integrate
> into SpiderMonkey. A few notes:
>
> 1. Depending on whether the stack overflows during parsing or
> analysis, the stack overflow message can vary ("Stack overflow" or
> "Maximum call stack size exceeded"). I kept that behaviour in this
> patch, under the assumption that stack overflow messages are
> (sadly) the sorts of things that real world code ends up depending
> on.
>
> 2. Depending on the point in code where the error was identified,
> invalid unicode escapes could be reported as "Invalid Unicode
> escape", "Invalid unicode escape", or "Invalid Unicode escape
> sequence". I fervently hope that nobody depends on the specific
> wording of a syntax error, so I standardized on the first one. (It
> was both the most common, and the most consistent with other
> "Invalid X escape" messages.)
>
> 3. In addition to changing the representation, this patch also adds an
> error_pos field to RegExpParser and RegExpCompileData, which stores
> the position at which an error occurred. This is used by
> SpiderMonkey to provide more helpful messages about where a syntax
> error occurred in large regular expressions.
>
> 4. This model is closer to V8's existing MessageTemplate
> infrastructure. I considered trying to integrate it more closely
> with MessageTemplate, but since one of our stated goals for this
> project was to make it easier to use irregexp outside of V8, I
> decided to hold off.
>
> R=jgruber@chromium.org
>
> Bug: v8:10303
> Change-Id: I62605fd2def2fc539f38a7e0eefa04d36e14bbde
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2091863
> Commit-Queue: Jakob Gruber <jgruber@chromium.org>
> Reviewed-by: Jakob Gruber <jgruber@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#66784}
R=jgruber@chromium.org
Bug: v8:10303
Change-Id: Iad1f11a0e0b9e525d7499aacb56c27eff9e7c7b5
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2109952
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Commit-Queue: Jakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66798}
2020-03-19 14:02:33 +00:00
|
|
|
const char* kInvalidUnicodeEscape = "Invalid Unicode escape";
|
2016-06-15 06:48:31 +00:00
|
|
|
ExpectError("(?<\\u{FISK}", kInvalidUnicodeEscape, true);
|
|
|
|
const char* kInvalidCaptureReferenced = "Invalid named capture referenced";
|
|
|
|
ExpectError("\\k<a>", kInvalidCaptureReferenced, true);
|
|
|
|
ExpectError("(?<b>)\\k<a>", kInvalidCaptureReferenced, true);
|
|
|
|
const char* kInvalidNamedReference = "Invalid named reference";
|
|
|
|
ExpectError("\\ka", kInvalidNamedReference, true);
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool IsDigit(uc16 c) {
|
|
|
|
return ('0' <= c && c <= '9');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool NotDigit(uc16 c) {
|
|
|
|
return !IsDigit(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-10 12:43:10 +00:00
|
|
|
static bool IsWhiteSpaceOrLineTerminator(uc16 c) {
|
|
|
|
// According to ECMA 5.1, 15.10.2.12 the CharacterClassEscape \s includes
|
|
|
|
// WhiteSpace (7.2) and LineTerminator (7.3) values.
|
2018-11-14 14:14:58 +00:00
|
|
|
return v8::internal::IsWhiteSpaceOrLineTerminator(c);
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-10 12:43:10 +00:00
|
|
|
static bool NotWhiteSpaceNorLineTermiantor(uc16 c) {
|
|
|
|
return !IsWhiteSpaceOrLineTerminator(c);
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool NotWord(uc16 c) {
|
2008-12-01 15:42:35 +00:00
|
|
|
return !IsRegExpWord(c);
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void TestCharacterClassEscapes(uc16 c, bool (pred)(uc16 c)) {
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2012-06-11 12:42:31 +00:00
|
|
|
ZoneList<CharacterRange>* ranges =
|
2020-07-09 10:56:29 +00:00
|
|
|
zone.New<ZoneList<CharacterRange>>(2, &zone);
|
2013-06-26 13:36:16 +00:00
|
|
|
CharacterRange::AddClassEscape(c, ranges, &zone);
|
2016-01-21 13:33:02 +00:00
|
|
|
for (uc32 i = 0; i < (1 << 16); i++) {
|
2008-11-25 11:07:48 +00:00
|
|
|
bool in_class = false;
|
|
|
|
for (int j = 0; !in_class && j < ranges->length(); j++) {
|
|
|
|
CharacterRange& range = ranges->at(j);
|
|
|
|
in_class = (range.from() <= i && i <= range.to());
|
|
|
|
}
|
|
|
|
CHECK_EQ(pred(i), in_class);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(CharacterClassEscapes) {
|
2008-12-01 15:42:35 +00:00
|
|
|
TestCharacterClassEscapes('.', IsRegExpNewline);
|
2008-11-25 11:07:48 +00:00
|
|
|
TestCharacterClassEscapes('d', IsDigit);
|
|
|
|
TestCharacterClassEscapes('D', NotDigit);
|
2014-02-10 12:43:10 +00:00
|
|
|
TestCharacterClassEscapes('s', IsWhiteSpaceOrLineTerminator);
|
|
|
|
TestCharacterClassEscapes('S', NotWhiteSpaceNorLineTermiantor);
|
2008-12-01 15:42:35 +00:00
|
|
|
TestCharacterClassEscapes('w', IsRegExpWord);
|
2008-11-25 11:07:48 +00:00
|
|
|
TestCharacterClassEscapes('W', NotWord);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-12 09:50:15 +00:00
|
|
|
static RegExpNode* Compile(const char* input, bool multiline, bool unicode,
|
|
|
|
bool is_one_byte, Zone* zone) {
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2011-04-04 18:26:06 +00:00
|
|
|
FlatStringReader reader(isolate, CStrVector(input));
|
2008-12-11 11:13:13 +00:00
|
|
|
RegExpCompileData compile_data;
|
2019-08-08 13:38:17 +00:00
|
|
|
compile_data.compilation_target = RegExpCompilationTarget::kNative;
|
2016-01-21 13:33:02 +00:00
|
|
|
JSRegExp::Flags flags = JSRegExp::kNone;
|
|
|
|
if (multiline) flags = JSRegExp::kMultiline;
|
|
|
|
if (unicode) flags = JSRegExp::kUnicode;
|
2015-01-23 15:19:34 +00:00
|
|
|
if (!v8::internal::RegExpParser::ParseRegExp(CcTest::i_isolate(), zone,
|
2016-01-21 13:33:02 +00:00
|
|
|
&reader, flags, &compile_data))
|
2017-10-13 16:33:03 +00:00
|
|
|
return nullptr;
|
2015-01-12 09:50:15 +00:00
|
|
|
Handle<String> pattern = isolate->factory()
|
|
|
|
->NewStringFromUtf8(CStrVector(input))
|
|
|
|
.ToHandleChecked();
|
2012-04-02 09:38:07 +00:00
|
|
|
Handle<String> sample_subject =
|
2014-04-17 13:27:02 +00:00
|
|
|
isolate->factory()->NewStringFromUtf8(CStrVector("")).ToHandleChecked();
|
2019-06-17 15:16:39 +00:00
|
|
|
RegExp::CompileForTesting(isolate, zone, &compile_data, flags, pattern,
|
|
|
|
sample_subject, is_one_byte);
|
2008-12-11 11:13:13 +00:00
|
|
|
return compile_data.node;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-12 09:50:15 +00:00
|
|
|
static void Execute(const char* input, bool multiline, bool unicode,
|
|
|
|
bool is_one_byte, bool dot_output = false) {
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2015-01-12 09:50:15 +00:00
|
|
|
RegExpNode* node = Compile(input, multiline, unicode, is_one_byte, &zone);
|
2008-11-25 11:07:48 +00:00
|
|
|
USE(node);
|
|
|
|
#ifdef DEBUG
|
2019-06-17 15:27:26 +00:00
|
|
|
if (dot_output) RegExp::DotPrintForTesting(input, node);
|
2008-11-25 11:07:48 +00:00
|
|
|
#endif // DEBUG
|
|
|
|
}
|
|
|
|
|
2010-01-07 19:01:23 +00:00
|
|
|
// Test of debug-only syntax.
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
TEST(ParsePossessiveRepetition) {
|
|
|
|
bool old_flag_value = FLAG_regexp_possessive_quantifier;
|
|
|
|
|
|
|
|
// Enable possessive quantifier syntax.
|
|
|
|
FLAG_regexp_possessive_quantifier = true;
|
|
|
|
|
2014-07-07 09:57:29 +00:00
|
|
|
CheckParseEq("a*+", "(# 0 - p 'a')");
|
|
|
|
CheckParseEq("a++", "(# 1 - p 'a')");
|
|
|
|
CheckParseEq("a?+", "(# 0 1 p 'a')");
|
|
|
|
CheckParseEq("a{10,20}+", "(# 10 20 p 'a')");
|
|
|
|
CheckParseEq("za{10,20}+b", "(: 'z' (# 10 20 p 'a') 'b')");
|
2010-01-07 19:01:23 +00:00
|
|
|
|
|
|
|
// Disable possessive quantifier syntax.
|
|
|
|
FLAG_regexp_possessive_quantifier = false;
|
|
|
|
|
|
|
|
CHECK_PARSE_ERROR("a*+");
|
|
|
|
CHECK_PARSE_ERROR("a++");
|
|
|
|
CHECK_PARSE_ERROR("a?+");
|
|
|
|
CHECK_PARSE_ERROR("a{10,20}+");
|
|
|
|
CHECK_PARSE_ERROR("a{10,20}+b");
|
|
|
|
|
|
|
|
FLAG_regexp_possessive_quantifier = old_flag_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2008-11-25 11:07:48 +00:00
|
|
|
|
2009-07-07 08:11:19 +00:00
|
|
|
// Tests of interpreter.
|
|
|
|
|
2009-08-31 12:40:37 +00:00
|
|
|
#if V8_TARGET_ARCH_IA32
|
2019-05-27 11:31:49 +00:00
|
|
|
using ArchRegExpMacroAssembler = RegExpMacroAssemblerIA32;
|
2009-08-31 12:40:37 +00:00
|
|
|
#elif V8_TARGET_ARCH_X64
|
2019-05-27 11:31:49 +00:00
|
|
|
using ArchRegExpMacroAssembler = RegExpMacroAssemblerX64;
|
2009-08-31 12:40:37 +00:00
|
|
|
#elif V8_TARGET_ARCH_ARM
|
2019-05-27 11:31:49 +00:00
|
|
|
using ArchRegExpMacroAssembler = RegExpMacroAssemblerARM;
|
2014-03-21 09:28:26 +00:00
|
|
|
#elif V8_TARGET_ARCH_ARM64
|
2019-05-27 11:31:49 +00:00
|
|
|
using ArchRegExpMacroAssembler = RegExpMacroAssemblerARM64;
|
2016-03-15 18:07:43 +00:00
|
|
|
#elif V8_TARGET_ARCH_S390
|
2019-05-27 11:31:49 +00:00
|
|
|
using ArchRegExpMacroAssembler = RegExpMacroAssemblerS390;
|
2020-02-21 14:37:03 +00:00
|
|
|
#elif V8_TARGET_ARCH_PPC || V8_TARGET_ARCH_PPC64
|
2019-05-27 11:31:49 +00:00
|
|
|
using ArchRegExpMacroAssembler = RegExpMacroAssemblerPPC;
|
2010-02-04 20:36:58 +00:00
|
|
|
#elif V8_TARGET_ARCH_MIPS
|
2019-05-27 11:31:49 +00:00
|
|
|
using ArchRegExpMacroAssembler = RegExpMacroAssemblerMIPS;
|
2014-07-09 11:08:26 +00:00
|
|
|
#elif V8_TARGET_ARCH_MIPS64
|
2019-05-27 11:31:49 +00:00
|
|
|
using ArchRegExpMacroAssembler = RegExpMacroAssemblerMIPS;
|
2014-05-23 16:37:27 +00:00
|
|
|
#elif V8_TARGET_ARCH_X87
|
2019-05-27 11:31:49 +00:00
|
|
|
using ArchRegExpMacroAssembler = RegExpMacroAssemblerX87;
|
2009-08-14 11:24:32 +00:00
|
|
|
#endif
|
|
|
|
|
2008-12-12 10:49:00 +00:00
|
|
|
class ContextInitializer {
|
|
|
|
public:
|
2009-08-14 11:24:32 +00:00
|
|
|
ContextInitializer()
|
2013-09-19 08:54:58 +00:00
|
|
|
: scope_(CcTest::isolate()),
|
|
|
|
env_(v8::Context::New(CcTest::isolate())) {
|
2008-12-12 10:49:00 +00:00
|
|
|
env_->Enter();
|
|
|
|
}
|
|
|
|
~ContextInitializer() {
|
|
|
|
env_->Exit();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
v8::HandleScope scope_;
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Local<v8::Context> env_;
|
2008-12-12 10:49:00 +00:00
|
|
|
};
|
|
|
|
|
2019-08-29 14:35:31 +00:00
|
|
|
// Create new JSRegExp object with only necessary fields (for this tests)
|
|
|
|
// initialized.
|
|
|
|
static Handle<JSRegExp> CreateJSRegExp(Handle<String> source, Handle<Code> code,
|
|
|
|
bool is_unicode = false) {
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
Handle<JSFunction> constructor = isolate->regexp_function();
|
|
|
|
Handle<JSRegExp> regexp =
|
|
|
|
Handle<JSRegExp>::cast(factory->NewJSObject(constructor));
|
|
|
|
|
|
|
|
factory->SetRegExpIrregexpData(regexp, JSRegExp::IRREGEXP, source,
|
2019-10-21 11:37:40 +00:00
|
|
|
JSRegExp::kNone, 0,
|
|
|
|
JSRegExp::kNoBacktrackLimit);
|
2019-08-29 14:35:31 +00:00
|
|
|
regexp->SetDataAt(is_unicode ? JSRegExp::kIrregexpUC16CodeIndex
|
|
|
|
: JSRegExp::kIrregexpLatin1CodeIndex,
|
|
|
|
*code);
|
|
|
|
|
|
|
|
return regexp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ArchRegExpMacroAssembler::Result Execute(JSRegExp regexp, String input,
|
2009-08-14 11:24:32 +00:00
|
|
|
int start_offset,
|
2018-04-13 22:28:05 +00:00
|
|
|
Address input_start,
|
|
|
|
Address input_end,
|
2010-01-26 11:08:42 +00:00
|
|
|
int* captures) {
|
2019-01-25 00:42:23 +00:00
|
|
|
return static_cast<NativeRegExpMacroAssembler::Result>(
|
2019-08-29 14:35:31 +00:00
|
|
|
NativeRegExpMacroAssembler::Execute(
|
|
|
|
input, start_offset, reinterpret_cast<byte*>(input_start),
|
|
|
|
reinterpret_cast<byte*>(input_end), captures, 0, CcTest::i_isolate(),
|
|
|
|
regexp));
|
2009-01-12 13:05:23 +00:00
|
|
|
}
|
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
TEST(MacroAssemblerNativeSuccess) {
|
2008-12-12 11:40:03 +00:00
|
|
|
v8::V8::Initialize();
|
2008-12-12 10:49:00 +00:00
|
|
|
ContextInitializer initializer;
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-06-26 13:36:16 +00:00
|
|
|
Factory* factory = isolate->factory();
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2008-11-25 13:13:02 +00:00
|
|
|
|
2015-01-23 15:19:34 +00:00
|
|
|
ArchRegExpMacroAssembler m(isolate, &zone, NativeRegExpMacroAssembler::LATIN1,
|
|
|
|
4);
|
2008-11-25 13:13:02 +00:00
|
|
|
|
|
|
|
m.Succeed();
|
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> source = factory->NewStringFromStaticChars("");
|
2008-12-08 09:22:12 +00:00
|
|
|
Handle<Object> code_object = m.GetCode(source);
|
2008-11-25 13:13:02 +00:00
|
|
|
Handle<Code> code = Handle<Code>::cast(code_object);
|
2019-08-29 14:35:31 +00:00
|
|
|
Handle<JSRegExp> regexp = CreateJSRegExp(source, code);
|
2008-11-25 13:13:02 +00:00
|
|
|
|
|
|
|
int captures[4] = {42, 37, 87, 117};
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> input = factory->NewStringFromStaticChars("foofoo");
|
2012-11-15 13:31:27 +00:00
|
|
|
Handle<SeqOneByteString> seq_input = Handle<SeqOneByteString>::cast(input);
|
2018-04-13 22:28:05 +00:00
|
|
|
Address start_adr = seq_input->GetCharsAddress();
|
2008-11-25 13:13:02 +00:00
|
|
|
|
2019-08-29 14:35:31 +00:00
|
|
|
NativeRegExpMacroAssembler::Result result = Execute(
|
|
|
|
*regexp, *input, 0, start_adr, start_adr + seq_input->length(), captures);
|
2008-11-25 13:13:02 +00:00
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
CHECK_EQ(NativeRegExpMacroAssembler::SUCCESS, result);
|
2008-11-25 13:13:02 +00:00
|
|
|
CHECK_EQ(-1, captures[0]);
|
|
|
|
CHECK_EQ(-1, captures[1]);
|
|
|
|
CHECK_EQ(-1, captures[2]);
|
|
|
|
CHECK_EQ(-1, captures[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
TEST(MacroAssemblerNativeSimple) {
|
2008-12-12 11:40:03 +00:00
|
|
|
v8::V8::Initialize();
|
2008-12-12 10:49:00 +00:00
|
|
|
ContextInitializer initializer;
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-06-26 13:36:16 +00:00
|
|
|
Factory* factory = isolate->factory();
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2008-11-25 13:13:02 +00:00
|
|
|
|
2015-01-23 15:19:34 +00:00
|
|
|
ArchRegExpMacroAssembler m(isolate, &zone, NativeRegExpMacroAssembler::LATIN1,
|
|
|
|
4);
|
2008-11-25 13:13:02 +00:00
|
|
|
|
2013-06-06 10:17:07 +00:00
|
|
|
Label fail, backtrack;
|
|
|
|
m.PushBacktrack(&fail);
|
2017-10-13 16:33:03 +00:00
|
|
|
m.CheckNotAtStart(0, nullptr);
|
|
|
|
m.LoadCurrentCharacter(2, nullptr);
|
|
|
|
m.CheckNotCharacter('o', nullptr);
|
|
|
|
m.LoadCurrentCharacter(1, nullptr, false);
|
|
|
|
m.CheckNotCharacter('o', nullptr);
|
|
|
|
m.LoadCurrentCharacter(0, nullptr, false);
|
|
|
|
m.CheckNotCharacter('f', nullptr);
|
2008-12-08 09:22:12 +00:00
|
|
|
m.WriteCurrentPositionToRegister(0, 0);
|
2013-06-06 10:17:07 +00:00
|
|
|
m.WriteCurrentPositionToRegister(1, 3);
|
2008-11-25 13:13:02 +00:00
|
|
|
m.AdvanceCurrentPosition(3);
|
2013-06-06 10:17:07 +00:00
|
|
|
m.PushBacktrack(&backtrack);
|
2008-11-25 13:13:02 +00:00
|
|
|
m.Succeed();
|
2020-03-17 14:54:12 +00:00
|
|
|
m.BindJumpTarget(&backtrack);
|
2013-06-06 10:17:07 +00:00
|
|
|
m.Backtrack();
|
2020-03-17 14:54:12 +00:00
|
|
|
m.BindJumpTarget(&fail);
|
2008-11-25 13:13:02 +00:00
|
|
|
m.Fail();
|
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> source = factory->NewStringFromStaticChars("^foo");
|
2008-12-08 09:22:12 +00:00
|
|
|
Handle<Object> code_object = m.GetCode(source);
|
2008-11-25 13:13:02 +00:00
|
|
|
Handle<Code> code = Handle<Code>::cast(code_object);
|
2019-08-29 14:35:31 +00:00
|
|
|
Handle<JSRegExp> regexp = CreateJSRegExp(source, code);
|
2008-11-25 13:13:02 +00:00
|
|
|
|
|
|
|
int captures[4] = {42, 37, 87, 117};
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> input = factory->NewStringFromStaticChars("foofoo");
|
2012-11-15 13:31:27 +00:00
|
|
|
Handle<SeqOneByteString> seq_input = Handle<SeqOneByteString>::cast(input);
|
2008-11-25 13:13:02 +00:00
|
|
|
Address start_adr = seq_input->GetCharsAddress();
|
|
|
|
|
2019-08-29 14:35:31 +00:00
|
|
|
NativeRegExpMacroAssembler::Result result = Execute(
|
|
|
|
*regexp, *input, 0, start_adr, start_adr + input->length(), captures);
|
2008-11-25 13:13:02 +00:00
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
CHECK_EQ(NativeRegExpMacroAssembler::SUCCESS, result);
|
2008-11-25 13:13:02 +00:00
|
|
|
CHECK_EQ(0, captures[0]);
|
|
|
|
CHECK_EQ(3, captures[1]);
|
|
|
|
CHECK_EQ(-1, captures[2]);
|
|
|
|
CHECK_EQ(-1, captures[3]);
|
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
input = factory->NewStringFromStaticChars("barbarbar");
|
2012-11-15 13:31:27 +00:00
|
|
|
seq_input = Handle<SeqOneByteString>::cast(input);
|
2008-11-25 13:13:02 +00:00
|
|
|
start_adr = seq_input->GetCharsAddress();
|
|
|
|
|
2019-08-29 14:35:31 +00:00
|
|
|
result = Execute(*regexp, *input, 0, start_adr, start_adr + input->length(),
|
2010-01-26 11:08:42 +00:00
|
|
|
captures);
|
2008-11-25 13:13:02 +00:00
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
CHECK_EQ(NativeRegExpMacroAssembler::FAILURE, result);
|
2008-11-25 13:13:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
TEST(MacroAssemblerNativeSimpleUC16) {
|
2008-12-12 11:40:03 +00:00
|
|
|
v8::V8::Initialize();
|
2008-12-12 10:49:00 +00:00
|
|
|
ContextInitializer initializer;
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-06-26 13:36:16 +00:00
|
|
|
Factory* factory = isolate->factory();
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2008-11-27 13:54:31 +00:00
|
|
|
|
2015-01-23 15:19:34 +00:00
|
|
|
ArchRegExpMacroAssembler m(isolate, &zone, NativeRegExpMacroAssembler::UC16,
|
|
|
|
4);
|
2008-11-27 13:54:31 +00:00
|
|
|
|
2013-06-06 10:17:07 +00:00
|
|
|
Label fail, backtrack;
|
|
|
|
m.PushBacktrack(&fail);
|
2017-10-13 16:33:03 +00:00
|
|
|
m.CheckNotAtStart(0, nullptr);
|
|
|
|
m.LoadCurrentCharacter(2, nullptr);
|
|
|
|
m.CheckNotCharacter('o', nullptr);
|
|
|
|
m.LoadCurrentCharacter(1, nullptr, false);
|
|
|
|
m.CheckNotCharacter('o', nullptr);
|
|
|
|
m.LoadCurrentCharacter(0, nullptr, false);
|
|
|
|
m.CheckNotCharacter('f', nullptr);
|
2008-12-08 09:22:12 +00:00
|
|
|
m.WriteCurrentPositionToRegister(0, 0);
|
2013-06-06 10:17:07 +00:00
|
|
|
m.WriteCurrentPositionToRegister(1, 3);
|
2008-11-27 13:54:31 +00:00
|
|
|
m.AdvanceCurrentPosition(3);
|
2013-06-06 10:17:07 +00:00
|
|
|
m.PushBacktrack(&backtrack);
|
2008-11-27 13:54:31 +00:00
|
|
|
m.Succeed();
|
2020-03-17 14:54:12 +00:00
|
|
|
m.BindJumpTarget(&backtrack);
|
2013-06-06 10:17:07 +00:00
|
|
|
m.Backtrack();
|
2020-03-17 14:54:12 +00:00
|
|
|
m.BindJumpTarget(&fail);
|
2008-11-27 13:54:31 +00:00
|
|
|
m.Fail();
|
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> source = factory->NewStringFromStaticChars("^foo");
|
2008-12-08 09:22:12 +00:00
|
|
|
Handle<Object> code_object = m.GetCode(source);
|
2008-11-27 13:54:31 +00:00
|
|
|
Handle<Code> code = Handle<Code>::cast(code_object);
|
2019-08-29 14:35:31 +00:00
|
|
|
Handle<JSRegExp> regexp = CreateJSRegExp(source, code, true);
|
2008-11-27 13:54:31 +00:00
|
|
|
|
|
|
|
int captures[4] = {42, 37, 87, 117};
|
2011-12-13 13:02:08 +00:00
|
|
|
const uc16 input_data[6] = {'f', 'o', 'o', 'f', 'o',
|
2013-02-26 10:49:59 +00:00
|
|
|
static_cast<uc16>(0x2603)};
|
2014-04-17 13:27:02 +00:00
|
|
|
Handle<String> input = factory->NewStringFromTwoByte(
|
|
|
|
Vector<const uc16>(input_data, 6)).ToHandleChecked();
|
2008-11-27 13:54:31 +00:00
|
|
|
Handle<SeqTwoByteString> seq_input = Handle<SeqTwoByteString>::cast(input);
|
|
|
|
Address start_adr = seq_input->GetCharsAddress();
|
|
|
|
|
2019-08-29 14:35:31 +00:00
|
|
|
NativeRegExpMacroAssembler::Result result = Execute(
|
|
|
|
*regexp, *input, 0, start_adr, start_adr + input->length(), captures);
|
2008-11-27 13:54:31 +00:00
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
CHECK_EQ(NativeRegExpMacroAssembler::SUCCESS, result);
|
2008-11-27 13:54:31 +00:00
|
|
|
CHECK_EQ(0, captures[0]);
|
|
|
|
CHECK_EQ(3, captures[1]);
|
|
|
|
CHECK_EQ(-1, captures[2]);
|
|
|
|
CHECK_EQ(-1, captures[3]);
|
|
|
|
|
2011-12-13 13:02:08 +00:00
|
|
|
const uc16 input_data2[9] = {'b', 'a', 'r', 'b', 'a', 'r', 'b', 'a',
|
2013-02-26 10:49:59 +00:00
|
|
|
static_cast<uc16>(0x2603)};
|
2014-04-17 13:27:02 +00:00
|
|
|
input = factory->NewStringFromTwoByte(
|
|
|
|
Vector<const uc16>(input_data2, 9)).ToHandleChecked();
|
2008-11-27 13:54:31 +00:00
|
|
|
seq_input = Handle<SeqTwoByteString>::cast(input);
|
|
|
|
start_adr = seq_input->GetCharsAddress();
|
|
|
|
|
2019-08-29 14:35:31 +00:00
|
|
|
result = Execute(*regexp, *input, 0, start_adr,
|
|
|
|
start_adr + input->length() * 2, captures);
|
2008-11-27 13:54:31 +00:00
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
CHECK_EQ(NativeRegExpMacroAssembler::FAILURE, result);
|
2008-11-27 13:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
TEST(MacroAssemblerNativeBacktrack) {
|
2008-12-12 11:40:03 +00:00
|
|
|
v8::V8::Initialize();
|
2008-12-12 10:49:00 +00:00
|
|
|
ContextInitializer initializer;
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-06-26 13:36:16 +00:00
|
|
|
Factory* factory = isolate->factory();
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2008-11-25 13:13:02 +00:00
|
|
|
|
2015-01-23 15:19:34 +00:00
|
|
|
ArchRegExpMacroAssembler m(isolate, &zone, NativeRegExpMacroAssembler::LATIN1,
|
|
|
|
0);
|
2008-11-25 13:13:02 +00:00
|
|
|
|
|
|
|
Label fail;
|
|
|
|
Label backtrack;
|
|
|
|
m.LoadCurrentCharacter(10, &fail);
|
|
|
|
m.Succeed();
|
2020-03-17 14:54:12 +00:00
|
|
|
m.BindJumpTarget(&fail);
|
2008-11-25 13:13:02 +00:00
|
|
|
m.PushBacktrack(&backtrack);
|
2017-10-13 16:33:03 +00:00
|
|
|
m.LoadCurrentCharacter(10, nullptr);
|
2008-11-25 13:13:02 +00:00
|
|
|
m.Succeed();
|
2020-03-17 14:54:12 +00:00
|
|
|
m.BindJumpTarget(&backtrack);
|
2008-11-25 13:13:02 +00:00
|
|
|
m.Fail();
|
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> source = factory->NewStringFromStaticChars("..........");
|
2008-12-08 09:22:12 +00:00
|
|
|
Handle<Object> code_object = m.GetCode(source);
|
2008-11-25 13:13:02 +00:00
|
|
|
Handle<Code> code = Handle<Code>::cast(code_object);
|
2019-08-29 14:35:31 +00:00
|
|
|
Handle<JSRegExp> regexp = CreateJSRegExp(source, code);
|
2008-11-25 13:13:02 +00:00
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> input = factory->NewStringFromStaticChars("foofoo");
|
2012-11-15 13:31:27 +00:00
|
|
|
Handle<SeqOneByteString> seq_input = Handle<SeqOneByteString>::cast(input);
|
2008-11-25 13:13:02 +00:00
|
|
|
Address start_adr = seq_input->GetCharsAddress();
|
|
|
|
|
2017-10-13 16:33:03 +00:00
|
|
|
NativeRegExpMacroAssembler::Result result = Execute(
|
2019-08-29 14:35:31 +00:00
|
|
|
*regexp, *input, 0, start_adr, start_adr + input->length(), nullptr);
|
2008-11-25 13:13:02 +00:00
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
CHECK_EQ(NativeRegExpMacroAssembler::FAILURE, result);
|
2008-11-25 13:13:02 +00:00
|
|
|
}
|
|
|
|
|
2008-12-03 13:24:34 +00:00
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
TEST(MacroAssemblerNativeBackReferenceLATIN1) {
|
2008-12-12 11:40:03 +00:00
|
|
|
v8::V8::Initialize();
|
2008-12-12 10:49:00 +00:00
|
|
|
ContextInitializer initializer;
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-06-26 13:36:16 +00:00
|
|
|
Factory* factory = isolate->factory();
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2008-12-03 13:24:34 +00:00
|
|
|
|
2015-01-23 15:19:34 +00:00
|
|
|
ArchRegExpMacroAssembler m(isolate, &zone, NativeRegExpMacroAssembler::LATIN1,
|
|
|
|
4);
|
2008-12-03 13:24:34 +00:00
|
|
|
|
2008-12-08 09:22:12 +00:00
|
|
|
m.WriteCurrentPositionToRegister(0, 0);
|
2008-12-03 13:24:34 +00:00
|
|
|
m.AdvanceCurrentPosition(2);
|
2008-12-08 09:22:12 +00:00
|
|
|
m.WriteCurrentPositionToRegister(1, 0);
|
2008-12-03 13:24:34 +00:00
|
|
|
Label nomatch;
|
2015-11-17 13:33:03 +00:00
|
|
|
m.CheckNotBackReference(0, false, &nomatch);
|
2008-12-03 13:24:34 +00:00
|
|
|
m.Fail();
|
|
|
|
m.Bind(&nomatch);
|
|
|
|
m.AdvanceCurrentPosition(2);
|
|
|
|
Label missing_match;
|
2015-11-17 13:33:03 +00:00
|
|
|
m.CheckNotBackReference(0, false, &missing_match);
|
2008-12-08 09:22:12 +00:00
|
|
|
m.WriteCurrentPositionToRegister(2, 0);
|
2008-12-03 13:24:34 +00:00
|
|
|
m.Succeed();
|
|
|
|
m.Bind(&missing_match);
|
|
|
|
m.Fail();
|
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> source = factory->NewStringFromStaticChars("^(..)..\1");
|
2008-12-08 09:22:12 +00:00
|
|
|
Handle<Object> code_object = m.GetCode(source);
|
2008-12-03 13:24:34 +00:00
|
|
|
Handle<Code> code = Handle<Code>::cast(code_object);
|
2019-08-29 14:35:31 +00:00
|
|
|
Handle<JSRegExp> regexp = CreateJSRegExp(source, code);
|
2008-12-03 13:24:34 +00:00
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> input = factory->NewStringFromStaticChars("fooofo");
|
2012-11-15 13:31:27 +00:00
|
|
|
Handle<SeqOneByteString> seq_input = Handle<SeqOneByteString>::cast(input);
|
2008-12-03 13:24:34 +00:00
|
|
|
Address start_adr = seq_input->GetCharsAddress();
|
|
|
|
|
2009-08-31 12:40:37 +00:00
|
|
|
int output[4];
|
2019-08-29 14:35:31 +00:00
|
|
|
NativeRegExpMacroAssembler::Result result = Execute(
|
|
|
|
*regexp, *input, 0, start_adr, start_adr + input->length(), output);
|
2009-08-14 11:24:32 +00:00
|
|
|
|
|
|
|
CHECK_EQ(NativeRegExpMacroAssembler::SUCCESS, result);
|
2008-12-03 13:24:34 +00:00
|
|
|
CHECK_EQ(0, output[0]);
|
|
|
|
CHECK_EQ(2, output[1]);
|
|
|
|
CHECK_EQ(6, output[2]);
|
2009-08-31 12:40:37 +00:00
|
|
|
CHECK_EQ(-1, output[3]);
|
2008-12-03 13:24:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
TEST(MacroAssemblerNativeBackReferenceUC16) {
|
2008-12-16 14:21:00 +00:00
|
|
|
v8::V8::Initialize();
|
|
|
|
ContextInitializer initializer;
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-06-26 13:36:16 +00:00
|
|
|
Factory* factory = isolate->factory();
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2008-12-16 14:21:00 +00:00
|
|
|
|
2015-01-23 15:19:34 +00:00
|
|
|
ArchRegExpMacroAssembler m(isolate, &zone, NativeRegExpMacroAssembler::UC16,
|
|
|
|
4);
|
2008-12-16 14:21:00 +00:00
|
|
|
|
|
|
|
m.WriteCurrentPositionToRegister(0, 0);
|
|
|
|
m.AdvanceCurrentPosition(2);
|
|
|
|
m.WriteCurrentPositionToRegister(1, 0);
|
|
|
|
Label nomatch;
|
2015-11-17 13:33:03 +00:00
|
|
|
m.CheckNotBackReference(0, false, &nomatch);
|
2008-12-16 14:21:00 +00:00
|
|
|
m.Fail();
|
|
|
|
m.Bind(&nomatch);
|
|
|
|
m.AdvanceCurrentPosition(2);
|
|
|
|
Label missing_match;
|
2015-11-17 13:33:03 +00:00
|
|
|
m.CheckNotBackReference(0, false, &missing_match);
|
2008-12-16 14:21:00 +00:00
|
|
|
m.WriteCurrentPositionToRegister(2, 0);
|
|
|
|
m.Succeed();
|
|
|
|
m.Bind(&missing_match);
|
|
|
|
m.Fail();
|
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> source = factory->NewStringFromStaticChars("^(..)..\1");
|
2008-12-16 14:21:00 +00:00
|
|
|
Handle<Object> code_object = m.GetCode(source);
|
|
|
|
Handle<Code> code = Handle<Code>::cast(code_object);
|
2019-08-29 14:35:31 +00:00
|
|
|
Handle<JSRegExp> regexp = CreateJSRegExp(source, code, true);
|
2008-12-16 14:21:00 +00:00
|
|
|
|
|
|
|
const uc16 input_data[6] = {'f', 0x2028, 'o', 'o', 'f', 0x2028};
|
2014-04-17 13:27:02 +00:00
|
|
|
Handle<String> input = factory->NewStringFromTwoByte(
|
|
|
|
Vector<const uc16>(input_data, 6)).ToHandleChecked();
|
2008-12-16 14:21:00 +00:00
|
|
|
Handle<SeqTwoByteString> seq_input = Handle<SeqTwoByteString>::cast(input);
|
|
|
|
Address start_adr = seq_input->GetCharsAddress();
|
|
|
|
|
2009-08-31 12:40:37 +00:00
|
|
|
int output[4];
|
2019-08-29 14:35:31 +00:00
|
|
|
NativeRegExpMacroAssembler::Result result = Execute(
|
|
|
|
*regexp, *input, 0, start_adr, start_adr + input->length() * 2, output);
|
2008-12-16 14:21:00 +00:00
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
CHECK_EQ(NativeRegExpMacroAssembler::SUCCESS, result);
|
2008-12-16 14:21:00 +00:00
|
|
|
CHECK_EQ(0, output[0]);
|
|
|
|
CHECK_EQ(2, output[1]);
|
|
|
|
CHECK_EQ(6, output[2]);
|
2009-08-31 12:40:37 +00:00
|
|
|
CHECK_EQ(-1, output[3]);
|
2008-12-16 14:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
TEST(MacroAssemblernativeAtStart) {
|
2008-12-12 11:40:03 +00:00
|
|
|
v8::V8::Initialize();
|
2008-12-12 10:49:00 +00:00
|
|
|
ContextInitializer initializer;
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-06-26 13:36:16 +00:00
|
|
|
Factory* factory = isolate->factory();
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2008-11-25 13:13:02 +00:00
|
|
|
|
2015-01-23 15:19:34 +00:00
|
|
|
ArchRegExpMacroAssembler m(isolate, &zone, NativeRegExpMacroAssembler::LATIN1,
|
|
|
|
0);
|
2008-12-01 09:57:14 +00:00
|
|
|
|
|
|
|
Label not_at_start, newline, fail;
|
2015-11-17 13:33:03 +00:00
|
|
|
m.CheckNotAtStart(0, ¬_at_start);
|
2008-12-01 09:57:14 +00:00
|
|
|
// Check that prevchar = '\n' and current = 'f'.
|
|
|
|
m.CheckCharacter('\n', &newline);
|
2020-03-17 14:54:12 +00:00
|
|
|
m.BindJumpTarget(&fail);
|
2008-12-01 09:57:14 +00:00
|
|
|
m.Fail();
|
|
|
|
m.Bind(&newline);
|
|
|
|
m.LoadCurrentCharacter(0, &fail);
|
|
|
|
m.CheckNotCharacter('f', &fail);
|
|
|
|
m.Succeed();
|
2008-11-25 13:13:02 +00:00
|
|
|
|
2008-12-01 09:57:14 +00:00
|
|
|
m.Bind(¬_at_start);
|
|
|
|
// Check that prevchar = 'o' and current = 'b'.
|
|
|
|
Label prevo;
|
|
|
|
m.CheckCharacter('o', &prevo);
|
|
|
|
m.Fail();
|
|
|
|
m.Bind(&prevo);
|
|
|
|
m.LoadCurrentCharacter(0, &fail);
|
|
|
|
m.CheckNotCharacter('b', &fail);
|
|
|
|
m.Succeed();
|
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> source = factory->NewStringFromStaticChars("(^f|ob)");
|
2008-12-08 09:22:12 +00:00
|
|
|
Handle<Object> code_object = m.GetCode(source);
|
2008-12-01 09:57:14 +00:00
|
|
|
Handle<Code> code = Handle<Code>::cast(code_object);
|
2019-08-29 14:35:31 +00:00
|
|
|
Handle<JSRegExp> regexp = CreateJSRegExp(source, code);
|
2008-12-01 09:57:14 +00:00
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> input = factory->NewStringFromStaticChars("foobar");
|
2012-11-15 13:31:27 +00:00
|
|
|
Handle<SeqOneByteString> seq_input = Handle<SeqOneByteString>::cast(input);
|
2008-12-01 09:57:14 +00:00
|
|
|
Address start_adr = seq_input->GetCharsAddress();
|
|
|
|
|
2017-10-13 16:33:03 +00:00
|
|
|
NativeRegExpMacroAssembler::Result result = Execute(
|
2019-08-29 14:35:31 +00:00
|
|
|
*regexp, *input, 0, start_adr, start_adr + input->length(), nullptr);
|
2009-08-14 11:24:32 +00:00
|
|
|
|
|
|
|
CHECK_EQ(NativeRegExpMacroAssembler::SUCCESS, result);
|
|
|
|
|
2019-08-29 14:35:31 +00:00
|
|
|
result = Execute(*regexp, *input, 3, start_adr + 3,
|
|
|
|
start_adr + input->length(), nullptr);
|
2009-08-14 11:24:32 +00:00
|
|
|
|
|
|
|
CHECK_EQ(NativeRegExpMacroAssembler::SUCCESS, result);
|
2008-12-01 09:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
TEST(MacroAssemblerNativeBackRefNoCase) {
|
2008-12-12 11:40:03 +00:00
|
|
|
v8::V8::Initialize();
|
2008-12-12 10:49:00 +00:00
|
|
|
ContextInitializer initializer;
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-06-26 13:36:16 +00:00
|
|
|
Factory* factory = isolate->factory();
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2008-12-05 09:18:55 +00:00
|
|
|
|
2015-01-23 15:19:34 +00:00
|
|
|
ArchRegExpMacroAssembler m(isolate, &zone, NativeRegExpMacroAssembler::LATIN1,
|
|
|
|
4);
|
2008-12-05 09:18:55 +00:00
|
|
|
|
|
|
|
Label fail, succ;
|
|
|
|
|
2008-12-08 09:22:12 +00:00
|
|
|
m.WriteCurrentPositionToRegister(0, 0);
|
|
|
|
m.WriteCurrentPositionToRegister(2, 0);
|
2008-12-05 09:18:55 +00:00
|
|
|
m.AdvanceCurrentPosition(3);
|
2008-12-08 09:22:12 +00:00
|
|
|
m.WriteCurrentPositionToRegister(3, 0);
|
2020-06-02 16:25:42 +00:00
|
|
|
m.CheckNotBackReferenceIgnoreCase(2, false, false, &fail); // Match "AbC".
|
|
|
|
m.CheckNotBackReferenceIgnoreCase(2, false, false, &fail); // Match "ABC".
|
2008-12-05 09:18:55 +00:00
|
|
|
Label expected_fail;
|
2020-06-02 16:25:42 +00:00
|
|
|
m.CheckNotBackReferenceIgnoreCase(2, false, false, &expected_fail);
|
2020-03-17 14:54:12 +00:00
|
|
|
m.BindJumpTarget(&fail);
|
2008-12-05 09:18:55 +00:00
|
|
|
m.Fail();
|
|
|
|
|
|
|
|
m.Bind(&expected_fail);
|
|
|
|
m.AdvanceCurrentPosition(3); // Skip "xYz"
|
2020-06-02 16:25:42 +00:00
|
|
|
m.CheckNotBackReferenceIgnoreCase(2, false, false, &succ);
|
2008-12-05 09:18:55 +00:00
|
|
|
m.Fail();
|
|
|
|
|
|
|
|
m.Bind(&succ);
|
2008-12-08 09:22:12 +00:00
|
|
|
m.WriteCurrentPositionToRegister(1, 0);
|
2008-12-05 09:18:55 +00:00
|
|
|
m.Succeed();
|
|
|
|
|
2008-12-08 09:22:12 +00:00
|
|
|
Handle<String> source =
|
2014-09-10 12:38:12 +00:00
|
|
|
factory->NewStringFromStaticChars("^(abc)\1\1(?!\1)...(?!\1)");
|
2008-12-08 09:22:12 +00:00
|
|
|
Handle<Object> code_object = m.GetCode(source);
|
2008-12-05 09:18:55 +00:00
|
|
|
Handle<Code> code = Handle<Code>::cast(code_object);
|
2019-08-29 14:35:31 +00:00
|
|
|
Handle<JSRegExp> regexp = CreateJSRegExp(source, code);
|
2008-12-05 09:18:55 +00:00
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> input = factory->NewStringFromStaticChars("aBcAbCABCxYzab");
|
2012-11-15 13:31:27 +00:00
|
|
|
Handle<SeqOneByteString> seq_input = Handle<SeqOneByteString>::cast(input);
|
2008-12-05 09:18:55 +00:00
|
|
|
Address start_adr = seq_input->GetCharsAddress();
|
|
|
|
|
|
|
|
int output[4];
|
2019-08-29 14:35:31 +00:00
|
|
|
NativeRegExpMacroAssembler::Result result = Execute(
|
|
|
|
*regexp, *input, 0, start_adr, start_adr + input->length(), output);
|
2009-08-14 11:24:32 +00:00
|
|
|
|
|
|
|
CHECK_EQ(NativeRegExpMacroAssembler::SUCCESS, result);
|
2008-12-05 09:18:55 +00:00
|
|
|
CHECK_EQ(0, output[0]);
|
|
|
|
CHECK_EQ(12, output[1]);
|
|
|
|
CHECK_EQ(0, output[2]);
|
|
|
|
CHECK_EQ(3, output[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
TEST(MacroAssemblerNativeRegisters) {
|
2008-12-12 11:40:03 +00:00
|
|
|
v8::V8::Initialize();
|
2008-12-12 10:49:00 +00:00
|
|
|
ContextInitializer initializer;
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-06-26 13:36:16 +00:00
|
|
|
Factory* factory = isolate->factory();
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2008-11-25 13:13:02 +00:00
|
|
|
|
2015-01-23 15:19:34 +00:00
|
|
|
ArchRegExpMacroAssembler m(isolate, &zone, NativeRegExpMacroAssembler::LATIN1,
|
|
|
|
6);
|
2008-11-25 13:13:02 +00:00
|
|
|
|
|
|
|
uc16 foo_chars[3] = {'f', 'o', 'o'};
|
|
|
|
Vector<const uc16> foo(foo_chars, 3);
|
|
|
|
|
2009-08-31 12:40:37 +00:00
|
|
|
enum registers { out1, out2, out3, out4, out5, out6, sp, loop_cnt };
|
2008-11-25 13:13:02 +00:00
|
|
|
Label fail;
|
|
|
|
Label backtrack;
|
2008-12-08 09:22:12 +00:00
|
|
|
m.WriteCurrentPositionToRegister(out1, 0); // Output: [0]
|
2009-01-12 13:05:23 +00:00
|
|
|
m.PushRegister(out1, RegExpMacroAssembler::kNoStackLimitCheck);
|
2008-11-25 13:13:02 +00:00
|
|
|
m.PushBacktrack(&backtrack);
|
|
|
|
m.WriteStackPointerToRegister(sp);
|
|
|
|
// Fill stack and registers
|
|
|
|
m.AdvanceCurrentPosition(2);
|
2008-12-08 09:22:12 +00:00
|
|
|
m.WriteCurrentPositionToRegister(out1, 0);
|
2009-01-12 13:05:23 +00:00
|
|
|
m.PushRegister(out1, RegExpMacroAssembler::kNoStackLimitCheck);
|
2008-11-25 13:13:02 +00:00
|
|
|
m.PushBacktrack(&fail);
|
|
|
|
// Drop backtrack stack frames.
|
|
|
|
m.ReadStackPointerFromRegister(sp);
|
|
|
|
// And take the first backtrack (to &backtrack)
|
|
|
|
m.Backtrack();
|
|
|
|
|
|
|
|
m.PushCurrentPosition();
|
|
|
|
m.AdvanceCurrentPosition(2);
|
|
|
|
m.PopCurrentPosition();
|
|
|
|
|
2020-03-17 14:54:12 +00:00
|
|
|
m.BindJumpTarget(&backtrack);
|
2008-11-25 13:13:02 +00:00
|
|
|
m.PopRegister(out1);
|
|
|
|
m.ReadCurrentPositionFromRegister(out1);
|
|
|
|
m.AdvanceCurrentPosition(3);
|
2008-12-08 09:22:12 +00:00
|
|
|
m.WriteCurrentPositionToRegister(out2, 0); // [0,3]
|
2008-11-25 13:13:02 +00:00
|
|
|
|
|
|
|
Label loop;
|
|
|
|
m.SetRegister(loop_cnt, 0); // loop counter
|
|
|
|
m.Bind(&loop);
|
|
|
|
m.AdvanceRegister(loop_cnt, 1);
|
|
|
|
m.AdvanceCurrentPosition(1);
|
|
|
|
m.IfRegisterLT(loop_cnt, 3, &loop);
|
2008-12-08 09:22:12 +00:00
|
|
|
m.WriteCurrentPositionToRegister(out3, 0); // [0,3,6]
|
2008-11-25 13:13:02 +00:00
|
|
|
|
|
|
|
Label loop2;
|
|
|
|
m.SetRegister(loop_cnt, 2); // loop counter
|
|
|
|
m.Bind(&loop2);
|
|
|
|
m.AdvanceRegister(loop_cnt, -1);
|
|
|
|
m.AdvanceCurrentPosition(1);
|
|
|
|
m.IfRegisterGE(loop_cnt, 0, &loop2);
|
2008-12-08 09:22:12 +00:00
|
|
|
m.WriteCurrentPositionToRegister(out4, 0); // [0,3,6,9]
|
2008-11-25 13:13:02 +00:00
|
|
|
|
|
|
|
Label loop3;
|
|
|
|
Label exit_loop3;
|
2009-01-12 13:05:23 +00:00
|
|
|
m.PushRegister(out4, RegExpMacroAssembler::kNoStackLimitCheck);
|
|
|
|
m.PushRegister(out4, RegExpMacroAssembler::kNoStackLimitCheck);
|
2008-11-25 13:13:02 +00:00
|
|
|
m.ReadCurrentPositionFromRegister(out3);
|
|
|
|
m.Bind(&loop3);
|
|
|
|
m.AdvanceCurrentPosition(1);
|
2008-12-08 09:22:12 +00:00
|
|
|
m.CheckGreedyLoop(&exit_loop3);
|
2008-11-25 13:13:02 +00:00
|
|
|
m.GoTo(&loop3);
|
|
|
|
m.Bind(&exit_loop3);
|
2008-12-08 09:22:12 +00:00
|
|
|
m.PopCurrentPosition();
|
2009-08-31 12:40:37 +00:00
|
|
|
m.WriteCurrentPositionToRegister(out5, 0); // [0,3,6,9,9,-1]
|
2008-11-25 13:13:02 +00:00
|
|
|
|
|
|
|
m.Succeed();
|
|
|
|
|
2020-03-17 14:54:12 +00:00
|
|
|
m.BindJumpTarget(&fail);
|
2008-11-25 13:13:02 +00:00
|
|
|
m.Fail();
|
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> source = factory->NewStringFromStaticChars("<loop test>");
|
2008-12-08 09:22:12 +00:00
|
|
|
Handle<Object> code_object = m.GetCode(source);
|
2008-11-25 13:13:02 +00:00
|
|
|
Handle<Code> code = Handle<Code>::cast(code_object);
|
2019-08-29 14:35:31 +00:00
|
|
|
Handle<JSRegExp> regexp = CreateJSRegExp(source, code);
|
2008-11-25 13:13:02 +00:00
|
|
|
|
|
|
|
// String long enough for test (content doesn't matter).
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> input = factory->NewStringFromStaticChars("foofoofoofoofoo");
|
2012-11-15 13:31:27 +00:00
|
|
|
Handle<SeqOneByteString> seq_input = Handle<SeqOneByteString>::cast(input);
|
2008-11-25 13:13:02 +00:00
|
|
|
Address start_adr = seq_input->GetCharsAddress();
|
|
|
|
|
2009-08-31 12:40:37 +00:00
|
|
|
int output[6];
|
2019-08-29 14:35:31 +00:00
|
|
|
NativeRegExpMacroAssembler::Result result = Execute(
|
|
|
|
*regexp, *input, 0, start_adr, start_adr + input->length(), output);
|
2008-12-12 10:49:00 +00:00
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
CHECK_EQ(NativeRegExpMacroAssembler::SUCCESS, result);
|
2008-11-25 13:13:02 +00:00
|
|
|
CHECK_EQ(0, output[0]);
|
|
|
|
CHECK_EQ(3, output[1]);
|
|
|
|
CHECK_EQ(6, output[2]);
|
|
|
|
CHECK_EQ(9, output[3]);
|
|
|
|
CHECK_EQ(9, output[4]);
|
2009-08-31 12:40:37 +00:00
|
|
|
CHECK_EQ(-1, output[5]);
|
2008-11-25 13:13:02 +00:00
|
|
|
}
|
|
|
|
|
2008-12-12 10:49:00 +00:00
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
TEST(MacroAssemblerStackOverflow) {
|
2008-12-12 11:40:03 +00:00
|
|
|
v8::V8::Initialize();
|
2008-12-12 10:49:00 +00:00
|
|
|
ContextInitializer initializer;
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2011-04-04 18:26:06 +00:00
|
|
|
Factory* factory = isolate->factory();
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2008-12-12 10:49:00 +00:00
|
|
|
|
2015-01-23 15:19:34 +00:00
|
|
|
ArchRegExpMacroAssembler m(isolate, &zone, NativeRegExpMacroAssembler::LATIN1,
|
|
|
|
0);
|
2008-12-12 10:49:00 +00:00
|
|
|
|
|
|
|
Label loop;
|
|
|
|
m.Bind(&loop);
|
|
|
|
m.PushBacktrack(&loop);
|
|
|
|
m.GoTo(&loop);
|
|
|
|
|
|
|
|
Handle<String> source =
|
2014-09-10 12:38:12 +00:00
|
|
|
factory->NewStringFromStaticChars("<stack overflow test>");
|
2008-12-12 10:49:00 +00:00
|
|
|
Handle<Object> code_object = m.GetCode(source);
|
|
|
|
Handle<Code> code = Handle<Code>::cast(code_object);
|
2019-08-29 14:35:31 +00:00
|
|
|
Handle<JSRegExp> regexp = CreateJSRegExp(source, code);
|
2008-12-12 10:49:00 +00:00
|
|
|
|
|
|
|
// String long enough for test (content doesn't matter).
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> input = factory->NewStringFromStaticChars("dummy");
|
2012-11-15 13:31:27 +00:00
|
|
|
Handle<SeqOneByteString> seq_input = Handle<SeqOneByteString>::cast(input);
|
2008-12-12 10:49:00 +00:00
|
|
|
Address start_adr = seq_input->GetCharsAddress();
|
|
|
|
|
2017-10-13 16:33:03 +00:00
|
|
|
NativeRegExpMacroAssembler::Result result = Execute(
|
2019-08-29 14:35:31 +00:00
|
|
|
*regexp, *input, 0, start_adr, start_adr + input->length(), nullptr);
|
2008-12-12 10:49:00 +00:00
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
CHECK_EQ(NativeRegExpMacroAssembler::EXCEPTION, result);
|
2011-04-04 18:26:06 +00:00
|
|
|
CHECK(isolate->has_pending_exception());
|
|
|
|
isolate->clear_pending_exception();
|
2008-12-12 10:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
TEST(MacroAssemblerNativeLotsOfRegisters) {
|
2009-01-12 13:05:23 +00:00
|
|
|
v8::V8::Initialize();
|
|
|
|
ContextInitializer initializer;
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2011-04-04 18:26:06 +00:00
|
|
|
Factory* factory = isolate->factory();
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2009-01-12 13:05:23 +00:00
|
|
|
|
2015-01-23 15:19:34 +00:00
|
|
|
ArchRegExpMacroAssembler m(isolate, &zone, NativeRegExpMacroAssembler::LATIN1,
|
|
|
|
2);
|
2009-01-12 13:05:23 +00:00
|
|
|
|
|
|
|
// At least 2048, to ensure the allocated space for registers
|
|
|
|
// span one full page.
|
|
|
|
const int large_number = 8000;
|
|
|
|
m.WriteCurrentPositionToRegister(large_number, 42);
|
|
|
|
m.WriteCurrentPositionToRegister(0, 0);
|
|
|
|
m.WriteCurrentPositionToRegister(1, 1);
|
|
|
|
Label done;
|
2015-11-17 13:33:03 +00:00
|
|
|
m.CheckNotBackReference(0, false, &done); // Performs a system-stack push.
|
2009-01-12 13:05:23 +00:00
|
|
|
m.Bind(&done);
|
|
|
|
m.PushRegister(large_number, RegExpMacroAssembler::kNoStackLimitCheck);
|
|
|
|
m.PopRegister(1);
|
|
|
|
m.Succeed();
|
|
|
|
|
|
|
|
Handle<String> source =
|
2014-09-10 12:38:12 +00:00
|
|
|
factory->NewStringFromStaticChars("<huge register space test>");
|
2009-01-12 13:05:23 +00:00
|
|
|
Handle<Object> code_object = m.GetCode(source);
|
|
|
|
Handle<Code> code = Handle<Code>::cast(code_object);
|
2019-08-29 14:35:31 +00:00
|
|
|
Handle<JSRegExp> regexp = CreateJSRegExp(source, code);
|
2009-01-12 13:05:23 +00:00
|
|
|
|
|
|
|
// String long enough for test (content doesn't matter).
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> input = factory->NewStringFromStaticChars("sample text");
|
2012-11-15 13:31:27 +00:00
|
|
|
Handle<SeqOneByteString> seq_input = Handle<SeqOneByteString>::cast(input);
|
2009-01-12 13:05:23 +00:00
|
|
|
Address start_adr = seq_input->GetCharsAddress();
|
|
|
|
|
|
|
|
int captures[2];
|
2019-08-29 14:35:31 +00:00
|
|
|
NativeRegExpMacroAssembler::Result result = Execute(
|
|
|
|
*regexp, *input, 0, start_adr, start_adr + input->length(), captures);
|
2009-08-14 11:24:32 +00:00
|
|
|
|
|
|
|
CHECK_EQ(NativeRegExpMacroAssembler::SUCCESS, result);
|
2009-01-12 13:05:23 +00:00
|
|
|
CHECK_EQ(0, captures[0]);
|
|
|
|
CHECK_EQ(42, captures[1]);
|
|
|
|
|
2011-04-04 18:26:06 +00:00
|
|
|
isolate->clear_pending_exception();
|
2009-01-12 13:05:23 +00:00
|
|
|
}
|
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
TEST(MacroAssembler) {
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2019-07-09 11:56:47 +00:00
|
|
|
RegExpBytecodeGenerator m(CcTest::i_isolate(), &zone);
|
2009-08-14 11:24:32 +00:00
|
|
|
// ^f(o)o.
|
2013-06-06 10:17:07 +00:00
|
|
|
Label start, fail, backtrack;
|
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
m.SetRegister(4, 42);
|
|
|
|
m.PushRegister(4, RegExpMacroAssembler::kNoStackLimitCheck);
|
|
|
|
m.AdvanceRegister(4, 42);
|
|
|
|
m.GoTo(&start);
|
|
|
|
m.Fail();
|
|
|
|
m.Bind(&start);
|
2013-06-06 10:17:07 +00:00
|
|
|
m.PushBacktrack(&fail);
|
2017-10-13 16:33:03 +00:00
|
|
|
m.CheckNotAtStart(0, nullptr);
|
|
|
|
m.LoadCurrentCharacter(0, nullptr);
|
|
|
|
m.CheckNotCharacter('f', nullptr);
|
|
|
|
m.LoadCurrentCharacter(1, nullptr);
|
|
|
|
m.CheckNotCharacter('o', nullptr);
|
|
|
|
m.LoadCurrentCharacter(2, nullptr);
|
|
|
|
m.CheckNotCharacter('o', nullptr);
|
2009-08-14 11:24:32 +00:00
|
|
|
m.WriteCurrentPositionToRegister(0, 0);
|
2013-06-06 10:17:07 +00:00
|
|
|
m.WriteCurrentPositionToRegister(1, 3);
|
|
|
|
m.WriteCurrentPositionToRegister(2, 1);
|
|
|
|
m.WriteCurrentPositionToRegister(3, 2);
|
2009-08-14 11:24:32 +00:00
|
|
|
m.AdvanceCurrentPosition(3);
|
2013-06-06 10:17:07 +00:00
|
|
|
m.PushBacktrack(&backtrack);
|
2009-08-14 11:24:32 +00:00
|
|
|
m.Succeed();
|
2020-03-17 14:54:12 +00:00
|
|
|
m.BindJumpTarget(&backtrack);
|
2013-06-06 10:17:07 +00:00
|
|
|
m.ClearRegisters(2, 3);
|
2009-08-14 11:24:32 +00:00
|
|
|
m.Backtrack();
|
2020-03-17 14:54:12 +00:00
|
|
|
m.BindJumpTarget(&fail);
|
2009-08-14 11:24:32 +00:00
|
|
|
m.PopRegister(0);
|
|
|
|
m.Fail();
|
|
|
|
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2011-04-04 18:26:06 +00:00
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
HandleScope scope(isolate);
|
2009-08-14 11:24:32 +00:00
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> source = factory->NewStringFromStaticChars("^f(o)o");
|
2009-08-14 11:24:32 +00:00
|
|
|
Handle<ByteArray> array = Handle<ByteArray>::cast(m.GetCode(source));
|
|
|
|
int captures[5];
|
2020-04-21 09:20:27 +00:00
|
|
|
std::memset(captures, 0, sizeof(captures));
|
2009-08-14 11:24:32 +00:00
|
|
|
|
|
|
|
const uc16 str1[] = {'f', 'o', 'o', 'b', 'a', 'r'};
|
2014-04-22 06:54:16 +00:00
|
|
|
Handle<String> f1_16 = factory->NewStringFromTwoByte(
|
|
|
|
Vector<const uc16>(str1, 6)).ToHandleChecked();
|
2009-08-14 11:24:32 +00:00
|
|
|
|
2020-04-21 09:20:27 +00:00
|
|
|
CHECK_EQ(IrregexpInterpreter::SUCCESS,
|
|
|
|
IrregexpInterpreter::MatchInternal(
|
|
|
|
isolate, *array, *f1_16, captures, 5, 5, 0,
|
|
|
|
RegExp::CallOrigin::kFromRuntime, JSRegExp::kNoBacktrackLimit));
|
2009-08-14 11:24:32 +00:00
|
|
|
CHECK_EQ(0, captures[0]);
|
|
|
|
CHECK_EQ(3, captures[1]);
|
|
|
|
CHECK_EQ(1, captures[2]);
|
|
|
|
CHECK_EQ(2, captures[3]);
|
|
|
|
CHECK_EQ(84, captures[4]);
|
|
|
|
|
|
|
|
const uc16 str2[] = {'b', 'a', 'r', 'f', 'o', 'o'};
|
2014-04-22 06:54:16 +00:00
|
|
|
Handle<String> f2_16 = factory->NewStringFromTwoByte(
|
|
|
|
Vector<const uc16>(str2, 6)).ToHandleChecked();
|
2009-08-14 11:24:32 +00:00
|
|
|
|
2020-04-21 09:20:27 +00:00
|
|
|
std::memset(captures, 0, sizeof(captures));
|
|
|
|
CHECK_EQ(IrregexpInterpreter::FAILURE,
|
|
|
|
IrregexpInterpreter::MatchInternal(
|
|
|
|
isolate, *array, *f2_16, captures, 5, 5, 0,
|
|
|
|
RegExp::CallOrigin::kFromRuntime, JSRegExp::kNoBacktrackLimit));
|
|
|
|
// Failed matches don't alter output registers.
|
|
|
|
CHECK_EQ(0, captures[0]);
|
|
|
|
CHECK_EQ(0, captures[1]);
|
|
|
|
CHECK_EQ(0, captures[2]);
|
|
|
|
CHECK_EQ(0, captures[3]);
|
|
|
|
CHECK_EQ(0, captures[4]);
|
2009-08-14 11:24:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 17:23:57 +00:00
|
|
|
#ifndef V8_INTL_SUPPORT
|
2008-11-25 11:07:48 +00:00
|
|
|
static uc32 canonicalize(uc32 c) {
|
|
|
|
unibrow::uchar canon[unibrow::Ecma262Canonicalize::kMaxWidth];
|
2017-10-13 16:33:03 +00:00
|
|
|
int count = unibrow::Ecma262Canonicalize::Convert(c, '\0', canon, nullptr);
|
2008-11-25 11:07:48 +00:00
|
|
|
if (count == 0) {
|
|
|
|
return c;
|
|
|
|
} else {
|
|
|
|
CHECK_EQ(1, count);
|
|
|
|
return canon[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LatinCanonicalize) {
|
|
|
|
unibrow::Mapping<unibrow::Ecma262UnCanonicalize> un_canonicalize;
|
2015-01-30 09:29:25 +00:00
|
|
|
for (unibrow::uchar lower = 'a'; lower <= 'z'; lower++) {
|
|
|
|
unibrow::uchar upper = lower + ('A' - 'a');
|
2008-11-25 11:07:48 +00:00
|
|
|
CHECK_EQ(canonicalize(lower), canonicalize(upper));
|
|
|
|
unibrow::uchar uncanon[unibrow::Ecma262UnCanonicalize::kMaxWidth];
|
|
|
|
int length = un_canonicalize.get(lower, '\0', uncanon);
|
|
|
|
CHECK_EQ(2, length);
|
|
|
|
CHECK_EQ(upper, uncanon[0]);
|
|
|
|
CHECK_EQ(lower, uncanon[1]);
|
|
|
|
}
|
|
|
|
for (uc32 c = 128; c < (1 << 21); c++)
|
|
|
|
CHECK_GE(canonicalize(c), 128);
|
|
|
|
unibrow::Mapping<unibrow::ToUppercase> to_upper;
|
2010-07-30 07:10:22 +00:00
|
|
|
// Canonicalization is only defined for the Basic Multilingual Plane.
|
|
|
|
for (uc32 c = 0; c < (1 << 16); c++) {
|
2008-11-25 11:07:48 +00:00
|
|
|
unibrow::uchar upper[unibrow::ToUppercase::kMaxWidth];
|
|
|
|
int length = to_upper.get(c, '\0', upper);
|
|
|
|
if (length == 0) {
|
|
|
|
length = 1;
|
|
|
|
upper[0] = c;
|
|
|
|
}
|
|
|
|
uc32 u = upper[0];
|
|
|
|
if (length > 1 || (c >= 128 && u < 128))
|
|
|
|
u = c;
|
|
|
|
CHECK_EQ(u, canonicalize(c));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-30 07:10:22 +00:00
|
|
|
static uc32 CanonRangeEnd(uc32 c) {
|
2008-11-25 11:07:48 +00:00
|
|
|
unibrow::uchar canon[unibrow::CanonicalizationRange::kMaxWidth];
|
2017-10-13 16:33:03 +00:00
|
|
|
int count = unibrow::CanonicalizationRange::Convert(c, '\0', canon, nullptr);
|
2008-11-25 11:07:48 +00:00
|
|
|
if (count == 0) {
|
|
|
|
return c;
|
|
|
|
} else {
|
|
|
|
CHECK_EQ(1, count);
|
|
|
|
return canon[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(RangeCanonicalization) {
|
|
|
|
// Check that we arrive at the same result when using the basic
|
|
|
|
// range canonicalization primitives as when using immediate
|
|
|
|
// canonicalization.
|
|
|
|
unibrow::Mapping<unibrow::Ecma262UnCanonicalize> un_canonicalize;
|
2010-07-30 07:10:22 +00:00
|
|
|
int block_start = 0;
|
|
|
|
while (block_start <= 0xFFFF) {
|
|
|
|
uc32 block_end = CanonRangeEnd(block_start);
|
|
|
|
unsigned block_length = block_end - block_start + 1;
|
|
|
|
if (block_length > 1) {
|
|
|
|
unibrow::uchar first[unibrow::Ecma262UnCanonicalize::kMaxWidth];
|
|
|
|
int first_length = un_canonicalize.get(block_start, '\0', first);
|
|
|
|
for (unsigned i = 1; i < block_length; i++) {
|
|
|
|
unibrow::uchar succ[unibrow::Ecma262UnCanonicalize::kMaxWidth];
|
|
|
|
int succ_length = un_canonicalize.get(block_start + i, '\0', succ);
|
|
|
|
CHECK_EQ(first_length, succ_length);
|
|
|
|
for (int j = 0; j < succ_length; j++) {
|
|
|
|
int calc = first[j] + i;
|
|
|
|
int found = succ[j];
|
|
|
|
CHECK_EQ(calc, found);
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-30 07:10:22 +00:00
|
|
|
block_start = block_start + block_length;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-26 06:05:07 +00:00
|
|
|
TEST(UncanonicalizeEquivalence) {
|
|
|
|
unibrow::Mapping<unibrow::Ecma262UnCanonicalize> un_canonicalize;
|
|
|
|
unibrow::uchar chars[unibrow::Ecma262UnCanonicalize::kMaxWidth];
|
|
|
|
for (int i = 0; i < (1 << 16); i++) {
|
|
|
|
int length = un_canonicalize.get(i, '\0', chars);
|
|
|
|
for (int j = 0; j < length; j++) {
|
|
|
|
unibrow::uchar chars2[unibrow::Ecma262UnCanonicalize::kMaxWidth];
|
|
|
|
int length2 = un_canonicalize.get(chars[j], '\0', chars2);
|
|
|
|
CHECK_EQ(length, length2);
|
|
|
|
for (int k = 0; k < length; k++)
|
|
|
|
CHECK_EQ(static_cast<int>(chars[k]), static_cast<int>(chars2[k]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 17:23:57 +00:00
|
|
|
#endif
|
2008-11-26 06:05:07 +00:00
|
|
|
|
2015-01-23 15:19:34 +00:00
|
|
|
static void TestRangeCaseIndependence(Isolate* isolate, CharacterRange input,
|
2008-11-25 11:07:48 +00:00
|
|
|
Vector<CharacterRange> expected) {
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2008-11-25 11:07:48 +00:00
|
|
|
int count = expected.length();
|
2012-06-11 12:42:31 +00:00
|
|
|
ZoneList<CharacterRange>* list =
|
2020-07-09 10:56:29 +00:00
|
|
|
zone.New<ZoneList<CharacterRange>>(count, &zone);
|
2016-01-27 08:24:50 +00:00
|
|
|
list->Add(input, &zone);
|
|
|
|
CharacterRange::AddCaseEquivalents(isolate, &zone, list, false);
|
|
|
|
list->Remove(0); // Remove the input before checking results.
|
2008-11-25 11:07:48 +00:00
|
|
|
CHECK_EQ(count, list->length());
|
|
|
|
for (int i = 0; i < list->length(); i++) {
|
|
|
|
CHECK_EQ(expected[i].from(), list->at(i).from());
|
|
|
|
CHECK_EQ(expected[i].to(), list->at(i).to());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-23 15:19:34 +00:00
|
|
|
static void TestSimpleRangeCaseIndependence(Isolate* isolate,
|
|
|
|
CharacterRange input,
|
2008-11-25 11:07:48 +00:00
|
|
|
CharacterRange expected) {
|
|
|
|
EmbeddedVector<CharacterRange, 1> vector;
|
|
|
|
vector[0] = expected;
|
2015-01-23 15:19:34 +00:00
|
|
|
TestRangeCaseIndependence(isolate, input, vector);
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(CharacterRangeCaseIndependence) {
|
2015-01-23 15:19:34 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
TestSimpleRangeCaseIndependence(isolate, CharacterRange::Singleton('a'),
|
2008-11-25 11:07:48 +00:00
|
|
|
CharacterRange::Singleton('A'));
|
2015-01-23 15:19:34 +00:00
|
|
|
TestSimpleRangeCaseIndependence(isolate, CharacterRange::Singleton('z'),
|
2008-11-25 11:07:48 +00:00
|
|
|
CharacterRange::Singleton('Z'));
|
2019-04-03 17:23:57 +00:00
|
|
|
#ifndef V8_INTL_SUPPORT
|
2016-03-09 15:54:54 +00:00
|
|
|
TestSimpleRangeCaseIndependence(isolate, CharacterRange::Range('a', 'z'),
|
|
|
|
CharacterRange::Range('A', 'Z'));
|
2019-04-03 17:23:57 +00:00
|
|
|
#endif // !V8_INTL_SUPPORT
|
2016-03-09 15:54:54 +00:00
|
|
|
TestSimpleRangeCaseIndependence(isolate, CharacterRange::Range('c', 'f'),
|
|
|
|
CharacterRange::Range('C', 'F'));
|
|
|
|
TestSimpleRangeCaseIndependence(isolate, CharacterRange::Range('a', 'b'),
|
|
|
|
CharacterRange::Range('A', 'B'));
|
|
|
|
TestSimpleRangeCaseIndependence(isolate, CharacterRange::Range('y', 'z'),
|
|
|
|
CharacterRange::Range('Y', 'Z'));
|
2019-04-03 17:23:57 +00:00
|
|
|
#ifndef V8_INTL_SUPPORT
|
2016-03-09 15:54:54 +00:00
|
|
|
TestSimpleRangeCaseIndependence(isolate,
|
|
|
|
CharacterRange::Range('a' - 1, 'z' + 1),
|
|
|
|
CharacterRange::Range('A', 'Z'));
|
|
|
|
TestSimpleRangeCaseIndependence(isolate, CharacterRange::Range('A', 'Z'),
|
|
|
|
CharacterRange::Range('a', 'z'));
|
2019-04-03 17:23:57 +00:00
|
|
|
#endif // !V8_INTL_SUPPORT
|
2016-03-09 15:54:54 +00:00
|
|
|
TestSimpleRangeCaseIndependence(isolate, CharacterRange::Range('C', 'F'),
|
|
|
|
CharacterRange::Range('c', 'f'));
|
2019-04-03 17:23:57 +00:00
|
|
|
#ifndef V8_INTL_SUPPORT
|
2016-03-09 15:54:54 +00:00
|
|
|
TestSimpleRangeCaseIndependence(isolate,
|
|
|
|
CharacterRange::Range('A' - 1, 'Z' + 1),
|
|
|
|
CharacterRange::Range('a', 'z'));
|
2008-11-25 11:07:48 +00:00
|
|
|
// Here we need to add [l-z] to complete the case independence of
|
|
|
|
// [A-Za-z] but we expect [a-z] to be added since we always add a
|
|
|
|
// whole block at a time.
|
2016-03-09 15:54:54 +00:00
|
|
|
TestSimpleRangeCaseIndependence(isolate, CharacterRange::Range('A', 'k'),
|
|
|
|
CharacterRange::Range('a', 'z'));
|
2019-04-03 17:23:57 +00:00
|
|
|
#endif // !V8_INTL_SUPPORT
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 12:40:27 +00:00
|
|
|
static bool InClass(uc32 c,
|
|
|
|
const UnicodeRangeSplitter::CharacterRangeVector* ranges) {
|
2017-10-13 16:33:03 +00:00
|
|
|
if (ranges == nullptr) return false;
|
2019-06-18 12:40:27 +00:00
|
|
|
for (size_t i = 0; i < ranges->size(); i++) {
|
2008-12-01 15:42:35 +00:00
|
|
|
CharacterRange range = ranges->at(i);
|
|
|
|
if (range.from() <= c && c <= range.to())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-21 13:33:02 +00:00
|
|
|
TEST(UnicodeRangeSplitter) {
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2020-07-09 10:56:29 +00:00
|
|
|
ZoneList<CharacterRange>* base = zone.New<ZoneList<CharacterRange>>(1, &zone);
|
2013-06-26 13:36:16 +00:00
|
|
|
base->Add(CharacterRange::Everything(), &zone);
|
2019-06-18 12:40:27 +00:00
|
|
|
UnicodeRangeSplitter splitter(base);
|
2016-01-21 13:33:02 +00:00
|
|
|
// BMP
|
2017-12-02 00:30:37 +00:00
|
|
|
for (uc32 c = 0; c < 0xD800; c++) {
|
2016-01-21 13:33:02 +00:00
|
|
|
CHECK(InClass(c, splitter.bmp()));
|
|
|
|
CHECK(!InClass(c, splitter.lead_surrogates()));
|
|
|
|
CHECK(!InClass(c, splitter.trail_surrogates()));
|
|
|
|
CHECK(!InClass(c, splitter.non_bmp()));
|
|
|
|
}
|
|
|
|
// Lead surrogates
|
2017-12-02 00:30:37 +00:00
|
|
|
for (uc32 c = 0xD800; c < 0xDBFF; c++) {
|
2016-01-21 13:33:02 +00:00
|
|
|
CHECK(!InClass(c, splitter.bmp()));
|
|
|
|
CHECK(InClass(c, splitter.lead_surrogates()));
|
|
|
|
CHECK(!InClass(c, splitter.trail_surrogates()));
|
|
|
|
CHECK(!InClass(c, splitter.non_bmp()));
|
|
|
|
}
|
|
|
|
// Trail surrogates
|
2017-12-02 00:30:37 +00:00
|
|
|
for (uc32 c = 0xDC00; c < 0xDFFF; c++) {
|
2016-01-21 13:33:02 +00:00
|
|
|
CHECK(!InClass(c, splitter.bmp()));
|
|
|
|
CHECK(!InClass(c, splitter.lead_surrogates()));
|
|
|
|
CHECK(InClass(c, splitter.trail_surrogates()));
|
|
|
|
CHECK(!InClass(c, splitter.non_bmp()));
|
|
|
|
}
|
|
|
|
// BMP
|
2017-12-02 00:30:37 +00:00
|
|
|
for (uc32 c = 0xE000; c < 0xFFFF; c++) {
|
2016-01-21 13:33:02 +00:00
|
|
|
CHECK(InClass(c, splitter.bmp()));
|
|
|
|
CHECK(!InClass(c, splitter.lead_surrogates()));
|
|
|
|
CHECK(!InClass(c, splitter.trail_surrogates()));
|
|
|
|
CHECK(!InClass(c, splitter.non_bmp()));
|
|
|
|
}
|
|
|
|
// Non-BMP
|
2017-12-02 00:30:37 +00:00
|
|
|
for (uc32 c = 0x10000; c < 0x10FFFF; c++) {
|
2016-01-21 13:33:02 +00:00
|
|
|
CHECK(!InClass(c, splitter.bmp()));
|
|
|
|
CHECK(!InClass(c, splitter.lead_surrogates()));
|
|
|
|
CHECK(!InClass(c, splitter.trail_surrogates()));
|
|
|
|
CHECK(InClass(c, splitter.non_bmp()));
|
2008-12-01 15:42:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-07 19:01:23 +00:00
|
|
|
TEST(CanonicalizeCharacterSets) {
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2020-07-09 10:56:29 +00:00
|
|
|
ZoneList<CharacterRange>* list = zone.New<ZoneList<CharacterRange>>(4, &zone);
|
2010-01-07 19:01:23 +00:00
|
|
|
CharacterSet set(list);
|
|
|
|
|
2016-03-09 15:54:54 +00:00
|
|
|
list->Add(CharacterRange::Range(10, 20), &zone);
|
|
|
|
list->Add(CharacterRange::Range(30, 40), &zone);
|
|
|
|
list->Add(CharacterRange::Range(50, 60), &zone);
|
2010-01-07 19:01:23 +00:00
|
|
|
set.Canonicalize();
|
2015-12-07 05:36:41 +00:00
|
|
|
CHECK_EQ(3, list->length());
|
|
|
|
CHECK_EQ(10, list->at(0).from());
|
|
|
|
CHECK_EQ(20, list->at(0).to());
|
|
|
|
CHECK_EQ(30, list->at(1).from());
|
|
|
|
CHECK_EQ(40, list->at(1).to());
|
|
|
|
CHECK_EQ(50, list->at(2).from());
|
|
|
|
CHECK_EQ(60, list->at(2).to());
|
2010-01-07 19:01:23 +00:00
|
|
|
|
|
|
|
list->Rewind(0);
|
2016-03-09 15:54:54 +00:00
|
|
|
list->Add(CharacterRange::Range(10, 20), &zone);
|
|
|
|
list->Add(CharacterRange::Range(50, 60), &zone);
|
|
|
|
list->Add(CharacterRange::Range(30, 40), &zone);
|
2010-01-07 19:01:23 +00:00
|
|
|
set.Canonicalize();
|
2015-12-07 05:36:41 +00:00
|
|
|
CHECK_EQ(3, list->length());
|
|
|
|
CHECK_EQ(10, list->at(0).from());
|
|
|
|
CHECK_EQ(20, list->at(0).to());
|
|
|
|
CHECK_EQ(30, list->at(1).from());
|
|
|
|
CHECK_EQ(40, list->at(1).to());
|
|
|
|
CHECK_EQ(50, list->at(2).from());
|
|
|
|
CHECK_EQ(60, list->at(2).to());
|
2010-01-07 19:01:23 +00:00
|
|
|
|
|
|
|
list->Rewind(0);
|
2016-03-09 15:54:54 +00:00
|
|
|
list->Add(CharacterRange::Range(30, 40), &zone);
|
|
|
|
list->Add(CharacterRange::Range(10, 20), &zone);
|
|
|
|
list->Add(CharacterRange::Range(25, 25), &zone);
|
|
|
|
list->Add(CharacterRange::Range(100, 100), &zone);
|
|
|
|
list->Add(CharacterRange::Range(1, 1), &zone);
|
2010-01-07 19:01:23 +00:00
|
|
|
set.Canonicalize();
|
2015-12-07 05:36:41 +00:00
|
|
|
CHECK_EQ(5, list->length());
|
|
|
|
CHECK_EQ(1, list->at(0).from());
|
|
|
|
CHECK_EQ(1, list->at(0).to());
|
|
|
|
CHECK_EQ(10, list->at(1).from());
|
|
|
|
CHECK_EQ(20, list->at(1).to());
|
|
|
|
CHECK_EQ(25, list->at(2).from());
|
|
|
|
CHECK_EQ(25, list->at(2).to());
|
|
|
|
CHECK_EQ(30, list->at(3).from());
|
|
|
|
CHECK_EQ(40, list->at(3).to());
|
|
|
|
CHECK_EQ(100, list->at(4).from());
|
|
|
|
CHECK_EQ(100, list->at(4).to());
|
2010-01-07 19:01:23 +00:00
|
|
|
|
|
|
|
list->Rewind(0);
|
2016-03-09 15:54:54 +00:00
|
|
|
list->Add(CharacterRange::Range(10, 19), &zone);
|
|
|
|
list->Add(CharacterRange::Range(21, 30), &zone);
|
|
|
|
list->Add(CharacterRange::Range(20, 20), &zone);
|
2010-01-07 19:01:23 +00:00
|
|
|
set.Canonicalize();
|
2015-12-07 05:36:41 +00:00
|
|
|
CHECK_EQ(1, list->length());
|
|
|
|
CHECK_EQ(10, list->at(0).from());
|
|
|
|
CHECK_EQ(30, list->at(0).to());
|
2010-01-07 19:01:23 +00:00
|
|
|
}
|
|
|
|
|
2010-01-25 12:56:49 +00:00
|
|
|
|
|
|
|
TEST(CharacterRangeMerge) {
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
2013-06-26 13:36:16 +00:00
|
|
|
ZoneList<CharacterRange> l1(4, &zone);
|
|
|
|
ZoneList<CharacterRange> l2(4, &zone);
|
2010-01-25 12:56:49 +00:00
|
|
|
// Create all combinations of intersections of ranges, both singletons and
|
|
|
|
// longer.
|
|
|
|
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
// The five kinds of singleton intersections:
|
|
|
|
// X
|
|
|
|
// Y - outside before
|
|
|
|
// Y - outside touching start
|
|
|
|
// Y - overlap
|
|
|
|
// Y - outside touching end
|
|
|
|
// Y - outside after
|
|
|
|
|
|
|
|
for (int i = 0; i < 5; i++) {
|
2013-06-26 13:36:16 +00:00
|
|
|
l1.Add(CharacterRange::Singleton(offset + 2), &zone);
|
|
|
|
l2.Add(CharacterRange::Singleton(offset + i), &zone);
|
2010-01-25 12:56:49 +00:00
|
|
|
offset += 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The seven kinds of singleton/non-singleton intersections:
|
|
|
|
// XXX
|
|
|
|
// Y - outside before
|
|
|
|
// Y - outside touching start
|
|
|
|
// Y - inside touching start
|
|
|
|
// Y - entirely inside
|
|
|
|
// Y - inside touching end
|
|
|
|
// Y - outside touching end
|
|
|
|
// Y - disjoint after
|
|
|
|
|
|
|
|
for (int i = 0; i < 7; i++) {
|
2013-06-26 13:36:16 +00:00
|
|
|
l1.Add(CharacterRange::Range(offset + 2, offset + 4), &zone);
|
|
|
|
l2.Add(CharacterRange::Singleton(offset + i), &zone);
|
2010-01-25 12:56:49 +00:00
|
|
|
offset += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The eleven kinds of non-singleton intersections:
|
|
|
|
//
|
|
|
|
// XXXXXXXX
|
|
|
|
// YYYY - outside before.
|
|
|
|
// YYYY - outside touching start.
|
|
|
|
// YYYY - overlapping start
|
|
|
|
// YYYY - inside touching start
|
|
|
|
// YYYY - entirely inside
|
|
|
|
// YYYY - inside touching end
|
|
|
|
// YYYY - overlapping end
|
|
|
|
// YYYY - outside touching end
|
|
|
|
// YYYY - outside after
|
|
|
|
// YYYYYYYY - identical
|
|
|
|
// YYYYYYYYYYYY - containing entirely.
|
|
|
|
|
|
|
|
for (int i = 0; i < 9; i++) {
|
2013-06-26 13:36:16 +00:00
|
|
|
l1.Add(CharacterRange::Range(offset + 6, offset + 15), &zone); // Length 8.
|
|
|
|
l2.Add(CharacterRange::Range(offset + 2 * i, offset + 2 * i + 3), &zone);
|
2010-01-25 12:56:49 +00:00
|
|
|
offset += 22;
|
|
|
|
}
|
2013-06-26 13:36:16 +00:00
|
|
|
l1.Add(CharacterRange::Range(offset + 6, offset + 15), &zone);
|
|
|
|
l2.Add(CharacterRange::Range(offset + 6, offset + 15), &zone);
|
2010-01-25 12:56:49 +00:00
|
|
|
offset += 22;
|
2013-06-26 13:36:16 +00:00
|
|
|
l1.Add(CharacterRange::Range(offset + 6, offset + 15), &zone);
|
|
|
|
l2.Add(CharacterRange::Range(offset + 4, offset + 17), &zone);
|
2010-01-25 12:56:49 +00:00
|
|
|
offset += 22;
|
|
|
|
|
|
|
|
// Different kinds of multi-range overlap:
|
|
|
|
// XXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXX
|
|
|
|
// YYYY Y YYYY Y YYYY Y YYYY Y YYYY Y YYYY Y
|
|
|
|
|
2013-06-26 13:36:16 +00:00
|
|
|
l1.Add(CharacterRange::Range(offset, offset + 21), &zone);
|
|
|
|
l1.Add(CharacterRange::Range(offset + 31, offset + 52), &zone);
|
2010-01-25 12:56:49 +00:00
|
|
|
for (int i = 0; i < 6; i++) {
|
2013-06-26 13:36:16 +00:00
|
|
|
l2.Add(CharacterRange::Range(offset + 2, offset + 5), &zone);
|
|
|
|
l2.Add(CharacterRange::Singleton(offset + 8), &zone);
|
2010-01-25 12:56:49 +00:00
|
|
|
offset += 9;
|
|
|
|
}
|
|
|
|
|
2015-12-07 05:36:41 +00:00
|
|
|
CHECK(CharacterRange::IsCanonical(&l1));
|
|
|
|
CHECK(CharacterRange::IsCanonical(&l2));
|
2010-01-25 12:56:49 +00:00
|
|
|
|
2013-06-26 13:36:16 +00:00
|
|
|
ZoneList<CharacterRange> first_only(4, &zone);
|
|
|
|
ZoneList<CharacterRange> second_only(4, &zone);
|
|
|
|
ZoneList<CharacterRange> both(4, &zone);
|
2010-01-25 12:56:49 +00:00
|
|
|
}
|
2010-01-07 19:01:23 +00:00
|
|
|
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
TEST(Graph) {
|
2010-01-07 19:01:23 +00:00
|
|
|
Execute("\\b\\w+\\b", false, true, true);
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
2015-12-22 09:16:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-10-13 16:33:03 +00:00
|
|
|
int* global_use_counts = nullptr;
|
2015-12-22 09:16:30 +00:00
|
|
|
|
|
|
|
void MockUseCounterCallback(v8::Isolate* isolate,
|
|
|
|
v8::Isolate::UseCounterFeature feature) {
|
|
|
|
++global_use_counts[feature];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-25 11:30:36 +00:00
|
|
|
// Test that ES2015+ RegExp compatibility fixes are in place, that they
|
2015-12-22 09:16:30 +00:00
|
|
|
// are not overly broad, and the appropriate UseCounters are incremented
|
|
|
|
TEST(UseCountRegExp) {
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
LocalContext env;
|
|
|
|
int use_counts[v8::Isolate::kUseCounterFeatureCount] = {};
|
|
|
|
global_use_counts = use_counts;
|
|
|
|
CcTest::isolate()->SetUseCounterCallback(MockUseCounterCallback);
|
|
|
|
|
|
|
|
// Compat fix: RegExp.prototype.sticky == undefined; UseCounter tracks it
|
|
|
|
v8::Local<v8::Value> resultSticky = CompileRun("RegExp.prototype.sticky");
|
|
|
|
CHECK_EQ(1, use_counts[v8::Isolate::kRegExpPrototypeStickyGetter]);
|
|
|
|
CHECK_EQ(0, use_counts[v8::Isolate::kRegExpPrototypeToString]);
|
|
|
|
CHECK(resultSticky->IsUndefined());
|
|
|
|
|
|
|
|
// re.sticky has approriate value and doesn't touch UseCounter
|
|
|
|
v8::Local<v8::Value> resultReSticky = CompileRun("/a/.sticky");
|
|
|
|
CHECK_EQ(1, use_counts[v8::Isolate::kRegExpPrototypeStickyGetter]);
|
|
|
|
CHECK_EQ(0, use_counts[v8::Isolate::kRegExpPrototypeToString]);
|
|
|
|
CHECK(resultReSticky->IsFalse());
|
|
|
|
|
2019-07-25 11:30:36 +00:00
|
|
|
// When the getter is called on another object, throw an exception
|
2015-12-22 09:16:30 +00:00
|
|
|
// and don't increment the UseCounter
|
|
|
|
v8::Local<v8::Value> resultStickyError = CompileRun(
|
|
|
|
"var exception;"
|
|
|
|
"try { "
|
|
|
|
" Object.getOwnPropertyDescriptor(RegExp.prototype, 'sticky')"
|
|
|
|
" .get.call(null);"
|
|
|
|
"} catch (e) {"
|
|
|
|
" exception = e;"
|
|
|
|
"}"
|
|
|
|
"exception");
|
|
|
|
CHECK_EQ(1, use_counts[v8::Isolate::kRegExpPrototypeStickyGetter]);
|
|
|
|
CHECK_EQ(0, use_counts[v8::Isolate::kRegExpPrototypeToString]);
|
|
|
|
CHECK(resultStickyError->IsObject());
|
|
|
|
|
|
|
|
// RegExp.prototype.toString() returns '/(?:)/' as a compatibility fix;
|
|
|
|
// a UseCounter is incremented to track it.
|
|
|
|
v8::Local<v8::Value> resultToString =
|
|
|
|
CompileRun("RegExp.prototype.toString().length");
|
2016-04-04 07:05:47 +00:00
|
|
|
CHECK_EQ(2, use_counts[v8::Isolate::kRegExpPrototypeStickyGetter]);
|
2015-12-22 09:16:30 +00:00
|
|
|
CHECK_EQ(1, use_counts[v8::Isolate::kRegExpPrototypeToString]);
|
|
|
|
CHECK(resultToString->IsInt32());
|
|
|
|
CHECK_EQ(6,
|
|
|
|
resultToString->Int32Value(isolate->GetCurrentContext()).FromJust());
|
|
|
|
|
|
|
|
// .toString() works on normal RegExps
|
|
|
|
v8::Local<v8::Value> resultReToString = CompileRun("/a/.toString().length");
|
2016-04-04 07:05:47 +00:00
|
|
|
CHECK_EQ(2, use_counts[v8::Isolate::kRegExpPrototypeStickyGetter]);
|
2015-12-22 09:16:30 +00:00
|
|
|
CHECK_EQ(1, use_counts[v8::Isolate::kRegExpPrototypeToString]);
|
|
|
|
CHECK(resultReToString->IsInt32());
|
|
|
|
CHECK_EQ(
|
|
|
|
3, resultReToString->Int32Value(isolate->GetCurrentContext()).FromJust());
|
|
|
|
|
|
|
|
// .toString() throws on non-RegExps that aren't RegExp.prototype
|
|
|
|
v8::Local<v8::Value> resultToStringError = CompileRun(
|
|
|
|
"var exception;"
|
|
|
|
"try { RegExp.prototype.toString.call(null) }"
|
|
|
|
"catch (e) { exception = e; }"
|
|
|
|
"exception");
|
2016-04-04 07:05:47 +00:00
|
|
|
CHECK_EQ(2, use_counts[v8::Isolate::kRegExpPrototypeStickyGetter]);
|
2015-12-22 09:16:30 +00:00
|
|
|
CHECK_EQ(1, use_counts[v8::Isolate::kRegExpPrototypeToString]);
|
|
|
|
CHECK(resultToStringError->IsObject());
|
|
|
|
}
|
2016-04-20 13:55:20 +00:00
|
|
|
|
|
|
|
class UncachedExternalString
|
|
|
|
: public v8::String::ExternalOneByteStringResource {
|
|
|
|
public:
|
|
|
|
const char* data() const override { return "abcdefghijklmnopqrstuvwxyz"; }
|
|
|
|
size_t length() const override { return 26; }
|
2018-09-04 13:13:02 +00:00
|
|
|
bool IsCacheable() const override { return false; }
|
2016-04-20 13:55:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(UncachedExternalString) {
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
LocalContext env;
|
|
|
|
v8::Local<v8::String> external =
|
|
|
|
v8::String::NewExternalOneByte(isolate, new UncachedExternalString())
|
|
|
|
.ToLocalChecked();
|
2018-08-23 13:01:35 +00:00
|
|
|
CHECK(v8::Utils::OpenHandle(*external)->map() ==
|
|
|
|
ReadOnlyRoots(CcTest::i_isolate())
|
|
|
|
.uncached_external_one_byte_string_map());
|
2016-04-20 13:55:20 +00:00
|
|
|
v8::Local<v8::Object> global = env->Global();
|
|
|
|
global->Set(env.local(), v8_str("external"), external).FromJust();
|
|
|
|
CompileRun("var re = /y(.)/; re.test('ab');");
|
|
|
|
ExpectString("external.substring(1).match(re)[1]", "z");
|
|
|
|
}
|
2017-08-11 11:22:28 +00:00
|
|
|
|
2019-10-01 11:55:16 +00:00
|
|
|
// Test bytecode peephole optimization
|
|
|
|
|
|
|
|
void CreatePeepholeNoChangeBytecode(RegExpMacroAssembler* m) {
|
|
|
|
Label fail, backtrack;
|
|
|
|
m->PushBacktrack(&fail);
|
|
|
|
m->CheckNotAtStart(0, nullptr);
|
|
|
|
m->LoadCurrentCharacter(2, nullptr);
|
|
|
|
m->CheckNotCharacter('o', nullptr);
|
|
|
|
m->LoadCurrentCharacter(1, nullptr, false);
|
|
|
|
m->CheckNotCharacter('o', nullptr);
|
|
|
|
m->LoadCurrentCharacter(0, nullptr, false);
|
|
|
|
m->CheckNotCharacter('f', nullptr);
|
|
|
|
m->WriteCurrentPositionToRegister(0, 0);
|
|
|
|
m->WriteCurrentPositionToRegister(1, 3);
|
|
|
|
m->AdvanceCurrentPosition(3);
|
|
|
|
m->PushBacktrack(&backtrack);
|
|
|
|
m->Succeed();
|
|
|
|
m->Bind(&backtrack);
|
|
|
|
m->Backtrack();
|
|
|
|
m->Bind(&fail);
|
|
|
|
m->Fail();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PeepholeNoChange) {
|
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
|
|
|
|
RegExpBytecodeGenerator orig(CcTest::i_isolate(), &zone);
|
|
|
|
RegExpBytecodeGenerator opt(CcTest::i_isolate(), &zone);
|
|
|
|
|
|
|
|
CreatePeepholeNoChangeBytecode(&orig);
|
|
|
|
CreatePeepholeNoChangeBytecode(&opt);
|
|
|
|
|
|
|
|
Handle<String> source = factory->NewStringFromStaticChars("^foo");
|
|
|
|
|
|
|
|
i::FLAG_regexp_peephole_optimization = false;
|
|
|
|
Handle<ByteArray> array = Handle<ByteArray>::cast(orig.GetCode(source));
|
|
|
|
int length = array->length();
|
|
|
|
byte* byte_array = array->GetDataStartAddress();
|
|
|
|
|
|
|
|
i::FLAG_regexp_peephole_optimization = true;
|
|
|
|
Handle<ByteArray> array_optimized =
|
|
|
|
Handle<ByteArray>::cast(opt.GetCode(source));
|
|
|
|
byte* byte_array_optimized = array_optimized->GetDataStartAddress();
|
|
|
|
|
|
|
|
CHECK_EQ(0, memcmp(byte_array, byte_array_optimized, length));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreatePeepholeSkipUntilCharBytecode(RegExpMacroAssembler* m) {
|
|
|
|
Label start;
|
|
|
|
m->Bind(&start);
|
|
|
|
m->LoadCurrentCharacter(0, nullptr, true);
|
|
|
|
m->CheckCharacter('x', nullptr);
|
|
|
|
m->AdvanceCurrentPosition(1);
|
|
|
|
m->GoTo(&start);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PeepholeSkipUntilChar) {
|
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
|
|
|
|
RegExpBytecodeGenerator orig(CcTest::i_isolate(), &zone);
|
|
|
|
RegExpBytecodeGenerator opt(CcTest::i_isolate(), &zone);
|
|
|
|
|
|
|
|
CreatePeepholeSkipUntilCharBytecode(&orig);
|
|
|
|
CreatePeepholeSkipUntilCharBytecode(&opt);
|
|
|
|
|
|
|
|
Handle<String> source = factory->NewStringFromStaticChars("dummy");
|
|
|
|
|
|
|
|
i::FLAG_regexp_peephole_optimization = false;
|
|
|
|
Handle<ByteArray> array = Handle<ByteArray>::cast(orig.GetCode(source));
|
|
|
|
int length = array->length();
|
|
|
|
|
|
|
|
i::FLAG_regexp_peephole_optimization = true;
|
|
|
|
Handle<ByteArray> array_optimized =
|
|
|
|
Handle<ByteArray>::cast(opt.GetCode(source));
|
|
|
|
int length_optimized = array_optimized->length();
|
|
|
|
|
|
|
|
int length_expected = RegExpBytecodeLength(BC_LOAD_CURRENT_CHAR) +
|
|
|
|
RegExpBytecodeLength(BC_CHECK_CHAR) +
|
|
|
|
RegExpBytecodeLength(BC_ADVANCE_CP_AND_GOTO) +
|
|
|
|
RegExpBytecodeLength(BC_POP_BT);
|
|
|
|
int length_optimized_expected = RegExpBytecodeLength(BC_SKIP_UNTIL_CHAR) +
|
|
|
|
RegExpBytecodeLength(BC_POP_BT);
|
|
|
|
|
|
|
|
CHECK_EQ(length, length_expected);
|
|
|
|
CHECK_EQ(length_optimized, length_optimized_expected);
|
|
|
|
|
|
|
|
CHECK_EQ(BC_SKIP_UNTIL_CHAR, array_optimized->get(0));
|
|
|
|
CHECK_EQ(BC_POP_BT,
|
|
|
|
array_optimized->get(RegExpBytecodeLength(BC_SKIP_UNTIL_CHAR)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreatePeepholeSkipUntilBitInTableBytecode(RegExpMacroAssembler* m,
|
|
|
|
Factory* factory) {
|
|
|
|
Handle<ByteArray> bit_table = factory->NewByteArray(
|
|
|
|
RegExpMacroAssembler::kTableSize, AllocationType::kOld);
|
|
|
|
for (uint32_t i = 0; i < RegExpMacroAssembler::kTableSize; i++) {
|
|
|
|
bit_table->set(i, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Label start;
|
|
|
|
m->Bind(&start);
|
|
|
|
m->LoadCurrentCharacter(0, nullptr, true);
|
|
|
|
m->CheckBitInTable(bit_table, nullptr);
|
|
|
|
m->AdvanceCurrentPosition(1);
|
|
|
|
m->GoTo(&start);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PeepholeSkipUntilBitInTable) {
|
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
|
|
|
|
RegExpBytecodeGenerator orig(CcTest::i_isolate(), &zone);
|
|
|
|
RegExpBytecodeGenerator opt(CcTest::i_isolate(), &zone);
|
|
|
|
|
|
|
|
CreatePeepholeSkipUntilBitInTableBytecode(&orig, factory);
|
|
|
|
CreatePeepholeSkipUntilBitInTableBytecode(&opt, factory);
|
|
|
|
|
|
|
|
Handle<String> source = factory->NewStringFromStaticChars("dummy");
|
|
|
|
|
|
|
|
i::FLAG_regexp_peephole_optimization = false;
|
|
|
|
Handle<ByteArray> array = Handle<ByteArray>::cast(orig.GetCode(source));
|
|
|
|
int length = array->length();
|
|
|
|
|
|
|
|
i::FLAG_regexp_peephole_optimization = true;
|
|
|
|
Handle<ByteArray> array_optimized =
|
|
|
|
Handle<ByteArray>::cast(opt.GetCode(source));
|
|
|
|
int length_optimized = array_optimized->length();
|
|
|
|
|
|
|
|
int length_expected = RegExpBytecodeLength(BC_LOAD_CURRENT_CHAR) +
|
|
|
|
RegExpBytecodeLength(BC_CHECK_BIT_IN_TABLE) +
|
|
|
|
RegExpBytecodeLength(BC_ADVANCE_CP_AND_GOTO) +
|
|
|
|
RegExpBytecodeLength(BC_POP_BT);
|
|
|
|
int length_optimized_expected =
|
|
|
|
RegExpBytecodeLength(BC_SKIP_UNTIL_BIT_IN_TABLE) +
|
|
|
|
RegExpBytecodeLength(BC_POP_BT);
|
|
|
|
|
|
|
|
CHECK_EQ(length, length_expected);
|
|
|
|
CHECK_EQ(length_optimized, length_optimized_expected);
|
|
|
|
|
|
|
|
CHECK_EQ(BC_SKIP_UNTIL_BIT_IN_TABLE, array_optimized->get(0));
|
|
|
|
CHECK_EQ(BC_POP_BT, array_optimized->get(
|
|
|
|
RegExpBytecodeLength(BC_SKIP_UNTIL_BIT_IN_TABLE)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreatePeepholeSkipUntilCharPosCheckedBytecode(RegExpMacroAssembler* m) {
|
|
|
|
Label start;
|
|
|
|
m->Bind(&start);
|
|
|
|
m->LoadCurrentCharacter(0, nullptr, true, 1, 2);
|
|
|
|
m->CheckCharacter('x', nullptr);
|
|
|
|
m->AdvanceCurrentPosition(1);
|
|
|
|
m->GoTo(&start);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PeepholeSkipUntilCharPosChecked) {
|
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
|
|
|
|
RegExpBytecodeGenerator orig(CcTest::i_isolate(), &zone);
|
|
|
|
RegExpBytecodeGenerator opt(CcTest::i_isolate(), &zone);
|
|
|
|
|
|
|
|
CreatePeepholeSkipUntilCharPosCheckedBytecode(&orig);
|
|
|
|
CreatePeepholeSkipUntilCharPosCheckedBytecode(&opt);
|
|
|
|
|
|
|
|
Handle<String> source = factory->NewStringFromStaticChars("dummy");
|
|
|
|
|
|
|
|
i::FLAG_regexp_peephole_optimization = false;
|
|
|
|
Handle<ByteArray> array = Handle<ByteArray>::cast(orig.GetCode(source));
|
|
|
|
int length = array->length();
|
|
|
|
|
|
|
|
i::FLAG_regexp_peephole_optimization = true;
|
|
|
|
Handle<ByteArray> array_optimized =
|
|
|
|
Handle<ByteArray>::cast(opt.GetCode(source));
|
|
|
|
int length_optimized = array_optimized->length();
|
|
|
|
|
|
|
|
int length_expected = RegExpBytecodeLength(BC_CHECK_CURRENT_POSITION) +
|
|
|
|
RegExpBytecodeLength(BC_LOAD_CURRENT_CHAR_UNCHECKED) +
|
|
|
|
RegExpBytecodeLength(BC_CHECK_CHAR) +
|
|
|
|
RegExpBytecodeLength(BC_ADVANCE_CP_AND_GOTO) +
|
|
|
|
RegExpBytecodeLength(BC_POP_BT);
|
|
|
|
int length_optimized_expected =
|
|
|
|
RegExpBytecodeLength(BC_SKIP_UNTIL_CHAR_POS_CHECKED) +
|
|
|
|
RegExpBytecodeLength(BC_POP_BT);
|
|
|
|
|
|
|
|
CHECK_EQ(length, length_expected);
|
|
|
|
CHECK_EQ(length_optimized, length_optimized_expected);
|
|
|
|
|
|
|
|
CHECK_EQ(BC_SKIP_UNTIL_CHAR_POS_CHECKED, array_optimized->get(0));
|
|
|
|
CHECK_EQ(BC_POP_BT, array_optimized->get(RegExpBytecodeLength(
|
|
|
|
BC_SKIP_UNTIL_CHAR_POS_CHECKED)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreatePeepholeSkipUntilCharAndBytecode(RegExpMacroAssembler* m) {
|
|
|
|
Label start;
|
|
|
|
m->Bind(&start);
|
|
|
|
m->LoadCurrentCharacter(0, nullptr, true, 1, 2);
|
|
|
|
m->CheckCharacterAfterAnd('x', 0xFF, nullptr);
|
|
|
|
m->AdvanceCurrentPosition(1);
|
|
|
|
m->GoTo(&start);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PeepholeSkipUntilCharAnd) {
|
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
|
|
|
|
RegExpBytecodeGenerator orig(CcTest::i_isolate(), &zone);
|
|
|
|
RegExpBytecodeGenerator opt(CcTest::i_isolate(), &zone);
|
|
|
|
|
|
|
|
CreatePeepholeSkipUntilCharAndBytecode(&orig);
|
|
|
|
CreatePeepholeSkipUntilCharAndBytecode(&opt);
|
|
|
|
|
|
|
|
Handle<String> source = factory->NewStringFromStaticChars("dummy");
|
|
|
|
|
|
|
|
i::FLAG_regexp_peephole_optimization = false;
|
|
|
|
Handle<ByteArray> array = Handle<ByteArray>::cast(orig.GetCode(source));
|
|
|
|
int length = array->length();
|
|
|
|
|
|
|
|
i::FLAG_regexp_peephole_optimization = true;
|
|
|
|
Handle<ByteArray> array_optimized =
|
|
|
|
Handle<ByteArray>::cast(opt.GetCode(source));
|
|
|
|
int length_optimized = array_optimized->length();
|
|
|
|
|
|
|
|
int length_expected = RegExpBytecodeLength(BC_CHECK_CURRENT_POSITION) +
|
|
|
|
RegExpBytecodeLength(BC_LOAD_CURRENT_CHAR_UNCHECKED) +
|
|
|
|
RegExpBytecodeLength(BC_AND_CHECK_CHAR) +
|
|
|
|
RegExpBytecodeLength(BC_ADVANCE_CP_AND_GOTO) +
|
|
|
|
RegExpBytecodeLength(BC_POP_BT);
|
|
|
|
int length_optimized_expected = RegExpBytecodeLength(BC_SKIP_UNTIL_CHAR_AND) +
|
|
|
|
RegExpBytecodeLength(BC_POP_BT);
|
|
|
|
|
|
|
|
CHECK_EQ(length, length_expected);
|
|
|
|
CHECK_EQ(length_optimized, length_optimized_expected);
|
|
|
|
|
|
|
|
CHECK_EQ(BC_SKIP_UNTIL_CHAR_AND, array_optimized->get(0));
|
|
|
|
CHECK_EQ(BC_POP_BT,
|
|
|
|
array_optimized->get(RegExpBytecodeLength(BC_SKIP_UNTIL_CHAR_AND)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreatePeepholeSkipUntilCharOrCharBytecode(RegExpMacroAssembler* m) {
|
|
|
|
Label start;
|
|
|
|
m->Bind(&start);
|
|
|
|
m->LoadCurrentCharacter(0, nullptr, true);
|
|
|
|
m->CheckCharacter('x', nullptr);
|
|
|
|
m->CheckCharacter('y', nullptr);
|
|
|
|
m->AdvanceCurrentPosition(1);
|
|
|
|
m->GoTo(&start);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PeepholeSkipUntilCharOrChar) {
|
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
|
|
|
|
RegExpBytecodeGenerator orig(CcTest::i_isolate(), &zone);
|
|
|
|
RegExpBytecodeGenerator opt(CcTest::i_isolate(), &zone);
|
|
|
|
|
|
|
|
CreatePeepholeSkipUntilCharOrCharBytecode(&orig);
|
|
|
|
CreatePeepholeSkipUntilCharOrCharBytecode(&opt);
|
|
|
|
|
|
|
|
Handle<String> source = factory->NewStringFromStaticChars("dummy");
|
|
|
|
|
|
|
|
i::FLAG_regexp_peephole_optimization = false;
|
|
|
|
Handle<ByteArray> array = Handle<ByteArray>::cast(orig.GetCode(source));
|
|
|
|
int length = array->length();
|
|
|
|
|
|
|
|
i::FLAG_regexp_peephole_optimization = true;
|
|
|
|
Handle<ByteArray> array_optimized =
|
|
|
|
Handle<ByteArray>::cast(opt.GetCode(source));
|
|
|
|
int length_optimized = array_optimized->length();
|
|
|
|
|
|
|
|
int length_expected = RegExpBytecodeLength(BC_LOAD_CURRENT_CHAR) +
|
|
|
|
RegExpBytecodeLength(BC_CHECK_CHAR) +
|
|
|
|
RegExpBytecodeLength(BC_CHECK_CHAR) +
|
|
|
|
RegExpBytecodeLength(BC_ADVANCE_CP_AND_GOTO) +
|
|
|
|
RegExpBytecodeLength(BC_POP_BT);
|
|
|
|
int length_optimized_expected =
|
|
|
|
RegExpBytecodeLength(BC_SKIP_UNTIL_CHAR_OR_CHAR) +
|
|
|
|
RegExpBytecodeLength(BC_POP_BT);
|
|
|
|
|
|
|
|
CHECK_EQ(length, length_expected);
|
|
|
|
CHECK_EQ(length_optimized, length_optimized_expected);
|
|
|
|
|
|
|
|
CHECK_EQ(BC_SKIP_UNTIL_CHAR_OR_CHAR, array_optimized->get(0));
|
|
|
|
CHECK_EQ(BC_POP_BT, array_optimized->get(
|
|
|
|
RegExpBytecodeLength(BC_SKIP_UNTIL_CHAR_OR_CHAR)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreatePeepholeSkipUntilGtOrNotBitInTableBytecode(RegExpMacroAssembler* m,
|
|
|
|
Factory* factory) {
|
|
|
|
Handle<ByteArray> bit_table = factory->NewByteArray(
|
|
|
|
RegExpMacroAssembler::kTableSize, AllocationType::kOld);
|
|
|
|
for (uint32_t i = 0; i < RegExpMacroAssembler::kTableSize; i++) {
|
|
|
|
bit_table->set(i, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Label start, end, advance;
|
|
|
|
m->Bind(&start);
|
|
|
|
m->LoadCurrentCharacter(0, nullptr, true);
|
|
|
|
m->CheckCharacterGT('x', nullptr);
|
|
|
|
m->CheckBitInTable(bit_table, &advance);
|
|
|
|
m->GoTo(&end);
|
|
|
|
m->Bind(&advance);
|
|
|
|
m->AdvanceCurrentPosition(1);
|
|
|
|
m->GoTo(&start);
|
|
|
|
m->Bind(&end);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PeepholeSkipUntilGtOrNotBitInTable) {
|
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
|
|
|
|
RegExpBytecodeGenerator orig(CcTest::i_isolate(), &zone);
|
|
|
|
RegExpBytecodeGenerator opt(CcTest::i_isolate(), &zone);
|
|
|
|
|
|
|
|
CreatePeepholeSkipUntilGtOrNotBitInTableBytecode(&orig, factory);
|
|
|
|
CreatePeepholeSkipUntilGtOrNotBitInTableBytecode(&opt, factory);
|
|
|
|
|
|
|
|
Handle<String> source = factory->NewStringFromStaticChars("dummy");
|
|
|
|
|
|
|
|
i::FLAG_regexp_peephole_optimization = false;
|
|
|
|
Handle<ByteArray> array = Handle<ByteArray>::cast(orig.GetCode(source));
|
|
|
|
int length = array->length();
|
|
|
|
|
|
|
|
i::FLAG_regexp_peephole_optimization = true;
|
|
|
|
Handle<ByteArray> array_optimized =
|
|
|
|
Handle<ByteArray>::cast(opt.GetCode(source));
|
|
|
|
int length_optimized = array_optimized->length();
|
|
|
|
|
|
|
|
int length_expected = RegExpBytecodeLength(BC_LOAD_CURRENT_CHAR) +
|
|
|
|
RegExpBytecodeLength(BC_CHECK_GT) +
|
|
|
|
RegExpBytecodeLength(BC_CHECK_BIT_IN_TABLE) +
|
|
|
|
RegExpBytecodeLength(BC_GOTO) +
|
|
|
|
RegExpBytecodeLength(BC_ADVANCE_CP_AND_GOTO) +
|
|
|
|
RegExpBytecodeLength(BC_POP_BT);
|
|
|
|
int length_optimized_expected =
|
|
|
|
RegExpBytecodeLength(BC_SKIP_UNTIL_GT_OR_NOT_BIT_IN_TABLE) +
|
|
|
|
RegExpBytecodeLength(BC_POP_BT);
|
|
|
|
|
|
|
|
CHECK_EQ(length, length_expected);
|
|
|
|
CHECK_EQ(length_optimized, length_optimized_expected);
|
|
|
|
|
|
|
|
CHECK_EQ(BC_SKIP_UNTIL_GT_OR_NOT_BIT_IN_TABLE, array_optimized->get(0));
|
|
|
|
CHECK_EQ(BC_POP_BT, array_optimized->get(RegExpBytecodeLength(
|
|
|
|
BC_SKIP_UNTIL_GT_OR_NOT_BIT_IN_TABLE)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreatePeepholeLabelFixupsInsideBytecode(RegExpMacroAssembler* m,
|
|
|
|
Label* dummy_before,
|
|
|
|
Label* dummy_after,
|
|
|
|
Label* dummy_inside) {
|
|
|
|
Label loop;
|
|
|
|
m->Bind(dummy_before);
|
|
|
|
m->LoadCurrentCharacter(0, dummy_before);
|
|
|
|
m->CheckCharacter('a', dummy_after);
|
|
|
|
m->CheckCharacter('b', dummy_inside);
|
|
|
|
m->Bind(&loop);
|
|
|
|
m->LoadCurrentCharacter(0, nullptr, true);
|
|
|
|
m->CheckCharacter('x', nullptr);
|
|
|
|
m->Bind(dummy_inside);
|
|
|
|
m->CheckCharacter('y', nullptr);
|
|
|
|
m->AdvanceCurrentPosition(1);
|
|
|
|
m->GoTo(&loop);
|
|
|
|
m->Bind(dummy_after);
|
|
|
|
m->LoadCurrentCharacter(0, dummy_before);
|
|
|
|
m->CheckCharacter('a', dummy_after);
|
|
|
|
m->CheckCharacter('b', dummy_inside);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PeepholeLabelFixupsInside) {
|
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
|
|
|
|
RegExpBytecodeGenerator orig(CcTest::i_isolate(), &zone);
|
|
|
|
RegExpBytecodeGenerator opt(CcTest::i_isolate(), &zone);
|
|
|
|
|
|
|
|
{
|
|
|
|
Label dummy_before, dummy_after, dummy_inside;
|
|
|
|
CreatePeepholeLabelFixupsInsideBytecode(&opt, &dummy_before, &dummy_after,
|
|
|
|
&dummy_inside);
|
|
|
|
}
|
|
|
|
Label dummy_before, dummy_after, dummy_inside;
|
|
|
|
CreatePeepholeLabelFixupsInsideBytecode(&orig, &dummy_before, &dummy_after,
|
|
|
|
&dummy_inside);
|
|
|
|
|
|
|
|
CHECK_EQ(0x00, dummy_before.pos());
|
|
|
|
CHECK_EQ(0x28, dummy_inside.pos());
|
|
|
|
CHECK_EQ(0x38, dummy_after.pos());
|
|
|
|
|
|
|
|
const Label* labels[] = {&dummy_before, &dummy_after, &dummy_inside};
|
|
|
|
const int label_positions[4][3] = {
|
|
|
|
{0x04, 0x3C}, // dummy_before
|
|
|
|
{0x0C, 0x44}, // dummy after
|
|
|
|
{0x14, 0x4C} // dummy inside
|
|
|
|
};
|
|
|
|
|
|
|
|
Handle<String> source = factory->NewStringFromStaticChars("dummy");
|
|
|
|
|
|
|
|
i::FLAG_regexp_peephole_optimization = false;
|
|
|
|
Handle<ByteArray> array = Handle<ByteArray>::cast(orig.GetCode(source));
|
|
|
|
|
|
|
|
for (int label_idx = 0; label_idx < 3; label_idx++) {
|
|
|
|
for (int pos_idx = 0; pos_idx < 2; pos_idx++) {
|
|
|
|
CHECK_EQ(labels[label_idx]->pos(),
|
|
|
|
array->get(label_positions[label_idx][pos_idx]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i::FLAG_regexp_peephole_optimization = true;
|
|
|
|
Handle<ByteArray> array_optimized =
|
|
|
|
Handle<ByteArray>::cast(opt.GetCode(source));
|
|
|
|
|
|
|
|
const int pos_fixups[] = {
|
|
|
|
0, // Position before optimization should be unchanged.
|
|
|
|
4, // Position after first replacement should be 4 (optimized size (20) -
|
|
|
|
// original size (32) + preserve length (16)).
|
|
|
|
};
|
|
|
|
const int target_fixups[] = {
|
|
|
|
0, // dummy_before should be unchanged
|
|
|
|
4, // dummy_inside should be 4
|
|
|
|
4 // dummy_after should be 4
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int label_idx = 0; label_idx < 3; label_idx++) {
|
|
|
|
for (int pos_idx = 0; pos_idx < 2; pos_idx++) {
|
|
|
|
int label_pos = label_positions[label_idx][pos_idx] + pos_fixups[pos_idx];
|
|
|
|
int jump_address = *reinterpret_cast<uint32_t*>(
|
|
|
|
array_optimized->GetDataStartAddress() + label_pos);
|
|
|
|
int expected_jump_address =
|
|
|
|
labels[label_idx]->pos() + target_fixups[label_idx];
|
|
|
|
CHECK_EQ(expected_jump_address, jump_address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreatePeepholeLabelFixupsComplexBytecode(RegExpMacroAssembler* m,
|
|
|
|
Label* dummy_before,
|
|
|
|
Label* dummy_between,
|
|
|
|
Label* dummy_after,
|
|
|
|
Label* dummy_inside) {
|
|
|
|
Label loop1, loop2;
|
|
|
|
m->Bind(dummy_before);
|
|
|
|
m->LoadCurrentCharacter(0, dummy_before);
|
|
|
|
m->CheckCharacter('a', dummy_between);
|
|
|
|
m->CheckCharacter('b', dummy_after);
|
|
|
|
m->CheckCharacter('c', dummy_inside);
|
|
|
|
m->Bind(&loop1);
|
|
|
|
m->LoadCurrentCharacter(0, nullptr, true);
|
|
|
|
m->CheckCharacter('x', nullptr);
|
|
|
|
m->CheckCharacter('y', nullptr);
|
|
|
|
m->AdvanceCurrentPosition(1);
|
|
|
|
m->GoTo(&loop1);
|
|
|
|
m->Bind(dummy_between);
|
|
|
|
m->LoadCurrentCharacter(0, dummy_before);
|
|
|
|
m->CheckCharacter('a', dummy_between);
|
|
|
|
m->CheckCharacter('b', dummy_after);
|
|
|
|
m->CheckCharacter('c', dummy_inside);
|
|
|
|
m->Bind(&loop2);
|
|
|
|
m->LoadCurrentCharacter(0, nullptr, true);
|
|
|
|
m->CheckCharacter('x', nullptr);
|
|
|
|
m->Bind(dummy_inside);
|
|
|
|
m->CheckCharacter('y', nullptr);
|
|
|
|
m->AdvanceCurrentPosition(1);
|
|
|
|
m->GoTo(&loop2);
|
|
|
|
m->Bind(dummy_after);
|
|
|
|
m->LoadCurrentCharacter(0, dummy_before);
|
|
|
|
m->CheckCharacter('a', dummy_between);
|
|
|
|
m->CheckCharacter('b', dummy_after);
|
|
|
|
m->CheckCharacter('c', dummy_inside);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PeepholeLabelFixupsComplex) {
|
|
|
|
Zone zone(CcTest::i_isolate()->allocator(), ZONE_NAME);
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
|
|
|
|
RegExpBytecodeGenerator orig(CcTest::i_isolate(), &zone);
|
|
|
|
RegExpBytecodeGenerator opt(CcTest::i_isolate(), &zone);
|
|
|
|
|
|
|
|
{
|
|
|
|
Label dummy_before, dummy_between, dummy_after, dummy_inside;
|
|
|
|
CreatePeepholeLabelFixupsComplexBytecode(
|
|
|
|
&opt, &dummy_before, &dummy_between, &dummy_after, &dummy_inside);
|
|
|
|
}
|
|
|
|
Label dummy_before, dummy_between, dummy_after, dummy_inside;
|
|
|
|
CreatePeepholeLabelFixupsComplexBytecode(&orig, &dummy_before, &dummy_between,
|
|
|
|
&dummy_after, &dummy_inside);
|
|
|
|
|
|
|
|
CHECK_EQ(0x00, dummy_before.pos());
|
|
|
|
CHECK_EQ(0x40, dummy_between.pos());
|
|
|
|
CHECK_EQ(0x70, dummy_inside.pos());
|
|
|
|
CHECK_EQ(0x80, dummy_after.pos());
|
|
|
|
|
|
|
|
const Label* labels[] = {&dummy_before, &dummy_between, &dummy_after,
|
|
|
|
&dummy_inside};
|
|
|
|
const int label_positions[4][3] = {
|
|
|
|
{0x04, 0x44, 0x84}, // dummy_before
|
|
|
|
{0x0C, 0x4C, 0x8C}, // dummy between
|
|
|
|
{0x14, 0x54, 0x94}, // dummy after
|
|
|
|
{0x1C, 0x5C, 0x9C} // dummy inside
|
|
|
|
};
|
|
|
|
|
|
|
|
Handle<String> source = factory->NewStringFromStaticChars("dummy");
|
|
|
|
|
|
|
|
i::FLAG_regexp_peephole_optimization = false;
|
|
|
|
Handle<ByteArray> array = Handle<ByteArray>::cast(orig.GetCode(source));
|
|
|
|
|
|
|
|
for (int label_idx = 0; label_idx < 4; label_idx++) {
|
|
|
|
for (int pos_idx = 0; pos_idx < 3; pos_idx++) {
|
|
|
|
CHECK_EQ(labels[label_idx]->pos(),
|
|
|
|
array->get(label_positions[label_idx][pos_idx]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i::FLAG_regexp_peephole_optimization = true;
|
|
|
|
Handle<ByteArray> array_optimized =
|
|
|
|
Handle<ByteArray>::cast(opt.GetCode(source));
|
|
|
|
|
|
|
|
const int pos_fixups[] = {
|
|
|
|
0, // Position before optimization should be unchanged.
|
|
|
|
-12, // Position after first replacement should be -12 (optimized size =
|
|
|
|
// 20 - 32 = original size).
|
|
|
|
-8 // Position after second replacement should be -8 (-12 from first
|
|
|
|
// optimization -12 from second optimization + 16 preserved
|
|
|
|
// bytecodes).
|
|
|
|
};
|
|
|
|
const int target_fixups[] = {
|
|
|
|
0, // dummy_before should be unchanged
|
|
|
|
-12, // dummy_between should be -12
|
|
|
|
-8, // dummy_inside should be -8
|
|
|
|
-8 // dummy_after should be -8
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int label_idx = 0; label_idx < 4; label_idx++) {
|
|
|
|
for (int pos_idx = 0; pos_idx < 3; pos_idx++) {
|
|
|
|
int label_pos = label_positions[label_idx][pos_idx] + pos_fixups[pos_idx];
|
|
|
|
int jump_address = *reinterpret_cast<uint32_t*>(
|
|
|
|
array_optimized->GetDataStartAddress() + label_pos);
|
|
|
|
int expected_jump_address =
|
|
|
|
labels[label_idx]->pos() + target_fixups[label_idx];
|
|
|
|
CHECK_EQ(expected_jump_address, jump_address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 06:31:16 +00:00
|
|
|
TEST(UnicodePropertyEscapeCodeSize) {
|
|
|
|
i::FlagScope<bool> f(&v8::internal::FLAG_regexp_tier_up, false);
|
|
|
|
|
|
|
|
LocalContext env;
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
i::Handle<i::JSRegExp> re = Utils::OpenHandle(
|
|
|
|
*CompileRun("const r = /\\p{L}\\p{L}\\p{L}/u; r.exec('\\u200b'); r;")
|
|
|
|
.As<v8::RegExp>());
|
|
|
|
|
2020-04-30 07:21:13 +00:00
|
|
|
static constexpr int kMaxSize = 200 * KB;
|
2020-04-29 06:31:16 +00:00
|
|
|
static constexpr bool kIsNotLatin1 = false;
|
|
|
|
Object maybe_code = re->Code(kIsNotLatin1);
|
|
|
|
Object maybe_bytecode = re->Bytecode(kIsNotLatin1);
|
|
|
|
if (maybe_bytecode.IsByteArray()) {
|
|
|
|
// On x64, excessive inlining produced >250KB.
|
|
|
|
CHECK_LT(ByteArray::cast(maybe_bytecode).Size(), kMaxSize);
|
|
|
|
} else if (maybe_code.IsCode()) {
|
|
|
|
// On x64, excessive inlining produced >360KB.
|
|
|
|
CHECK_LT(Code::cast(maybe_code).Size(), kMaxSize);
|
|
|
|
CHECK_EQ(Code::cast(maybe_code).kind(), Code::REGEXP);
|
|
|
|
} else {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 08:20:31 +00:00
|
|
|
#undef CHECK_PARSE_ERROR
|
|
|
|
#undef CHECK_SIMPLE
|
|
|
|
#undef CHECK_MIN_MAX
|
|
|
|
|
2017-09-21 03:29:52 +00:00
|
|
|
} // namespace test_regexp
|
2017-08-11 11:22:28 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|