• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024-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 #include "plugins/ets/runtime/interop_js/stack_info.h"
17 
18 #include "macros.h"
19 #include "plugins/ets/runtime/interop_js/interop_context.h"
20 #include "runtime/coroutines/stackful_coroutine.h"
21 #include "interop_js/napi_impl/napi_impl.h"
22 
23 #include <cstddef>
24 #include <memory>
25 
26 // CC-OFFNXT(G.NAM.03) has to be compatible with JS NAPI struct
27 // NOLINTNEXTLINE(readability-identifier-naming)
28 #ifndef PANDA_JS_ETS_HYBRID_MODE
29 struct NapiStackInfo {
30     size_t stackStart;
31     size_t stackSize;
32 };
33 #endif
34 
35 // NOLINTNEXTLINE(readability-identifier-naming)
36 napi_status __attribute__((weak)) napi_set_stackinfo(napi_env env, NapiStackInfo *info);
37 // NOLINTNEXTLINE(readability-identifier-naming)
38 napi_status __attribute__((weak)) napi_get_stackinfo(napi_env env, NapiStackInfo *result);
39 
40 namespace ark::ets::interop::js {
41 
StackInfoManagerOhos(InteropCtx * ctx,EtsCoroutine * coro)42 StackInfoManagerOhos::StackInfoManagerOhos(InteropCtx *ctx, EtsCoroutine *coro) : StackInfoManagerBase {ctx, coro} {}
43 
44 // NOLINTNEXTLINE(modernize-use-equals-default)
~StackInfoManagerOhos()45 StackInfoManagerOhos::~StackInfoManagerOhos() {}
46 
InitStackInfoIfNeeded()47 void StackInfoManagerOhos::InitStackInfoIfNeeded()
48 {
49     ASSERT(EtsCoroutine::GetCurrent() == mainCoro_);
50     if (LIKELY(!mainStackInfo_)) {
51         mainStackInfo_ = std::make_unique<NapiStackInfo>();
52         auto env = ctx_->GetJSEnv();
53         napi_get_stackinfo(env, mainStackInfo_.get());
54     }
55 }
56 
UpdateStackInfoIfNeeded()57 void StackInfoManagerOhos::UpdateStackInfoIfNeeded()
58 {
59     if (UNLIKELY(!mainStackInfo_)) {
60         return;
61     }
62     auto *coro = EtsCoroutine::GetCurrent();
63     if (coro == mainCoro_) {
64         napi_set_stackinfo(ctx_->GetJSEnv(), mainStackInfo_.get());
65     } else {
66         void *stackAddr = nullptr;
67         size_t stackSize {};
68         size_t guardSize {};
69         ASSERT(coro != nullptr);
70         coro->GetContext<StackfulCoroutineContext>()->RetrieveStackInfo(stackAddr, stackSize, guardSize);
71         NapiStackInfo currentStackinfo {reinterpret_cast<size_t>(stackAddr), stackSize};
72         napi_set_stackinfo(ctx_->GetJSEnv(), &currentStackinfo);
73     }
74 }
75 
76 }  // namespace ark::ets::interop::js
77