1 /**
2 * Copyright (c) 2021-2022 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/interop_context.h"
17 #include "plugins/ets/runtime/interop_js/js_refconvert.h"
18
19 #include "plugins/ets/runtime/interop_js/js_refconvert_array.h"
20
21 namespace panda::ets::interop::js {
22
JSRefConvertCreateImpl(InteropCtx * ctx,Class * klass)23 static std::unique_ptr<JSRefConvert> JSRefConvertCreateImpl(InteropCtx *ctx, Class *klass)
24 {
25 INTEROP_FATAL_IF(klass->IsClassClass());
26
27 if (klass->IsArrayClass()) {
28 auto type = klass->GetComponentType()->GetType().GetId();
29 if (type != panda_file::Type::TypeId::REFERENCE) {
30 ctx->Fatal(std::string("Unhandled primitive array: ") + utf::Mutf8AsCString(klass->GetDescriptor()));
31 }
32 return std::unique_ptr<JSRefConvert>(new JSRefConvertReftypeArray(klass));
33 }
34
35 if (js_proxy::JSProxy::IsProxyClass(klass)) {
36 return ets_proxy::EtsClassWrapper::CreateJSRefConvertJSProxy(ctx, klass);
37 }
38 return ets_proxy::EtsClassWrapper::CreateJSRefConvertEtsProxy(ctx, klass);
39 }
40
41 template <bool ALLOW_INIT>
JSRefConvertCreate(InteropCtx * ctx,Class * klass)42 JSRefConvert *JSRefConvertCreate(InteropCtx *ctx, Class *klass)
43 {
44 if (!CheckClassInitialized<ALLOW_INIT>(klass)) {
45 ASSERT(EtsCoroutine::GetCurrent()->HasPendingException());
46 return nullptr;
47 }
48 auto conv = JSRefConvertCreateImpl(ctx, klass);
49 if (UNLIKELY(conv == nullptr)) {
50 ctx->Fatal(std::string("Can't create convertor for ") + utf::Mutf8AsCString(klass->GetDescriptor()));
51 }
52 return ctx->GetRefConvertCache()->Insert(klass, std::move(conv));
53 }
54
55 template JSRefConvert *JSRefConvertCreate<false>(InteropCtx *ctx, Class *klass);
56 template JSRefConvert *JSRefConvertCreate<true>(InteropCtx *ctx, Class *klass);
57
58 } // namespace panda::ets::interop::js
59