Skip to content
Merged
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
277 changes: 117 additions & 160 deletions Cargo.lock

Large diffs are not rendered by default.

42 changes: 26 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "wit-bindgen-cli"
authors = ["Alex Crichton <alex@alexcrichton.com>"]
version = "0.6.0"
version = "0.7.0"
edition = { workspace = true }
repository = 'https://github.com/bytecodealliance/wit-bindgen'
license = "Apache-2.0 WITH LLVM-exception"
Expand Down Expand Up @@ -29,20 +29,20 @@ clap = { version = "4.2.2", features = ["derive"] }
env_logger = "0.10.0"
indexmap = "1.9.3"

wasm-encoder = "0.28.0"
wasm-metadata = "0.7.0"
wat = "1.0.65"
wit-parser = "0.7.1"
wit-component = "0.10.0"
wasm-encoder = "0.29.0"
wasm-metadata = "0.8.0"
wat = "1.0.66"
wit-parser = "0.8.0"
wit-component = "0.11.0"

wit-bindgen-core = { path = 'crates/core', version = '0.6.0' }
wit-bindgen-c = { path = 'crates/c', version = '0.6.0' }
wit-bindgen-rust = { path = "crates/rust", version = "0.6.0" }
wit-bindgen-teavm-java = { path = 'crates/teavm-java', version = '0.6.0' }
wit-bindgen-go = { path = 'crates/go', version = '0.4.0' }
wit-bindgen-markdown = { path = 'crates/markdown', version = '0.6.0' }
wit-bindgen-rust-lib = { path = 'crates/rust-lib', version = '0.6.0' }
wit-bindgen = { path = 'crates/guest-rust', version = '0.6.0', default-features = false }
wit-bindgen-core = { path = 'crates/core', version = '0.7.0' }
wit-bindgen-c = { path = 'crates/c', version = '0.7.0' }
wit-bindgen-rust = { path = "crates/rust", version = "0.7.0" }
wit-bindgen-teavm-java = { path = 'crates/teavm-java', version = '0.7.0' }
wit-bindgen-go = { path = 'crates/go', version = '0.5.0' }
wit-bindgen-markdown = { path = 'crates/markdown', version = '0.7.0' }
wit-bindgen-rust-lib = { path = 'crates/rust-lib', version = '0.7.0' }
wit-bindgen = { path = 'crates/guest-rust', version = '0.7.0', default-features = false }
wit-bindgen-rust-macro-shared = { path = 'crates/rust-macro-shared', version = '0.3.0' }

[[bin]]
Expand All @@ -62,7 +62,14 @@ wit-component = { workspace = true }
wasm-encoder = { workspace = true }

[features]
default = ['c', 'rust', 'markdown', 'teavm-java', 'go']
default = [
'c',
'rust',
'markdown',
# TODO: fix these code generators and turn them back on
# 'teavm-java',
# 'go',
]
c = ['dep:wit-bindgen-c']
rust = ['dep:wit-bindgen-rust']
markdown = ['dep:wit-bindgen-markdown']
Expand All @@ -71,6 +78,9 @@ go = ['dep:wit-bindgen-go']

[dev-dependencies]
heck = { workspace = true }
wasmtime = { version = "9", features = ['component-model'] }
wasmtime = { version = "10", features = ['component-model'] }
test-artifacts = { path = 'crates/test-rust-wasm/artifacts' }
wit-parser = { workspace = true }

[patch.crates-io]
wasmtime = { git = 'https://github.com/bytecodealliance/wasmtime' }
2 changes: 1 addition & 1 deletion crates/c/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "wit-bindgen-c"
authors = ["Alex Crichton <alex@alexcrichton.com>"]
version = "0.6.0"
version = "0.7.0"
edition.workspace = true
repository = 'https://github.com/bytecodealliance/wit-bindgen'
license = "Apache-2.0 WITH LLVM-exception"
Expand Down
77 changes: 54 additions & 23 deletions crates/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct C {
// This is subsequently used to generate a namespace for each type that's
// used, but only in the case that the interface itself doesn't already have
// an original name.
interface_names: HashMap<InterfaceId, String>,
interface_names: HashMap<InterfaceId, WorldKey>,

// Interfaces who have had their types printed.
//
Expand Down Expand Up @@ -109,11 +109,11 @@ impl WorldGenerator for C {
fn import_interface(
&mut self,
resolve: &Resolve,
name: &str,
name: &WorldKey,
id: InterfaceId,
_files: &mut Files,
) {
let prev = self.interface_names.insert(id, name.to_string());
let prev = self.interface_names.insert(id, name.clone());
assert!(prev.is_none());
let mut gen = self.interface(resolve, true);
gen.interface = Some(id);
Expand All @@ -123,6 +123,7 @@ impl WorldGenerator for C {

for (i, (_name, func)) in resolve.interfaces[id].functions.iter().enumerate() {
if i == 0 {
let name = resolve.name_world_key(name);
uwriteln!(gen.src.h_fns, "\n// Imported Functions from `{name}`");
}
gen.import(Some(name), func);
Expand Down Expand Up @@ -154,11 +155,11 @@ impl WorldGenerator for C {
fn export_interface(
&mut self,
resolve: &Resolve,
name: &str,
name: &WorldKey,
id: InterfaceId,
_files: &mut Files,
) {
self.interface_names.insert(id, name.to_string());
self.interface_names.insert(id, name.clone());
let mut gen = self.interface(resolve, false);
gen.interface = Some(id);
if gen.gen.interfaces_with_types_printed.insert(id) {
Expand All @@ -167,6 +168,7 @@ impl WorldGenerator for C {

for (i, (_name, func)) in resolve.interfaces[id].functions.iter().enumerate() {
if i == 0 {
let name = resolve.name_world_key(name);
uwriteln!(gen.src.h_fns, "\n// Exported Functions from `{name}`");
}
gen.export(func, Some(name));
Expand Down Expand Up @@ -687,16 +689,23 @@ impl C {
fn owner_namespace(&mut self, resolve: &Resolve, id: TypeId) -> String {
let ty = &resolve.types[id];
match ty.owner {
// If this type belongs to an interface, then use that interface's
// original name if it's listed in the source. Otherwise if it's an
// "anonymous" interface as part of a world use the name of the
// import/export in the world which would have been stored in
// `interface_names`.
TypeOwner::Interface(owner) => resolve.interfaces[owner]
.name
.as_ref()
.map(|s| s.to_snake_case())
.unwrap_or_else(|| self.interface_names[&owner].to_snake_case()),
TypeOwner::Interface(owner) => {
let key = &self.interface_names[&owner];
match key {
WorldKey::Name(name) => name.to_snake_case(),
WorldKey::Interface(id) => {
let mut ns = String::new();
let iface = &resolve.interfaces[*id];
let pkg = &resolve.packages[iface.package.unwrap()];
ns.push_str(&pkg.name.namespace.to_snake_case());
ns.push_str("_");
ns.push_str(&pkg.name.name.to_snake_case());
ns.push_str("_");
ns.push_str(&iface.name.as_ref().unwrap().to_snake_case());
ns
}
}
}

TypeOwner::World(owner) => resolve.worlds[owner].name.to_snake_case(),

Expand Down Expand Up @@ -1079,12 +1088,30 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
}

impl InterfaceGenerator<'_> {
fn c_func_name(&self, interface_name: Option<&str>, func: &Function) -> String {
let ns = interface_name.unwrap_or(&self.gen.world);
format!("{}_{}", ns.to_snake_case(), func.name.to_snake_case())
fn c_func_name(&self, interface_name: Option<&WorldKey>, func: &Function) -> String {
let mut name = String::new();
match interface_name {
Some(WorldKey::Name(k)) => name.push_str(&k.to_snake_case()),
Some(WorldKey::Interface(id)) => {
if !self.in_import {
name.push_str("exports_");
}
let iface = &self.resolve.interfaces[*id];
let pkg = &self.resolve.packages[iface.package.unwrap()];
name.push_str(&pkg.name.namespace.to_snake_case());
name.push_str("_");
name.push_str(&pkg.name.name.to_snake_case());
name.push_str("_");
name.push_str(&iface.name.as_ref().unwrap().to_snake_case());
}
None => name.push_str(&self.gen.world.to_snake_case()),
}
name.push_str("_");
name.push_str(&func.name.to_snake_case());
name
}

fn import(&mut self, interface_name: Option<&str>, func: &Function) {
fn import(&mut self, interface_name: Option<&WorldKey>, func: &Function) {
self.docs(&func.docs, SourceType::HFns);
let sig = self.resolve.wasm_signature(AbiVariant::GuestImport, func);

Expand All @@ -1096,7 +1123,10 @@ impl InterfaceGenerator<'_> {
uwriteln!(
self.src.c_fns,
"__attribute__((__import_module__(\"{}\"), __import_name__(\"{}\")))",
interface_name.unwrap_or("$root"),
match interface_name {
Some(name) => self.resolve.name_world_key(name),
None => "$root".to_string(),
},
func.name
);
let name = self.c_func_name(interface_name, func);
Expand Down Expand Up @@ -1194,10 +1224,11 @@ impl InterfaceGenerator<'_> {
self.src.c_adapters("}\n");
}

fn export(&mut self, func: &Function, interface_name: Option<&str>) {
fn export(&mut self, func: &Function, interface_name: Option<&WorldKey>) {
let sig = self.resolve.wasm_signature(AbiVariant::GuestExport, func);

let export_name = func.core_export_name(interface_name);
let core_module_name = interface_name.map(|s| self.resolve.name_world_key(s));
let export_name = func.core_export_name(core_module_name.as_deref());

// Print the actual header for this function into the header file, and
// it's what we'll be calling.
Expand Down Expand Up @@ -1279,7 +1310,7 @@ impl InterfaceGenerator<'_> {

fn print_sig(
&mut self,
interface_name: Option<&str>,
interface_name: Option<&WorldKey>,
func: &Function,
sig_flattening: bool,
) -> CSig {
Expand Down
2 changes: 1 addition & 1 deletion crates/core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "wit-bindgen-core"
authors = ["Alex Crichton <alex@alexcrichton.com>"]
version = "0.6.0"
version = "0.7.0"
edition.workspace = true
repository = 'https://github.com/bytecodealliance/wit-bindgen'
license = "Apache-2.0 WITH LLVM-exception"
Expand Down
17 changes: 12 additions & 5 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,20 @@ pub trait WorldGenerator {
let world = &resolve.worlds[id];
self.preprocess(resolve, id);

fn unwrap_name(key: &WorldKey) -> &str {
match key {
WorldKey::Name(name) => name,
WorldKey::Interface(_) => panic!("unexpected interface key"),
}
}

let mut funcs = Vec::new();
let mut types = Vec::new();
for (name, import) in world.imports.iter() {
match import {
WorldItem::Function(f) => funcs.push((name.as_str(), f)),
WorldItem::Function(f) => funcs.push((unwrap_name(name), f)),
WorldItem::Interface(id) => self.import_interface(resolve, name, *id, files),
WorldItem::Type(id) => types.push((name.as_str(), *id)),
WorldItem::Type(id) => types.push((unwrap_name(name), *id)),
}
}
if !types.is_empty() {
Expand All @@ -418,7 +425,7 @@ pub trait WorldGenerator {
let mut interfaces = Vec::new();
for (name, export) in world.exports.iter() {
match export {
WorldItem::Function(f) => funcs.push((name.as_str(), f)),
WorldItem::Function(f) => funcs.push((unwrap_name(name), f)),
WorldItem::Interface(id) => interfaces.push((name, id)),
WorldItem::Type(_) => unreachable!(),
}
Expand All @@ -440,14 +447,14 @@ pub trait WorldGenerator {
fn import_interface(
&mut self,
resolve: &Resolve,
name: &str,
name: &WorldKey,
iface: InterfaceId,
files: &mut Files,
);
fn export_interface(
&mut self,
resolve: &Resolve,
name: &str,
name: &WorldKey,
iface: InterfaceId,
files: &mut Files,
);
Expand Down
2 changes: 1 addition & 1 deletion crates/go/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "wit-bindgen-go"
authors = ["Mossaka <duibao55328@gmail.com>"]
version = "0.4.0"
version = "0.5.0"
edition.workspace = true
repository = 'https://github.com/bytecodealliance/wit-bindgen'
license = "Apache-2.0 WITH LLVM-exception"
Expand Down
Loading