• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 namespace ark::ets::intrinsics {
27 
StdCoreRuntimeGetPlatformIsLittleEndian()28 EtsBoolean StdCoreRuntimeGetPlatformIsLittleEndian()
29 {
30     ASSERT(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__);
31     return __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
32 }
33 
StdCoreRuntimeIsSameReference(ObjectHeader * header,EtsObject * source,EtsObject * target)34 uint8_t StdCoreRuntimeIsSameReference([[maybe_unused]] ObjectHeader *header, EtsObject *source, EtsObject *target)
35 {
36     return (source == target) ? UINT8_C(1) : UINT8_C(0);
37 }
38 
StdCoreRuntimeGetHashCode(ObjectHeader * header,EtsObject * source)39 EtsInt StdCoreRuntimeGetHashCode([[maybe_unused]] ObjectHeader *header, EtsObject *source)
40 {
41     ASSERT(source != nullptr);
42     if (source->IsHashed()) {
43         return source->GetInteropHash();
44     }
45     auto hash = ObjectHeader::GenerateHashCode();
46     source->SetInteropHash(hash);
47     return bit_cast<EtsInt>(hash);
48 }
49 
ReferenceTypeString(EtsCoroutine * coro,EtsObject * obj)50 static char const *ReferenceTypeString(EtsCoroutine *coro, EtsObject *obj)
51 {
52     if (obj == nullptr) {
53         return "null";
54     }
55     if (obj == EtsObject::FromCoreType(coro->GetUndefinedObject())) {
56         return "undefined";
57     }
58     return obj->GetClass()->GetDescriptor();
59 }
60 
StdCoreRuntimeFailedTypeCastException(EtsObject * source,EtsString * target)61 ObjectHeader *StdCoreRuntimeFailedTypeCastException(EtsObject *source, EtsString *target)
62 {
63     auto coro = EtsCoroutine::GetCurrent();
64 
65     auto message = PandaString(ReferenceTypeString(coro, source)) + " cannot be cast to " + target->GetMutf8();
66 
67     return ets::SetupEtsException(coro, panda_file_items::class_descriptors::CLASS_CAST_ERROR.data(), message.data())
68         ->GetCoreType();
69 }
70 
StdCoreRuntimeTypeof(EtsObject * obj)71 EtsString *StdCoreRuntimeTypeof(EtsObject *obj)
72 {
73     if (obj != nullptr) {
74         if (obj->GetClass()->IsUndefined()) {
75             return EtsString::CreateFromMUtf8("undefined");
76         }
77         if (obj->IsStringClass()) {
78             return EtsString::CreateFromMUtf8("string");
79         }
80         if (obj->GetClass()->IsFunctionalClass()) {
81             return EtsString::CreateFromMUtf8("function");
82         }
83         auto ext = static_cast<PandaEtsVM *>(Runtime::GetCurrent()->GetPandaVM())
84                        ->GetClassLinker()
85                        ->GetEtsClassLinkerExtension();
86         if (obj->GetClass()->GetRuntimeClass() == ext->GetBoxBooleanClass()) {
87             return EtsString::CreateFromMUtf8("boolean");
88         }
89         if (obj->GetClass()->GetRuntimeClass() == ext->GetBoxByteClass()) {
90             return EtsString::CreateFromMUtf8("byte");
91         }
92         if (obj->GetClass()->GetRuntimeClass() == ext->GetBoxCharClass()) {
93             return EtsString::CreateFromMUtf8("char");
94         }
95         if (obj->GetClass()->GetRuntimeClass() == ext->GetBoxShortClass()) {
96             return EtsString::CreateFromMUtf8("short");
97         }
98         if (obj->GetClass()->GetRuntimeClass() == ext->GetBoxIntClass()) {
99             return EtsString::CreateFromMUtf8("int");
100         }
101         if (obj->GetClass()->GetRuntimeClass() == ext->GetBoxLongClass()) {
102             return EtsString::CreateFromMUtf8("long");
103         }
104         if (obj->GetClass()->GetRuntimeClass() == ext->GetBoxFloatClass()) {
105             return EtsString::CreateFromMUtf8("float");
106         }
107         if (obj->GetClass()->GetRuntimeClass() == ext->GetBoxDoubleClass()) {
108             return EtsString::CreateFromMUtf8("number");
109         }
110     }
111 
112     return EtsString::CreateFromMUtf8("object");
113 }
114 
115 }  // namespace ark::ets::intrinsics
116