1 /**
2 * Copyright (c) 2021-2023 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 <node_api.h>
17 #include "runtime/handle_scope-inl.h"
18 #include "runtime/mem/refstorage/reference.h"
19 #include "plugins/ets/runtime/ets_vm.h"
20 #include "plugins/ets/runtime/lambda_utils.h"
21 #include "plugins/ets/runtime/types/ets_method.h"
22 #include "plugins/ets/runtime/interop_js/js_job_queue.h"
23 #include "plugins/ets/runtime/interop_js/interop_common.h"
24 #include "plugins/ets/runtime/interop_js/interop_context.h"
25 #include "plugins/ets/runtime/interop_js/js_value.h"
26 #include "plugins/ets/runtime/types/ets_promise.h"
27 #include "plugins/ets/runtime/ets_handle_scope.h"
28 #include "plugins/ets/runtime/ets_handle.h"
29 #include "plugins/ets/runtime/interop_js/napi_env_scope.h"
30 #include "runtime/coroutines/stackful_coroutine.h"
31 #include "intrinsics.h"
32
33 namespace panda::ets::interop::js {
ThenCallback(napi_env env,napi_callback_info info)34 static napi_value ThenCallback(napi_env env, napi_callback_info info)
35 {
36 EtsCoroutine *coro = EtsCoroutine::GetCurrent();
37 PandaEtsVM *vm = coro->GetPandaVM();
38 auto ctx = InteropCtx::Current(coro);
39 [[maybe_unused]] EtsJSNapiEnvScope napiScope(ctx, env);
40
41 mem::Reference *callbackRef = nullptr;
42 [[maybe_unused]] napi_status status =
43 napi_get_cb_info(env, info, nullptr, nullptr, nullptr, reinterpret_cast<void **>(&callbackRef));
44 ASSERT(status == napi_ok);
45
46 ScopedManagedCodeThread scope(coro);
47 [[maybe_unused]] HandleScope<ObjectHeader *> handleScope(ManagedThread::GetCurrent());
48 VMHandle<EtsObject> callback(coro, vm->GetGlobalObjectStorage()->Get(callbackRef));
49 vm->GetGlobalObjectStorage()->Remove(callbackRef);
50
51 LambdaUtils::InvokeVoid(coro, callback.GetPtr());
52 if (coro->HasPendingException()) {
53 napi_throw_error(env, nullptr, "EtsVM internal error");
54 }
55 napi_value undefined;
56 napi_get_undefined(env, &undefined);
57 return undefined;
58 }
59
AddJob(EtsObject * callback)60 void JsJobQueue::AddJob(EtsObject *callback)
61 {
62 EtsCoroutine *coro = EtsCoroutine::GetCurrent();
63 PandaEtsVM *vm = coro->GetPandaVM();
64 napi_env env = InteropCtx::Current(coro)->GetJSEnv();
65 napi_deferred deferred;
66 napi_value undefined;
67 napi_value jsPromise;
68 napi_value thenFn;
69
70 napi_get_undefined(env, &undefined);
71 napi_status status = napi_create_promise(env, &deferred, &jsPromise);
72 if (status != napi_ok) {
73 InteropCtx::Fatal("Cannot allocate a Promise instance");
74 }
75 status = napi_get_named_property(env, jsPromise, "then", &thenFn);
76 ASSERT(status == napi_ok);
77 (void)status;
78
79 mem::Reference *callbackRef =
80 vm->GetGlobalObjectStorage()->Add(callback->GetCoreType(), mem::Reference::ObjectType::GLOBAL);
81 ASSERT(callbackRef != nullptr);
82
83 napi_value thenCallback;
84 status = napi_create_function(env, nullptr, 0, ThenCallback, callbackRef, &thenCallback);
85 if (status != napi_ok) {
86 InteropCtx::Fatal("Cannot create a function");
87 }
88
89 napi_value thenPromise;
90 status = napi_call_function(env, jsPromise, thenFn, 1, &thenCallback, &thenPromise);
91 ASSERT(status == napi_ok);
92 (void)status;
93
94 napi_resolve_deferred(env, deferred, undefined);
95 }
96
OnJsPromiseResolved(napi_env env,napi_callback_info info)97 static napi_value OnJsPromiseResolved(napi_env env, [[maybe_unused]] napi_callback_info info)
98 {
99 EtsCoroutine *coro = EtsCoroutine::GetCurrent();
100 PandaEtsVM *vm = coro->GetPandaVM();
101 auto ctx = InteropCtx::Current(coro);
102 [[maybe_unused]] EtsJSNapiEnvScope napiScope(ctx, env);
103
104 mem::Reference *promiseRef = nullptr;
105 size_t argc = 1;
106 napi_value value;
107 napi_status status = napi_get_cb_info(env, info, &argc, &value, nullptr, reinterpret_cast<void **>(&promiseRef));
108 if (status != napi_ok) {
109 InteropCtx::Fatal("Cannot call napi_get_cb_info!");
110 }
111
112 ScopedManagedCodeThread scope(coro);
113 EtsHandleScope hScope(coro);
114 EtsHandle<EtsPromise> promiseHandle(coro, EtsPromise::FromCoreType(vm->GetGlobalObjectStorage()->Get(promiseRef)));
115 vm->GetGlobalObjectStorage()->Remove(promiseRef);
116 ASSERT(promiseHandle.GetPtr()->GetEventPtr() != nullptr);
117 promiseHandle.GetPtr()->GetEventPtr()->SetHappened();
118
119 auto jsval = JSValue::Create(coro, ctx, value);
120 panda::ets::intrinsics::EtsPromiseResolve(promiseHandle.GetPtr(), jsval->AsObject());
121
122 vm->GetCoroutineManager()->UnblockWaiters(promiseHandle.GetPtr()->GetEventPtr());
123 vm->GetCoroutineManager()->Schedule();
124
125 napi_value undefined;
126 napi_get_undefined(env, &undefined);
127 return undefined;
128 }
129
CreatePromiseLink(JSValue * jsPromise,EtsPromise * etsPromise)130 void JsJobQueue::CreatePromiseLink(JSValue *jsPromise, EtsPromise *etsPromise)
131 {
132 EtsCoroutine *coro = EtsCoroutine::GetCurrent();
133 PandaEtsVM *vm = coro->GetPandaVM();
134 napi_env env = InteropCtx::Current(coro)->GetJSEnv();
135
136 napi_value thenFn;
137 napi_status status = napi_get_named_property(env, jsPromise->GetNapiValue(env), "then", &thenFn);
138 if (status != napi_ok) {
139 InteropCtx::Fatal("Cannot call then() from a JS promise");
140 }
141
142 mem::Reference *promiseRef = vm->GetGlobalObjectStorage()->Add(etsPromise, mem::Reference::ObjectType::GLOBAL);
143
144 // NOTE(konstanting, #I67QXC): OnJsPromiseRejected
145 napi_value thenCallback;
146 status = napi_create_function(env, nullptr, 0, OnJsPromiseResolved, promiseRef, &thenCallback);
147 if (status != napi_ok) {
148 InteropCtx::Fatal("Cannot create a function");
149 }
150
151 napi_value thenResult;
152 status = napi_call_function(env, jsPromise->GetNapiValue(env), thenFn, 1, &thenCallback, &thenResult);
153 if (status != napi_ok) {
154 InteropCtx::Fatal("Cannot call then() from a JS Promise");
155 }
156 }
157
CreateLink(EtsObject * source,EtsObject * target)158 void JsJobQueue::CreateLink(EtsObject *source, EtsObject *target)
159 {
160 auto addLinkProc = [&]() { CreatePromiseLink(JSValue::FromEtsObject(source), EtsPromise::FromEtsObject(target)); };
161
162 auto *mainT = EtsCoroutine::GetCurrent()->GetPandaVM()->GetCoroutineManager()->GetMainThread();
163 Coroutine *mainCoro = Coroutine::CastFromThread(mainT);
164 if (Coroutine::GetCurrent() != mainCoro) {
165 // NOTE(konstanting, #I67QXC): figure out if we need to ExecuteOnThisContext() for OHOS
166 mainCoro->GetContext<StackfulCoroutineContext>()->ExecuteOnThisContext(
167 &addLinkProc, EtsCoroutine::GetCurrent()->GetContext<StackfulCoroutineContext>());
168 } else {
169 addLinkProc();
170 }
171 }
172 } // namespace panda::ets::interop::js
173