Skip to content

Commit 09f6c1d

Browse files
Jasper-Bekkersemilio
authored andcommitted
Add support for wasm_import_module
1 parent 7d61f36 commit 09f6c1d

File tree

5 files changed

+61
-3
lines changed

5 files changed

+61
-3
lines changed

src/codegen/mod.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -3611,10 +3611,22 @@ impl CodeGenerator for Function {
36113611
attributes.push(attributes::link_name(link_name));
36123612
}
36133613

3614+
// Unfortunately this can't piggyback on the `attributes` list because
3615+
// the #[link(wasm_import_module)] needs to happen before the `extern "C"` block.
3616+
// it doesn't get picked up properly otherwise
3617+
let wasm_link_attribute =
3618+
ctx.options().wasm_import_module_name.as_ref().map(|name| {
3619+
quote! {
3620+
#[link(wasm_import_module = #name)]
3621+
}
3622+
});
3623+
36143624
let ident = ctx.rust_ident(canonical_name);
3615-
let tokens = quote!( extern #abi {
3616-
#(#attributes)*
3617-
pub fn #ident ( #( #args ),* ) #ret;
3625+
let tokens = quote!(
3626+
#wasm_link_attribute
3627+
extern #abi {
3628+
#(#attributes)*
3629+
pub fn #ident ( #( #args ),* ) #ret;
36183630
});
36193631
result.push(tokens);
36203632
}

src/lib.rs

+20
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,13 @@ impl Builder {
522522
output_vector.push("--use-array-pointers-in-arguments".into());
523523
}
524524

525+
if let Some(ref wasm_import_module_name) =
526+
self.options.wasm_import_module_name
527+
{
528+
output_vector.push("--wasm-import-module-name".into());
529+
output_vector.push(wasm_import_module_name.clone());
530+
}
531+
525532
self.options
526533
.opaque_types
527534
.get_items()
@@ -1491,6 +1498,15 @@ impl Builder {
14911498
self.options.array_pointers_in_arguments = doit;
14921499
self
14931500
}
1501+
1502+
/// Set the wasm import module name
1503+
pub fn wasm_import_module_name<T: Into<String>>(
1504+
mut self,
1505+
import_name: T,
1506+
) -> Self {
1507+
self.options.wasm_import_module_name = Some(import_name.into());
1508+
self
1509+
}
14941510
}
14951511

14961512
/// Configuration options for generated bindings.
@@ -1750,6 +1766,9 @@ struct BindgenOptions {
17501766

17511767
/// Decide if C arrays should be regular pointers in rust or array pointers
17521768
array_pointers_in_arguments: bool,
1769+
1770+
/// Wasm import module name.
1771+
wasm_import_module_name: Option<String>,
17531772
}
17541773

17551774
/// TODO(emilio): This is sort of a lie (see the error message that results from
@@ -1875,6 +1894,7 @@ impl Default for BindgenOptions {
18751894
no_copy_types: Default::default(),
18761895
no_hash_types: Default::default(),
18771896
array_pointers_in_arguments: false,
1897+
wasm_import_module_name: None,
18781898
}
18791899
}
18801900
}

src/options.rs

+10
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,11 @@ where
434434
Arg::with_name("use-array-pointers-in-arguments")
435435
.long("use-array-pointers-in-arguments")
436436
.help("Use `*const [T; size]` instead of `*const T` for C arrays"),
437+
Arg::with_name("wasm-import-module-name")
438+
.long("wasm-import-module-name")
439+
.value_name("name")
440+
.takes_value(true)
441+
.help("The name to be used in a #[link(wasm_import_module = ...)] statement")
437442
]) // .args()
438443
.get_matches_from(args);
439444

@@ -601,6 +606,11 @@ where
601606
builder = builder.array_pointers_in_arguments(true);
602607
}
603608

609+
if let Some(wasm_import_name) = matches.value_of("wasm-import-module-name")
610+
{
611+
builder = builder.wasm_import_module_name(wasm_import_name);
612+
}
613+
604614
if let Some(prefix) = matches.value_of("ctypes-prefix") {
605615
builder = builder.ctypes_prefix(prefix);
606616
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
9+
10+
#[link(wasm_import_module = "test-module")]
11+
extern "C" {
12+
pub fn test_function();
13+
}

tests/headers/wasm-import-module.h

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// bindgen-flags: --wasm-import-module-name test-module
2+
3+
void test_function();

0 commit comments

Comments
 (0)