it_lilo/
utils.rs

1/*
2 * Copyright 2021 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use crate::IRecordType;
18use crate::IType;
19use crate::IValue;
20
21/// Size of a value in a serialized view.
22pub fn ser_type_size(ty: &IType) -> u32 {
23    const WASM_POINTER_SIZE: u32 = 4;
24
25    match ty {
26        IType::Boolean | IType::S8 | IType::U8 => 1,
27        IType::S16 | IType::U16 => 2,
28        IType::S32 | IType::U32 | IType::I32 | IType::F32 => 4,
29        IType::Record(_) => 4,
30        // Vec-like types are passed by pointer and size
31        IType::String | IType::ByteArray | IType::Array(_) => 2 * WASM_POINTER_SIZE,
32        IType::S64 | IType::U64 | IType::I64 | IType::F64 => 8,
33    }
34}
35
36/// Size of a value in a serialized view.
37pub fn ser_value_size(value: &IValue) -> u32 {
38    match value {
39        IValue::Boolean(_) | IValue::S8(_) | IValue::U8(_) => 1,
40        IValue::S16(_) | IValue::U16(_) => 2,
41        IValue::S32(_) | IValue::U32(_) | IValue::F32(_) | IValue::I32(_) => 4,
42        IValue::S64(_) | IValue::U64(_) | IValue::F64(_) | IValue::I64(_) => 8,
43        IValue::String(_) | IValue::ByteArray(_) | IValue::Array(_) => 2 * 4,
44        IValue::Record(_) => 4,
45    }
46}
47
48/// Returns the record size in bytes.
49pub fn record_size(record_type: &IRecordType) -> u32 {
50    record_type
51        .fields
52        .iter()
53        .map(|f| ser_type_size(&f.ty))
54        .sum()
55}
56
57pub fn type_tag_form_itype(itype: &IType) -> u32 {
58    const POINTER_CODE: u32 = 3; // u32 in the sdk
59
60    match itype {
61        IType::Boolean => 0,          // u8
62        IType::U8 => 1,               // u8
63        IType::U16 => 2,              // u16
64        IType::U32 => 3,              // u32
65        IType::U64 => 4,              // u64
66        IType::S8 => 5,               // i8
67        IType::S16 => 6,              // i16
68        IType::S32 | IType::I32 => 7, // i32
69        IType::S64 | IType::I64 => 8, // i64
70        IType::F32 => 9,              // f32
71        IType::F64 => 10,             // f64
72        IType::ByteArray | IType::Array(_) | IType::Record(_) | IType::String => POINTER_CODE,
73    }
74}
75
76pub fn type_tag_form_ivalue(itype: &IValue) -> u32 {
77    const POINTER_CODE: u32 = 3; // u32 in the sdk
78
79    match itype {
80        IValue::Boolean(_) => 0,              // u8
81        IValue::U8(_) => 1,                   // u8
82        IValue::U16(_) => 2,                  // u16
83        IValue::U32(_) => 3,                  // u32
84        IValue::U64(_) => 4,                  // u64
85        IValue::S8(_) => 5,                   // i8
86        IValue::S16(_) => 6,                  // i16
87        IValue::S32(_) | IValue::I32(_) => 7, // i32
88        IValue::S64(_) | IValue::I64(_) => 8, // i64
89        IValue::F32(_) => 9,                  // f32
90        IValue::F64(_) => 10,                 // f64
91        IValue::ByteArray(_) | IValue::Array(_) | IValue::Record(_) | IValue::String(_) => {
92            POINTER_CODE
93        }
94    }
95}