diff --git a/ecmascript/js_api/js_api_stack.cpp b/ecmascript/js_api/js_api_stack.cpp index 4e6500cef9..2805257524 100644 --- a/ecmascript/js_api/js_api_stack.cpp +++ b/ecmascript/js_api/js_api_stack.cpp @@ -213,7 +213,17 @@ bool JSAPIStack::SetProperty(JSThread *thread, const JSHandle &obj, const JSHandle &value) { int length = obj->GetTop() + 1; - int index = key->GetInt(); + JSHandle indexKey = key; + if (indexKey->IsDouble()) { + // Math.floor(1) will produce TaggedDouble, we need to cast into TaggedInt + // For integer which is greater than INT32_MAX, it will remain TaggedDouble + indexKey = JSHandle(thread, JSTaggedValue::TryCastDoubleToInt32(indexKey->GetDouble())); + } + if (!indexKey->IsInt()) { + return false; + } + + int index = indexKey->GetInt(); if (index < 0 || index >= length) { return false; } diff --git a/test/moduletest/stack/BUILD.gn b/test/moduletest/stack/BUILD.gn new file mode 100644 index 0000000000..3aa4e0db22 --- /dev/null +++ b/test/moduletest/stack/BUILD.gn @@ -0,0 +1,18 @@ +# Copyright (c) 2024 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("stack") { + deps = [] +} diff --git a/test/moduletest/stack/expect_output.txt b/test/moduletest/stack/expect_output.txt new file mode 100644 index 0000000000..9e5cf50ea6 --- /dev/null +++ b/test/moduletest/stack/expect_output.txt @@ -0,0 +1,14 @@ +# Copyright (c) 2024 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. + +[object Stack] diff --git a/test/moduletest/stack/stack.js b/test/moduletest/stack/stack.js new file mode 100644 index 0000000000..2e377bc811 --- /dev/null +++ b/test/moduletest/stack/stack.js @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2024 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. + */ + +/* + * @tc.name:stack + * @tc.desc:test Stack + * @tc.type: FUNC + * @tc.require: issueIAZ99N + */ +let arkPrivate = globalThis.ArkPrivate; +var Stack = arkPrivate.Load(arkPrivate.Stack); +const v1 = new Stack(); +let v2 = Math.floor([1.1]); +v1[v2] = 0; +print(v1); \ No newline at end of file