1 /**
2 * Copyright (c) 2021-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/js_refconvert.h"
17
18 #include "plugins/ets/runtime/interop_js/ets_proxy/ets_class_wrapper.h"
19 #include "plugins/ets/runtime/interop_js/interop_context.h"
20 #include "plugins/ets/runtime/interop_js/js_refconvert_array.h"
21 #include "plugins/ets/runtime/interop_js/js_refconvert_function.h"
22 #include "plugins/ets/runtime/interop_js/js_refconvert_record.h"
23
24 namespace ark::ets::interop::js {
25
IsFunctionClass(InteropCtx * ctx,Class * klass)26 static bool IsFunctionClass(InteropCtx *ctx, Class *klass)
27 {
28 if (panda_file_items::class_descriptors::FUNCTION.compare(utf::Mutf8AsCString(klass->GetDescriptor())) == 0) {
29 return true;
30 }
31 if (ctx->IsFunctionalInterface(klass)) {
32 return true;
33 }
34 for (auto *itf : klass->GetInterfaces()) {
35 if (ctx->IsFunctionalInterface(itf)) {
36 return true;
37 }
38 }
39 return false;
40 }
41
IsRecord(Class * klass)42 static bool IsRecord(Class *klass)
43 {
44 return PlatformTypes()->escompatRecord->GetRuntimeClass()->IsAssignableFrom(klass);
45 }
46
JSRefConvertCreateImpl(InteropCtx * ctx,Class * klass)47 static std::unique_ptr<JSRefConvert> JSRefConvertCreateImpl(InteropCtx *ctx, Class *klass)
48 {
49 INTEROP_FATAL_IF(klass->IsClassClass());
50
51 if (klass->IsArrayClass()) {
52 auto type = klass->GetComponentType()->GetType().GetId();
53 if (type != panda_file::Type::TypeId::REFERENCE) {
54 ctx->Fatal(std::string("Unhandled primitive array: ") + utf::Mutf8AsCString(klass->GetDescriptor()));
55 }
56 return std::make_unique<JSRefConvertReftypeArray>(klass);
57 }
58
59 if (js_proxy::JSProxy::IsProxyClass(klass)) {
60 return ets_proxy::EtsClassWrapper::CreateJSRefConvertJSProxy(ctx, klass);
61 }
62
63 if (IsFunctionClass(ctx, klass)) {
64 return std::make_unique<JSRefConvertFunction>(klass);
65 }
66
67 if (IsRecord(klass)) {
68 return std::make_unique<JSRefConvertRecord>(ctx);
69 }
70
71 if (klass->IsInterface()) {
72 return ets_proxy::EtsClassWrapper::CreateJSRefConvertEtsInterface(ctx, klass);
73 }
74
75 return ets_proxy::EtsClassWrapper::CreateJSRefConvertEtsProxy(ctx, klass);
76 }
77
78 template <bool ALLOW_INIT>
JSRefConvertCreate(InteropCtx * ctx,Class * klass)79 JSRefConvert *JSRefConvertCreate(InteropCtx *ctx, Class *klass)
80 {
81 if (!CheckClassInitialized<ALLOW_INIT>(klass)) {
82 ASSERT(EtsCoroutine::GetCurrent()->HasPendingException());
83 return nullptr;
84 }
85 auto conv = JSRefConvertCreateImpl(ctx, klass);
86 if (UNLIKELY(conv == nullptr)) {
87 ctx->ThrowETSError(EtsCoroutine::GetCurrent(), std::string("Seamless conversion for class ") +
88 utf::Mutf8AsCString(klass->GetDescriptor()) +
89 std::string(" is not supported"));
90 return nullptr;
91 }
92 return ctx->GetRefConvertCache()->Insert(klass, std::move(conv));
93 }
94
95 template JSRefConvert *JSRefConvertCreate<false>(InteropCtx *ctx, Class *klass);
96 template JSRefConvert *JSRefConvertCreate<true>(InteropCtx *ctx, Class *klass);
97
98 } // namespace ark::ets::interop::js
99