1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "vm/RegExpStatics.h"
8 
9 #include "gc/FreeOp.h"
10 
11 #include "vm/NativeObject-inl.h"
12 
13 using namespace js;
14 
15 // static
create(JSContext * cx)16 UniquePtr<RegExpStatics> RegExpStatics::create(JSContext* cx) {
17   return cx->make_unique<RegExpStatics>();
18 }
19 
executeLazy(JSContext * cx)20 bool RegExpStatics::executeLazy(JSContext* cx) {
21   if (!pendingLazyEvaluation) {
22     return true;
23   }
24 
25   MOZ_ASSERT(lazySource);
26   MOZ_ASSERT(matchesInput);
27   MOZ_ASSERT(lazyIndex != size_t(-1));
28 
29   /* Retrieve or create the RegExpShared in this zone. */
30   RootedAtom source(cx, lazySource);
31   RootedRegExpShared shared(cx,
32                             cx->zone()->regExps().get(cx, source, lazyFlags));
33   if (!shared) {
34     return false;
35   }
36 
37   /*
38    * It is not necessary to call aboutToWrite(): evaluation of
39    * implicit copies is safe.
40    */
41 
42   /* Execute the full regular expression. */
43   RootedLinearString input(cx, matchesInput);
44   RegExpRunStatus status =
45       RegExpShared::execute(cx, &shared, input, lazyIndex, &this->matches);
46   if (status == RegExpRunStatus_Error) {
47     return false;
48   }
49 
50   /*
51    * RegExpStatics are only updated on successful (matching) execution.
52    * Re-running the same expression must therefore produce a matching result.
53    */
54   MOZ_ASSERT(status == RegExpRunStatus_Success);
55 
56   /* Unset lazy state and remove rooted values that now have no use. */
57   pendingLazyEvaluation = false;
58   lazySource = nullptr;
59   lazyIndex = size_t(-1);
60 
61   return true;
62 }
63