Skip to content
Open
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
22 changes: 1 addition & 21 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -187,31 +187,11 @@ cc_library(
],
)

genrule(
name = "prefixed_wasmtime_sources",
srcs = [
"src/wasmtime/types.h",
"src/wasmtime/wasmtime.cc",
],
outs = [
"src/wasmtime/prefixed_types.h",
"src/wasmtime/prefixed_wasmtime.cc",
],
cmd = """
for file in $(SRCS); do
sed -e 's/wasm_/wasmtime_wasm_/g' \
-e 's/wasmtime\\/types.h/wasmtime\\/prefixed_types.h/g' \
$$file >$(@D)/src/wasmtime/prefixed_$$(basename $$file)
done
""",
)

cc_library(
name = "wasmtime_lib",
srcs = [
"src/common/types.h",
"src/wasmtime/prefixed_types.h",
"src/wasmtime/prefixed_wasmtime.cc",
"src/wasmtime/wasmtime.cc",
],
hdrs = ["include/proxy-wasm/wasmtime.h"],
copts = [
Expand Down
13 changes: 11 additions & 2 deletions bazel/external/wasmtime.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,20 @@ genrule(
toolchains = ["@bazel_tools//tools/cpp:current_cc_toolchain"],
)

# This must match the features defined in `bazel/cargo/wasmtime/Cargo.toml` for
# the C/C++ API to expose the right set of methods.
# This should match the features defined in `bazel/cargo/wasmtime/Cargo.toml`
# for the C/C++ API to expose the right set of methods. Listing the feature
# here enables the C-api implementations of the features. Rust-side
# implementations are controlled by the Cargo.toml file. Mismatching features
# will result in compile/link time failures.
features = [
"cranelift",
"gc-drc",
# The C++ API references the wat feature whenever cranelift is turned on.
# Without adding `wat` to the headers, the C++ API will fail at compile time.
# `wat` is not actually used by proxy-wasm, so the corresponding feature is not
# enabled in Cargo.toml. If proxy-wasm used wat, this configuration would fail
# at link time.
"wat",
]

# Wasmtime C-api headers use cmakedefines to generate the config file.
Expand Down
11 changes: 10 additions & 1 deletion include/proxy-wasm/wasm_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,21 @@ template <size_t N>
using WasmCallVoid = std::function<WasmCallInFuncType<N, void, ContextBase *, Word>>;
template <size_t N>
using WasmCallWord = std::function<WasmCallInFuncType<N, Word, ContextBase *, Word>>;
// Callback used to test arg passing from host to wasm.
using WasmCall_WWlfd = std::function<Word(ContextBase *, Word, uint64_t, float, double)>;
// Types used to test return values. Floats are passed as parameters as these
// do not conflict with ProxyWasm ABI signatures.
using WasmCall_lf = std::function<uint64_t(ContextBase *, float)>;
using WasmCall_fff = std::function<float(ContextBase *, float, float)>;
using WasmCall_dfff = std::function<double(ContextBase *, float, float, float)>;

#define FOR_ALL_WASM_VM_EXPORTS(_f) \
_f(proxy_wasm::WasmCallVoid<0>) _f(proxy_wasm::WasmCallVoid<1>) _f(proxy_wasm::WasmCallVoid<2>) \
_f(proxy_wasm::WasmCallVoid<3>) _f(proxy_wasm::WasmCallVoid<5>) \
_f(proxy_wasm::WasmCallWord<0>) _f(proxy_wasm::WasmCallWord<1>) \
_f(proxy_wasm::WasmCallWord<2>) _f(proxy_wasm::WasmCallWord<3>)
_f(proxy_wasm::WasmCallWord<2>) _f(proxy_wasm::WasmCallWord<3>) \
_f(proxy_wasm::WasmCall_WWlfd) _f(proxy_wasm::WasmCall_lf) \
_f(proxy_wasm::WasmCall_fff) _f(proxy_wasm::WasmCall_dfff)

// These are templates and its helper for constructing signatures of functions callbacks from Wasm
// VMs.
Expand Down
12 changes: 10 additions & 2 deletions include/proxy-wasm/word.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ namespace proxy_wasm {
// Use byteswap functions only when compiling for big-endian platforms.
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define htowasm(x, vm_uses_wasm_byte_order) ((vm_uses_wasm_byte_order) ? __builtin_bswap32(x) : (x))
#define wasmtoh(x, vm_uses_wasm_byte_order) ((vm_uses_wasm_byte_order) ? __builtin_bswap32(x) : (x))
static inline float bswap(float x) {
return std::bit_cast<float>(__builtin_bswap32(std::bit_cast<int32_t>(x)));
}
static inline double bswap(double x) {
return std::bit_cast<double>(__builtin_bswap64(std::bit_cast<int64_t>(x)));
}
static inline uint32_t bswap(uint32_t x) { return __builtin_bswap32(x); }
static inline auto bswap(auto x) { return __builtin_bswap64(x); }
#define htowasm(x, vm_uses_wasm_byte_order) ((vm_uses_wasm_byte_order) ? bswap(x) : (x))
#define wasmtoh(x, vm_uses_wasm_byte_order) ((vm_uses_wasm_byte_order) ? bswap(x) : (x))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this change? AFAICT, all the callers are always passing uint32_t.

Copy link
Contributor Author

@leonm1 leonm1 Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a few hostcalls that use int64_t:

Word increment_metric(Word metric_id, int64_t offset);

Word wasi_unstable_clock_time_get(Word, uint64_t, Word);

#else
#define htowasm(x, vm_uses_wasm_byte_order) (x)
#define wasmtoh(x, vm_uses_wasm_byte_order) (x)
Expand Down
1 change: 1 addition & 0 deletions src/v8/v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ template <> constexpr auto convertArgToValKind<uint32_t>() { return wasm::ValKin
template <> constexpr auto convertArgToValKind<int64_t>() { return wasm::ValKind::I64; };
template <> constexpr auto convertArgToValKind<uint64_t>() { return wasm::ValKind::I64; };
template <> constexpr auto convertArgToValKind<double>() { return wasm::ValKind::F64; };
template <> constexpr auto convertArgToValKind<float>() { return wasm::ValKind::F32; };

template <typename T, std::size_t... I>
constexpr auto convertArgsTupleToValTypesImpl(std::index_sequence<I...> /*comptime*/) {
Expand Down
10 changes: 9 additions & 1 deletion src/wamr/wamr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@ template <> void assignVal(uint64_t t, wasm_val_t &val) {
val.kind = WASM_I64;
val.of.i64 = static_cast<int64_t>(t);
}
template <> void assignVal(float t, wasm_val_t &val) {
val.kind = WASM_F32;
val.of.f32 = static_cast<float>(t);
}
template <> void assignVal(double t, wasm_val_t &val) {
val.kind = WASM_F64;
val.of.f64 = t;
Expand All @@ -485,17 +489,21 @@ template <> auto convertArgToValTypePtr<Word>() { return wasm_valtype_new_i32();
template <> auto convertArgToValTypePtr<uint32_t>() { return wasm_valtype_new_i32(); };
template <> auto convertArgToValTypePtr<int64_t>() { return wasm_valtype_new_i64(); };
template <> auto convertArgToValTypePtr<uint64_t>() { return wasm_valtype_new_i64(); };
template <> auto convertArgToValTypePtr<float>() { return wasm_valtype_new_f32(); };
template <> auto convertArgToValTypePtr<double>() { return wasm_valtype_new_f64(); };

template <typename T> T convertValueTypeToArg(wasm_val_t val);
template <> uint32_t convertValueTypeToArg<uint32_t>(wasm_val_t val) {
return static_cast<uint32_t>(val.of.i32);
}
template <> Word convertValueTypeToArg<Word>(wasm_val_t val) { return val.of.i32; }
template <> Word convertValueTypeToArg<Word>(wasm_val_t val) {
return std::bit_cast<uint32_t>(val.of.i32);
}
template <> int64_t convertValueTypeToArg<int64_t>(wasm_val_t val) { return val.of.i64; }
template <> uint64_t convertValueTypeToArg<uint64_t>(wasm_val_t val) {
return static_cast<uint64_t>(val.of.i64);
}
template <> float convertValueTypeToArg<float>(wasm_val_t val) { return val.of.f32; }
template <> double convertValueTypeToArg<double>(wasm_val_t val) { return val.of.f64; }

template <typename T, typename U, std::size_t... I>
Expand Down
7 changes: 6 additions & 1 deletion src/wasmedge/wasmedge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ template <> WasmEdge_Value makeVal(uint64_t t) {
return WasmEdge_ValueGenI64(static_cast<int64_t>(t));
}
template <> WasmEdge_Value makeVal(double t) { return WasmEdge_ValueGenF64(t); }
template <> WasmEdge_Value makeVal(float t) { return WasmEdge_ValueGenF32(t); }

// Helper function to print values.
std::string printValue(const WasmEdge_Value &value) {
Expand Down Expand Up @@ -144,19 +145,23 @@ template <> WasmEdge_ValType convArgToValType<uint32_t>() { return WasmEdge_ValT
template <> WasmEdge_ValType convArgToValType<int64_t>() { return WasmEdge_ValTypeGenI64(); }
template <> WasmEdge_ValType convArgToValType<uint64_t>() { return WasmEdge_ValTypeGenI64(); }
template <> WasmEdge_ValType convArgToValType<double>() { return WasmEdge_ValTypeGenF64(); }
template <> WasmEdge_ValType convArgToValType<float>() { return WasmEdge_ValTypeGenF32(); }

// Helper templates to convert valtype to arg.
template <typename T> T convValTypeToArg(WasmEdge_Value val);
template <> uint32_t convValTypeToArg<uint32_t>(WasmEdge_Value val) {
return static_cast<uint32_t>(WasmEdge_ValueGetI32(val));
}
template <> Word convValTypeToArg<Word>(WasmEdge_Value val) { return WasmEdge_ValueGetI32(val); }
template <> Word convValTypeToArg<Word>(WasmEdge_Value val) {
return std::bit_cast<uint32_t>(WasmEdge_ValueGetI32(val));
}
template <> int64_t convValTypeToArg<int64_t>(WasmEdge_Value val) {
return WasmEdge_ValueGetI64(val);
}
template <> uint64_t convValTypeToArg<uint64_t>(WasmEdge_Value val) {
return static_cast<uint64_t>(WasmEdge_ValueGetI64(val));
}
template <> float convValTypeToArg<float>(WasmEdge_Value val) { return WasmEdge_ValueGetF32(val); }
template <> double convValTypeToArg<double>(WasmEdge_Value val) {
return WasmEdge_ValueGetF64(val);
}
Expand Down
43 changes: 0 additions & 43 deletions src/wasmtime/types.h

This file was deleted.

Loading
Loading