From a6c6dfa3ef20722cc09ccf6ea7423553fd961f48 Mon Sep 17 00:00:00 2001 From: ctw-ian Date: Fri, 28 Jul 2023 09:07:11 +0800 Subject: [PATCH] Fix do-while statement compilation Issue:https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I7OMEW Signed-off-by: ctw-ian Change-Id: If32e344280421331439aeb93f859f5bdfbb89d3b --- es2panda/compiler/core/dynamicContext.cpp | 2 +- es2panda/compiler/core/envScope.cpp | 9 +++++ es2panda/compiler/core/envScope.h | 2 + es2panda/ir/statements/doWhileStatement.cpp | 7 ++-- .../do-while-body-lexical-break-expected.txt | 1 + .../lexicalEnv/do-while-body-lexical-break.js | 29 ++++++++++++++ ...o-while-body-lexical-continue-expected.txt | 1 + .../do-while-body-lexical-continue.js | 34 ++++++++++++++++ ...while-body-lexical-exit-break-expected.txt | 1 + .../do-while-body-lexical-exit-break.js | 39 +++++++++++++++++++ .../do-while-body-lexical-expected.txt | 1 + .../do-while-body-lexical-return-expected.txt | 1 + .../do-while-body-lexical-return.js | 29 ++++++++++++++ .../js/lexicalEnv/do-while-body-lexical.js | 29 ++++++++++++++ 14 files changed, 180 insertions(+), 5 deletions(-) create mode 100644 es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-break-expected.txt create mode 100644 es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-break.js create mode 100644 es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-continue-expected.txt create mode 100644 es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-continue.js create mode 100644 es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-exit-break-expected.txt create mode 100644 es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-exit-break.js create mode 100644 es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-expected.txt create mode 100644 es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-return-expected.txt create mode 100644 es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-return.js create mode 100644 es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical.js diff --git a/es2panda/compiler/core/dynamicContext.cpp b/es2panda/compiler/core/dynamicContext.cpp index b2cc4c26d6..372c3738bc 100644 --- a/es2panda/compiler/core/dynamicContext.cpp +++ b/es2panda/compiler/core/dynamicContext.cpp @@ -99,7 +99,7 @@ void LexEnvContext::AbortContext([[maybe_unused]] ControlFlowChange cfc, } const auto *node = envScope_->Scope()->Node(); - if (node->IsForUpdateStatement()) { + if (node->IsForUpdateStatement() || node->IsDoWhileStatement()) { return; } diff --git a/es2panda/compiler/core/envScope.cpp b/es2panda/compiler/core/envScope.cpp index aa3e98344a..7aff0e1bfb 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 73085741bf..147a2230ac 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 d5b05b8bf5..359adf736f 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 0000000000..d00491fd7e --- /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 0000000000..fe3a950c6b --- /dev/null +++ b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-break.js @@ -0,0 +1,29 @@ +/* + * 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. + */ + +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 0000000000..d00491fd7e --- /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 0000000000..f31590865c --- /dev/null +++ b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-continue.js @@ -0,0 +1,34 @@ +/* + * 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. + */ + +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-exit-break-expected.txt b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-exit-break-expected.txt new file mode 100644 index 0000000000..0cfbf08886 --- /dev/null +++ b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-exit-break-expected.txt @@ -0,0 +1 @@ +2 diff --git a/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-exit-break.js b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-exit-break.js new file mode 100644 index 0000000000..3a3e23b42e --- /dev/null +++ b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-exit-break.js @@ -0,0 +1,39 @@ +/* + * 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 a2() { + let dd; + let a1 = 2 + function a() { + var c = 1; + do { + let d = 1; + if (c == 2) { + break; + } + dd = function b() { + print(d, a1); + } + c++; + d++; + } while (true) + print(a1); + } + + a(); +} + +a2(); + 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 0000000000..d00491fd7e --- /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 0000000000..d00491fd7e --- /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 0000000000..9e4434d987 --- /dev/null +++ b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical-return.js @@ -0,0 +1,29 @@ +/* + * 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. + */ + +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 0000000000..8d4da17a6d --- /dev/null +++ b/es2panda/test/compiler/js/lexicalEnv/do-while-body-lexical.js @@ -0,0 +1,29 @@ +/* + * 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. + */ + +let c; +let b = 1; +function x() { + do { + let a = 1; + c = function b() { + print(a); + } + b++; + } while ((() => { return b })() == 1) +} + +x(); +c(); -- Gitee