mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | |
| 29 | #include <stdlib.h> |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 30 | |
| 31 | #include "v8.h" |
| 32 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 33 | #include "ast.h" |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 34 | #include "char-predicates-inl.h" |
| 35 | #include "cctest.h" |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 36 | #include "jsregexp.h" |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 37 | #include "parser.h" |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 38 | #include "regexp-macro-assembler.h" |
| 39 | #include "regexp-macro-assembler-irregexp.h" |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 40 | #include "string-stream.h" |
| 41 | #include "zone-inl.h" |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 42 | #ifdef V8_INTERPRETED_REGEXP |
| 43 | #include "interpreter-irregexp.h" |
| 44 | #else // V8_INTERPRETED_REGEXP |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 45 | #include "macro-assembler.h" |
| 46 | #include "code.h" |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 47 | #ifdef V8_TARGET_ARCH_ARM |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 48 | #include "arm/assembler-arm.h" |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 49 | #include "arm/macro-assembler-arm.h" |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 50 | #include "arm/regexp-macro-assembler-arm.h" |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 51 | #endif |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 52 | #ifdef V8_TARGET_ARCH_MIPS |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 53 | #include "mips/assembler-mips.h" |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 54 | #include "mips/macro-assembler-mips.h" |
| 55 | #include "mips/regexp-macro-assembler-mips.h" |
| 56 | #endif |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 57 | #ifdef V8_TARGET_ARCH_X64 |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 58 | #include "x64/assembler-x64.h" |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 59 | #include "x64/macro-assembler-x64.h" |
| 60 | #include "x64/regexp-macro-assembler-x64.h" |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 61 | #endif |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 62 | #ifdef V8_TARGET_ARCH_IA32 |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 63 | #include "ia32/assembler-ia32.h" |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 64 | #include "ia32/macro-assembler-ia32.h" |
| 65 | #include "ia32/regexp-macro-assembler-ia32.h" |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 66 | #endif |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 67 | #endif // V8_INTERPRETED_REGEXP |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 68 | |
| 69 | using namespace v8::internal; |
| 70 | |
| 71 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 72 | static bool CheckParse(const char* input) { |
| 73 | V8::Initialize(NULL); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 74 | v8::HandleScope scope(v8::Isolate::GetCurrent()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 75 | ZoneScope zone_scope(Isolate::Current()->runtime_zone(), DELETE_ON_EXIT); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 76 | FlatStringReader reader(Isolate::Current(), CStrVector(input)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 77 | RegExpCompileData result; |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 78 | return v8::internal::RegExpParser::ParseRegExp( |
| 79 | &reader, false, &result, Isolate::Current()->runtime_zone()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 83 | static SmartArrayPointer<const char> Parse(const char* input) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 84 | V8::Initialize(NULL); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 85 | v8::HandleScope scope(v8::Isolate::GetCurrent()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 86 | ZoneScope zone_scope(Isolate::Current()->runtime_zone(), DELETE_ON_EXIT); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 87 | FlatStringReader reader(Isolate::Current(), CStrVector(input)); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 88 | RegExpCompileData result; |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 89 | CHECK(v8::internal::RegExpParser::ParseRegExp( |
| 90 | &reader, false, &result, Isolate::Current()->runtime_zone())); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 91 | CHECK(result.tree != NULL); |
| 92 | CHECK(result.error.is_null()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 93 | SmartArrayPointer<const char> output = |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 94 | result.tree->ToString(Isolate::Current()->runtime_zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 95 | return output; |
| 96 | } |
| 97 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 98 | static bool CheckSimple(const char* input) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 99 | V8::Initialize(NULL); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 100 | v8::HandleScope scope(v8::Isolate::GetCurrent()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 101 | ZoneScope zone_scope(Isolate::Current()->runtime_zone(), DELETE_ON_EXIT); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 102 | FlatStringReader reader(Isolate::Current(), CStrVector(input)); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 103 | RegExpCompileData result; |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 104 | CHECK(v8::internal::RegExpParser::ParseRegExp( |
| 105 | &reader, false, &result, Isolate::Current()->runtime_zone())); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 106 | CHECK(result.tree != NULL); |
| 107 | CHECK(result.error.is_null()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 108 | return result.simple; |
| 109 | } |
| 110 | |
| 111 | struct MinMaxPair { |
| 112 | int min_match; |
| 113 | int max_match; |
| 114 | }; |
| 115 | |
| 116 | static MinMaxPair CheckMinMaxMatch(const char* input) { |
| 117 | V8::Initialize(NULL); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 118 | v8::HandleScope scope(v8::Isolate::GetCurrent()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 119 | ZoneScope zone_scope(Isolate::Current()->runtime_zone(), DELETE_ON_EXIT); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 120 | FlatStringReader reader(Isolate::Current(), CStrVector(input)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 121 | RegExpCompileData result; |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 122 | CHECK(v8::internal::RegExpParser::ParseRegExp( |
| 123 | &reader, false, &result, Isolate::Current()->runtime_zone())); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 124 | CHECK(result.tree != NULL); |
| 125 | CHECK(result.error.is_null()); |
| 126 | int min_match = result.tree->min_match(); |
| 127 | int max_match = result.tree->max_match(); |
| 128 | MinMaxPair pair = { min_match, max_match }; |
| 129 | return pair; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 133 | #define CHECK_PARSE_ERROR(input) CHECK(!CheckParse(input)) |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 134 | #define CHECK_PARSE_EQ(input, expected) CHECK_EQ(expected, *Parse(input)) |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 135 | #define CHECK_SIMPLE(input, simple) CHECK_EQ(simple, CheckSimple(input)); |
| 136 | #define CHECK_MIN_MAX(input, min, max) \ |
| 137 | { MinMaxPair min_max = CheckMinMaxMatch(input); \ |
| 138 | CHECK_EQ(min, min_max.min_match); \ |
| 139 | CHECK_EQ(max, min_max.max_match); \ |
| 140 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 141 | |
| 142 | TEST(Parser) { |
| 143 | V8::Initialize(NULL); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 144 | |
| 145 | CHECK_PARSE_ERROR("?"); |
| 146 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 147 | CHECK_PARSE_EQ("abc", "'abc'"); |
| 148 | CHECK_PARSE_EQ("", "%"); |
| 149 | CHECK_PARSE_EQ("abc|def", "(| 'abc' 'def')"); |
| 150 | CHECK_PARSE_EQ("abc|def|ghi", "(| 'abc' 'def' 'ghi')"); |
| 151 | CHECK_PARSE_EQ("^xxx$", "(: @^i 'xxx' @$i)"); |
| 152 | CHECK_PARSE_EQ("ab\\b\\d\\bcd", "(: 'ab' @b [0-9] @b 'cd')"); |
| 153 | CHECK_PARSE_EQ("\\w|\\d", "(| [0-9 A-Z _ a-z] [0-9])"); |
| 154 | CHECK_PARSE_EQ("a*", "(# 0 - g 'a')"); |
| 155 | CHECK_PARSE_EQ("a*?", "(# 0 - n 'a')"); |
| 156 | CHECK_PARSE_EQ("abc+", "(: 'ab' (# 1 - g 'c'))"); |
| 157 | CHECK_PARSE_EQ("abc+?", "(: 'ab' (# 1 - n 'c'))"); |
| 158 | CHECK_PARSE_EQ("xyz?", "(: 'xy' (# 0 1 g 'z'))"); |
| 159 | CHECK_PARSE_EQ("xyz??", "(: 'xy' (# 0 1 n 'z'))"); |
| 160 | CHECK_PARSE_EQ("xyz{0,1}", "(: 'xy' (# 0 1 g 'z'))"); |
| 161 | CHECK_PARSE_EQ("xyz{0,1}?", "(: 'xy' (# 0 1 n 'z'))"); |
| 162 | CHECK_PARSE_EQ("xyz{93}", "(: 'xy' (# 93 93 g 'z'))"); |
| 163 | CHECK_PARSE_EQ("xyz{93}?", "(: 'xy' (# 93 93 n 'z'))"); |
| 164 | CHECK_PARSE_EQ("xyz{1,32}", "(: 'xy' (# 1 32 g 'z'))"); |
| 165 | CHECK_PARSE_EQ("xyz{1,32}?", "(: 'xy' (# 1 32 n 'z'))"); |
| 166 | CHECK_PARSE_EQ("xyz{1,}", "(: 'xy' (# 1 - g 'z'))"); |
| 167 | CHECK_PARSE_EQ("xyz{1,}?", "(: 'xy' (# 1 - n 'z'))"); |
| 168 | CHECK_PARSE_EQ("a\\fb\\nc\\rd\\te\\vf", "'a\\x0cb\\x0ac\\x0dd\\x09e\\x0bf'"); |
| 169 | CHECK_PARSE_EQ("a\\nb\\bc", "(: 'a\\x0ab' @b 'c')"); |
| 170 | CHECK_PARSE_EQ("(?:foo)", "'foo'"); |
| 171 | CHECK_PARSE_EQ("(?: foo )", "' foo '"); |
| 172 | CHECK_PARSE_EQ("(foo|bar|baz)", "(^ (| 'foo' 'bar' 'baz'))"); |
| 173 | CHECK_PARSE_EQ("foo|(bar|baz)|quux", "(| 'foo' (^ (| 'bar' 'baz')) 'quux')"); |
| 174 | CHECK_PARSE_EQ("foo(?=bar)baz", "(: 'foo' (-> + 'bar') 'baz')"); |
| 175 | CHECK_PARSE_EQ("foo(?!bar)baz", "(: 'foo' (-> - 'bar') 'baz')"); |
| 176 | CHECK_PARSE_EQ("()", "(^ %)"); |
| 177 | CHECK_PARSE_EQ("(?=)", "(-> + %)"); |
| 178 | CHECK_PARSE_EQ("[]", "^[\\x00-\\uffff]"); // Doesn't compile on windows |
| 179 | CHECK_PARSE_EQ("[^]", "[\\x00-\\uffff]"); // \uffff isn't in codepage 1252 |
| 180 | CHECK_PARSE_EQ("[x]", "[x]"); |
| 181 | CHECK_PARSE_EQ("[xyz]", "[x y z]"); |
| 182 | CHECK_PARSE_EQ("[a-zA-Z0-9]", "[a-z A-Z 0-9]"); |
| 183 | CHECK_PARSE_EQ("[-123]", "[- 1 2 3]"); |
| 184 | CHECK_PARSE_EQ("[^123]", "^[1 2 3]"); |
| 185 | CHECK_PARSE_EQ("]", "']'"); |
| 186 | CHECK_PARSE_EQ("}", "'}'"); |
| 187 | CHECK_PARSE_EQ("[a-b-c]", "[a-b - c]"); |
| 188 | CHECK_PARSE_EQ("[\\d]", "[0-9]"); |
| 189 | CHECK_PARSE_EQ("[x\\dz]", "[x 0-9 z]"); |
lrn@chromium.org | 14a7035 | 2010-12-15 12:43:21 +0000 | [diff] [blame] | 190 | CHECK_PARSE_EQ("[\\d-z]", "[0-9 - z]"); |
| 191 | CHECK_PARSE_EQ("[\\d-\\d]", "[0-9 - 0-9]"); |
| 192 | CHECK_PARSE_EQ("[z-\\d]", "[z - 0-9]"); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 193 | // Control character outside character class. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 194 | CHECK_PARSE_EQ("\\cj\\cJ\\ci\\cI\\ck\\cK", |
| 195 | "'\\x0a\\x0a\\x09\\x09\\x0b\\x0b'"); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 196 | CHECK_PARSE_EQ("\\c!", "'\\c!'"); |
| 197 | CHECK_PARSE_EQ("\\c_", "'\\c_'"); |
| 198 | CHECK_PARSE_EQ("\\c~", "'\\c~'"); |
| 199 | CHECK_PARSE_EQ("\\c1", "'\\c1'"); |
| 200 | // Control character inside character class. |
| 201 | CHECK_PARSE_EQ("[\\c!]", "[\\ c !]"); |
| 202 | CHECK_PARSE_EQ("[\\c_]", "[\\x1f]"); |
| 203 | CHECK_PARSE_EQ("[\\c~]", "[\\ c ~]"); |
| 204 | CHECK_PARSE_EQ("[\\ca]", "[\\x01]"); |
| 205 | CHECK_PARSE_EQ("[\\cz]", "[\\x1a]"); |
| 206 | CHECK_PARSE_EQ("[\\cA]", "[\\x01]"); |
| 207 | CHECK_PARSE_EQ("[\\cZ]", "[\\x1a]"); |
| 208 | CHECK_PARSE_EQ("[\\c1]", "[\\x11]"); |
| 209 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 210 | CHECK_PARSE_EQ("[a\\]c]", "[a ] c]"); |
| 211 | CHECK_PARSE_EQ("\\[\\]\\{\\}\\(\\)\\%\\^\\#\\ ", "'[]{}()%^# '"); |
| 212 | CHECK_PARSE_EQ("[\\[\\]\\{\\}\\(\\)\\%\\^\\#\\ ]", "[[ ] { } ( ) % ^ # ]"); |
| 213 | CHECK_PARSE_EQ("\\0", "'\\x00'"); |
| 214 | CHECK_PARSE_EQ("\\8", "'8'"); |
| 215 | CHECK_PARSE_EQ("\\9", "'9'"); |
| 216 | CHECK_PARSE_EQ("\\11", "'\\x09'"); |
| 217 | CHECK_PARSE_EQ("\\11a", "'\\x09a'"); |
| 218 | CHECK_PARSE_EQ("\\011", "'\\x09'"); |
| 219 | CHECK_PARSE_EQ("\\00011", "'\\x0011'"); |
| 220 | CHECK_PARSE_EQ("\\118", "'\\x098'"); |
| 221 | CHECK_PARSE_EQ("\\111", "'I'"); |
| 222 | CHECK_PARSE_EQ("\\1111", "'I1'"); |
| 223 | CHECK_PARSE_EQ("(x)(x)(x)\\1", "(: (^ 'x') (^ 'x') (^ 'x') (<- 1))"); |
| 224 | CHECK_PARSE_EQ("(x)(x)(x)\\2", "(: (^ 'x') (^ 'x') (^ 'x') (<- 2))"); |
| 225 | CHECK_PARSE_EQ("(x)(x)(x)\\3", "(: (^ 'x') (^ 'x') (^ 'x') (<- 3))"); |
| 226 | CHECK_PARSE_EQ("(x)(x)(x)\\4", "(: (^ 'x') (^ 'x') (^ 'x') '\\x04')"); |
| 227 | CHECK_PARSE_EQ("(x)(x)(x)\\1*", "(: (^ 'x') (^ 'x') (^ 'x')" |
| 228 | " (# 0 - g (<- 1)))"); |
| 229 | CHECK_PARSE_EQ("(x)(x)(x)\\2*", "(: (^ 'x') (^ 'x') (^ 'x')" |
| 230 | " (# 0 - g (<- 2)))"); |
| 231 | CHECK_PARSE_EQ("(x)(x)(x)\\3*", "(: (^ 'x') (^ 'x') (^ 'x')" |
| 232 | " (# 0 - g (<- 3)))"); |
| 233 | CHECK_PARSE_EQ("(x)(x)(x)\\4*", "(: (^ 'x') (^ 'x') (^ 'x')" |
| 234 | " (# 0 - g '\\x04'))"); |
| 235 | CHECK_PARSE_EQ("(x)(x)(x)(x)(x)(x)(x)(x)(x)(x)\\10", |
| 236 | "(: (^ 'x') (^ 'x') (^ 'x') (^ 'x') (^ 'x') (^ 'x')" |
| 237 | " (^ 'x') (^ 'x') (^ 'x') (^ 'x') (<- 10))"); |
| 238 | CHECK_PARSE_EQ("(x)(x)(x)(x)(x)(x)(x)(x)(x)(x)\\11", |
| 239 | "(: (^ 'x') (^ 'x') (^ 'x') (^ 'x') (^ 'x') (^ 'x')" |
| 240 | " (^ 'x') (^ 'x') (^ 'x') (^ 'x') '\\x09')"); |
| 241 | CHECK_PARSE_EQ("(a)\\1", "(: (^ 'a') (<- 1))"); |
| 242 | CHECK_PARSE_EQ("(a\\1)", "(^ 'a')"); |
| 243 | CHECK_PARSE_EQ("(\\1a)", "(^ 'a')"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 244 | CHECK_PARSE_EQ("(?=a)?a", "'a'"); |
| 245 | CHECK_PARSE_EQ("(?=a){0,10}a", "'a'"); |
| 246 | CHECK_PARSE_EQ("(?=a){1,10}a", "(: (-> + 'a') 'a')"); |
| 247 | CHECK_PARSE_EQ("(?=a){9,10}a", "(: (-> + 'a') 'a')"); |
| 248 | CHECK_PARSE_EQ("(?!a)?a", "'a'"); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 249 | CHECK_PARSE_EQ("\\1(a)", "(^ 'a')"); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 250 | CHECK_PARSE_EQ("(?!(a))\\1", "(: (-> - (^ 'a')) (<- 1))"); |
| 251 | CHECK_PARSE_EQ("(?!\\1(a\\1)\\1)\\1", "(: (-> - (: (^ 'a') (<- 1))) (<- 1))"); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 252 | CHECK_PARSE_EQ("[\\0]", "[\\x00]"); |
| 253 | CHECK_PARSE_EQ("[\\11]", "[\\x09]"); |
| 254 | CHECK_PARSE_EQ("[\\11a]", "[\\x09 a]"); |
| 255 | CHECK_PARSE_EQ("[\\011]", "[\\x09]"); |
| 256 | CHECK_PARSE_EQ("[\\00011]", "[\\x00 1 1]"); |
| 257 | CHECK_PARSE_EQ("[\\118]", "[\\x09 8]"); |
| 258 | CHECK_PARSE_EQ("[\\111]", "[I]"); |
| 259 | CHECK_PARSE_EQ("[\\1111]", "[I 1]"); |
| 260 | CHECK_PARSE_EQ("\\x34", "'\x34'"); |
| 261 | CHECK_PARSE_EQ("\\x60", "'\x60'"); |
| 262 | CHECK_PARSE_EQ("\\x3z", "'x3z'"); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 263 | CHECK_PARSE_EQ("\\c", "'\\c'"); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 264 | CHECK_PARSE_EQ("\\u0034", "'\x34'"); |
| 265 | CHECK_PARSE_EQ("\\u003z", "'u003z'"); |
| 266 | CHECK_PARSE_EQ("foo[z]*", "(: 'foo' (# 0 - g [z]))"); |
| 267 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 268 | CHECK_SIMPLE("", false); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 269 | CHECK_SIMPLE("a", true); |
| 270 | CHECK_SIMPLE("a|b", false); |
| 271 | CHECK_SIMPLE("a\\n", false); |
| 272 | CHECK_SIMPLE("^a", false); |
| 273 | CHECK_SIMPLE("a$", false); |
| 274 | CHECK_SIMPLE("a\\b!", false); |
| 275 | CHECK_SIMPLE("a\\Bb", false); |
| 276 | CHECK_SIMPLE("a*", false); |
| 277 | CHECK_SIMPLE("a*?", false); |
| 278 | CHECK_SIMPLE("a?", false); |
| 279 | CHECK_SIMPLE("a??", false); |
| 280 | CHECK_SIMPLE("a{0,1}?", false); |
| 281 | CHECK_SIMPLE("a{1,1}?", false); |
| 282 | CHECK_SIMPLE("a{1,2}?", false); |
| 283 | CHECK_SIMPLE("a+?", false); |
| 284 | CHECK_SIMPLE("(a)", false); |
| 285 | CHECK_SIMPLE("(a)\\1", false); |
| 286 | CHECK_SIMPLE("(\\1a)", false); |
| 287 | CHECK_SIMPLE("\\1(a)", false); |
| 288 | CHECK_SIMPLE("a\\s", false); |
| 289 | CHECK_SIMPLE("a\\S", false); |
| 290 | CHECK_SIMPLE("a\\d", false); |
| 291 | CHECK_SIMPLE("a\\D", false); |
| 292 | CHECK_SIMPLE("a\\w", false); |
| 293 | CHECK_SIMPLE("a\\W", false); |
| 294 | CHECK_SIMPLE("a.", false); |
| 295 | CHECK_SIMPLE("a\\q", false); |
| 296 | CHECK_SIMPLE("a[a]", false); |
| 297 | CHECK_SIMPLE("a[^a]", false); |
| 298 | CHECK_SIMPLE("a[a-z]", false); |
| 299 | CHECK_SIMPLE("a[\\q]", false); |
| 300 | CHECK_SIMPLE("a(?:b)", false); |
| 301 | CHECK_SIMPLE("a(?=b)", false); |
| 302 | CHECK_SIMPLE("a(?!b)", false); |
| 303 | CHECK_SIMPLE("\\x60", false); |
| 304 | CHECK_SIMPLE("\\u0060", false); |
| 305 | CHECK_SIMPLE("\\cA", false); |
| 306 | CHECK_SIMPLE("\\q", false); |
| 307 | CHECK_SIMPLE("\\1112", false); |
| 308 | CHECK_SIMPLE("\\0", false); |
| 309 | CHECK_SIMPLE("(a)\\1", false); |
| 310 | CHECK_SIMPLE("(?=a)?a", false); |
| 311 | CHECK_SIMPLE("(?!a)?a\\1", false); |
| 312 | CHECK_SIMPLE("(?:(?=a))a\\1", false); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 313 | |
| 314 | CHECK_PARSE_EQ("a{}", "'a{}'"); |
| 315 | CHECK_PARSE_EQ("a{,}", "'a{,}'"); |
| 316 | CHECK_PARSE_EQ("a{", "'a{'"); |
| 317 | CHECK_PARSE_EQ("a{z}", "'a{z}'"); |
| 318 | CHECK_PARSE_EQ("a{1z}", "'a{1z}'"); |
| 319 | CHECK_PARSE_EQ("a{12z}", "'a{12z}'"); |
| 320 | CHECK_PARSE_EQ("a{12,", "'a{12,'"); |
| 321 | CHECK_PARSE_EQ("a{12,3b", "'a{12,3b'"); |
| 322 | CHECK_PARSE_EQ("{}", "'{}'"); |
| 323 | CHECK_PARSE_EQ("{,}", "'{,}'"); |
| 324 | CHECK_PARSE_EQ("{", "'{'"); |
| 325 | CHECK_PARSE_EQ("{z}", "'{z}'"); |
| 326 | CHECK_PARSE_EQ("{1z}", "'{1z}'"); |
| 327 | CHECK_PARSE_EQ("{12z}", "'{12z}'"); |
| 328 | CHECK_PARSE_EQ("{12,", "'{12,'"); |
| 329 | CHECK_PARSE_EQ("{12,3b", "'{12,3b'"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 330 | |
| 331 | CHECK_MIN_MAX("a", 1, 1); |
| 332 | CHECK_MIN_MAX("abc", 3, 3); |
| 333 | CHECK_MIN_MAX("a[bc]d", 3, 3); |
| 334 | CHECK_MIN_MAX("a|bc", 1, 2); |
| 335 | CHECK_MIN_MAX("ab|c", 1, 2); |
| 336 | CHECK_MIN_MAX("a||bc", 0, 2); |
| 337 | CHECK_MIN_MAX("|", 0, 0); |
| 338 | CHECK_MIN_MAX("(?:ab)", 2, 2); |
| 339 | CHECK_MIN_MAX("(?:ab|cde)", 2, 3); |
| 340 | CHECK_MIN_MAX("(?:ab)|cde", 2, 3); |
| 341 | CHECK_MIN_MAX("(ab)", 2, 2); |
| 342 | CHECK_MIN_MAX("(ab|cde)", 2, 3); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 343 | CHECK_MIN_MAX("(ab)\\1", 2, 4); |
| 344 | CHECK_MIN_MAX("(ab|cde)\\1", 2, 6); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 345 | CHECK_MIN_MAX("(?:ab)?", 0, 2); |
| 346 | CHECK_MIN_MAX("(?:ab)*", 0, RegExpTree::kInfinity); |
| 347 | CHECK_MIN_MAX("(?:ab)+", 2, RegExpTree::kInfinity); |
| 348 | CHECK_MIN_MAX("a?", 0, 1); |
| 349 | CHECK_MIN_MAX("a*", 0, RegExpTree::kInfinity); |
| 350 | CHECK_MIN_MAX("a+", 1, RegExpTree::kInfinity); |
| 351 | CHECK_MIN_MAX("a??", 0, 1); |
| 352 | CHECK_MIN_MAX("a*?", 0, RegExpTree::kInfinity); |
| 353 | CHECK_MIN_MAX("a+?", 1, RegExpTree::kInfinity); |
| 354 | CHECK_MIN_MAX("(?:a?)?", 0, 1); |
| 355 | CHECK_MIN_MAX("(?:a*)?", 0, RegExpTree::kInfinity); |
| 356 | CHECK_MIN_MAX("(?:a+)?", 0, RegExpTree::kInfinity); |
| 357 | CHECK_MIN_MAX("(?:a?)+", 0, RegExpTree::kInfinity); |
| 358 | CHECK_MIN_MAX("(?:a*)+", 0, RegExpTree::kInfinity); |
| 359 | CHECK_MIN_MAX("(?:a+)+", 1, RegExpTree::kInfinity); |
| 360 | CHECK_MIN_MAX("(?:a?)*", 0, RegExpTree::kInfinity); |
| 361 | CHECK_MIN_MAX("(?:a*)*", 0, RegExpTree::kInfinity); |
| 362 | CHECK_MIN_MAX("(?:a+)*", 0, RegExpTree::kInfinity); |
| 363 | CHECK_MIN_MAX("a{0}", 0, 0); |
| 364 | CHECK_MIN_MAX("(?:a+){0}", 0, 0); |
| 365 | CHECK_MIN_MAX("(?:a+){0,0}", 0, 0); |
| 366 | CHECK_MIN_MAX("a*b", 1, RegExpTree::kInfinity); |
| 367 | CHECK_MIN_MAX("a+b", 2, RegExpTree::kInfinity); |
| 368 | CHECK_MIN_MAX("a*b|c", 1, RegExpTree::kInfinity); |
| 369 | CHECK_MIN_MAX("a+b|c", 1, RegExpTree::kInfinity); |
| 370 | CHECK_MIN_MAX("(?:a{5,1000000}){3,1000000}", 15, RegExpTree::kInfinity); |
| 371 | CHECK_MIN_MAX("(?:ab){4,7}", 8, 14); |
| 372 | CHECK_MIN_MAX("a\\bc", 2, 2); |
| 373 | CHECK_MIN_MAX("a\\Bc", 2, 2); |
| 374 | CHECK_MIN_MAX("a\\sc", 3, 3); |
| 375 | CHECK_MIN_MAX("a\\Sc", 3, 3); |
| 376 | CHECK_MIN_MAX("a(?=b)c", 2, 2); |
| 377 | CHECK_MIN_MAX("a(?=bbb|bb)c", 2, 2); |
| 378 | CHECK_MIN_MAX("a(?!bbb|bb)c", 2, 2); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | TEST(ParserRegression) { |
| 382 | CHECK_PARSE_EQ("[A-Z$-][x]", "(! [A-Z $ -] [x])"); |
| 383 | CHECK_PARSE_EQ("a{3,4*}", "(: 'a{3,' (# 0 - g '4') '}')"); |
| 384 | CHECK_PARSE_EQ("{", "'{'"); |
| 385 | CHECK_PARSE_EQ("a|", "(| 'a' %)"); |
| 386 | } |
| 387 | |
| 388 | static void ExpectError(const char* input, |
| 389 | const char* expected) { |
| 390 | V8::Initialize(NULL); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 391 | v8::HandleScope scope(v8::Isolate::GetCurrent()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 392 | ZoneScope zone_scope(Isolate::Current()->runtime_zone(), DELETE_ON_EXIT); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 393 | FlatStringReader reader(Isolate::Current(), CStrVector(input)); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 394 | RegExpCompileData result; |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 395 | CHECK(!v8::internal::RegExpParser::ParseRegExp( |
| 396 | &reader, false, &result, Isolate::Current()->runtime_zone())); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 397 | CHECK(result.tree == NULL); |
| 398 | CHECK(!result.error.is_null()); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 399 | SmartArrayPointer<char> str = result.error->ToCString(ALLOW_NULLS); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 400 | CHECK_EQ(expected, *str); |
| 401 | } |
| 402 | |
| 403 | |
| 404 | TEST(Errors) { |
| 405 | V8::Initialize(NULL); |
| 406 | const char* kEndBackslash = "\\ at end of pattern"; |
| 407 | ExpectError("\\", kEndBackslash); |
| 408 | const char* kUnterminatedGroup = "Unterminated group"; |
| 409 | ExpectError("(foo", kUnterminatedGroup); |
| 410 | const char* kInvalidGroup = "Invalid group"; |
| 411 | ExpectError("(?", kInvalidGroup); |
| 412 | const char* kUnterminatedCharacterClass = "Unterminated character class"; |
| 413 | ExpectError("[", kUnterminatedCharacterClass); |
| 414 | ExpectError("[a-", kUnterminatedCharacterClass); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 415 | const char* kNothingToRepeat = "Nothing to repeat"; |
| 416 | ExpectError("*", kNothingToRepeat); |
| 417 | ExpectError("?", kNothingToRepeat); |
| 418 | ExpectError("+", kNothingToRepeat); |
| 419 | ExpectError("{1}", kNothingToRepeat); |
| 420 | ExpectError("{1,2}", kNothingToRepeat); |
| 421 | ExpectError("{1,}", kNothingToRepeat); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 422 | |
| 423 | // Check that we don't allow more than kMaxCapture captures |
| 424 | const int kMaxCaptures = 1 << 16; // Must match RegExpParser::kMaxCaptures. |
| 425 | const char* kTooManyCaptures = "Too many captures"; |
| 426 | HeapStringAllocator allocator; |
| 427 | StringStream accumulator(&allocator); |
| 428 | for (int i = 0; i <= kMaxCaptures; i++) { |
| 429 | accumulator.Add("()"); |
| 430 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 431 | SmartArrayPointer<const char> many_captures(accumulator.ToCString()); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 432 | ExpectError(*many_captures, kTooManyCaptures); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | |
| 436 | static bool IsDigit(uc16 c) { |
| 437 | return ('0' <= c && c <= '9'); |
| 438 | } |
| 439 | |
| 440 | |
| 441 | static bool NotDigit(uc16 c) { |
| 442 | return !IsDigit(c); |
| 443 | } |
| 444 | |
| 445 | |
| 446 | static bool IsWhiteSpace(uc16 c) { |
| 447 | switch (c) { |
| 448 | case 0x09: |
| 449 | case 0x0A: |
| 450 | case 0x0B: |
| 451 | case 0x0C: |
| 452 | case 0x0d: |
| 453 | case 0x20: |
| 454 | case 0xA0: |
| 455 | case 0x2028: |
| 456 | case 0x2029: |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 457 | case 0xFEFF: |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 458 | return true; |
| 459 | default: |
| 460 | return unibrow::Space::Is(c); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | |
| 465 | static bool NotWhiteSpace(uc16 c) { |
| 466 | return !IsWhiteSpace(c); |
| 467 | } |
| 468 | |
| 469 | |
| 470 | static bool NotWord(uc16 c) { |
| 471 | return !IsRegExpWord(c); |
| 472 | } |
| 473 | |
| 474 | |
| 475 | static void TestCharacterClassEscapes(uc16 c, bool (pred)(uc16 c)) { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 476 | ZoneScope scope(Isolate::Current()->runtime_zone(), DELETE_ON_EXIT); |
| 477 | Zone* zone = Isolate::Current()->runtime_zone(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 478 | ZoneList<CharacterRange>* ranges = |
| 479 | new(zone) ZoneList<CharacterRange>(2, zone); |
| 480 | CharacterRange::AddClassEscape(c, ranges, zone); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 481 | for (unsigned i = 0; i < (1 << 16); i++) { |
| 482 | bool in_class = false; |
| 483 | for (int j = 0; !in_class && j < ranges->length(); j++) { |
| 484 | CharacterRange& range = ranges->at(j); |
| 485 | in_class = (range.from() <= i && i <= range.to()); |
| 486 | } |
| 487 | CHECK_EQ(pred(i), in_class); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | |
| 492 | TEST(CharacterClassEscapes) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 493 | v8::internal::V8::Initialize(NULL); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 494 | TestCharacterClassEscapes('.', IsRegExpNewline); |
| 495 | TestCharacterClassEscapes('d', IsDigit); |
| 496 | TestCharacterClassEscapes('D', NotDigit); |
| 497 | TestCharacterClassEscapes('s', IsWhiteSpace); |
| 498 | TestCharacterClassEscapes('S', NotWhiteSpace); |
| 499 | TestCharacterClassEscapes('w', IsRegExpWord); |
| 500 | TestCharacterClassEscapes('W', NotWord); |
| 501 | } |
| 502 | |
| 503 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 504 | static RegExpNode* Compile(const char* input, bool multiline, bool is_ascii) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 505 | V8::Initialize(NULL); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 506 | Isolate* isolate = Isolate::Current(); |
| 507 | FlatStringReader reader(isolate, CStrVector(input)); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 508 | RegExpCompileData compile_data; |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 509 | if (!v8::internal::RegExpParser::ParseRegExp(&reader, multiline, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 510 | &compile_data, |
| 511 | isolate->runtime_zone())) |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 512 | return NULL; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 513 | Handle<String> pattern = isolate->factory()-> |
| 514 | NewStringFromUtf8(CStrVector(input)); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 515 | Handle<String> sample_subject = |
| 516 | isolate->factory()->NewStringFromUtf8(CStrVector("")); |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame] | 517 | RegExpEngine::Compile(&compile_data, |
| 518 | false, |
| 519 | false, |
| 520 | multiline, |
| 521 | pattern, |
| 522 | sample_subject, |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 523 | is_ascii, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 524 | isolate->runtime_zone()); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 525 | return compile_data.node; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | |
| 529 | static void Execute(const char* input, |
| 530 | bool multiline, |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 531 | bool is_ascii, |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 532 | bool dot_output = false) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 533 | v8::HandleScope scope(v8::Isolate::GetCurrent()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 534 | ZoneScope zone_scope(Isolate::Current()->runtime_zone(), DELETE_ON_EXIT); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 535 | RegExpNode* node = Compile(input, multiline, is_ascii); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 536 | USE(node); |
| 537 | #ifdef DEBUG |
| 538 | if (dot_output) { |
| 539 | RegExpEngine::DotPrint(input, node, false); |
| 540 | exit(0); |
| 541 | } |
| 542 | #endif // DEBUG |
| 543 | } |
| 544 | |
| 545 | |
| 546 | class TestConfig { |
| 547 | public: |
| 548 | typedef int Key; |
| 549 | typedef int Value; |
| 550 | static const int kNoKey; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 551 | static int NoValue() { return 0; } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 552 | static inline int Compare(int a, int b) { |
| 553 | if (a < b) |
| 554 | return -1; |
| 555 | else if (a > b) |
| 556 | return 1; |
| 557 | else |
| 558 | return 0; |
| 559 | } |
| 560 | }; |
| 561 | |
| 562 | |
| 563 | const int TestConfig::kNoKey = 0; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 564 | |
| 565 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 566 | static unsigned PseudoRandom(int i, int j) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 567 | return ~(~((i * 781) ^ (j * 329))); |
| 568 | } |
| 569 | |
| 570 | |
| 571 | TEST(SplayTreeSimple) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 572 | v8::internal::V8::Initialize(NULL); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 573 | static const unsigned kLimit = 1000; |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 574 | ZoneScope zone_scope(Isolate::Current()->runtime_zone(), DELETE_ON_EXIT); |
| 575 | ZoneSplayTree<TestConfig> tree(Isolate::Current()->runtime_zone()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 576 | bool seen[kLimit]; |
| 577 | for (unsigned i = 0; i < kLimit; i++) seen[i] = false; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 578 | #define CHECK_MAPS_EQUAL() do { \ |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 579 | for (unsigned k = 0; k < kLimit; k++) \ |
| 580 | CHECK_EQ(seen[k], tree.Find(k, &loc)); \ |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 581 | } while (false) |
| 582 | for (int i = 0; i < 50; i++) { |
| 583 | for (int j = 0; j < 50; j++) { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 584 | unsigned next = PseudoRandom(i, j) % kLimit; |
| 585 | if (seen[next]) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 586 | // We've already seen this one. Check the value and remove |
| 587 | // it. |
| 588 | ZoneSplayTree<TestConfig>::Locator loc; |
| 589 | CHECK(tree.Find(next, &loc)); |
| 590 | CHECK_EQ(next, loc.key()); |
| 591 | CHECK_EQ(3 * next, loc.value()); |
| 592 | tree.Remove(next); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 593 | seen[next] = false; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 594 | CHECK_MAPS_EQUAL(); |
| 595 | } else { |
| 596 | // Check that it wasn't there already and then add it. |
| 597 | ZoneSplayTree<TestConfig>::Locator loc; |
| 598 | CHECK(!tree.Find(next, &loc)); |
| 599 | CHECK(tree.Insert(next, &loc)); |
| 600 | CHECK_EQ(next, loc.key()); |
| 601 | loc.set_value(3 * next); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 602 | seen[next] = true; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 603 | CHECK_MAPS_EQUAL(); |
| 604 | } |
| 605 | int val = PseudoRandom(j, i) % kLimit; |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 606 | if (seen[val]) { |
| 607 | ZoneSplayTree<TestConfig>::Locator loc; |
| 608 | CHECK(tree.FindGreatestLessThan(val, &loc)); |
| 609 | CHECK_EQ(loc.key(), val); |
| 610 | break; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 611 | } |
| 612 | val = PseudoRandom(i + j, i - j) % kLimit; |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 613 | if (seen[val]) { |
| 614 | ZoneSplayTree<TestConfig>::Locator loc; |
| 615 | CHECK(tree.FindLeastGreaterThan(val, &loc)); |
| 616 | CHECK_EQ(loc.key(), val); |
| 617 | break; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | |
| 624 | TEST(DispatchTableConstruction) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 625 | v8::internal::V8::Initialize(NULL); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 626 | // Initialize test data. |
| 627 | static const int kLimit = 1000; |
| 628 | static const int kRangeCount = 8; |
| 629 | static const int kRangeSize = 16; |
| 630 | uc16 ranges[kRangeCount][2 * kRangeSize]; |
| 631 | for (int i = 0; i < kRangeCount; i++) { |
| 632 | Vector<uc16> range(ranges[i], 2 * kRangeSize); |
| 633 | for (int j = 0; j < 2 * kRangeSize; j++) { |
| 634 | range[j] = PseudoRandom(i + 25, j + 87) % kLimit; |
| 635 | } |
| 636 | range.Sort(); |
| 637 | for (int j = 1; j < 2 * kRangeSize; j++) { |
| 638 | CHECK(range[j-1] <= range[j]); |
| 639 | } |
| 640 | } |
| 641 | // Enter test data into dispatch table. |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 642 | ZoneScope zone_scope(Isolate::Current()->runtime_zone(), DELETE_ON_EXIT); |
| 643 | DispatchTable table(Isolate::Current()->runtime_zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 644 | for (int i = 0; i < kRangeCount; i++) { |
| 645 | uc16* range = ranges[i]; |
| 646 | for (int j = 0; j < 2 * kRangeSize; j += 2) |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 647 | table.AddRange(CharacterRange(range[j], range[j + 1]), i, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 648 | Isolate::Current()->runtime_zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 649 | } |
| 650 | // Check that the table looks as we would expect |
| 651 | for (int p = 0; p < kLimit; p++) { |
| 652 | OutSet* outs = table.Get(p); |
| 653 | for (int j = 0; j < kRangeCount; j++) { |
| 654 | uc16* range = ranges[j]; |
| 655 | bool is_on = false; |
| 656 | for (int k = 0; !is_on && (k < 2 * kRangeSize); k += 2) |
| 657 | is_on = (range[k] <= p && p <= range[k + 1]); |
| 658 | CHECK_EQ(is_on, outs->Get(j)); |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 663 | // Test of debug-only syntax. |
| 664 | #ifdef DEBUG |
| 665 | |
| 666 | TEST(ParsePossessiveRepetition) { |
| 667 | bool old_flag_value = FLAG_regexp_possessive_quantifier; |
| 668 | |
| 669 | // Enable possessive quantifier syntax. |
| 670 | FLAG_regexp_possessive_quantifier = true; |
| 671 | |
| 672 | CHECK_PARSE_EQ("a*+", "(# 0 - p 'a')"); |
| 673 | CHECK_PARSE_EQ("a++", "(# 1 - p 'a')"); |
| 674 | CHECK_PARSE_EQ("a?+", "(# 0 1 p 'a')"); |
| 675 | CHECK_PARSE_EQ("a{10,20}+", "(# 10 20 p 'a')"); |
| 676 | CHECK_PARSE_EQ("za{10,20}+b", "(: 'z' (# 10 20 p 'a') 'b')"); |
| 677 | |
| 678 | // Disable possessive quantifier syntax. |
| 679 | FLAG_regexp_possessive_quantifier = false; |
| 680 | |
| 681 | CHECK_PARSE_ERROR("a*+"); |
| 682 | CHECK_PARSE_ERROR("a++"); |
| 683 | CHECK_PARSE_ERROR("a?+"); |
| 684 | CHECK_PARSE_ERROR("a{10,20}+"); |
| 685 | CHECK_PARSE_ERROR("a{10,20}+b"); |
| 686 | |
| 687 | FLAG_regexp_possessive_quantifier = old_flag_value; |
| 688 | } |
| 689 | |
| 690 | #endif |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 691 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 692 | // Tests of interpreter. |
| 693 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 694 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 695 | #ifndef V8_INTERPRETED_REGEXP |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 696 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 697 | #if V8_TARGET_ARCH_IA32 |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 698 | typedef RegExpMacroAssemblerIA32 ArchRegExpMacroAssembler; |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 699 | #elif V8_TARGET_ARCH_X64 |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 700 | typedef RegExpMacroAssemblerX64 ArchRegExpMacroAssembler; |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 701 | #elif V8_TARGET_ARCH_ARM |
| 702 | typedef RegExpMacroAssemblerARM ArchRegExpMacroAssembler; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 703 | #elif V8_TARGET_ARCH_MIPS |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 704 | typedef RegExpMacroAssemblerMIPS ArchRegExpMacroAssembler; |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 705 | #endif |
| 706 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 707 | class ContextInitializer { |
| 708 | public: |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 709 | ContextInitializer() |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 710 | : env_(), |
| 711 | scope_(v8::Isolate::GetCurrent()), |
| 712 | zone_(Isolate::Current()->runtime_zone(), DELETE_ON_EXIT) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 713 | env_ = v8::Context::New(); |
| 714 | env_->Enter(); |
| 715 | } |
| 716 | ~ContextInitializer() { |
| 717 | env_->Exit(); |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 718 | env_.Dispose(env_->GetIsolate()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 719 | } |
| 720 | private: |
| 721 | v8::Persistent<v8::Context> env_; |
| 722 | v8::HandleScope scope_; |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 723 | v8::internal::ZoneScope zone_; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 724 | }; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 725 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 726 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 727 | static ArchRegExpMacroAssembler::Result Execute(Code* code, |
| 728 | String* input, |
| 729 | int start_offset, |
| 730 | const byte* input_start, |
| 731 | const byte* input_end, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 732 | int* captures) { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 733 | return NativeRegExpMacroAssembler::Execute( |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 734 | code, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 735 | input, |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 736 | start_offset, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 737 | input_start, |
| 738 | input_end, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 739 | captures, |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame] | 740 | 0, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 741 | Isolate::Current()); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 742 | } |
| 743 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 744 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 745 | TEST(MacroAssemblerNativeSuccess) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 746 | v8::V8::Initialize(); |
| 747 | ContextInitializer initializer; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 748 | Factory* factory = Isolate::Current()->factory(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 749 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 750 | ArchRegExpMacroAssembler m(NativeRegExpMacroAssembler::ASCII, 4, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 751 | Isolate::Current()->runtime_zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 752 | |
| 753 | m.Succeed(); |
| 754 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 755 | Handle<String> source = factory->NewStringFromAscii(CStrVector("")); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 756 | Handle<Object> code_object = m.GetCode(source); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 757 | Handle<Code> code = Handle<Code>::cast(code_object); |
| 758 | |
| 759 | int captures[4] = {42, 37, 87, 117}; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 760 | Handle<String> input = factory->NewStringFromAscii(CStrVector("foofoo")); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 761 | Handle<SeqOneByteString> seq_input = Handle<SeqOneByteString>::cast(input); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 762 | const byte* start_adr = |
| 763 | reinterpret_cast<const byte*>(seq_input->GetCharsAddress()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 764 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 765 | NativeRegExpMacroAssembler::Result result = |
| 766 | Execute(*code, |
| 767 | *input, |
| 768 | 0, |
| 769 | start_adr, |
| 770 | start_adr + seq_input->length(), |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 771 | captures); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 772 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 773 | CHECK_EQ(NativeRegExpMacroAssembler::SUCCESS, result); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 774 | CHECK_EQ(-1, captures[0]); |
| 775 | CHECK_EQ(-1, captures[1]); |
| 776 | CHECK_EQ(-1, captures[2]); |
| 777 | CHECK_EQ(-1, captures[3]); |
| 778 | } |
| 779 | |
| 780 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 781 | TEST(MacroAssemblerNativeSimple) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 782 | v8::V8::Initialize(); |
| 783 | ContextInitializer initializer; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 784 | Factory* factory = Isolate::Current()->factory(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 785 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 786 | ArchRegExpMacroAssembler m(NativeRegExpMacroAssembler::ASCII, 4, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 787 | Isolate::Current()->runtime_zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 788 | |
| 789 | uc16 foo_chars[3] = {'f', 'o', 'o'}; |
| 790 | Vector<const uc16> foo(foo_chars, 3); |
| 791 | |
| 792 | Label fail; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 793 | m.CheckCharacters(foo, 0, &fail, true); |
| 794 | m.WriteCurrentPositionToRegister(0, 0); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 795 | m.AdvanceCurrentPosition(3); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 796 | m.WriteCurrentPositionToRegister(1, 0); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 797 | m.Succeed(); |
| 798 | m.Bind(&fail); |
| 799 | m.Fail(); |
| 800 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 801 | Handle<String> source = factory->NewStringFromAscii(CStrVector("^foo")); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 802 | Handle<Object> code_object = m.GetCode(source); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 803 | Handle<Code> code = Handle<Code>::cast(code_object); |
| 804 | |
| 805 | int captures[4] = {42, 37, 87, 117}; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 806 | Handle<String> input = factory->NewStringFromAscii(CStrVector("foofoo")); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 807 | Handle<SeqOneByteString> seq_input = Handle<SeqOneByteString>::cast(input); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 808 | Address start_adr = seq_input->GetCharsAddress(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 809 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 810 | NativeRegExpMacroAssembler::Result result = |
| 811 | Execute(*code, |
| 812 | *input, |
| 813 | 0, |
| 814 | start_adr, |
| 815 | start_adr + input->length(), |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 816 | captures); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 817 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 818 | CHECK_EQ(NativeRegExpMacroAssembler::SUCCESS, result); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 819 | CHECK_EQ(0, captures[0]); |
| 820 | CHECK_EQ(3, captures[1]); |
| 821 | CHECK_EQ(-1, captures[2]); |
| 822 | CHECK_EQ(-1, captures[3]); |
| 823 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 824 | input = factory->NewStringFromAscii(CStrVector("barbarbar")); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 825 | seq_input = Handle<SeqOneByteString>::cast(input); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 826 | start_adr = seq_input->GetCharsAddress(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 827 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 828 | result = Execute(*code, |
| 829 | *input, |
| 830 | 0, |
| 831 | start_adr, |
| 832 | start_adr + input->length(), |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 833 | captures); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 834 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 835 | CHECK_EQ(NativeRegExpMacroAssembler::FAILURE, result); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 839 | TEST(MacroAssemblerNativeSimpleUC16) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 840 | v8::V8::Initialize(); |
| 841 | ContextInitializer initializer; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 842 | Factory* factory = Isolate::Current()->factory(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 843 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 844 | ArchRegExpMacroAssembler m(NativeRegExpMacroAssembler::UC16, 4, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 845 | Isolate::Current()->runtime_zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 846 | |
| 847 | uc16 foo_chars[3] = {'f', 'o', 'o'}; |
| 848 | Vector<const uc16> foo(foo_chars, 3); |
| 849 | |
| 850 | Label fail; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 851 | m.CheckCharacters(foo, 0, &fail, true); |
| 852 | m.WriteCurrentPositionToRegister(0, 0); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 853 | m.AdvanceCurrentPosition(3); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 854 | m.WriteCurrentPositionToRegister(1, 0); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 855 | m.Succeed(); |
| 856 | m.Bind(&fail); |
| 857 | m.Fail(); |
| 858 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 859 | Handle<String> source = factory->NewStringFromAscii(CStrVector("^foo")); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 860 | Handle<Object> code_object = m.GetCode(source); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 861 | Handle<Code> code = Handle<Code>::cast(code_object); |
| 862 | |
| 863 | int captures[4] = {42, 37, 87, 117}; |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 864 | const uc16 input_data[6] = {'f', 'o', 'o', 'f', 'o', |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 865 | static_cast<uc16>(0x2603)}; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 866 | Handle<String> input = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 867 | factory->NewStringFromTwoByte(Vector<const uc16>(input_data, 6)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 868 | Handle<SeqTwoByteString> seq_input = Handle<SeqTwoByteString>::cast(input); |
| 869 | Address start_adr = seq_input->GetCharsAddress(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 870 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 871 | NativeRegExpMacroAssembler::Result result = |
| 872 | Execute(*code, |
| 873 | *input, |
| 874 | 0, |
| 875 | start_adr, |
| 876 | start_adr + input->length(), |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 877 | captures); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 878 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 879 | CHECK_EQ(NativeRegExpMacroAssembler::SUCCESS, result); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 880 | CHECK_EQ(0, captures[0]); |
| 881 | CHECK_EQ(3, captures[1]); |
| 882 | CHECK_EQ(-1, captures[2]); |
| 883 | CHECK_EQ(-1, captures[3]); |
| 884 | |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 885 | const uc16 input_data2[9] = {'b', 'a', 'r', 'b', 'a', 'r', 'b', 'a', |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 886 | static_cast<uc16>(0x2603)}; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 887 | input = factory->NewStringFromTwoByte(Vector<const uc16>(input_data2, 9)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 888 | seq_input = Handle<SeqTwoByteString>::cast(input); |
| 889 | start_adr = seq_input->GetCharsAddress(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 890 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 891 | result = Execute(*code, |
| 892 | *input, |
| 893 | 0, |
| 894 | start_adr, |
| 895 | start_adr + input->length() * 2, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 896 | captures); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 897 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 898 | CHECK_EQ(NativeRegExpMacroAssembler::FAILURE, result); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 902 | TEST(MacroAssemblerNativeBacktrack) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 903 | v8::V8::Initialize(); |
| 904 | ContextInitializer initializer; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 905 | Factory* factory = Isolate::Current()->factory(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 906 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 907 | ArchRegExpMacroAssembler m(NativeRegExpMacroAssembler::ASCII, 0, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 908 | Isolate::Current()->runtime_zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 909 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 910 | Label fail; |
| 911 | Label backtrack; |
| 912 | m.LoadCurrentCharacter(10, &fail); |
| 913 | m.Succeed(); |
| 914 | m.Bind(&fail); |
| 915 | m.PushBacktrack(&backtrack); |
| 916 | m.LoadCurrentCharacter(10, NULL); |
| 917 | m.Succeed(); |
| 918 | m.Bind(&backtrack); |
| 919 | m.Fail(); |
| 920 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 921 | Handle<String> source = factory->NewStringFromAscii(CStrVector("..........")); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 922 | Handle<Object> code_object = m.GetCode(source); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 923 | Handle<Code> code = Handle<Code>::cast(code_object); |
| 924 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 925 | Handle<String> input = factory->NewStringFromAscii(CStrVector("foofoo")); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 926 | Handle<SeqOneByteString> seq_input = Handle<SeqOneByteString>::cast(input); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 927 | Address start_adr = seq_input->GetCharsAddress(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 928 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 929 | NativeRegExpMacroAssembler::Result result = |
| 930 | Execute(*code, |
| 931 | *input, |
| 932 | 0, |
| 933 | start_adr, |
| 934 | start_adr + input->length(), |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 935 | NULL); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 936 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 937 | CHECK_EQ(NativeRegExpMacroAssembler::FAILURE, result); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 941 | TEST(MacroAssemblerNativeBackReferenceASCII) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 942 | v8::V8::Initialize(); |
| 943 | ContextInitializer initializer; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 944 | Factory* factory = Isolate::Current()->factory(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 945 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 946 | ArchRegExpMacroAssembler m(NativeRegExpMacroAssembler::ASCII, 4, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 947 | Isolate::Current()->runtime_zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 948 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 949 | m.WriteCurrentPositionToRegister(0, 0); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 950 | m.AdvanceCurrentPosition(2); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 951 | m.WriteCurrentPositionToRegister(1, 0); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 952 | Label nomatch; |
| 953 | m.CheckNotBackReference(0, &nomatch); |
| 954 | m.Fail(); |
| 955 | m.Bind(&nomatch); |
| 956 | m.AdvanceCurrentPosition(2); |
| 957 | Label missing_match; |
| 958 | m.CheckNotBackReference(0, &missing_match); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 959 | m.WriteCurrentPositionToRegister(2, 0); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 960 | m.Succeed(); |
| 961 | m.Bind(&missing_match); |
| 962 | m.Fail(); |
| 963 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 964 | Handle<String> source = factory->NewStringFromAscii(CStrVector("^(..)..\1")); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 965 | Handle<Object> code_object = m.GetCode(source); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 966 | Handle<Code> code = Handle<Code>::cast(code_object); |
| 967 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 968 | Handle<String> input = factory->NewStringFromAscii(CStrVector("fooofo")); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 969 | Handle<SeqOneByteString> seq_input = Handle<SeqOneByteString>::cast(input); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 970 | Address start_adr = seq_input->GetCharsAddress(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 971 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 972 | int output[4]; |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 973 | NativeRegExpMacroAssembler::Result result = |
| 974 | Execute(*code, |
| 975 | *input, |
| 976 | 0, |
| 977 | start_adr, |
| 978 | start_adr + input->length(), |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 979 | output); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 980 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 981 | CHECK_EQ(NativeRegExpMacroAssembler::SUCCESS, result); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 982 | CHECK_EQ(0, output[0]); |
| 983 | CHECK_EQ(2, output[1]); |
| 984 | CHECK_EQ(6, output[2]); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 985 | CHECK_EQ(-1, output[3]); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 989 | TEST(MacroAssemblerNativeBackReferenceUC16) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 990 | v8::V8::Initialize(); |
| 991 | ContextInitializer initializer; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 992 | Factory* factory = Isolate::Current()->factory(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 993 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 994 | ArchRegExpMacroAssembler m(NativeRegExpMacroAssembler::UC16, 4, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 995 | Isolate::Current()->runtime_zone()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 996 | |
| 997 | m.WriteCurrentPositionToRegister(0, 0); |
| 998 | m.AdvanceCurrentPosition(2); |
| 999 | m.WriteCurrentPositionToRegister(1, 0); |
| 1000 | Label nomatch; |
| 1001 | m.CheckNotBackReference(0, &nomatch); |
| 1002 | m.Fail(); |
| 1003 | m.Bind(&nomatch); |
| 1004 | m.AdvanceCurrentPosition(2); |
| 1005 | Label missing_match; |
| 1006 | m.CheckNotBackReference(0, &missing_match); |
| 1007 | m.WriteCurrentPositionToRegister(2, 0); |
| 1008 | m.Succeed(); |
| 1009 | m.Bind(&missing_match); |
| 1010 | m.Fail(); |
| 1011 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1012 | Handle<String> source = factory->NewStringFromAscii(CStrVector("^(..)..\1")); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1013 | Handle<Object> code_object = m.GetCode(source); |
| 1014 | Handle<Code> code = Handle<Code>::cast(code_object); |
| 1015 | |
| 1016 | const uc16 input_data[6] = {'f', 0x2028, 'o', 'o', 'f', 0x2028}; |
| 1017 | Handle<String> input = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1018 | factory->NewStringFromTwoByte(Vector<const uc16>(input_data, 6)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1019 | Handle<SeqTwoByteString> seq_input = Handle<SeqTwoByteString>::cast(input); |
| 1020 | Address start_adr = seq_input->GetCharsAddress(); |
| 1021 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1022 | int output[4]; |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1023 | NativeRegExpMacroAssembler::Result result = |
| 1024 | Execute(*code, |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame] | 1025 | *input, |
| 1026 | 0, |
| 1027 | start_adr, |
| 1028 | start_adr + input->length() * 2, |
| 1029 | output); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1030 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1031 | CHECK_EQ(NativeRegExpMacroAssembler::SUCCESS, result); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1032 | CHECK_EQ(0, output[0]); |
| 1033 | CHECK_EQ(2, output[1]); |
| 1034 | CHECK_EQ(6, output[2]); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1035 | CHECK_EQ(-1, output[3]); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | |
| 1039 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1040 | TEST(MacroAssemblernativeAtStart) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1041 | v8::V8::Initialize(); |
| 1042 | ContextInitializer initializer; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1043 | Factory* factory = Isolate::Current()->factory(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1044 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1045 | ArchRegExpMacroAssembler m(NativeRegExpMacroAssembler::ASCII, 0, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1046 | Isolate::Current()->runtime_zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1047 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1048 | Label not_at_start, newline, fail; |
| 1049 | m.CheckNotAtStart(¬_at_start); |
| 1050 | // Check that prevchar = '\n' and current = 'f'. |
| 1051 | m.CheckCharacter('\n', &newline); |
| 1052 | m.Bind(&fail); |
| 1053 | m.Fail(); |
| 1054 | m.Bind(&newline); |
| 1055 | m.LoadCurrentCharacter(0, &fail); |
| 1056 | m.CheckNotCharacter('f', &fail); |
| 1057 | m.Succeed(); |
| 1058 | |
| 1059 | m.Bind(¬_at_start); |
| 1060 | // Check that prevchar = 'o' and current = 'b'. |
| 1061 | Label prevo; |
| 1062 | m.CheckCharacter('o', &prevo); |
| 1063 | m.Fail(); |
| 1064 | m.Bind(&prevo); |
| 1065 | m.LoadCurrentCharacter(0, &fail); |
| 1066 | m.CheckNotCharacter('b', &fail); |
| 1067 | m.Succeed(); |
| 1068 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1069 | Handle<String> source = factory->NewStringFromAscii(CStrVector("(^f|ob)")); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1070 | Handle<Object> code_object = m.GetCode(source); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1071 | Handle<Code> code = Handle<Code>::cast(code_object); |
| 1072 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1073 | Handle<String> input = factory->NewStringFromAscii(CStrVector("foobar")); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 1074 | Handle<SeqOneByteString> seq_input = Handle<SeqOneByteString>::cast(input); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1075 | Address start_adr = seq_input->GetCharsAddress(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1076 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1077 | NativeRegExpMacroAssembler::Result result = |
| 1078 | Execute(*code, |
| 1079 | *input, |
| 1080 | 0, |
| 1081 | start_adr, |
| 1082 | start_adr + input->length(), |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1083 | NULL); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1084 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1085 | CHECK_EQ(NativeRegExpMacroAssembler::SUCCESS, result); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1086 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1087 | result = Execute(*code, |
| 1088 | *input, |
| 1089 | 3, |
| 1090 | start_adr + 3, |
| 1091 | start_adr + input->length(), |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1092 | NULL); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1093 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1094 | CHECK_EQ(NativeRegExpMacroAssembler::SUCCESS, result); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1098 | TEST(MacroAssemblerNativeBackRefNoCase) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1099 | v8::V8::Initialize(); |
| 1100 | ContextInitializer initializer; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1101 | Factory* factory = Isolate::Current()->factory(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1102 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1103 | ArchRegExpMacroAssembler m(NativeRegExpMacroAssembler::ASCII, 4, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1104 | Isolate::Current()->runtime_zone()); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1105 | |
| 1106 | Label fail, succ; |
| 1107 | |
| 1108 | m.WriteCurrentPositionToRegister(0, 0); |
| 1109 | m.WriteCurrentPositionToRegister(2, 0); |
| 1110 | m.AdvanceCurrentPosition(3); |
| 1111 | m.WriteCurrentPositionToRegister(3, 0); |
| 1112 | m.CheckNotBackReferenceIgnoreCase(2, &fail); // Match "AbC". |
| 1113 | m.CheckNotBackReferenceIgnoreCase(2, &fail); // Match "ABC". |
| 1114 | Label expected_fail; |
| 1115 | m.CheckNotBackReferenceIgnoreCase(2, &expected_fail); |
| 1116 | m.Bind(&fail); |
| 1117 | m.Fail(); |
| 1118 | |
| 1119 | m.Bind(&expected_fail); |
| 1120 | m.AdvanceCurrentPosition(3); // Skip "xYz" |
| 1121 | m.CheckNotBackReferenceIgnoreCase(2, &succ); |
| 1122 | m.Fail(); |
| 1123 | |
| 1124 | m.Bind(&succ); |
| 1125 | m.WriteCurrentPositionToRegister(1, 0); |
| 1126 | m.Succeed(); |
| 1127 | |
| 1128 | Handle<String> source = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1129 | factory->NewStringFromAscii(CStrVector("^(abc)\1\1(?!\1)...(?!\1)")); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1130 | Handle<Object> code_object = m.GetCode(source); |
| 1131 | Handle<Code> code = Handle<Code>::cast(code_object); |
| 1132 | |
| 1133 | Handle<String> input = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1134 | factory->NewStringFromAscii(CStrVector("aBcAbCABCxYzab")); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 1135 | Handle<SeqOneByteString> seq_input = Handle<SeqOneByteString>::cast(input); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1136 | Address start_adr = seq_input->GetCharsAddress(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1137 | |
| 1138 | int output[4]; |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1139 | NativeRegExpMacroAssembler::Result result = |
| 1140 | Execute(*code, |
| 1141 | *input, |
| 1142 | 0, |
| 1143 | start_adr, |
| 1144 | start_adr + input->length(), |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1145 | output); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1146 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1147 | CHECK_EQ(NativeRegExpMacroAssembler::SUCCESS, result); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1148 | CHECK_EQ(0, output[0]); |
| 1149 | CHECK_EQ(12, output[1]); |
| 1150 | CHECK_EQ(0, output[2]); |
| 1151 | CHECK_EQ(3, output[3]); |
| 1152 | } |
| 1153 | |
| 1154 | |
| 1155 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1156 | TEST(MacroAssemblerNativeRegisters) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1157 | v8::V8::Initialize(); |
| 1158 | ContextInitializer initializer; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1159 | Factory* factory = Isolate::Current()->factory(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1160 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1161 | ArchRegExpMacroAssembler m(NativeRegExpMacroAssembler::ASCII, 6, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1162 | Isolate::Current()->runtime_zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1163 | |
| 1164 | uc16 foo_chars[3] = {'f', 'o', 'o'}; |
| 1165 | Vector<const uc16> foo(foo_chars, 3); |
| 1166 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1167 | enum registers { out1, out2, out3, out4, out5, out6, sp, loop_cnt }; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1168 | Label fail; |
| 1169 | Label backtrack; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1170 | m.WriteCurrentPositionToRegister(out1, 0); // Output: [0] |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1171 | m.PushRegister(out1, RegExpMacroAssembler::kNoStackLimitCheck); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1172 | m.PushBacktrack(&backtrack); |
| 1173 | m.WriteStackPointerToRegister(sp); |
| 1174 | // Fill stack and registers |
| 1175 | m.AdvanceCurrentPosition(2); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1176 | m.WriteCurrentPositionToRegister(out1, 0); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1177 | m.PushRegister(out1, RegExpMacroAssembler::kNoStackLimitCheck); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1178 | m.PushBacktrack(&fail); |
| 1179 | // Drop backtrack stack frames. |
| 1180 | m.ReadStackPointerFromRegister(sp); |
| 1181 | // And take the first backtrack (to &backtrack) |
| 1182 | m.Backtrack(); |
| 1183 | |
| 1184 | m.PushCurrentPosition(); |
| 1185 | m.AdvanceCurrentPosition(2); |
| 1186 | m.PopCurrentPosition(); |
| 1187 | |
| 1188 | m.Bind(&backtrack); |
| 1189 | m.PopRegister(out1); |
| 1190 | m.ReadCurrentPositionFromRegister(out1); |
| 1191 | m.AdvanceCurrentPosition(3); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1192 | m.WriteCurrentPositionToRegister(out2, 0); // [0,3] |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1193 | |
| 1194 | Label loop; |
| 1195 | m.SetRegister(loop_cnt, 0); // loop counter |
| 1196 | m.Bind(&loop); |
| 1197 | m.AdvanceRegister(loop_cnt, 1); |
| 1198 | m.AdvanceCurrentPosition(1); |
| 1199 | m.IfRegisterLT(loop_cnt, 3, &loop); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1200 | m.WriteCurrentPositionToRegister(out3, 0); // [0,3,6] |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1201 | |
| 1202 | Label loop2; |
| 1203 | m.SetRegister(loop_cnt, 2); // loop counter |
| 1204 | m.Bind(&loop2); |
| 1205 | m.AdvanceRegister(loop_cnt, -1); |
| 1206 | m.AdvanceCurrentPosition(1); |
| 1207 | m.IfRegisterGE(loop_cnt, 0, &loop2); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1208 | m.WriteCurrentPositionToRegister(out4, 0); // [0,3,6,9] |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1209 | |
| 1210 | Label loop3; |
| 1211 | Label exit_loop3; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1212 | m.PushRegister(out4, RegExpMacroAssembler::kNoStackLimitCheck); |
| 1213 | m.PushRegister(out4, RegExpMacroAssembler::kNoStackLimitCheck); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1214 | m.ReadCurrentPositionFromRegister(out3); |
| 1215 | m.Bind(&loop3); |
| 1216 | m.AdvanceCurrentPosition(1); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1217 | m.CheckGreedyLoop(&exit_loop3); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1218 | m.GoTo(&loop3); |
| 1219 | m.Bind(&exit_loop3); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1220 | m.PopCurrentPosition(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1221 | m.WriteCurrentPositionToRegister(out5, 0); // [0,3,6,9,9,-1] |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1222 | |
| 1223 | m.Succeed(); |
| 1224 | |
| 1225 | m.Bind(&fail); |
| 1226 | m.Fail(); |
| 1227 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1228 | Handle<String> source = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1229 | factory->NewStringFromAscii(CStrVector("<loop test>")); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1230 | Handle<Object> code_object = m.GetCode(source); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1231 | Handle<Code> code = Handle<Code>::cast(code_object); |
| 1232 | |
| 1233 | // String long enough for test (content doesn't matter). |
| 1234 | Handle<String> input = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1235 | factory->NewStringFromAscii(CStrVector("foofoofoofoofoo")); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 1236 | Handle<SeqOneByteString> seq_input = Handle<SeqOneByteString>::cast(input); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1237 | Address start_adr = seq_input->GetCharsAddress(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1238 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1239 | int output[6]; |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1240 | NativeRegExpMacroAssembler::Result result = |
| 1241 | Execute(*code, |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1242 | *input, |
| 1243 | 0, |
| 1244 | start_adr, |
| 1245 | start_adr + input->length(), |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1246 | output); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1247 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1248 | CHECK_EQ(NativeRegExpMacroAssembler::SUCCESS, result); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1249 | CHECK_EQ(0, output[0]); |
| 1250 | CHECK_EQ(3, output[1]); |
| 1251 | CHECK_EQ(6, output[2]); |
| 1252 | CHECK_EQ(9, output[3]); |
| 1253 | CHECK_EQ(9, output[4]); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1254 | CHECK_EQ(-1, output[5]); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1257 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1258 | TEST(MacroAssemblerStackOverflow) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1259 | v8::V8::Initialize(); |
| 1260 | ContextInitializer initializer; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1261 | Isolate* isolate = Isolate::Current(); |
| 1262 | Factory* factory = isolate->factory(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1263 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1264 | ArchRegExpMacroAssembler m(NativeRegExpMacroAssembler::ASCII, 0, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1265 | Isolate::Current()->runtime_zone()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1266 | |
| 1267 | Label loop; |
| 1268 | m.Bind(&loop); |
| 1269 | m.PushBacktrack(&loop); |
| 1270 | m.GoTo(&loop); |
| 1271 | |
| 1272 | Handle<String> source = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1273 | factory->NewStringFromAscii(CStrVector("<stack overflow test>")); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1274 | Handle<Object> code_object = m.GetCode(source); |
| 1275 | Handle<Code> code = Handle<Code>::cast(code_object); |
| 1276 | |
| 1277 | // String long enough for test (content doesn't matter). |
| 1278 | Handle<String> input = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1279 | factory->NewStringFromAscii(CStrVector("dummy")); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 1280 | Handle<SeqOneByteString> seq_input = Handle<SeqOneByteString>::cast(input); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1281 | Address start_adr = seq_input->GetCharsAddress(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1282 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1283 | NativeRegExpMacroAssembler::Result result = |
| 1284 | Execute(*code, |
| 1285 | *input, |
| 1286 | 0, |
| 1287 | start_adr, |
| 1288 | start_adr + input->length(), |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1289 | NULL); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1290 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1291 | CHECK_EQ(NativeRegExpMacroAssembler::EXCEPTION, result); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1292 | CHECK(isolate->has_pending_exception()); |
| 1293 | isolate->clear_pending_exception(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1297 | TEST(MacroAssemblerNativeLotsOfRegisters) { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1298 | v8::V8::Initialize(); |
| 1299 | ContextInitializer initializer; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1300 | Isolate* isolate = Isolate::Current(); |
| 1301 | Factory* factory = isolate->factory(); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1302 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1303 | ArchRegExpMacroAssembler m(NativeRegExpMacroAssembler::ASCII, 2, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1304 | Isolate::Current()->runtime_zone()); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1305 | |
| 1306 | // At least 2048, to ensure the allocated space for registers |
| 1307 | // span one full page. |
| 1308 | const int large_number = 8000; |
| 1309 | m.WriteCurrentPositionToRegister(large_number, 42); |
| 1310 | m.WriteCurrentPositionToRegister(0, 0); |
| 1311 | m.WriteCurrentPositionToRegister(1, 1); |
| 1312 | Label done; |
| 1313 | m.CheckNotBackReference(0, &done); // Performs a system-stack push. |
| 1314 | m.Bind(&done); |
| 1315 | m.PushRegister(large_number, RegExpMacroAssembler::kNoStackLimitCheck); |
| 1316 | m.PopRegister(1); |
| 1317 | m.Succeed(); |
| 1318 | |
| 1319 | Handle<String> source = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1320 | factory->NewStringFromAscii(CStrVector("<huge register space test>")); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1321 | Handle<Object> code_object = m.GetCode(source); |
| 1322 | Handle<Code> code = Handle<Code>::cast(code_object); |
| 1323 | |
| 1324 | // String long enough for test (content doesn't matter). |
| 1325 | Handle<String> input = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1326 | factory->NewStringFromAscii(CStrVector("sample text")); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 1327 | Handle<SeqOneByteString> seq_input = Handle<SeqOneByteString>::cast(input); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1328 | Address start_adr = seq_input->GetCharsAddress(); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1329 | |
| 1330 | int captures[2]; |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1331 | NativeRegExpMacroAssembler::Result result = |
| 1332 | Execute(*code, |
| 1333 | *input, |
| 1334 | 0, |
| 1335 | start_adr, |
| 1336 | start_adr + input->length(), |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1337 | captures); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1338 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1339 | CHECK_EQ(NativeRegExpMacroAssembler::SUCCESS, result); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1340 | CHECK_EQ(0, captures[0]); |
| 1341 | CHECK_EQ(42, captures[1]); |
| 1342 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1343 | isolate->clear_pending_exception(); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1346 | #else // V8_INTERPRETED_REGEXP |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1347 | |
| 1348 | TEST(MacroAssembler) { |
| 1349 | V8::Initialize(NULL); |
| 1350 | byte codes[1024]; |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1351 | RegExpMacroAssemblerIrregexp m(Vector<byte>(codes, 1024), |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1352 | Isolate::Current()->runtime_zone()); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1353 | // ^f(o)o. |
| 1354 | Label fail, fail2, start; |
| 1355 | uc16 foo_chars[3]; |
| 1356 | foo_chars[0] = 'f'; |
| 1357 | foo_chars[1] = 'o'; |
| 1358 | foo_chars[2] = 'o'; |
| 1359 | Vector<const uc16> foo(foo_chars, 3); |
| 1360 | m.SetRegister(4, 42); |
| 1361 | m.PushRegister(4, RegExpMacroAssembler::kNoStackLimitCheck); |
| 1362 | m.AdvanceRegister(4, 42); |
| 1363 | m.GoTo(&start); |
| 1364 | m.Fail(); |
| 1365 | m.Bind(&start); |
| 1366 | m.PushBacktrack(&fail2); |
| 1367 | m.CheckCharacters(foo, 0, &fail, true); |
| 1368 | m.WriteCurrentPositionToRegister(0, 0); |
| 1369 | m.PushCurrentPosition(); |
| 1370 | m.AdvanceCurrentPosition(3); |
| 1371 | m.WriteCurrentPositionToRegister(1, 0); |
| 1372 | m.PopCurrentPosition(); |
| 1373 | m.AdvanceCurrentPosition(1); |
| 1374 | m.WriteCurrentPositionToRegister(2, 0); |
| 1375 | m.AdvanceCurrentPosition(1); |
| 1376 | m.WriteCurrentPositionToRegister(3, 0); |
| 1377 | m.Succeed(); |
| 1378 | |
| 1379 | m.Bind(&fail); |
| 1380 | m.Backtrack(); |
| 1381 | m.Succeed(); |
| 1382 | |
| 1383 | m.Bind(&fail2); |
| 1384 | m.PopRegister(0); |
| 1385 | m.Fail(); |
| 1386 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1387 | Isolate* isolate = Isolate::Current(); |
| 1388 | Factory* factory = isolate->factory(); |
| 1389 | HandleScope scope(isolate); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1390 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1391 | Handle<String> source = factory->NewStringFromAscii(CStrVector("^f(o)o")); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1392 | Handle<ByteArray> array = Handle<ByteArray>::cast(m.GetCode(source)); |
| 1393 | int captures[5]; |
| 1394 | |
| 1395 | const uc16 str1[] = {'f', 'o', 'o', 'b', 'a', 'r'}; |
| 1396 | Handle<String> f1_16 = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1397 | factory->NewStringFromTwoByte(Vector<const uc16>(str1, 6)); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1398 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1399 | CHECK(IrregexpInterpreter::Match(isolate, array, f1_16, captures, 0)); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1400 | CHECK_EQ(0, captures[0]); |
| 1401 | CHECK_EQ(3, captures[1]); |
| 1402 | CHECK_EQ(1, captures[2]); |
| 1403 | CHECK_EQ(2, captures[3]); |
| 1404 | CHECK_EQ(84, captures[4]); |
| 1405 | |
| 1406 | const uc16 str2[] = {'b', 'a', 'r', 'f', 'o', 'o'}; |
| 1407 | Handle<String> f2_16 = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1408 | factory->NewStringFromTwoByte(Vector<const uc16>(str2, 6)); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1409 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1410 | CHECK(!IrregexpInterpreter::Match(isolate, array, f2_16, captures, 0)); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1411 | CHECK_EQ(42, captures[0]); |
| 1412 | } |
| 1413 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1414 | #endif // V8_INTERPRETED_REGEXP |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1415 | |
| 1416 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1417 | TEST(AddInverseToTable) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1418 | v8::internal::V8::Initialize(NULL); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1419 | static const int kLimit = 1000; |
| 1420 | static const int kRangeCount = 16; |
| 1421 | for (int t = 0; t < 10; t++) { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1422 | ZoneScope zone_scope(Isolate::Current()->runtime_zone(), DELETE_ON_EXIT); |
| 1423 | Zone* zone = Isolate::Current()->runtime_zone(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1424 | ZoneList<CharacterRange>* ranges = |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1425 | new(zone) |
| 1426 | ZoneList<CharacterRange>(kRangeCount, zone); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1427 | for (int i = 0; i < kRangeCount; i++) { |
| 1428 | int from = PseudoRandom(t + 87, i + 25) % kLimit; |
| 1429 | int to = from + (PseudoRandom(i + 87, t + 25) % (kLimit / 20)); |
| 1430 | if (to > kLimit) to = kLimit; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1431 | ranges->Add(CharacterRange(from, to), zone); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1432 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1433 | DispatchTable table(zone); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1434 | DispatchTableConstructor cons(&table, false, |
| 1435 | Isolate::Current()->runtime_zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1436 | cons.set_choice_index(0); |
| 1437 | cons.AddInverse(ranges); |
| 1438 | for (int i = 0; i < kLimit; i++) { |
| 1439 | bool is_on = false; |
| 1440 | for (int j = 0; !is_on && j < kRangeCount; j++) |
| 1441 | is_on = ranges->at(j).Contains(i); |
| 1442 | OutSet* set = table.Get(i); |
| 1443 | CHECK_EQ(is_on, set->Get(0) == false); |
| 1444 | } |
| 1445 | } |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1446 | ZoneScope zone_scope(Isolate::Current()->runtime_zone(), DELETE_ON_EXIT); |
| 1447 | Zone* zone = Isolate::Current()->runtime_zone(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1448 | ZoneList<CharacterRange>* ranges = |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1449 | new(zone) ZoneList<CharacterRange>(1, zone); |
| 1450 | ranges->Add(CharacterRange(0xFFF0, 0xFFFE), zone); |
| 1451 | DispatchTable table(zone); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1452 | DispatchTableConstructor cons(&table, false, |
| 1453 | Isolate::Current()->runtime_zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1454 | cons.set_choice_index(0); |
| 1455 | cons.AddInverse(ranges); |
| 1456 | CHECK(!table.Get(0xFFFE)->Get(0)); |
| 1457 | CHECK(table.Get(0xFFFF)->Get(0)); |
| 1458 | } |
| 1459 | |
| 1460 | |
| 1461 | static uc32 canonicalize(uc32 c) { |
| 1462 | unibrow::uchar canon[unibrow::Ecma262Canonicalize::kMaxWidth]; |
| 1463 | int count = unibrow::Ecma262Canonicalize::Convert(c, '\0', canon, NULL); |
| 1464 | if (count == 0) { |
| 1465 | return c; |
| 1466 | } else { |
| 1467 | CHECK_EQ(1, count); |
| 1468 | return canon[0]; |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | |
| 1473 | TEST(LatinCanonicalize) { |
| 1474 | unibrow::Mapping<unibrow::Ecma262UnCanonicalize> un_canonicalize; |
| 1475 | for (char lower = 'a'; lower <= 'z'; lower++) { |
| 1476 | char upper = lower + ('A' - 'a'); |
| 1477 | CHECK_EQ(canonicalize(lower), canonicalize(upper)); |
| 1478 | unibrow::uchar uncanon[unibrow::Ecma262UnCanonicalize::kMaxWidth]; |
| 1479 | int length = un_canonicalize.get(lower, '\0', uncanon); |
| 1480 | CHECK_EQ(2, length); |
| 1481 | CHECK_EQ(upper, uncanon[0]); |
| 1482 | CHECK_EQ(lower, uncanon[1]); |
| 1483 | } |
| 1484 | for (uc32 c = 128; c < (1 << 21); c++) |
| 1485 | CHECK_GE(canonicalize(c), 128); |
| 1486 | unibrow::Mapping<unibrow::ToUppercase> to_upper; |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1487 | // Canonicalization is only defined for the Basic Multilingual Plane. |
| 1488 | for (uc32 c = 0; c < (1 << 16); c++) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1489 | unibrow::uchar upper[unibrow::ToUppercase::kMaxWidth]; |
| 1490 | int length = to_upper.get(c, '\0', upper); |
| 1491 | if (length == 0) { |
| 1492 | length = 1; |
| 1493 | upper[0] = c; |
| 1494 | } |
| 1495 | uc32 u = upper[0]; |
| 1496 | if (length > 1 || (c >= 128 && u < 128)) |
| 1497 | u = c; |
| 1498 | CHECK_EQ(u, canonicalize(c)); |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1503 | static uc32 CanonRangeEnd(uc32 c) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1504 | unibrow::uchar canon[unibrow::CanonicalizationRange::kMaxWidth]; |
| 1505 | int count = unibrow::CanonicalizationRange::Convert(c, '\0', canon, NULL); |
| 1506 | if (count == 0) { |
| 1507 | return c; |
| 1508 | } else { |
| 1509 | CHECK_EQ(1, count); |
| 1510 | return canon[0]; |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | |
| 1515 | TEST(RangeCanonicalization) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1516 | // Check that we arrive at the same result when using the basic |
| 1517 | // range canonicalization primitives as when using immediate |
| 1518 | // canonicalization. |
| 1519 | unibrow::Mapping<unibrow::Ecma262UnCanonicalize> un_canonicalize; |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1520 | int block_start = 0; |
| 1521 | while (block_start <= 0xFFFF) { |
| 1522 | uc32 block_end = CanonRangeEnd(block_start); |
| 1523 | unsigned block_length = block_end - block_start + 1; |
| 1524 | if (block_length > 1) { |
| 1525 | unibrow::uchar first[unibrow::Ecma262UnCanonicalize::kMaxWidth]; |
| 1526 | int first_length = un_canonicalize.get(block_start, '\0', first); |
| 1527 | for (unsigned i = 1; i < block_length; i++) { |
| 1528 | unibrow::uchar succ[unibrow::Ecma262UnCanonicalize::kMaxWidth]; |
| 1529 | int succ_length = un_canonicalize.get(block_start + i, '\0', succ); |
| 1530 | CHECK_EQ(first_length, succ_length); |
| 1531 | for (int j = 0; j < succ_length; j++) { |
| 1532 | int calc = first[j] + i; |
| 1533 | int found = succ[j]; |
| 1534 | CHECK_EQ(calc, found); |
| 1535 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1536 | } |
| 1537 | } |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1538 | block_start = block_start + block_length; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | |
| 1543 | TEST(UncanonicalizeEquivalence) { |
| 1544 | unibrow::Mapping<unibrow::Ecma262UnCanonicalize> un_canonicalize; |
| 1545 | unibrow::uchar chars[unibrow::Ecma262UnCanonicalize::kMaxWidth]; |
| 1546 | for (int i = 0; i < (1 << 16); i++) { |
| 1547 | int length = un_canonicalize.get(i, '\0', chars); |
| 1548 | for (int j = 0; j < length; j++) { |
| 1549 | unibrow::uchar chars2[unibrow::Ecma262UnCanonicalize::kMaxWidth]; |
| 1550 | int length2 = un_canonicalize.get(chars[j], '\0', chars2); |
| 1551 | CHECK_EQ(length, length2); |
| 1552 | for (int k = 0; k < length; k++) |
| 1553 | CHECK_EQ(static_cast<int>(chars[k]), static_cast<int>(chars2[k])); |
| 1554 | } |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | |
| 1559 | static void TestRangeCaseIndependence(CharacterRange input, |
| 1560 | Vector<CharacterRange> expected) { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1561 | ZoneScope zone_scope(Isolate::Current()->runtime_zone(), DELETE_ON_EXIT); |
| 1562 | Zone* zone = Isolate::Current()->runtime_zone(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1563 | int count = expected.length(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1564 | ZoneList<CharacterRange>* list = |
| 1565 | new(zone) ZoneList<CharacterRange>(count, zone); |
| 1566 | input.AddCaseEquivalents(list, false, zone); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1567 | CHECK_EQ(count, list->length()); |
| 1568 | for (int i = 0; i < list->length(); i++) { |
| 1569 | CHECK_EQ(expected[i].from(), list->at(i).from()); |
| 1570 | CHECK_EQ(expected[i].to(), list->at(i).to()); |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | |
| 1575 | static void TestSimpleRangeCaseIndependence(CharacterRange input, |
| 1576 | CharacterRange expected) { |
| 1577 | EmbeddedVector<CharacterRange, 1> vector; |
| 1578 | vector[0] = expected; |
| 1579 | TestRangeCaseIndependence(input, vector); |
| 1580 | } |
| 1581 | |
| 1582 | |
| 1583 | TEST(CharacterRangeCaseIndependence) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1584 | v8::internal::V8::Initialize(NULL); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1585 | TestSimpleRangeCaseIndependence(CharacterRange::Singleton('a'), |
| 1586 | CharacterRange::Singleton('A')); |
| 1587 | TestSimpleRangeCaseIndependence(CharacterRange::Singleton('z'), |
| 1588 | CharacterRange::Singleton('Z')); |
| 1589 | TestSimpleRangeCaseIndependence(CharacterRange('a', 'z'), |
| 1590 | CharacterRange('A', 'Z')); |
| 1591 | TestSimpleRangeCaseIndependence(CharacterRange('c', 'f'), |
| 1592 | CharacterRange('C', 'F')); |
| 1593 | TestSimpleRangeCaseIndependence(CharacterRange('a', 'b'), |
| 1594 | CharacterRange('A', 'B')); |
| 1595 | TestSimpleRangeCaseIndependence(CharacterRange('y', 'z'), |
| 1596 | CharacterRange('Y', 'Z')); |
| 1597 | TestSimpleRangeCaseIndependence(CharacterRange('a' - 1, 'z' + 1), |
| 1598 | CharacterRange('A', 'Z')); |
| 1599 | TestSimpleRangeCaseIndependence(CharacterRange('A', 'Z'), |
| 1600 | CharacterRange('a', 'z')); |
| 1601 | TestSimpleRangeCaseIndependence(CharacterRange('C', 'F'), |
| 1602 | CharacterRange('c', 'f')); |
| 1603 | TestSimpleRangeCaseIndependence(CharacterRange('A' - 1, 'Z' + 1), |
| 1604 | CharacterRange('a', 'z')); |
| 1605 | // Here we need to add [l-z] to complete the case independence of |
| 1606 | // [A-Za-z] but we expect [a-z] to be added since we always add a |
| 1607 | // whole block at a time. |
| 1608 | TestSimpleRangeCaseIndependence(CharacterRange('A', 'k'), |
| 1609 | CharacterRange('a', 'z')); |
| 1610 | } |
| 1611 | |
| 1612 | |
| 1613 | static bool InClass(uc16 c, ZoneList<CharacterRange>* ranges) { |
| 1614 | if (ranges == NULL) |
| 1615 | return false; |
| 1616 | for (int i = 0; i < ranges->length(); i++) { |
| 1617 | CharacterRange range = ranges->at(i); |
| 1618 | if (range.from() <= c && c <= range.to()) |
| 1619 | return true; |
| 1620 | } |
| 1621 | return false; |
| 1622 | } |
| 1623 | |
| 1624 | |
| 1625 | TEST(CharClassDifference) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1626 | v8::internal::V8::Initialize(NULL); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1627 | ZoneScope zone_scope(Isolate::Current()->runtime_zone(), DELETE_ON_EXIT); |
| 1628 | Zone* zone = Isolate::Current()->runtime_zone(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1629 | ZoneList<CharacterRange>* base = |
| 1630 | new(zone) ZoneList<CharacterRange>(1, zone); |
| 1631 | base->Add(CharacterRange::Everything(), zone); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1632 | Vector<const int> overlay = CharacterRange::GetWordBounds(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1633 | ZoneList<CharacterRange>* included = NULL; |
| 1634 | ZoneList<CharacterRange>* excluded = NULL; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1635 | CharacterRange::Split(base, overlay, &included, &excluded, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1636 | Isolate::Current()->runtime_zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1637 | for (int i = 0; i < (1 << 16); i++) { |
| 1638 | bool in_base = InClass(i, base); |
| 1639 | if (in_base) { |
| 1640 | bool in_overlay = false; |
| 1641 | for (int j = 0; !in_overlay && j < overlay.length(); j += 2) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1642 | if (overlay[j] <= i && i < overlay[j+1]) |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1643 | in_overlay = true; |
| 1644 | } |
| 1645 | CHECK_EQ(in_overlay, InClass(i, included)); |
| 1646 | CHECK_EQ(!in_overlay, InClass(i, excluded)); |
| 1647 | } else { |
| 1648 | CHECK(!InClass(i, included)); |
| 1649 | CHECK(!InClass(i, excluded)); |
| 1650 | } |
| 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1655 | TEST(CanonicalizeCharacterSets) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1656 | v8::internal::V8::Initialize(NULL); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1657 | ZoneScope scope(Isolate::Current()->runtime_zone(), DELETE_ON_EXIT); |
| 1658 | Zone* zone = Isolate::Current()->runtime_zone(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1659 | ZoneList<CharacterRange>* list = |
| 1660 | new(zone) ZoneList<CharacterRange>(4, zone); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1661 | CharacterSet set(list); |
| 1662 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1663 | list->Add(CharacterRange(10, 20), zone); |
| 1664 | list->Add(CharacterRange(30, 40), zone); |
| 1665 | list->Add(CharacterRange(50, 60), zone); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1666 | set.Canonicalize(); |
| 1667 | ASSERT_EQ(3, list->length()); |
| 1668 | ASSERT_EQ(10, list->at(0).from()); |
| 1669 | ASSERT_EQ(20, list->at(0).to()); |
| 1670 | ASSERT_EQ(30, list->at(1).from()); |
| 1671 | ASSERT_EQ(40, list->at(1).to()); |
| 1672 | ASSERT_EQ(50, list->at(2).from()); |
| 1673 | ASSERT_EQ(60, list->at(2).to()); |
| 1674 | |
| 1675 | list->Rewind(0); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1676 | list->Add(CharacterRange(10, 20), zone); |
| 1677 | list->Add(CharacterRange(50, 60), zone); |
| 1678 | list->Add(CharacterRange(30, 40), zone); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1679 | set.Canonicalize(); |
| 1680 | ASSERT_EQ(3, list->length()); |
| 1681 | ASSERT_EQ(10, list->at(0).from()); |
| 1682 | ASSERT_EQ(20, list->at(0).to()); |
| 1683 | ASSERT_EQ(30, list->at(1).from()); |
| 1684 | ASSERT_EQ(40, list->at(1).to()); |
| 1685 | ASSERT_EQ(50, list->at(2).from()); |
| 1686 | ASSERT_EQ(60, list->at(2).to()); |
| 1687 | |
| 1688 | list->Rewind(0); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1689 | list->Add(CharacterRange(30, 40), zone); |
| 1690 | list->Add(CharacterRange(10, 20), zone); |
| 1691 | list->Add(CharacterRange(25, 25), zone); |
| 1692 | list->Add(CharacterRange(100, 100), zone); |
| 1693 | list->Add(CharacterRange(1, 1), zone); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1694 | set.Canonicalize(); |
| 1695 | ASSERT_EQ(5, list->length()); |
| 1696 | ASSERT_EQ(1, list->at(0).from()); |
| 1697 | ASSERT_EQ(1, list->at(0).to()); |
| 1698 | ASSERT_EQ(10, list->at(1).from()); |
| 1699 | ASSERT_EQ(20, list->at(1).to()); |
| 1700 | ASSERT_EQ(25, list->at(2).from()); |
| 1701 | ASSERT_EQ(25, list->at(2).to()); |
| 1702 | ASSERT_EQ(30, list->at(3).from()); |
| 1703 | ASSERT_EQ(40, list->at(3).to()); |
| 1704 | ASSERT_EQ(100, list->at(4).from()); |
| 1705 | ASSERT_EQ(100, list->at(4).to()); |
| 1706 | |
| 1707 | list->Rewind(0); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1708 | list->Add(CharacterRange(10, 19), zone); |
| 1709 | list->Add(CharacterRange(21, 30), zone); |
| 1710 | list->Add(CharacterRange(20, 20), zone); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1711 | set.Canonicalize(); |
| 1712 | ASSERT_EQ(1, list->length()); |
| 1713 | ASSERT_EQ(10, list->at(0).from()); |
| 1714 | ASSERT_EQ(30, list->at(0).to()); |
| 1715 | } |
| 1716 | |
ager@chromium.org | 6448867 | 2010-01-25 13:24:36 +0000 | [diff] [blame] | 1717 | |
| 1718 | TEST(CharacterRangeMerge) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1719 | v8::internal::V8::Initialize(NULL); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1720 | ZoneScope zone_scope(Isolate::Current()->runtime_zone(), DELETE_ON_EXIT); |
| 1721 | ZoneList<CharacterRange> l1(4, Isolate::Current()->runtime_zone()); |
| 1722 | ZoneList<CharacterRange> l2(4, Isolate::Current()->runtime_zone()); |
| 1723 | Zone* zone = Isolate::Current()->runtime_zone(); |
ager@chromium.org | 6448867 | 2010-01-25 13:24:36 +0000 | [diff] [blame] | 1724 | // Create all combinations of intersections of ranges, both singletons and |
| 1725 | // longer. |
| 1726 | |
| 1727 | int offset = 0; |
| 1728 | |
| 1729 | // The five kinds of singleton intersections: |
| 1730 | // X |
| 1731 | // Y - outside before |
| 1732 | // Y - outside touching start |
| 1733 | // Y - overlap |
| 1734 | // Y - outside touching end |
| 1735 | // Y - outside after |
| 1736 | |
| 1737 | for (int i = 0; i < 5; i++) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1738 | l1.Add(CharacterRange::Singleton(offset + 2), zone); |
| 1739 | l2.Add(CharacterRange::Singleton(offset + i), zone); |
ager@chromium.org | 6448867 | 2010-01-25 13:24:36 +0000 | [diff] [blame] | 1740 | offset += 6; |
| 1741 | } |
| 1742 | |
| 1743 | // The seven kinds of singleton/non-singleton intersections: |
| 1744 | // XXX |
| 1745 | // Y - outside before |
| 1746 | // Y - outside touching start |
| 1747 | // Y - inside touching start |
| 1748 | // Y - entirely inside |
| 1749 | // Y - inside touching end |
| 1750 | // Y - outside touching end |
| 1751 | // Y - disjoint after |
| 1752 | |
| 1753 | for (int i = 0; i < 7; i++) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1754 | l1.Add(CharacterRange::Range(offset + 2, offset + 4), zone); |
| 1755 | l2.Add(CharacterRange::Singleton(offset + i), zone); |
ager@chromium.org | 6448867 | 2010-01-25 13:24:36 +0000 | [diff] [blame] | 1756 | offset += 8; |
| 1757 | } |
| 1758 | |
| 1759 | // The eleven kinds of non-singleton intersections: |
| 1760 | // |
| 1761 | // XXXXXXXX |
| 1762 | // YYYY - outside before. |
| 1763 | // YYYY - outside touching start. |
| 1764 | // YYYY - overlapping start |
| 1765 | // YYYY - inside touching start |
| 1766 | // YYYY - entirely inside |
| 1767 | // YYYY - inside touching end |
| 1768 | // YYYY - overlapping end |
| 1769 | // YYYY - outside touching end |
| 1770 | // YYYY - outside after |
| 1771 | // YYYYYYYY - identical |
| 1772 | // YYYYYYYYYYYY - containing entirely. |
| 1773 | |
| 1774 | for (int i = 0; i < 9; i++) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1775 | l1.Add(CharacterRange::Range(offset + 6, offset + 15), zone); // Length 8. |
| 1776 | l2.Add(CharacterRange::Range(offset + 2 * i, offset + 2 * i + 3), zone); |
ager@chromium.org | 6448867 | 2010-01-25 13:24:36 +0000 | [diff] [blame] | 1777 | offset += 22; |
| 1778 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1779 | l1.Add(CharacterRange::Range(offset + 6, offset + 15), zone); |
| 1780 | l2.Add(CharacterRange::Range(offset + 6, offset + 15), zone); |
ager@chromium.org | 6448867 | 2010-01-25 13:24:36 +0000 | [diff] [blame] | 1781 | offset += 22; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1782 | l1.Add(CharacterRange::Range(offset + 6, offset + 15), zone); |
| 1783 | l2.Add(CharacterRange::Range(offset + 4, offset + 17), zone); |
ager@chromium.org | 6448867 | 2010-01-25 13:24:36 +0000 | [diff] [blame] | 1784 | offset += 22; |
| 1785 | |
| 1786 | // Different kinds of multi-range overlap: |
| 1787 | // XXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXX |
| 1788 | // YYYY Y YYYY Y YYYY Y YYYY Y YYYY Y YYYY Y |
| 1789 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1790 | l1.Add(CharacterRange::Range(offset, offset + 21), zone); |
| 1791 | l1.Add(CharacterRange::Range(offset + 31, offset + 52), zone); |
ager@chromium.org | 6448867 | 2010-01-25 13:24:36 +0000 | [diff] [blame] | 1792 | for (int i = 0; i < 6; i++) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1793 | l2.Add(CharacterRange::Range(offset + 2, offset + 5), zone); |
| 1794 | l2.Add(CharacterRange::Singleton(offset + 8), zone); |
ager@chromium.org | 6448867 | 2010-01-25 13:24:36 +0000 | [diff] [blame] | 1795 | offset += 9; |
| 1796 | } |
| 1797 | |
| 1798 | ASSERT(CharacterRange::IsCanonical(&l1)); |
| 1799 | ASSERT(CharacterRange::IsCanonical(&l2)); |
| 1800 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1801 | ZoneList<CharacterRange> first_only(4, Isolate::Current()->runtime_zone()); |
| 1802 | ZoneList<CharacterRange> second_only(4, Isolate::Current()->runtime_zone()); |
| 1803 | ZoneList<CharacterRange> both(4, Isolate::Current()->runtime_zone()); |
ager@chromium.org | 6448867 | 2010-01-25 13:24:36 +0000 | [diff] [blame] | 1804 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1805 | |
| 1806 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1807 | TEST(Graph) { |
| 1808 | V8::Initialize(NULL); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1809 | Execute("\\b\\w+\\b", false, true, true); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1810 | } |