Home | History | Annotate | Download | only in text
      1 /*
      2  * Copyright (C) 2004, 2008, 2009 Apple Inc. All rights reserved.
      3  * Copyright (C) 2008 Collabora Ltd.
      4  * Copyright (C) 2011 Peter Varga (pvarga (at) webkit.org), University of Szeged
      5  * Copyright (C) 2013 Google Inc. All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     24  * 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 "config.h"
     30 #include "core/platform/text/RegularExpression.h"
     31 
     32 // FIXME: These seem like a layering violation, but converting the strings manually
     33 // without v8String is difficult, and calling into v8 without V8RecursionScope will
     34 // assert. Perhaps v8 basic utilities shouldn't be in bindings, or we should put
     35 // RegularExpression as some kind of abstract interface that's implemented in bindings.
     36 #include "bindings/v8/V8Binding.h"
     37 #include "bindings/v8/V8PerIsolateData.h"
     38 #include "bindings/v8/V8RecursionScope.h"
     39 
     40 namespace WebCore {
     41 
     42 RegularExpression::RegularExpression(const String& pattern, TextCaseSensitivity caseSensitivity, MultilineMode multilineMode)
     43 {
     44     v8::Isolate* isolate = v8::Isolate::GetCurrent();
     45     v8::HandleScope handleScope(isolate);
     46     v8::Local<v8::Context> context = V8PerIsolateData::from(isolate)->ensureRegexContext();
     47     v8::Context::Scope scope(context);
     48 
     49     unsigned flags = v8::RegExp::kNone;
     50     if (caseSensitivity == TextCaseInsensitive)
     51         flags |= v8::RegExp::kIgnoreCase;
     52     if (multilineMode == MultilineEnabled)
     53         flags |= v8::RegExp::kMultiline;
     54 
     55     v8::TryCatch tryCatch;
     56     v8::Local<v8::RegExp> regex = v8::RegExp::New(v8String(pattern, context->GetIsolate()), static_cast<v8::RegExp::Flags>(flags));
     57 
     58     // If the regex failed to compile we'll get an empty handle.
     59     if (!regex.IsEmpty())
     60         m_regex.set(isolate, regex);
     61 }
     62 
     63 int RegularExpression::match(const String& string, int startFrom, int* matchLength) const
     64 {
     65     if (matchLength)
     66         *matchLength = 0;
     67 
     68     if (m_regex.isEmpty() || string.isNull())
     69          return -1;
     70 
     71     // v8 strings are limited to int.
     72     if (string.length() > INT_MAX)
     73          return -1;
     74 
     75     v8::Isolate* isolate = v8::Isolate::GetCurrent();
     76     v8::HandleScope handleScope(isolate);
     77     v8::Local<v8::Context> context = V8PerIsolateData::current()->ensureRegexContext();
     78     v8::Context::Scope scope(context);
     79     v8::TryCatch tryCatch;
     80 
     81     V8RecursionScope::MicrotaskSuppression microtaskScope;
     82 
     83     v8::Local<v8::RegExp> regex = m_regex.newLocal(isolate);
     84     v8::Local<v8::Function> exec = regex->Get(v8::String::NewSymbol("exec")).As<v8::Function>();
     85 
     86     v8::Handle<v8::Value> argv[] = { v8String(string.substring(startFrom), context->GetIsolate()) };
     87     v8::Local<v8::Value> returnValue = exec->Call(regex, 1, argv);
     88 
     89     // RegExp#exec returns null if there's no match, otherwise it returns an
     90     // Array of strings with the first being the whole match string and others
     91     // being subgroups. The Array also has some random properties tacked on like
     92     // "index" which is the offset of the match.
     93     //
     94     // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp/exec
     95 
     96     if (!returnValue->IsArray())
     97          return -1;
     98 
     99     v8::Local<v8::Array> result = returnValue.As<v8::Array>();
    100     int matchOffset = result->Get(v8::String::NewSymbol("index"))->ToInt32()->Value();
    101 
    102     if (matchLength) {
    103         v8::Local<v8::String> match = result->Get(0).As<v8::String>();
    104         *matchLength = match->Length();
    105     }
    106 
    107     return matchOffset + startFrom;
    108 }
    109 
    110 } // namespace WebCore
    111