1 /**
2 * Copyright (c) 2021-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 <iostream>
17 #include <string_view>
18 #include "plugins/ets/runtime/ets_vm.h"
19 #include "plugins/ets/runtime/types/ets_string.h"
20 #include "libpandabase/utils/utf.h"
21
22 #include "intrinsics.h"
23
24 namespace ark::ets::intrinsics {
25
26 namespace {
27 constexpr const char *NAN_LITERAL = "NaN";
28 constexpr const char *INF_LITERAL = "Infinity";
29 constexpr const char *NEGINF_LITERAL = "-Infinity";
30 } // namespace
31
StdConsolePrintln(ObjectHeader * header)32 extern "C" void StdConsolePrintln(ObjectHeader *header [[maybe_unused]])
33 {
34 std::cout << std::endl;
35 }
36
StdConsolePrintBool(ObjectHeader * header,uint8_t b)37 extern "C" void StdConsolePrintBool([[maybe_unused]] ObjectHeader *header, uint8_t b)
38 {
39 if (b != 0U) {
40 std::cout << "true";
41 } else {
42 std::cout << "false";
43 }
44 }
45
StdConsolePrintChar(ObjectHeader * header,uint16_t c)46 extern "C" void StdConsolePrintChar([[maybe_unused]] ObjectHeader *header, uint16_t c)
47 {
48 const utf::Utf8Char utf8Ch = utf::ConvertUtf16ToUtf8(c, 0, false);
49 std::cout << std::string_view(reinterpret_cast<const char *>(utf8Ch.ch.data()), utf8Ch.n);
50 }
51
StdConsolePrintString(ObjectHeader * header,EtsString * str)52 extern "C" void StdConsolePrintString([[maybe_unused]] ObjectHeader *header, EtsString *str)
53 {
54 ark::intrinsics::PrintString(str->GetCoreType());
55 }
56
StdConsolePrintI32(ObjectHeader * header,int32_t v)57 extern "C" void StdConsolePrintI32([[maybe_unused]] ObjectHeader *header, int32_t v)
58 {
59 ark::intrinsics::PrintI32(v);
60 }
61
StdConsolePrintI16(ObjectHeader * header,int16_t v)62 extern "C" void StdConsolePrintI16([[maybe_unused]] ObjectHeader *header, int16_t v)
63 {
64 ark::intrinsics::PrintI32(v);
65 }
66
StdConsolePrintI8(ObjectHeader * header,int8_t v)67 extern "C" void StdConsolePrintI8([[maybe_unused]] ObjectHeader *header, int8_t v)
68 {
69 ark::intrinsics::PrintI32(v);
70 }
71
StdConsolePrintI64(ObjectHeader * header,int64_t v)72 extern "C" void StdConsolePrintI64([[maybe_unused]] ObjectHeader *header, int64_t v)
73 {
74 ark::intrinsics::PrintI64(v);
75 }
76
StdConsolePrintF32(ObjectHeader * header,float v)77 extern "C" void StdConsolePrintF32([[maybe_unused]] ObjectHeader *header, float v)
78 {
79 auto coroutine = EtsCoroutine::GetCurrent();
80 [[maybe_unused]] EtsHandleScope scope(coroutine);
81 if (std::isnan(v)) {
82 StdConsolePrintString(header,
83 EtsHandle<EtsString>(coroutine, EtsString::CreateFromMUtf8(NAN_LITERAL)).GetPtr());
84 } else if (!std::isfinite(v)) {
85 if (v < 0) {
86 StdConsolePrintString(header,
87 EtsHandle<EtsString>(coroutine, EtsString::CreateFromMUtf8(NEGINF_LITERAL)).GetPtr());
88 } else {
89 StdConsolePrintString(header,
90 EtsHandle<EtsString>(coroutine, EtsString::CreateFromMUtf8(INF_LITERAL)).GetPtr());
91 }
92 } else {
93 ark::intrinsics::PrintF32(v);
94 }
95 }
96
StdConsolePrintF64(ObjectHeader * header,double v)97 extern "C" void StdConsolePrintF64([[maybe_unused]] ObjectHeader *header, double v)
98 {
99 auto coroutine = EtsCoroutine::GetCurrent();
100 [[maybe_unused]] EtsHandleScope scope(coroutine);
101 if (std::isnan(v)) {
102 StdConsolePrintString(header,
103 EtsHandle<EtsString>(coroutine, EtsString::CreateFromMUtf8(NAN_LITERAL)).GetPtr());
104 } else if (!std::isfinite(v)) {
105 if (v < 0) {
106 StdConsolePrintString(header,
107 EtsHandle<EtsString>(coroutine, EtsString::CreateFromMUtf8(NEGINF_LITERAL)).GetPtr());
108 } else {
109 StdConsolePrintString(header,
110 EtsHandle<EtsString>(coroutine, EtsString::CreateFromMUtf8(INF_LITERAL)).GetPtr());
111 }
112 } else {
113 ark::intrinsics::PrintF64(v);
114 }
115 }
116
117 } // namespace ark::ets::intrinsics
118