StringPrototype.cpp [plain text]
#include "config.h"
#include "StringPrototype.h"
#include "BuiltinNames.h"
#include "ButterflyInlines.h"
#include "CachedCall.h"
#include "Error.h"
#include "FrameTracers.h"
#include "InterpreterInlines.h"
#include "IntlObject.h"
#include "JITCodeInlines.h"
#include "JSArray.h"
#include "JSCBuiltins.h"
#include "JSCInlines.h"
#include "JSFunction.h"
#include "JSGlobalObjectFunctions.h"
#include "JSStringIterator.h"
#include "Lookup.h"
#include "ObjectPrototype.h"
#include "ParseInt.h"
#include "PropertyNameArray.h"
#include "RegExpCache.h"
#include "RegExpConstructor.h"
#include "RegExpObject.h"
#include "SuperSampler.h"
#include <algorithm>
#include <unicode/uconfig.h>
#include <unicode/unorm.h>
#include <unicode/ustring.h>
#include <wtf/ASCIICType.h>
#include <wtf/MathExtras.h>
#include <wtf/text/StringBuilder.h>
#include <wtf/text/StringView.h>
#include <wtf/unicode/Collator.h>
using namespace WTF;
namespace JSC {
STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(StringPrototype);
EncodedJSValue JSC_HOST_CALL stringProtoFuncToString(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncCharAt(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncCharCodeAt(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncCodePointAt(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncIndexOf(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncLastIndexOf(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncReplaceUsingRegExp(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncReplaceUsingStringSearch(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncSlice(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncSubstr(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncSubstring(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncToLowerCase(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncToUpperCase(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncLocaleCompare(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncToLocaleLowerCase(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncToLocaleUpperCase(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncTrim(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncTrimStart(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncTrimEnd(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncStartsWith(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncEndsWith(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncIncludes(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncNormalize(ExecState*);
EncodedJSValue JSC_HOST_CALL stringProtoFuncIterator(ExecState*);
}
#include "StringPrototype.lut.h"
namespace JSC {
const ClassInfo StringPrototype::s_info = { "String", &StringObject::s_info, &stringPrototypeTable, nullptr, CREATE_METHOD_TABLE(StringPrototype) };
StringPrototype::StringPrototype(VM& vm, Structure* structure)
: StringObject(vm, structure)
{
}
void StringPrototype::finishCreation(VM& vm, JSGlobalObject* globalObject, JSString* nameAndMessage)
{
Base::finishCreation(vm, nameAndMessage);
ASSERT(inherits(vm, info()));
JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->toString, stringProtoFuncToString, static_cast<unsigned>(PropertyAttribute::DontEnum), 0, StringPrototypeValueOfIntrinsic);
JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->valueOf, stringProtoFuncToString, static_cast<unsigned>(PropertyAttribute::DontEnum), 0, StringPrototypeValueOfIntrinsic);
JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION("charAt", stringProtoFuncCharAt, static_cast<unsigned>(PropertyAttribute::DontEnum), 1, CharAtIntrinsic);
JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION("charCodeAt", stringProtoFuncCharCodeAt, static_cast<unsigned>(PropertyAttribute::DontEnum), 1, CharCodeAtIntrinsic);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("codePointAt", stringProtoFuncCodePointAt, static_cast<unsigned>(PropertyAttribute::DontEnum), 1);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("indexOf", stringProtoFuncIndexOf, static_cast<unsigned>(PropertyAttribute::DontEnum), 1);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("lastIndexOf", stringProtoFuncLastIndexOf, static_cast<unsigned>(PropertyAttribute::DontEnum), 1);
JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->builtinNames().replaceUsingRegExpPrivateName(), stringProtoFuncReplaceUsingRegExp, static_cast<unsigned>(PropertyAttribute::DontEnum), 2, StringPrototypeReplaceRegExpIntrinsic);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->builtinNames().replaceUsingStringSearchPrivateName(), stringProtoFuncReplaceUsingStringSearch, static_cast<unsigned>(PropertyAttribute::DontEnum), 2);
JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION("slice", stringProtoFuncSlice, static_cast<unsigned>(PropertyAttribute::DontEnum), 2, StringPrototypeSliceIntrinsic);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("substr", stringProtoFuncSubstr, static_cast<unsigned>(PropertyAttribute::DontEnum), 2);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("substring", stringProtoFuncSubstring, static_cast<unsigned>(PropertyAttribute::DontEnum), 2);
JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION("toLowerCase", stringProtoFuncToLowerCase, static_cast<unsigned>(PropertyAttribute::DontEnum), 0, StringPrototypeToLowerCaseIntrinsic);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("toUpperCase", stringProtoFuncToUpperCase, static_cast<unsigned>(PropertyAttribute::DontEnum), 0);
#if ENABLE(INTL)
JSC_BUILTIN_FUNCTION_WITHOUT_TRANSITION("localeCompare", stringPrototypeLocaleCompareCodeGenerator, static_cast<unsigned>(PropertyAttribute::DontEnum));
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("toLocaleLowerCase", stringProtoFuncToLocaleLowerCase, static_cast<unsigned>(PropertyAttribute::DontEnum), 0);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("toLocaleUpperCase", stringProtoFuncToLocaleUpperCase, static_cast<unsigned>(PropertyAttribute::DontEnum), 0);
#else
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("localeCompare", stringProtoFuncLocaleCompare, static_cast<unsigned>(PropertyAttribute::DontEnum), 1);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("toLocaleLowerCase", stringProtoFuncToLowerCase, static_cast<unsigned>(PropertyAttribute::DontEnum), 0);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("toLocaleUpperCase", stringProtoFuncToUpperCase, static_cast<unsigned>(PropertyAttribute::DontEnum), 0);
#endif
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("trim", stringProtoFuncTrim, static_cast<unsigned>(PropertyAttribute::DontEnum), 0);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("startsWith", stringProtoFuncStartsWith, static_cast<unsigned>(PropertyAttribute::DontEnum), 1);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("endsWith", stringProtoFuncEndsWith, static_cast<unsigned>(PropertyAttribute::DontEnum), 1);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("includes", stringProtoFuncIncludes, static_cast<unsigned>(PropertyAttribute::DontEnum), 1);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("normalize", stringProtoFuncNormalize, static_cast<unsigned>(PropertyAttribute::DontEnum), 0);
JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->builtinNames().charCodeAtPrivateName(), stringProtoFuncCharCodeAt, static_cast<unsigned>(PropertyAttribute::DontEnum), 1, CharCodeAtIntrinsic);
JSFunction* trimStartFunction = JSFunction::create(vm, globalObject, 0, "trimStart"_s, stringProtoFuncTrimStart, NoIntrinsic);
JSFunction* trimEndFunction = JSFunction::create(vm, globalObject, 0, "trimEnd"_s, stringProtoFuncTrimEnd, NoIntrinsic);
putDirectWithoutTransition(vm, Identifier::fromString(&vm, "trimStart"), trimStartFunction, static_cast<unsigned>(PropertyAttribute::DontEnum));
putDirectWithoutTransition(vm, Identifier::fromString(&vm, "trimLeft"), trimStartFunction, static_cast<unsigned>(PropertyAttribute::DontEnum));
putDirectWithoutTransition(vm, Identifier::fromString(&vm, "trimEnd"), trimEndFunction, static_cast<unsigned>(PropertyAttribute::DontEnum));
putDirectWithoutTransition(vm, Identifier::fromString(&vm, "trimRight"), trimEndFunction, static_cast<unsigned>(PropertyAttribute::DontEnum));
JSFunction* iteratorFunction = JSFunction::create(vm, globalObject, 0, "[Symbol.iterator]"_s, stringProtoFuncIterator, NoIntrinsic);
putDirectWithoutTransition(vm, vm.propertyNames->iteratorSymbol, iteratorFunction, static_cast<unsigned>(PropertyAttribute::DontEnum));
putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(0), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum);
}
StringPrototype* StringPrototype::create(VM& vm, JSGlobalObject* globalObject, Structure* structure)
{
JSString* empty = jsEmptyString(&vm);
StringPrototype* prototype = new (NotNull, allocateCell<StringPrototype>(vm.heap)) StringPrototype(vm, structure);
prototype->finishCreation(vm, globalObject, empty);
return prototype;
}
static NEVER_INLINE void substituteBackreferencesSlow(StringBuilder& result, StringView replacement, StringView source, const int* ovector, RegExp* reg, size_t i)
{
bool hasNamedCaptures = reg && reg->hasNamedCaptures();
int offset = 0;
do {
if (i + 1 == replacement.length())
break;
UChar ref = replacement[i + 1];
if (ref == '$') {
++i;
result.append(replacement.substring(offset, i - offset));
offset = i + 1;
continue;
}
int backrefStart;
int backrefLength;
int advance = 0;
if (ref == '&') {
backrefStart = ovector[0];
backrefLength = ovector[1] - backrefStart;
} else if (ref == '`') {
backrefStart = 0;
backrefLength = ovector[0];
} else if (ref == '\'') {
backrefStart = ovector[1];
backrefLength = source.length() - backrefStart;
} else if (reg && ref == '<') {
if (!hasNamedCaptures) {
result.append(replacement.substring(i, 2));
offset = i + 2;
advance = 1;
continue;
}
size_t closingBracket = replacement.find('>', i + 2);
if (closingBracket == WTF::notFound) {
continue;
}
unsigned nameLength = closingBracket - i - 2;
unsigned backrefIndex = reg->subpatternForName(replacement.substring(i + 2, nameLength).toString());
if (!backrefIndex || backrefIndex > reg->numSubpatterns()) {
backrefStart = 0;
backrefLength = 0;
} else {
backrefStart = ovector[2 * backrefIndex];
backrefLength = ovector[2 * backrefIndex + 1] - backrefStart;
}
advance = nameLength + 1;
} else if (reg && isASCIIDigit(ref)) {
unsigned backrefIndex = ref - '0';
if (backrefIndex > reg->numSubpatterns())
continue;
if (replacement.length() > i + 2) {
ref = replacement[i + 2];
if (isASCIIDigit(ref)) {
backrefIndex = 10 * backrefIndex + ref - '0';
if (backrefIndex > reg->numSubpatterns())
backrefIndex = backrefIndex / 10; else
advance = 1;
}
}
if (!backrefIndex)
continue;
backrefStart = ovector[2 * backrefIndex];
backrefLength = ovector[2 * backrefIndex + 1] - backrefStart;
} else
continue;
if (i - offset)
result.append(replacement.substring(offset, i - offset));
i += 1 + advance;
offset = i + 1;
if (backrefStart >= 0)
result.append(source.substring(backrefStart, backrefLength));
} while ((i = replacement.find('$', i + 1)) != notFound);
if (replacement.length() - offset)
result.append(replacement.substring(offset));
}
inline void substituteBackreferencesInline(StringBuilder& result, const String& replacement, StringView source, const int* ovector, RegExp* reg)
{
size_t i = replacement.find('$');
if (UNLIKELY(i != notFound))
return substituteBackreferencesSlow(result, replacement, source, ovector, reg, i);
result.append(replacement);
}
void substituteBackreferences(StringBuilder& result, const String& replacement, StringView source, const int* ovector, RegExp* reg)
{
substituteBackreferencesInline(result, replacement, source, ovector, reg);
}
struct StringRange {
StringRange(int pos, int len)
: position(pos)
, length(len)
{
}
StringRange()
{
}
int position;
int length;
};
static ALWAYS_INLINE JSString* jsSpliceSubstrings(ExecState* exec, JSString* sourceVal, const String& source, const StringRange* substringRanges, int rangeCount)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (rangeCount == 1) {
int sourceSize = source.length();
int position = substringRanges[0].position;
int length = substringRanges[0].length;
if (position <= 0 && length >= sourceSize)
return sourceVal;
RELEASE_AND_RETURN(scope, jsString(exec, StringImpl::createSubstringSharingImpl(*source.impl(), std::max(0, position), std::min(sourceSize, length))));
}
int totalLength = 0;
for (int i = 0; i < rangeCount; i++)
totalLength += substringRanges[i].length;
if (!totalLength)
return jsEmptyString(exec);
if (source.is8Bit()) {
LChar* buffer;
const LChar* sourceData = source.characters8();
auto impl = StringImpl::tryCreateUninitialized(totalLength, buffer);
if (!impl) {
throwOutOfMemoryError(exec, scope);
return nullptr;
}
int bufferPos = 0;
for (int i = 0; i < rangeCount; i++) {
if (int srcLen = substringRanges[i].length) {
StringImpl::copyCharacters(buffer + bufferPos, sourceData + substringRanges[i].position, srcLen);
bufferPos += srcLen;
}
}
RELEASE_AND_RETURN(scope, jsString(exec, WTFMove(impl)));
}
UChar* buffer;
const UChar* sourceData = source.characters16();
auto impl = StringImpl::tryCreateUninitialized(totalLength, buffer);
if (!impl) {
throwOutOfMemoryError(exec, scope);
return nullptr;
}
int bufferPos = 0;
for (int i = 0; i < rangeCount; i++) {
if (int srcLen = substringRanges[i].length) {
StringImpl::copyCharacters(buffer + bufferPos, sourceData + substringRanges[i].position, srcLen);
bufferPos += srcLen;
}
}
RELEASE_AND_RETURN(scope, jsString(exec, WTFMove(impl)));
}
static ALWAYS_INLINE JSString* jsSpliceSubstringsWithSeparators(ExecState* exec, JSString* sourceVal, const String& source, const StringRange* substringRanges, int rangeCount, const String* separators, int separatorCount)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (rangeCount == 1 && separatorCount == 0) {
int sourceSize = source.length();
int position = substringRanges[0].position;
int length = substringRanges[0].length;
if (position <= 0 && length >= sourceSize)
return sourceVal;
RELEASE_AND_RETURN(scope, jsString(exec, StringImpl::createSubstringSharingImpl(*source.impl(), std::max(0, position), std::min(sourceSize, length))));
}
Checked<int, RecordOverflow> totalLength = 0;
bool allSeparators8Bit = true;
for (int i = 0; i < rangeCount; i++)
totalLength += substringRanges[i].length;
for (int i = 0; i < separatorCount; i++) {
totalLength += separators[i].length();
if (separators[i].length() && !separators[i].is8Bit())
allSeparators8Bit = false;
}
if (totalLength.hasOverflowed()) {
throwOutOfMemoryError(exec, scope);
return nullptr;
}
if (!totalLength)
return jsEmptyString(exec);
if (source.is8Bit() && allSeparators8Bit) {
LChar* buffer;
const LChar* sourceData = source.characters8();
auto impl = StringImpl::tryCreateUninitialized(totalLength.unsafeGet(), buffer);
if (!impl) {
throwOutOfMemoryError(exec, scope);
return nullptr;
}
int maxCount = std::max(rangeCount, separatorCount);
int bufferPos = 0;
for (int i = 0; i < maxCount; i++) {
if (i < rangeCount) {
if (int srcLen = substringRanges[i].length) {
StringImpl::copyCharacters(buffer + bufferPos, sourceData + substringRanges[i].position, srcLen);
bufferPos += srcLen;
}
}
if (i < separatorCount) {
if (int sepLen = separators[i].length()) {
StringImpl::copyCharacters(buffer + bufferPos, separators[i].characters8(), sepLen);
bufferPos += sepLen;
}
}
}
RELEASE_AND_RETURN(scope, jsString(exec, WTFMove(impl)));
}
UChar* buffer;
auto impl = StringImpl::tryCreateUninitialized(totalLength.unsafeGet(), buffer);
if (!impl) {
throwOutOfMemoryError(exec, scope);
return nullptr;
}
int maxCount = std::max(rangeCount, separatorCount);
int bufferPos = 0;
for (int i = 0; i < maxCount; i++) {
if (i < rangeCount) {
if (int srcLen = substringRanges[i].length) {
if (source.is8Bit())
StringImpl::copyCharacters(buffer + bufferPos, source.characters8() + substringRanges[i].position, srcLen);
else
StringImpl::copyCharacters(buffer + bufferPos, source.characters16() + substringRanges[i].position, srcLen);
bufferPos += srcLen;
}
}
if (i < separatorCount) {
if (int sepLen = separators[i].length()) {
if (separators[i].is8Bit())
StringImpl::copyCharacters(buffer + bufferPos, separators[i].characters8(), sepLen);
else
StringImpl::copyCharacters(buffer + bufferPos, separators[i].characters16(), sepLen);
bufferPos += sepLen;
}
}
}
RELEASE_AND_RETURN(scope, jsString(exec, WTFMove(impl)));
}
#define OUT_OF_MEMORY(exec__, scope__) \
do { \
throwOutOfMemoryError(exec__, scope__); \
return nullptr; \
} while (false)
static ALWAYS_INLINE JSString* removeUsingRegExpSearch(VM& vm, ExecState* exec, JSString* string, const String& source, RegExp* regExp)
{
auto scope = DECLARE_THROW_SCOPE(vm);
SuperSamplerScope superSamplerScope(false);
size_t lastIndex = 0;
unsigned startPosition = 0;
Vector<StringRange, 16> sourceRanges;
RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor();
unsigned sourceLen = source.length();
while (true) {
MatchResult result = regExpConstructor->performMatch(vm, regExp, string, source, startPosition);
RETURN_IF_EXCEPTION(scope, nullptr);
if (!result)
break;
if (lastIndex < result.start) {
if (UNLIKELY(!sourceRanges.tryConstructAndAppend(lastIndex, result.start - lastIndex)))
OUT_OF_MEMORY(exec, scope);
}
lastIndex = result.end;
startPosition = lastIndex;
if (result.empty()) {
startPosition++;
if (startPosition > sourceLen)
break;
}
}
if (!lastIndex)
return string;
if (static_cast<unsigned>(lastIndex) < sourceLen) {
if (UNLIKELY(!sourceRanges.tryConstructAndAppend(lastIndex, sourceLen - lastIndex)))
OUT_OF_MEMORY(exec, scope);
}
RELEASE_AND_RETURN(scope, jsSpliceSubstrings(exec, string, source, sourceRanges.data(), sourceRanges.size()));
}
static ALWAYS_INLINE JSString* replaceUsingRegExpSearch(
VM& vm, ExecState* exec, JSString* string, JSValue searchValue, CallData& callData,
CallType callType, String& replacementString, JSValue replaceValue)
{
auto scope = DECLARE_THROW_SCOPE(vm);
const String& source = string->value(exec);
unsigned sourceLen = source.length();
RETURN_IF_EXCEPTION(scope, nullptr);
RegExpObject* regExpObject = jsCast<RegExpObject*>(searchValue);
RegExp* regExp = regExpObject->regExp();
bool global = regExp->global();
bool hasNamedCaptures = regExp->hasNamedCaptures();
if (global) {
regExpObject->setLastIndex(exec, 0);
RETURN_IF_EXCEPTION(scope, nullptr);
if (callType == CallType::None && !replacementString.length())
RELEASE_AND_RETURN(scope, removeUsingRegExpSearch(vm, exec, string, source, regExp));
}
RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor();
size_t lastIndex = 0;
unsigned startPosition = 0;
Vector<StringRange, 16> sourceRanges;
Vector<String, 16> replacements;
if (global && callType == CallType::JS) {
int argCount = regExp->numSubpatterns() + 1 + 2;
if (hasNamedCaptures)
++argCount;
JSFunction* func = jsCast<JSFunction*>(replaceValue);
CachedCall cachedCall(exec, func, argCount);
RETURN_IF_EXCEPTION(scope, nullptr);
while (true) {
int* ovector;
MatchResult result = regExpConstructor->performMatch(vm, regExp, string, source, startPosition, &ovector);
RETURN_IF_EXCEPTION(scope, nullptr);
if (!result)
break;
if (UNLIKELY(!sourceRanges.tryConstructAndAppend(lastIndex, result.start - lastIndex)))
OUT_OF_MEMORY(exec, scope);
cachedCall.clearArguments();
JSObject* groups = nullptr;
if (hasNamedCaptures) {
JSGlobalObject* globalObject = exec->lexicalGlobalObject();
groups = JSFinalObject::create(vm, JSFinalObject::createStructure(vm, globalObject, globalObject->objectPrototype(), 0));
}
for (unsigned i = 0; i < regExp->numSubpatterns() + 1; ++i) {
int matchStart = ovector[i * 2];
int matchLen = ovector[i * 2 + 1] - matchStart;
JSValue patternValue;
if (matchStart < 0)
patternValue = jsUndefined();
else
patternValue = jsSubstring(&vm, source, matchStart, matchLen);
cachedCall.appendArgument(patternValue);
if (i && hasNamedCaptures) {
String groupName = regExp->getCaptureGroupName(i);
if (!groupName.isEmpty())
groups->putDirect(vm, Identifier::fromString(&vm, groupName), patternValue);
}
}
cachedCall.appendArgument(jsNumber(result.start));
cachedCall.appendArgument(string);
if (hasNamedCaptures)
cachedCall.appendArgument(groups);
cachedCall.setThis(jsUndefined());
if (UNLIKELY(cachedCall.hasOverflowedArguments())) {
throwOutOfMemoryError(exec, scope);
return nullptr;
}
JSValue jsResult = cachedCall.call();
RETURN_IF_EXCEPTION(scope, nullptr);
replacements.append(jsResult.toWTFString(exec));
RETURN_IF_EXCEPTION(scope, nullptr);
lastIndex = result.end;
startPosition = lastIndex;
if (result.empty()) {
startPosition++;
if (startPosition > sourceLen)
break;
}
}
} else {
do {
int* ovector;
MatchResult result = regExpConstructor->performMatch(vm, regExp, string, source, startPosition, &ovector);
RETURN_IF_EXCEPTION(scope, nullptr);
if (!result)
break;
if (callType != CallType::None) {
if (UNLIKELY(!sourceRanges.tryConstructAndAppend(lastIndex, result.start - lastIndex)))
OUT_OF_MEMORY(exec, scope);
MarkedArgumentBuffer args;
JSObject* groups = nullptr;
if (hasNamedCaptures) {
JSGlobalObject* globalObject = exec->lexicalGlobalObject();
groups = JSFinalObject::create(vm, JSFinalObject::createStructure(vm, globalObject, globalObject->objectPrototype(), 0));
}
for (unsigned i = 0; i < regExp->numSubpatterns() + 1; ++i) {
int matchStart = ovector[i * 2];
int matchLen = ovector[i * 2 + 1] - matchStart;
JSValue patternValue;
if (matchStart < 0)
patternValue = jsUndefined();
else
patternValue = jsSubstring(exec, source, matchStart, matchLen);
args.append(patternValue);
if (i && hasNamedCaptures) {
String groupName = regExp->getCaptureGroupName(i);
if (!groupName.isEmpty())
groups->putDirect(vm, Identifier::fromString(&vm, groupName), patternValue);
}
}
args.append(jsNumber(result.start));
args.append(string);
if (hasNamedCaptures)
args.append(groups);
if (UNLIKELY(args.hasOverflowed())) {
throwOutOfMemoryError(exec, scope);
return nullptr;
}
JSValue replacement = call(exec, replaceValue, callType, callData, jsUndefined(), args);
RETURN_IF_EXCEPTION(scope, nullptr);
String replacementString = replacement.toWTFString(exec);
RETURN_IF_EXCEPTION(scope, nullptr);
replacements.append(replacementString);
RETURN_IF_EXCEPTION(scope, nullptr);
} else {
int replLen = replacementString.length();
if (lastIndex < result.start || replLen) {
if (UNLIKELY(!sourceRanges.tryConstructAndAppend(lastIndex, result.start - lastIndex)))
OUT_OF_MEMORY(exec, scope);
if (replLen) {
StringBuilder replacement(StringBuilder::OverflowHandler::RecordOverflow);
substituteBackreferences(replacement, replacementString, source, ovector, regExp);
if (UNLIKELY(replacement.hasOverflowed()))
OUT_OF_MEMORY(exec, scope);
replacements.append(replacement.toString());
} else
replacements.append(String());
}
}
lastIndex = result.end;
startPosition = lastIndex;
if (result.empty()) {
startPosition++;
if (startPosition > sourceLen)
break;
}
} while (global);
}
if (!lastIndex && replacements.isEmpty())
return string;
if (static_cast<unsigned>(lastIndex) < sourceLen) {
if (UNLIKELY(!sourceRanges.tryConstructAndAppend(lastIndex, sourceLen - lastIndex)))
OUT_OF_MEMORY(exec, scope);
}
RELEASE_AND_RETURN(scope, jsSpliceSubstringsWithSeparators(exec, string, source, sourceRanges.data(), sourceRanges.size(), replacements.data(), replacements.size()));
}
JSCell* JIT_OPERATION operationStringProtoFuncReplaceRegExpEmptyStr(
ExecState* exec, JSString* thisValue, RegExpObject* searchValue)
{
VM& vm = exec->vm();
NativeCallFrameTracer tracer(&vm, exec);
auto scope = DECLARE_THROW_SCOPE(vm);
RegExp* regExp = searchValue->regExp();
if (regExp->global()) {
searchValue->setLastIndex(exec, 0);
RETURN_IF_EXCEPTION(scope, nullptr);
RELEASE_AND_RETURN(scope, removeUsingRegExpSearch(vm, exec, thisValue, thisValue->value(exec), regExp));
}
CallData callData;
String replacementString = emptyString();
RELEASE_AND_RETURN(scope, replaceUsingRegExpSearch(
vm, exec, thisValue, searchValue, callData, CallType::None, replacementString, JSValue()));
}
JSCell* JIT_OPERATION operationStringProtoFuncReplaceRegExpString(
ExecState* exec, JSString* thisValue, RegExpObject* searchValue, JSString* replaceString)
{
VM& vm = exec->vm();
NativeCallFrameTracer tracer(&vm, exec);
CallData callData;
String replacementString = replaceString->value(exec);
return replaceUsingRegExpSearch(
vm, exec, thisValue, searchValue, callData, CallType::None, replacementString, replaceString);
}
static ALWAYS_INLINE JSString* replaceUsingRegExpSearch(VM& vm, ExecState* exec, JSString* string, JSValue searchValue, JSValue replaceValue)
{
auto scope = DECLARE_THROW_SCOPE(vm);
String replacementString;
CallData callData;
CallType callType = getCallData(vm, replaceValue, callData);
if (callType == CallType::None) {
replacementString = replaceValue.toWTFString(exec);
RETURN_IF_EXCEPTION(scope, nullptr);
}
RELEASE_AND_RETURN(scope, replaceUsingRegExpSearch(
vm, exec, string, searchValue, callData, callType, replacementString, replaceValue));
}
static ALWAYS_INLINE JSString* replaceUsingStringSearch(VM& vm, ExecState* exec, JSString* jsString, JSValue searchValue, JSValue replaceValue)
{
auto scope = DECLARE_THROW_SCOPE(vm);
const String& string = jsString->value(exec);
RETURN_IF_EXCEPTION(scope, nullptr);
String searchString = searchValue.toWTFString(exec);
RETURN_IF_EXCEPTION(scope, nullptr);
size_t matchStart = string.find(searchString);
if (matchStart == notFound)
return jsString;
CallData callData;
CallType callType = getCallData(vm, replaceValue, callData);
if (callType != CallType::None) {
MarkedArgumentBuffer args;
args.append(jsSubstring(exec, string, matchStart, searchString.impl()->length()));
args.append(jsNumber(matchStart));
args.append(jsString);
ASSERT(!args.hasOverflowed());
replaceValue = call(exec, replaceValue, callType, callData, jsUndefined(), args);
RETURN_IF_EXCEPTION(scope, nullptr);
}
String replaceString = replaceValue.toWTFString(exec);
RETURN_IF_EXCEPTION(scope, nullptr);
StringImpl* stringImpl = string.impl();
String leftPart(StringImpl::createSubstringSharingImpl(*stringImpl, 0, matchStart));
size_t matchEnd = matchStart + searchString.impl()->length();
int ovector[2] = { static_cast<int>(matchStart), static_cast<int>(matchEnd)};
String middlePart;
if (callType != CallType::None)
middlePart = replaceString;
else {
StringBuilder replacement(StringBuilder::OverflowHandler::RecordOverflow);
substituteBackreferences(replacement, replaceString, string, ovector, 0);
if (UNLIKELY(replacement.hasOverflowed()))
OUT_OF_MEMORY(exec, scope);
middlePart = replacement.toString();
}
size_t leftLength = stringImpl->length() - matchEnd;
String rightPart(StringImpl::createSubstringSharingImpl(*stringImpl, matchEnd, leftLength));
RELEASE_AND_RETURN(scope, JSC::jsString(exec, leftPart, middlePart, rightPart));
}
static inline bool checkObjectCoercible(JSValue thisValue)
{
if (thisValue.isString())
return true;
if (thisValue.isUndefinedOrNull())
return false;
if (thisValue.isObject() && asObject(thisValue)->isEnvironment())
return false;
return true;
}
template <typename CharacterType>
static inline JSString* repeatCharacter(ExecState& exec, CharacterType character, unsigned repeatCount)
{
VM& vm = exec.vm();
auto scope = DECLARE_THROW_SCOPE(vm);
CharacterType* buffer = nullptr;
auto impl = StringImpl::tryCreateUninitialized(repeatCount, buffer);
if (!impl) {
throwOutOfMemoryError(&exec, scope);
return nullptr;
}
std::fill_n(buffer, repeatCount, character);
RELEASE_AND_RETURN(scope, jsString(&exec, WTFMove(impl)));
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncRepeatCharacter(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT(exec->argumentCount() == 2);
ASSERT(exec->uncheckedArgument(0).isString());
JSString* string = asString(exec->uncheckedArgument(0));
ASSERT(string->length() == 1);
JSValue repeatCountValue = exec->uncheckedArgument(1);
RELEASE_ASSERT(repeatCountValue.isNumber());
int32_t repeatCount;
double value = repeatCountValue.asNumber();
if (value > JSString::MaxLength)
return JSValue::encode(throwOutOfMemoryError(exec, scope));
repeatCount = static_cast<int32_t>(value);
ASSERT(repeatCount >= 0);
ASSERT(!repeatCountValue.isDouble() || repeatCountValue.asDouble() == repeatCount);
auto viewWithString = string->viewWithUnderlyingString(exec);
StringView view = viewWithString.view;
ASSERT(view.length() == 1);
scope.assertNoException();
UChar character = view[0];
scope.release();
if (!(character & ~0xff))
return JSValue::encode(repeatCharacter(*exec, static_cast<LChar>(character), repeatCount));
return JSValue::encode(repeatCharacter(*exec, character, repeatCount));
}
ALWAYS_INLINE JSString* replace(
VM& vm, ExecState* exec, JSString* string, JSValue searchValue, JSValue replaceValue)
{
if (searchValue.inherits<RegExpObject>(vm))
return replaceUsingRegExpSearch(vm, exec, string, searchValue, replaceValue);
return replaceUsingStringSearch(vm, exec, string, searchValue, replaceValue);
}
ALWAYS_INLINE JSString* replace(
VM& vm, ExecState* exec, JSValue thisValue, JSValue searchValue, JSValue replaceValue)
{
auto scope = DECLARE_THROW_SCOPE(vm);
if (!checkObjectCoercible(thisValue)) {
throwVMTypeError(exec, scope);
return nullptr;
}
JSString* string = thisValue.toString(exec);
RETURN_IF_EXCEPTION(scope, nullptr);
RELEASE_AND_RETURN(scope, replace(vm, exec, string, searchValue, replaceValue));
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncReplaceUsingRegExp(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSString* string = exec->thisValue().toString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
JSValue searchValue = exec->argument(0);
if (!searchValue.inherits<RegExpObject>(vm))
return JSValue::encode(jsUndefined());
RELEASE_AND_RETURN(scope, JSValue::encode(replaceUsingRegExpSearch(vm, exec, string, searchValue, exec->argument(1))));
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncReplaceUsingStringSearch(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSString* string = exec->thisValue().toString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
RELEASE_AND_RETURN(scope, JSValue::encode(replaceUsingStringSearch(vm, exec, string, exec->argument(0), exec->argument(1))));
}
JSCell* JIT_OPERATION operationStringProtoFuncReplaceGeneric(
ExecState* exec, EncodedJSValue thisValue, EncodedJSValue searchValue,
EncodedJSValue replaceValue)
{
VM& vm = exec->vm();
NativeCallFrameTracer tracer(&vm, exec);
return replace(
vm, exec, JSValue::decode(thisValue), JSValue::decode(searchValue),
JSValue::decode(replaceValue));
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncToString(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = exec->thisValue();
if (thisValue.isString())
return JSValue::encode(thisValue);
auto* stringObject = jsDynamicCast<StringObject*>(vm, thisValue);
if (stringObject)
return JSValue::encode(stringObject->internalValue());
return throwVMTypeError(exec, scope);
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncCharAt(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = exec->thisValue();
if (!checkObjectCoercible(thisValue))
return throwVMTypeError(exec, scope);
auto viewWithString = thisValue.toString(exec)->viewWithUnderlyingString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
StringView view = viewWithString.view;
RETURN_IF_EXCEPTION(scope, encodedJSValue());
JSValue a0 = exec->argument(0);
if (a0.isUInt32()) {
uint32_t i = a0.asUInt32();
if (i < view.length())
return JSValue::encode(jsSingleCharacterString(exec, view[i]));
return JSValue::encode(jsEmptyString(exec));
}
double dpos = a0.toInteger(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
if (dpos >= 0 && dpos < view.length())
return JSValue::encode(jsSingleCharacterString(exec, view[static_cast<unsigned>(dpos)]));
return JSValue::encode(jsEmptyString(exec));
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncCharCodeAt(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = exec->thisValue();
if (!checkObjectCoercible(thisValue))
return throwVMTypeError(exec, scope);
auto viewWithString = thisValue.toString(exec)->viewWithUnderlyingString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
StringView view = viewWithString.view;
JSValue a0 = exec->argument(0);
if (a0.isUInt32()) {
uint32_t i = a0.asUInt32();
if (i < view.length())
return JSValue::encode(jsNumber(view[i]));
return JSValue::encode(jsNaN());
}
double dpos = a0.toInteger(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
if (dpos >= 0 && dpos < view.length())
return JSValue::encode(jsNumber(view[static_cast<int>(dpos)]));
return JSValue::encode(jsNaN());
}
static inline UChar32 codePointAt(const String& string, unsigned position, unsigned length)
{
RELEASE_ASSERT(position < length);
if (string.is8Bit())
return string.characters8()[position];
UChar32 character;
U16_NEXT(string.characters16(), position, length, character);
return character;
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncCodePointAt(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = exec->thisValue();
if (!checkObjectCoercible(thisValue))
return throwVMTypeError(exec, scope);
String string = thisValue.toWTFString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
unsigned length = string.length();
JSValue argument0 = exec->argument(0);
if (argument0.isUInt32()) {
unsigned position = argument0.asUInt32();
if (position < length)
return JSValue::encode(jsNumber(codePointAt(string, position, length)));
return JSValue::encode(jsUndefined());
}
RETURN_IF_EXCEPTION(scope, encodedJSValue());
double doublePosition = argument0.toInteger(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
if (doublePosition >= 0 && doublePosition < length)
return JSValue::encode(jsNumber(codePointAt(string, static_cast<unsigned>(doublePosition), length)));
return JSValue::encode(jsUndefined());
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncIndexOf(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = exec->thisValue();
if (!checkObjectCoercible(thisValue))
return throwVMTypeError(exec, scope);
JSValue a0 = exec->argument(0);
JSValue a1 = exec->argument(1);
JSString* thisJSString = thisValue.toString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
JSString* otherJSString = a0.toString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
unsigned pos = 0;
if (!a1.isUndefined()) {
int len = thisJSString->length();
RELEASE_ASSERT(len >= 0);
if (a1.isUInt32())
pos = std::min<uint32_t>(a1.asUInt32(), len);
else {
double dpos = a1.toInteger(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
if (dpos < 0)
dpos = 0;
else if (dpos > len)
dpos = len;
pos = static_cast<unsigned>(dpos);
}
}
if (thisJSString->length() < otherJSString->length() + pos)
return JSValue::encode(jsNumber(-1));
auto thisViewWithString = thisJSString->viewWithUnderlyingString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
auto otherViewWithString = otherJSString->viewWithUnderlyingString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
size_t result = thisViewWithString.view.find(otherViewWithString.view, pos);
if (result == notFound)
return JSValue::encode(jsNumber(-1));
return JSValue::encode(jsNumber(result));
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncLastIndexOf(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = exec->thisValue();
if (!checkObjectCoercible(thisValue))
return throwVMTypeError(exec, scope);
JSValue a0 = exec->argument(0);
JSValue a1 = exec->argument(1);
JSString* thisJSString = thisValue.toString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
unsigned len = thisJSString->length();
JSString* otherJSString = a0.toString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
double dpos = a1.toIntegerPreserveNaN(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
unsigned startPosition;
if (dpos < 0)
startPosition = 0;
else if (!(dpos <= len)) startPosition = len;
else
startPosition = static_cast<unsigned>(dpos);
if (len < otherJSString->length())
return JSValue::encode(jsNumber(-1));
String thisString = thisJSString->value(exec);
String otherString = otherJSString->value(exec);
size_t result;
if (!startPosition)
result = thisString.startsWith(otherString) ? 0 : notFound;
else
result = thisString.reverseFind(otherString, startPosition);
if (result == notFound)
return JSValue::encode(jsNumber(-1));
return JSValue::encode(jsNumber(result));
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncSlice(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = exec->thisValue();
if (!checkObjectCoercible(thisValue))
return throwVMTypeError(exec, scope);
String s = thisValue.toWTFString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
int len = s.length();
RELEASE_ASSERT(len >= 0);
JSValue a0 = exec->argument(0);
JSValue a1 = exec->argument(1);
double start = a0.toInteger(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
double end = a1.isUndefined() ? len : a1.toInteger(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
double from = start < 0 ? len + start : start;
double to = end < 0 ? len + end : end;
if (to > from && to > 0 && from < len) {
if (from < 0)
from = 0;
if (to > len)
to = len;
return JSValue::encode(jsSubstring(exec, s, static_cast<unsigned>(from), static_cast<unsigned>(to) - static_cast<unsigned>(from)));
}
return JSValue::encode(jsEmptyString(exec));
}
template<typename CharacterType>
static ALWAYS_INLINE bool splitStringByOneCharacterImpl(ExecState* exec, JSArray* result, JSValue originalValue, const String& input, StringImpl* string, UChar separatorCharacter, size_t& position, unsigned& resultLength, unsigned limitLength)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
size_t matchPosition;
const CharacterType* characters = string->characters<CharacterType>();
while ((matchPosition = WTF::find(characters, string->length(), separatorCharacter, position)) != notFound) {
result->putDirectIndex(exec, resultLength, jsSubstring(exec, originalValue, input, position, matchPosition - position));
RETURN_IF_EXCEPTION(scope, false);
if (++resultLength == limitLength)
return true;
position = matchPosition + 1;
}
return false;
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncSplitFast(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = exec->thisValue();
ASSERT(checkObjectCoercible(thisValue));
String input = thisValue.toWTFString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
ASSERT(!input.isNull());
JSArray* result = constructEmptyArray(exec, 0);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
unsigned resultLength = 0;
JSValue limitValue = exec->uncheckedArgument(1);
unsigned limit = limitValue.isUndefined() ? 0xFFFFFFFFu : limitValue.toUInt32(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
size_t position = 0;
JSValue separatorValue = exec->uncheckedArgument(0);
String separator = separatorValue.toWTFString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
if (!limit)
return JSValue::encode(result);
if (separatorValue.isUndefined()) {
scope.release();
result->putDirectIndex(exec, 0, jsStringWithReuse(exec, thisValue, input));
return JSValue::encode(result);
}
if (input.isEmpty()) {
if (!separator.isEmpty()) {
scope.release();
result->putDirectIndex(exec, 0, jsStringWithReuse(exec, thisValue, input));
}
return JSValue::encode(result);
}
if (separator.isEmpty()) {
limit = std::min(limit, input.length());
ASSERT(limit);
do {
result->putDirectIndex(exec, position, jsSingleCharacterString(exec, input[position]));
RETURN_IF_EXCEPTION(scope, encodedJSValue());
} while (++position < limit);
return JSValue::encode(result);
}
StringImpl* stringImpl = input.impl();
StringImpl* separatorImpl = separator.impl();
size_t separatorLength = separatorImpl->length();
if (separatorLength == 1) {
UChar separatorCharacter;
if (separatorImpl->is8Bit())
separatorCharacter = separatorImpl->characters8()[0];
else
separatorCharacter = separatorImpl->characters16()[0];
if (stringImpl->is8Bit()) {
if (splitStringByOneCharacterImpl<LChar>(exec, result, thisValue, input, stringImpl, separatorCharacter, position, resultLength, limit))
RELEASE_AND_RETURN(scope, JSValue::encode(result));
} else {
if (splitStringByOneCharacterImpl<UChar>(exec, result, thisValue, input, stringImpl, separatorCharacter, position, resultLength, limit))
RELEASE_AND_RETURN(scope, JSValue::encode(result));
}
RETURN_IF_EXCEPTION(scope, encodedJSValue());
} else {
size_t matchPosition;
while ((matchPosition = stringImpl->find(separatorImpl, position)) != notFound) {
result->putDirectIndex(exec, resultLength, jsSubstring(exec, thisValue, input, position, matchPosition - position));
RETURN_IF_EXCEPTION(scope, encodedJSValue());
if (++resultLength == limit)
return JSValue::encode(result);
position = matchPosition + separator.length();
}
}
scope.release();
result->putDirectIndex(exec, resultLength++, jsSubstring(exec, thisValue, input, position, input.length() - position));
return JSValue::encode(result);
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncSubstr(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = exec->thisValue();
if (!checkObjectCoercible(thisValue))
return throwVMTypeError(exec, scope);
unsigned len;
JSString* jsString = 0;
String uString;
if (thisValue.isString()) {
jsString = asString(thisValue);
len = jsString->length();
} else {
uString = thisValue.toWTFString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
len = uString.length();
}
JSValue a0 = exec->argument(0);
JSValue a1 = exec->argument(1);
double start = a0.toInteger(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
double length = a1.isUndefined() ? len : a1.toInteger(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
if (start >= len || length <= 0)
return JSValue::encode(jsEmptyString(exec));
if (start < 0) {
start += len;
if (start < 0)
start = 0;
}
if (start + length > len)
length = len - start;
unsigned substringStart = static_cast<unsigned>(start);
unsigned substringLength = static_cast<unsigned>(length);
if (jsString)
return JSValue::encode(jsSubstring(exec, jsString, substringStart, substringLength));
return JSValue::encode(jsSubstring(exec, uString, substringStart, substringLength));
}
EncodedJSValue JSC_HOST_CALL builtinStringSubstrInternal(ExecState* exec)
{
ASSERT(exec->thisValue().isString());
return stringProtoFuncSubstr(exec);
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncSubstring(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = exec->thisValue();
if (!checkObjectCoercible(thisValue))
return throwVMTypeError(exec, scope);
JSString* jsString = thisValue.toString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
JSValue a0 = exec->argument(0);
JSValue a1 = exec->argument(1);
int len = jsString->length();
RELEASE_ASSERT(len >= 0);
double start = a0.toNumber(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
double end;
if (!(start >= 0)) start = 0;
else if (start > len)
start = len;
if (a1.isUndefined())
end = len;
else {
end = a1.toNumber(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
if (!(end >= 0)) end = 0;
else if (end > len)
end = len;
}
if (start > end) {
double temp = end;
end = start;
start = temp;
}
unsigned substringStart = static_cast<unsigned>(start);
unsigned substringLength = static_cast<unsigned>(end) - substringStart;
return JSValue::encode(jsSubstring(exec, jsString, substringStart, substringLength));
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncToLowerCase(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = exec->thisValue();
if (!checkObjectCoercible(thisValue))
return throwVMTypeError(exec, scope);
JSString* sVal = thisValue.toString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
const String& s = sVal->value(exec);
String lowercasedString = s.convertToLowercaseWithoutLocale();
if (lowercasedString.impl() == s.impl())
return JSValue::encode(sVal);
RELEASE_AND_RETURN(scope, JSValue::encode(jsString(exec, lowercasedString)));
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncToUpperCase(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = exec->thisValue();
if (!checkObjectCoercible(thisValue))
return throwVMTypeError(exec, scope);
JSString* sVal = thisValue.toString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
const String& s = sVal->value(exec);
String uppercasedString = s.convertToUppercaseWithoutLocale();
if (uppercasedString.impl() == s.impl())
return JSValue::encode(sVal);
RELEASE_AND_RETURN(scope, JSValue::encode(jsString(exec, uppercasedString)));
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncLocaleCompare(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = exec->thisValue();
if (!checkObjectCoercible(thisValue))
return throwVMTypeError(exec, scope);
String s = thisValue.toWTFString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
JSValue a0 = exec->argument(0);
String str = a0.toWTFString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
return JSValue::encode(jsNumber(Collator().collate(s, str)));
}
#if ENABLE(INTL)
static EncodedJSValue toLocaleCase(ExecState* state, int32_t (*convertCase)(UChar*, int32_t, const UChar*, int32_t, const char*, UErrorCode*))
{
VM& vm = state->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = state->thisValue();
if (!checkObjectCoercible(thisValue))
return throwVMTypeError(state, scope);
JSString* sVal = thisValue.toString(state);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
const String& s = sVal->value(state);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
if (s.isEmpty())
return JSValue::encode(sVal);
Vector<String> requestedLocales = canonicalizeLocaleList(*state, state->argument(0));
RETURN_IF_EXCEPTION(scope, encodedJSValue());
size_t len = requestedLocales.size();
String requestedLocale = len > 0 ? requestedLocales.first() : defaultLocale(*state);
String noExtensionsLocale = removeUnicodeLocaleExtension(requestedLocale);
const HashSet<String> availableLocales({ "az"_s, "lt"_s, "tr"_s });
String locale = bestAvailableLocale(availableLocales, noExtensionsLocale);
if (locale.isNull())
locale = "und"_s;
CString utf8LocaleBuffer = locale.utf8();
const StringView view(s);
const int32_t viewLength = view.length();
UErrorCode error(U_ZERO_ERROR);
Vector<UChar> buffer(viewLength);
String lower;
const int32_t resultLength = convertCase(buffer.data(), viewLength, view.upconvertedCharacters(), viewLength, utf8LocaleBuffer.data(), &error);
if (U_SUCCESS(error))
lower = String(buffer.data(), resultLength);
else if (error == U_BUFFER_OVERFLOW_ERROR) {
UErrorCode error(U_ZERO_ERROR);
Vector<UChar> buffer(resultLength);
convertCase(buffer.data(), resultLength, view.upconvertedCharacters(), viewLength, utf8LocaleBuffer.data(), &error);
if (U_FAILURE(error))
return throwVMTypeError(state, scope, u_errorName(error));
lower = String(buffer.data(), resultLength);
} else
return throwVMTypeError(state, scope, u_errorName(error));
RELEASE_AND_RETURN(scope, JSValue::encode(jsString(state, lower)));
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncToLocaleLowerCase(ExecState* state)
{
return toLocaleCase(state, u_strToLower);
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncToLocaleUpperCase(ExecState* state)
{
return toLocaleCase(state, u_strToUpper);
}
#endif // ENABLE(INTL)
enum {
TrimStart = 1,
TrimEnd = 2
};
static inline JSValue trimString(ExecState* exec, JSValue thisValue, int trimKind)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (!checkObjectCoercible(thisValue))
return throwTypeError(exec, scope);
String str = thisValue.toWTFString(exec);
RETURN_IF_EXCEPTION(scope, { });
unsigned left = 0;
if (trimKind & TrimStart) {
while (left < str.length() && isStrWhiteSpace(str[left]))
left++;
}
unsigned right = str.length();
if (trimKind & TrimEnd) {
while (right > left && isStrWhiteSpace(str[right - 1]))
right--;
}
if (left == 0 && right == str.length() && thisValue.isString())
return thisValue;
RELEASE_AND_RETURN(scope, jsString(exec, str.substringSharingImpl(left, right - left)));
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncTrim(ExecState* exec)
{
JSValue thisValue = exec->thisValue();
return JSValue::encode(trimString(exec, thisValue, TrimStart | TrimEnd));
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncTrimStart(ExecState* exec)
{
JSValue thisValue = exec->thisValue();
return JSValue::encode(trimString(exec, thisValue, TrimStart));
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncTrimEnd(ExecState* exec)
{
JSValue thisValue = exec->thisValue();
return JSValue::encode(trimString(exec, thisValue, TrimEnd));
}
static inline unsigned clampAndTruncateToUnsigned(double value, unsigned min, unsigned max)
{
if (value < min)
return min;
if (value > max)
return max;
return static_cast<unsigned>(value);
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncStartsWith(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = exec->thisValue();
if (!checkObjectCoercible(thisValue))
return throwVMTypeError(exec, scope);
String stringToSearchIn = thisValue.toWTFString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
JSValue a0 = exec->argument(0);
bool isRegularExpression = isRegExp(vm, exec, a0);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
if (isRegularExpression)
return throwVMTypeError(exec, scope, "Argument to String.prototype.startsWith cannot be a RegExp");
String searchString = a0.toWTFString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
JSValue positionArg = exec->argument(1);
unsigned start = 0;
if (positionArg.isInt32())
start = std::max(0, positionArg.asInt32());
else {
unsigned length = stringToSearchIn.length();
start = clampAndTruncateToUnsigned(positionArg.toInteger(exec), 0, length);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
}
return JSValue::encode(jsBoolean(stringToSearchIn.hasInfixStartingAt(searchString, start)));
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncEndsWith(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = exec->thisValue();
if (!checkObjectCoercible(thisValue))
return throwVMTypeError(exec, scope);
String stringToSearchIn = thisValue.toWTFString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
JSValue a0 = exec->argument(0);
bool isRegularExpression = isRegExp(vm, exec, a0);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
if (isRegularExpression)
return throwVMTypeError(exec, scope, "Argument to String.prototype.endsWith cannot be a RegExp");
String searchString = a0.toWTFString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
unsigned length = stringToSearchIn.length();
JSValue endPositionArg = exec->argument(1);
unsigned end = length;
if (endPositionArg.isInt32())
end = std::max(0, endPositionArg.asInt32());
else if (!endPositionArg.isUndefined()) {
end = clampAndTruncateToUnsigned(endPositionArg.toInteger(exec), 0, length);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
}
return JSValue::encode(jsBoolean(stringToSearchIn.hasInfixEndingAt(searchString, std::min(end, length))));
}
static EncodedJSValue JSC_HOST_CALL stringIncludesImpl(VM& vm, ExecState* exec, String stringToSearchIn, String searchString, JSValue positionArg)
{
auto scope = DECLARE_THROW_SCOPE(vm);
unsigned start = 0;
if (positionArg.isInt32())
start = std::max(0, positionArg.asInt32());
else {
unsigned length = stringToSearchIn.length();
start = clampAndTruncateToUnsigned(positionArg.toInteger(exec), 0, length);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
}
return JSValue::encode(jsBoolean(stringToSearchIn.find(searchString, start) != notFound));
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncIncludes(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = exec->thisValue();
if (!checkObjectCoercible(thisValue))
return throwVMTypeError(exec, scope);
String stringToSearchIn = thisValue.toWTFString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
JSValue a0 = exec->argument(0);
bool isRegularExpression = isRegExp(vm, exec, a0);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
if (isRegularExpression)
return throwVMTypeError(exec, scope, "Argument to String.prototype.includes cannot be a RegExp");
String searchString = a0.toWTFString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
JSValue positionArg = exec->argument(1);
RELEASE_AND_RETURN(scope, stringIncludesImpl(vm, exec, stringToSearchIn, searchString, positionArg));
}
EncodedJSValue JSC_HOST_CALL builtinStringIncludesInternal(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = exec->thisValue();
ASSERT(checkObjectCoercible(thisValue));
String stringToSearchIn = thisValue.toWTFString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
JSValue a0 = exec->uncheckedArgument(0);
String searchString = a0.toWTFString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
JSValue positionArg = exec->argument(1);
RELEASE_AND_RETURN(scope, stringIncludesImpl(vm, exec, stringToSearchIn, searchString, positionArg));
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncIterator(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = exec->thisValue();
if (!checkObjectCoercible(thisValue))
return throwVMTypeError(exec, scope);
JSString* string = thisValue.toString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
return JSValue::encode(JSStringIterator::create(exec, exec->jsCallee()->globalObject(vm)->stringIteratorStructure(), string));
}
enum class NormalizationForm {
CanonicalComposition,
CanonicalDecomposition,
CompatibilityComposition,
CompatibilityDecomposition
};
static JSValue normalize(ExecState* exec, const UChar* source, size_t sourceLength, NormalizationForm form)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
UErrorCode status = U_ZERO_ERROR;
const UNormalizer2* normalizer = nullptr;
switch (form) {
case NormalizationForm::CanonicalComposition:
normalizer = unorm2_getNFCInstance(&status);
break;
case NormalizationForm::CanonicalDecomposition:
normalizer = unorm2_getNFDInstance(&status);
break;
case NormalizationForm::CompatibilityComposition:
normalizer = unorm2_getNFKCInstance(&status);
break;
case NormalizationForm::CompatibilityDecomposition:
normalizer = unorm2_getNFKDInstance(&status);
break;
}
if (!normalizer || U_FAILURE(status))
return throwTypeError(exec, scope);
int32_t normalizedStringLength = unorm2_normalize(normalizer, source, sourceLength, nullptr, 0, &status);
if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) {
return throwTypeError(exec, scope);
}
UChar* buffer = nullptr;
auto impl = StringImpl::tryCreateUninitialized(normalizedStringLength, buffer);
if (!impl)
return throwOutOfMemoryError(exec, scope);
status = U_ZERO_ERROR;
unorm2_normalize(normalizer, source, sourceLength, buffer, normalizedStringLength, &status);
if (U_FAILURE(status))
return throwTypeError(exec, scope);
RELEASE_AND_RETURN(scope, jsString(exec, WTFMove(impl)));
}
EncodedJSValue JSC_HOST_CALL stringProtoFuncNormalize(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = exec->thisValue();
if (!checkObjectCoercible(thisValue))
return throwVMTypeError(exec, scope);
auto viewWithString = thisValue.toString(exec)->viewWithUnderlyingString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
StringView view = viewWithString.view;
NormalizationForm form = NormalizationForm::CanonicalComposition;
if (!exec->argument(0).isUndefined()) {
String formString = exec->uncheckedArgument(0).toWTFString(exec);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
if (formString == "NFC")
form = NormalizationForm::CanonicalComposition;
else if (formString == "NFD")
form = NormalizationForm::CanonicalDecomposition;
else if (formString == "NFKC")
form = NormalizationForm::CompatibilityComposition;
else if (formString == "NFKD")
form = NormalizationForm::CompatibilityDecomposition;
else
return throwVMError(exec, scope, createRangeError(exec, "argument does not match any normalization form"_s));
}
RELEASE_AND_RETURN(scope, JSValue::encode(normalize(exec, view.upconvertedCharacters(), view.length(), form)));
}
}