Skip to content

Commit 15f8730

Browse files
committed
WIP: C++ wrapper [tests failing]
1 parent c9fab36 commit 15f8730

File tree

13 files changed

+324
-227
lines changed

13 files changed

+324
-227
lines changed

cpp/include/mun/field_info.h

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,54 @@
1-
#ifndef MUN_FIELD_INFO_H
2-
#define MUN_FIELD_INFO_H
1+
#ifndef MUN_FIELD_INFO_H_
2+
#define MUN_FIELD_INFO_H_
3+
4+
#include <cassert>
5+
6+
#include "mun/runtime_capi.h"
7+
#include "mun/type_info.h"
38

49
namespace mun {
10+
/**
11+
* @brief A wrapper around a Mun field information handle.
12+
*/
13+
class FieldInfo {
14+
public:
15+
/**
16+
* @brief Constructs field information from an instantiated `MunFieldInfoHandle`.
17+
*/
18+
FieldInfo(MunFieldInfoHandle handle) noexcept : m_handle(handle) {}
19+
20+
/**
21+
* @brief Retrieves the field's name.
22+
*/
23+
std::string_view name() const noexcept {
24+
const auto ptr = mun_field_info_name(m_handle);
25+
assert(ptr);
26+
return ptr;
27+
}
28+
29+
/**
30+
* @brief Retrieves the field's type.
31+
*/
32+
TypeInfo type() const noexcept {
33+
const auto type_handle = mun_field_info_type(m_handle);
34+
assert(type_handle._0);
35+
return TypeInfo(type_handle);
36+
}
37+
38+
/**
39+
* @brief Retrieves the field's offset.
40+
*/
41+
uint16_t offset() const noexcept {
42+
uint16_t offset;
43+
const auto error_handle = mun_field_info_offset(m_handle, &offset);
44+
assert(error_handle._0 == nullptr);
45+
return offset;
46+
}
47+
48+
private:
49+
MunFieldInfoHandle m_handle;
50+
};
51+
552
/**
653
* @brief A wrapper around a span of Mun field informations, which are owned by the Mun runtime.
754
*/
@@ -39,4 +86,4 @@ class FieldInfoSpan {
3986
};
4087
} // namespace mun
4188

42-
#endif // MUN_FIELD_INFO_H
89+
#endif // MUN_FIELD_INFO_H_

cpp/include/mun/function_info.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#ifndef MUN_FUNCTION_INFO_H_
2+
#define MUN_FUNCTION_INFO_H_
3+
4+
#include <cassert>
5+
6+
#include "mun/runtime_capi.h"
7+
#include "mun/type_info.h"
8+
9+
namespace mun {
10+
/**
11+
* @brief A wrapper around a Mun function information handle.
12+
*/
13+
class FunctionInfo {
14+
public:
15+
/**
16+
* @brief Constructs type information from an instantiated `MunFunctionInfoHandle`.
17+
*/
18+
FunctionInfo(MunFunctionInfoHandle handle) noexcept : m_handle(handle) {}
19+
20+
/**
21+
* @brief Retrieves the function's name.
22+
*/
23+
std::string_view name() const noexcept {
24+
const auto ptr = mun_function_info_name(m_handle);
25+
assert(ptr);
26+
return ptr;
27+
}
28+
29+
/**
30+
* @brief Retrieves the function's argument types.
31+
*/
32+
TypeInfoSpan argument_types() const noexcept {
33+
MunTypeInfoSpan span;
34+
const auto error_handle = mun_function_info_argument_types(m_handle, &span);
35+
assert(error_handle._0 == nullptr);
36+
return TypeInfoSpan(span);
37+
}
38+
39+
/**
40+
* @brief Retrieves the function's return type.
41+
*/
42+
TypeInfo return_type() const noexcept {
43+
const auto handle = mun_function_info_return_type(m_handle);
44+
assert(handle._0);
45+
return TypeInfo(handle);
46+
}
47+
48+
/**
49+
* @brief Retrieves the function's pointer.
50+
*/
51+
const void* function_pointer() const noexcept {
52+
const void* fn_ptr = mun_function_info_fn_ptr(m_handle);
53+
assert(fn_ptr);
54+
return fn_ptr;
55+
}
56+
57+
private:
58+
MunFunctionInfoHandle m_handle;
59+
};
60+
} // namespace mun
61+
62+
#endif // MUN_FUNCTION_INFO_H_

cpp/include/mun/gc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace mun {
99
class Runtime;
1010

1111
class GcRootPtr {
12-
public:
12+
public:
1313
/** Constructs a rooted garbage collection pointer from the provided raw
1414
* garbage collection handle.
1515
*
@@ -79,7 +79,7 @@ class GcRootPtr {
7979
return ptr;
8080
}
8181

82-
private:
82+
private:
8383
MunGcPtr m_ptr;
8484
const Runtime* m_runtime;
8585
};

cpp/include/mun/invoke_fn.h

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,25 @@ InvokeResult<Output, Args...> invoke_fn(Runtime& runtime, std::string_view fn_na
3333

3434
Error error;
3535
constexpr auto NUM_ARGS = sizeof...(Args);
36-
if (auto fn_info = runtime.find_function_definition(fn_name, &error); error) {
36+
if (auto fn_info = runtime.find_function_info(fn_name, &error); error) {
3737
std::cerr << "Failed to retrieve function info due to error: " << error.message()
3838
<< std::endl;
3939
} else if (!fn_info) {
4040
std::cerr << "Failed to obtain function '" << fn_name << "'" << std::endl;
4141
} else {
42-
const auto& prototype = fn_info->prototype;
43-
const auto& signature = prototype.signature;
44-
if (signature.num_arg_types != NUM_ARGS) {
42+
const auto arg_types = fn_info->argument_types();
43+
if (arg_types.size() != NUM_ARGS) {
4544
std::cerr << "Invalid number of arguments. Expected: "
46-
<< std::to_string(signature.num_arg_types)
47-
<< ". Found: " << std::to_string(NUM_ARGS) << "." << std::endl;
45+
<< std::to_string(arg_types.size()) << ". Found: " << std::to_string(NUM_ARGS)
46+
<< "." << std::endl;
4847

4948
return make_error(runtime, fn_name, args...);
5049
}
5150

5251
if constexpr (NUM_ARGS > 0) {
53-
const MunTypeInfo* const* arg_ptr = signature.arg_types;
52+
const MunTypeInfoHandle* arg_ptr = arg_types.begin();
5453
const std::optional<std::pair<const char*, const char*>> return_type_diffs[] = {
55-
reflection::equals_argument_type(**(arg_ptr++), args)...};
54+
reflection::equals_argument_type(TypeInfo(*(arg_ptr++)), args)...};
5655

5756
for (size_t idx = 0; idx < NUM_ARGS; ++idx) {
5857
if (auto diff = return_type_diffs[idx]) {
@@ -66,26 +65,16 @@ InvokeResult<Output, Args...> invoke_fn(Runtime& runtime, std::string_view fn_na
6665
}
6766
}
6867

69-
if (signature.return_type) {
70-
const auto& return_type = signature.return_type;
71-
if (auto diff = reflection::equals_return_type<Output>(*return_type)) {
72-
const auto& [expected, found] = *diff;
73-
std::cerr << "Invalid return type. Expected: " << expected << ". Found: " << found
74-
<< "." << std::endl;
75-
76-
return make_error(runtime, fn_name, args...);
77-
}
78-
} else if (!reflection::equal_types<void, Output>()) {
79-
std::cerr << "Invalid return type. Expected: "
80-
<< ReturnTypeReflection<void>::type_name()
81-
<< ". Found: " << ReturnTypeReflection<Output>::type_name() << "."
82-
<< std::endl;
68+
if (auto diff = reflection::equals_return_type<Output>(fn_info->return_type())) {
69+
const auto& [expected, found] = *diff;
70+
std::cerr << "Invalid return type. Expected: " << expected << ". Found: " << found
71+
<< "." << std::endl;
8372

8473
return make_error(runtime, fn_name, args...);
8574
}
8675

8776
auto fn = reinterpret_cast<typename Marshal<Output>::type(MUN_CALLTYPE*)(
88-
typename Marshal<Args>::type...)>(const_cast<void*>(fn_info->fn_ptr));
77+
typename Marshal<Args>::type...)>(const_cast<void*>(fn_info->function_pointer()));
8978
if constexpr (std::is_same_v<Output, void>) {
9079
fn(Marshal<Args>::to(args)...);
9180
return InvokeResult<Output, Args...>(std::monostate{});

cpp/include/mun/marshal.h

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,25 @@ namespace mun {
77
template <typename T>
88
struct Marshal;
99

10-
#define IMPL_PRIMITIVE_TYPE_MARSHAL(ty) \
11-
template <> \
12-
struct Marshal<ty> { \
13-
using type = ty; \
14-
\
15-
static type from(type value, const Runtime&) noexcept { return value; } \
16-
\
17-
static type to(type value) noexcept { return value; } \
18-
\
19-
static type copy_from(const type* value, const Runtime&, \
20-
std::optional<const MunTypeInfo*>) noexcept { \
21-
return *value; \
22-
} \
23-
\
24-
static void move_to(type value, type* ptr, std::optional<const MunTypeInfo*>) noexcept { \
25-
*ptr = std::move(value); \
26-
} \
27-
\
28-
static type swap_at(type value, type* ptr, const Runtime&, \
29-
std::optional<const MunTypeInfo*>) noexcept { \
30-
std::swap(value, *ptr); \
31-
return std::move(value); \
32-
} \
10+
#define IMPL_PRIMITIVE_TYPE_MARSHAL(ty) \
11+
template <> \
12+
struct Marshal<ty> { \
13+
using type = ty; \
14+
\
15+
static type from(type value, const Runtime&) noexcept { return value; } \
16+
\
17+
static type to(type value) noexcept { return value; } \
18+
\
19+
static type copy_from(const type* value, const Runtime&, TypeInfo) noexcept { \
20+
return *value; \
21+
} \
22+
\
23+
static void move_to(type value, type* ptr, TypeInfo) noexcept { *ptr = std::move(value); } \
24+
\
25+
static type swap_at(type value, type* ptr, const Runtime&, TypeInfo) noexcept { \
26+
std::swap(value, *ptr); \
27+
return std::move(value); \
28+
} \
3329
};
3430

3531
// TODO: Add support for 128-bit integers

cpp/include/mun/reflection.h

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,59 +10,51 @@
1010
#include "mun/type_info.h"
1111

1212
namespace mun {
13-
constexpr inline bool operator==(const MunGuid& lhs, const MunGuid& rhs) noexcept {
13+
constexpr inline bool operator==(const MunTypeId& lhs, const MunTypeId& rhs) noexcept {
1414
for (auto idx = 0; idx < 16; ++idx) {
15-
if (lhs._0[idx] != rhs._0[idx]) {
15+
if (lhs.guid._0[idx] != rhs.guid._0[idx]) {
1616
return false;
1717
}
1818
}
1919
return true;
2020
}
2121

22-
constexpr inline bool operator!=(const MunGuid& lhs, const MunGuid& rhs) noexcept {
22+
constexpr inline bool operator!=(const MunTypeId& lhs, const MunTypeId& rhs) noexcept {
2323
return !(lhs == rhs);
2424
}
2525

2626
template <typename T>
2727
struct ArgumentReflection {
28-
static constexpr const char* type_name(const T&) noexcept {
29-
return StaticTypeInfo<T>::Type.name;
30-
}
31-
static constexpr MunGuid type_guid(const T&) noexcept { return StaticTypeInfo<T>::Type.guid; }
28+
static constexpr const char* type_name(const T&) noexcept { return StaticTypeInfo<T>::name(); }
29+
static constexpr MunTypeId type_id(const T&) noexcept { return StaticTypeInfo<T>::id(); }
3230
};
3331

3432
template <typename T>
3533
struct ReturnTypeReflection {
36-
static constexpr const char* type_name() noexcept { return StaticTypeInfo<T>::Type.name; }
37-
static constexpr MunGuid type_guid() noexcept { return StaticTypeInfo<T>::Type.guid; }
38-
};
39-
40-
template <>
41-
struct ReturnTypeReflection<void> {
42-
static constexpr const char* type_name() noexcept { return "core::empty"; }
43-
static constexpr MunGuid type_guid() noexcept { return details::type_guid(type_name()); }
34+
static constexpr const char* type_name() noexcept { return StaticTypeInfo<T>::name(); }
35+
static constexpr MunTypeId type_id() noexcept { return StaticTypeInfo<T>::id(); }
4436
};
4537

4638
namespace reflection {
4739
template <typename T, typename U>
4840
constexpr bool equal_types() noexcept {
49-
return ReturnTypeReflection<T>::type_guid() == ReturnTypeReflection<U>::type_guid();
41+
return ReturnTypeReflection<T>::type_id() == ReturnTypeReflection<U>::type_id();
5042
}
5143

5244
template <typename Arg>
5345
inline std::optional<std::pair<const char*, const char*>> equals_argument_type(
54-
const MunTypeInfo& type_info, const Arg& arg) noexcept {
55-
if (type_info.guid == ArgumentReflection<Arg>::type_guid(arg)) {
46+
const TypeInfo& type_info, const Arg& arg) noexcept {
47+
if (type_info.id() == ArgumentReflection<Arg>::type_id(arg)) {
5648
return std::nullopt;
5749
} else {
5850
const auto expected_name = ArgumentReflection<Arg>::type_name(arg);
59-
return std::make_pair(type_info.name, expected_name);
51+
return std::make_pair(type_info.name().data(), expected_name);
6052
}
6153
}
6254

6355
template <typename T>
6456
inline std::optional<std::pair<const char*, const char*>> equals_return_type(
65-
const MunTypeInfo& type_info) noexcept;
57+
const TypeInfo& type_info) noexcept;
6658

6759
} // namespace reflection
6860

@@ -74,13 +66,13 @@ namespace mun {
7466
namespace reflection {
7567
template <typename T>
7668
std::optional<std::pair<const char*, const char*>> equals_return_type(
77-
const MunTypeInfo& type_info) noexcept {
78-
if (type_info.data.tag == MunTypeInfoData_Tag::Primitive) {
79-
if (type_info.guid != ReturnTypeReflection<T>::type_guid()) {
80-
return std::make_pair(type_info.name, ReturnTypeReflection<T>::type_name());
69+
const TypeInfo& type_info) noexcept {
70+
if (type_info.data().tag == MunTypeInfoData_Tag::Primitive) {
71+
if (type_info.id() != ReturnTypeReflection<T>::type_id()) {
72+
return std::make_pair(type_info.name().data(), ReturnTypeReflection<T>::type_name());
8173
}
8274
} else if (!reflection::equal_types<StructRef, T>()) {
83-
return std::make_pair(type_info.name, ReturnTypeReflection<T>::type_name());
75+
return std::make_pair(type_info.name().data(), ReturnTypeReflection<T>::type_name());
8476
}
8577

8678
return std::nullopt;

0 commit comments

Comments
 (0)