Generate normal Rust functions for imported bindings (#410)

* Generate normal Rust functions for imported bindings

No need to use the C ABI, it just mixes up the documentation!

* Update expected webidl output
This commit is contained in:
Alex Crichton 2018-07-07 12:20:42 -05:00 committed by GitHub
parent 2d50d5209b
commit 791e69a5c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 39 deletions

View File

@ -701,7 +701,7 @@ impl ToTokens for ast::ImportFunction {
#(#attrs)* #(#attrs)*
#[allow(bad_style)] #[allow(bad_style)]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#vis extern fn #rust_name(#me #(#arguments),*) #ret { #vis fn #rust_name(#me #(#arguments),*) #ret {
::wasm_bindgen::__rt::link_this_library(); ::wasm_bindgen::__rt::link_this_library();
#[wasm_import_module = "__wbindgen_placeholder__"] #[wasm_import_module = "__wbindgen_placeholder__"]
extern { extern {

View File

@ -1,8 +1,11 @@
use diff; use std::env;
use env_logger; use std::fs::File;
use std::io::{self, Write}; use std::io::{self, Write};
use std::process; use std::process;
use std::sync::{Once, ONCE_INIT}; use std::sync::{Once, ONCE_INIT};
use diff;
use env_logger;
use wb_webidl; use wb_webidl;
fn rustfmt<S: Into<String>>(source: S) -> (String, String) { fn rustfmt<S: Into<String>>(source: S) -> (String, String) {
@ -82,7 +85,7 @@ The latest `rustfmt` is required to run the `wasm-bindgen` test suite. Install
(formatted, stderr) (formatted, stderr)
} }
fn strip_wasm_bindgen_generated(source: String) -> String { fn strip_wasm_bindgen_generated(source: &str) -> String {
let lines: Vec<_> = source let lines: Vec<_> = source
.lines() .lines()
.filter(|l| !l.contains("__WASM_BINDGEN_GENERATED")) .filter(|l| !l.contains("__WASM_BINDGEN_GENERATED"))
@ -90,7 +93,7 @@ fn strip_wasm_bindgen_generated(source: String) -> String {
lines.join("\n") lines.join("\n")
} }
pub fn assert_compile(webidl: &str, expected: &str) { pub fn assert_compile(webidl: &str, expected: &str, expected_file: &str) {
static INIT_ENV_LOGGER: Once = ONCE_INIT; static INIT_ENV_LOGGER: Once = ONCE_INIT;
INIT_ENV_LOGGER.call_once(|| { INIT_ENV_LOGGER.call_once(|| {
env_logger::init(); env_logger::init();
@ -98,16 +101,24 @@ pub fn assert_compile(webidl: &str, expected: &str) {
let actual = wb_webidl::compile(webidl).expect("should compile the webidl source OK"); let actual = wb_webidl::compile(webidl).expect("should compile the webidl source OK");
let (actual, actual_stderr) = rustfmt(actual); let (actual_orig, actual_stderr) = rustfmt(actual);
let (expected, expected_stderr) = rustfmt(expected); let (expected, expected_stderr) = rustfmt(expected);
let actual = strip_wasm_bindgen_generated(actual); let actual = strip_wasm_bindgen_generated(&actual_orig);
let expected = strip_wasm_bindgen_generated(expected); let expected = strip_wasm_bindgen_generated(&expected);
if expected == actual { if expected == actual {
return; return;
} }
if env::var("UPDATE_EXPECTED").is_ok() {
File::create(expected_file)
.unwrap()
.write_all(actual_orig.as_bytes())
.unwrap();
return
}
eprintln!("rustfmt(expected) stderr:"); eprintln!("rustfmt(expected) stderr:");
eprintln!("{}", expected_stderr); eprintln!("{}", expected_stderr);
eprintln!(); eprintln!();
@ -149,7 +160,13 @@ macro_rules! assert_compile {
stringify!($test_name), stringify!($test_name),
".rs" ".rs"
)); ));
$crate::assert_compile(webidl_source, expected_output); let expected_file = concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/expected/",
stringify!($test_name),
".rs"
);
$crate::assert_compile(webidl_source, expected_output, expected_file);
} }
}; };
} }

File diff suppressed because one or more lines are too long

View File

@ -418,7 +418,8 @@ fn rename_setter_getter() {
#[wasm_bindgen] #[wasm_bindgen]
pub fn run() { pub fn run() {
let a = Foo::new(); let x: fn() -> Foo = Foo::new;
let a = x();
assert_eq!(a.test(), 1); assert_eq!(a.test(), 1);
a.another(2); a.another(2);
assert_eq!(a.test(), 2); assert_eq!(a.test(), 2);