Skip to content

Commit

Permalink
address review feedback
Browse files Browse the repository at this point in the history
- Create a named constant `BARE_FUNCTION_NAMESPACE`
- Swap `Source` instances instead of trying to merge them

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
  • Loading branch information
dicej committed Aug 22, 2023
1 parent d31a7bf commit c0d2a39
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 36 deletions.
23 changes: 15 additions & 8 deletions rust/bindgen/src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ use wit_bindgen_core::abi::{self, AbiVariant, Bindgen, Bitcast, Instruction, Lif
use wit_component::DecodedWasm;
use wit_parser::*;

/// The name under which to group "bare" host functions (i.e. those imported directly by the world rather than via
/// an interface).
const BARE_FUNCTION_NAMESPACE: &str = "host";

#[derive(Default)]
pub struct WasmtimePy {
// `$out_dir/__init__.py`
Expand Down Expand Up @@ -144,7 +148,7 @@ impl WasmtimePy {
}
}
if !root_functions.is_empty() {
self.import_functions(&resolve, "host", &root_functions);
self.import_functions(&resolve, &root_functions);
}

for (world_key, export) in world.exports.clone().into_iter() {
Expand Down Expand Up @@ -375,10 +379,14 @@ impl WasmtimePy {
self.imports.push(name.to_string());
}

fn import_functions(&mut self, resolve: &Resolve, world_name: &str, functions: &[Function]) {
fn import_functions(&mut self, resolve: &Resolve, functions: &[Function]) {
let src = mem::take(&mut self.imports_init);
let mut gen = self.interface(resolve);
gen.src = src;

let camel = world_name.to_upper_camel_case().escape();
let camel = BARE_FUNCTION_NAMESPACE.to_upper_camel_case();
gen.src.pyimport("typing", "Protocol");
gen.src.pyimport("abc", "abstractmethod");
uwriteln!(gen.src, "class {camel}(Protocol):");
gen.src.indent();
for func in functions.iter() {
Expand All @@ -392,10 +400,7 @@ impl WasmtimePy {
gen.src.dedent();
gen.src.push_str("\n");

let src = gen.src;
self.imports_init.pyimport("typing", "Protocol");
self.imports_init.pyimport("abc", "abstractmethod");
self.imports_init.push_src(src);
self.imports_init = gen.src;
}

fn export_interface(
Expand Down Expand Up @@ -425,7 +430,9 @@ impl WasmtimePy {
uwriteln!(self.imports_init, "class {camel}Imports:");
self.imports_init.indent();
if has_root_imports {
self.imports_init.push_str("host: Host\n");
let camel = BARE_FUNCTION_NAMESPACE.to_upper_camel_case();
let snake = BARE_FUNCTION_NAMESPACE.to_snake_case();
uwriteln!(self.imports_init, "{snake}: {camel}");
}
for import in self.imports.iter() {
let snake = import.to_snake_case().escape();
Expand Down
22 changes: 0 additions & 22 deletions rust/bindgen/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,6 @@ impl PyImports {
push(&mut self.typing_imports, module, name.into())
}

pub fn merge(&mut self, imports: PyImports) {
for (module, names) in imports.pyimports {
if let Some(names) = names {
for name in names {
self.pyimport(&module, Some(name.as_str()));
}
} else {
self.pyimport(&module, None);
}
}

for (module, names) in imports.typing_imports {
if let Some(names) = names {
for name in names {
self.typing_import(&module, Some(name.as_str()));
}
} else {
self.typing_import(&module, None);
}
}
}

pub fn is_empty(&self) -> bool {
self.pyimports.is_empty()
}
Expand Down
6 changes: 0 additions & 6 deletions rust/bindgen/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,6 @@ impl Source {
pub fn replace_body(&mut self, body: Body) -> String {
mem::replace(&mut self.body, body).contents
}

pub fn push_src(&mut self, src: Source) {
assert!(self.body.indent == src.body.indent);
self.imports.merge(src.imports);
self.body.contents.push_str(&src.body.contents);
}
}

impl Write for Source {
Expand Down

0 comments on commit c0d2a39

Please sign in to comment.