From fc3a84a9f93c3a26d8723ad6c18211c45a1a2659 Mon Sep 17 00:00:00 2001 From: huyunhui Date: Tue, 23 Jan 2024 07:06:21 +0000 Subject: [PATCH] use define semantic instead of set semantic in object literal Issue:https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/I8XNRO Signed-off-by: huyunhui Change-Id: Id8677bd57d0aae87371a5afb2706462c3578c94d --- es2panda/compiler/core/function.cpp | 2 +- es2panda/compiler/core/pandagen.cpp | 2 +- es2panda/compiler/core/pandagen.h | 2 +- es2panda/ir/expressions/objectExpression.cpp | 6 ++++- .../test-commonjs-main-func-args-expected.txt | 2 +- .../object-define-property-expected.txt | 0 .../object-define-property.js | 27 +++++++++++++++++++ .../binder/test-func-naming-expected.txt | 4 +-- 8 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 es2panda/test/compiler/js/objectExpression/object-define-property-expected.txt create mode 100644 es2panda/test/compiler/js/objectExpression/object-define-property.js diff --git a/es2panda/compiler/core/function.cpp b/es2panda/compiler/core/function.cpp index 8700de95af..9041063eb7 100644 --- a/es2panda/compiler/core/function.cpp +++ b/es2panda/compiler/core/function.cpp @@ -164,7 +164,7 @@ static void CompileField(PandaGen *pg, const ir::ClassProperty *prop, VReg thisR if (prop->IsPrivate()) { pg->DefineClassPrivateField(prop, result.lexLevel, result.result.slot, thisReg); } else { - pg->DefineClassField(prop, thisReg, op); + pg->DefineOwnProperty(prop, thisReg, op); } } diff --git a/es2panda/compiler/core/pandagen.cpp b/es2panda/compiler/core/pandagen.cpp index be375c35b8..79f9656c1d 100644 --- a/es2panda/compiler/core/pandagen.cpp +++ b/es2panda/compiler/core/pandagen.cpp @@ -473,7 +473,7 @@ void PandaGen::StoreObjProperty(const ir::AstNode *node, VReg obj, const Operand StoreObjByName(node, obj, std::get(prop)); } -void PandaGen::DefineClassField(const ir::AstNode *node, VReg obj, const Operand &prop) +void PandaGen::DefineOwnProperty(const ir::AstNode *node, VReg obj, const Operand &prop) { if (std::holds_alternative(prop)) { DefineFieldByValue(node, obj, std::get(prop)); diff --git a/es2panda/compiler/core/pandagen.h b/es2panda/compiler/core/pandagen.h index 5702c4dee3..5516727bfd 100644 --- a/es2panda/compiler/core/pandagen.h +++ b/es2panda/compiler/core/pandagen.h @@ -303,7 +303,7 @@ public: void LoadObjByName(const ir::AstNode *node, VReg obj, const util::StringView &prop); void StoreObjProperty(const ir::AstNode *node, VReg obj, const Operand &prop); - void DefineClassField(const ir::AstNode *node, VReg obj, const Operand &prop); + void DefineOwnProperty(const ir::AstNode *node, VReg obj, const Operand &prop); void DefineClassPrivateField(const ir::AstNode *node, uint32_t level, uint32_t slot, VReg obj); void StoreOwnProperty(const ir::AstNode *node, VReg obj, const Operand &prop, bool nameSetting = false); void DeleteObjProperty(const ir::AstNode *node, VReg obj, const Operand &prop); diff --git a/es2panda/ir/expressions/objectExpression.cpp b/es2panda/ir/expressions/objectExpression.cpp index 07517eac04..90cc020fa8 100644 --- a/es2panda/ir/expressions/objectExpression.cpp +++ b/es2panda/ir/expressions/objectExpression.cpp @@ -394,7 +394,11 @@ void ObjectExpression::CompilePropertyWithInit(compiler::PandaGen *pg, const ir: } value->Compile(pg); - pg->StoreOwnProperty(this, objReg, key, nameSetting); + if (!nameSetting && pg->Binder()->Program()->TargetApiVersion() > 10) { + pg->DefineOwnProperty(this, objReg, key); + } else { + pg->StoreOwnProperty(this, objReg, key, nameSetting); + } pg->SetSourceLocationFlag(lexer::SourceLocationFlag::VALID_SOURCE_LOCATION); } diff --git a/es2panda/test/bytecode/commonjs/test-commonjs-main-func-args-expected.txt b/es2panda/test/bytecode/commonjs/test-commonjs-main-func-args-expected.txt index 7b14cad064..ab578fdfd2 100644 --- a/es2panda/test/bytecode/commonjs/test-commonjs-main-func-args-expected.txt +++ b/es2panda/test/bytecode/commonjs/test-commonjs-main-func-args-expected.txt @@ -8,7 +8,7 @@ label_0: createobjectwithbuffer 0x0, _0 sta v1 lda v0 - stownbyname 0x1, a, v1 + definefieldbyname 0x1, a, v1 ldundefined returnundefined label_2: diff --git a/es2panda/test/compiler/js/objectExpression/object-define-property-expected.txt b/es2panda/test/compiler/js/objectExpression/object-define-property-expected.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/es2panda/test/compiler/js/objectExpression/object-define-property.js b/es2panda/test/compiler/js/objectExpression/object-define-property.js new file mode 100644 index 0000000000..8f2311408a --- /dev/null +++ b/es2panda/test/compiler/js/objectExpression/object-define-property.js @@ -0,0 +1,27 @@ +/* + * 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. + */ + +function func() { + let a = 1; + let obj = { + set a(run) { + run() + }, + [a]: 1, + "b": 2, + a: func + } +} +func(); \ No newline at end of file diff --git a/es2panda/test/parser/binder/test-func-naming-expected.txt b/es2panda/test/parser/binder/test-func-naming-expected.txt index 793348b894..455956e2aa 100644 --- a/es2panda/test/parser/binder/test-func-naming-expected.txt +++ b/es2panda/test/parser/binder/test-func-naming-expected.txt @@ -66,13 +66,13 @@ label_0: createobjectwithbuffer 0x7, _0 sta v0 definefunc 0x8, .#1471473088249306135#, 0x0 - stownbyname 0x9, index.js, v0 + definefieldbyname 0x9, index.js, v0 lda v0 sttoglobalrecord 0xb, obj createobjectwithbuffer 0xc, _1 sta v0 definefunc 0xd, .#5496465400387880830#, 0x0 - stownbyname 0xe, \testcase, v0 + definefieldbyname 0xe, \testcase, v0 lda v0 sttoglobalrecord 0x10, objBackslash ldundefined -- Gitee