| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2004, 2008, 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2004, 2008, 2009 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2008 Collabora Ltd. | 3 * Copyright (C) 2008 Collabora Ltd. |
| 4 * Copyright (C) 2011 Peter Varga (pvarga@webkit.org), University of Szeged | 4 * Copyright (C) 2011 Peter Varga (pvarga@webkit.org), University of Szeged |
| 5 * Copyright (C) 2013 Google Inc. All rights reserved. | |
| 6 * | 5 * |
| 7 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
| 8 * modification, are permitted provided that the following conditions | 7 * modification, are permitted provided that the following conditions |
| 9 * are met: | 8 * are met: |
| 10 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| 12 * 2. Redistributions in binary form must reproduce the above copyright | 11 * 2. Redistributions in binary form must reproduce the above copyright |
| 13 * notice, this list of conditions and the following disclaimer in the | 12 * notice, this list of conditions and the following disclaimer in the |
| 14 * documentation and/or other materials provided with the distribution. | 13 * documentation and/or other materials provided with the distribution. |
| 15 * | 14 * |
| 16 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | 15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | 18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 24 * (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. | 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 */ | 26 */ |
| 28 | 27 |
| 29 #include "config.h" | 28 #include "config.h" |
| 30 #include "RegularExpression.h" | 29 #include "RegularExpression.h" |
| 31 | 30 |
| 32 // FIXME: These seem like a layering violation, but converting the strings manua
lly | 31 #include <wtf/BumpPointerAllocator.h> |
| 33 // without v8String is difficult, and calling into v8 without V8RecursionScope w
ill | 32 #include <yarr/Yarr.h> |
| 34 // assert. Perhaps v8 basic utilities shouldn't be in bindings, or we should put | 33 #include "Logging.h" |
| 35 // RegularExpression as some kind of abstract interface that's implemented in bi
ndings. | |
| 36 #include "V8Binding.h" | |
| 37 #include "V8RecursionScope.h" | |
| 38 #include <wtf/MainThread.h> | |
| 39 | 34 |
| 40 namespace WebCore { | 35 namespace WebCore { |
| 41 | 36 |
| 42 static v8::Local<v8::Context> regexContext() | 37 RegularExpression::RegularExpression(const String& pattern, TextCaseSensitivity
caseSensitivity, MultilineMode multilineMode) |
| 38 : m_numSubpatterns(0) |
| 39 , m_regExpByteCode(compile(pattern, caseSensitivity, multilineMode)) |
| 43 { | 40 { |
| 44 ASSERT(isMainThread()); | |
| 45 static ScopedPersistent<v8::Context>* staticRegexContext = new ScopedPersist
ent<v8::Context>(v8::Context::New()); | |
| 46 return v8::Local<v8::Context>::New(staticRegexContext->get()); | |
| 47 } | 41 } |
| 48 | 42 |
| 49 RegularExpression::RegularExpression(const String& pattern, TextCaseSensitivity
caseSensitivity, MultilineMode multilineMode) | 43 PassOwnPtr<JSC::Yarr::BytecodePattern> RegularExpression::compile(const String&
patternString, TextCaseSensitivity caseSensitivity, MultilineMode multilineMode) |
| 50 { | 44 { |
| 51 v8::HandleScope handleScope; | 45 const char* constructionError = 0; |
| 52 v8::Local<v8::Context> context(regexContext()); | 46 JSC::Yarr::YarrPattern pattern(patternString, (caseSensitivity == TextCaseIn
sensitive), (multilineMode == MultilineEnabled), &constructionError); |
| 53 v8::Context::Scope scope(context); | 47 if (constructionError) { |
| 48 LOG_ERROR("RegularExpression: YARR compile failed with '%s'", constructi
onError); |
| 49 return nullptr; |
| 50 } |
| 54 | 51 |
| 55 unsigned flags = v8::RegExp::kNone; | 52 m_numSubpatterns = pattern.m_numSubpatterns; |
| 56 if (caseSensitivity == TextCaseInsensitive) | |
| 57 flags |= v8::RegExp::kIgnoreCase; | |
| 58 if (multilineMode == MultilineEnabled) | |
| 59 flags |= v8::RegExp::kMultiline; | |
| 60 | 53 |
| 61 v8::TryCatch tryCatch; | 54 return JSC::Yarr::byteCompile(pattern, &m_regexAllocator); |
| 62 v8::Local<v8::RegExp> regex = v8::RegExp::New(v8String(pattern, context->Get
Isolate()), static_cast<v8::RegExp::Flags>(flags)); | |
| 63 | |
| 64 // If the regex failed to compile we'll get an empty handle. | |
| 65 if (!regex.IsEmpty()) | |
| 66 m_regex.set(regex); | |
| 67 } | 55 } |
| 68 | 56 |
| 69 int RegularExpression::match(const String& string, int startFrom, int* matchLeng
th) const | 57 int RegularExpression::match(const String& str, int startFrom, int* matchLength)
const |
| 70 { | 58 { |
| 71 if (m_regex.isEmpty() || string.isNull()) | 59 if (!m_regExpByteCode) |
| 72 return -1; | 60 return -1; |
| 73 | 61 |
| 74 // v8 strings are limited to int. | 62 if (str.isNull()) |
| 75 if (string.length() > INT_MAX) | |
| 76 return -1; | 63 return -1; |
| 77 | 64 |
| 78 v8::HandleScope handleScope; | 65 int offsetVectorSize = (m_numSubpatterns + 1) * 2; |
| 79 v8::Local<v8::Context> context(regexContext()); | 66 unsigned* offsetVector; |
| 80 v8::Context::Scope scope(context); | 67 Vector<unsigned, 32> nonReturnedOvector; |
| 81 v8::TryCatch tryCatch; | |
| 82 | 68 |
| 83 V8RecursionScope::MicrotaskSuppression microtaskScope; | 69 nonReturnedOvector.resize(offsetVectorSize); |
| 70 offsetVector = nonReturnedOvector.data(); |
| 84 | 71 |
| 85 v8::Local<v8::Function> exec = m_regex->Get(v8::String::NewSymbol("exec")).A
s<v8::Function>(); | 72 ASSERT(offsetVector); |
| 73 for (unsigned j = 0, i = 0; i < m_numSubpatterns + 1; j += 2, i++) |
| 74 offsetVector[j] = JSC::Yarr::offsetNoMatch; |
| 86 | 75 |
| 87 v8::Handle<v8::Value> argv[] = { v8String(string, context->GetIsolate()) }; | 76 unsigned result; |
| 88 v8::Local<v8::Value> returnValue = exec->Call(m_regex.get(), 1, argv); | 77 if (str.length() <= INT_MAX) |
| 78 result = JSC::Yarr::interpret(m_regExpByteCode.get(), str, startFrom, of
fsetVector); |
| 79 else { |
| 80 // This code can't handle unsigned offsets. Limit our processing to stri
ngs with offsets that |
| 81 // can be represented as ints. |
| 82 result = JSC::Yarr::offsetNoMatch; |
| 83 } |
| 89 | 84 |
| 90 // RegExp#exec returns null if there's no match, otherwise it returns an | 85 if (result == JSC::Yarr::offsetNoMatch) |
| 91 // Array of strings with the first being the whole match string and others | |
| 92 // being subgroups. The Array also has some random properties tacked on like | |
| 93 // "index" which is the offset of the match. | |
| 94 // | |
| 95 // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Obje
cts/RegExp/exec | |
| 96 | |
| 97 if (!returnValue->IsArray()) | |
| 98 return -1; | 86 return -1; |
| 99 | 87 |
| 100 v8::Local<v8::Array> result = returnValue.As<v8::Array>(); | 88 // 1 means 1 match; 0 means more than one match. First match is recorded in
offsetVector. |
| 101 int matchOffset = result->Get(v8::String::NewSymbol("index"))->ToInt32()->Va
lue(); | 89 if (matchLength) |
| 102 | 90 *matchLength = offsetVector[1] - offsetVector[0]; |
| 103 if (matchLength) { | 91 return offsetVector[0]; |
| 104 v8::Local<v8::String> match = result->Get(0).As<v8::String>(); | |
| 105 *matchLength = match->Length(); | |
| 106 } | |
| 107 | |
| 108 return matchOffset; | |
| 109 } | 92 } |
| 110 | 93 |
| 111 } // namespace WebCore | 94 } // namespace WebCore |
| OLD | NEW |