1 /**
2 * Copyright (c) 2023-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 "include/object_header.h"
17 #include "intrinsics.h"
18 #include "libpandabase/utils/logger.h"
19 #include "runtime/handle_scope-inl.h"
20 #include "plugins/ets/runtime/ets_coroutine.h"
21 #include "plugins/ets/runtime/ets_exceptions.h"
22 #include "plugins/ets/runtime/types/ets_method.h"
23 #include "plugins/ets/runtime/ets_class_linker_extension.h"
24 #include "plugins/ets/runtime/types/ets_string.h"
25 #include "plugins/ets/runtime/ets_stubs.h"
26
27 namespace ark::ets::intrinsics {
28
StdCoreRuntimeIsLittleEndianPlatform()29 EtsBoolean StdCoreRuntimeIsLittleEndianPlatform()
30 {
31 ASSERT(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__);
32 return __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
33 }
34
StdCoreRuntimeIsSameReference(EtsObject * source,EtsObject * target)35 uint8_t StdCoreRuntimeIsSameReference(EtsObject *source, EtsObject *target)
36 {
37 return (source == target) ? UINT8_C(1) : UINT8_C(0);
38 }
39
StdCoreRuntimeGetHashCode(EtsObject * source)40 EtsInt StdCoreRuntimeGetHashCode(EtsObject *source)
41 {
42 ASSERT(source != nullptr);
43 return bit_cast<EtsInt>(source->GetHashCode());
44 }
45
ReferenceTypeString(EtsCoroutine * coro,EtsObject * obj)46 static char const *ReferenceTypeString(EtsCoroutine *coro, EtsObject *obj)
47 {
48 ASSERT(coro != nullptr);
49 if (obj == nullptr) {
50 return "undefined";
51 }
52 ASSERT(coro != nullptr);
53 if (obj == EtsObject::FromCoreType(coro->GetNullValue())) {
54 return "null";
55 }
56 return obj->GetClass()->GetDescriptor();
57 }
58
StdCoreRuntimeFailedTypeCastException(EtsObject * source,EtsString * target)59 ObjectHeader *StdCoreRuntimeFailedTypeCastException(EtsObject *source, EtsString *target)
60 {
61 auto coro = EtsCoroutine::GetCurrent();
62
63 ASSERT(coro != nullptr);
64
65 auto message = PandaString(ReferenceTypeString(coro, source)) + " cannot be cast to " + target->GetMutf8();
66
67 auto *exc =
68 ets::SetupEtsException(coro, panda_file_items::class_descriptors::CLASS_CAST_ERROR.data(), message.data());
69
70 if (LIKELY(exc != nullptr)) {
71 return exc->GetCoreType();
72 }
73
74 ASSERT(coro->HasPendingException());
75
76 return nullptr;
77 }
78
StdCoreRuntimeGetTypeInfo(EtsObject * header)79 EtsClass *StdCoreRuntimeGetTypeInfo([[maybe_unused]] EtsObject *header)
80 {
81 return nullptr;
82 }
83
StdCoreRuntimeAllocSameTypeArray(EtsClass * cls,int32_t length)84 ObjectHeader *StdCoreRuntimeAllocSameTypeArray(EtsClass *cls, int32_t length)
85 {
86 if (UNLIKELY(!cls->IsArrayClass())) {
87 // should not appear for the optimized version of intrinsic, which is always inlined
88 ThrowEtsException(EtsCoroutine::GetCurrent(), panda_file_items::class_descriptors::ERROR,
89 "class is not an array");
90 return nullptr;
91 }
92 return coretypes::Array::Create(cls->GetRuntimeClass(), length);
93 }
94
95 } // namespace ark::ets::intrinsics
96