| 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 |
/* JavaScript RegExp objects. */ |
| 8 |
|
| 9 |
#ifndef vm_RegExpObject_h |
| 10 |
#define vm_RegExpObject_h |
| 11 |
|
| 12 |
#include "builtin/SelfHostingDefines.h" |
| 13 |
#include "js/RegExpFlags.h" |
| 14 |
#include "proxy/Proxy.h" |
| 15 |
#include "vm/JSAtomState.h" |
| 16 |
#include "vm/JSContext.h" |
| 17 |
#include "vm/RegExpShared.h" |
| 18 |
#include "vm/Shape.h" |
| 19 |
|
| 20 |
/* |
| 21 |
* JavaScript Regular Expressions |
| 22 |
* |
| 23 |
* There are several engine concepts associated with a single logical regexp: |
| 24 |
* |
| 25 |
* RegExpObject: |
| 26 |
* The JS-visible object whose .[[Class]] equals "RegExp". |
| 27 |
* RegExpShared: |
| 28 |
* The compiled representation of the regexp (lazily created, cleared |
| 29 |
* during some forms of GC). |
| 30 |
* RegExpZone: |
| 31 |
* Owns all RegExpShared instances in a zone. |
| 32 |
*/ |
| 33 |
namespace js { |
| 34 |
|
| 35 |
class GenericPrinter; |
| 36 |
class JSONPrinter; |
| 37 |
|
| 38 |
extern RegExpObject* RegExpAlloc(JSContext* cx, NewObjectKind newKind, |
| 39 |
HandleObject proto = nullptr); |
| 40 |
|
| 41 |
extern JSObject* CloneRegExpObject(JSContext* cx, Handle<RegExpObject*> regex); |
| 42 |
|
| 43 |
class RegExpObject : public NativeObject { |
| 44 |
static const unsigned LAST_INDEX_SLOT = 0; |
| 45 |
static const unsigned SOURCE_SLOT = 1; |
| 46 |
static const unsigned FLAGS_SLOT = 2; |
| 47 |
|
| 48 |
static_assert(RegExpObject::FLAGS_SLOT == REGEXP_FLAGS_SLOT, |
| 49 |
"FLAGS_SLOT values should be in sync with self-hosted JS"); |
| 50 |
|
| 51 |
static RegExpObject* create(JSContext* cx, Handle<JSAtom*> source, |
| 52 |
NewObjectKind newKind); |
| 53 |
|
| 54 |
public: |
| 55 |
static const unsigned SHARED_SLOT = 3; |
| 56 |
static const unsigned RESERVED_SLOTS = 4; |
| 57 |
|
| 58 |
// This must match RESERVED_SLOTS. See assertions in CloneRegExpObject. |
| 59 |
static constexpr gc::AllocKind AllocKind = gc::AllocKind::OBJECT4; |
| 60 |
|
| 61 |
static const JSClass class_; |
| 62 |
static const JSClass protoClass_; |
| 63 |
|
| 64 |
// The maximum number of pairs a MatchResult can have, without having to |
| 65 |
// allocate a bigger MatchResult. |
| 66 |
static const size_t MaxPairCount = 14; |
| 67 |
|
| 68 |
template <typename CharT> |
| 69 |
static RegExpObject* create(JSContext* cx, const CharT* chars, size_t length, |
| 70 |
JS::RegExpFlags flags, NewObjectKind newKind); |
| 71 |
|
| 72 |
// This variant assumes that the characters have already previously been |
| 73 |
// syntax checked. |
| 74 |
static RegExpObject* createSyntaxChecked(JSContext* cx, |
| 75 |
Handle<JSAtom*> source, |
| 76 |
JS::RegExpFlags flags, |
| 77 |
NewObjectKind newKind); |
| 78 |
|
| 79 |
static RegExpObject* create(JSContext* cx, Handle<JSAtom*> source, |
| 80 |
JS::RegExpFlags flags, NewObjectKind newKind); |
| 81 |
|
| 82 |
/* |
| 83 |
* Compute the initial shape to associate with fresh RegExp objects, |
| 84 |
* encoding their initial properties. Return the shape after |
| 85 |
* changing |obj|'s last property to it. |
| 86 |
*/ |
| 87 |
static SharedShape* assignInitialShape(JSContext* cx, |
| 88 |
Handle<RegExpObject*> obj); |
| 89 |
|
| 90 |
/* Accessors. */ |
| 91 |
|
| 92 |
static constexpr size_t lastIndexSlot() { return LAST_INDEX_SLOT; } |
| 93 |
|
| 94 |
static constexpr size_t offsetOfLastIndex() { |
| 95 |
return getFixedSlotOffset(lastIndexSlot()); |
| 96 |
} |
| 97 |
|
| 98 |
static bool isInitialShape(RegExpObject* rx) { |
| 99 |
// RegExpObject has a non-configurable lastIndex property, so there must be |
| 100 |
// at least one property. Even though lastIndex is non-configurable, it can |
| 101 |
// be made non-writable, so we have to check if it's still writable. |
| 102 |
MOZ_ASSERT(!rx->empty()); |
| 103 |
PropertyInfoWithKey prop = rx->getLastProperty(); |
| 104 |
return prop.isDataProperty() && prop.slot() == LAST_INDEX_SLOT && |
| 105 |
prop.writable(); |
| 106 |
} |
| 107 |
|
| 108 |
const Value& getLastIndex() const { return getReservedSlot(LAST_INDEX_SLOT); } |
| 109 |
|
| 110 |
void setLastIndex(JSContext* cx, int32_t lastIndex) { |
| 111 |
MOZ_ASSERT(lastIndex >= 0); |
| 112 |
MOZ_ASSERT(lookupPure(cx->names().lastIndex)->writable(), |
| 113 |
"can't infallibly set a non-writable lastIndex on a " |
| 114 |
"RegExp that's been exposed to script"); |
| 115 |
setReservedSlot(LAST_INDEX_SLOT, Int32Value(lastIndex)); |
| 116 |
} |
| 117 |
void zeroLastIndex(JSContext* cx) { setLastIndex(cx, 0); } |
| 118 |
|
| 119 |
static JSLinearString* toString(JSContext* cx, Handle<RegExpObject*> obj); |
| 120 |
|
| 121 |
JSAtom* getSource() const { |
| 122 |
return &getReservedSlot(SOURCE_SLOT).toString()->asAtom(); |
| 123 |
} |
| 124 |
|
| 125 |
void setSource(JSAtom* source) { |
| 126 |
setReservedSlot(SOURCE_SLOT, StringValue(source)); |
| 127 |
} |
| 128 |
|
| 129 |
/* Flags. */ |
| 130 |
|
| 131 |
static constexpr size_t flagsSlot() { return FLAGS_SLOT; } |
| 132 |
|
| 133 |
static constexpr size_t offsetOfFlags() { |
| 134 |
return getFixedSlotOffset(flagsSlot()); |
| 135 |
} |
| 136 |
|
| 137 |
static constexpr size_t offsetOfShared() { |
| 138 |
return getFixedSlotOffset(SHARED_SLOT); |
| 139 |
} |
| 140 |
|
| 141 |
JS::RegExpFlags getFlags() const { |
| 142 |
return JS::RegExpFlags(getFixedSlot(FLAGS_SLOT).toInt32()); |
| 143 |
} |
| 144 |
void setFlags(JS::RegExpFlags flags) { |
| 145 |
setFixedSlot(FLAGS_SLOT, Int32Value(flags.value())); |
| 146 |
} |
| 147 |
|
| 148 |
bool hasIndices() const { return getFlags().hasIndices(); } |
| 149 |
bool global() const { return getFlags().global(); } |
| 150 |
bool ignoreCase() const { return getFlags().ignoreCase(); } |
| 151 |
bool multiline() const { return getFlags().multiline(); } |
| 152 |
bool dotAll() const { return getFlags().dotAll(); } |
| 153 |
bool unicode() const { return getFlags().unicode(); } |
| 154 |
bool unicodeSets() const { return getFlags().unicodeSets(); } |
| 155 |
bool sticky() const { return getFlags().sticky(); } |
| 156 |
|
| 157 |
bool isGlobalOrSticky() const { |
| 158 |
JS::RegExpFlags flags = getFlags(); |
| 159 |
return flags.global() || flags.sticky(); |
| 160 |
} |
| 161 |
|
| 162 |
static bool isOriginalFlagGetter(JSNative native, JS::RegExpFlags* mask); |
| 163 |
|
| 164 |
static RegExpShared* getShared(JSContext* cx, Handle<RegExpObject*> regexp); |
| 165 |
|
| 166 |
bool hasShared() const { return !getFixedSlot(SHARED_SLOT).isUndefined(); } |
| 167 |
|
| 168 |
RegExpShared* getShared() const { |
| 169 |
return static_cast<RegExpShared*>(getFixedSlot(SHARED_SLOT).toGCThing()); |
| 170 |
} |
| 171 |
|
| 172 |
void setShared(RegExpShared* shared) { |
| 173 |
MOZ_ASSERT(shared); |
| 174 |
setFixedSlot(SHARED_SLOT, PrivateGCThingValue(shared)); |
| 175 |
} |
| 176 |
|
| 177 |
void clearShared() { setFixedSlot(SHARED_SLOT, UndefinedValue()); } |
| 178 |
|
| 179 |
void initIgnoringLastIndex(JSAtom* source, JS::RegExpFlags flags); |
| 180 |
|
| 181 |
// NOTE: This method is *only* safe to call on RegExps that haven't been |
| 182 |
// exposed to script, because it requires that the "lastIndex" |
| 183 |
// property be writable. |
| 184 |
void initAndZeroLastIndex(JSAtom* source, JS::RegExpFlags flags, |
| 185 |
JSContext* cx); |
| 186 |
|
| 187 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 188 |
void dumpOwnFields(js::JSONPrinter& json) const; |
| 189 |
void dumpOwnStringContent(js::GenericPrinter& out) const; |
| 190 |
#endif |
| 191 |
|
| 192 |
private: |
| 193 |
/* |
| 194 |
* Precondition: the syntax for |source| has already been validated. |
| 195 |
* Side effect: sets the private field. |
| 196 |
*/ |
| 197 |
static RegExpShared* createShared(JSContext* cx, |
| 198 |
Handle<RegExpObject*> regexp); |
| 199 |
|
| 200 |
/* Call setShared in preference to setPrivate. */ |
| 201 |
void setPrivate(void* priv) = delete; |
| 202 |
}; |
| 203 |
|
| 204 |
/* |
| 205 |
* Parse regexp flags. Report an error and return false if an invalid |
| 206 |
* sequence of flags is encountered (repeat/invalid flag). |
| 207 |
* |
| 208 |
* N.B. flagStr must be rooted. |
| 209 |
*/ |
| 210 |
bool ParseRegExpFlags(JSContext* cx, JSString* flagStr, |
| 211 |
JS::RegExpFlags* flagsOut); |
| 212 |
|
| 213 |
// Assuming GetBuiltinClass(obj) is ESClass::RegExp, return a RegExpShared for |
| 214 |
// obj. |
| 215 |
inline RegExpShared* RegExpToShared(JSContext* cx, HandleObject obj) { |
| 216 |
if (obj->is<RegExpObject>()) { |
| 217 |
return RegExpObject::getShared(cx, obj.as<RegExpObject>()); |
| 218 |
} |
| 219 |
|
| 220 |
return Proxy::regexp_toShared(cx, obj); |
| 221 |
} |
| 222 |
|
| 223 |
/* Escape all slashes and newlines in the given string. */ |
| 224 |
extern JSLinearString* EscapeRegExpPattern(JSContext* cx, Handle<JSAtom*> src); |
| 225 |
|
| 226 |
template <typename CharT> |
| 227 |
extern bool HasRegExpMetaChars(const CharT* chars, size_t length); |
| 228 |
|
| 229 |
extern bool StringHasRegExpMetaChars(const JSLinearString* str); |
| 230 |
|
| 231 |
} /* namespace js */ |
| 232 |
|
| 233 |
#endif /* vm_RegExpObject_h */ |