darling-JavaScriptCore/runtime/RegExpObject.cpp

197 lines
7.7 KiB
C++
Raw Normal View History

2017-08-12 16:48:01 +00:00
/*
* Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
* Copyright (C) 2003-2019 Apple Inc. All Rights Reserved.
2017-08-12 16:48:01 +00:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "config.h"
#include "RegExpObject.h"
#include "RegExpObjectInlines.h"
namespace JSC {
STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(RegExpObject);
2018-01-03 05:16:05 +00:00
const ClassInfo RegExpObject::s_info = { "RegExp", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(RegExpObject) };
2017-08-12 16:48:01 +00:00
static JSC_DECLARE_CUSTOM_SETTER(regExpObjectSetLastIndexStrict);
static JSC_DECLARE_CUSTOM_SETTER(regExpObjectSetLastIndexNonStrict);
2017-08-12 16:48:01 +00:00
RegExpObject::RegExpObject(VM& vm, Structure* structure, RegExp* regExp)
: JSNonFinalObject(vm, structure)
2020-08-29 13:27:11 +00:00
, m_regExpAndLastIndexIsNotWritableFlag(bitwise_cast<uintptr_t>(regExp)) // lastIndexIsNotWritableFlag is not set.
2017-08-12 16:48:01 +00:00
{
m_lastIndex.setWithoutWriteBarrier(jsNumber(0));
}
void RegExpObject::finishCreation(VM& vm)
{
Base::finishCreation(vm);
2018-01-03 05:16:05 +00:00
ASSERT(inherits(vm, info()));
2020-08-29 13:27:11 +00:00
ASSERT(type() == RegExpObjectType);
2017-08-12 16:48:01 +00:00
}
void RegExpObject::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
RegExpObject* thisObject = jsCast<RegExpObject*>(cell);
ASSERT_GC_OBJECT_INHERITS(thisObject, info());
Base::visitChildren(thisObject, visitor);
2020-08-29 13:27:11 +00:00
visitor.appendUnbarriered(thisObject->regExp());
2017-08-12 16:48:01 +00:00
visitor.append(thisObject->m_lastIndex);
}
bool RegExpObject::getOwnPropertySlot(JSObject* object, JSGlobalObject* globalObject, PropertyName propertyName, PropertySlot& slot)
2017-08-12 16:48:01 +00:00
{
VM& vm = globalObject->vm();
2020-08-29 13:27:11 +00:00
if (propertyName == vm.propertyNames->lastIndex) {
RegExpObject* regExp = jsCast<RegExpObject*>(object);
unsigned attributes = regExp->lastIndexIsWritable() ? PropertyAttribute::DontDelete | PropertyAttribute::DontEnum : PropertyAttribute::DontDelete | PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly;
2017-08-12 16:48:01 +00:00
slot.setValue(regExp, attributes, regExp->getLastIndex());
return true;
}
return Base::getOwnPropertySlot(object, globalObject, propertyName, slot);
2017-08-12 16:48:01 +00:00
}
bool RegExpObject::deleteProperty(JSCell* cell, JSGlobalObject* globalObject, PropertyName propertyName, DeletePropertySlot& slot)
2017-08-12 16:48:01 +00:00
{
VM& vm = globalObject->vm();
2020-08-29 13:27:11 +00:00
if (propertyName == vm.propertyNames->lastIndex)
2017-08-12 16:48:01 +00:00
return false;
return Base::deleteProperty(cell, globalObject, propertyName, slot);
2017-08-12 16:48:01 +00:00
}
void RegExpObject::getOwnSpecialPropertyNames(JSObject*, JSGlobalObject* globalObject, PropertyNameArray& propertyNames, DontEnumPropertiesMode mode)
2017-08-12 16:48:01 +00:00
{
VM& vm = globalObject->vm();
if (mode == DontEnumPropertiesMode::Include)
2020-08-29 13:27:11 +00:00
propertyNames.add(vm.propertyNames->lastIndex);
2017-08-12 16:48:01 +00:00
}
bool RegExpObject::defineOwnProperty(JSObject* object, JSGlobalObject* globalObject, PropertyName propertyName, const PropertyDescriptor& descriptor, bool shouldThrow)
2017-08-12 16:48:01 +00:00
{
VM& vm = globalObject->vm();
2017-08-12 16:48:01 +00:00
auto scope = DECLARE_THROW_SCOPE(vm);
if (propertyName == vm.propertyNames->lastIndex) {
2020-08-29 13:27:11 +00:00
RegExpObject* regExp = jsCast<RegExpObject*>(object);
2017-08-12 16:48:01 +00:00
if (descriptor.configurablePresent() && descriptor.configurable())
return typeError(globalObject, scope, shouldThrow, UnconfigurablePropertyChangeConfigurabilityError);
2017-08-12 16:48:01 +00:00
if (descriptor.enumerablePresent() && descriptor.enumerable())
return typeError(globalObject, scope, shouldThrow, UnconfigurablePropertyChangeEnumerabilityError);
2017-08-12 16:48:01 +00:00
if (descriptor.isAccessorDescriptor())
return typeError(globalObject, scope, shouldThrow, UnconfigurablePropertyChangeAccessMechanismError);
2020-08-29 13:27:11 +00:00
if (!regExp->lastIndexIsWritable()) {
2017-08-12 16:48:01 +00:00
if (descriptor.writablePresent() && descriptor.writable())
return typeError(globalObject, scope, shouldThrow, UnconfigurablePropertyChangeWritabilityError);
if (descriptor.value()) {
bool isSame = sameValue(globalObject, regExp->getLastIndex(), descriptor.value());
RETURN_IF_EXCEPTION(scope, false);
if (!isSame)
return typeError(globalObject, scope, shouldThrow, ReadonlyPropertyChangeError);
}
2017-08-12 16:48:01 +00:00
return true;
}
if (descriptor.value()) {
regExp->setLastIndex(globalObject, descriptor.value(), false);
2017-08-12 16:48:01 +00:00
RETURN_IF_EXCEPTION(scope, false);
}
if (descriptor.writablePresent() && !descriptor.writable())
2020-08-29 13:27:11 +00:00
regExp->setLastIndexIsNotWritable();
2017-08-12 16:48:01 +00:00
return true;
}
RELEASE_AND_RETURN(scope, Base::defineOwnProperty(object, globalObject, propertyName, descriptor, shouldThrow));
2017-08-12 16:48:01 +00:00
}
JSC_DEFINE_CUSTOM_SETTER(regExpObjectSetLastIndexStrict, (JSGlobalObject* globalObject, EncodedJSValue thisValue, EncodedJSValue value))
2017-08-12 16:48:01 +00:00
{
return jsCast<RegExpObject*>(JSValue::decode(thisValue))->setLastIndex(globalObject, JSValue::decode(value), true);
2017-08-12 16:48:01 +00:00
}
JSC_DEFINE_CUSTOM_SETTER(regExpObjectSetLastIndexNonStrict, (JSGlobalObject* globalObject, EncodedJSValue thisValue, EncodedJSValue value))
2017-08-12 16:48:01 +00:00
{
return jsCast<RegExpObject*>(JSValue::decode(thisValue))->setLastIndex(globalObject, JSValue::decode(value), false);
2017-08-12 16:48:01 +00:00
}
bool RegExpObject::put(JSCell* cell, JSGlobalObject* globalObject, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
2017-08-12 16:48:01 +00:00
{
VM& vm = globalObject->vm();
2017-08-12 16:48:01 +00:00
RegExpObject* thisObject = jsCast<RegExpObject*>(cell);
if (UNLIKELY(isThisValueAltered(slot, thisObject)))
return ordinarySetSlow(globalObject, thisObject, propertyName, value, slot.thisValue(), slot.isStrictMode());
2017-08-12 16:48:01 +00:00
2020-08-29 13:27:11 +00:00
if (propertyName == vm.propertyNames->lastIndex) {
bool result = thisObject->setLastIndex(globalObject, value, slot.isStrictMode());
2020-08-29 13:27:11 +00:00
slot.setCustomValue(thisObject, slot.isStrictMode()
2017-08-12 16:48:01 +00:00
? regExpObjectSetLastIndexStrict
: regExpObjectSetLastIndexNonStrict);
return result;
}
return Base::put(cell, globalObject, propertyName, value, slot);
}
String RegExpObject::toStringName(const JSObject*, JSGlobalObject*)
{
return "RegExp"_s;
2017-08-12 16:48:01 +00:00
}
JSValue RegExpObject::exec(JSGlobalObject* globalObject, JSString* string)
2017-08-12 16:48:01 +00:00
{
return execInline(globalObject, string);
2017-08-12 16:48:01 +00:00
}
// Shared implementation used by test and exec.
MatchResult RegExpObject::match(JSGlobalObject* globalObject, JSString* string)
2017-08-12 16:48:01 +00:00
{
return matchInline(globalObject, string);
2017-08-12 16:48:01 +00:00
}
JSValue RegExpObject::matchGlobal(JSGlobalObject* globalObject, JSString* string)
2017-08-12 16:48:01 +00:00
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
RegExp* regExp = this->regExp();
ASSERT(regExp->global());
setLastIndex(globalObject, 0);
2017-08-12 16:48:01 +00:00
RETURN_IF_EXCEPTION(scope, { });
String s = string->value(globalObject);
2020-08-29 13:27:11 +00:00
RETURN_IF_EXCEPTION(scope, { });
ASSERT(!s.isNull());
2017-08-12 16:48:01 +00:00
if (regExp->unicode()) {
unsigned stringLength = s.length();
2020-08-29 13:27:11 +00:00
RELEASE_AND_RETURN(scope, collectMatches(
vm, globalObject, string, s, regExp,
2017-08-12 16:48:01 +00:00
[&] (size_t end) -> size_t {
return advanceStringUnicode(s, stringLength, end);
2020-08-29 13:27:11 +00:00
}));
2017-08-12 16:48:01 +00:00
}
2020-08-29 13:27:11 +00:00
RELEASE_AND_RETURN(scope, collectMatches(
vm, globalObject, string, s, regExp,
2017-08-12 16:48:01 +00:00
[&] (size_t end) -> size_t {
return end + 1;
2020-08-29 13:27:11 +00:00
}));
2017-08-12 16:48:01 +00:00
}
} // namespace JSC