From 5b0cae7d9558957c4f81b9d0da18c6babfaac16f Mon Sep 17 00:00:00 2001 From: ctw-ian Date: Mon, 6 Mar 2023 17:30:50 +0800 Subject: [PATCH] fixed 62fec9a from https://gitee.com/ctw-ian/ark_ts2abc/pulls/900 Fix several for-await-of 262 cases Issue:I6KAG4 Signed-off-by: ctw-ian Change-Id: I991255f9c2ae7d1a18b4535208015652968ac676 --- es2panda/binder/binder.cpp | 71 +++++++++++++++++++ es2panda/binder/binder.h | 3 + es2panda/compiler/base/destructuring.cpp | 37 ++++------ es2panda/compiler/base/iterators.cpp | 9 ++- es2panda/compiler/base/iterators.h | 1 + es2panda/compiler/base/lreference.cpp | 2 +- es2panda/compiler/core/dynamicContext.cpp | 44 ++++++++++++ es2panda/compiler/core/dynamicContext.h | 29 ++++++++ ...tructuring-target-arguments-1-expected.txt | 1 + ...nvalid-destructuring-target-arguments-1.js | 18 +++++ ...tructuring-target-arguments-2-expected.txt | 1 + ...nvalid-destructuring-target-arguments-2.js | 18 +++++ ...tructuring-target-arguments-3-expected.txt | 1 + ...nvalid-destructuring-target-arguments-3.js | 18 +++++ ...tructuring-target-arguments-4-expected.txt | 1 + ...nvalid-destructuring-target-arguments-4.js | 18 +++++ ...tructuring-target-arguments-5-expected.txt | 1 + ...nvalid-destructuring-target-arguments-5.js | 18 +++++ ...tructuring-target-arguments-6-expected.txt | 1 + ...nvalid-destructuring-target-arguments-6.js | 18 +++++ ...tructuring-target-arguments-7-expected.txt | 1 + ...nvalid-destructuring-target-arguments-7.js | 18 +++++ ...tructuring-target-arguments-8-expected.txt | 1 + ...nvalid-destructuring-target-arguments-8.js | 18 +++++ es2panda/util/helpers.cpp | 24 +++++++ es2panda/util/helpers.h | 3 + test262/es2abc_skip_tests.json | 54 -------------- 27 files changed, 348 insertions(+), 81 deletions(-) create mode 100644 es2panda/test/parser/js/Invalid-destructuring-target-arguments-1-expected.txt create mode 100644 es2panda/test/parser/js/Invalid-destructuring-target-arguments-1.js create mode 100644 es2panda/test/parser/js/Invalid-destructuring-target-arguments-2-expected.txt create mode 100644 es2panda/test/parser/js/Invalid-destructuring-target-arguments-2.js create mode 100644 es2panda/test/parser/js/Invalid-destructuring-target-arguments-3-expected.txt create mode 100644 es2panda/test/parser/js/Invalid-destructuring-target-arguments-3.js create mode 100644 es2panda/test/parser/js/Invalid-destructuring-target-arguments-4-expected.txt create mode 100644 es2panda/test/parser/js/Invalid-destructuring-target-arguments-4.js create mode 100644 es2panda/test/parser/js/Invalid-destructuring-target-arguments-5-expected.txt create mode 100644 es2panda/test/parser/js/Invalid-destructuring-target-arguments-5.js create mode 100644 es2panda/test/parser/js/Invalid-destructuring-target-arguments-6-expected.txt create mode 100644 es2panda/test/parser/js/Invalid-destructuring-target-arguments-6.js create mode 100644 es2panda/test/parser/js/Invalid-destructuring-target-arguments-7-expected.txt create mode 100644 es2panda/test/parser/js/Invalid-destructuring-target-arguments-7.js create mode 100644 es2panda/test/parser/js/Invalid-destructuring-target-arguments-8-expected.txt create mode 100644 es2panda/test/parser/js/Invalid-destructuring-target-arguments-8.js diff --git a/es2panda/binder/binder.cpp b/es2panda/binder/binder.cpp index e9369fa834..e74979bb3b 100644 --- a/es2panda/binder/binder.cpp +++ b/es2panda/binder/binder.cpp @@ -96,6 +96,70 @@ void Binder::ThrowUndeclaredExport(const lexer::SourcePosition &pos, const util: throw Error(ErrorType::SYNTAX, ss.str(), loc.line, loc.col); } +void Binder::ThrowInvalidDstrTarget(const lexer::SourcePosition &pos, const util::StringView &name) +{ + lexer::LineIndex index(program_->SourceCode()); + lexer::SourceLocation loc = index.GetLocation(pos); + + std::stringstream ss; + ss << "Invalid destructuring assignment target: " << name; + throw Error(ErrorType::SYNTAX, ss.str(), loc.line, loc.col); +} + +void Binder::CheckMandatoryArguments(const ir::Identifier *ident) +{ + const auto *iter = static_cast(ident); + bool isPatternMember = false; + while (iter) { + if (iter->IsArrayExpression() || iter->IsArrayPattern()) { + isPatternMember = true; + break; + } + + if (iter->IsObjectExpression() || iter->IsObjectPattern()) { + isPatternMember = util::Helpers::IsObjectPropertyValue(iter->AsObjectExpression(), ident); + break; + } + iter = iter->Parent(); + } + + if (!isPatternMember) { + return; + } + + auto *patternNode = iter; + + while (iter) { + if (iter->IsAssignmentExpression() || iter->IsVariableDeclarator() || iter->IsForInStatement() || + iter->IsForOfStatement()) { + break; + } + + iter = iter->Parent(); + } + + if (!iter) { + return; + } + + const ir::AstNode *potentialParent = iter; + + if (iter->IsAssignmentExpression()) { + potentialParent = iter->AsAssignmentExpression()->Left(); + } else if (iter->IsVariableDeclarator()) { + potentialParent = iter->AsVariableDeclarator()->Id(); + } else { + potentialParent = iter->IsForInStatement() ? iter->AsForInStatement()->Left() : + iter->AsForOfStatement()->Left(); + } + + if (!util::Helpers::IsChild(potentialParent, patternNode)) { + return; + } + + ThrowInvalidDstrTarget(ident->Start(), ident->Name()); +} + void Binder::AssignIndexToModuleVariable() { ASSERT(program_->ModuleRecord()); @@ -262,6 +326,9 @@ void Binder::BuildVarDeclaratorId(const ir::AstNode *parent, ir::AstNode *childN case ir::AstNodeType::IDENTIFIER: { auto *ident = childNode->AsIdentifier(); const auto &name = ident->Name(); + if (name.Is(FUNCTION_ARGUMENTS)) { + CheckMandatoryArguments(ident); + } if (util::Helpers::IsGlobalIdentifier(name)) { break; @@ -456,6 +523,10 @@ void Binder::ResolveReference(const ir::AstNode *parent, ir::AstNode *childNode) case ir::AstNodeType::IDENTIFIER: { auto *ident = childNode->AsIdentifier(); + if (ident->Name().Is(FUNCTION_ARGUMENTS)) { + CheckMandatoryArguments(ident); + } + if (ident->IsReference()) { LookupIdentReference(ident); } diff --git a/es2panda/binder/binder.h b/es2panda/binder/binder.h index 5d3edaaf95..47cf5efec0 100644 --- a/es2panda/binder/binder.h +++ b/es2panda/binder/binder.h @@ -89,6 +89,9 @@ public: [[noreturn]] void ThrowRedeclaration(const lexer::SourcePosition &pos, const util::StringView &name); [[noreturn]] void ThrowUndeclaredExport(const lexer::SourcePosition &pos, const util::StringView &name); + [[noreturn]] void ThrowInvalidDstrTarget(const lexer::SourcePosition &pos, const util::StringView &name); + + void CheckMandatoryArguments(const ir::Identifier *ident); template friend class LexicalScope; diff --git a/es2panda/compiler/base/destructuring.cpp b/es2panda/compiler/base/destructuring.cpp index 557be97ecb..90bc9feff3 100644 --- a/es2panda/compiler/base/destructuring.cpp +++ b/es2panda/compiler/base/destructuring.cpp @@ -78,9 +78,7 @@ static void GenArray(PandaGen *pg, const ir::ArrayExpression *array) return; } - TryContext tryCtx(pg); - const auto &labelSet = tryCtx.LabelSet(); - pg->SetLabel(array, labelSet.TryBegin()); + DestructuringIteratorContext dstrCtx(pg, iterator); for (const auto *element : array->Elements()) { RegScope ers(pg); @@ -123,28 +121,6 @@ static void GenArray(PandaGen *pg, const ir::ArrayExpression *array) lref.SetValue(); } - - pg->SetLabel(array, labelSet.TryEnd()); - - // Normal completion - pg->LoadAccumulator(array, iterator.Done()); - pg->BranchIfTrue(array, labelSet.CatchEnd()); - iterator.Close(false); - - pg->Branch(array, labelSet.CatchEnd()); - - Label *end = pg->AllocLabel(); - pg->SetLabel(array, labelSet.CatchBegin()); - pg->StoreAccumulator(array, iterator.Result()); - pg->LoadAccumulator(array, iterator.Done()); - - pg->BranchIfTrue(array, end); - pg->LoadAccumulator(array, iterator.Result()); - iterator.Close(true); - pg->SetLabel(array, end); - pg->LoadAccumulator(array, iterator.Result()); - pg->EmitThrow(array); - pg->SetLabel(array, labelSet.CatchEnd()); } static void GenObjectProperty(PandaGen *pg, const ir::ObjectExpression *object, @@ -200,6 +176,17 @@ static void GenObjectWithRest(PandaGen *pg, const ir::ObjectExpression *object, const auto &properties = object->Properties(); RegScope rs(pg); + + if (properties.size() == 1) { + auto *element = properties[0]; + ASSERT(element->IsRestElement()); + VReg defaultProp = pg->AllocReg(); + LReference lref = LReference::CreateLRef(pg, element, object->IsDeclaration()); + pg->CreateObjectWithExcludedKeys(element, rhs, defaultProp, 0); + lref.SetValue(); + return; + } + VReg propStart = pg->NextReg(); for (const auto *element : properties) { diff --git a/es2panda/compiler/base/iterators.cpp b/es2panda/compiler/base/iterators.cpp index 1cd69d46a0..2acf0f802e 100644 --- a/es2panda/compiler/base/iterators.cpp +++ b/es2panda/compiler/base/iterators.cpp @@ -24,7 +24,8 @@ namespace panda::es2panda::compiler { // Iterator Iterator::Iterator(PandaGen *pg, const ir::AstNode *node, IteratorType type) - : pg_(pg), node_(node), method_(pg->AllocReg()), iterator_(pg->AllocReg()), nextResult_(pg->AllocReg()), type_(type) + : pg_(pg), node_(node), closed_(pg->AllocReg()), method_(pg->AllocReg()), iterator_(pg->AllocReg()), + nextResult_(pg->AllocReg()), type_(type) { if (type_ == IteratorType::ASYNC) { pg_->GetAsyncIterator(node); @@ -35,6 +36,7 @@ Iterator::Iterator(PandaGen *pg, const ir::AstNode *node, IteratorType type) pg_->StoreAccumulator(node, iterator_); pg_->LoadObjByName(node_, iterator_, "next"); pg_->StoreAccumulator(node_, method_); + pg_->StoreConst(node_, closed_, Constant::JS_FALSE); } void Iterator::GetMethod(util::StringView name) const @@ -84,6 +86,11 @@ void Iterator::Close(bool abruptCompletion) const Label *noReturn = pg_->AllocLabel(); pg_->StoreAccumulator(node_, completion); + pg_->LoadAccumulator(node_, closed_); + pg_->BranchIfTrue(node_, noReturn); + + pg_->StoreConst(node_, closed_, Constant::JS_TRUE); + pg_->StoreConst(node_, innerException, Constant::JS_HOLE); TryContext tryCtx(pg_); diff --git a/es2panda/compiler/base/iterators.h b/es2panda/compiler/base/iterators.h index d43bc95687..05c0eda04d 100644 --- a/es2panda/compiler/base/iterators.h +++ b/es2panda/compiler/base/iterators.h @@ -67,6 +67,7 @@ public: protected: PandaGen *pg_; const ir::AstNode *node_; + VReg closed_; // These 3 regs must be allocated continuously VReg method_; VReg iterator_; diff --git a/es2panda/compiler/base/lreference.cpp b/es2panda/compiler/base/lreference.cpp index e354250c80..1fdc88a98a 100644 --- a/es2panda/compiler/base/lreference.cpp +++ b/es2panda/compiler/base/lreference.cpp @@ -123,7 +123,7 @@ LReference LReference::CreateLRef(PandaGen *pg, const ir::AstNode *node, bool is return LReference::CreateLRef(pg, node->AsAssignmentPattern()->Left(), true); } case ir::AstNodeType::REST_ELEMENT: { - return LReference::CreateLRef(pg, node->AsRestElement()->Argument(), true); + return LReference::CreateLRef(pg, node->AsRestElement()->Argument(), isDeclaration); } case ir::AstNodeType::EXPORT_DEFAULT_DECLARATION: { // export default [anonymous class decl] diff --git a/es2panda/compiler/core/dynamicContext.cpp b/es2panda/compiler/core/dynamicContext.cpp index 1dab58cece..c586200056 100644 --- a/es2panda/compiler/core/dynamicContext.cpp +++ b/es2panda/compiler/core/dynamicContext.cpp @@ -132,6 +132,50 @@ void IteratorContext::AbortContext([[maybe_unused]] ControlFlowChange cfc, iterator_.Close(false); } +DestructuringIteratorContext::DestructuringIteratorContext(PandaGen *pg, const DestructuringIterator &iterator) + : DynamicContext(pg, {}), iterator_(iterator), catchTable_(pg->CreateCatchTable()) +{ + const auto &labelSet = catchTable_->LabelSet(); + pg_->SetLabel(iterator_.Node(), labelSet.TryBegin()); +} + +DestructuringIteratorContext::~DestructuringIteratorContext() +{ + const auto &labelSet = catchTable_->LabelSet(); + const auto *node = iterator_.Node(); + + pg_->SetLabel(node, labelSet.TryEnd()); + + // Normal completion + pg_->LoadAccumulator(node, iterator_.Done()); + pg_->BranchIfTrue(node, labelSet.CatchEnd()); + iterator_.Close(false); + + pg_->Branch(node, labelSet.CatchEnd()); + + Label *end = pg_->AllocLabel(); + pg_->SetLabel(node, labelSet.CatchBegin()); + pg_->StoreAccumulator(node, iterator_.Result()); + pg_->LoadAccumulator(node, iterator_.Done()); + + pg_->BranchIfTrue(node, end); + pg_->LoadAccumulator(node, iterator_.Result()); + iterator_.Close(true); + pg_->SetLabel(node, end); + pg_->LoadAccumulator(node, iterator_.Result()); + pg_->EmitThrow(node); + pg_->SetLabel(node, labelSet.CatchEnd()); +} + +void DestructuringIteratorContext::AbortContext(ControlFlowChange cfc, const util::StringView &targetLabel) +{ + if (cfc == ControlFlowChange::CONTINUE && target_.ContinueLabel() == targetLabel) { + return; + } + + iterator_.Close(false); +} + void TryContext::InitFinalizer() { ASSERT(tryStmt_); diff --git a/es2panda/compiler/core/dynamicContext.h b/es2panda/compiler/core/dynamicContext.h index 8edb42e5c2..1cd4550cfc 100644 --- a/es2panda/compiler/core/dynamicContext.h +++ b/es2panda/compiler/core/dynamicContext.h @@ -151,6 +151,35 @@ private: CatchTable *catchTable_; }; +class DestructuringIteratorContext : public DynamicContext { +public: + explicit DestructuringIteratorContext(PandaGen *pg, const DestructuringIterator &iterator); + NO_COPY_SEMANTIC(DestructuringIteratorContext); + NO_MOVE_SEMANTIC(DestructuringIteratorContext); + ~DestructuringIteratorContext() override; + + DynamicContextType Type() const override + { + return DynamicContextType::ITERATOR; + } + + const DestructuringIterator &GetIterator() const + { + return iterator_; + } + + bool HasTryCatch() const override + { + return true; + } + + void AbortContext(ControlFlowChange cfc, const util::StringView &targetLabel) override; + +private: + const DestructuringIterator &iterator_; + CatchTable *catchTable_; +}; + class TryContext : public DynamicContext { public: explicit TryContext(PandaGen *pg, const ir::TryStatement *tryStmt, bool hasFinalizer = true) diff --git a/es2panda/test/parser/js/Invalid-destructuring-target-arguments-1-expected.txt b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-1-expected.txt new file mode 100644 index 0000000000..490965ebb5 --- /dev/null +++ b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-1-expected.txt @@ -0,0 +1 @@ +SyntaxError: Invalid destructuring assignment target: arguments [Invalid-destructuring-target-arguments-1.js:18:6] diff --git a/es2panda/test/parser/js/Invalid-destructuring-target-arguments-1.js b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-1.js new file mode 100644 index 0000000000..7442bc913a --- /dev/null +++ b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-1.js @@ -0,0 +1,18 @@ +/* + * 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. + */ + +"use strict"; + +let [arguments] = [1, 2]; \ No newline at end of file diff --git a/es2panda/test/parser/js/Invalid-destructuring-target-arguments-2-expected.txt b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-2-expected.txt new file mode 100644 index 0000000000..94a6d76f86 --- /dev/null +++ b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-2-expected.txt @@ -0,0 +1 @@ +SyntaxError: Invalid destructuring assignment target: arguments [Invalid-destructuring-target-arguments-2.js:18:7] diff --git a/es2panda/test/parser/js/Invalid-destructuring-target-arguments-2.js b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-2.js new file mode 100644 index 0000000000..4890c20e88 --- /dev/null +++ b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-2.js @@ -0,0 +1,18 @@ +/* + * 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. + */ + +"use strict"; + +for ([arguments] of [1, 2, 3]) {} \ No newline at end of file diff --git a/es2panda/test/parser/js/Invalid-destructuring-target-arguments-3-expected.txt b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-3-expected.txt new file mode 100644 index 0000000000..673a7d696a --- /dev/null +++ b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-3-expected.txt @@ -0,0 +1 @@ +SyntaxError: Invalid destructuring assignment target: arguments [Invalid-destructuring-target-arguments-3.js:18:2] diff --git a/es2panda/test/parser/js/Invalid-destructuring-target-arguments-3.js b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-3.js new file mode 100644 index 0000000000..8cd3897ace --- /dev/null +++ b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-3.js @@ -0,0 +1,18 @@ +/* + * 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. + */ + +"use strict"; + +[arguments] = [1, 2]; \ No newline at end of file diff --git a/es2panda/test/parser/js/Invalid-destructuring-target-arguments-4-expected.txt b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-4-expected.txt new file mode 100644 index 0000000000..ddba310fac --- /dev/null +++ b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-4-expected.txt @@ -0,0 +1 @@ +SyntaxError: Invalid destructuring assignment target: arguments [Invalid-destructuring-target-arguments-4.js:18:11] diff --git a/es2panda/test/parser/js/Invalid-destructuring-target-arguments-4.js b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-4.js new file mode 100644 index 0000000000..b3c9bfd257 --- /dev/null +++ b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-4.js @@ -0,0 +1,18 @@ +/* + * 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. + */ + +"use strict"; + +for (let [arguments] of [1, 2]) {} \ No newline at end of file diff --git a/es2panda/test/parser/js/Invalid-destructuring-target-arguments-5-expected.txt b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-5-expected.txt new file mode 100644 index 0000000000..5f9fee3417 --- /dev/null +++ b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-5-expected.txt @@ -0,0 +1 @@ +SyntaxError: Invalid destructuring assignment target: arguments [Invalid-destructuring-target-arguments-5.js:18:11] diff --git a/es2panda/test/parser/js/Invalid-destructuring-target-arguments-5.js b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-5.js new file mode 100644 index 0000000000..f7ba3db2ff --- /dev/null +++ b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-5.js @@ -0,0 +1,18 @@ +/* + * 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. + */ + +"use strict"; + +let {"a": arguments} = {"a": 1}; \ No newline at end of file diff --git a/es2panda/test/parser/js/Invalid-destructuring-target-arguments-6-expected.txt b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-6-expected.txt new file mode 100644 index 0000000000..727df18390 --- /dev/null +++ b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-6-expected.txt @@ -0,0 +1 @@ +SyntaxError: Invalid destructuring assignment target: arguments [Invalid-destructuring-target-arguments-6.js:18:8] diff --git a/es2panda/test/parser/js/Invalid-destructuring-target-arguments-6.js b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-6.js new file mode 100644 index 0000000000..d5e590e9d1 --- /dev/null +++ b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-6.js @@ -0,0 +1,18 @@ +/* + * 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. + */ + +"use strict"; + +({"a": arguments} = {"a": 1}); \ No newline at end of file diff --git a/es2panda/test/parser/js/Invalid-destructuring-target-arguments-7-expected.txt b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-7-expected.txt new file mode 100644 index 0000000000..dc0d0bdee3 --- /dev/null +++ b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-7-expected.txt @@ -0,0 +1 @@ +SyntaxError: Invalid destructuring assignment target: arguments [Invalid-destructuring-target-arguments-7.js:18:16] diff --git a/es2panda/test/parser/js/Invalid-destructuring-target-arguments-7.js b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-7.js new file mode 100644 index 0000000000..e22e457a06 --- /dev/null +++ b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-7.js @@ -0,0 +1,18 @@ +/* + * 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. + */ + +"use strict"; + +for (let {"a": arguments} of {"a": 1}) {} \ No newline at end of file diff --git a/es2panda/test/parser/js/Invalid-destructuring-target-arguments-8-expected.txt b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-8-expected.txt new file mode 100644 index 0000000000..f638d1ea75 --- /dev/null +++ b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-8-expected.txt @@ -0,0 +1 @@ +SyntaxError: Invalid destructuring assignment target: arguments [Invalid-destructuring-target-arguments-8.js:18:12] diff --git a/es2panda/test/parser/js/Invalid-destructuring-target-arguments-8.js b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-8.js new file mode 100644 index 0000000000..e04f6803c5 --- /dev/null +++ b/es2panda/test/parser/js/Invalid-destructuring-target-arguments-8.js @@ -0,0 +1,18 @@ +/* + * 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. + */ + +"use strict"; + +for ({"a": arguments} of {"a": 1}) {} \ No newline at end of file diff --git a/es2panda/util/helpers.cpp b/es2panda/util/helpers.cpp index afe9ed62ab..72be34a1e8 100644 --- a/es2panda/util/helpers.cpp +++ b/es2panda/util/helpers.cpp @@ -413,6 +413,30 @@ std::tuple Helpers::ParamName(ArenaAllocator *allocator, return {Helpers::ToStringView(allocator, index), true}; } +bool Helpers::IsChild(const ir::AstNode *parent, const ir::AstNode *child) +{ + while (child) { + if (child == parent) { + return true; + } + + child = child->Parent(); + } + + return false; +} + +bool Helpers::IsObjectPropertyValue(const ir::ObjectExpression *object, const ir::AstNode *ident) +{ + for (const auto *prop : object->Properties()) { + if (prop->AsProperty()->Value() == ident) { + return true; + } + } + + return false; +} + bool Helpers::OptimizeProgram(panda::pandasm::Program * prog, es2panda::CompilerOptions *options) { std::map stat; diff --git a/es2panda/util/helpers.h b/es2panda/util/helpers.h index 35144bce15..eab6632661 100644 --- a/es2panda/util/helpers.h +++ b/es2panda/util/helpers.h @@ -30,6 +30,7 @@ class ClassDefinition; class ClassProperty; class Identifier; class AstNode; +class ObjectExpression; } // namespace panda::es2panda::ir namespace panda::es2panda { @@ -74,6 +75,8 @@ public: static util::StringView FunctionName(const ir::ScriptFunction *func); static std::tuple ParamName(ArenaAllocator *allocator, const ir::AstNode *param, uint32_t index); + static bool IsChild(const ir::AstNode *parent, const ir::AstNode *child); + static bool IsObjectPropertyValue(const ir::ObjectExpression *object, const ir::AstNode *ident); static bool OptimizeProgram(panda::pandasm::Program *prog, es2panda::CompilerOptions *options); template diff --git a/test262/es2abc_skip_tests.json b/test262/es2abc_skip_tests.json index 93050ec329..ece6ce7cf7 100644 --- a/test262/es2abc_skip_tests.json +++ b/test262/es2abc_skip_tests.json @@ -109,60 +109,6 @@ "built-ins/Function/prototype/toString/unicode.js" ] }, - { - "reason" : "For-await-of failed cases due to parser", - "files" : [ - "language/statements/for-await-of/async-func-decl-dstr-array-elem-target-simple-strict.js", - "language/statements/for-await-of/async-func-decl-dstr-array-rest-after-element.js", - "language/statements/for-await-of/async-func-decl-dstr-array-rest-after-elision.js", - "language/statements/for-await-of/async-func-decl-dstr-array-rest-elision.js", - "language/statements/for-await-of/async-func-decl-dstr-array-rest-iteration.js", - "language/statements/for-await-of/async-func-decl-dstr-obj-rest-descriptors.js", - "language/statements/for-await-of/async-func-decl-dstr-obj-rest-empty-obj.js", - "language/statements/for-await-of/async-func-decl-dstr-obj-rest-getter.js", - "language/statements/for-await-of/async-func-decl-dstr-obj-rest-same-name.js", - "language/statements/for-await-of/async-func-decl-dstr-obj-rest-skip-non-enumerable.js", - "language/statements/for-await-of/async-func-decl-dstr-obj-rest-valid-object.js", - "language/statements/for-await-of/async-gen-decl-dstr-array-elem-trlg-iter-rest-nrml-close-skip.js", - "language/statements/for-await-of/async-gen-decl-dstr-array-rest-after-element.js", - "language/statements/for-await-of/async-gen-decl-dstr-array-rest-after-elision.js", - "language/statements/for-await-of/async-gen-decl-dstr-array-rest-elision.js", - "language/statements/for-await-of/async-gen-decl-dstr-array-rest-iter-nrml-close-skip.js", - "language/statements/for-await-of/async-gen-decl-dstr-array-rest-iteration.js", - "language/statements/for-await-of/async-gen-decl-dstr-obj-rest-descriptors.js", - "language/statements/for-await-of/async-gen-decl-dstr-obj-rest-empty-obj.js", - "language/statements/for-await-of/async-gen-decl-dstr-obj-rest-getter.js", - "language/statements/for-await-of/async-gen-decl-dstr-obj-rest-same-name.js", - "language/statements/for-await-of/async-gen-decl-dstr-obj-rest-skip-non-enumerable.js", - "language/statements/for-await-of/async-gen-decl-dstr-obj-rest-valid-object.js" - ] - }, - { - "reason" : "For-await-of failed cases due to destructuring", - "files" : [ - "language/statements/for-await-of/async-gen-decl-dstr-obj-rest-to-property.js", - "language/statements/for-await-of/async-gen-decl-dstr-obj-rest-to-property-with-setter.js", - "language/statements/for-await-of/async-gen-decl-dstr-obj-rest-symbol-val.js", - "language/statements/for-await-of/async-gen-decl-dstr-obj-rest-str-val.js", - "language/statements/for-await-of/async-gen-decl-dstr-obj-rest-number.js", - "language/statements/for-await-of/async-gen-decl-dstr-array-elem-put-const.js", - "language/statements/for-await-of/async-gen-decl-dstr-array-elem-iter-rtrn-close-null.js", - "language/statements/for-await-of/async-func-decl-dstr-obj-rest-to-property.js", - "language/statements/for-await-of/async-func-decl-dstr-obj-rest-to-property-with-setter.js", - "language/statements/for-await-of/async-func-decl-dstr-obj-rest-symbol-val.js", - "language/statements/for-await-of/async-func-decl-dstr-obj-rest-str-val.js", - "language/statements/for-await-of/async-func-decl-dstr-obj-rest-number.js", - "language/statements/for-await-of/async-func-decl-dstr-array-elem-put-const.js" - ] - }, - { - "reason" : "Special for-await-of cases that failed", - "files" : [ - "language/statements/for-await-of/ticks-with-sync-iter-resolved-promise-and-constructor-lookup.js", - "language/statements/for-await-of/ticks-with-async-iter-resolved-promise-and-constructor-lookup-two.js", - "language/statements/for-await-of/async-from-sync-iterator-continuation-abrupt-completion-get-constructor.js" - ] - }, { "reason" : "parameter with default initialization of itself", "files" : [ -- Gitee