Make dictionaries use their original text in js

This commit is contained in:
Richard Dodd 2019-01-07 14:15:00 +00:00
parent 1758c8d5af
commit 46d4330637
3 changed files with 12 additions and 12 deletions

View File

@ -262,7 +262,8 @@ pub struct Dictionary {
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[derive(Clone)]
pub struct DictionaryField {
pub name: Ident,
pub rust_name: Ident,
pub js_name: String,
pub required: bool,
pub ty: syn::Type,
}

View File

@ -1152,7 +1152,7 @@ impl ToTokens for ast::Dictionary {
.fields
.iter()
.filter(|f| f.required)
.map(|f| &f.name)
.map(|f| &f.rust_name)
.collect::<Vec<_>>();
let required_types = &self
.fields
@ -1282,14 +1282,15 @@ impl ToTokens for ast::Dictionary {
impl ToTokens for ast::DictionaryField {
fn to_tokens(&self, tokens: &mut TokenStream) {
let name = &self.name;
let rust_name = &self.rust_name;
let js_name = &self.js_name;
let ty = &self.ty;
(quote! {
pub fn #name(&mut self, val: #ty) -> &mut Self {
pub fn #rust_name(&mut self, val: #ty) -> &mut Self {
use wasm_bindgen::JsValue;
let r = ::js_sys::Reflect::set(
self.obj.as_ref(),
&JsValue::from(stringify!(#name)),
&JsValue::from(#js_name),
&JsValue::from(val),
);
debug_assert!(r.is_ok(), "setting properties should never fail on our dictionary objects");

View File

@ -337,11 +337,7 @@ impl<'src> FirstPassRecord<'src> {
}
}
}
// Note that this sort isn't *quite* right in that it is sorting
// based on snake case instead of the original casing which could
// produce inconsistent results, but should work well enough for
// now!
dst[start..].sort_by_key(|f| f.name.clone());
dst[start..].sort_by_key(|f| f.js_name.clone());
return true;
}
@ -394,7 +390,8 @@ impl<'src> FirstPassRecord<'src> {
Some(ast::DictionaryField {
required: field.required.is_some(),
name: rust_ident(&snake_case_ident(field.identifier.0)),
rust_name: rust_ident(&snake_case_ident(field.identifier.0)),
js_name: field.identifier.0.to_string(),
ty,
})
}
@ -742,7 +739,8 @@ impl<'src> FirstPassRecord<'src> {
let pos = TypePosition::Argument;
fields.push(ast::DictionaryField {
required: false,
name: rust_ident(&snake_case_ident(identifier)),
rust_name: rust_ident(&snake_case_ident(identifier)),
js_name: identifier.to_string(),
ty: idl_type::IdlType::Callback.to_syn_type(pos).unwrap(),
});
}