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 "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
StdCoreRuntimeGetPlatformIsLittleEndian()29 EtsBoolean StdCoreRuntimeGetPlatformIsLittleEndian()
30 {
31 ASSERT(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__);
32 return __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
33 }
34
StdCoreRuntimeIsSameReference(ObjectHeader * header,EtsObject * source,EtsObject * target)35 uint8_t StdCoreRuntimeIsSameReference([[maybe_unused]] ObjectHeader *header, EtsObject *source, EtsObject *target)
36 {
37 return (source == target) ? UINT8_C(1) : UINT8_C(0);
38 }
39
StdCoreRuntimeGetHashCode(ObjectHeader * header,EtsObject * source)40 EtsInt StdCoreRuntimeGetHashCode([[maybe_unused]] ObjectHeader *header, 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 if (obj == nullptr) {
49 return "null";
50 }
51 if (obj == EtsObject::FromCoreType(coro->GetUndefinedObject())) {
52 return "undefined";
53 }
54 return obj->GetClass()->GetDescriptor();
55 }
56
StdCoreRuntimeFailedTypeCastException(EtsObject * source,EtsString * target)57 ObjectHeader *StdCoreRuntimeFailedTypeCastException(EtsObject *source, EtsString *target)
58 {
59 auto coro = EtsCoroutine::GetCurrent();
60
61 auto message = PandaString(ReferenceTypeString(coro, source)) + " cannot be cast to " + target->GetMutf8();
62
63 return ets::SetupEtsException(coro, panda_file_items::class_descriptors::CLASS_CAST_ERROR.data(), message.data())
64 ->GetCoreType();
65 }
66
StdCoreRuntimeTypeof(EtsObject * obj)67 EtsString *StdCoreRuntimeTypeof(EtsObject *obj)
68 {
69 return EtsGetTypeof(EtsCoroutine::GetCurrent(), obj);
70 }
71
72 } // namespace ark::ets::intrinsics
73