• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/inspector/v8-regex.h"
6 
7 #include <limits.h>
8 
9 #include "include/v8-container.h"
10 #include "include/v8-context.h"
11 #include "include/v8-function.h"
12 #include "include/v8-inspector.h"
13 #include "include/v8-microtask-queue.h"
14 #include "include/v8-regexp.h"
15 #include "src/inspector/string-util.h"
16 #include "src/inspector/v8-inspector-impl.h"
17 
18 namespace v8_inspector {
19 
V8Regex(V8InspectorImpl * inspector,const String16 & pattern,bool caseSensitive,bool multiline)20 V8Regex::V8Regex(V8InspectorImpl* inspector, const String16& pattern,
21                  bool caseSensitive, bool multiline)
22     : m_inspector(inspector) {
23   v8::Isolate* isolate = m_inspector->isolate();
24   v8::HandleScope handleScope(isolate);
25   v8::Local<v8::Context> context;
26   if (!m_inspector->regexContext().ToLocal(&context)) {
27     DCHECK(isolate->IsExecutionTerminating());
28     m_errorMessage = "terminated";
29     return;
30   }
31   v8::Context::Scope contextScope(context);
32   v8::TryCatch tryCatch(isolate);
33 
34   unsigned flags = v8::RegExp::kNone;
35   if (!caseSensitive) flags |= v8::RegExp::kIgnoreCase;
36   if (multiline) flags |= v8::RegExp::kMultiline;
37 
38   v8::Local<v8::RegExp> regex;
39   if (v8::RegExp::New(context, toV8String(isolate, pattern),
40                       static_cast<v8::RegExp::Flags>(flags))
41           .ToLocal(&regex))
42     m_regex.Reset(isolate, regex);
43   else if (tryCatch.HasCaught())
44     m_errorMessage = toProtocolString(isolate, tryCatch.Message()->Get());
45   else
46     m_errorMessage = "Internal error";
47 }
48 
match(const String16 & string,int startFrom,int * matchLength) const49 int V8Regex::match(const String16& string, int startFrom,
50                    int* matchLength) const {
51   if (matchLength) *matchLength = 0;
52 
53   if (m_regex.IsEmpty() || string.isEmpty()) return -1;
54 
55   // v8 strings are limited to int.
56   if (string.length() > INT_MAX) return -1;
57 
58   v8::Isolate* isolate = m_inspector->isolate();
59   v8::HandleScope handleScope(isolate);
60   v8::Local<v8::Context> context;
61   if (!m_inspector->regexContext().ToLocal(&context)) {
62     DCHECK(isolate->IsExecutionTerminating());
63     return -1;
64   }
65   v8::Context::Scope contextScope(context);
66   v8::MicrotasksScope microtasks(isolate,
67                                  v8::MicrotasksScope::kDoNotRunMicrotasks);
68   v8::TryCatch tryCatch(isolate);
69 
70   v8::Local<v8::RegExp> regex = m_regex.Get(isolate);
71   v8::Local<v8::Value> exec;
72   if (!regex->Get(context, toV8StringInternalized(isolate, "exec"))
73            .ToLocal(&exec))
74     return -1;
75   v8::Local<v8::Value> argv[] = {
76       toV8String(isolate, string.substring(startFrom))};
77   v8::Local<v8::Value> returnValue;
78   if (!exec.As<v8::Function>()
79            ->Call(context, regex, arraysize(argv), argv)
80            .ToLocal(&returnValue))
81     return -1;
82 
83   // RegExp#exec returns null if there's no match, otherwise it returns an
84   // Array of strings with the first being the whole match string and others
85   // being subgroups. The Array also has some random properties tacked on like
86   // "index" which is the offset of the match.
87   //
88   // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp/exec
89 
90   DCHECK(!returnValue.IsEmpty());
91   if (!returnValue->IsArray()) return -1;
92 
93   v8::Local<v8::Array> result = returnValue.As<v8::Array>();
94   v8::Local<v8::Value> matchOffset;
95   if (!result->Get(context, toV8StringInternalized(isolate, "index"))
96            .ToLocal(&matchOffset))
97     return -1;
98   if (matchLength) {
99     v8::Local<v8::Value> match;
100     if (!result->Get(context, 0).ToLocal(&match)) return -1;
101     *matchLength = match.As<v8::String>()->Length();
102   }
103 
104   return matchOffset.As<v8::Int32>()->Value() + startFrom;
105 }
106 
107 }  // namespace v8_inspector
108