js/src/vm/MatchPairs.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     2  * vim: set ts=8 sts=4 et sw=4 tw=99:
     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/. */
     7 #ifndef vm_MatchPairs_h
     8 #define vm_MatchPairs_h
    10 #include "jsalloc.h"
    12 #include "ds/LifoAlloc.h"
    13 #include "js/Vector.h"
    15 /*
    16  * RegExp match results are succinctly represented by pairs of integer
    17  * indices delimiting (start, limit] segments of the input string.
    18  *
    19  * The pair count for a given RegExp match is the capturing parentheses
    20  * count plus one for the "0 capturing paren" whole text match.
    21  */
    23 namespace js {
    25 struct MatchPair
    26 {
    27     int start;
    28     int limit;
    30     MatchPair()
    31       : start(-1), limit(-1)
    32     { }
    34     MatchPair(int start, int limit)
    35       : start(start), limit(limit)
    36     { }
    38     size_t length()      const { JS_ASSERT(!isUndefined()); return limit - start; }
    39     bool isEmpty()       const { return length() == 0; }
    40     bool isUndefined()   const { return start < 0; }
    42     void displace(size_t amount) {
    43         start += (start < 0) ? 0 : amount;
    44         limit += (limit < 0) ? 0 : amount;
    45     }
    47     inline bool check() const {
    48         JS_ASSERT(limit >= start);
    49         JS_ASSERT_IF(start < 0, start == -1);
    50         JS_ASSERT_IF(limit < 0, limit == -1);
    51         return true;
    52     }
    53 };
    55 /* Base class for RegExp execution output. */
    56 class MatchPairs
    57 {
    58   protected:
    59     size_t     pairCount_;   /* Length of pairs_. */
    60     MatchPair *pairs_;       /* Raw pointer into an allocated MatchPair buffer. */
    62   protected:
    63     /* Not used directly: use ScopedMatchPairs or VectorMatchPairs. */
    64     MatchPairs()
    65       : pairCount_(0), pairs_(nullptr)
    66     { }
    68   protected:
    69     /* Functions used by friend classes. */
    70     friend class RegExpShared;
    71     friend class RegExpStatics;
    73     /* MatchPair buffer allocator: set pairs_ and pairCount_. */
    74     virtual bool allocOrExpandArray(size_t pairCount) = 0;
    76     bool initArray(size_t pairCount);
    77     bool initArrayFrom(MatchPairs &copyFrom);
    78     void forgetArray() { pairs_ = nullptr; }
    80     void displace(size_t disp);
    81     void checkAgainst(size_t inputLength) {
    82 #ifdef DEBUG
    83         for (size_t i = 0; i < pairCount_; i++) {
    84             const MatchPair &p = pair(i);
    85             JS_ASSERT(p.check());
    86             if (p.isUndefined())
    87                 continue;
    88             JS_ASSERT(size_t(p.limit) <= inputLength);
    89         }
    90 #endif
    91     }
    93   public:
    94     /* Querying functions in the style of RegExpStatics. */
    95     bool   empty() const           { return pairCount_ == 0; }
    96     size_t pairCount() const       { JS_ASSERT(pairCount_ > 0); return pairCount_; }
    97     size_t parenCount() const      { return pairCount_ - 1; }
    99   public:
   100     unsigned *rawBuf() const { return reinterpret_cast<unsigned *>(pairs_); }
   101     size_t length() const { return pairCount_; }
   103     /* Pair accessors. */
   104     const MatchPair &pair(size_t i) const {
   105         JS_ASSERT(pairCount_ && i < pairCount_);
   106         JS_ASSERT(pairs_);
   107         return pairs_[i];
   108     }
   110     const MatchPair &operator[](size_t i) const { return pair(i); }
   111 };
   113 /* MatchPairs allocated into temporary storage, removed when out of scope. */
   114 class ScopedMatchPairs : public MatchPairs
   115 {
   116     LifoAllocScope lifoScope_;
   118   public:
   119     /* Constructs an implicit LifoAllocScope. */
   120     ScopedMatchPairs(LifoAlloc *lifoAlloc)
   121       : lifoScope_(lifoAlloc)
   122     { }
   124     const MatchPair &operator[](size_t i) const { return pair(i); }
   126   protected:
   127     bool allocOrExpandArray(size_t pairCount);
   128 };
   130 /*
   131  * MatchPairs allocated into permanent storage, for RegExpStatics.
   132  * The Vector of MatchPairs is reusable by Vector expansion.
   133  */
   134 class VectorMatchPairs : public MatchPairs
   135 {
   136     Vector<MatchPair, 10, SystemAllocPolicy> vec_;
   138   public:
   139     VectorMatchPairs() {
   140         vec_.clear();
   141     }
   143     const MatchPair &operator[](size_t i) const { return pair(i); }
   145   protected:
   146     friend class RegExpStatics;
   147     bool allocOrExpandArray(size_t pairCount);
   148 };
   150 /*
   151  * Passes either MatchPair or MatchPairs through ExecuteRegExp()
   152  * to avoid duplication of generic code.
   153  */
   154 struct MatchConduit
   155 {
   156     union {
   157         MatchPair  *pair;
   158         MatchPairs *pairs;
   159     } u;
   160     bool isPair;
   162     explicit MatchConduit(MatchPair *pair) {
   163         isPair = true;
   164         u.pair = pair;
   165     }
   166     explicit MatchConduit(MatchPairs *pairs) {
   167         isPair = false;
   168         u.pairs = pairs;
   169     }
   170 };
   172 } /* namespace js */
   174 #endif /* vm_MatchPairs_h */

mercurial