WebAssembly FFI shells (#322)

* Allow imgui-rs to generate wasm-ffi bindings

* Missing wasm_bindings.rs file

* Update wasm bindings

* Take wasm import name from environment variable

* Remove debug message

* Formatting
This commit is contained in:
Jasper Bekkers 2020-05-18 20:54:30 +02:00 committed by GitHub
parent 48eaf63e5f
commit d90fe8171e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 8551 additions and 25 deletions

View File

@ -22,6 +22,9 @@ imgui-sys = { version = "0.4.0-pre", path = "imgui-sys" }
lazy_static = "1.1"
parking_lot = "0.10"
[features]
wasm = ["imgui-sys/wasm"]
[dev-dependencies]
memoffset = "0.5"

View File

@ -13,4 +13,4 @@ bindgen = "0.53"
failure = "0.1"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
serde_json = "1.0"

View File

@ -77,7 +77,10 @@ fn parse_whitelist<R: Read>(
})
}
pub fn generate_bindings<P: AsRef<Path>>(cimgui_path: &P) -> Result<Bindings, Error> {
pub fn generate_bindings<P: AsRef<Path>>(
cimgui_path: &P,
wasm_import_name: Option<String>,
) -> Result<Bindings, Error> {
let cimgui_output_path = cimgui_path.as_ref().join("generator").join("output");
let structs_and_enums = File::open(cimgui_output_path.join("structs_and_enums.json"))?;
let definitions = File::open(cimgui_output_path.join("definitions.json"))?;
@ -105,6 +108,11 @@ pub fn generate_bindings<P: AsRef<Path>>(cimgui_path: &P) -> Result<Bindings, Er
.impl_debug(true)
.rustfmt_bindings(true)
.clang_arg("-DCIMGUI_DEFINE_ENUMS_AND_STRUCTS=1");
if let Some(name) = wasm_import_name {
builder = builder.wasm_import_module_name(name);
}
for e in whitelist.structs {
builder = builder.whitelist_type(format!("^{}", e));
}

View File

@ -10,11 +10,25 @@ fn main() {
.join("imgui-sys")
.canonicalize()
.expect("Failed to find imgui-sys directory");
let bindings = generate_bindings(&sys_path.join("third-party").join("cimgui"))
let bindings = generate_bindings(&sys_path.join("third-party").join("cimgui"), None)
.expect("Failed to generate bindings");
let output_path = sys_path.join("src").join("bindings.rs");
bindings
.write_to_file(&output_path)
.expect("Failed to write bindings");
println!("Wrote bindings to {}", output_path.to_string_lossy());
let wasm_ffi_import_name = option_env!("IMGUI_RS_WASM_IMPORT_NAME")
.map(|s| s.to_string())
.or(Some("imgui-sys".to_string()));
let wasm_bindings = generate_bindings(
&sys_path.join("third-party").join("cimgui"),
wasm_ffi_import_name,
)
.expect("Failed to generate bindings");
let output_path = sys_path.join("src").join("wasm_bindings.rs");
wasm_bindings
.write_to_file(&output_path)
.expect("Failed to write wasm bindings");
}

View File

@ -12,3 +12,7 @@ build = "build.rs"
[build-dependencies]
cc = "1.0"
[features]
default = []
wasm = []

View File

@ -1,3 +1,5 @@
#![allow(dead_code)]
use std::fs;
use std::io;
@ -23,19 +25,22 @@ fn assert_file_exists(path: &str) -> io::Result<()> {
}
fn main() -> io::Result<()> {
let mut build = cc::Build::new();
build.cpp(true);
// Disabled due to linking issues
build
.define("CIMGUI_NO_EXPORT", None)
.define("IMGUI_DISABLE_WIN32_FUNCTIONS", None)
.define("IMGUI_DISABLE_OSX_FUNCTIONS", None);
#[cfg(not(feature = "wasm"))]
{
let mut build = cc::Build::new();
build.cpp(true);
// Disabled due to linking issues
build
.define("CIMGUI_NO_EXPORT", None)
.define("IMGUI_DISABLE_WIN32_FUNCTIONS", None)
.define("IMGUI_DISABLE_OSX_FUNCTIONS", None);
build.flag_if_supported("-Wno-return-type-c-linkage");
for path in &CPP_FILES {
assert_file_exists(path)?;
build.file(path);
build.flag_if_supported("-Wno-return-type-c-linkage");
for path in &CPP_FILES {
assert_file_exists(path)?;
build.file(path);
}
build.compile("libcimgui.a");
}
build.compile("libcimgui.a");
Ok(())
}

View File

@ -1,5 +1,13 @@
#[cfg(feature = "wasm")]
mod wasm_bindings;
#[cfg(feature = "wasm")]
pub use crate::wasm_bindings::*;
#[cfg(not(feature = "wasm"))]
mod bindings;
#[cfg(not(feature = "wasm"))]
pub use crate::bindings::*;
impl ImVec2 {

File diff suppressed because it is too large Load Diff

View File

@ -11,7 +11,7 @@ macro_rules! impl_display_format {
self.display_format = display_format;
self
}
}
};
}
macro_rules! impl_speed {
@ -21,7 +21,7 @@ macro_rules! impl_speed {
self.speed = value;
self
}
}
};
}
macro_rules! impl_power {
@ -31,7 +31,7 @@ macro_rules! impl_power {
self.power = value;
self
}
}
};
}
macro_rules! impl_min_max {
@ -47,7 +47,7 @@ macro_rules! impl_min_max {
self.max = value;
self
}
}
};
}
#[must_use]

View File

@ -52,7 +52,8 @@ macro_rules! impl_text_flags {
#[inline]
pub fn callback_completion(mut self, value: bool) -> Self {
self.flags.set(ImGuiInputTextFlags::CallbackCompletion, value);
self.flags
.set(ImGuiInputTextFlags::CallbackCompletion, value);
self
}
@ -70,7 +71,8 @@ macro_rules! impl_text_flags {
#[inline]
pub fn callback_char_filter(mut self, value: bool) -> Self {
self.flags.set(ImGuiInputTextFlags::CallbackCharFilter, value);
self.flags
.set(ImGuiInputTextFlags::CallbackCharFilter, value);
self
}
@ -88,7 +90,8 @@ macro_rules! impl_text_flags {
#[inline]
pub fn no_horizontal_scroll(mut self, value: bool) -> Self {
self.flags.set(ImGuiInputTextFlags::NoHorizontalScroll, value);
self.flags
.set(ImGuiInputTextFlags::NoHorizontalScroll, value);
self
}
@ -115,7 +118,7 @@ macro_rules! impl_text_flags {
self.flags.set(ImGuiInputTextFlags::NoUndoRedo, value);
self
}
}
};
}
macro_rules! impl_step_params {
@ -131,7 +134,7 @@ macro_rules! impl_step_params {
self.step_fast = value;
self
}
}
};
}
extern "C" fn resize_callback(data: *mut sys::ImGuiInputTextCallbackData) -> c_int {