修复flat和flatmap返回数组构造异常的问题。

Signed-off-by: 杨云飞 <yangyunfei19@h-partners.com>
This commit is contained in:
杨云飞 2023-12-11 08:45:20 +08:00
parent 313a711d12
commit 8f141e3211
7 changed files with 73 additions and 1 deletions

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

@ -21,6 +21,7 @@ group("ark_js_moduletest") {
"array",
"arrayfindlast",
"arrayflat",
"arrayflatmap",
"arrayfindindex",
"arrayfindlastindex",
"arrayforeach",
@ -183,6 +184,7 @@ group("ark_asm_test") {
"array",
"arrayfindlast",
"arrayflat",
"arrayflatmap",
"arrayfindlastindex",
"arrayfindindex",
"arrayforeach",
@ -318,6 +320,7 @@ group("ark_asm_single_step_test") {
"arrayfindindex",
"arrayfindlast",
"arrayflat",
"arrayflatmap",
"arrayfindlastindex",
"arrayforeach",
"arrayjoin",

View File

@ -22,3 +22,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,25 @@
/*
* 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);
}

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