From b9f2cc6884fd198ea7d516975833498b8e977380 Mon Sep 17 00:00:00 2001 From: hecunmao Date: Mon, 29 Apr 2024 16:52:32 +0800 Subject: [PATCH] =?UTF-8?q?bugfix:FlatMap=20IR=E5=8C=96=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E9=94=99=E8=AF=AF=20issue:https://gitee.com/?= =?UTF-8?q?openharmony/arkcompiler=5Fets=5Fruntime/issues/I9KPNL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hecunmao Change-Id: Ifcff3266943801802b67943feba1c878ba171476 --- .../builtins/builtins_array_stub_builder.cpp | 32 +++++++++++++++---- test/moduletest/arrayflatmap/arrayflatmap.js | 14 +++++++- .../moduletest/arrayflatmap/expect_output.txt | 2 ++ 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/ecmascript/compiler/builtins/builtins_array_stub_builder.cpp b/ecmascript/compiler/builtins/builtins_array_stub_builder.cpp index aacd2b5916..33b02c9437 100644 --- a/ecmascript/compiler/builtins/builtins_array_stub_builder.cpp +++ b/ecmascript/compiler/builtins/builtins_array_stub_builder.cpp @@ -4629,8 +4629,9 @@ void BuiltinsArrayStubBuilder::FlatMap(GateRef glue, GateRef thisValue, GateRef } Bind(&retValueIsJsArray); { - GateRef arrLen = ZExtInt32ToInt64(GetArrayLength(retValue)); - newArrLen = Int64Sub(Int64Add(*newArrLen, arrLen), Int64(1)); + GateRef elementsNum = + ZExtInt32ToInt64(GetNumberOfElements(retValue)); // newArray only contains non-hole elements + newArrLen = Int64Sub(Int64Add(*newArrLen, elementsNum), Int64(1)); Jump(&loopEnd); } } @@ -4709,8 +4710,9 @@ void BuiltinsArrayStubBuilder::FlatMap(GateRef glue, GateRef thisValue, GateRef } Bind(&retValueIsJsArray); { - GateRef arrLen = ZExtInt32ToInt64(GetArrayLength(retValue)); - newArrLen = Int64Sub(Int64Add(*newArrLen, arrLen), Int64(1)); + GateRef elementsNum = + ZExtInt32ToInt64(GetNumberOfElements(retValue)); // newArray only contains non-hole elements + newArrLen = Int64Sub(Int64Add(*newArrLen, elementsNum), Int64(1)); Jump(&loopEnd); } } @@ -4727,6 +4729,7 @@ void BuiltinsArrayStubBuilder::FlatMap(GateRef glue, GateRef thisValue, GateRef { i = Int64(0); DEFVARIABLE(j, VariableType::INT64(), Int64(0)); + DEFVARIABLE(retValueItem, VariableType::JS_ANY(), Hole()); GateRef newArray = NewArray(glue, *newArrLen); Label loopHead2(env); Label loopEnd2(env); @@ -4750,19 +4753,36 @@ void BuiltinsArrayStubBuilder::FlatMap(GateRef glue, GateRef thisValue, GateRef BRANCH(IsJsArray(retValue), &retValueIsJsArray, &retValueIsNotJsArray); Bind(&retValueIsJsArray); { + Label retValueIsStableArray(env); + Label retValueNotStableArray(env); + GateRef retValueIsStable = IsStableJSArray(glue, retValue); GateRef arrLen = ZExtInt32ToInt64(GetArrayLength(retValue)); DEFVARIABLE(k, VariableType::INT64(), Int64(0)); Label loopHead3(env); Label loopEnd3(env); Label next3(env); Label loopExit3(env); + Label setValue(env); + Label itemExist(env); Jump(&loopHead3); LoopBegin(&loopHead3); { BRANCH(Int64LessThan(*k, arrLen), &next3, &loopExit3); Bind(&next3); - SetValueWithElementsKind(glue, newArray, GetTaggedValueWithElementsKind(retValue, *k), *j, - Boolean(true), Int32(static_cast(ElementsKind::NONE))); + BRANCH(retValueIsStable, &retValueIsStableArray, &retValueNotStableArray); + Bind(&retValueIsStableArray); + retValueItem = GetTaggedValueWithElementsKind(retValue, *k); + BRANCH(TaggedIsHole(*retValueItem), &loopEnd3, &setValue); + Bind(&retValueNotStableArray); + GateRef hasProp = CallRuntime(glue, RTSTUB_ID(HasProperty), { retValue, IntToTaggedInt(*k) }); + BRANCH(TaggedIsTrue(hasProp), &itemExist, &loopEnd3); + Bind(&itemExist); + retValueItem = + FastGetPropertyByIndex(glue, retValue, TruncInt64ToInt32(*k), ProfileOperation()); + Jump(&setValue); + Bind(&setValue); + SetValueWithElementsKind(glue, newArray, *retValueItem, *j, Boolean(true), + Int32(static_cast(ElementsKind::NONE))); j = Int64Add(*j, Int64(1)); Jump(&loopEnd3); } diff --git a/test/moduletest/arrayflatmap/arrayflatmap.js b/test/moduletest/arrayflatmap/arrayflatmap.js index a6866afbdd..689be7a480 100644 --- a/test/moduletest/arrayflatmap/arrayflatmap.js +++ b/test/moduletest/arrayflatmap/arrayflatmap.js @@ -62,4 +62,16 @@ arr3.__proto__.pop(3); let arr4 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; var result4 = arr4.flatMap(testFunction); -print(result4); \ No newline at end of file +print(result4); + +let arr = [1,,,,,5]; +let res = arr.flatMap((x)=>{ + let ret=[x,x+1]; + ret[105] = x+2; + Object.defineProperty(ret,2000,{value:x+3}); + return ret; +}) +print(res); +print(res.length) + + diff --git a/test/moduletest/arrayflatmap/expect_output.txt b/test/moduletest/arrayflatmap/expect_output.txt index 29194bff9c..163abc9b07 100644 --- a/test/moduletest/arrayflatmap/expect_output.txt +++ b/test/moduletest/arrayflatmap/expect_output.txt @@ -16,3 +16,5 @@ true 1,1,1,3,9,4,16,5,25 1,1,1,3,9,4,16 1,1,1,3,9,4,16,5,25 +1,2,3,4,5,6,7,8 +8