From 7031c85c2b03a429c1638d38727071de5037d045 Mon Sep 17 00:00:00 2001 From: gavin1012_hw Date: Thu, 9 Mar 2023 19:53:02 +0800 Subject: [PATCH] fixed be4baf4 from https://gitee.com/gavin1012_hw/ark_ts2abc/pulls/906 Fix the bug of reading symbolMap's lexenv in hotfix process Issue: I6LJ9J Signed-off-by: gavin1012_hw Change-Id: I4e762e68c4b33b2822762f50a05256056e95df2e --- .../add-lexenv-1/expected.txt | 2 +- .../modify-lexenv-1/expected.txt | 2 +- .../modify-lexenv-2/expected.txt | 2 +- .../modify-lexenv-3/expected.txt | 2 +- .../modify-lexenv-4/expected.txt | 2 +- .../modify-lexenv-5/expected.txt | 2 +- .../modify-lexenv-6/expected.txt | 2 +- .../multi-lexenv-with-same-name/base.js | 28 +++++++++++++++++++ .../multi-lexenv-with-same-name/base_mod.js | 28 +++++++++++++++++++ .../multi-lexenv-with-same-name/expected.txt | 12 ++++++++ es2panda/util/hotfix.cpp | 22 ++++++++------- es2panda/util/symbolTable.cpp | 7 +++-- es2panda/util/symbolTable.h | 2 +- 13 files changed, 92 insertions(+), 21 deletions(-) create mode 100644 es2panda/test/hotfix/hotfix-throwerror/multi-lexenv-with-same-name/base.js create mode 100644 es2panda/test/hotfix/hotfix-throwerror/multi-lexenv-with-same-name/base_mod.js create mode 100644 es2panda/test/hotfix/hotfix-throwerror/multi-lexenv-with-same-name/expected.txt diff --git a/es2panda/test/hotfix/hotfix-throwerror/add-lexenv-1/expected.txt b/es2panda/test/hotfix/hotfix-throwerror/add-lexenv-1/expected.txt index 48879050e5..15b6dd4258 100644 --- a/es2panda/test/hotfix/hotfix-throwerror/add-lexenv-1/expected.txt +++ b/es2panda/test/hotfix/hotfix-throwerror/add-lexenv-1/expected.txt @@ -11,7 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -[Patch] Found lexenv size changed, not supported! +[Patch] Found lexical variable added or removed in .A, not supported! [Patch] Found unsupported change in file, will not generate patch! Error: [base_mod.js:0:0] the size of programs is expected to be 1, but is 0 diff --git a/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-1/expected.txt b/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-1/expected.txt index 0729488a2a..939e788db7 100644 --- a/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-1/expected.txt +++ b/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-1/expected.txt @@ -11,7 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -[Patch] Found new lex env added in function .A, not supported! +[Patch] Found lexical variable changed in function .A, not supported! [Patch] Found unsupported change in file, will not generate patch! Error: [base_mod.js:0:0] the size of programs is expected to be 1, but is 0 diff --git a/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-2/expected.txt b/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-2/expected.txt index 0729488a2a..939e788db7 100644 --- a/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-2/expected.txt +++ b/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-2/expected.txt @@ -11,7 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -[Patch] Found new lex env added in function .A, not supported! +[Patch] Found lexical variable changed in function .A, not supported! [Patch] Found unsupported change in file, will not generate patch! Error: [base_mod.js:0:0] the size of programs is expected to be 1, but is 0 diff --git a/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-3/expected.txt b/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-3/expected.txt index eb704505c4..939e788db7 100644 --- a/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-3/expected.txt +++ b/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-3/expected.txt @@ -11,7 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -[Patch] Found new lex env changed(slot or type) in function .A, not supported! +[Patch] Found lexical variable changed in function .A, not supported! [Patch] Found unsupported change in file, will not generate patch! Error: [base_mod.js:0:0] the size of programs is expected to be 1, but is 0 diff --git a/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-4/expected.txt b/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-4/expected.txt index 48879050e5..f2be1c8b31 100644 --- a/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-4/expected.txt +++ b/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-4/expected.txt @@ -11,7 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -[Patch] Found lexenv size changed, not supported! +[Patch] Found lexical variable added or removed in .makeAdder, not supported! [Patch] Found unsupported change in file, will not generate patch! Error: [base_mod.js:0:0] the size of programs is expected to be 1, but is 0 diff --git a/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-5/expected.txt b/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-5/expected.txt index 48879050e5..5d87e24565 100644 --- a/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-5/expected.txt +++ b/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-5/expected.txt @@ -11,7 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -[Patch] Found lexenv size changed, not supported! +[Patch] Found lexical variable added or removed in .a, not supported! [Patch] Found unsupported change in file, will not generate patch! Error: [base_mod.js:0:0] the size of programs is expected to be 1, but is 0 diff --git a/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-6/expected.txt b/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-6/expected.txt index 48879050e5..5d87e24565 100644 --- a/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-6/expected.txt +++ b/es2panda/test/hotfix/hotfix-throwerror/modify-lexenv-6/expected.txt @@ -11,7 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -[Patch] Found lexenv size changed, not supported! +[Patch] Found lexical variable added or removed in .a, not supported! [Patch] Found unsupported change in file, will not generate patch! Error: [base_mod.js:0:0] the size of programs is expected to be 1, but is 0 diff --git a/es2panda/test/hotfix/hotfix-throwerror/multi-lexenv-with-same-name/base.js b/es2panda/test/hotfix/hotfix-throwerror/multi-lexenv-with-same-name/base.js new file mode 100644 index 0000000000..8a2d6f621f --- /dev/null +++ b/es2panda/test/hotfix/hotfix-throwerror/multi-lexenv-with-same-name/base.js @@ -0,0 +1,28 @@ +/* + * 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 A() { + let a = 1; + { + let a = 2; + function b() { + a = 3; + } + } + + function c() { + a = 4; + } +} \ No newline at end of file diff --git a/es2panda/test/hotfix/hotfix-throwerror/multi-lexenv-with-same-name/base_mod.js b/es2panda/test/hotfix/hotfix-throwerror/multi-lexenv-with-same-name/base_mod.js new file mode 100644 index 0000000000..8a2d6f621f --- /dev/null +++ b/es2panda/test/hotfix/hotfix-throwerror/multi-lexenv-with-same-name/base_mod.js @@ -0,0 +1,28 @@ +/* + * 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 A() { + let a = 1; + { + let a = 2; + function b() { + a = 3; + } + } + + function c() { + a = 4; + } +} \ No newline at end of file diff --git a/es2panda/test/hotfix/hotfix-throwerror/multi-lexenv-with-same-name/expected.txt b/es2panda/test/hotfix/hotfix-throwerror/multi-lexenv-with-same-name/expected.txt new file mode 100644 index 0000000000..4d0f814fda --- /dev/null +++ b/es2panda/test/hotfix/hotfix-throwerror/multi-lexenv-with-same-name/expected.txt @@ -0,0 +1,12 @@ +# 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. diff --git a/es2panda/util/hotfix.cpp b/es2panda/util/hotfix.cpp index ae0b0fc036..fc88941249 100644 --- a/es2panda/util/hotfix.cpp +++ b/es2panda/util/hotfix.cpp @@ -278,9 +278,10 @@ uint32_t Hotfix::GetSlotIdFromSymbolTable(const std::string &variableName) { auto functionIter = originFunctionInfo_->find(funcMain0_); if (functionIter != originFunctionInfo_->end()) { - auto lexenvIter = functionIter->second.lexenv.find(variableName); - if (lexenvIter != functionIter->second.lexenv.end()) { - return lexenvIter->second.first; + for (const auto &lexenv : functionIter->second.lexenv) { + if (lexenv.second.first == variableName) { + return lexenv.first; + } } } return UINT32_MAX; @@ -429,24 +430,25 @@ bool Hotfix::CompareLexenv(const std::string &funcName, const compiler::PandaGen auto &lexenv = bytecodeInfo.lexenv; if (funcName != funcMain0_) { if (lexenv.size() != lexicalVarNameAndTypes.size()) { - std::cerr << "[Patch] Found lexenv size changed, not supported!" << std::endl; + std::cerr << "[Patch] Found lexical variable added or removed in " << funcName << ", not supported!" + << std::endl; patchError_ = true; return false; } for (auto &variable: lexicalVarNameAndTypes) { - auto varName = std::string(variable.second.first); - auto lexenvIter = lexenv.find(varName); + auto varSlot = variable.first; + auto lexenvIter = lexenv.find(varSlot); if (lexenvIter == lexenv.end()) { - std::cerr << "[Patch] Found new lex env added in function " << funcName << ", not supported!" + std::cerr << "[Patch] Found new lexical variable added in function " << funcName << ", not supported!" << std::endl; patchError_ = true; return false; } auto &lexInfo = lexenvIter->second; - if (variable.first != lexInfo.first || variable.second.second != lexInfo.second) { - std::cerr << "[Patch] Found new lex env changed(slot or type) in function " << funcName - << ", not supported!" << std::endl; + if (std::string(variable.second.first) != lexInfo.first || variable.second.second != lexInfo.second) { + std::cerr << "[Patch] Found lexical variable changed in function " << funcName << ", not supported!" + << std::endl; patchError_ = true; return false; } diff --git a/es2panda/util/symbolTable.cpp b/es2panda/util/symbolTable.cpp index 75a6297a08..59b3eb8943 100644 --- a/es2panda/util/symbolTable.cpp +++ b/es2panda/util/symbolTable.cpp @@ -72,9 +72,10 @@ bool SymbolTable::ReadSymbolTable(const std::string &symbolTable) info.classHash.insert(std::pair(classItems[i], classItems[i + 1])); } for (size_t i = 0; i < lexItems.size(); i = i + 3) { - std::pair slotAndType(std::atoi(std::string(lexItems[i + 1]).c_str()), - std::atoi(std::string(lexItems[i + 2]).c_str())); - info.lexenv.insert(std::pair>(lexItems[i], slotAndType)); + auto name = std::string(lexItems[i]); + auto slot = std::atoi(std::string(lexItems[i + 1]).c_str()); + auto type = std::atoi(std::string(lexItems[i + 2]).c_str()); + info.lexenv.insert({slot, std::pair(name, type)}); } originFunctionInfo_.insert(std::pair(info.funcInternalName, info)); diff --git a/es2panda/util/symbolTable.h b/es2panda/util/symbolTable.h index 6d27b55a8f..b8a90dec81 100644 --- a/es2panda/util/symbolTable.h +++ b/es2panda/util/symbolTable.h @@ -30,7 +30,7 @@ public: std::string funcName; std::string funcInternalName; std::string funcHash; - ArenaMap> lexenv; // lexenv: > + ArenaMap> lexenv; // lexenv: > ArenaMap classHash; explicit OriginFunctionInfo(ArenaAllocator *allocator) : lexenv(allocator->Adapter()), -- Gitee