16bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/* 26bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * Copyright (C) 2004, 2008, 2009 Apple Inc. All rights reserved. 36bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * Copyright (C) 2008 Collabora Ltd. 46bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * Copyright (C) 2011 Peter Varga (pvarga@webkit.org), University of Szeged 56bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * Copyright (C) 2013 Google Inc. All rights reserved. 66bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * 76bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * Redistribution and use in source and binary forms, with or without 86bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * modification, are permitted provided that the following conditions 96bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * are met: 106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * 1. Redistributions of source code must retain the above copyright 116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * notice, this list of conditions and the following disclaimer. 126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * 2. Redistributions in binary form must reproduce the above copyright 136bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * notice, this list of conditions and the following disclaimer in the 146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * documentation and/or other materials provided with the distribution. 156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * 166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines */ 286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines 296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include "config.h" 306bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include "bindings/core/v8/ScriptRegexp.h" 316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines 326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include "bindings/core/v8/V8Binding.h" 336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include "bindings/core/v8/V8PerIsolateData.h" 346bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include "bindings/core/v8/V8ScriptRunner.h" 356bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include "platform/ScriptForbiddenScope.h" 366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines 376bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesnamespace blink { 386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines 396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen HinesScriptRegexp::ScriptRegexp(const String& pattern, TextCaseSensitivity caseSensitivity, MultilineMode multilineMode) 406bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines{ 416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines v8::Isolate* isolate = v8::Isolate::GetCurrent(); 426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines v8::HandleScope handleScope(isolate); 43 v8::Context::Scope contextScope(V8PerIsolateData::from(isolate)->ensureScriptRegexpContext()); 44 v8::TryCatch tryCatch; 45 46 unsigned flags = v8::RegExp::kNone; 47 if (caseSensitivity == TextCaseInsensitive) 48 flags |= v8::RegExp::kIgnoreCase; 49 if (multilineMode == MultilineEnabled) 50 flags |= v8::RegExp::kMultiline; 51 52 v8::Local<v8::RegExp> regex = v8::RegExp::New(v8String(isolate, pattern), static_cast<v8::RegExp::Flags>(flags)); 53 54 // If the regex failed to compile we'll get an empty handle. 55 if (!regex.IsEmpty()) 56 m_regex.set(isolate, regex); 57} 58 59int ScriptRegexp::match(const String& string, int startFrom, int* matchLength) const 60{ 61 if (matchLength) 62 *matchLength = 0; 63 64 if (m_regex.isEmpty() || string.isNull()) 65 return -1; 66 67 // v8 strings are limited to int. 68 if (string.length() > INT_MAX) 69 return -1; 70 71 ScriptForbiddenScope::AllowUserAgentScript allowScript; 72 73 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 74 v8::HandleScope handleScope(isolate); 75 v8::Context::Scope contextScope(V8PerIsolateData::from(isolate)->ensureScriptRegexpContext()); 76 v8::TryCatch tryCatch; 77 78 v8::Local<v8::RegExp> regex = m_regex.newLocal(isolate); 79 v8::Local<v8::Function> exec = regex->Get(v8AtomicString(isolate, "exec")).As<v8::Function>(); 80 v8::Handle<v8::Value> argv[] = { v8String(isolate, string.substring(startFrom)) }; 81 v8::Local<v8::Value> returnValue = V8ScriptRunner::callInternalFunction(exec, regex, WTF_ARRAY_LENGTH(argv), argv, isolate); 82 83 if (tryCatch.HasCaught()) 84 return -1; 85 86 // RegExp#exec returns null if there's no match, otherwise it returns an 87 // Array of strings with the first being the whole match string and others 88 // being subgroups. The Array also has some random properties tacked on like 89 // "index" which is the offset of the match. 90 // 91 // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp/exec 92 93 ASSERT(!returnValue.IsEmpty()); 94 if (!returnValue->IsArray()) 95 return -1; 96 97 v8::Local<v8::Array> result = returnValue.As<v8::Array>(); 98 int matchOffset = result->Get(v8AtomicString(isolate, "index"))->ToInt32()->Value(); 99 if (matchLength) { 100 v8::Local<v8::String> match = result->Get(0).As<v8::String>(); 101 *matchLength = match->Length(); 102 } 103 104 return matchOffset + startFrom; 105} 106 107} // namespace blink 108