diff --git a/es2panda/compiler/core/envScope.cpp b/es2panda/compiler/core/envScope.cpp index aa3e98344a8b732ed722d99c151615de6372f716..7aff0e1bfbf92641fc473b642d8258ef5d5a2687 100644 --- a/es2panda/compiler/core/envScope.cpp +++ b/es2panda/compiler/core/envScope.cpp @@ -81,4 +81,13 @@ void LoopEnvScope::CopyPerIterationCtx() } } +void LoopEnvScope::PopLexEnvBeforeTheNextIter() +{ + if (!HasEnv()) { + return; + } + + pg_->PopLexEnv(scope_->Node()); +} + } // namespace panda::es2panda::compiler diff --git a/es2panda/compiler/core/envScope.h b/es2panda/compiler/core/envScope.h index 73085741bf688d28671a9cd6a85b0a37b4558cb3..147a2230ac0a30d3b6ddf5087954f6c605aa6985 100644 --- a/es2panda/compiler/core/envScope.h +++ b/es2panda/compiler/core/envScope.h @@ -93,6 +93,8 @@ public: void CopyPerIterationCtx(); + void PopLexEnvBeforeTheNextIter(); + private: static bool NeedEnv(binder::VariableScope *scope) { diff --git a/es2panda/ir/statements/doWhileStatement.cpp b/es2panda/ir/statements/doWhileStatement.cpp index d5b05b8bf5d7101c69753950b708b8c831bafd52..359adf736f15377ccb84ecdfe0af6ba7199384f8 100644 --- a/es2panda/ir/statements/doWhileStatement.cpp +++ b/es2panda/ir/statements/doWhileStatement.cpp @@ -44,13 +44,12 @@ void DoWhileStatement::Compile(compiler::PandaGen *pg) const pg->SetLabel(this, startLabel); - { - compiler::LoopEnvScope envScope(pg, labelTarget, scope_); - body_->Compile(pg); - } + compiler::LoopEnvScope envScope(pg, labelTarget, scope_); + body_->Compile(pg); pg->SetLabel(this, labelTarget.ContinueTarget()); compiler::Condition::Compile(pg, this->Test(), labelTarget.BreakTarget()); + envScope.PopLexEnvBeforeTheNextIter(); pg->Branch(this, startLabel); pg->SetLabel(this, labelTarget.BreakTarget()); diff --git a/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-break-expected.txt b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-break-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..d00491fd7e5bb6fa28c517a0bb32b8b506539d4d --- /dev/null +++ b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-break-expected.txt @@ -0,0 +1 @@ +1 diff --git a/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-break.js b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-break.js new file mode 100644 index 0000000000000000000000000000000000000000..56f5e8e8f855fa5d01e153624c7614a23fa58151 --- /dev/null +++ b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-break.js @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022 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. + */ + +let c; +let b = 1; +function x() { + do { + let a = 1; + c = function b() { + print(a); + } + break; + } while ((() => { return b })() == 1) +} + +x(); +c(); diff --git a/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-continue-expected.txt b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-continue-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..d00491fd7e5bb6fa28c517a0bb32b8b506539d4d --- /dev/null +++ b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-continue-expected.txt @@ -0,0 +1 @@ +1 diff --git a/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-continue.js b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-continue.js new file mode 100644 index 0000000000000000000000000000000000000000..2e06e43af6a1507c4f82167d8958b5d8df218c3e --- /dev/null +++ b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-continue.js @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022 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. + */ + +let c; +let b = 1; +function x() { + do { + let a = 1; + if (b == 1) { + b++; + continue; + } + + c = function b() { + print(a); + } + b++; + } while ((() => { return b })() != 3) +} + +x(); +c(); diff --git a/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-expected.txt b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..d00491fd7e5bb6fa28c517a0bb32b8b506539d4d --- /dev/null +++ b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-expected.txt @@ -0,0 +1 @@ +1 diff --git a/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-return-expected.txt b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-return-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..d00491fd7e5bb6fa28c517a0bb32b8b506539d4d --- /dev/null +++ b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-return-expected.txt @@ -0,0 +1 @@ +1 diff --git a/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-return.js b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-return.js new file mode 100644 index 0000000000000000000000000000000000000000..2a1db269e72ddc7ca2c108ed16e8b6e81a35557d --- /dev/null +++ b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-return.js @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022 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. + */ + +let c; +let b = 1; +function x() { + do { + let a = 1; + c = function b() { + print(a); + } + return; + } while ((() => { return b })() == 1) +} + +x() +c(); diff --git a/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical.js b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical.js new file mode 100644 index 0000000000000000000000000000000000000000..938eac345a92cb73bbaff87a40f4c8289b71a911 --- /dev/null +++ b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical.js @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022 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. + */ + +let c; +let b = 1; +function x() { + do { + let a = 1; + c = function b() { + print(a); + } + b++; + } while ((() => { return b })() == 1) +} + +x(); +c();