Skip to content

Commit 4c97ae4

Browse files
committed
WIP
1 parent c59bcc1 commit 4c97ae4

19 files changed

Lines changed: 747 additions & 109 deletions

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ project(component-model-cpp)
33
set(CMAKE_CXX_STANDARD 20)
44
set(CMAKE_CXX_STANDARD_REQUIRED ON)
55

6+
find_package(Boost)
7+
68
set(SRC
79
boolean.cpp
810
boolean.hpp

src/lift.hpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
#include "string.hpp"
88
#include "list.hpp"
99
#include "flags.hpp"
10-
#include "record.hpp"
10+
#include "tuple.hpp"
1111
#include "variant.hpp"
1212
#include "util.hpp"
1313

14+
#include <boost/pfr.hpp>
15+
1416
namespace cmcpp
1517
{
1618
template <Boolean T>
@@ -62,13 +64,21 @@ namespace cmcpp
6264
return flags::lift_flat<T>(cx, vi);
6365
}
6466

65-
template <Record T>
67+
template <Tuple T>
6668
T lift_flat(const CallContext &cx, const CoreValueIter &vi)
6769
{
68-
auto x = record::lift_flat_record<T>(cx, vi);
70+
auto x = tuple::lift_flat_tuple<T>(cx, vi);
6971
return x;
7072
}
7173

74+
template <Record T>
75+
T lift_flat(const CallContext &cx, const CoreValueIter &vi)
76+
{
77+
using TupleType = decltype(boost::pfr::structure_to_tuple(std::declval<typename ValTrait<T>::inner_type>()));
78+
TupleType x = tuple::lift_flat_tuple<TupleType>(cx, vi);
79+
return to_struct<T>(x);
80+
}
81+
7282
template <Variant T>
7383
T lift_flat(const CallContext &cx, const CoreValueIter &vi)
7484
{

src/lower.hpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
#include "string.hpp"
88
#include "list.hpp"
99
#include "flags.hpp"
10-
#include "record.hpp"
10+
#include "tuple.hpp"
1111
#include "util.hpp"
1212

1313
#include <tuple>
1414
#include <cassert>
1515

16+
#include "boost/pfr.hpp"
17+
1618
namespace cmcpp
1719
{
1820
template <Boolean T>
@@ -66,17 +68,25 @@ namespace cmcpp
6668
return flags::lower_flat(cx, v);
6769
}
6870

71+
template <Tuple T>
72+
inline WasmValVector lower_flat(CallContext &cx, const T &v)
73+
{
74+
return tuple::lower_flat_tuple(cx, v);
75+
}
76+
6977
template <Record T>
7078
inline WasmValVector lower_flat(CallContext &cx, const T &v)
7179
{
72-
return record::lower_flat_record(cx, v);
80+
auto x = boost::pfr::structure_to_tuple(v);
81+
return tuple::lower_flat_tuple(cx, x);
7382
}
7483

7584
template <Variant T>
7685
inline WasmValVector lower_flat(CallContext &cx, const T &v)
7786
{
7887
return variant::lower_flat_variant(cx, v);
7988
}
89+
8090
}
8191

8292
#endif

src/record.cpp

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/traits.hpp

Lines changed: 106 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -509,13 +509,13 @@ namespace cmcpp
509509
template <typename T>
510510
concept Field = ValTrait<T>::type != ValType::UNKNOWN;
511511

512-
// Record --------------------------------------------------------------------
512+
// Tuple --------------------------------------------------------------------
513513
template <Field... Ts>
514-
using record_t = std::tuple<Ts...>;
514+
using tuple_t = std::tuple<Ts...>;
515515
template <Field... Ts>
516-
struct ValTrait<record_t<Ts...>>
516+
struct ValTrait<tuple_t<Ts...>>
517517
{
518-
static constexpr ValType type = ValType::Record;
518+
static constexpr ValType type = ValType::Tuple;
519519
using inner_type = typename std::tuple<Ts...>;
520520
static constexpr size_t flat_types_len = []() constexpr
521521
{
@@ -541,22 +541,117 @@ namespace cmcpp
541541
}();
542542
};
543543
template <typename T>
544+
concept Tuple = ValTrait<T>::type == ValType::Tuple;
545+
546+
// Record ------------------------------------------------------------------
547+
template <typename S>
548+
concept Struct = (std::is_aggregate_v<S>);
549+
550+
template <Struct R>
551+
using record_t = R;
552+
553+
template <Struct R>
554+
struct ValTrait<record_t<R>>
555+
{
556+
static constexpr ValType type = ValType::Record;
557+
using inner_type = R;
558+
559+
// using tuple_type = tuple_t<Ts...>;
560+
// static constexpr size_t flat_types_len = ValTrait<tuple_type>::flat_types_len;
561+
// static constexpr std::array<WasmValType, flat_types_len> flat_types = ValTrait<tuple_type>::flat_types;
562+
};
563+
template <typename T>
544564
concept Record = ValTrait<T>::type == ValType::Record;
545565

546-
template <typename T, Record R, std::size_t... I>
547-
T to_struct_impl(const R &t, std::index_sequence<I...>)
566+
template <Record R, Tuple T, std::size_t... I>
567+
R to_struct_impl(const T &t, std::index_sequence<I...>)
548568
{
549-
return T{std::get<I>(t)...};
569+
return R{std::get<I>(t)...};
550570
}
551571

552-
template <typename T, Record R>
553-
T to_struct(const R &t)
572+
template <Record R, Tuple T>
573+
R to_struct(const T &t)
554574
{
555-
return to_struct_impl<T>(t, std::make_index_sequence<std::tuple_size_v<R>>{});
575+
return to_struct_impl<R>(t, std::make_index_sequence<std::tuple_size_v<T>>{});
556576
}
557577

578+
// {
579+
// using inner_type = std::tuple<Ts...>;
580+
// inner_type fields;
581+
582+
// template <size_t I>
583+
// auto get() const
584+
// {
585+
// return std::get<I>(fields);
586+
// }
587+
// };
588+
589+
// template <RecordConcept T, Field... Ts>
590+
// tuple_t<Ts...> toTupleImpl(const T &record, Ts... members)
591+
// {
592+
// return std::make_tuple(record.*members...);
593+
// }
594+
595+
// Helper to get the value from Field
596+
597+
// Helper to get field by index
598+
599+
// Recursive template to build the tuple
600+
601+
// struct record_t
602+
// {
603+
// using inner_type = std::tuple<Ts...>;
604+
// inner_type fields;
605+
606+
// template <size_t I>
607+
// auto get() const
608+
// {
609+
// return std::get<I>(fields);
610+
// }
611+
612+
// // auto toTupleImpl(const T &record, Ts... members)
613+
// // {
614+
// // return std::make_tuple(record.*members...);
615+
// // }
616+
617+
// // auto toTuple(const T &record, Ts... members)
618+
// // {
619+
// // return toTupleImpl(record, members...);
620+
// // }
621+
// };
622+
623+
// template <Field... Ts>
624+
// struct ValTrait<record_t<Ts...>>
625+
// {
626+
// static constexpr ValType type = ValType::Record;
627+
// };
628+
// template <typename T>
629+
// concept Record = ValTrait<T>::type == ValType::Record;
630+
631+
// struct MyStruct : public record_t<int32_t, int64_t, float32_t, float64_t>
632+
// {
633+
// int32_t a;
634+
// int64_t b;
635+
// float32_t c;
636+
// float64_t d;
637+
// };
638+
// record_t<int32_t, int64_t, float32_t, float64_t> r = {{1, 2, 3.0, 4.0}};
639+
640+
// template <typename T, Tuple R, std::size_t... I>
641+
// T to_struct_impl(const R &t, std::index_sequence<I...>)
642+
// {
643+
// return T{std::get<I>(t)...};
644+
// }
645+
646+
// template <typename T, Tuple R>
647+
// T to_struct(const R &t)
648+
// {
649+
// return to_struct_impl<T>(t, std::make_index_sequence<std::tuple_size_v<R>>{});
650+
// }
651+
558652
// Variant ------------------------------------------------------------------
559-
inline constexpr WasmValType join(WasmValType a, WasmValType b)
653+
inline constexpr WasmValType
654+
join(WasmValType a, WasmValType b)
560655
{
561656
if (a == b)
562657
{

src/tuple.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "tuple.hpp"
2+
3+
namespace cmcpp
4+
{
5+
6+
namespace tuple
7+
{
8+
}
9+
}

src/record.hpp renamed to src/tuple.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef CMCPP_RECORD_HPP
2-
#define CMCPP_RECORD_HPP
1+
#ifndef CMCPP_TUPLE_HPP
2+
#define CMCPP_TUPLE_HPP
33

44
#include "context.hpp"
55
#include "integer.hpp"
@@ -13,10 +13,10 @@
1313

1414
namespace cmcpp
1515
{
16-
namespace record
16+
namespace tuple
1717
{
1818

19-
template <Record T>
19+
template <Tuple T>
2020
void store(const CallContext &cx, const T &v, uint32_t ptr)
2121
{
2222
auto process_field = [&](auto &&field)
@@ -30,8 +30,8 @@ namespace cmcpp
3030
{ (process_field(fields), ...); }, v);
3131
}
3232

33-
template <Record T>
34-
WasmValVector lower_flat_record(CallContext &cx, const T &v)
33+
template <Tuple T>
34+
WasmValVector lower_flat_tuple(CallContext &cx, const T &v)
3535
{
3636
WasmValVector retVal = {};
3737
auto process_field = [&](auto &&field)
@@ -45,7 +45,7 @@ namespace cmcpp
4545
return retVal;
4646
}
4747

48-
template <Record T>
48+
template <Tuple T>
4949
T load(const CallContext &cx, uint32_t ptr)
5050
{
5151
T result;
@@ -61,8 +61,8 @@ namespace cmcpp
6161
return result;
6262
}
6363

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

src/util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace cmcpp
2222
switch (t)
2323
{
2424
case ValType::Tuple:
25-
return ValType::Record;
25+
return ValType::Tuple;
2626
case ValType::Enum:
2727
return ValType::Variant;
2828
case ValType::Option:

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ include_directories(
2121

2222
add_executable(${PROJECT_NAME}
2323
main.cpp
24+
scratchpad.cpp
2425
host-util.hpp
2526
host-util.cpp
2627
)

test/host-util.hpp

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,55 @@
1-
#include <utility>
1+
22
#include "context.hpp"
3+
#include "util.hpp"
4+
5+
#include <utility>
6+
#include <iostream>
37

48
using namespace cmcpp;
59

610
void trap(const char *msg = "");
711

8-
const char * encodingToICU(const Encoding encoding);
12+
const char *encodingToICU(const Encoding encoding);
913

1014
std::pair<void *, size_t> convert(void *dest, uint32_t dest_byte_len, const void *src, uint32_t src_byte_len, Encoding from_encoding, Encoding to_encoding);
11-
15+
16+
class Heap
17+
{
18+
private:
19+
uint32_t last_alloc = 0;
20+
21+
public:
22+
std::vector<uint8_t> memory;
23+
24+
Heap(size_t arg) : memory(arg), last_alloc(0)
25+
{
26+
}
27+
28+
uint32_t realloc(uint32_t original_ptr, size_t original_size, uint32_t alignment, size_t new_size)
29+
{
30+
if (original_ptr != 0 && new_size < original_size)
31+
{
32+
return align_to(original_ptr, alignment);
33+
}
34+
35+
uint32_t ret = align_to(last_alloc, alignment);
36+
last_alloc = ret + new_size;
37+
if (last_alloc > memory.size())
38+
{
39+
std::cout << "oom: have " << memory.size() << " need " << last_alloc << std::endl;
40+
trap("oom");
41+
}
42+
std::memcpy(&memory[ret], &memory[original_ptr], original_size);
43+
return ret;
44+
}
45+
};
46+
47+
inline std::unique_ptr<CallContext> createCallContext(Heap *heap, Encoding encoding)
48+
{
49+
std::unique_ptr<cmcpp::InstanceContext> instanceContext = std::make_unique<cmcpp::InstanceContext>(trap, convert,
50+
[heap](int original_ptr, int original_size, int alignment, int new_size) -> int
51+
{
52+
return heap->realloc(original_ptr, original_size, alignment, new_size);
53+
});
54+
return instanceContext->createCallContext(heap->memory, encoding);
55+
}

0 commit comments

Comments
 (0)