From 18158f5456667f8d258abd1b4ad96625b3edf383 Mon Sep 17 00:00:00 2001 From: Jasper-Bekkers Date: Tue, 10 Dec 2019 16:12:16 +0100 Subject: [PATCH] Add support for wasm_import_module --- src/codegen/mod.rs | 18 ++++++++++++++--- src/lib.rs | 20 +++++++++++++++++++ src/options.rs | 10 ++++++++++ .../expectations/tests/wasm-import-module.rs | 13 ++++++++++++ tests/headers/wasm-import-module.h | 3 +++ 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 tests/expectations/tests/wasm-import-module.rs create mode 100644 tests/headers/wasm-import-module.h diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index d9ac4aa071..b1612c706a 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -3611,10 +3611,22 @@ impl CodeGenerator for Function { attributes.push(attributes::link_name(link_name)); } + // Unfortunately this can't piggyback on the `attributes` list because + // the #[link(wasm_import_module)] needs to happen before the `extern "C"` block. + // it doesn't get picked up properly otherwise + let wasm_link_attribute = + ctx.options().wasm_import_module_name.as_ref().map(|name| { + quote! { + #[link(wasm_import_module = #name)] + } + }); + let ident = ctx.rust_ident(canonical_name); - let tokens = quote!( extern #abi { - #(#attributes)* - pub fn #ident ( #( #args ),* ) #ret; + let tokens = quote!( + #wasm_link_attribute + extern #abi { + #(#attributes)* + pub fn #ident ( #( #args ),* ) #ret; }); result.push(tokens); } diff --git a/src/lib.rs b/src/lib.rs index 877dbd9adc..a818409be0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -522,6 +522,13 @@ impl Builder { output_vector.push("--use-array-pointers-in-arguments".into()); } + if let Some(ref wasm_import_module_name) = + self.options.wasm_import_module_name + { + output_vector.push("--wasm-import-module-name".into()); + output_vector.push(wasm_import_module_name.clone()); + } + self.options .opaque_types .get_items() @@ -1491,6 +1498,15 @@ impl Builder { self.options.array_pointers_in_arguments = doit; self } + + /// Set the wasm import module name + pub fn wasm_import_module_name>( + mut self, + import_name: T, + ) -> Self { + self.options.wasm_import_module_name = Some(import_name.into()); + self + } } /// Configuration options for generated bindings. @@ -1750,6 +1766,9 @@ struct BindgenOptions { /// Decide if C arrays should be regular pointers in rust or array pointers array_pointers_in_arguments: bool, + + /// Wasm import module name. + wasm_import_module_name: Option, } /// TODO(emilio): This is sort of a lie (see the error message that results from @@ -1875,6 +1894,7 @@ impl Default for BindgenOptions { no_copy_types: Default::default(), no_hash_types: Default::default(), array_pointers_in_arguments: false, + wasm_import_module_name: None, } } } diff --git a/src/options.rs b/src/options.rs index b09ba91905..01982f13cf 100644 --- a/src/options.rs +++ b/src/options.rs @@ -434,6 +434,11 @@ where Arg::with_name("use-array-pointers-in-arguments") .long("use-array-pointers-in-arguments") .help("Use `*const [T; size]` instead of `*const T` for C arrays"), + Arg::with_name("wasm-import-module-name") + .long("wasm-import-module-name") + .value_name("name") + .takes_value(true) + .help("The name to be used in a #[link(wasm_import_module = ...)] statement") ]) // .args() .get_matches_from(args); @@ -601,6 +606,11 @@ where builder = builder.array_pointers_in_arguments(true); } + if let Some(wasm_import_name) = matches.value_of("wasm-import-module-name") + { + builder = builder.wasm_import_module_name(wasm_import_name); + } + if let Some(prefix) = matches.value_of("ctypes-prefix") { builder = builder.ctypes_prefix(prefix); } diff --git a/tests/expectations/tests/wasm-import-module.rs b/tests/expectations/tests/wasm-import-module.rs new file mode 100644 index 0000000000..afbc58462a --- /dev/null +++ b/tests/expectations/tests/wasm-import-module.rs @@ -0,0 +1,13 @@ +/* automatically generated by rust-bindgen */ + +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +#[link(wasm_import_module = "test-module")] +extern "C" { + pub fn test_function(); +} \ No newline at end of file diff --git a/tests/headers/wasm-import-module.h b/tests/headers/wasm-import-module.h new file mode 100644 index 0000000000..db4fe85c8d --- /dev/null +++ b/tests/headers/wasm-import-module.h @@ -0,0 +1,3 @@ +// bindgen-flags: --wasm-import-module-name test-module + +void test_function(); \ No newline at end of file