diff --git a/include/cmcpp/context.hpp b/include/cmcpp/context.hpp index 5eb1e4d..6b5f7c7 100644 --- a/include/cmcpp/context.hpp +++ b/include/cmcpp/context.hpp @@ -109,14 +109,14 @@ namespace cmcpp public: CanonicalOptions opts; ComponentInstance inst; - func_t ft; + func_t ft; std::optional supertask; std::optional> on_return; std::function(std::future)> on_block; int num_borrows = 0; ContextLocalStorage context(); - Task(CanonicalOptions &opts, ComponentInstance &inst, func_t &ft, std::optional &supertask = std::nullopt, std::optional> &on_return = std::nullopt, std::function(std::future)> &on_block = std::nullopt) + Task(CanonicalOptions &opts, ComponentInstance &inst, func_t &ft, std::optional &supertask = std::nullopt, std::optional> &on_return = std::nullopt, std::function(std::future)> &on_block = std::nullopt) : opts(opts), inst(inst), ft(ft), supertask(supertask), on_return(on_return), on_block(on_block) {} }; diff --git a/include/cmcpp/lift.hpp b/include/cmcpp/lift.hpp index bf89780..4316159 100644 --- a/include/cmcpp/lift.hpp +++ b/include/cmcpp/lift.hpp @@ -53,7 +53,7 @@ namespace cmcpp } template - 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::flat_types; if (flat_types.size() > max_flat) diff --git a/include/cmcpp/lower.hpp b/include/cmcpp/lower.hpp index 563bfff..c4dbca8 100644 --- a/include/cmcpp/lower.hpp +++ b/include/cmcpp/lower.hpp @@ -76,7 +76,7 @@ namespace cmcpp } template - 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; diff --git a/include/cmcpp/traits.hpp b/include/cmcpp/traits.hpp index 85e0909..3448ef8 100644 --- a/include/cmcpp/traits.hpp +++ b/include/cmcpp/traits.hpp @@ -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, @@ -666,7 +674,7 @@ namespace cmcpp static constexpr ValType type = ValType::Variant; using inner_type = typename std::variant; - static constexpr int match = static_cast(std::ceil(std::log2(std::variant_size_v) / 8.0)); + static constexpr int match = ceil_div8(ceil_log2(std::variant_size_v)); using discriminant_type = std::conditional_t>>>; static constexpr uint32_t max_case_alignment = []() constexpr { @@ -696,18 +704,26 @@ namespace cmcpp ((i = std::max(i, ValTrait::flat_types.size())), ...); return i + 1; }(); + template + static constexpr void process_types(std::array& flat) + { + for (auto& ft : ValTrait::flat_types) { + if (StartIndex < flat_types_len) { + flat[StartIndex] = join(flat[StartIndex], ft); + process_types(flat); + } + } + } + template + static constexpr void process_types(std::array& flat) + { + } static constexpr std::array flat_types = []() constexpr { std::array flat; flat.fill(WasmValType::i32); flat[0] = ValTrait::flat_types[0]; - ([&]() - { - size_t i = 1; - for (auto &ft : ValTrait::flat_types) { - flat[i] = join(flat[i], ft); - ++i; - } }(), ...); + process_types<1, Ts...>(flat); return flat; }(); }; @@ -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 struct func_t_impl; diff --git a/include/wamr.hpp b/include/wamr.hpp index 952e8ee..211bc51 100644 --- a/include/wamr.hpp +++ b/include/wamr.hpp @@ -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 wasmVal2wam_val_t(const WasmValVector &values) + inline std::vector wasmVal2wam_val_t(const WasmValVector &values) { std::vector result; result.reserve(values.size()); @@ -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); @@ -94,7 +97,7 @@ namespace cmcpp std::vector inputs = wasmVal2wam_val_t(lowered_args); constexpr size_t output_size = std::is_same::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, @@ -121,7 +124,7 @@ namespace cmcpp }; } - std::pair 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 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) { @@ -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 \ No newline at end of file