From 5bb6656b683e5886ec5631aa6026e24e3f834133 Mon Sep 17 00:00:00 2001 From: ctw-ian Date: Thu, 9 Feb 2023 21:35:00 +0800 Subject: [PATCH] fixed bef14e0 from https://gitee.com/ctw-ian/ark_ts2abc/pulls/839 Support async function in taskpool Issue:I6EZXE Signed-off-by: ctw-ian Change-Id: I158d70e180c94ce91724164c9457f4e5fe203965 --- es2panda/compiler/core/function.cpp | 2 +- es2panda/compiler/core/pandagen.cpp | 7 +++++++ es2panda/compiler/core/pandagen.h | 6 ++++++ .../function/asyncFunctionBuilder.cpp | 2 ++ .../compiler/function/functionBuilder.cpp | 5 +++++ es2panda/ir/base/scriptFunction.h | 2 +- ...function-decl-explicit-return-expected.txt | 2 ++ ...ent-async-function-decl-explicit-return.js | 10 ++++++++++ ...function-decl-implicit-return-expected.txt | 1 + ...ent-async-function-decl-implicit-return.js | 9 +++++++++ ...function-decl-explicit-return-expected.txt | 2 ++ ...oncurrent-function-decl-explicit-return.js | 9 +++++++++ ...function-decl-implicit-return-expected.txt | 1 + ...oncurrent-function-decl-implicit-return.js | 8 ++++++++ ...lid-concurrent-async-function-expected.txt | 2 -- .../invalid-concurrent-async-function.js | 20 ------------------- ts2panda/templates/irnodes.ts.erb | 3 +++ 17 files changed, 67 insertions(+), 24 deletions(-) create mode 100644 es2panda/test/compiler/js/concurrent/concurrent-async-function-decl-explicit-return-expected.txt create mode 100644 es2panda/test/compiler/js/concurrent/concurrent-async-function-decl-explicit-return.js create mode 100644 es2panda/test/compiler/js/concurrent/concurrent-async-function-decl-implicit-return-expected.txt create mode 100644 es2panda/test/compiler/js/concurrent/concurrent-async-function-decl-implicit-return.js create mode 100644 es2panda/test/compiler/js/concurrent/concurrent-function-decl-explicit-return-expected.txt create mode 100644 es2panda/test/compiler/js/concurrent/concurrent-function-decl-explicit-return.js create mode 100644 es2panda/test/compiler/js/concurrent/concurrent-function-decl-implicit-return-expected.txt create mode 100644 es2panda/test/compiler/js/concurrent/concurrent-function-decl-implicit-return.js delete mode 100644 es2panda/test/parser/concurrent/invalid-concurrent-async-function-expected.txt delete mode 100644 es2panda/test/parser/concurrent/invalid-concurrent-async-function.js diff --git a/es2panda/compiler/core/function.cpp b/es2panda/compiler/core/function.cpp index a284a72bc6..d25dfb8a2e 100644 --- a/es2panda/compiler/core/function.cpp +++ b/es2panda/compiler/core/function.cpp @@ -203,8 +203,8 @@ static void CompileFunctionOrProgram(PandaGen *pg) void Function::Compile(PandaGen *pg) { - CompileFunctionOrProgram(pg); pg->SetFunctionKind(); + CompileFunctionOrProgram(pg); pg->SetSourceLocationFlag(lexer::SourceLocationFlag::INVALID_SOURCE_LOCATION); pg->CopyFunctionArguments(pg->RootNode()); pg->InitializeLexEnv(pg->RootNode()); diff --git a/es2panda/compiler/core/pandagen.cpp b/es2panda/compiler/core/pandagen.cpp index babd7f15dd..cfdcc1bf6e 100644 --- a/es2panda/compiler/core/pandagen.cpp +++ b/es2panda/compiler/core/pandagen.cpp @@ -1271,6 +1271,13 @@ void PandaGen::SuperCallSpread(const ir::AstNode *node, VReg vs) ra_.Emit(node, 0, vs); } +void PandaGen::NotifyConcurrentResult(const ir::AstNode *node) +{ + if (IsConcurrent()) { + ra_.Emit(node); + } +} + void PandaGen::NewObject(const ir::AstNode *node, VReg startReg, size_t argCount) { if (argCount <= util::Helpers::MAX_INT8) { diff --git a/es2panda/compiler/core/pandagen.h b/es2panda/compiler/core/pandagen.h index 4b17ee205c..344ef308e0 100644 --- a/es2panda/compiler/core/pandagen.h +++ b/es2panda/compiler/core/pandagen.h @@ -225,6 +225,11 @@ public: return funcKind_; } + bool IsConcurrent() const + { + return funcKind_ == panda::panda_file::FunctionKind::CONCURRENT_FUNCTION; + } + void SetFunctionKind(); bool IsDebug() const; @@ -339,6 +344,7 @@ public: void CallSpread(const ir::AstNode *node, VReg func, VReg thisReg, VReg args); void SuperCall(const ir::AstNode *node, VReg startReg, size_t argCount); void SuperCallSpread(const ir::AstNode *node, VReg vs); + void NotifyConcurrentResult(const ir::AstNode *node); void NewObject(const ir::AstNode *node, VReg startReg, size_t argCount); void DefineFunction(const ir::AstNode *node, const ir::ScriptFunction *realNode, const util::StringView &name); diff --git a/es2panda/compiler/function/asyncFunctionBuilder.cpp b/es2panda/compiler/function/asyncFunctionBuilder.cpp index da2a187e22..c02a8cfc13 100644 --- a/es2panda/compiler/function/asyncFunctionBuilder.cpp +++ b/es2panda/compiler/function/asyncFunctionBuilder.cpp @@ -24,6 +24,7 @@ namespace panda::es2panda::compiler { void AsyncFunctionBuilder::DirectReturn(const ir::AstNode *node) const { pg_->AsyncFunctionResolve(node, funcObj_); // retVal is in acc + pg_->NotifyConcurrentResult(node); pg_->EmitReturn(node); } @@ -50,6 +51,7 @@ void AsyncFunctionBuilder::CleanUp(const ir::ScriptFunction *node) const VReg exception = pg_->AllocReg(); pg_->StoreAccumulator(node, exception); pg_->AsyncFunctionReject(node, funcObj_); + pg_->NotifyConcurrentResult(node); pg_->EmitReturn(node); pg_->SetLabel(node, labelSet.CatchEnd()); } diff --git a/es2panda/compiler/function/functionBuilder.cpp b/es2panda/compiler/function/functionBuilder.cpp index 5a808fded2..eb79e61556 100644 --- a/es2panda/compiler/function/functionBuilder.cpp +++ b/es2panda/compiler/function/functionBuilder.cpp @@ -36,6 +36,7 @@ IteratorType FunctionBuilder::GeneratorKind() const void FunctionBuilder::DirectReturn(const ir::AstNode *node) const { + pg_->NotifyConcurrentResult(node); pg_->EmitReturn(node); } @@ -45,15 +46,19 @@ void FunctionBuilder::ImplicitReturn(const ir::AstNode *node) const if (!rootNode->IsScriptFunction() || !rootNode->AsScriptFunction()->IsConstructor()) { if (pg_->isDebuggerEvaluateExpressionMode()) { + pg_->NotifyConcurrentResult(node); pg_->EmitReturn(node); return; } + pg_->LoadConst(node, Constant::JS_UNDEFINED); + pg_->NotifyConcurrentResult(node); pg_->EmitReturnUndefined(node); return; } pg_->GetThis(rootNode); pg_->ThrowIfSuperNotCorrectCall(rootNode, 0); + pg_->NotifyConcurrentResult(node); pg_->EmitReturn(node); } diff --git a/es2panda/ir/base/scriptFunction.h b/es2panda/ir/base/scriptFunction.h index 0e1514a8a0..56e4c910f5 100644 --- a/es2panda/ir/base/scriptFunction.h +++ b/es2panda/ir/base/scriptFunction.h @@ -174,7 +174,7 @@ public: bool CanBeConcurrent() const { - return !(IsGenerator() || IsAsync() || IsArrow() || IsConstructor() || IsMethod()); + return !(IsGenerator() || IsArrow() || IsConstructor() || IsMethod()); } void Iterate(const NodeTraverser &cb) const override; diff --git a/es2panda/test/compiler/js/concurrent/concurrent-async-function-decl-explicit-return-expected.txt b/es2panda/test/compiler/js/concurrent/concurrent-async-function-decl-explicit-return-expected.txt new file mode 100644 index 0000000000..9689045438 --- /dev/null +++ b/es2panda/test/compiler/js/concurrent/concurrent-async-function-decl-explicit-return-expected.txt @@ -0,0 +1,2 @@ +[object Promise] +111 diff --git a/es2panda/test/compiler/js/concurrent/concurrent-async-function-decl-explicit-return.js b/es2panda/test/compiler/js/concurrent/concurrent-async-function-decl-explicit-return.js new file mode 100644 index 0000000000..e82948db0c --- /dev/null +++ b/es2panda/test/compiler/js/concurrent/concurrent-async-function-decl-explicit-return.js @@ -0,0 +1,10 @@ +"use strict"; + +async function a() { + "use concurrent"; + await 1; + print(111); + return 1; +} + +print(a()); diff --git a/es2panda/test/compiler/js/concurrent/concurrent-async-function-decl-implicit-return-expected.txt b/es2panda/test/compiler/js/concurrent/concurrent-async-function-decl-implicit-return-expected.txt new file mode 100644 index 0000000000..58c9bdf9d0 --- /dev/null +++ b/es2panda/test/compiler/js/concurrent/concurrent-async-function-decl-implicit-return-expected.txt @@ -0,0 +1 @@ +111 diff --git a/es2panda/test/compiler/js/concurrent/concurrent-async-function-decl-implicit-return.js b/es2panda/test/compiler/js/concurrent/concurrent-async-function-decl-implicit-return.js new file mode 100644 index 0000000000..de77a9453f --- /dev/null +++ b/es2panda/test/compiler/js/concurrent/concurrent-async-function-decl-implicit-return.js @@ -0,0 +1,9 @@ +"use strict"; + +async function a() { + "use concurrent"; + await 1; + print(111); +} + +a(); diff --git a/es2panda/test/compiler/js/concurrent/concurrent-function-decl-explicit-return-expected.txt b/es2panda/test/compiler/js/concurrent/concurrent-function-decl-explicit-return-expected.txt new file mode 100644 index 0000000000..9737894eb6 --- /dev/null +++ b/es2panda/test/compiler/js/concurrent/concurrent-function-decl-explicit-return-expected.txt @@ -0,0 +1,2 @@ +111 +1 diff --git a/es2panda/test/compiler/js/concurrent/concurrent-function-decl-explicit-return.js b/es2panda/test/compiler/js/concurrent/concurrent-function-decl-explicit-return.js new file mode 100644 index 0000000000..75e95b9b90 --- /dev/null +++ b/es2panda/test/compiler/js/concurrent/concurrent-function-decl-explicit-return.js @@ -0,0 +1,9 @@ +"use strict"; + +function a() { + "use concurrent"; + print(111); + return 1; +} + +print(a()); diff --git a/es2panda/test/compiler/js/concurrent/concurrent-function-decl-implicit-return-expected.txt b/es2panda/test/compiler/js/concurrent/concurrent-function-decl-implicit-return-expected.txt new file mode 100644 index 0000000000..58c9bdf9d0 --- /dev/null +++ b/es2panda/test/compiler/js/concurrent/concurrent-function-decl-implicit-return-expected.txt @@ -0,0 +1 @@ +111 diff --git a/es2panda/test/compiler/js/concurrent/concurrent-function-decl-implicit-return.js b/es2panda/test/compiler/js/concurrent/concurrent-function-decl-implicit-return.js new file mode 100644 index 0000000000..b9459e4751 --- /dev/null +++ b/es2panda/test/compiler/js/concurrent/concurrent-function-decl-implicit-return.js @@ -0,0 +1,8 @@ +"use strict"; + +function a() { + "use concurrent"; + print(111); +} + +a(); diff --git a/es2panda/test/parser/concurrent/invalid-concurrent-async-function-expected.txt b/es2panda/test/parser/concurrent/invalid-concurrent-async-function-expected.txt deleted file mode 100644 index a53c7f6492..0000000000 --- a/es2panda/test/parser/concurrent/invalid-concurrent-async-function-expected.txt +++ /dev/null @@ -1,2 +0,0 @@ -Error: Concurrent function should only be function declaration [invalid-concurrent-async-function.js:18:4] -the size of programs is expected to be 1, but is 0 diff --git a/es2panda/test/parser/concurrent/invalid-concurrent-async-function.js b/es2panda/test/parser/concurrent/invalid-concurrent-async-function.js deleted file mode 100644 index 45d163165b..0000000000 --- a/es2panda/test/parser/concurrent/invalid-concurrent-async-function.js +++ /dev/null @@ -1,20 +0,0 @@ -/* - * 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. - */ - -"use strict"; - -async function a() { - "use concurrent"; -} \ No newline at end of file diff --git a/ts2panda/templates/irnodes.ts.erb b/ts2panda/templates/irnodes.ts.erb index a083d8b5a3..acb70721c2 100755 --- a/ts2panda/templates/irnodes.ts.erb +++ b/ts2panda/templates/irnodes.ts.erb @@ -75,6 +75,9 @@ import { PandaGen } from "./pandagen" % end % % def is_Call(insn) +% if insn.mnemonic.start_with? "callruntime" +% return false +% end % if insn.mnemonic.start_with? "call" % return true % end -- Gitee