• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "ecmascript/cross_vm/dynamic_type_converter.h"
17 
18 #include "ecmascript/js_bigint-inl.h"
19 #include "ecmascript/js_tagged_value.h"
20 #include "ecmascript/js_tagged_value-inl.h"
21 #include "common_interfaces/objects/base_object_dispatcher.h"
22 #include "common_interfaces/objects/base_type.h"
23 
24 namespace panda::ecmascript {
25 
26 DynamicTypeConverter DynamicTypeConverter::dynTypeConverter_;
27 
Initialize()28 void DynamicTypeConverter::Initialize()
29 {
30     common::BaseObjectDispatcher::GetDispatcher().RegisterDynamicTypeConverter(&dynTypeConverter_);
31 }
32 
WrapTagged(ThreadHolder * thread,BaseType value)33 JSTaggedValue DynamicTypeConverter::WrapTagged(ThreadHolder *thread, BaseType value)
34 {
35     JSTaggedValue result;
36     std::visit(
37         [&](auto&& arg) {
38             using T = std::decay_t<decltype(arg)>;
39             if constexpr (std::is_same_v<T, std::monostate>) {
40                 result = JSTaggedValue::Hole();
41             } else if constexpr (std::is_same_v<T, bool>) {
42                 result = JSTaggedValue(arg);
43             } else if constexpr (std::is_same_v<T, int64_t> || std::is_same_v<T, uint64_t>) {
44                 result = JSTaggedValue(static_cast<double>(arg));
45             } else if constexpr (std::is_same_v<T, uint32_t>) {
46                 result = JSTaggedValue(arg);
47             } else if constexpr (std::is_integral_v<T>) {
48                 result = JSTaggedValue(static_cast<int32_t>(arg));
49             } else if constexpr (std::is_floating_point_v<T>) {
50                 result = JSTaggedValue(static_cast<double>(arg));
51             } else if constexpr (std::is_same_v<T, common::BaseUndefined>) {
52                 result = JSTaggedValue::Undefined();
53             } else if constexpr (std::is_same_v<T, common::BaseNull>) {
54                 result = JSTaggedValue::Null();
55             } else if constexpr (std::is_same_v<T, common::BaseBigInt>) {
56                 BigInt* bigInt = *BigInt::CreateBigint(thread->GetJSThread(), arg.length);
57                 bigInt->SetSign(arg.sign);
58                 for (uint32_t i = 0; i < arg.length; i++) {
59                     bigInt->SetDigit(i, arg.data[i]);
60                 }
61                 result = JSTaggedValue(static_cast<TaggedObject*>(bigInt));
62             } else if constexpr (std::is_same_v<T, BaseObject*>) {
63                 result = JSTaggedValue(static_cast<TaggedObject*>(arg));
64             } else if constexpr (std::is_same_v<T, BaseString*>) {
65                 result = JSTaggedValue(EcmaString::FromBaseString(arg));
66             } else {
67                 LOG_ECMA(FATAL) << "this branch is unreachable";
68                 UNREACHABLE();
69             }
70         },
71         value);
72     return result;
73 }
74 
UnWrapTagged(JSTaggedValue value)75 BaseType DynamicTypeConverter::UnWrapTagged(JSTaggedValue value)
76 {
77     if (value.IsBoolean()) {
78         return value.ToBoolean();
79     } else if (value.IsInt()) {
80         return static_cast<int32_t>(value.GetInt());
81     } else if (value.IsDouble()) {
82         return static_cast<double>(value.GetDouble());
83     } else if (value.IsUndefined()) {
84         return common::BaseUndefined();
85     } else if (value.IsNull()) {
86         return common::BaseNull();
87     } else if (value.IsBigInt()) {
88         BigInt *bigInt = BigInt::Cast(value.GetTaggedObject());
89         common::BaseBigInt baseBigInt;
90         baseBigInt.length = bigInt->GetLength();
91         baseBigInt.sign = bigInt->GetSign();
92         baseBigInt.data.resize(baseBigInt.length);
93         for (uint32_t i = 0; i < baseBigInt.length; i++) {
94             baseBigInt.data[i] = bigInt->GetDigit(i);
95         }
96         return baseBigInt;
97     } else if (value.IsString()) {
98         return EcmaString::Cast(value)->ToBaseString();
99     } else if (value.IsHeapObject()) {
100         return static_cast<BaseObject*>(value.GetTaggedObject());
101     } else {
102         LOG_ECMA(FATAL) << "this branch is unreachable";
103         UNREACHABLE();
104     }
105     return std::monostate();
106 }
107 }
108