Source code

Revision control

Copy as Markdown

Other Tools

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* 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 "builtin/RegExp.h"
#include "mozilla/Casting.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/TextUtils.h"
#include "jsapi.h"
#include "frontend/FrontendContext.h" // AutoReportFrontendContext
#include "frontend/TokenStream.h"
#include "irregexp/RegExpAPI.h"
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_NEWREGEXP_FLAGGED
#include "js/PropertySpec.h"
#include "js/RegExpFlags.h" // JS::RegExpFlag, JS::RegExpFlags
#include "util/StringBuilder.h"
#include "vm/Interpreter.h"
#include "vm/JSContext.h"
#include "vm/RegExpObject.h"
#include "vm/RegExpStatics.h"
#include "vm/SelfHosting.h"
#include "vm/EnvironmentObject-inl.h"
#include "vm/GeckoProfiler-inl.h"
#include "vm/JSObject-inl.h"
#include "vm/ObjectOperations-inl.h"
#include "vm/PlainObject-inl.h"
using namespace js;
using mozilla::AssertedCast;
using mozilla::CheckedInt;
using mozilla::IsAsciiDigit;
using JS::CompileOptions;
using JS::RegExpFlag;
using JS::RegExpFlags;
// Allocate an object for the |.groups| or |.indices.groups| property
// of a regexp match result.
static PlainObject* CreateGroupsObject(JSContext* cx,
Handle<PlainObject*> groupsTemplate) {
if (groupsTemplate->inDictionaryMode()) {
return NewPlainObjectWithProto(cx, nullptr);
}
// The groups template object is stored in RegExpShared, which is shared
// across compartments and realms. So watch out for the case when the template
// object's realm is different from the current realm.
if (cx->realm() != groupsTemplate->realm()) {
return PlainObject::createWithTemplateFromDifferentRealm(cx,
groupsTemplate);
}
return PlainObject::createWithTemplate(cx, groupsTemplate);
}
static inline void getValueAndIndex(HandleRegExpShared re, uint32_t i,
Handle<ArrayObject*> arr,
MutableHandleValue val,
uint32_t& valueIndex) {
if (re->numNamedCaptures() == re->numDistinctNamedCaptures()) {
valueIndex = re->getNamedCaptureIndex(i);
val.set(arr->getDenseElement(valueIndex));
} else {
mozilla::Span<uint32_t> indicesSlice = re->getNamedCaptureIndices(i);
MOZ_ASSERT(!indicesSlice.IsEmpty());
valueIndex = indicesSlice[0];
for (uint32_t index : indicesSlice) {
val.set(arr->getDenseElement(index));
if (!val.isUndefined()) {
valueIndex = index;
break;
}
}
}
}
/*
* Implements RegExpBuiltinExec: Steps 18-35
*/
bool js::CreateRegExpMatchResult(JSContext* cx, HandleRegExpShared re,
HandleString input, const MatchPairs& matches,
MutableHandleValue rval) {
MOZ_ASSERT(re);
MOZ_ASSERT(input);
/*
* Create the (slow) result array for a match.
*
* Array contents:
* 0: matched string
* 1..pairCount-1: paren matches
* input: input string
* index: start index for the match
* groups: named capture groups for the match
* indices: capture indices for the match, if required
*/
bool hasIndices = re->hasIndices();
// Get the shape for the output object.
RegExpRealm::ResultShapeKind kind =
hasIndices ? RegExpRealm::ResultShapeKind::WithIndices
: RegExpRealm::ResultShapeKind::Normal;
Rooted<SharedShape*> shape(
cx, cx->global()->regExpRealm().getOrCreateMatchResultShape(cx, kind));
if (!shape) {
return false;
}
// Steps 18-19
size_t numPairs = matches.length();
MOZ_ASSERT(numPairs > 0);
// Steps 20-21: Allocate the match result object.
Rooted<ArrayObject*> arr(
cx, NewDenseFullyAllocatedArrayWithShape(cx, numPairs, shape));
if (!arr) {
return false;
}
// Steps 28-29 and 33 a-d: Initialize the elements of the match result.
// Store a Value for each match pair.
for (size_t i = 0; i < numPairs; i++) {
const MatchPair& pair = matches[i];
if (pair.isUndefined()) {
MOZ_ASSERT(i != 0); // Since we had a match, first pair must be present.
arr->setDenseInitializedLength(i + 1);
arr->initDenseElement(i, UndefinedValue());
} else {
JSLinearString* str =
NewDependentString(cx, input, pair.start, pair.length());
if (!str) {
return false;
}
arr->setDenseInitializedLength(i + 1);
arr->initDenseElement(i, StringValue(str));
}
}
// Step 34a (reordered): Allocate and initialize the indices object if needed.
// This is an inlined implementation of MakeIndicesArray:
Rooted<ArrayObject*> indices(cx);
Rooted<PlainObject*> indicesGroups(cx);
if (hasIndices) {
// MakeIndicesArray: step 8
Rooted<SharedShape*> indicesShape(
cx, cx->global()->regExpRealm().getOrCreateMatchResultShape(
cx, RegExpRealm::ResultShapeKind::Indices));
if (!indicesShape) {
return false;
}
indices = NewDenseFullyAllocatedArrayWithShape(cx, numPairs, indicesShape);
if (!indices) {
return false;
}
// MakeIndicesArray: steps 10-12
if (re->numNamedCaptures() > 0) {
Rooted<PlainObject*> groupsTemplate(cx, re->getGroupsTemplate());
indicesGroups = CreateGroupsObject(cx, groupsTemplate);
if (!indicesGroups) {
return false;
}
indices->initSlot(RegExpRealm::IndicesGroupsSlot,
ObjectValue(*indicesGroups));
}
// MakeIndicesArray: step 13 a-d. (Step 13.e is implemented below.)
for (size_t i = 0; i < numPairs; i++) {
const MatchPair& pair = matches[i];
if (pair.isUndefined()) {
// Since we had a match, first pair must be present.
MOZ_ASSERT(i != 0);
indices->setDenseInitializedLength(i + 1);
indices->initDenseElement(i, UndefinedValue());
} else {
Rooted<ArrayObject*> indexPair(cx, NewDenseFullyAllocatedArray(cx, 2));
if (!indexPair) {
return false;
}
indexPair->setDenseInitializedLength(2);
indexPair->initDenseElement(0, Int32Value(pair.start));
indexPair->initDenseElement(1, Int32Value(pair.limit));
indices->setDenseInitializedLength(i + 1);
indices->initDenseElement(i, ObjectValue(*indexPair));
}
}
}
// Steps 30-31 (reordered): Allocate the groups object (if needed).
Rooted<PlainObject*> groups(cx);
bool groupsInDictionaryMode = false;
if (re->numNamedCaptures() > 0) {
Rooted<PlainObject*> groupsTemplate(cx, re->getGroupsTemplate());
groupsInDictionaryMode = groupsTemplate->inDictionaryMode();
groups = CreateGroupsObject(cx, groupsTemplate);
if (!groups) {
return false;
}
}
// Step 33 e-f: Initialize the properties of |groups| and |indices.groups|.
// The groups template object stores the names of the named captures
// in the the order in which they are defined. The named capture
// indices vector stores the corresponding capture indices. In
// dictionary mode, we have to define the properties explicitly. If
// we are not in dictionary mode, we simply fill in the slots with
// the correct values.
if (groupsInDictionaryMode) {
RootedIdVector keys(cx);
Rooted<PlainObject*> groupsTemplate(cx, re->getGroupsTemplate());
if (!GetPropertyKeys(cx, groupsTemplate, 0, &keys)) {
return false;
}
MOZ_ASSERT(keys.length() == re->numDistinctNamedCaptures());
RootedId key(cx);
RootedValue val(cx);
uint32_t valueIndex;
for (uint32_t i = 0; i < keys.length(); i++) {
key = keys[i];
getValueAndIndex(re, i, arr, &val, valueIndex);
if (!NativeDefineDataProperty(cx, groups, key, val, JSPROP_ENUMERATE)) {
return false;
}
// MakeIndicesArray: Step 13.e (reordered)
if (hasIndices) {
val = indices->getDenseElement(valueIndex);
if (!NativeDefineDataProperty(cx, indicesGroups, key, val,
JSPROP_ENUMERATE)) {
return false;
}
}
}
} else {
RootedValue val(cx);
uint32_t valueIndex;
for (uint32_t i = 0; i < re->numDistinctNamedCaptures(); i++) {
getValueAndIndex(re, i, arr, &val, valueIndex);
groups->initSlot(i, val);
// MakeIndicesArray: Step 13.e (reordered)
if (hasIndices) {
indicesGroups->initSlot(i, indices->getDenseElement(valueIndex));
}
}
}
// Step 22 (reordered).
// Set the |index| property.
arr->initSlot(RegExpRealm::MatchResultObjectIndexSlot,
Int32Value(matches[0].start));
// Step 23 (reordered).
// Set the |input| property.
arr->initSlot(RegExpRealm::MatchResultObjectInputSlot, StringValue(input));
// Step 32 (reordered)
// Set the |groups| property.
if (groups) {
arr->initSlot(RegExpRealm::MatchResultObjectGroupsSlot,
ObjectValue(*groups));
}
// Step 34b
// Set the |indices| property.
if (re->hasIndices()) {
arr->initSlot(RegExpRealm::MatchResultObjectIndicesSlot,
ObjectValue(*indices));
}
#ifdef DEBUG
RootedValue test(cx);
RootedId id(cx, NameToId(cx->names().index));
if (!NativeGetProperty(cx, arr, id, &test)) {
return false;
}
MOZ_ASSERT(test == arr->getSlot(RegExpRealm::MatchResultObjectIndexSlot));
id = NameToId(cx->names().input);
if (!NativeGetProperty(cx, arr, id, &test)) {
return false;
}
MOZ_ASSERT(test == arr->getSlot(RegExpRealm::MatchResultObjectInputSlot));
#endif
// Step 35.
rval.setObject(*arr);
return true;
}
static int32_t CreateRegExpSearchResult(JSContext* cx,
const MatchPairs& matches) {
MOZ_ASSERT(matches[0].start >= 0);
MOZ_ASSERT(matches[0].limit >= 0);
MOZ_ASSERT(cx->regExpSearcherLastLimit == RegExpSearcherLastLimitSentinel);
#ifdef DEBUG
static_assert(JSString::MAX_LENGTH < RegExpSearcherLastLimitSentinel);
MOZ_ASSERT(uint32_t(matches[0].limit) < RegExpSearcherLastLimitSentinel);
#endif
cx->regExpSearcherLastLimit = matches[0].limit;
return matches[0].start;
}
/*
* ES 2017 draft rev 6a13789aa9e7c6de4e96b7d3e24d9e6eba6584ad 21.2.5.2.2
* steps 3, 9-14, except 12.a.i, 12.c.i.1.
*/
static RegExpRunStatus ExecuteRegExpImpl(JSContext* cx, RegExpStatics* res,
MutableHandleRegExpShared re,
Handle<JSLinearString*> input,
size_t searchIndex,
VectorMatchPairs* matches) {
RegExpRunStatus status =
RegExpShared::execute(cx, re, input, searchIndex, matches);
/* Out of spec: Update RegExpStatics. */
if (status == RegExpRunStatus::Success && res) {
if (!res->updateFromMatchPairs(cx, input, *matches)) {
return RegExpRunStatus::Error;
}
}
return status;
}
/* Legacy ExecuteRegExp behavior is baked into the JSAPI. */
bool js::ExecuteRegExpLegacy(JSContext* cx, RegExpStatics* res,
Handle<RegExpObject*> reobj,
Handle<JSLinearString*> input, size_t* lastIndex,
bool test, MutableHandleValue rval) {
cx->check(reobj, input);
RootedRegExpShared shared(cx, RegExpObject::getShared(cx, reobj));
if (!shared) {
return false;
}
VectorMatchPairs matches;
RegExpRunStatus status =
ExecuteRegExpImpl(cx, res, &shared, input, *lastIndex, &matches);
if (status == RegExpRunStatus::Error) {
return false;
}
if (status == RegExpRunStatus::Success_NotFound) {
/* ExecuteRegExp() previously returned an array or null. */
rval.setNull();
return true;
}
*lastIndex = matches[0].limit;
if (test) {
/* Forbid an array, as an optimization. */
rval.setBoolean(true);
return true;
}
return CreateRegExpMatchResult(cx, shared, input, matches, rval);
}
static bool CheckPatternSyntaxSlow(JSContext* cx, Handle<JSAtom*> pattern,
RegExpFlags flags) {
LifoAllocScope allocScope(&cx->tempLifoAlloc());
AutoReportFrontendContext fc(cx);
CompileOptions options(cx);
frontend::DummyTokenStream dummyTokenStream(&fc, options);
return irregexp::CheckPatternSyntax(cx, cx->stackLimitForCurrentPrincipal(),
dummyTokenStream, pattern, flags);
}
static RegExpShared* CheckPatternSyntax(JSContext* cx, Handle<JSAtom*> pattern,
RegExpFlags flags) {
// If we already have a RegExpShared for this pattern/flags, we can
// avoid the much slower CheckPatternSyntaxSlow call.
RootedRegExpShared shared(cx, cx->zone()->regExps().maybeGet(pattern, flags));
if (shared) {
#ifdef DEBUG
// Assert the pattern is valid.
if (!CheckPatternSyntaxSlow(cx, pattern, flags)) {
MOZ_ASSERT(cx->isThrowingOutOfMemory() || cx->isThrowingOverRecursed());
return nullptr;
}
#endif
return shared;
}
if (!CheckPatternSyntaxSlow(cx, pattern, flags)) {
return nullptr;
}
// Allocate and return a new RegExpShared so we will hit the fast path
// next time.
return cx->zone()->regExps().get(cx, pattern, flags);
}
/*
* ES 2016 draft Mar 25, 2016 21.2.3.2.2.
*
* Steps 14-15 set |obj|'s "lastIndex" property to zero. Some of
* RegExpInitialize's callers have a fresh RegExp not yet exposed to script:
* in these cases zeroing "lastIndex" is infallible. But others have a RegExp
* whose "lastIndex" property might have been made non-writable: here, zeroing
* "lastIndex" can fail. We efficiently solve this problem by completely
* removing "lastIndex" zeroing from the provided function.
*
* CALLERS MUST HANDLE "lastIndex" ZEROING THEMSELVES!
*
* Because this function only ever returns a user-provided |obj| in the spec,
* we omit it and just return the usual success/failure.
*/
static bool RegExpInitializeIgnoringLastIndex(JSContext* cx,
Handle<RegExpObject*> obj,
HandleValue patternValue,
HandleValue flagsValue) {
Rooted<JSAtom*> pattern(cx);
if (patternValue.isUndefined()) {
/* Step 1. */
pattern = cx->names().empty_;
} else {
/* Step 2. */
pattern = ToAtom<CanGC>(cx, patternValue);
if (!pattern) {
return false;
}
}
/* Step 3. */
RegExpFlags flags = RegExpFlag::NoFlags;
if (!flagsValue.isUndefined()) {
/* Step 4. */
RootedString flagStr(cx, ToString<CanGC>(cx, flagsValue));
if (!flagStr) {
return false;
}
/* Step 5. */
if (!ParseRegExpFlags(cx, flagStr, &flags)) {
return false;
}
}
/* Steps 7-8. */
RegExpShared* shared = CheckPatternSyntax(cx, pattern, flags);
if (!shared) {
return false;
}
/* Steps 9-12. */
obj->initIgnoringLastIndex(pattern, flags);
obj->setShared(shared);
return true;
}
/* ES 2016 draft Mar 25, 2016 21.2.3.2.3. */
bool js::RegExpCreate(JSContext* cx, HandleValue patternValue,
HandleValue flagsValue, MutableHandleValue rval) {
/* Step 1. */
Rooted<RegExpObject*> regexp(cx, RegExpAlloc(cx, GenericObject));
if (!regexp) {
return false;
}
/* Step 2. */
if (!RegExpInitializeIgnoringLastIndex(cx, regexp, patternValue,
flagsValue)) {
return false;
}
regexp->zeroLastIndex(cx);
rval.setObject(*regexp);
return true;
}
MOZ_ALWAYS_INLINE bool IsRegExpObject(HandleValue v) {
return v.isObject() && v.toObject().is<RegExpObject>();
}
/* ES6 draft rc3 7.2.8. */
bool js::IsRegExp(JSContext* cx, HandleValue value, bool* result) {
/* Step 1. */
if (!value.isObject()) {
*result = false;
return true;
}
RootedObject obj(cx, &value.toObject());
/* Steps 2-3. */
RootedValue isRegExp(cx);
RootedId matchId(cx, PropertyKey::Symbol(cx->wellKnownSymbols().match));
if (!GetProperty(cx, obj, obj, matchId, &isRegExp)) {
return false;
}
/* Step 4. */
if (!isRegExp.isUndefined()) {
*result = ToBoolean(isRegExp);
return true;
}
/* Steps 5-6. */
ESClass cls;
if (!GetClassOfValue(cx, value, &cls)) {
return false;
}
*result = cls == ESClass::RegExp;
return true;
}
// The "lastIndex" property is non-configurable, but it can be made
// non-writable. If CalledFromJit is true, we have emitted guards to ensure it's
// writable.
template <bool CalledFromJit = false>
static bool SetLastIndex(JSContext* cx, Handle<RegExpObject*> regexp,
int32_t lastIndex) {
MOZ_ASSERT(lastIndex >= 0);
if (CalledFromJit || MOZ_LIKELY(RegExpObject::isInitialShape(regexp)) ||
regexp->lookupPure(cx->names().lastIndex)->writable()) {
regexp->setLastIndex(cx, lastIndex);
return true;
}
Rooted<Value> val(cx, Int32Value(lastIndex));
return SetProperty(cx, regexp, cx->names().lastIndex, val);
}
/* ES6 B.2.5.1. */
MOZ_ALWAYS_INLINE bool regexp_compile_impl(JSContext* cx,
const CallArgs& args) {
MOZ_ASSERT(IsRegExpObject(args.thisv()));
Rooted<RegExpObject*> regexp(cx, &args.thisv().toObject().as<RegExpObject>());
// Step 3.
RootedValue patternValue(cx, args.get(0));
ESClass cls;
if (!GetClassOfValue(cx, patternValue, &cls)) {
return false;
}
if (cls == ESClass::RegExp) {
// Step 3a.
if (args.hasDefined(1)) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
JSMSG_NEWREGEXP_FLAGGED);
return false;
}
// Beware! |patternObj| might be a proxy into another compartment, so
// don't assume |patternObj.is<RegExpObject>()|. For the same reason,
// don't reuse the RegExpShared below.
RootedObject patternObj(cx, &patternValue.toObject());
Rooted<JSAtom*> sourceAtom(cx);
RegExpFlags flags = RegExpFlag::NoFlags;
{
// Step 3b.
RegExpShared* shared = RegExpToShared(cx, patternObj);
if (!shared) {
return false;
}
sourceAtom = shared->getSource();
flags = shared->getFlags();
}
// Step 5, minus lastIndex zeroing.
regexp->initIgnoringLastIndex(sourceAtom, flags);
} else {
// Step 4.
RootedValue P(cx, patternValue);
RootedValue F(cx, args.get(1));
// Step 5, minus lastIndex zeroing.
if (!RegExpInitializeIgnoringLastIndex(cx, regexp, P, F)) {
return false;
}
}
// The final niggling bit of step 5.
//
// |regexp| is user-exposed, so its "lastIndex" property might be
// non-writable.
if (!SetLastIndex(cx, regexp, 0)) {
return false;
}
args.rval().setObject(*regexp);
return true;
}
static bool regexp_compile(JSContext* cx, unsigned argc, Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
/* Steps 1-2. */
return CallNonGenericMethod<IsRegExpObject, regexp_compile_impl>(cx, args);
}
/*
* ES 2017 draft rev 6a13789aa9e7c6de4e96b7d3e24d9e6eba6584ad 21.2.3.1.
*/
bool js::regexp_construct(JSContext* cx, unsigned argc, Value* vp) {
AutoJSConstructorProfilerEntry pseudoFrame(cx, "RegExp");
CallArgs args = CallArgsFromVp(argc, vp);
// Steps 1.
bool patternIsRegExp;
if (!IsRegExp(cx, args.get(0), &patternIsRegExp)) {
return false;
}
// We can delay step 3 and step 4a until later, during
// GetPrototypeFromBuiltinConstructor calls. Accessing the new.target
// and the callee from the stack is unobservable.
if (!args.isConstructing()) {
// Step 3.b.
if (patternIsRegExp && !args.hasDefined(1)) {
RootedObject patternObj(cx, &args[0].toObject());
// Step 3.b.i.
RootedValue patternConstructor(cx);
if (!GetProperty(cx, patternObj, patternObj, cx->names().constructor,
&patternConstructor)) {
return false;
}
// Step 3.b.ii.
if (patternConstructor.isObject() &&
patternConstructor.toObject() == args.callee()) {
args.rval().set(args[0]);
return true;
}
}
}
RootedValue patternValue(cx, args.get(0));
// Step 4.
ESClass cls;
if (!GetClassOfValue(cx, patternValue, &cls)) {
return false;
}
if (cls == ESClass::RegExp) {
// Beware! |patternObj| might be a proxy into another compartment, so
// don't assume |patternObj.is<RegExpObject>()|.
RootedObject patternObj(cx, &patternValue.toObject());
Rooted<JSAtom*> sourceAtom(cx);
RegExpFlags flags;
RootedRegExpShared shared(cx);
{
// Step 4.a.
shared = RegExpToShared(cx, patternObj);
if (!shared) {
return false;
}
sourceAtom = shared->getSource();
// Step 4.b.
// Get original flags in all cases, to compare with passed flags.
flags = shared->getFlags();
// If the RegExpShared is in another Zone, don't reuse it.
if (cx->zone() != shared->zone()) {
shared = nullptr;
}
}
// Step 7.
RootedObject proto(cx);
if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_RegExp, &proto)) {
return false;
}
Rooted<RegExpObject*> regexp(cx, RegExpAlloc(cx, GenericObject, proto));
if (!regexp) {
return false;
}
// Step 8.
if (args.hasDefined(1)) {
// Step 4.c / 21.2.3.2.2 RegExpInitialize step 4.
RegExpFlags flagsArg = RegExpFlag::NoFlags;
RootedString flagStr(cx, ToString<CanGC>(cx, args[1]));
if (!flagStr) {
return false;
}
if (!ParseRegExpFlags(cx, flagStr, &flagsArg)) {
return false;
}
// Don't reuse the RegExpShared if we have different flags.
if (flags != flagsArg) {
shared = nullptr;
}
if (!flags.unicode() && flagsArg.unicode()) {
// Have to check syntax again when adding 'u' flag.
// ES 2017 draft rev 9b49a888e9dfe2667008a01b2754c3662059ae56
// 21.2.3.2.2 step 7.
shared = CheckPatternSyntax(cx, sourceAtom, flagsArg);
if (!shared) {
return false;
}
}
flags = flagsArg;
}
regexp->initAndZeroLastIndex(sourceAtom, flags, cx);
if (shared) {
regexp->setShared(shared);
}
args.rval().setObject(*regexp);
return true;
}
RootedValue P(cx);
RootedValue F(cx);
// Step 5.
if (patternIsRegExp) {
RootedObject patternObj(cx, &patternValue.toObject());
// Step 5.a.
if (!GetProperty(cx, patternObj, patternObj, cx->names().source, &P)) {
return false;
}
// Step 5.b.
F = args.get(1);
if (F.isUndefined()) {
if (!GetProperty(cx, patternObj, patternObj, cx->names().flags, &F)) {
return false;
}
}
} else {
// Steps 6.a-b.
P = patternValue;
F = args.get(1);
}
// Step 7.
RootedObject proto(cx);
if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_RegExp, &proto)) {
return false;
}
Rooted<RegExpObject*> regexp(cx, RegExpAlloc(cx, GenericObject, proto));
if (!regexp) {
return false;
}
// Step 8.
if (!RegExpInitializeIgnoringLastIndex(cx, regexp, P, F)) {
return false;
}
regexp->zeroLastIndex(cx);
args.rval().setObject(*regexp);
return true;
}
/*
* ES 2017 draft rev 6a13789aa9e7c6de4e96b7d3e24d9e6eba6584ad 21.2.3.1
* steps 4, 7-8.
*/
bool js::regexp_construct_raw_flags(JSContext* cx, unsigned argc, Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
MOZ_ASSERT(args.length() == 2);
MOZ_ASSERT(!args.isConstructing());
// Step 4.a.
Rooted<JSAtom*> sourceAtom(cx, AtomizeString(cx, args[0].toString()));
if (!sourceAtom) {
return false;
}
// Step 4.c.
RegExpFlags flags = AssertedCast<uint8_t>(int32_t(args[1].toNumber()));
// Step 7.
RegExpObject* regexp = RegExpAlloc(cx, GenericObject);
if (!regexp) {
return false;
}
// Step 8.
regexp->initAndZeroLastIndex(sourceAtom, flags, cx);
args.rval().setObject(*regexp);
return true;
}
// This is a specialized implementation of "UnwrapAndTypeCheckThis" for RegExp
// getters that need to return a special value for same-realm
// %RegExp.prototype%.
template <typename Fn>
static bool RegExpGetter(JSContext* cx, CallArgs& args, const char* methodName,
Fn&& fn,
HandleValue fallbackValue = UndefinedHandleValue) {
JSObject* obj = nullptr;
if (args.thisv().isObject()) {
obj = &args.thisv().toObject();
if (IsWrapper(obj)) {
obj = CheckedUnwrapStatic(obj);
if (!obj) {
ReportAccessDenied(cx);
return false;
}
}
}
if (obj) {
// Step 4ff
if (obj->is<RegExpObject>()) {
return fn(&obj->as<RegExpObject>());
}
// Step 3.a. "If SameValue(R, %RegExp.prototype%) is true, return
// undefined."
// Or `return "(?:)"` for get RegExp.prototype.source.
if (obj == cx->global()->maybeGetRegExpPrototype()) {
args.rval().set(fallbackValue);
return true;
}
// fall-through
}
// Step 2. and Step 3.b.
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr,
JSMSG_INCOMPATIBLE_REGEXP_GETTER, methodName,
InformalValueTypeName(args.thisv()));
return false;
}
bool js::regexp_hasIndices(JSContext* cx, unsigned argc, JS::Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
return RegExpGetter(cx, args, "hasIndices", [args](RegExpObject* unwrapped) {
args.rval().setBoolean(unwrapped->hasIndices());
return true;
});
}
// ES2021 draft rev 0b3a808af87a9123890767152a26599cc8fde161
// 21.2.5.5 get RegExp.prototype.global
bool js::regexp_global(JSContext* cx, unsigned argc, JS::Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
return RegExpGetter(cx, args, "global", [args](RegExpObject* unwrapped) {
args.rval().setBoolean(unwrapped->global());
return true;
});
}
// ES2021 draft rev 0b3a808af87a9123890767152a26599cc8fde161
// 21.2.5.6 get RegExp.prototype.ignoreCase
bool js::regexp_ignoreCase(JSContext* cx, unsigned argc, JS::Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
return RegExpGetter(cx, args, "ignoreCase", [args](RegExpObject* unwrapped) {
args.rval().setBoolean(unwrapped->ignoreCase());
return true;
});
}
// ES2021 draft rev 0b3a808af87a9123890767152a26599cc8fde161
// 21.2.5.9 get RegExp.prototype.multiline
bool js::regexp_multiline(JSContext* cx, unsigned argc, JS::Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
return RegExpGetter(cx, args, "multiline", [args](RegExpObject* unwrapped) {
args.rval().setBoolean(unwrapped->multiline());
return true;
});
}
// ES2021 draft rev 0b3a808af87a9123890767152a26599cc8fde161
// 21.2.5.12 get RegExp.prototype.source
static bool regexp_source(JSContext* cx, unsigned argc, JS::Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
// Step 3.a. Return "(?:)" for %RegExp.prototype%.
RootedValue fallback(cx, StringValue(cx->names().emptyRegExp_));
return RegExpGetter(
cx, args, "source",
[cx, args](RegExpObject* unwrapped) {
Rooted<JSAtom*> src(cx, unwrapped->getSource());
MOZ_ASSERT(src);
// Mark potentially cross-zone JSAtom.
if (cx->zone() != unwrapped->zone()) {
cx->markAtom(src);
}
// Step 7.
JSString* escaped = EscapeRegExpPattern(cx, src);
if (!escaped) {
return false;
}
args.rval().setString(escaped);
return true;
},
fallback);
}
// ES2021 draft rev 0b3a808af87a9123890767152a26599cc8fde161
// 21.2.5.3 get RegExp.prototype.dotAll
bool js::regexp_dotAll(JSContext* cx, unsigned argc, JS::Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
return RegExpGetter(cx, args, "dotAll", [args](RegExpObject* unwrapped) {
args.rval().setBoolean(unwrapped->dotAll());
return true;
});
}
// ES2021 draft rev 0b3a808af87a9123890767152a26599cc8fde161
// 21.2.5.14 get RegExp.prototype.sticky
bool js::regexp_sticky(JSContext* cx, unsigned argc, JS::Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
return RegExpGetter(cx, args, "sticky", [args](RegExpObject* unwrapped) {
args.rval().setBoolean(unwrapped->sticky());
return true;
});
}
// ES2021 draft rev 0b3a808af87a9123890767152a26599cc8fde161
// 21.2.5.17 get RegExp.prototype.unicode
bool js::regexp_unicode(JSContext* cx, unsigned argc, JS::Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
return RegExpGetter(cx, args, "unicode", [args](RegExpObject* unwrapped) {
args.rval().setBoolean(unwrapped->unicode());
return true;
});
}
// 21.2.6.19 get RegExp.prototype.unicodeSets
bool js::regexp_unicodeSets(JSContext* cx, unsigned argc, JS::Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
return RegExpGetter(cx, args, "unicodeSets", [args](RegExpObject* unwrapped) {
args.rval().setBoolean(unwrapped->unicodeSets());
return true;
});
}
const JSPropertySpec js::regexp_properties[] = {
JS_SELF_HOSTED_GET("flags", "$RegExpFlagsGetter", 0),
JS_PSG("hasIndices", regexp_hasIndices, 0),
JS_PSG("global", regexp_global, 0),
JS_PSG("ignoreCase", regexp_ignoreCase, 0),
JS_PSG("multiline", regexp_multiline, 0),
JS_PSG("dotAll", regexp_dotAll, 0),
JS_PSG("source", regexp_source, 0),
JS_PSG("sticky", regexp_sticky, 0),
JS_PSG("unicode", regexp_unicode, 0),
JS_PSG("unicodeSets", regexp_unicodeSets, 0),
JS_PS_END,
};
const JSFunctionSpec js::regexp_methods[] = {
JS_SELF_HOSTED_FN("toSource", "$RegExpToString", 0, 0),
JS_SELF_HOSTED_FN("toString", "$RegExpToString", 0, 0),
JS_FN("compile", regexp_compile, 2, 0),
JS_SELF_HOSTED_FN("exec", "RegExp_prototype_Exec", 1, 0),
JS_SELF_HOSTED_FN("test", "RegExpTest", 1, 0),
JS_SELF_HOSTED_SYM_FN(match, "RegExpMatch", 1, 0),
JS_SELF_HOSTED_SYM_FN(matchAll, "RegExpMatchAll", 1, 0),
JS_SELF_HOSTED_SYM_FN(replace, "RegExpReplace", 2, 0),
JS_SELF_HOSTED_SYM_FN(search, "RegExpSearch", 1, 0),
JS_SELF_HOSTED_SYM_FN(split, "RegExpSplit", 2, 0),
JS_FS_END,
};
static constexpr JS::Latin1Char SHOULD_HEX_ESCAPE = JSString::MAX_LATIN1_CHAR;
/**
* Ascii escape map.
*
* 1. If a character is mapped to zero (0x00), then no escape sequence is used.
* 2. Else,
* a. If a character is mapped to SHOULD_HEX_ESCAPE, then hex-escape.
* b. Else, escape with `\` followed by the mapped value.
*/
static constexpr auto AsciiRegExpEscapeMap() {
std::array<JS::Latin1Char, 128> result = {};
// SyntaxCharacter or U+002F (SOLIDUS)
result['^'] = '^';
result['$'] = '$';
result['\\'] = '\\';
result['.'] = '.';
result['*'] = '*';
result['+'] = '+';
result['?'] = '?';
result['('] = '(';
result[')'] = ')';
result['['] = '[';
result[']'] = ']';
result['{'] = '{';
result['}'] = '}';
result['|'] = '|';
result['/'] = '/';
// ControlEscape Code Point Values
result['\t'] = 't';
result['\n'] = 'n';
result['\v'] = 'v';
result['\f'] = 'f';
result['\r'] = 'r';
// Other punctuators ",-=<>#&!%:;@~'`" or 0x0022 (QUOTATION MARK)
result[','] = SHOULD_HEX_ESCAPE;
result['-'] = SHOULD_HEX_ESCAPE;
result['='] = SHOULD_HEX_ESCAPE;
result['<'] = SHOULD_HEX_ESCAPE;
result['>'] = SHOULD_HEX_ESCAPE;
result['#'] = SHOULD_HEX_ESCAPE;
result['&'] = SHOULD_HEX_ESCAPE;
result['!'] = SHOULD_HEX_ESCAPE;
result['%'] = SHOULD_HEX_ESCAPE;
result[':'] = SHOULD_HEX_ESCAPE;
result[';'] = SHOULD_HEX_ESCAPE;
result['@'] = SHOULD_HEX_ESCAPE;
result['~'] = SHOULD_HEX_ESCAPE;
result['\''] = SHOULD_HEX_ESCAPE;
result['`'] = SHOULD_HEX_ESCAPE;
result['"'] = SHOULD_HEX_ESCAPE;
// WhiteSpace or LineTerminator
result[' '] = SHOULD_HEX_ESCAPE;
return result;
}
/**
* EncodeForRegExpEscape ( c )
*
*/
template <typename CharT>
[[nodiscard]] static bool EncodeForRegExpEscape(
mozilla::Span<const CharT> chars, JSStringBuilder& sb) {
MOZ_ASSERT(sb.empty());
const size_t length = chars.size();
if (length == 0) {
return true;
}
static constexpr auto asciiEscapeMap = AsciiRegExpEscapeMap();
// Number of characters added when escaping.
static constexpr size_t EscapeAddLength = 2 - 1;
static constexpr size_t HexEscapeAddLength = 4 - 1;
static constexpr size_t UnicodeEscapeAddLength = 6 - 1;
// Initial scan to determine if escape sequences are needed and to compute
// the output length.
size_t outLength = length;
// Leading Ascii alpha-numeric character is hex-escaped.
size_t scanStart = 0;
if (mozilla::IsAsciiAlphanumeric(chars[0])) {
outLength += HexEscapeAddLength;
scanStart = 1;
}
for (size_t i = scanStart; i < length; i++) {
CharT ch = chars[i];
JS::Latin1Char escape = 0;
if (mozilla::IsAscii(ch)) {
escape = asciiEscapeMap[ch];
} else {
// Surrogate pair.
if (unicode::IsLeadSurrogate(ch) && i + 1 < length &&
unicode::IsTrailSurrogate(chars[i + 1])) {
i += 1;
continue;
}
// WhiteSpace or LineTerminator or unmatched surrogate.
if (unicode::IsSpace(ch) || unicode::IsSurrogate(ch)) {
escape = SHOULD_HEX_ESCAPE;
}
}
if (!escape) {
continue;
}
if (mozilla::IsAscii(escape)) {
outLength += EscapeAddLength;
} else if (ch <= JSString::MAX_LATIN1_CHAR) {
outLength += HexEscapeAddLength;
} else {
outLength += UnicodeEscapeAddLength;
}
}
// Return if no escape sequences are needed.
if (outLength == length) {
return true;
}
MOZ_ASSERT(outLength > length);
// Inflating is fallible, so we have to convert to two-byte upfront.
if constexpr (std::is_same_v<CharT, char16_t>) {
if (!sb.ensureTwoByteChars()) {
return false;
}
}
// Allocate memory for the output using the final length.
if (!sb.reserve(outLength)) {
return false;
}
// NB: Lower case hex digits.
static constexpr char HexDigits[] = "0123456789abcdef";
static_assert(std::char_traits<char>::length(HexDigits) == 16);
// Append |ch| as an escaped character.
auto appendEscape = [&](JS::Latin1Char ch) {
MOZ_ASSERT(mozilla::IsAscii(ch));
sb.infallibleAppend('\\');
sb.infallibleAppend(ch);
};
// Append |ch| as a hex-escape sequence.
auto appendHexEscape = [&](CharT ch) {
MOZ_ASSERT(ch <= JSString::MAX_LATIN1_CHAR);
sb.infallibleAppend('\\');
sb.infallibleAppend('x');
sb.infallibleAppend(HexDigits[(ch >> 4) & 0xf]);
sb.infallibleAppend(HexDigits[ch & 0xf]);
};
// Append |ch| as a Unicode-escape sequence.
auto appendUnicodeEscape = [&](char16_t ch) {
MOZ_ASSERT(ch > JSString::MAX_LATIN1_CHAR);
sb.infallibleAppend('\\');
sb.infallibleAppend('u');
sb.infallibleAppend(HexDigits[(ch >> 12) & 0xf]);
sb.infallibleAppend(HexDigits[(ch >> 8) & 0xf]);
sb.infallibleAppend(HexDigits[(ch >> 4) & 0xf]);
sb.infallibleAppend(HexDigits[ch & 0xf]);
};
// Index after the last character which produced an escape sequence.
size_t startUnescaped = 0;
// Append unescaped characters from |startUnescaped| (inclusive) to |end|
// (exclusive).
auto appendUnescaped = [&](size_t end) {
MOZ_ASSERT(startUnescaped <= end && end <= length);
if (startUnescaped < end) {
auto unescaped = chars.FromTo(startUnescaped, end);
sb.infallibleAppend(unescaped.data(), unescaped.size());
}
startUnescaped = end + 1;
};
// Leading Ascii alpha-numeric character is hex-escaped.
size_t start = 0;
if (mozilla::IsAsciiAlphanumeric(chars[0])) {
appendHexEscape(chars[0]);
start = 1;
startUnescaped = 1;
}
for (size_t i = start; i < length; i++) {
CharT ch = chars[i];
JS::Latin1Char escape = 0;
if (mozilla::IsAscii(ch)) {
escape = asciiEscapeMap[ch];
} else {
// Surrogate pair.
if (unicode::IsLeadSurrogate(ch) && i + 1 < length &&
unicode::IsTrailSurrogate(chars[i + 1])) {
i += 1;
continue;
}
// WhiteSpace or LineTerminator or unmatched surrogate.
if (unicode::IsSpace(ch) || unicode::IsSurrogate(ch)) {
escape = SHOULD_HEX_ESCAPE;
}
}
if (!escape) {
continue;
}
appendUnescaped(i);
if (mozilla::IsAscii(escape)) {
appendEscape(