From 1ccad4e7c0b4d5fa58e448b78a655c28742c3c22 Mon Sep 17 00:00:00 2001 From: hecunmao Date: Tue, 9 Sep 2025 15:57:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AD=A3=E5=88=99=E7=BC=93=E5=AD=98=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue:https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/ICX4YR Signed-off-by: hecunmao Change-Id: I44ab6266b61acd9d6fdbf7b11df54ce03ad1da24 --- ecmascript/builtins/builtins_regexp.cpp | 15 +++++++-------- ecmascript/regexp/regexp_executor.cpp | 12 ++++++------ test/moduletest/regexp/regexp.js | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/ecmascript/builtins/builtins_regexp.cpp b/ecmascript/builtins/builtins_regexp.cpp index 82f411e150..d9bb5a2cb7 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 60e0877a3a..07ed37ab58 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 dd1589c3de..0aeb86ec7c 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"]') -- Gitee