Merge pull request #1155 from derekdreery/fix_dictionaries

Make dictionaries use their original text in js
This commit is contained in:
Alex Crichton 2019-01-07 12:45:15 -06:00 committed by GitHub
commit 8b0416a0e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 12 deletions

View File

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

View File

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

View File

@ -20,3 +20,7 @@ global.assert_dict_required = function(c) {
assert.strictEqual(c.b, "a"); assert.strictEqual(c.b, "a");
assert.strictEqual(c.c, 4); assert.strictEqual(c.c, 4);
}; };
global.assert_camel_case = function(dict) {
assert.strictEqual(dict.wierd_fieldName, 1);
}

View File

@ -16,6 +16,7 @@ extern "C" {
#[wasm_bindgen(js_name = mk_dict_a)] #[wasm_bindgen(js_name = mk_dict_a)]
fn mk_dict_a2() -> Option<A>; fn mk_dict_a2() -> Option<A>;
fn assert_dict_required(r: &Required); fn assert_dict_required(r: &Required);
fn assert_camel_case(dict: &PreserveNames);
} }
#[wasm_bindgen_test] #[wasm_bindgen_test]
@ -51,3 +52,8 @@ fn many_types() {
fn required() { fn required() {
assert_dict_required(Required::new(3, "a").c(4)); assert_dict_required(Required::new(3, "a").c(4));
} }
#[wasm_bindgen_test]
fn correct_casing_in_js() {
assert_camel_case(PreserveNames::new().wierd_field_name(1));
}

View File

@ -45,3 +45,7 @@ dictionary Required {
required long a; required long a;
long c; long c;
}; };
dictionary PreserveNames {
long wierd_fieldName;
};

View File

@ -337,11 +337,7 @@ impl<'src> FirstPassRecord<'src> {
} }
} }
} }
// Note that this sort isn't *quite* right in that it is sorting dst[start..].sort_by_key(|f| f.js_name.clone());
// 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());
return true; return true;
} }
@ -394,7 +390,8 @@ impl<'src> FirstPassRecord<'src> {
Some(ast::DictionaryField { Some(ast::DictionaryField {
required: field.required.is_some(), 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, ty,
}) })
} }
@ -742,7 +739,8 @@ impl<'src> FirstPassRecord<'src> {
let pos = TypePosition::Argument; let pos = TypePosition::Argument;
fields.push(ast::DictionaryField { fields.push(ast::DictionaryField {
required: false, 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(), ty: idl_type::IdlType::Callback.to_syn_type(pos).unwrap(),
}); });
} }