1 /** 2 * Copyright (c) 2023-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_CODE_SCOPES_H 17 #define PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_CODE_SCOPES_H 18 19 #include "plugins/ets/runtime/interop_js/interop_context.h" 20 21 namespace ark::ets::interop::js { 22 23 template <bool IN_ETS_MANAGED = false> 24 class InteropCodeScope { 25 public: coro_(coro)26 explicit InteropCodeScope(EtsCoroutine *coro, char const *descr = nullptr) : coro_(coro) 27 { 28 ASSERT(coro_ == ManagedThread::GetCurrent()); 29 auto ctx = InteropCtx::Current(coro_); 30 31 if constexpr (IN_ETS_MANAGED) { 32 ctx->UpdateInteropStackInfoIfNeeded(); 33 } 34 ctx->CallStack().AllocRecord(coro_->GetCurrentFrame(), descr); 35 } 36 ~InteropCodeScope()37 ~InteropCodeScope() 38 { 39 auto ctx = InteropCtx::Current(EtsCoroutine::CastFromThread(coro_)); 40 41 ctx->CallStack().PopRecord(); 42 } 43 44 private: 45 EtsCoroutine *coro_; 46 47 NO_COPY_SEMANTIC(InteropCodeScope); 48 NO_MOVE_SEMANTIC(InteropCodeScope); 49 }; 50 51 using InteropCodeScopeETS = InteropCodeScope<true>; 52 53 class InteropCodeScopeJS { 54 public: codeScope_(coro,descr)55 explicit InteropCodeScopeJS(EtsCoroutine *coro, char const *descr = nullptr) : codeScope_(coro, descr) {} 56 ~InteropCodeScopeJS()57 ~InteropCodeScopeJS() 58 { 59 auto ctx = InteropCtx::Current(); 60 ctx->UpdateInteropStackInfoIfNeeded(); 61 } 62 63 private: 64 NO_COPY_SEMANTIC(InteropCodeScopeJS); 65 NO_MOVE_SEMANTIC(InteropCodeScopeJS); 66 67 InteropCodeScope<false> codeScope_; 68 }; 69 70 // CC-OFFNXT(G.PRE.02-CPP) for readability and ease of use 71 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 72 #define INTEROP_CODE_SCOPE_JS(coro) InteropCodeScopeJS codeScope(coro, __PRETTY_FUNCTION__) 73 // CC-OFFNXT(G.PRE.02-CPP) for readability and ease of use 74 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 75 #define INTEROP_CODE_SCOPE_ETS(coro) InteropCodeScopeETS codeScope(coro, __PRETTY_FUNCTION__) 76 77 } // namespace ark::ets::interop::js 78 79 #endif // !PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_CODE_SCOPES_H 80