• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024 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/intrinsics_api_impl.h"
17 #include "plugins/ets/runtime/interop_js/js_remote_promise_resolver.h"
18 
19 namespace ark::ets::interop::js {
20 
JsRemotePromiseResolver(napi_deferred deferred)21 JsRemotePromiseResolver::JsRemotePromiseResolver(napi_deferred deferred) : deferred_(deferred)
22 {
23     [[maybe_unused]] auto *coro = EtsCoroutine::GetCurrent();
24     ASSERT(coro == coro->GetPandaVM()->GetCoroutineManager()->GetMainThread());
25     // post async call to event loop
26     auto *vm = coro->GetPandaVM();
27     ASSERT(vm != nullptr);
28     poster_ = vm->CreateCallbackPoster();
29     ASSERT(poster_ != nullptr);
30 }
31 
ResolveViaCallback(EtsObject * resolveValue,Action action)32 void JsRemotePromiseResolver::ResolveViaCallback(EtsObject *resolveValue, Action action)
33 {
34     poster_->Post(ResolveRemotePromise, deferred_, resolveValue, action);
35 }
36 
ResolveInPlace(EtsObject * resolveValue,Action action)37 void JsRemotePromiseResolver::ResolveInPlace(EtsObject *resolveValue, Action action)
38 {
39     [[maybe_unused]] auto *coro = EtsCoroutine::GetCurrent();
40     ASSERT(coro == coro->GetPandaVM()->GetCoroutineManager()->GetMainThread());
41     // running in place
42     ResolveRemotePromise(deferred_, resolveValue, action);
43 }
44 
45 /*static*/
ResolveRemotePromise(napi_deferred deferred,EtsObject * resolveValue,Action action)46 void JsRemotePromiseResolver::ResolveRemotePromise(napi_deferred deferred, EtsObject *resolveValue, Action action)
47 {
48     napi_env env = InteropCtx::Current()->GetJsEnvForEventLoopCallbacks();
49     INTEROP_CODE_SCOPE_JS(EtsCoroutine::GetCurrent(), env);
50     switch (action) {
51         case Action::RESOLVE: {
52             PromiseInteropResolve(resolveValue, reinterpret_cast<EtsLong>(deferred));
53             break;
54         }
55         case Action::REJECT: {
56             PromiseInteropReject(resolveValue, reinterpret_cast<EtsLong>(deferred));
57             break;
58         }
59         default: {
60             UNREACHABLE();
61             break;
62         }
63     }
64 }
65 
66 }  // namespace ark::ets::interop::js
67