Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make dictionaries use their original text in js #1155

Merged
merged 3 commits into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/backend/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
9 changes: 5 additions & 4 deletions crates/backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 4 additions & 0 deletions crates/webidl-tests/dictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ global.assert_dict_required = function(c) {
assert.strictEqual(c.b, "a");
assert.strictEqual(c.c, 4);
};

global.assert_camel_case = function(dict) {
assert.strictEqual(dict.wierd_fieldName, 1);
}
6 changes: 6 additions & 0 deletions crates/webidl-tests/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extern "C" {
#[wasm_bindgen(js_name = mk_dict_a)]
fn mk_dict_a2() -> Option<A>;
fn assert_dict_required(r: &Required);
fn assert_camel_case(dict: &PreserveNames);
}

#[wasm_bindgen_test]
Expand Down Expand Up @@ -51,3 +52,8 @@ fn many_types() {
fn required() {
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));
}
4 changes: 4 additions & 0 deletions crates/webidl-tests/dictionary.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ dictionary Required {
required long a;
long c;
};

dictionary PreserveNames {
long wierd_fieldName;
};
12 changes: 5 additions & 7 deletions crates/webidl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
})
}
Expand Down Expand Up @@ -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(),
});
}
Expand Down