修复了自定义类型继承数组类,flat和flatmap后返回数组构造异常

Signed-off-by: 杨云飞 <yangyunfei19@h-partners.com>
This commit is contained in:
杨云飞 2023-11-29 10:16:40 +08:00
parent 3a6b687493
commit 5ac305f78a
8 changed files with 73 additions and 3 deletions

View File

@ -2771,7 +2771,7 @@ JSTaggedValue BuiltinsArray::Flat(EcmaRuntimeCallInfo *argv)
uint32_t arrayLen = 0;
JSTaggedValue newArray = JSArray::ArraySpeciesCreate(thread, thisObjHandle, JSTaggedNumber(arrayLen));
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
base::FlattenArgs args = { sourceLen, 0, depthNum };
JSHandle<JSObject> newArrayHandle(thread, newArray);
// 6. Perform ? FlattenIntoArray(A, O, sourceLen, 0, depthNum).

View File

@ -116,7 +116,8 @@ JSTaggedValue JSArray::ArraySpeciesCreate(JSThread *thread, const JSHandle<JSObj
if (isArray) {
// Let C be Get(originalArray, "constructor").
auto *hclass = originalArray->GetJSHClass();
if (hclass->IsJSArray() && !hclass->HasConstructor()) {
JSTaggedValue proto = hclass->GetPrototype();
if (hclass->IsJSArray() && !hclass->HasConstructor() && proto.IsJSArray()) {
return JSArray::ArrayCreate(thread, length, ArrayMode::LITERAL).GetTaggedValue();
}
JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();

View File

@ -20,6 +20,7 @@ group("ark_js_moduletest") {
"array",
"arrayfindlast",
"arrayflat",
"arrayflatmap",
"arrayfindindex",
"arrayfindlastindex",
"arrayforeach",
@ -178,6 +179,7 @@ group("ark_asm_test") {
"array",
"arrayfindlast",
"arrayflat",
"arrayflatmap",
"arrayfindlastindex",
"arrayfindindex",
"arrayforeach",
@ -311,6 +313,7 @@ group("ark_asm_single_step_test") {
"arrayfindindex",
"arrayfindlast",
"arrayflat",
"arrayflatmap",
"arrayfindlastindex",
"arrayforeach",
"arrayjoin",

View File

@ -21,4 +21,13 @@
*/
const input = [1, [2], [[3]]];
print(input.flat(undefined));
{
class MyArray extends Array {
static get [Symbol.species]() {
return this;
}
}
const wannabe = new MyArray();
const flattened = wannabe.flat(Infinity);
print(flattened instanceof MyArray);
}

View File

@ -12,3 +12,4 @@
# limitations under the License.
1,2,3
true

View File

@ -0,0 +1,18 @@
# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//arkcompiler/ets_runtime/test/test_helper.gni")
host_moduletest_action("arrayflatmap") {
deps = []
}

View File

@ -0,0 +1,24 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
{
class MyArray extends Array {
static get [Symbol.species]() {
return this;
}
}
const wannabe = new MyArray();
const result = wannabe.flatMap(x => [x, x]);
print(result instanceof MyArray);//t
}

View File

@ -0,0 +1,14 @@
# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
true