if (global && callType == CallType::JS) {
// regExp->numSubpatterns() + 1 for pattern args, + 2 for match start and string
int argCount = regExp->numSubpatterns() + 1 + 2;
+ if (hasNamedCaptures)
+ ++argCount;
JSFunction* func = jsCast<JSFunction*>(replaceValue);
CachedCall cachedCall(exec, func, argCount);
RETURN_IF_EXCEPTION(scope, nullptr);
- if (source.is8Bit()) {
- while (true) {
- int* ovector;
- MatchResult result = regExpConstructor->performMatch(vm, regExp, string, source, startPosition, &ovector);
- if (!result)
- break;
-
- if (UNLIKELY(!sourceRanges.tryConstructAndAppend(lastIndex, result.start - lastIndex)))
- OUT_OF_MEMORY(exec, scope);
-
- unsigned i = 0;
- cachedCall.clearArguments();
-
- JSObject* groups = nullptr;
-
- if (hasNamedCaptures) {
- JSGlobalObject* globalObject = exec->lexicalGlobalObject();
- groups = JSFinalObject::create(vm, JSFinalObject::createStructure(vm, globalObject, globalObject->objectPrototype(), 0));
- }
-
- for (; 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);
+ while (true) {
+ int* ovector;
+ MatchResult result = regExpConstructor->performMatch(vm, regExp, string, source, startPosition, &ovector);
+ if (!result)
+ break;
- cachedCall.setThis(jsUndefined());
- if (UNLIKELY(cachedCall.hasOverflowedArguments())) {
- throwOutOfMemoryError(exec, scope);
- return nullptr;
- }
+ if (UNLIKELY(!sourceRanges.tryConstructAndAppend(lastIndex, result.start - lastIndex)))
+ OUT_OF_MEMORY(exec, scope);
- JSValue jsResult = cachedCall.call();
- RETURN_IF_EXCEPTION(scope, nullptr);
- replacements.append(jsResult.toWTFString(exec));
- RETURN_IF_EXCEPTION(scope, nullptr);
+ cachedCall.clearArguments();
- lastIndex = result.end;
- startPosition = lastIndex;
+ JSObject* groups = nullptr;
- // special case of empty match
- if (result.empty()) {
- startPosition++;
- if (startPosition > sourceLen)
- break;
- }
+ if (hasNamedCaptures) {
+ JSGlobalObject* globalObject = exec->lexicalGlobalObject();
+ groups = JSFinalObject::create(vm, JSFinalObject::createStructure(vm, globalObject, globalObject->objectPrototype(), 0));
}
- } else {
- while (true) {
- int* ovector;
- MatchResult result = regExpConstructor->performMatch(vm, regExp, string, source, startPosition, &ovector);
- if (!result)
- break;
- if (UNLIKELY(!sourceRanges.tryConstructAndAppend(lastIndex, result.start - lastIndex)))
- OUT_OF_MEMORY(exec, scope);
+ for (unsigned i = 0; i < regExp->numSubpatterns() + 1; ++i) {
+ int matchStart = ovector[i * 2];
+ int matchLen = ovector[i * 2 + 1] - matchStart;
- unsigned i = 0;
- cachedCall.clearArguments();
+ JSValue patternValue;
- JSObject* groups = nullptr;
-
- if (hasNamedCaptures) {
- JSGlobalObject* globalObject = exec->lexicalGlobalObject();
- groups = JSFinalObject::create(vm, JSFinalObject::createStructure(vm, globalObject, globalObject->objectPrototype(), 0));
- }
-
- for (; 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);
+ if (matchStart < 0)
+ patternValue = jsUndefined();
+ else
+ patternValue = jsSubstring(&vm, source, matchStart, matchLen);
- cachedCall.appendArgument(patternValue);
+ cachedCall.appendArgument(patternValue);
- if (i && hasNamedCaptures) {
- String groupName = regExp->getCaptureGroupName(i);
- if (!groupName.isEmpty())
- groups->putDirect(vm, Identifier::fromString(&vm, groupName), 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.appendArgument(jsNumber(result.start));
+ cachedCall.appendArgument(string);
+ if (hasNamedCaptures)
+ cachedCall.appendArgument(groups);
- cachedCall.setThis(jsUndefined());
- if (UNLIKELY(cachedCall.hasOverflowedArguments())) {
- throwOutOfMemoryError(exec, scope);
- return nullptr;
- }
+ 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);
+ JSValue jsResult = cachedCall.call();
+ RETURN_IF_EXCEPTION(scope, nullptr);
+ replacements.append(jsResult.toWTFString(exec));
+ RETURN_IF_EXCEPTION(scope, nullptr);
- lastIndex = result.end;
- startPosition = lastIndex;
+ lastIndex = result.end;
+ startPosition = lastIndex;
- // special case of empty match
- if (result.empty()) {
- startPosition++;
- if (startPosition > sourceLen)
- break;
- }
+ // special case of empty match
+ if (result.empty()) {
+ startPosition++;
+ if (startPosition > sourceLen)
+ break;
}
}
} else {