Skip to content
Draft
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
1,892 changes: 1,021 additions & 871 deletions Cargo.lock

Large diffs are not rendered by default.

26 changes: 14 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -341,18 +341,20 @@ wit-bindgen = { version = "0.50.0", default-features = false }
wit-bindgen-rust-macro = { version = "0.50.0", default-features = false }

# wasm-tools family:
wasmparser = { version = "0.243.0", default-features = false, features = ['simd'] }
wat = "1.243.0"
wast = "243.0.0"
wasmprinter = "0.243.0"
wasm-encoder = "0.243.0"
wasm-smith = "0.243.0"
wasm-mutate = "0.243.0"
wit-parser = "0.243.0"
wit-component = "0.243.0"
wasm-wave = "0.243.0"
wasm-compose = "0.243.0"
json-from-wast = "0.243.0"
# TODO: well, these are not published to crates.io yet, so we need to use the git repository.
# I am gonna remove this once the feature is "completed" and the crates are published to crates.io.
wasmparser = { git = "https://github.com/bytecodealliance/wasm-tools", default-features = false, features = ['simd'] }
wat = { git = "https://github.com/bytecodealliance/wasm-tools" }
wast = { git = "https://github.com/bytecodealliance/wasm-tools" }
wasmprinter = { git = "https://github.com/bytecodealliance/wasm-tools" }
wasm-encoder = { git = "https://github.com/bytecodealliance/wasm-tools" }
wasm-smith = { git = "https://github.com/bytecodealliance/wasm-tools" }
wasm-mutate = { git = "https://github.com/bytecodealliance/wasm-tools" }
wit-parser = { git = "https://github.com/bytecodealliance/wasm-tools" }
wit-component = { git = "https://github.com/bytecodealliance/wasm-tools" }
wasm-wave = { git = "https://github.com/bytecodealliance/wasm-tools" }
wasm-compose = { git = "https://github.com/bytecodealliance/wasm-tools" }
json-from-wast = { git = "https://github.com/bytecodealliance/wasm-tools" }

# Non-Bytecode Alliance maintained dependencies:
# --------------------------
Expand Down
8 changes: 7 additions & 1 deletion crates/c-api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ else()
endif()
endif()

set(WASMTIME_TARGET_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_TARGET}/${WASMTIME_BUILD_TYPE})
# Respect CARGO_TARGET_DIR if set, allowing users to customize where Cargo
# outputs build artifacts
if(DEFINED ENV{CARGO_TARGET_DIR})
set(WASMTIME_TARGET_DIR $ENV{CARGO_TARGET_DIR}/${WASMTIME_TARGET}/${WASMTIME_BUILD_TYPE})
else()
set(WASMTIME_TARGET_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_TARGET}/${WASMTIME_BUILD_TYPE})
endif()

if(WASMTIME_TARGET MATCHES "apple")
set(WASMTIME_SHARED_FILES libwasmtime.dylib)
Expand Down
43 changes: 43 additions & 0 deletions crates/c-api/include/wasmtime/component/types/val.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,43 @@ WASM_API_EXTERN bool wasmtime_component_stream_type_ty(
const wasmtime_component_stream_type_t *ty,
struct wasmtime_component_valtype_t *type_ret);

// ----------- maps ------------------------------------------------------------

/// \brief Opaque type representing a component map type.
typedef struct wasmtime_component_map_type wasmtime_component_map_type_t;

/// \brief Clones a component map type.
///
/// The returned pointer must be deallocated with
/// `wasmtime_component_map_type_delete`.
WASM_API_EXTERN wasmtime_component_map_type_t *
wasmtime_component_map_type_clone(const wasmtime_component_map_type_t *ty);

/// \brief Compares two component map types for equality.
WASM_API_EXTERN bool
wasmtime_component_map_type_equal(const wasmtime_component_map_type_t *a,
const wasmtime_component_map_type_t *b);

/// \brief Deallocates a component map type.
WASM_API_EXTERN void
wasmtime_component_map_type_delete(wasmtime_component_map_type_t *ptr);

/// \brief Returns the key type of a component map type.
///
/// The returned type must be deallocated with
/// `wasmtime_component_valtype_delete`.
WASM_API_EXTERN void wasmtime_component_map_type_key(
const wasmtime_component_map_type_t *ty,
struct wasmtime_component_valtype_t *type_ret);

/// \brief Returns the value type of a component map type.
///
/// The returned type must be deallocated with
/// `wasmtime_component_valtype_delete`.
WASM_API_EXTERN void wasmtime_component_map_type_value(
const wasmtime_component_map_type_t *ty,
struct wasmtime_component_valtype_t *type_ret);

// ----------- valtype ---------------------------------------------------------

/// \brief Value of #wasmtime_component_valtype_kind_t meaning that
Expand Down Expand Up @@ -415,6 +452,9 @@ WASM_API_EXTERN bool wasmtime_component_stream_type_ty(
/// \brief Value of #wasmtime_component_valtype_kind_t meaning that
/// #wasmtime_component_valtype_t is an `error context` WIT type.
#define WASMTIME_COMPONENT_VALTYPE_ERROR_CONTEXT 25
/// \brief Value of #wasmtime_component_valtype_kind_t meaning that
/// #wasmtime_component_valtype_t is a `map` WIT type.
#define WASMTIME_COMPONENT_VALTYPE_MAP 26

/// \brief Discriminant used in #wasmtime_component_valtype_t::kind
typedef uint8_t wasmtime_component_valtype_kind_t;
Expand Down Expand Up @@ -457,6 +497,9 @@ typedef union wasmtime_component_valtype_union {
/// Field used if #wasmtime_component_valtype_t::kind is
/// #WASMTIME_COMPONENT_VALTYPE_STREAM
wasmtime_component_stream_type_t *stream;
/// Field used if #wasmtime_component_valtype_t::kind is
/// #WASMTIME_COMPONENT_VALTYPE_MAP
wasmtime_component_map_type_t *map;
} wasmtime_component_valtype_union_t;

/// \brief Represents a single value type in the component model.
Expand Down
40 changes: 40 additions & 0 deletions crates/c-api/include/wasmtime/component/types/val.hh
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ class StreamType {
std::optional<ValType> ty() const;
};

/**
* \brief Represents a component map type.
*/
class MapType {
WASMTIME_CLONE_EQUAL_WRAPPER(MapType, wasmtime_component_map_type);

/// Returns the key type of this map type.
ValType key() const;

/// Returns the value type of this map type.
ValType value() const;
};

/**
* \brief Represents a component value type.
*/
Expand Down Expand Up @@ -382,6 +395,12 @@ public:
ty.of.stream = stream.capi_release();
}

/// Creates a map value type.
ValType(MapType map) {
ty.kind = WASMTIME_COMPONENT_VALTYPE_MAP;
ty.of.map = map.capi_release();
}

/// Returns the kind of this value type.
wasmtime_component_valtype_kind_t kind() const { return ty.kind; }

Expand Down Expand Up @@ -481,6 +500,9 @@ public:
return ty.kind == WASMTIME_COMPONENT_VALTYPE_ERROR_CONTEXT;
}

/// Returns true if this is a map type.
bool is_map() const { return ty.kind == WASMTIME_COMPONENT_VALTYPE_MAP; }

/// Returns the list type, asserting that this is indeed a list.
const ListType &list() const {
assert(is_list());
Expand Down Expand Up @@ -553,6 +575,12 @@ public:
return *StreamType::from_capi(&ty.of.stream);
}

/// Returns the map type, asserting that this is indeed a map.
const MapType &map() const {
assert(is_map());
return *MapType::from_capi(&ty.of.map);
}

/// \brief Returns the underlying C API pointer.
const wasmtime_component_valtype_t *capi() const { return &ty; }
/// \brief Returns the underlying C API pointer.
Expand Down Expand Up @@ -640,6 +668,18 @@ inline std::optional<ValType> StreamType::ty() const {
return std::nullopt;
}

inline ValType MapType::key() const {
wasmtime_component_valtype_t type_ret;
wasmtime_component_map_type_key(ptr.get(), &type_ret);
return ValType(std::move(type_ret));
}

inline ValType MapType::value() const {
wasmtime_component_valtype_t type_ret;
wasmtime_component_map_type_value(ptr.get(), &type_ret);
return ValType(std::move(type_ret));
}

} // namespace component
} // namespace wasmtime

Expand Down
17 changes: 17 additions & 0 deletions crates/c-api/include/wasmtime/component/val.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,13 @@ typedef uint8_t wasmtime_component_valkind_t;
/// \brief Value of #wasmtime_component_valkind_t meaning that
/// #wasmtime_component_val_t is a resource
#define WASMTIME_COMPONENT_RESOURCE 21
/// \brief Value of #wasmtime_component_valkind_t meaning that
/// #wasmtime_component_val_t is a map
#define WASMTIME_COMPONENT_MAP 22

struct wasmtime_component_val;
struct wasmtime_component_valrecord_entry;
struct wasmtime_component_valmap_entry;

#define DECLARE_VEC(name, type) \
/** \brief A vec of a type */ \
Expand Down Expand Up @@ -296,6 +300,8 @@ DECLARE_VEC(wasmtime_component_valrecord,
struct wasmtime_component_valrecord_entry)
DECLARE_VEC(wasmtime_component_valtuple, struct wasmtime_component_val)
DECLARE_VEC(wasmtime_component_valflags, wasm_name_t)
DECLARE_VEC(wasmtime_component_valmap,
struct wasmtime_component_valmap_entry)

#undef DECLARE_VEC

Expand Down Expand Up @@ -366,6 +372,8 @@ typedef union {
wasmtime_component_valresult_t result;
/// Field used if #wasmtime_component_val_t::kind is #WASMTIME_COMPONENT_FLAGS
wasmtime_component_valflags_t flags;
/// Field used if #wasmtime_component_val_t::kind is #WASMTIME_COMPONENT_MAP
wasmtime_component_valmap_t map;
/// Field used if #wasmtime_component_val_t::kind is
/// #WASMTIME_COMPONENT_RESOURCE
wasmtime_component_resource_any_t *resource;
Expand All @@ -389,6 +397,15 @@ typedef struct wasmtime_component_valrecord_entry {
wasmtime_component_val_t val;
} wasmtime_component_valrecord_entry_t;

/// \brief A pair of a key and a value that represents one entry in a value
/// with kind #WASMTIME_COMPONENT_MAP
typedef struct wasmtime_component_valmap_entry {
/// The key of this entry
wasmtime_component_val_t key;
/// The value of this entry
wasmtime_component_val_t value;
} wasmtime_component_valmap_entry_t;

/// \brief Allocates a new `wasmtime_component_val_t` on the heap, initializing
/// it with the contents of `val`.
///
Expand Down
29 changes: 29 additions & 0 deletions crates/c-api/src/component/types/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub enum wasmtime_component_valtype_t {
Future(Box<wasmtime_component_future_type_t>),
Stream(Box<wasmtime_component_stream_type_t>),
ErrorContext,
Map(Box<wasmtime_component_map_type_t>),
}

impl From<Type> for wasmtime_component_valtype_t {
Expand Down Expand Up @@ -61,6 +62,7 @@ impl From<Type> for wasmtime_component_valtype_t {
Type::Borrow(ty) => Self::Borrow(Box::new(ty.into())),
Type::Future(ty) => Self::Future(Box::new(ty.into())),
Type::Stream(ty) => Self::Stream(Box::new(ty.into())),
Type::Map(ty) => Self::Map(Box::new(ty.into())),
Type::ErrorContext => Self::ErrorContext,
}
}
Expand Down Expand Up @@ -108,6 +110,33 @@ pub extern "C" fn wasmtime_component_list_type_element(
type_ret.write(ty.ty.ty().into());
}

type_wrapper! {
#[derive(PartialEq)]
pub struct wasmtime_component_map_type_t {
pub(crate) ty: Map,
}

clone: wasmtime_component_map_type_clone,
delete: wasmtime_component_map_type_delete,
equal: wasmtime_component_map_type_equal,
}

#[unsafe(no_mangle)]
pub extern "C" fn wasmtime_component_map_type_key(
ty: &wasmtime_component_map_type_t,
type_ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
) {
type_ret.write(ty.ty.key().into());
}

#[unsafe(no_mangle)]
pub extern "C" fn wasmtime_component_map_type_value(
ty: &wasmtime_component_map_type_t,
type_ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
) {
type_ret.write(ty.ty.value().into());
}

type_wrapper! {
#[derive(PartialEq)]
pub struct wasmtime_component_record_type_t {
Expand Down
42 changes: 42 additions & 0 deletions crates/c-api/src/component/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ crate::declare_vecs! {
copy: wasmtime_component_valflags_copy,
delete: wasmtime_component_valflags_delete,
)
(
name: wasmtime_component_valmap_t,
ty: wasmtime_component_valmap_entry_t,
new: wasmtime_component_valmap_new,
empty: wasmtime_component_valmap_new_empty,
uninit: wasmtime_component_valmap_new_uninit,
copy: wasmtime_component_valmap_copy,
delete: wasmtime_component_valmap_delete,
)
}

impl From<&wasmtime_component_vallist_t> for Vec<Val> {
Expand All @@ -63,13 +72,43 @@ impl From<&[Val]> for wasmtime_component_vallist_t {
}
}

impl From<&wasmtime_component_valmap_t> for Vec<(Val, Val)> {
fn from(value: &wasmtime_component_valmap_t) -> Self {
value
.as_slice()
.iter()
.map(|entry| (Val::from(&entry.key), Val::from(&entry.value)))
.collect()
}
}

impl From<&[(Val, Val)]> for wasmtime_component_valmap_t {
fn from(value: &[(Val, Val)]) -> Self {
value
.iter()
.map(|(k, v)| wasmtime_component_valmap_entry_t {
key: wasmtime_component_val_t::from(k),
value: wasmtime_component_val_t::from(v),
})
.collect::<Vec<_>>()
.into()
}
}

#[derive(Clone)]
#[repr(C)]
pub struct wasmtime_component_valrecord_entry_t {
name: wasm_name_t,
val: wasmtime_component_val_t,
}

#[derive(Clone, Default)]
#[repr(C)]
pub struct wasmtime_component_valmap_entry_t {
key: wasmtime_component_val_t,
value: wasmtime_component_val_t,
}

impl Default for wasmtime_component_valrecord_entry_t {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -233,6 +272,7 @@ pub enum wasmtime_component_val_t {
Option(Option<Box<Self>>),
Result(wasmtime_component_valresult_t),
Flags(wasmtime_component_valflags_t),
Map(wasmtime_component_valmap_t),
Resource(Box<wasmtime_component_resource_any_t>),
}

Expand Down Expand Up @@ -275,6 +315,7 @@ impl From<&wasmtime_component_val_t> for Val {
}
wasmtime_component_val_t::Result(x) => Val::Result(x.into()),
wasmtime_component_val_t::Flags(x) => Val::Flags(x.into()),
wasmtime_component_val_t::Map(x) => Val::Map(x.into()),
wasmtime_component_val_t::Resource(x) => Val::Resource(x.resource),
}
}
Expand Down Expand Up @@ -309,6 +350,7 @@ impl From<&Val> for wasmtime_component_val_t {
),
Val::Result(x) => wasmtime_component_val_t::Result(x.into()),
Val::Flags(x) => wasmtime_component_val_t::Flags(x.as_slice().into()),
Val::Map(x) => wasmtime_component_val_t::Map(x.as_slice().into()),
Val::Resource(resource_any) => {
wasmtime_component_val_t::Resource(Box::new(wasmtime_component_resource_any_t {
resource: *resource_any,
Expand Down
9 changes: 9 additions & 0 deletions crates/c-api/tests/component/types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ TEST(types, valtype_list) {
EXPECT_TRUE(elem.is_u8());
}

TEST(types, valtype_map) {
auto ty =
result("(component (import \"f\" (func (result (map u32 string)))))");
EXPECT_TRUE(ty.is_map());
auto map_ty = ty.map();
EXPECT_TRUE(map_ty.key().is_u32());
EXPECT_TRUE(map_ty.value().is_string());
}

TEST(types, valtype_record) {
auto ty = result(R"(
(component
Expand Down
5 changes: 1 addition & 4 deletions crates/environ/src/compile/module_environ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,7 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> {
Payload::ImportSection(imports) => {
self.validator.import_section(&imports)?;

let cnt = usize::try_from(imports.count()).unwrap();
self.result.module.initializers.reserve(cnt);

for entry in imports {
for entry in imports.into_imports() {
let import = entry?;
let ty = match import.ty {
TypeRef::Func(index) => {
Expand Down
Loading
Loading