Skip to content

Commit d25a68e

Browse files
authored
Don't use reflect in webidl dictionary setters (#3898)
1 parent 9347af3 commit d25a68e

File tree

545 files changed

+8000
-24107
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

545 files changed

+8000
-24107
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
* Stabilize Web Share API.
2020
[#3882](https://github.com/rustwasm/wasm-bindgen/pull/3882)
2121

22+
* Generate JS bindings for WebIDL dictionary setters instead of using `Reflect`. This increases the size of the Web API bindings but should be more performant. Also, importing getters/setters from JS now supports specifying the JS attribute name as a string, e.g. `#[wasm_bindgen(method, setter = "x-cdm-codecs")]`.
23+
[#3898](https://github.com/rustwasm/wasm-bindgen/pull/3898)
24+
2225
### Fixed
2326

2427
* Copy port from headless test server when using `WASM_BINDGEN_TEST_ADDRESS`.

crates/backend/src/ast.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,10 @@ pub struct Operation {
242242
pub enum OperationKind {
243243
/// A standard method, nothing special
244244
Regular,
245-
/// A method for getting the value of the provided Ident
246-
Getter(Option<Ident>),
247-
/// A method for setting the value of the provided Ident
248-
Setter(Option<Ident>),
245+
/// A method for getting the value of the provided Ident or String
246+
Getter(Option<String>),
247+
/// A method for setting the value of the provided Ident or String
248+
Setter(Option<String>),
249249
/// A dynamically intercepted getter
250250
IndexingGetter,
251251
/// A dynamically intercepted setter

crates/backend/src/encode.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -531,12 +531,12 @@ fn from_ast_method_kind<'a>(
531531
let is_static = *is_static;
532532
let kind = match kind {
533533
ast::OperationKind::Getter(g) => {
534-
let g = g.as_ref().map(|g| intern.intern(g));
534+
let g = g.as_ref().map(|g| intern.intern_str(g));
535535
OperationKind::Getter(g.unwrap_or_else(|| function.infer_getter_property()))
536536
}
537537
ast::OperationKind::Regular => OperationKind::Regular,
538538
ast::OperationKind::Setter(s) => {
539-
let s = s.as_ref().map(|s| intern.intern(s));
539+
let s = s.as_ref().map(|s| intern.intern_str(s));
540540
OperationKind::Setter(match s {
541541
Some(s) => s,
542542
None => intern.intern_str(&function.infer_setter_property()?),

crates/macro-support/src/parser.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ macro_rules! attrgen {
6565
(module, Module(Span, String, Span)),
6666
(raw_module, RawModule(Span, String, Span)),
6767
(inline_js, InlineJs(Span, String, Span)),
68-
(getter, Getter(Span, Option<Ident>)),
69-
(setter, Setter(Span, Option<Ident>)),
68+
(getter, Getter(Span, Option<String>)),
69+
(setter, Setter(Span, Option<String>)),
7070
(indexing_getter, IndexingGetter(Span)),
7171
(indexing_setter, IndexingSetter(Span)),
7272
(indexing_deleter, IndexingDeleter(Span)),
@@ -299,10 +299,15 @@ impl Parse for BindgenAttr {
299299
return Ok(BindgenAttr::$variant(attr_span, ident))
300300
});
301301

302-
(@parser $variant:ident(Span, Option<Ident>)) => ({
302+
(@parser $variant:ident(Span, Option<String>)) => ({
303303
if input.parse::<Token![=]>().is_ok() {
304+
if input.peek(syn::LitStr) {
305+
let litstr = input.parse::<syn::LitStr>()?;
306+
return Ok(BindgenAttr::$variant(attr_span, Some(litstr.value())))
307+
}
308+
304309
let ident = input.parse::<AnyIdent>()?.0;
305-
return Ok(BindgenAttr::$variant(attr_span, Some(ident)))
310+
return Ok(BindgenAttr::$variant(attr_span, Some(ident.to_string())))
306311
} else {
307312
return Ok(BindgenAttr::$variant(attr_span, None));
308313
}

crates/web-sys/src/features/gen_AddEventListenerOptions.rs

+9-29
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ extern "C" {
1010
#[doc = ""]
1111
#[doc = "*This API requires the following crate features to be activated: `AddEventListenerOptions`*"]
1212
pub type AddEventListenerOptions;
13+
#[wasm_bindgen(method, setter = "capture")]
14+
fn capture_shim(this: &AddEventListenerOptions, val: bool);
15+
#[wasm_bindgen(method, setter = "once")]
16+
fn once_shim(this: &AddEventListenerOptions, val: bool);
17+
#[wasm_bindgen(method, setter = "passive")]
18+
fn passive_shim(this: &AddEventListenerOptions, val: bool);
1319
}
1420
impl AddEventListenerOptions {
1521
#[doc = "Construct a new `AddEventListenerOptions`."]
@@ -24,47 +30,21 @@ impl AddEventListenerOptions {
2430
#[doc = ""]
2531
#[doc = "*This API requires the following crate features to be activated: `AddEventListenerOptions`*"]
2632
pub fn capture(&mut self, val: bool) -> &mut Self {
27-
use wasm_bindgen::JsValue;
28-
let r = ::js_sys::Reflect::set(
29-
self.as_ref(),
30-
&JsValue::from("capture"),
31-
&JsValue::from(val),
32-
);
33-
debug_assert!(
34-
r.is_ok(),
35-
"setting properties should never fail on our dictionary objects"
36-
);
37-
let _ = r;
33+
self.capture_shim(val);
3834
self
3935
}
4036
#[doc = "Change the `once` field of this object."]
4137
#[doc = ""]
4238
#[doc = "*This API requires the following crate features to be activated: `AddEventListenerOptions`*"]
4339
pub fn once(&mut self, val: bool) -> &mut Self {
44-
use wasm_bindgen::JsValue;
45-
let r = ::js_sys::Reflect::set(self.as_ref(), &JsValue::from("once"), &JsValue::from(val));
46-
debug_assert!(
47-
r.is_ok(),
48-
"setting properties should never fail on our dictionary objects"
49-
);
50-
let _ = r;
40+
self.once_shim(val);
5141
self
5242
}
5343
#[doc = "Change the `passive` field of this object."]
5444
#[doc = ""]
5545
#[doc = "*This API requires the following crate features to be activated: `AddEventListenerOptions`*"]
5646
pub fn passive(&mut self, val: bool) -> &mut Self {
57-
use wasm_bindgen::JsValue;
58-
let r = ::js_sys::Reflect::set(
59-
self.as_ref(),
60-
&JsValue::from("passive"),
61-
&JsValue::from(val),
62-
);
63-
debug_assert!(
64-
r.is_ok(),
65-
"setting properties should never fail on our dictionary objects"
66-
);
67-
let _ = r;
47+
self.passive_shim(val);
6848
self
6949
}
7050
}

crates/web-sys/src/features/gen_AesCbcParams.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ extern "C" {
1010
#[doc = ""]
1111
#[doc = "*This API requires the following crate features to be activated: `AesCbcParams`*"]
1212
pub type AesCbcParams;
13+
#[wasm_bindgen(method, setter = "name")]
14+
fn name_shim(this: &AesCbcParams, val: &str);
15+
#[wasm_bindgen(method, setter = "iv")]
16+
fn iv_shim(this: &AesCbcParams, val: &::js_sys::Object);
1317
}
1418
impl AesCbcParams {
1519
#[doc = "Construct a new `AesCbcParams`."]
@@ -26,26 +30,14 @@ impl AesCbcParams {
2630
#[doc = ""]
2731
#[doc = "*This API requires the following crate features to be activated: `AesCbcParams`*"]
2832
pub fn name(&mut self, val: &str) -> &mut Self {
29-
use wasm_bindgen::JsValue;
30-
let r = ::js_sys::Reflect::set(self.as_ref(), &JsValue::from("name"), &JsValue::from(val));
31-
debug_assert!(
32-
r.is_ok(),
33-
"setting properties should never fail on our dictionary objects"
34-
);
35-
let _ = r;
33+
self.name_shim(val);
3634
self
3735
}
3836
#[doc = "Change the `iv` field of this object."]
3937
#[doc = ""]
4038
#[doc = "*This API requires the following crate features to be activated: `AesCbcParams`*"]
4139
pub fn iv(&mut self, val: &::js_sys::Object) -> &mut Self {
42-
use wasm_bindgen::JsValue;
43-
let r = ::js_sys::Reflect::set(self.as_ref(), &JsValue::from("iv"), &JsValue::from(val));
44-
debug_assert!(
45-
r.is_ok(),
46-
"setting properties should never fail on our dictionary objects"
47-
);
48-
let _ = r;
40+
self.iv_shim(val);
4941
self
5042
}
5143
}

crates/web-sys/src/features/gen_AesCtrParams.rs

+9-26
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ extern "C" {
1010
#[doc = ""]
1111
#[doc = "*This API requires the following crate features to be activated: `AesCtrParams`*"]
1212
pub type AesCtrParams;
13+
#[wasm_bindgen(method, setter = "name")]
14+
fn name_shim(this: &AesCtrParams, val: &str);
15+
#[wasm_bindgen(method, setter = "counter")]
16+
fn counter_shim(this: &AesCtrParams, val: &::js_sys::Object);
17+
#[wasm_bindgen(method, setter = "length")]
18+
fn length_shim(this: &AesCtrParams, val: u8);
1319
}
1420
impl AesCtrParams {
1521
#[doc = "Construct a new `AesCtrParams`."]
@@ -27,44 +33,21 @@ impl AesCtrParams {
2733
#[doc = ""]
2834
#[doc = "*This API requires the following crate features to be activated: `AesCtrParams`*"]
2935
pub fn name(&mut self, val: &str) -> &mut Self {
30-
use wasm_bindgen::JsValue;
31-
let r = ::js_sys::Reflect::set(self.as_ref(), &JsValue::from("name"), &JsValue::from(val));
32-
debug_assert!(
33-
r.is_ok(),
34-
"setting properties should never fail on our dictionary objects"
35-
);
36-
let _ = r;
36+
self.name_shim(val);
3737
self
3838
}
3939
#[doc = "Change the `counter` field of this object."]
4040
#[doc = ""]
4141
#[doc = "*This API requires the following crate features to be activated: `AesCtrParams`*"]
4242
pub fn counter(&mut self, val: &::js_sys::Object) -> &mut Self {
43-
use wasm_bindgen::JsValue;
44-
let r = ::js_sys::Reflect::set(
45-
self.as_ref(),
46-
&JsValue::from("counter"),
47-
&JsValue::from(val),
48-
);
49-
debug_assert!(
50-
r.is_ok(),
51-
"setting properties should never fail on our dictionary objects"
52-
);
53-
let _ = r;
43+
self.counter_shim(val);
5444
self
5545
}
5646
#[doc = "Change the `length` field of this object."]
5747
#[doc = ""]
5848
#[doc = "*This API requires the following crate features to be activated: `AesCtrParams`*"]
5949
pub fn length(&mut self, val: u8) -> &mut Self {
60-
use wasm_bindgen::JsValue;
61-
let r =
62-
::js_sys::Reflect::set(self.as_ref(), &JsValue::from("length"), &JsValue::from(val));
63-
debug_assert!(
64-
r.is_ok(),
65-
"setting properties should never fail on our dictionary objects"
66-
);
67-
let _ = r;
50+
self.length_shim(val);
6851
self
6952
}
7053
}

crates/web-sys/src/features/gen_AesDerivedKeyParams.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ extern "C" {
1010
#[doc = ""]
1111
#[doc = "*This API requires the following crate features to be activated: `AesDerivedKeyParams`*"]
1212
pub type AesDerivedKeyParams;
13+
#[wasm_bindgen(method, setter = "name")]
14+
fn name_shim(this: &AesDerivedKeyParams, val: &str);
15+
#[wasm_bindgen(method, setter = "length")]
16+
fn length_shim(this: &AesDerivedKeyParams, val: u32);
1317
}
1418
impl AesDerivedKeyParams {
1519
#[doc = "Construct a new `AesDerivedKeyParams`."]
@@ -26,27 +30,14 @@ impl AesDerivedKeyParams {
2630
#[doc = ""]
2731
#[doc = "*This API requires the following crate features to be activated: `AesDerivedKeyParams`*"]
2832
pub fn name(&mut self, val: &str) -> &mut Self {
29-
use wasm_bindgen::JsValue;
30-
let r = ::js_sys::Reflect::set(self.as_ref(), &JsValue::from("name"), &JsValue::from(val));
31-
debug_assert!(
32-
r.is_ok(),
33-
"setting properties should never fail on our dictionary objects"
34-
);
35-
let _ = r;
33+
self.name_shim(val);
3634
self
3735
}
3836
#[doc = "Change the `length` field of this object."]
3937
#[doc = ""]
4038
#[doc = "*This API requires the following crate features to be activated: `AesDerivedKeyParams`*"]
4139
pub fn length(&mut self, val: u32) -> &mut Self {
42-
use wasm_bindgen::JsValue;
43-
let r =
44-
::js_sys::Reflect::set(self.as_ref(), &JsValue::from("length"), &JsValue::from(val));
45-
debug_assert!(
46-
r.is_ok(),
47-
"setting properties should never fail on our dictionary objects"
48-
);
49-
let _ = r;
40+
self.length_shim(val);
5041
self
5142
}
5243
}

crates/web-sys/src/features/gen_AesGcmParams.rs

+12-36
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ extern "C" {
1010
#[doc = ""]
1111
#[doc = "*This API requires the following crate features to be activated: `AesGcmParams`*"]
1212
pub type AesGcmParams;
13+
#[wasm_bindgen(method, setter = "name")]
14+
fn name_shim(this: &AesGcmParams, val: &str);
15+
#[wasm_bindgen(method, setter = "additionalData")]
16+
fn additional_data_shim(this: &AesGcmParams, val: &::js_sys::Object);
17+
#[wasm_bindgen(method, setter = "iv")]
18+
fn iv_shim(this: &AesGcmParams, val: &::js_sys::Object);
19+
#[wasm_bindgen(method, setter = "tagLength")]
20+
fn tag_length_shim(this: &AesGcmParams, val: u8);
1321
}
1422
impl AesGcmParams {
1523
#[doc = "Construct a new `AesGcmParams`."]
@@ -26,60 +34,28 @@ impl AesGcmParams {
2634
#[doc = ""]
2735
#[doc = "*This API requires the following crate features to be activated: `AesGcmParams`*"]
2836
pub fn name(&mut self, val: &str) -> &mut Self {
29-
use wasm_bindgen::JsValue;
30-
let r = ::js_sys::Reflect::set(self.as_ref(), &JsValue::from("name"), &JsValue::from(val));
31-
debug_assert!(
32-
r.is_ok(),
33-
"setting properties should never fail on our dictionary objects"
34-
);
35-
let _ = r;
37+
self.name_shim(val);
3638
self
3739
}
3840
#[doc = "Change the `additionalData` field of this object."]
3941
#[doc = ""]
4042
#[doc = "*This API requires the following crate features to be activated: `AesGcmParams`*"]
4143
pub fn additional_data(&mut self, val: &::js_sys::Object) -> &mut Self {
42-
use wasm_bindgen::JsValue;
43-
let r = ::js_sys::Reflect::set(
44-
self.as_ref(),
45-
&JsValue::from("additionalData"),
46-
&JsValue::from(val),
47-
);
48-
debug_assert!(
49-
r.is_ok(),
50-
"setting properties should never fail on our dictionary objects"
51-
);
52-
let _ = r;
44+
self.additional_data_shim(val);
5345
self
5446
}
5547
#[doc = "Change the `iv` field of this object."]
5648
#[doc = ""]
5749
#[doc = "*This API requires the following crate features to be activated: `AesGcmParams`*"]
5850
pub fn iv(&mut self, val: &::js_sys::Object) -> &mut Self {
59-
use wasm_bindgen::JsValue;
60-
let r = ::js_sys::Reflect::set(self.as_ref(), &JsValue::from("iv"), &JsValue::from(val));
61-
debug_assert!(
62-
r.is_ok(),
63-
"setting properties should never fail on our dictionary objects"
64-
);
65-
let _ = r;
51+
self.iv_shim(val);
6652
self
6753
}
6854
#[doc = "Change the `tagLength` field of this object."]
6955
#[doc = ""]
7056
#[doc = "*This API requires the following crate features to be activated: `AesGcmParams`*"]
7157
pub fn tag_length(&mut self, val: u8) -> &mut Self {
72-
use wasm_bindgen::JsValue;
73-
let r = ::js_sys::Reflect::set(
74-
self.as_ref(),
75-
&JsValue::from("tagLength"),
76-
&JsValue::from(val),
77-
);
78-
debug_assert!(
79-
r.is_ok(),
80-
"setting properties should never fail on our dictionary objects"
81-
);
82-
let _ = r;
58+
self.tag_length_shim(val);
8359
self
8460
}
8561
}

crates/web-sys/src/features/gen_AesKeyAlgorithm.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ extern "C" {
1010
#[doc = ""]
1111
#[doc = "*This API requires the following crate features to be activated: `AesKeyAlgorithm`*"]
1212
pub type AesKeyAlgorithm;
13+
#[wasm_bindgen(method, setter = "name")]
14+
fn name_shim(this: &AesKeyAlgorithm, val: &str);
15+
#[wasm_bindgen(method, setter = "length")]
16+
fn length_shim(this: &AesKeyAlgorithm, val: u16);
1317
}
1418
impl AesKeyAlgorithm {
1519
#[doc = "Construct a new `AesKeyAlgorithm`."]
@@ -26,27 +30,14 @@ impl AesKeyAlgorithm {
2630
#[doc = ""]
2731
#[doc = "*This API requires the following crate features to be activated: `AesKeyAlgorithm`*"]
2832
pub fn name(&mut self, val: &str) -> &mut Self {
29-
use wasm_bindgen::JsValue;
30-
let r = ::js_sys::Reflect::set(self.as_ref(), &JsValue::from("name"), &JsValue::from(val));
31-
debug_assert!(
32-
r.is_ok(),
33-
"setting properties should never fail on our dictionary objects"
34-
);
35-
let _ = r;
33+
self.name_shim(val);
3634
self
3735
}
3836
#[doc = "Change the `length` field of this object."]
3937
#[doc = ""]
4038
#[doc = "*This API requires the following crate features to be activated: `AesKeyAlgorithm`*"]
4139
pub fn length(&mut self, val: u16) -> &mut Self {
42-
use wasm_bindgen::JsValue;
43-
let r =
44-
::js_sys::Reflect::set(self.as_ref(), &JsValue::from("length"), &JsValue::from(val));
45-
debug_assert!(
46-
r.is_ok(),
47-
"setting properties should never fail on our dictionary objects"
48-
);
49-
let _ = r;
40+
self.length_shim(val);
5041
self
5142
}
5243
}

0 commit comments

Comments
 (0)