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
5 changes: 5 additions & 0 deletions crates/bindgen-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ mod tests {

pub trait WorldGenerator {
fn generate(&mut self, name: &str, interfaces: &ComponentInterfaces, files: &mut Files) {
self.preprocess(name);
for (name, import) in interfaces.imports.iter() {
self.import(name, import, files);
}
Expand All @@ -576,6 +577,10 @@ pub trait WorldGenerator {
self.finish(name, interfaces, files);
}

fn preprocess(&mut self, name: &str) {
drop(name);
}

fn import(&mut self, name: &str, iface: &Interface, files: &mut Files);
fn export(&mut self, name: &str, iface: &Interface, files: &mut Files);
fn export_default(&mut self, name: &str, iface: &Interface, files: &mut Files);
Expand Down
2 changes: 1 addition & 1 deletion crates/gen-guest-c/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ heck = { workspace = true }
clap = { workspace = true, optional = true }

[dev-dependencies]
test-helpers = { path = '../test-helpers', default-features = false }
test-helpers = { path = '../test-helpers', default-features = false, features = ['macros'] }
45 changes: 17 additions & 28 deletions crates/gen-guest-c/src/component_type_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,14 @@ use heck::ToSnakeCase;
use wasm_encoder::{
CodeSection, CustomSection, Encode, Function, FunctionSection, Module, TypeSection,
};
use wit_bindgen_core::{wit_parser::Interface, Direction};
use wit_component::ComponentEncoder;
use wit_component::{ComponentEncoder, ComponentInterfaces};

pub fn linking_symbol(iface: &Interface, direction: Direction) -> String {
format!(
"__component_type_object_force_link_{}_{}",
iface.name.to_snake_case(),
match direction {
Direction::Import => "import",
Direction::Export => "export",
}
)
pub fn linking_symbol(name: &str) -> String {
let snake = name.to_snake_case();
format!("__component_type_object_force_link_{snake}")
}

pub fn object(iface: &Interface, direction: Direction) -> Result<Vec<u8>> {
pub fn object(name: &str, interfaces: &ComponentInterfaces) -> Result<Vec<u8>> {
let mut module = Module::new();

// Build a module with one function that's a "dummy function"
Expand All @@ -31,32 +24,28 @@ pub fn object(iface: &Interface, direction: Direction) -> Result<Vec<u8>> {
code.function(&Function::new([]));
module.section(&code);

let mut encoder = ComponentEncoder::default();
encoder = match direction {
Direction::Import => encoder.imports([iface.clone()])?,
Direction::Export => encoder.interface(iface.clone())?,
};
let mut encoder = ComponentEncoder::default()
.imports(interfaces.imports.values().cloned())?
.exports(interfaces.exports.values().cloned())?;

if let Some(default) = &interfaces.default {
encoder = encoder.interface(default.clone())?;
}

let data = encoder
.types_only(true)
.encode()
.with_context(|| format!("translating interface {} to component type", iface.name))?;
.with_context(|| format!("translating {name} to component type"))?;

// The custom section name here must start with "component-type" but
// otherwise is attempted to be unique here to ensure that this doesn't get
// concatenated to other custom sections by LLD by accident since LLD will
// concatenate custom sections of the same name.
let name = format!(
"component-type:{}:{}",
match direction {
Direction::Import => "import",
Direction::Export => "export",
},
iface.name
);
let section_name = format!("component-type:{name}",);

// Add our custom section
module.section(&CustomSection {
name: &name,
name: &section_name,
data: data.as_slice(),
});

Expand All @@ -69,7 +58,7 @@ pub fn object(iface: &Interface, direction: Direction) -> Result<Vec<u8>> {
subsection.push(0x00); // SYMTAB_FUNCTION
0u32.encode(&mut subsection); // flags
0u32.encode(&mut subsection); // index
linking_symbol(iface, direction).encode(&mut subsection); // name
linking_symbol(name).encode(&mut subsection); // name

data.push(0x08); // `WASM_SYMBOL_TABLE`
subsection.encode(&mut data);
Expand Down
Loading