Skip to content

Commit

Permalink
Don't use reflect in webidl dictionary setters (#3898)
Browse files Browse the repository at this point in the history
  • Loading branch information
Davidster committed Apr 3, 2024
1 parent 9347af3 commit d25a68e
Show file tree
Hide file tree
Showing 545 changed files with 8,000 additions and 24,107 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
* Stabilize Web Share API.
[#3882](https://github.com/rustwasm/wasm-bindgen/pull/3882)

* 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")]`.
[#3898](https://github.com/rustwasm/wasm-bindgen/pull/3898)

### Fixed

* Copy port from headless test server when using `WASM_BINDGEN_TEST_ADDRESS`.
Expand Down
8 changes: 4 additions & 4 deletions crates/backend/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,10 @@ pub struct Operation {
pub enum OperationKind {
/// A standard method, nothing special
Regular,
/// A method for getting the value of the provided Ident
Getter(Option<Ident>),
/// A method for setting the value of the provided Ident
Setter(Option<Ident>),
/// A method for getting the value of the provided Ident or String
Getter(Option<String>),
/// A method for setting the value of the provided Ident or String
Setter(Option<String>),
/// A dynamically intercepted getter
IndexingGetter,
/// A dynamically intercepted setter
Expand Down
4 changes: 2 additions & 2 deletions crates/backend/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,12 +531,12 @@ fn from_ast_method_kind<'a>(
let is_static = *is_static;
let kind = match kind {
ast::OperationKind::Getter(g) => {
let g = g.as_ref().map(|g| intern.intern(g));
let g = g.as_ref().map(|g| intern.intern_str(g));
OperationKind::Getter(g.unwrap_or_else(|| function.infer_getter_property()))
}
ast::OperationKind::Regular => OperationKind::Regular,
ast::OperationKind::Setter(s) => {
let s = s.as_ref().map(|s| intern.intern(s));
let s = s.as_ref().map(|s| intern.intern_str(s));
OperationKind::Setter(match s {
Some(s) => s,
None => intern.intern_str(&function.infer_setter_property()?),
Expand Down
13 changes: 9 additions & 4 deletions crates/macro-support/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ macro_rules! attrgen {
(module, Module(Span, String, Span)),
(raw_module, RawModule(Span, String, Span)),
(inline_js, InlineJs(Span, String, Span)),
(getter, Getter(Span, Option<Ident>)),
(setter, Setter(Span, Option<Ident>)),
(getter, Getter(Span, Option<String>)),
(setter, Setter(Span, Option<String>)),
(indexing_getter, IndexingGetter(Span)),
(indexing_setter, IndexingSetter(Span)),
(indexing_deleter, IndexingDeleter(Span)),
Expand Down Expand Up @@ -299,10 +299,15 @@ impl Parse for BindgenAttr {
return Ok(BindgenAttr::$variant(attr_span, ident))
});

(@parser $variant:ident(Span, Option<Ident>)) => ({
(@parser $variant:ident(Span, Option<String>)) => ({
if input.parse::<Token![=]>().is_ok() {
if input.peek(syn::LitStr) {
let litstr = input.parse::<syn::LitStr>()?;
return Ok(BindgenAttr::$variant(attr_span, Some(litstr.value())))
}

let ident = input.parse::<AnyIdent>()?.0;
return Ok(BindgenAttr::$variant(attr_span, Some(ident)))
return Ok(BindgenAttr::$variant(attr_span, Some(ident.to_string())))
} else {
return Ok(BindgenAttr::$variant(attr_span, None));
}
Expand Down
38 changes: 9 additions & 29 deletions crates/web-sys/src/features/gen_AddEventListenerOptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ extern "C" {
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AddEventListenerOptions`*"]
pub type AddEventListenerOptions;
#[wasm_bindgen(method, setter = "capture")]
fn capture_shim(this: &AddEventListenerOptions, val: bool);
#[wasm_bindgen(method, setter = "once")]
fn once_shim(this: &AddEventListenerOptions, val: bool);
#[wasm_bindgen(method, setter = "passive")]
fn passive_shim(this: &AddEventListenerOptions, val: bool);
}
impl AddEventListenerOptions {
#[doc = "Construct a new `AddEventListenerOptions`."]
Expand All @@ -24,47 +30,21 @@ impl AddEventListenerOptions {
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AddEventListenerOptions`*"]
pub fn capture(&mut self, val: bool) -> &mut Self {
use wasm_bindgen::JsValue;
let r = ::js_sys::Reflect::set(
self.as_ref(),
&JsValue::from("capture"),
&JsValue::from(val),
);
debug_assert!(
r.is_ok(),
"setting properties should never fail on our dictionary objects"
);
let _ = r;
self.capture_shim(val);
self
}
#[doc = "Change the `once` field of this object."]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AddEventListenerOptions`*"]
pub fn once(&mut self, val: bool) -> &mut Self {
use wasm_bindgen::JsValue;
let r = ::js_sys::Reflect::set(self.as_ref(), &JsValue::from("once"), &JsValue::from(val));
debug_assert!(
r.is_ok(),
"setting properties should never fail on our dictionary objects"
);
let _ = r;
self.once_shim(val);
self
}
#[doc = "Change the `passive` field of this object."]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AddEventListenerOptions`*"]
pub fn passive(&mut self, val: bool) -> &mut Self {
use wasm_bindgen::JsValue;
let r = ::js_sys::Reflect::set(
self.as_ref(),
&JsValue::from("passive"),
&JsValue::from(val),
);
debug_assert!(
r.is_ok(),
"setting properties should never fail on our dictionary objects"
);
let _ = r;
self.passive_shim(val);
self
}
}
Expand Down
20 changes: 6 additions & 14 deletions crates/web-sys/src/features/gen_AesCbcParams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ extern "C" {
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AesCbcParams`*"]
pub type AesCbcParams;
#[wasm_bindgen(method, setter = "name")]
fn name_shim(this: &AesCbcParams, val: &str);
#[wasm_bindgen(method, setter = "iv")]
fn iv_shim(this: &AesCbcParams, val: &::js_sys::Object);
}
impl AesCbcParams {
#[doc = "Construct a new `AesCbcParams`."]
Expand All @@ -26,26 +30,14 @@ impl AesCbcParams {
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AesCbcParams`*"]
pub fn name(&mut self, val: &str) -> &mut Self {
use wasm_bindgen::JsValue;
let r = ::js_sys::Reflect::set(self.as_ref(), &JsValue::from("name"), &JsValue::from(val));
debug_assert!(
r.is_ok(),
"setting properties should never fail on our dictionary objects"
);
let _ = r;
self.name_shim(val);
self
}
#[doc = "Change the `iv` field of this object."]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AesCbcParams`*"]
pub fn iv(&mut self, val: &::js_sys::Object) -> &mut Self {
use wasm_bindgen::JsValue;
let r = ::js_sys::Reflect::set(self.as_ref(), &JsValue::from("iv"), &JsValue::from(val));
debug_assert!(
r.is_ok(),
"setting properties should never fail on our dictionary objects"
);
let _ = r;
self.iv_shim(val);
self
}
}
35 changes: 9 additions & 26 deletions crates/web-sys/src/features/gen_AesCtrParams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ extern "C" {
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AesCtrParams`*"]
pub type AesCtrParams;
#[wasm_bindgen(method, setter = "name")]
fn name_shim(this: &AesCtrParams, val: &str);
#[wasm_bindgen(method, setter = "counter")]
fn counter_shim(this: &AesCtrParams, val: &::js_sys::Object);
#[wasm_bindgen(method, setter = "length")]
fn length_shim(this: &AesCtrParams, val: u8);
}
impl AesCtrParams {
#[doc = "Construct a new `AesCtrParams`."]
Expand All @@ -27,44 +33,21 @@ impl AesCtrParams {
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AesCtrParams`*"]
pub fn name(&mut self, val: &str) -> &mut Self {
use wasm_bindgen::JsValue;
let r = ::js_sys::Reflect::set(self.as_ref(), &JsValue::from("name"), &JsValue::from(val));
debug_assert!(
r.is_ok(),
"setting properties should never fail on our dictionary objects"
);
let _ = r;
self.name_shim(val);
self
}
#[doc = "Change the `counter` field of this object."]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AesCtrParams`*"]
pub fn counter(&mut self, val: &::js_sys::Object) -> &mut Self {
use wasm_bindgen::JsValue;
let r = ::js_sys::Reflect::set(
self.as_ref(),
&JsValue::from("counter"),
&JsValue::from(val),
);
debug_assert!(
r.is_ok(),
"setting properties should never fail on our dictionary objects"
);
let _ = r;
self.counter_shim(val);
self
}
#[doc = "Change the `length` field of this object."]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AesCtrParams`*"]
pub fn length(&mut self, val: u8) -> &mut Self {
use wasm_bindgen::JsValue;
let r =
::js_sys::Reflect::set(self.as_ref(), &JsValue::from("length"), &JsValue::from(val));
debug_assert!(
r.is_ok(),
"setting properties should never fail on our dictionary objects"
);
let _ = r;
self.length_shim(val);
self
}
}
21 changes: 6 additions & 15 deletions crates/web-sys/src/features/gen_AesDerivedKeyParams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ extern "C" {
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AesDerivedKeyParams`*"]
pub type AesDerivedKeyParams;
#[wasm_bindgen(method, setter = "name")]
fn name_shim(this: &AesDerivedKeyParams, val: &str);
#[wasm_bindgen(method, setter = "length")]
fn length_shim(this: &AesDerivedKeyParams, val: u32);
}
impl AesDerivedKeyParams {
#[doc = "Construct a new `AesDerivedKeyParams`."]
Expand All @@ -26,27 +30,14 @@ impl AesDerivedKeyParams {
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AesDerivedKeyParams`*"]
pub fn name(&mut self, val: &str) -> &mut Self {
use wasm_bindgen::JsValue;
let r = ::js_sys::Reflect::set(self.as_ref(), &JsValue::from("name"), &JsValue::from(val));
debug_assert!(
r.is_ok(),
"setting properties should never fail on our dictionary objects"
);
let _ = r;
self.name_shim(val);
self
}
#[doc = "Change the `length` field of this object."]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AesDerivedKeyParams`*"]
pub fn length(&mut self, val: u32) -> &mut Self {
use wasm_bindgen::JsValue;
let r =
::js_sys::Reflect::set(self.as_ref(), &JsValue::from("length"), &JsValue::from(val));
debug_assert!(
r.is_ok(),
"setting properties should never fail on our dictionary objects"
);
let _ = r;
self.length_shim(val);
self
}
}
48 changes: 12 additions & 36 deletions crates/web-sys/src/features/gen_AesGcmParams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ extern "C" {
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AesGcmParams`*"]
pub type AesGcmParams;
#[wasm_bindgen(method, setter = "name")]
fn name_shim(this: &AesGcmParams, val: &str);
#[wasm_bindgen(method, setter = "additionalData")]
fn additional_data_shim(this: &AesGcmParams, val: &::js_sys::Object);
#[wasm_bindgen(method, setter = "iv")]
fn iv_shim(this: &AesGcmParams, val: &::js_sys::Object);
#[wasm_bindgen(method, setter = "tagLength")]
fn tag_length_shim(this: &AesGcmParams, val: u8);
}
impl AesGcmParams {
#[doc = "Construct a new `AesGcmParams`."]
Expand All @@ -26,60 +34,28 @@ impl AesGcmParams {
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AesGcmParams`*"]
pub fn name(&mut self, val: &str) -> &mut Self {
use wasm_bindgen::JsValue;
let r = ::js_sys::Reflect::set(self.as_ref(), &JsValue::from("name"), &JsValue::from(val));
debug_assert!(
r.is_ok(),
"setting properties should never fail on our dictionary objects"
);
let _ = r;
self.name_shim(val);
self
}
#[doc = "Change the `additionalData` field of this object."]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AesGcmParams`*"]
pub fn additional_data(&mut self, val: &::js_sys::Object) -> &mut Self {
use wasm_bindgen::JsValue;
let r = ::js_sys::Reflect::set(
self.as_ref(),
&JsValue::from("additionalData"),
&JsValue::from(val),
);
debug_assert!(
r.is_ok(),
"setting properties should never fail on our dictionary objects"
);
let _ = r;
self.additional_data_shim(val);
self
}
#[doc = "Change the `iv` field of this object."]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AesGcmParams`*"]
pub fn iv(&mut self, val: &::js_sys::Object) -> &mut Self {
use wasm_bindgen::JsValue;
let r = ::js_sys::Reflect::set(self.as_ref(), &JsValue::from("iv"), &JsValue::from(val));
debug_assert!(
r.is_ok(),
"setting properties should never fail on our dictionary objects"
);
let _ = r;
self.iv_shim(val);
self
}
#[doc = "Change the `tagLength` field of this object."]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AesGcmParams`*"]
pub fn tag_length(&mut self, val: u8) -> &mut Self {
use wasm_bindgen::JsValue;
let r = ::js_sys::Reflect::set(
self.as_ref(),
&JsValue::from("tagLength"),
&JsValue::from(val),
);
debug_assert!(
r.is_ok(),
"setting properties should never fail on our dictionary objects"
);
let _ = r;
self.tag_length_shim(val);
self
}
}
21 changes: 6 additions & 15 deletions crates/web-sys/src/features/gen_AesKeyAlgorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ extern "C" {
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AesKeyAlgorithm`*"]
pub type AesKeyAlgorithm;
#[wasm_bindgen(method, setter = "name")]
fn name_shim(this: &AesKeyAlgorithm, val: &str);
#[wasm_bindgen(method, setter = "length")]
fn length_shim(this: &AesKeyAlgorithm, val: u16);
}
impl AesKeyAlgorithm {
#[doc = "Construct a new `AesKeyAlgorithm`."]
Expand All @@ -26,27 +30,14 @@ impl AesKeyAlgorithm {
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AesKeyAlgorithm`*"]
pub fn name(&mut self, val: &str) -> &mut Self {
use wasm_bindgen::JsValue;
let r = ::js_sys::Reflect::set(self.as_ref(), &JsValue::from("name"), &JsValue::from(val));
debug_assert!(
r.is_ok(),
"setting properties should never fail on our dictionary objects"
);
let _ = r;
self.name_shim(val);
self
}
#[doc = "Change the `length` field of this object."]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `AesKeyAlgorithm`*"]
pub fn length(&mut self, val: u16) -> &mut Self {
use wasm_bindgen::JsValue;
let r =
::js_sys::Reflect::set(self.as_ref(), &JsValue::from("length"), &JsValue::from(val));
debug_assert!(
r.is_ok(),
"setting properties should never fail on our dictionary objects"
);
let _ = r;
self.length_shim(val);
self
}
}

0 comments on commit d25a68e

Please sign in to comment.