Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/cmcpp/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ namespace cmcpp
public:
CanonicalOptions opts;
ComponentInstance inst;
func_t<R, Args...> ft;
func_t<R(Args...)> ft;
std::optional<Task> supertask;
std::optional<std::function<void()>> on_return;
std::function<std::future<void>(std::future<void>)> on_block;
int num_borrows = 0;
ContextLocalStorage context();

Task(CanonicalOptions &opts, ComponentInstance &inst, func_t<R, Args...> &ft, std::optional<Task> &supertask = std::nullopt, std::optional<std::function<void()>> &on_return = std::nullopt, std::function<std::future<void>(std::future<void>)> &on_block = std::nullopt)
Task(CanonicalOptions &opts, ComponentInstance &inst, func_t<R(Args...)> &ft, std::optional<Task> &supertask = std::nullopt, std::optional<std::function<void()>> &on_return = std::nullopt, std::function<std::future<void>(std::future<void>)> &on_block = std::nullopt)
: opts(opts), inst(inst), ft(ft), supertask(supertask), on_return(on_return), on_block(on_block) {}
};

Expand Down
2 changes: 1 addition & 1 deletion include/cmcpp/lift.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace cmcpp
}

template <Field T>
inline T lift_flat_values(const LiftLowerContext &cx, uint max_flat, const CoreValueIter &vi)
inline T lift_flat_values(const LiftLowerContext &cx, uint32_t max_flat, const CoreValueIter &vi)
{
auto flat_types = ValTrait<T>::flat_types;
if (flat_types.size() > max_flat)
Expand Down
2 changes: 1 addition & 1 deletion include/cmcpp/lower.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace cmcpp
}

template <Field... Ts>
inline WasmValVector lower_flat_values(LiftLowerContext &cx, uint max_flat, uint32_t *out_param, Ts &&...vs)
inline WasmValVector lower_flat_values(LiftLowerContext &cx, uint32_t max_flat, uint32_t *out_param, Ts &&...vs)
{
WasmValVector retVal = {};
// cx.inst.may_leave=false;
Expand Down
36 changes: 26 additions & 10 deletions include/cmcpp/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ namespace cmcpp
using bytes = uint32_t;
using size = uint32_t;

static constexpr int ceil_log2(std::size_t n) {
return (n <= 1) ? 0 : 1 + ceil_log2((n + 1) / 2);
}

static constexpr int ceil_div8(int bits) {
return (bits + 7) / 8;
}

enum class WasmValType : uint8_t
{
UNKNOWN,
Expand Down Expand Up @@ -666,7 +674,7 @@ namespace cmcpp
static constexpr ValType type = ValType::Variant;
using inner_type = typename std::variant<Ts...>;

static constexpr int match = static_cast<int>(std::ceil(std::log2(std::variant_size_v<inner_type>) / 8.0));
static constexpr int match = ceil_div8(ceil_log2(std::variant_size_v<inner_type>));
using discriminant_type = std::conditional_t<match == 0, uint8_t, std::conditional_t<match == 1, uint8_t, std::conditional_t<match == 2, uint16_t, std::conditional_t<match == 3, uint32_t, void>>>>;
static constexpr uint32_t max_case_alignment = []() constexpr
{
Expand Down Expand Up @@ -696,18 +704,26 @@ namespace cmcpp
((i = std::max(i, ValTrait<Ts>::flat_types.size())), ...);
return i + 1;
}();
template<size_t StartIndex, typename First, typename... Rest>
static constexpr void process_types(std::array<WasmValType, flat_types_len>& flat)
{
for (auto& ft : ValTrait<First>::flat_types) {
if (StartIndex < flat_types_len) {
flat[StartIndex] = join(flat[StartIndex], ft);
process_types<StartIndex + 1, Rest...>(flat);
}
}
}
template<size_t StartIndex>
static constexpr void process_types(std::array<WasmValType, flat_types_len>& flat)
{
}
static constexpr std::array<WasmValType, flat_types_len> flat_types = []() constexpr
{
std::array<WasmValType, flat_types_len> flat;
flat.fill(WasmValType::i32);
flat[0] = ValTrait<discriminant_type>::flat_types[0];
([&]()
{
size_t i = 1;
for (auto &ft : ValTrait<Ts>::flat_types) {
flat[i] = join(flat[i], ft);
++i;
} }(), ...);
process_types<1, Ts...>(flat);
return flat;
}();
};
Expand Down Expand Up @@ -739,8 +755,8 @@ namespace cmcpp
using enum_t = uint32_t;

// Func --------------------------------------------------------------------
constexpr uint MAX_FLAT_PARAMS = 16;
constexpr uint MAX_FLAT_RESULTS = 1;
constexpr uint32_t MAX_FLAT_PARAMS = 16;
constexpr uint32_t MAX_FLAT_RESULTS = 1;

template <typename>
struct func_t_impl;
Expand Down
17 changes: 11 additions & 6 deletions include/wamr.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#ifndef CMCPP_WAMR_HPP
#define CMCPP_WAMR_HPP

#include "wasm_export.h"
#include "cmcpp.hpp"

namespace cmcpp
{

void trap(const char *msg)
inline void trap(const char *msg)
{
throw new std::runtime_error(msg);
}

std::vector<wasm_val_t> wasmVal2wam_val_t(const WasmValVector &values)
inline std::vector<wasm_val_t> wasmVal2wam_val_t(const WasmValVector &values)
{
std::vector<wasm_val_t> result;
result.reserve(values.size());
Expand Down Expand Up @@ -41,7 +44,7 @@ namespace cmcpp
return result;
}

WasmValVector wam_val_t2wasmVal(size_t count, const wasm_val_t *values)
inline WasmValVector wam_val_t2wasmVal(size_t count, const wasm_val_t *values)
{
WasmValVector result;
result.reserve(count);
Expand Down Expand Up @@ -94,7 +97,7 @@ namespace cmcpp
std::vector<wasm_val_t> inputs = wasmVal2wam_val_t(lowered_args);

constexpr size_t output_size = std::is_same<result_t, void>::value ? 0 : 1;
wasm_val_t outputs[output_size];
wasm_val_t outputs[output_size == 0 ? 1 : output_size];

bool success = wasm_runtime_call_wasm_a(exec_env, guest_func,
output_size, outputs,
Expand All @@ -121,7 +124,7 @@ namespace cmcpp
};
}

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)
inline 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)
{
if (from_encoding == to_encoding)
{
Expand Down Expand Up @@ -236,9 +239,11 @@ namespace cmcpp
return symbol;
}

bool host_module(const char *module_name, NativeSymbol *native_symbols, uint32_t n_native_symbols)
inline bool host_module(const char *module_name, NativeSymbol *native_symbols, uint32_t n_native_symbols)
{
return wasm_runtime_register_natives_raw(module_name, native_symbols, n_native_symbols);
}

}

#endif
Loading