Skip to content

Commit 12160e3

Browse files
committed
feat: Add variant support
Improved constexpr types Signed-off-by Gordon Smith <GordonJSmith@gmail>
1 parent 15f06d1 commit 12160e3

19 files changed

Lines changed: 403 additions & 224 deletions

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ endif()
1010

1111
enable_testing()
1212

13+
set(ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
14+
1315
add_subdirectory(src)
1416
add_subdirectory(test)

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,9 @@ This repository contains a C++ ABI implementation of the WebAssembly Component M
3939
- [x] utf16 String
4040
- [x] latin1+utf16 String
4141
- [x] List
42-
- [ ] Field
43-
- [ ] Record
44-
- [ ] Tuple
45-
- [ ] Case
46-
- [ ] Variant
42+
- [x] Record
43+
- [x] Tuple
44+
- [x] Variant
4745
- [ ] Enum
4846
- [ ] Option
4947
- [ ] Result

src/flags.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace cmcpp
4343
}
4444

4545
template <Flags T>
46-
T lift_flat(const CallContext &cx, const WasmValVectorIterator &vi)
46+
T lift_flat(const CallContext &cx, const CoreValueIter &vi)
4747
{
4848
auto i = vi.next<int32_t>();
4949
uint8_t buff[ValTrait<T>::size];

src/float.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ namespace cmcpp
66
{
77
int32_t encode_float_as_i32(float32_t f)
88
{
9-
return *reinterpret_cast<int32_t*>(&f);
9+
return *reinterpret_cast<int32_t *>(&f);
1010
}
1111

1212
int64_t encode_float_as_i64(float64_t f)
1313
{
14-
return *reinterpret_cast<int64_t*>(&f);
14+
return *reinterpret_cast<int64_t *>(&f);
1515
}
1616

1717
float32_t decode_i32_as_float(int32_t i)
1818
{
19-
return *reinterpret_cast<float32_t*>(&i);
19+
return *reinterpret_cast<float32_t *>(&i);
2020
}
2121

2222
float64_t decode_i64_as_float(int64_t i)
2323
{
24-
return *reinterpret_cast<float64_t*>(&i);
24+
return *reinterpret_cast<float64_t *>(&i);
2525
}
2626

2727
float32_t core_f32_reinterpret_i32(int32_t i)

src/integer.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define CMCPP_INTEGER_HPP
33

44
#include "context.hpp"
5+
#include "util.hpp"
56

67
#include <cstring>
78
#include <iostream>
@@ -36,19 +37,19 @@ namespace cmcpp
3637
}
3738

3839
template <typename T>
39-
T lift_flat_unsigned(const WasmValVectorIterator &vi, uint32_t core_width, uint32_t t_width)
40+
T lift_flat_unsigned(const CoreValueIter &vi, uint32_t core_width, uint32_t t_width)
4041
{
4142
using WasmValType = ValTrait<T>::flat_type;
42-
auto retVal = vi.next<WasmValType>();
43+
auto retVal = std::get<WasmValType>(vi.next(ValTrait<T>::flat_types[0]));
4344
assert(ValTrait<WasmValType>::LOW_VALUE <= retVal && retVal < ValTrait<WasmValType>::HIGH_VALUE);
4445
return retVal;
4546
}
4647

4748
template <typename T>
48-
T lift_flat_signed(const WasmValVectorIterator &vi, uint32_t core_width, uint32_t t_width)
49+
T lift_flat_signed(const CoreValueIter &vi, uint32_t core_width, uint32_t t_width)
4950
{
5051
using WasmValType = ValTrait<T>::flat_type;
51-
auto retVal = static_cast<T>(vi.next<WasmValType>());
52+
auto retVal = static_cast<T>(std::get<WasmValType>(vi.next(ValTrait<T>::flat_types[0])));
5253
assert(ValTrait<T>::LOW_VALUE <= retVal && retVal <= ValTrait<T>::HIGH_VALUE);
5354
return retVal;
5455
}

src/lift.hpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,64 +8,72 @@
88
#include "list.hpp"
99
#include "flags.hpp"
1010
#include "record.hpp"
11+
#include "variant.hpp"
1112
#include "util.hpp"
1213

1314
namespace cmcpp
1415
{
1516
template <Boolean T>
16-
inline T lift_flat(const CallContext &cx, const WasmValVectorIterator &vi)
17+
inline T lift_flat(const CallContext &cx, const CoreValueIter &vi)
1718
{
1819
return convert_int_to_bool(vi.next<int32_t>());
1920
}
2021

2122
template <Char T>
22-
inline T lift_flat(const CallContext &cx, const WasmValVectorIterator &vi)
23+
inline T lift_flat(const CallContext &cx, const CoreValueIter &vi)
2324
{
2425
return convert_i32_to_char(cx, vi.next<int32_t>());
2526
}
2627

2728
template <UnsignedInteger T>
28-
inline T lift_flat(const CallContext &cx, const WasmValVectorIterator &vi)
29+
inline T lift_flat(const CallContext &cx, const CoreValueIter &vi)
2930
{
3031
return integer::lift_flat_unsigned<T>(vi, ValTrait<T>::size * 8, 8);
3132
}
3233

3334
template <SignedInteger T>
34-
inline T lift_flat(const CallContext &cx, const WasmValVectorIterator &vi)
35+
inline T lift_flat(const CallContext &cx, const CoreValueIter &vi)
3536
{
3637
return integer::lift_flat_signed<T>(vi, ValTrait<T>::size * 8, 8);
3738
}
3839

3940
template <Float T>
40-
inline T lift_flat(const CallContext &cx, const WasmValVectorIterator &vi)
41+
inline T lift_flat(const CallContext &cx, const CoreValueIter &vi)
4142
{
42-
return float_::canonicalize_nan<T>(vi.next<T>());
43+
using WasmValType = ValTrait<T>::flat_type;
44+
return float_::canonicalize_nan<T>(std::get<WasmValType>(vi.next(ValTrait<T>::flat_types[0])));
4345
}
4446

4547
template <String T>
46-
inline T lift_flat(const CallContext &cx, const WasmValVectorIterator &vi)
48+
inline T lift_flat(const CallContext &cx, const CoreValueIter &vi)
4749
{
4850
return string::lift_flat<T>(cx, vi);
4951
}
5052

5153
template <List T>
52-
inline T lift_flat(const CallContext &cx, const WasmValVectorIterator &vi)
54+
inline T lift_flat(const CallContext &cx, const CoreValueIter &vi)
5355
{
5456
return list::lift_flat<typename ValTrait<T>::inner_type>(cx, vi);
5557
}
5658

5759
template <Flags T>
58-
inline T lift_flat(const CallContext &cx, const WasmValVectorIterator &vi)
60+
inline T lift_flat(const CallContext &cx, const CoreValueIter &vi)
5961
{
6062
return flags::lift_flat<T>(cx, vi);
6163
}
6264

6365
template <Record T>
64-
T lift_flat(const CallContext &cx, const WasmValVectorIterator &vi)
66+
T lift_flat(const CallContext &cx, const CoreValueIter &vi)
6567
{
66-
auto x = record::lift_flat<T>(cx, vi);
68+
auto x = record::lift_flat_record<T>(cx, vi);
6769
return x;
6870
}
71+
72+
template <Variant T>
73+
T lift_flat(const CallContext &cx, const CoreValueIter &vi)
74+
{
75+
return variant::lift_flat<T>(cx, vi);
76+
}
6977
}
7078

7179
#endif

src/list.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace cmcpp
5757
}
5858

5959
template <typename T>
60-
WasmValVector lower_flat(CallContext &cx, const list_t<T> &v)
60+
WasmValVector lower_flat_list(CallContext &cx, const list_t<T> &v)
6161
{
6262
auto [ptr, length] = store_into_range(cx, v);
6363
return {static_cast<int32_t>(ptr), static_cast<int32_t>(length)};
@@ -85,7 +85,7 @@ namespace cmcpp
8585
}
8686

8787
template <typename T>
88-
list_t<T> lift_flat(const CallContext &cx, const WasmValVectorIterator &vi)
88+
list_t<T> lift_flat(const CallContext &cx, const CoreValueIter &vi)
8989
{
9090
auto ptr = vi.next<int32_t>();
9191
auto length = vi.next<int32_t>();

src/lower.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace cmcpp
5757
template <List T>
5858
inline WasmValVector lower_flat(CallContext &cx, const T &v)
5959
{
60-
return list::lower_flat(cx, v);
60+
return list::lower_flat_list(cx, v);
6161
}
6262

6363
template <Flags T>
@@ -69,7 +69,13 @@ namespace cmcpp
6969
template <Record T>
7070
inline WasmValVector lower_flat(CallContext &cx, const T &v)
7171
{
72-
return record::lower_flat(cx, v);
72+
return record::lower_flat_record(cx, v);
73+
}
74+
75+
template <Variant T>
76+
inline WasmValVector lower_flat(CallContext &cx, const T &v)
77+
{
78+
return variant::lower_flat_variant(cx, v);
7379
}
7480
}
7581

src/record.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace cmcpp
1717
{
1818

1919
template <Record T>
20-
void store(const CallContext &cx, const T&v, uint32_t ptr)
20+
void store(const CallContext &cx, const T &v, uint32_t ptr)
2121
{
2222
auto process_field = [&](auto &&field)
2323
{
@@ -31,7 +31,7 @@ namespace cmcpp
3131
}
3232

3333
template <Record T>
34-
WasmValVector lower_flat(CallContext &cx, const T &v)
34+
WasmValVector lower_flat_record(CallContext &cx, const T &v)
3535
{
3636
WasmValVector retVal = {};
3737
auto process_field = [&](auto &&field)
@@ -62,7 +62,7 @@ namespace cmcpp
6262
}
6363

6464
template <Record T>
65-
inline T lift_flat(const CallContext &cx, const WasmValVectorIterator &vi)
65+
inline T lift_flat_record(const CallContext &cx, const CoreValueIter &vi)
6666
{
6767
T result;
6868
auto process_field = [&](auto &&field)

0 commit comments

Comments
 (0)