summaryrefslogtreecommitdiffstats
path: root/js/src/vm/RegExpStatics.cpp
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /js/src/vm/RegExpStatics.cpp
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'js/src/vm/RegExpStatics.cpp')
-rw-r--r--js/src/vm/RegExpStatics.cpp111
1 files changed, 111 insertions, 0 deletions
diff --git a/js/src/vm/RegExpStatics.cpp b/js/src/vm/RegExpStatics.cpp
new file mode 100644
index 000000000..5375c0039
--- /dev/null
+++ b/js/src/vm/RegExpStatics.cpp
@@ -0,0 +1,111 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ * vim: set ts=8 sts=4 et sw=4 tw=99:
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "vm/RegExpStatics.h"
+
+#include "vm/RegExpStaticsObject.h"
+
+#include "vm/NativeObject-inl.h"
+
+using namespace js;
+
+/*
+ * RegExpStatics allocates memory -- in order to keep the statics stored
+ * per-global and not leak, we create a js::Class to wrap the C++ instance and
+ * provide an appropriate finalizer. We lazily create and store an instance of
+ * that js::Class in a global reserved slot.
+ */
+
+static void
+resc_finalize(FreeOp* fop, JSObject* obj)
+{
+ MOZ_ASSERT(fop->onMainThread());
+ RegExpStatics* res = static_cast<RegExpStatics*>(obj->as<RegExpStaticsObject>().getPrivate());
+ fop->delete_(res);
+}
+
+static void
+resc_trace(JSTracer* trc, JSObject* obj)
+{
+ void* pdata = obj->as<RegExpStaticsObject>().getPrivate();
+ if (pdata)
+ static_cast<RegExpStatics*>(pdata)->mark(trc);
+}
+
+static const ClassOps RegExpStaticsObjectClassOps = {
+ nullptr, /* addProperty */
+ nullptr, /* delProperty */
+ nullptr, /* getProperty */
+ nullptr, /* setProperty */
+ nullptr, /* enumerate */
+ nullptr, /* resolve */
+ nullptr, /* mayResolve */
+ resc_finalize,
+ nullptr, /* call */
+ nullptr, /* hasInstance */
+ nullptr, /* construct */
+ resc_trace
+};
+
+const Class RegExpStaticsObject::class_ = {
+ "RegExpStatics",
+ JSCLASS_HAS_PRIVATE |
+ JSCLASS_FOREGROUND_FINALIZE,
+ &RegExpStaticsObjectClassOps
+};
+
+RegExpStaticsObject*
+RegExpStatics::create(ExclusiveContext* cx, Handle<GlobalObject*> parent)
+{
+ RegExpStaticsObject* obj = NewObjectWithGivenProto<RegExpStaticsObject>(cx, nullptr);
+ if (!obj)
+ return nullptr;
+ RegExpStatics* res = cx->new_<RegExpStatics>();
+ if (!res)
+ return nullptr;
+ obj->setPrivate(static_cast<void*>(res));
+ return obj;
+}
+
+bool
+RegExpStatics::executeLazy(JSContext* cx)
+{
+ if (!pendingLazyEvaluation)
+ return true;
+
+ MOZ_ASSERT(lazySource);
+ MOZ_ASSERT(matchesInput);
+ MOZ_ASSERT(lazyIndex != size_t(-1));
+
+ /* Retrieve or create the RegExpShared in this compartment. */
+ RegExpGuard g(cx);
+ if (!cx->compartment()->regExps.get(cx, lazySource, lazyFlags, &g))
+ return false;
+
+ /*
+ * It is not necessary to call aboutToWrite(): evaluation of
+ * implicit copies is safe.
+ */
+
+ /* Execute the full regular expression. */
+ RootedLinearString input(cx, matchesInput);
+ RegExpRunStatus status = g->execute(cx, input, lazyIndex, &this->matches, nullptr);
+ if (status == RegExpRunStatus_Error)
+ return false;
+
+ /*
+ * RegExpStatics are only updated on successful (matching) execution.
+ * Re-running the same expression must therefore produce a matching result.
+ */
+ MOZ_ASSERT(status == RegExpRunStatus_Success);
+
+ /* Unset lazy state and remove rooted values that now have no use. */
+ pendingLazyEvaluation = false;
+ lazySource = nullptr;
+ lazyIndex = size_t(-1);
+
+ return true;
+}