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 48879050e5a348ae048aa8b23122ff3a30932d76..15b6dd42582ac855ea8676bbc77acdbab5b72aed 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 0729488a2a0faca244406e723bdea0c5d38f183f..939e788db79cbff903c04e69bc01ef672f94eb52 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 0729488a2a0faca244406e723bdea0c5d38f183f..939e788db79cbff903c04e69bc01ef672f94eb52 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 eb704505c4782ae8b9f905b672f3585450c400c7..939e788db79cbff903c04e69bc01ef672f94eb52 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 48879050e5a348ae048aa8b23122ff3a30932d76..f2be1c8b3187fb892e03ce00b08dc9524d5880f0 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 48879050e5a348ae048aa8b23122ff3a30932d76..5d87e24565a8a06e7b4bb68be28108551e2cad5b 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 48879050e5a348ae048aa8b23122ff3a30932d76..5d87e24565a8a06e7b4bb68be28108551e2cad5b 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 0000000000000000000000000000000000000000..8a2d6f621fba30627ce461cae330141b0d0300d0 --- /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 0000000000000000000000000000000000000000..8a2d6f621fba30627ce461cae330141b0d0300d0 --- /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 0000000000000000000000000000000000000000..4d0f814fda98c789dedae02afbeb4f129ab83674 --- /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 ae0b0fc03662c9fd118b3eca21183f77c33bd97f..fc889412496bd69a8fabc46245fc3563a0d65b22 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 75a6297a08e7ac5fe8f1bef1067efea82b354d7b..59b3eb8943ede237e071e3ea12d5a944af1745ba 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 6d27b55a8f2a317a9a08587744cc6c3d2450f198..b8a90dec81ca7f8e5619679f013859b38677ad6a 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()),