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
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[workspace]
resolver = "3"
members = ["cmd/*", "examples/*"]
# The examples are wasm guests. Built for the host, a guest that exports a
# versioned interface does not link: its export names
# (`ns:pkg/iface@1.0.0#func`) are not valid in a linker version script. A
# bare `cargo build` builds the tool; `-p example-<name> --target
# wasm32-unknown-unknown` builds a guest.
default-members = ["cmd/*"]
23 changes: 17 additions & 6 deletions cmd/gravity/src/codegen/bindings.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::BTreeMap;

use genco::{prelude::*, tokens::Tokens};
use wit_bindgen_core::wit_parser::{Resolve, SizeAlign, World};
use wit_bindgen_core::wit_parser::{Resolve, Result_, SizeAlign, Type, TypeDefKind, World};

use crate::{
codegen::{
Expand Down Expand Up @@ -56,7 +56,7 @@ impl<'a> Bindings<'a> {
pub fn generate(&mut self) {
let (imports, chains) = self.generate_imports();
self.generate_factory(&imports, chains);
self.generate_exports(&imports.instance_name);
self.generate_exports(&imports);
}

/// Generates the imports for the bindings.
Expand All @@ -77,24 +77,35 @@ impl<'a> Bindings<'a> {
analyzed_imports: &AnalyzedImports,
import_chains: BTreeMap<String, Tokens<Go>>,
) {
let typed_result_errors = self.resolve.types.iter().any(|(_, def)| {
matches!(
def.kind,
TypeDefKind::Result(Result_ {
err: Some(err),
..
}) if err != Type::String
)
});
let config = FactoryConfig {
analyzed_imports,
import_chains,
wasm_var_name: &self.raw_wasm_var,
typed_result_errors,
};
FactoryGenerator::new(config).format_into(&mut self.out)
}

/// Generates all exports for the world.
///
/// Note: for now this only generates functions; types and interfaces are
/// still TODO
fn generate_exports(&mut self, instance: &GoIdentifier) {
/// Functions the world exports become instance methods; an exported
/// interface becomes its types and a Go type holding its functions.
fn generate_exports(&mut self, imports: &AnalyzedImports) {
let config = ExportConfig {
instance,
instance: &imports.instance_name,
world: self.world,
resolve: self.resolve,
sizes: self.sizes,
analyzed_imports: imports,
};
ExportGenerator::new(config).format_into(&mut self.out)
}
Expand Down
Loading
Loading