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@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 "third_party/blink/renderer/bindings/core/v8/script_regexp.h"
30 
31 #include "base/stl_util.h"
32 #include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
33 #include "third_party/blink/renderer/bindings/core/v8/v8_script_runner.h"
34 #include "third_party/blink/renderer/platform/bindings/script_forbidden_scope.h"
35 #include "third_party/blink/renderer/platform/bindings/v8_per_isolate_data.h"
36 
37 namespace blink {
38 
39 namespace {
40 const uint32_t kBacktrackLimit = 1'000'000;
41 }  // namespace
42 
ScriptRegexp(const String & pattern,TextCaseSensitivity case_sensitivity,MultilineMode multiline_mode,CharacterMode char_mode)43 ScriptRegexp::ScriptRegexp(const String& pattern,
44                            TextCaseSensitivity case_sensitivity,
45                            MultilineMode multiline_mode,
46                            CharacterMode char_mode) {
47   v8::Isolate* isolate = v8::Isolate::GetCurrent();
48   v8::HandleScope handle_scope(isolate);
49   v8::Local<v8::Context> context =
50       V8PerIsolateData::From(isolate)->EnsureScriptRegexpContext();
51   v8::Context::Scope context_scope(context);
52   v8::TryCatch try_catch(isolate);
53 
54   unsigned flags = v8::RegExp::kNone;
55   if (case_sensitivity != kTextCaseSensitive)
56     flags |= v8::RegExp::kIgnoreCase;
57   if (multiline_mode == kMultilineEnabled)
58     flags |= v8::RegExp::kMultiline;
59   if (char_mode == UTF16)
60     flags |= v8::RegExp::kUnicode;
61 
62   v8::Local<v8::RegExp> regex;
63   if (v8::RegExp::NewWithBacktrackLimit(context, V8String(isolate, pattern),
64                                         static_cast<v8::RegExp::Flags>(flags),
65                                         kBacktrackLimit)
66           .ToLocal(&regex))
67     regex_.Set(isolate, regex);
68   if (try_catch.HasCaught() && !try_catch.Message().IsEmpty())
69     exception_message_ =
70         ToCoreStringWithUndefinedOrNullCheck(try_catch.Message()->Get());
71 }
72 
Match(const String & string,int start_from,int * match_length) const73 int ScriptRegexp::Match(const String& string,
74                         int start_from,
75                         int* match_length) const {
76   if (match_length)
77     *match_length = 0;
78 
79   if (regex_.IsEmpty() || string.IsNull())
80     return -1;
81 
82   // v8 strings are limited to int.
83   if (string.length() > INT_MAX)
84     return -1;
85 
86   ScriptForbiddenScope::AllowUserAgentScript allow_script;
87 
88   v8::Isolate* isolate = v8::Isolate::GetCurrent();
89   v8::HandleScope handle_scope(isolate);
90   v8::Local<v8::Context> context =
91       V8PerIsolateData::From(isolate)->EnsureScriptRegexpContext();
92   v8::Context::Scope context_scope(context);
93   v8::TryCatch try_catch(isolate);
94 
95   v8::Local<v8::RegExp> regex = regex_.NewLocal(isolate);
96   v8::Local<v8::String> subject =
97       V8String(isolate, string.Substring(start_from));
98   v8::Local<v8::Value> return_value;
99   if (!regex->Exec(context, subject).ToLocal(&return_value))
100     return -1;
101 
102   // RegExp#exec returns null if there's no match, otherwise it returns an
103   // Array of strings with the first being the whole match string and others
104   // being subgroups. The Array also has some random properties tacked on like
105   // "index" which is the offset of the match.
106   //
107   // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp/exec
108 
109   DCHECK(!return_value.IsEmpty());
110   if (!return_value->IsArray())
111     return -1;
112 
113   v8::Local<v8::Array> result = return_value.As<v8::Array>();
114   v8::Local<v8::Value> match_offset;
115   if (!result->Get(context, V8AtomicString(isolate, "index"))
116            .ToLocal(&match_offset))
117     return -1;
118   if (match_length) {
119     v8::Local<v8::Value> match;
120     if (!result->Get(context, 0).ToLocal(&match))
121       return -1;
122     *match_length = match.As<v8::String>()->Length();
123   }
124 
125   return match_offset.As<v8::Int32>()->Value() + start_from;
126 }
127 
128 }  // namespace blink
129