diff --git a/ecmascript/builtins/builtins_regexp.cpp b/ecmascript/builtins/builtins_regexp.cpp index 82f411e150da1dbadb92ca6ea4840f70d3d80e98..d9bb5a2cb7315c2c724fabb88dbdaab0a0bd92de 100644 --- a/ecmascript/builtins/builtins_regexp.cpp +++ b/ecmascript/builtins/builtins_regexp.cpp @@ -2606,17 +2606,15 @@ void RegExpExecResultCache::AddResultInCache(JSThread *thread, JSHandle(resultArray); } JSHandle globalTable(thread->GetGlobalEnv()->GetRegExpGlobalResult()); - JSHandle taggedArray = JSHandle::Cast(globalTable); auto factory = thread->GetEcmaVM()->GetFactory(); uint32_t arrayLength = globalTable->GetLength(); - JSHandle resTableArray = factory->NewAndCopyTaggedArray(taggedArray, arrayLength, arrayLength); + JSTaggedValue globalTableValue = globalTable.GetTaggedValue(); JSTaggedValue patternValue = pattern.GetTaggedValue(); JSTaggedValue flagsValue = flags.GetTaggedValue(); JSTaggedValue inputValue = input.GetTaggedValue(); JSTaggedValue extendValue = extend.GetTaggedValue(); JSTaggedValue lastIndexInputValue(lastIndexInput); JSTaggedValue lastIndexValue(lastIndex); - JSTaggedValue resTableArrayValue = resTableArray.GetTaggedValue(); uint32_t hash = patternValue.GetKeyHashCode(thread) + static_cast(flagsValue.GetInt()) + inputValue.GetKeyHashCode(thread) + lastIndexInput; @@ -2628,7 +2626,7 @@ void RegExpExecResultCache::AddResultInCache(JSThread *thread, JSHandleGet(thread, index).IsUndefined()) { cache->SetCacheCount(thread, cache->GetCacheCount() + 1); cache->SetEntry(thread, entry, patternValue, flagsValue, inputValue, - lastIndexInputValue, lastIndexValue, extendValue, resTableArrayValue); + lastIndexInputValue, lastIndexValue, extendValue, globalTableValue); cache->UpdateResultArray(thread, entry, resultArrayCopy.GetTaggedValue(), type); cache->SetLastMatchGlobalTableIndex(thread, index); // update last match index } else if (cache->Match(thread, entry, patternValue, flagsValue, inputValue, @@ -2646,18 +2644,19 @@ void RegExpExecResultCache::AddResultInCache(JSThread *thread, JSHandleSetCacheLength(thread, DEFAULT_CACHE_NUMBER); entry2 = hash & static_cast(cache->GetCacheLength() - 1); index2 = CACHE_TABLE_HEADER_SIZE + entry2 * ENTRY_SIZE; } extendValue = extend.GetTaggedValue(); - resTableArrayValue = resTableArray.GetTaggedValue(); + globalTableValue = globalTable.GetTaggedValue(); + cache->SetLastMatchGlobalTableIndex(thread, index2); // update last match index if (cache->Get(thread, index2).IsUndefined()) { cache->SetCacheCount(thread, cache->GetCacheCount() + 1); cache->SetEntry(thread, entry2, patternValue, flagsValue, inputValue, - lastIndexInputValue, lastIndexValue, extendValue, resTableArrayValue); + lastIndexInputValue, lastIndexValue, extendValue, globalTableValue); cache->UpdateResultArray(thread, entry2, resultArrayCopy.GetTaggedValue(), type); } else if (cache->Match(thread, entry2, patternValue, flagsValue, inputValue, lastIndexInputValue, extendValue, type)) { @@ -2667,7 +2666,7 @@ void RegExpExecResultCache::AddResultInCache(JSThread *thread, JSHandleSetCacheCount(thread, cache->GetCacheCount() - 1); cache->ClearEntry(thread, entry2); cache->SetEntry(thread, entry2, patternValue, flagsValue, inputValue, - lastIndexInputValue, lastIndexValue, extendValue, resTableArrayValue); + lastIndexInputValue, lastIndexValue, extendValue, globalTableValue); cache->UpdateResultArray(thread, entry2, resultArrayCopy.GetTaggedValue(), type); } } diff --git a/ecmascript/regexp/regexp_executor.cpp b/ecmascript/regexp/regexp_executor.cpp index 60e0877a3ac617030e17a8b974c6b786edb39137..07ed37ab58bf4f9fa334fce2cccd6dcb1423ce3f 100644 --- a/ecmascript/regexp/regexp_executor.cpp +++ b/ecmascript/regexp/regexp_executor.cpp @@ -260,14 +260,14 @@ void RegExpExecutor::DumpResult(std::ostream &out) const void RegExpExecutor::GetResult(JSThread *thread) { - JSHandle matchResult(thread->GetGlobalEnv()->GetRegExpGlobalResult()); - matchResult->SetTotalCaptureCounts(thread, JSTaggedValue(nCapture_)); uint32_t firstIndex = RegExpGlobalResult::FIRST_CAPTURE_INDEX; - uint32_t availableCaptureSlot = matchResult->GetLength() - firstIndex; uint32_t requiredLength = nCapture_ * 2; - if (requiredLength > availableCaptureSlot) { - matchResult = RegExpGlobalResult::GrowCapturesCapacity(thread, matchResult, requiredLength + firstIndex); - } + ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); + JSHandle matchResult(factory->NewTaggedArray(requiredLength + firstIndex)); + matchResult->SetTotalCaptureCounts(thread, JSTaggedValue(nCapture_)); + JSHandle env = thread->GetEcmaVM()->GetGlobalEnv(); + env->SetRegExpGlobalResult(thread, matchResult.GetTaggedValue()); + for (uint32_t i = 0; i < nCapture_; i++) { CaptureState *captureState = &captureResultList_[i]; int32_t len = captureState->captureEnd - captureState->captureStart; diff --git a/test/moduletest/regexp/regexp.js b/test/moduletest/regexp/regexp.js index dd1589c3de794d9b1bc426c516a573214a50a817..0aeb86ec7cd800219232b976ae6beb7ea2377776 100644 --- a/test/moduletest/regexp/regexp.js +++ b/test/moduletest/regexp/regexp.js @@ -20,6 +20,22 @@ * @tc.require: issueI5NO8G */ +{ + let reg1 = /(a)/ + reg1.test("a") + assert_equal(RegExp.$1, "a"); + + reg1.test("a") + assert_equal(RegExp.$1, "a"); + + let reg2 = /b(b)/; + reg2.test("bb"); + assert_equal(RegExp.$1, "b"); + + reg1.test("a") + assert_equal(RegExp.$1, "a"); +} + { let reg= /ab|cd||/ assert_equal(JSON.stringify(reg.exec("cd")),'["cd"]')