Skip to content

Add support for wasm_import_module #1691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 11, 2019
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
18 changes: 15 additions & 3 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
20 changes: 20 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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<T: Into<String>>(
mut self,
import_name: T,
) -> Self {
self.options.wasm_import_module_name = Some(import_name.into());
self
}
}

/// Configuration options for generated bindings.
Expand Down Expand Up @@ -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<String>,
}

/// TODO(emilio): This is sort of a lie (see the error message that results from
Expand Down Expand Up @@ -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,
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
Expand Down
13 changes: 13 additions & 0 deletions tests/expectations/tests/wasm-import-module.rs
Original file line number Diff line number Diff line change
@@ -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();
}
3 changes: 3 additions & 0 deletions tests/headers/wasm-import-module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// bindgen-flags: --wasm-import-module-name test-module

void test_function();