Skip to content

Commit

Permalink
Use conventional meta-item syntax for attributes
Browse files Browse the repository at this point in the history
Using the conventional form for attributes, which is compatible with
rustfmt and syn's meta-item parsing. This change is breaking and
substantial, but the necessary edits are largely mechanical.

With meta-items, it becomes possible to offload attribute parsing and
validation to `darling`. This offload fixes rustwasm#2874, as `darling` will
automatically generate errors for unexpected meta-item names. This also
makes it easier for people who are new to the codebase to understand
how to add new options.
  • Loading branch information
Ted Driggs committed Apr 28, 2022
1 parent e8ea739 commit 3078ea5
Show file tree
Hide file tree
Showing 1,335 changed files with 55,415 additions and 11,247 deletions.
16 changes: 8 additions & 8 deletions benchmarks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ use web_sys::Node;

#[wasm_bindgen(raw_module = "../globals.js")]
extern "C" {
#[wasm_bindgen(js_name = jsthunk)]
#[wasm_bindgen(js_name = "jsthunk")]
fn js_thunk();
#[wasm_bindgen(js_name = add)]
#[wasm_bindgen(js_name = "add")]
fn js_add(a: i32, b: i32) -> i32;

pub type Foo;
#[wasm_bindgen(method, final, js_name = bar)]
#[wasm_bindgen(method, final, js_name = "bar")]
fn bar_final(this: &Foo);
#[wasm_bindgen(method, structural, js_name = bar)]
#[wasm_bindgen(method, structural, js_name = "bar")]
fn bar_structural(this: &Foo);

#[wasm_bindgen(js_name = jsthunk)]
#[wasm_bindgen(js_name = "jsthunk")]
fn doesnt_throw();
#[wasm_bindgen(catch, js_name = jsthunk)]
#[wasm_bindgen(catch, js_name = "jsthunk")]
fn doesnt_throw_catch() -> Result<(), JsValue>;
}

Expand Down Expand Up @@ -102,9 +102,9 @@ pub fn call_doesnt_throw_with_catch_n_times(n: usize) {
extern "C" {
pub type Element;

#[wasm_bindgen(method, js_name = firstChild, final, getter)]
#[wasm_bindgen(method, js_name = "firstChild", final, getter)]
fn first_child_final(this: &Element) -> Element;
#[wasm_bindgen(method, js_name = firstChild, structural, getter)]
#[wasm_bindgen(method, js_name = "firstChild", structural, getter)]
fn first_child_structural(this: &Element) -> Element;
}

Expand Down
1 change: 1 addition & 0 deletions crates/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extra-traits = ["syn/extra-traits"]

[dependencies]
bumpalo = "3.0.0"
darling = "0.14"
lazy_static = "1.0.2"
log = "0.4"
proc-macro2 = "1.0"
Expand Down
13 changes: 13 additions & 0 deletions crates/backend/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ enum Repr {
text: String,
span: Option<(Span, Span)>,
},
Darling(TokenStream),
SynError(Error),
Multi {
diagnostics: Vec<Diagnostic>,
Expand Down Expand Up @@ -86,6 +87,7 @@ impl Diagnostic {
Repr::Single { text, .. } => panic!("{}", text),
Repr::SynError(error) => panic!("{}", error),
Repr::Multi { diagnostics } => diagnostics[0].panic(),
Repr::Darling(_error) => panic!("Darling error"),
}
}
}
Expand All @@ -98,6 +100,14 @@ impl From<Error> for Diagnostic {
}
}

impl From<darling::Error> for Diagnostic {
fn from(err: darling::Error) -> Self {
Self {
inner: Repr::Darling(err.write_errors()),
}
}
}

fn extract_spans(node: &dyn ToTokens) -> Option<(Span, Span)> {
let mut t = TokenStream::new();
node.to_tokens(&mut t);
Expand Down Expand Up @@ -129,6 +139,9 @@ impl ToTokens for Diagnostic {
Repr::SynError(err) => {
err.to_compile_error().to_tokens(dst);
}
Repr::Darling(err) => {
err.to_tokens(dst);
}
}
}
}
6 changes: 3 additions & 3 deletions crates/cli/tests/wasm-bindgen/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ fn namespace_global_and_noglobal_works() {
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "fs")]
extern "C" {
#[wasm_bindgen(js_namespace = window)]
#[wasm_bindgen(js_namespace = "window")]
fn t1();
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = window)]
#[wasm_bindgen(js_namespace = "window")]
fn t2();
}
#[wasm_bindgen]
Expand Down Expand Up @@ -189,7 +189,7 @@ fn bin_crate_works() {
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
#[wasm_bindgen(js_namespace = "console")]
fn log(data: &str);
}
Expand Down
6 changes: 3 additions & 3 deletions crates/futures/src/task/multithread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,13 @@ fn wait_async(ptr: &AtomicI32, current_value: i32) -> Option<js_sys::Promise> {
type Atomics;
type WaitAsyncResult;

#[wasm_bindgen(static_method_of = Atomics, js_name = waitAsync)]
#[wasm_bindgen(static_method_of = "Atomics", js_name = "waitAsync")]
fn wait_async(buf: &js_sys::Int32Array, index: i32, value: i32) -> WaitAsyncResult;

#[wasm_bindgen(static_method_of = Atomics, js_name = waitAsync, getter)]
#[wasm_bindgen(static_method_of = "Atomics", js_name = "waitAsync", getter)]
fn get_wait_async() -> JsValue;

#[wasm_bindgen(method, getter, structural, js_name = async)]
#[wasm_bindgen(method, getter, structural, js_name = "async")]
fn async_(this: &WaitAsyncResult) -> bool;

#[wasm_bindgen(method, getter, structural)]
Expand Down
Loading

0 comments on commit 3078ea5

Please sign in to comment.