1 /**
2 * Copyright (c) 2023-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/ets_proxy/ets_proxy.h"
17
18 #include "plugins/ets/runtime/ets_utils.h"
19 #include "plugins/ets/runtime/interop_js/ets_proxy/ets_method_set.h"
20 #include "plugins/ets/runtime/interop_js/ets_proxy/ets_class_wrapper.h"
21 #include "plugins/ets/runtime/interop_js/ets_proxy/ets_method_wrapper.h"
22 #include "plugins/ets/runtime/interop_js/interop_context.h"
23 #include "plugins/ets/runtime/interop_js/code_scopes.h"
24
25 namespace ark::ets::interop::js::ets_proxy {
26
GetETSFunction(napi_env env,std::string_view packageName,std::string_view methodName)27 napi_value GetETSFunction(napi_env env, std::string_view packageName, std::string_view methodName)
28 {
29 EtsCoroutine *coro = EtsCoroutine::GetCurrent();
30 INTEROP_CODE_SCOPE_JS(coro, env);
31
32 std::ostringstream classDescriptorBuilder;
33 classDescriptorBuilder << "L" << packageName << (packageName.empty() ? "ETSGLOBAL;" : "/ETSGLOBAL;");
34 std::string classDescriptor = classDescriptorBuilder.str();
35
36 if (IsEtsGlobalClassName(packageName.data())) {
37 // Old-style value for legacy code
38 // NOTE remove this check after all tests fixed
39 classDescriptor = packageName;
40 }
41
42 napi_value jsClass = GetETSClass(env, classDescriptor);
43 ASSERT(GetValueType(env, jsClass) == napi_function);
44
45 napi_value jsMethod;
46 const napi_status resolveStatus = napi_get_named_property(env, jsClass, methodName.data(), &jsMethod);
47 if (UNLIKELY(napi_ok != resolveStatus || GetValueType(env, jsMethod) != napi_function)) {
48 InteropCtx::ThrowJSError(env, "GetETSFunction: class " + std::string(classDescriptor) + " doesn't contain " +
49 std::string(methodName) + " method");
50 return nullptr;
51 }
52
53 NAPI_CHECK_FATAL(NapiObjectSeal(env, jsMethod));
54 return jsMethod;
55 }
56
GetETSClass(napi_env env,std::string_view classDescriptor)57 napi_value GetETSClass(napi_env env, std::string_view classDescriptor)
58 {
59 EtsCoroutine *coro = EtsCoroutine::GetCurrent();
60 InteropCtx *ctx = InteropCtx::Current(coro);
61 INTEROP_CODE_SCOPE_JS(coro, env);
62
63 EtsClass *etsKlass = coro->GetPandaVM()->GetClassLinker()->GetClass(classDescriptor.data());
64 if (UNLIKELY(etsKlass == nullptr)) {
65 InteropCtx::ThrowJSError(env, "GetETSClass: unresolved klass " + std::string(classDescriptor));
66 return nullptr;
67 }
68
69 EtsClassWrapper *etsClassWrapper = EtsClassWrapper::Get(ctx, etsKlass);
70 if (UNLIKELY(etsClassWrapper == nullptr)) {
71 return nullptr;
72 }
73
74 return etsClassWrapper->GetJsCtor(env);
75 }
76
77 } // namespace ark::ets::interop::js::ets_proxy
78