diff options
author | Konstantin Tokarev <annulen@yandex.ru> | 2016-08-25 19:20:41 +0300 |
---|---|---|
committer | Konstantin Tokarev <annulen@yandex.ru> | 2017-02-02 12:30:55 +0000 |
commit | 6882a04fb36642862b11efe514251d32070c3d65 (patch) | |
tree | b7959826000b061fd5ccc7512035c7478742f7b0 /Source/JavaScriptCore/runtime/RegExpObject.cpp | |
parent | ab6df191029eeeb0b0f16f127d553265659f739e (diff) | |
download | qtwebkit-6882a04fb36642862b11efe514251d32070c3d65.tar.gz |
Imported QtWebKit TP3 (git b57bc6801f1876c3220d5a4bfea33d620d477443)
Change-Id: I3b1d8a2808782c9f34d50240000e20cb38d3680f
Reviewed-by: Konstantin Tokarev <annulen@yandex.ru>
Diffstat (limited to 'Source/JavaScriptCore/runtime/RegExpObject.cpp')
-rw-r--r-- | Source/JavaScriptCore/runtime/RegExpObject.cpp | 213 |
1 files changed, 35 insertions, 178 deletions
diff --git a/Source/JavaScriptCore/runtime/RegExpObject.cpp b/Source/JavaScriptCore/runtime/RegExpObject.cpp index 8b2d03c02..d61ec74e9 100644 --- a/Source/JavaScriptCore/runtime/RegExpObject.cpp +++ b/Source/JavaScriptCore/runtime/RegExpObject.cpp @@ -28,85 +28,51 @@ #include "JSArray.h" #include "JSGlobalObject.h" #include "JSString.h" -#include "Lexer.h" #include "Lookup.h" -#include "Operations.h" +#include "JSCInlines.h" #include "RegExpConstructor.h" #include "RegExpMatchesArray.h" #include "RegExpPrototype.h" -#include <wtf/PassOwnPtr.h> #include <wtf/text/StringBuilder.h> namespace JSC { -static JSValue regExpObjectGlobal(ExecState*, JSValue, PropertyName); -static JSValue regExpObjectIgnoreCase(ExecState*, JSValue, PropertyName); -static JSValue regExpObjectMultiline(ExecState*, JSValue, PropertyName); -static JSValue regExpObjectSource(ExecState*, JSValue, PropertyName); +STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(RegExpObject); -} // namespace JSC - -#include "RegExpObject.lut.h" - -namespace JSC { - -ASSERT_HAS_TRIVIAL_DESTRUCTOR(RegExpObject); - -const ClassInfo RegExpObject::s_info = { "RegExp", &Base::s_info, 0, ExecState::regExpTable, CREATE_METHOD_TABLE(RegExpObject) }; - -/* Source for RegExpObject.lut.h -@begin regExpTable - global regExpObjectGlobal DontDelete|ReadOnly|DontEnum - ignoreCase regExpObjectIgnoreCase DontDelete|ReadOnly|DontEnum - multiline regExpObjectMultiline DontDelete|ReadOnly|DontEnum - source regExpObjectSource DontDelete|ReadOnly|DontEnum -@end -*/ +const ClassInfo RegExpObject::s_info = { "RegExp", &Base::s_info, nullptr, CREATE_METHOD_TABLE(RegExpObject) }; -RegExpObject::RegExpObject(JSGlobalObject* globalObject, Structure* structure, RegExp* regExp) - : JSNonFinalObject(globalObject->vm(), structure) - , m_regExp(globalObject->vm(), this, regExp) +RegExpObject::RegExpObject(VM& vm, Structure* structure, RegExp* regExp) + : JSNonFinalObject(vm, structure) + , m_regExp(vm, this, regExp) , m_lastIndexIsWritable(true) { m_lastIndex.setWithoutWriteBarrier(jsNumber(0)); } -void RegExpObject::finishCreation(JSGlobalObject* globalObject) +void RegExpObject::finishCreation(VM& vm) { - Base::finishCreation(globalObject->vm()); - ASSERT(inherits(&s_info)); + Base::finishCreation(vm); + ASSERT(inherits(info())); } void RegExpObject::visitChildren(JSCell* cell, SlotVisitor& visitor) { RegExpObject* thisObject = jsCast<RegExpObject*>(cell); - ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info); - COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag); - ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren()); - + ASSERT_GC_OBJECT_INHERITS(thisObject, info()); Base::visitChildren(thisObject, visitor); visitor.append(&thisObject->m_regExp); visitor.append(&thisObject->m_lastIndex); } -bool RegExpObject::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot& slot) -{ - if (propertyName == exec->propertyNames().lastIndex) { - RegExpObject* regExp = asRegExpObject(cell); - slot.setValue(regExp, regExp->getLastIndex()); - return true; - } - return getStaticValueSlot<RegExpObject, JSObject>(exec, ExecState::regExpTable(exec), jsCast<RegExpObject*>(cell), propertyName, slot); -} - -bool RegExpObject::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor) +bool RegExpObject::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot) { if (propertyName == exec->propertyNames().lastIndex) { RegExpObject* regExp = asRegExpObject(object); - descriptor.setDescriptor(regExp->getLastIndex(), regExp->m_lastIndexIsWritable ? DontDelete | DontEnum : DontDelete | DontEnum | ReadOnly); + unsigned attributes = regExp->m_lastIndexIsWritable ? DontDelete | DontEnum : DontDelete | DontEnum | ReadOnly; + slot.setValue(regExp, attributes, regExp->getLastIndex()); return true; } - return getStaticValueDescriptor<RegExpObject, JSObject>(exec, ExecState::regExpTable(exec), jsCast<RegExpObject*>(object), propertyName, descriptor); + return Base::getOwnPropertySlot(object, exec, propertyName, slot); } bool RegExpObject::deleteProperty(JSCell* cell, ExecState* exec, PropertyName propertyName) @@ -118,18 +84,25 @@ bool RegExpObject::deleteProperty(JSCell* cell, ExecState* exec, PropertyName pr void RegExpObject::getOwnNonIndexPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode) { - if (mode == IncludeDontEnumProperties) + if (mode.includeDontEnumProperties()) propertyNames.add(exec->propertyNames().lastIndex); Base::getOwnNonIndexPropertyNames(object, exec, propertyNames, mode); } void RegExpObject::getPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode) { - if (mode == IncludeDontEnumProperties) + if (mode.includeDontEnumProperties()) propertyNames.add(exec->propertyNames().lastIndex); Base::getPropertyNames(object, exec, propertyNames, mode); } +void RegExpObject::getGenericPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode) +{ + if (mode.includeDontEnumProperties()) + propertyNames.add(exec->propertyNames().lastIndex); + Base::getGenericPropertyNames(object, exec, propertyNames, mode); +} + static bool reject(ExecState* exec, bool throwException, const char* message) { if (throwException) @@ -137,7 +110,7 @@ static bool reject(ExecState* exec, bool throwException, const char* message) return false; } -bool RegExpObject::defineOwnProperty(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor, bool shouldThrow) +bool RegExpObject::defineOwnProperty(JSObject* object, ExecState* exec, PropertyName propertyName, const PropertyDescriptor& descriptor, bool shouldThrow) { if (propertyName == exec->propertyNames().lastIndex) { RegExpObject* regExp = asRegExpObject(object); @@ -154,158 +127,42 @@ bool RegExpObject::defineOwnProperty(JSObject* object, ExecState* exec, Property return reject(exec, shouldThrow, "Attempting to change value of a readonly property."); return true; } - if (descriptor.writablePresent() && !descriptor.writable()) - regExp->m_lastIndexIsWritable = false; if (descriptor.value()) regExp->setLastIndex(exec, descriptor.value(), false); + if (descriptor.writablePresent() && !descriptor.writable()) + regExp->m_lastIndexIsWritable = false; return true; } return Base::defineOwnProperty(object, exec, propertyName, descriptor, shouldThrow); } -JSValue regExpObjectGlobal(ExecState*, JSValue slotBase, PropertyName) -{ - return jsBoolean(asRegExpObject(slotBase)->regExp()->global()); -} - -JSValue regExpObjectIgnoreCase(ExecState*, JSValue slotBase, PropertyName) +static void regExpObjectSetLastIndexStrict(ExecState* exec, EncodedJSValue thisValue, EncodedJSValue value) { - return jsBoolean(asRegExpObject(slotBase)->regExp()->ignoreCase()); -} - -JSValue regExpObjectMultiline(ExecState*, JSValue slotBase, PropertyName) -{ - return jsBoolean(asRegExpObject(slotBase)->regExp()->multiline()); -} - -template <typename CharacterType> -static inline void appendLineTerminatorEscape(StringBuilder&, CharacterType); - -template <> -inline void appendLineTerminatorEscape<LChar>(StringBuilder& builder, LChar lineTerminator) -{ - if (lineTerminator == '\n') - builder.append('n'); - else - builder.append('r'); -} - -template <> -inline void appendLineTerminatorEscape<UChar>(StringBuilder& builder, UChar lineTerminator) -{ - if (lineTerminator == '\n') - builder.append('n'); - else if (lineTerminator == '\r') - builder.append('r'); - else if (lineTerminator == 0x2028) - builder.appendLiteral("u2028"); - else - builder.appendLiteral("u2029"); -} - -template <typename CharacterType> -static inline JSValue regExpObjectSourceInternal(ExecState* exec, String pattern, const CharacterType* characters, unsigned length) -{ - bool previousCharacterWasBackslash = false; - bool inBrackets = false; - bool shouldEscape = false; - - // 15.10.6.4 specifies that RegExp.prototype.toString must return '/' + source + '/', - // and also states that the result must be a valid RegularExpressionLiteral. '//' is - // not a valid RegularExpressionLiteral (since it is a single line comment), and hence - // source cannot ever validly be "". If the source is empty, return a different Pattern - // that would match the same thing. - if (!length) - return jsNontrivialString(exec, ASCIILiteral("(?:)")); - - // early return for strings that don't contain a forwards slash and LineTerminator - for (unsigned i = 0; i < length; ++i) { - CharacterType ch = characters[i]; - if (!previousCharacterWasBackslash) { - if (inBrackets) { - if (ch == ']') - inBrackets = false; - } else { - if (ch == '/') { - shouldEscape = true; - break; - } - if (ch == '[') - inBrackets = true; - } - } - - if (Lexer<CharacterType>::isLineTerminator(ch)) { - shouldEscape = true; - break; - } - - if (previousCharacterWasBackslash) - previousCharacterWasBackslash = false; - else - previousCharacterWasBackslash = ch == '\\'; - } - - if (!shouldEscape) - return jsString(exec, pattern); - - previousCharacterWasBackslash = false; - inBrackets = false; - StringBuilder result; - for (unsigned i = 0; i < length; ++i) { - CharacterType ch = characters[i]; - if (!previousCharacterWasBackslash) { - if (inBrackets) { - if (ch == ']') - inBrackets = false; - } else { - if (ch == '/') - result.append('\\'); - else if (ch == '[') - inBrackets = true; - } - } - - // escape LineTerminator - if (Lexer<CharacterType>::isLineTerminator(ch)) { - if (!previousCharacterWasBackslash) - result.append('\\'); - - appendLineTerminatorEscape<CharacterType>(result, ch); - } else - result.append(ch); - - if (previousCharacterWasBackslash) - previousCharacterWasBackslash = false; - else - previousCharacterWasBackslash = ch == '\\'; - } - - return jsString(exec, result.toString()); + asRegExpObject(JSValue::decode(thisValue))->setLastIndex(exec, JSValue::decode(value), true); } -JSValue regExpObjectSource(ExecState* exec, JSValue slotBase, PropertyName) +static void regExpObjectSetLastIndexNonStrict(ExecState* exec, EncodedJSValue thisValue, EncodedJSValue value) { - String pattern = asRegExpObject(slotBase)->regExp()->pattern(); - if (pattern.is8Bit()) - return regExpObjectSourceInternal(exec, pattern, pattern.characters8(), pattern.length()); - return regExpObjectSourceInternal(exec, pattern, pattern.characters16(), pattern.length()); + asRegExpObject(JSValue::decode(thisValue))->setLastIndex(exec, JSValue::decode(value), false); } void RegExpObject::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot) { if (propertyName == exec->propertyNames().lastIndex) { asRegExpObject(cell)->setLastIndex(exec, value, slot.isStrictMode()); + slot.setCustomValue(asRegExpObject(cell), slot.isStrictMode() + ? regExpObjectSetLastIndexStrict + : regExpObjectSetLastIndexNonStrict); return; } - lookupPut<RegExpObject, JSObject>(exec, propertyName, value, ExecState::regExpTable(exec), jsCast<RegExpObject*>(cell), slot); + Base::put(cell, exec, propertyName, value, slot); } JSValue RegExpObject::exec(ExecState* exec, JSString* string) { if (MatchResult result = match(exec, string)) - return RegExpMatchesArray::create(exec, string, regExp(), result); + return createRegExpMatchesArray(exec, string, regExp(), result); return jsNull(); } |