From aa735d221ab5ae739caeb96ea256487c2a461dfd Mon Sep 17 00:00:00 2001 From: Danielle Pham Date: Sun, 12 Aug 2018 23:16:18 -0400 Subject: [PATCH 1/3] Use js_class for static method bindings as well --- crates/macro-support/src/parser.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/macro-support/src/parser.rs b/crates/macro-support/src/parser.rs index ae7815d5..18b198a6 100644 --- a/crates/macro-support/src/parser.rs +++ b/crates/macro-support/src/parser.rs @@ -473,7 +473,10 @@ impl<'a> ConvertToAst<(BindgenAttrs, &'a Option)> for syn::ForeignItemFn kind, } } else if let Some(cls) = opts.static_method_of() { - let class = cls.to_string(); + let class = opts + .js_class() + .map(Into::into) + .unwrap_or_else(|| cls.to_string()); let ty = ident_ty(cls.clone()); let kind = ast::MethodKind::Operation(ast::Operation { From fd5958b51bed57ef48e25443d14f82b077351b9d Mon Sep 17 00:00:00 2001 From: Danielle Pham Date: Sun, 12 Aug 2018 23:16:35 -0400 Subject: [PATCH 2/3] Add bindings for String.from_char_code --- crates/js-sys/src/lib.rs | 28 ++++++++++++++++++++++++++++ crates/js-sys/tests/wasm/JsString.rs | 13 +++++++++++++ 2 files changed, 41 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index bf3c9704..60c92d87 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -2940,6 +2940,34 @@ extern "C" { #[wasm_bindgen(method, js_class = "String", js_name = endsWith)] pub fn ends_with(this: &JsString, search_string: &str, length: i32) -> bool; + /// The static String.fromCharCode() method returns a string created from + /// the specified sequence of UTF-16 code units. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode + /// + /// # Notes + /// + /// There are a few bindings to `from_char_code` in `js-sys`: `from_char_code1`, `from_char_code2`, etc... + /// with different arities. + #[wasm_bindgen(static_method_of = JsString, js_class = "String", js_name = fromCharCode)] + pub fn from_char_code1(a: f64) -> JsString; + + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode + #[wasm_bindgen(static_method_of = JsString, js_class = "String", js_name = fromCharCode)] + pub fn from_char_code2(a: f64, b: f64) -> JsString; + + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode + #[wasm_bindgen(static_method_of = JsString, js_class = "String", js_name = fromCharCode)] + pub fn from_char_code3(a: f64, b: f64, c: f64) -> JsString; + + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode + #[wasm_bindgen(static_method_of = JsString, js_class = "String", js_name = fromCharCode)] + pub fn from_char_code4(a: f64, b: f64, c: f64, d: f64) -> JsString; + + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode + #[wasm_bindgen(static_method_of = JsString, js_class = "String", js_name = fromCharCode)] + pub fn from_char_code5(a: f64, b: f64, c: f64, d: f64, e: f64) -> JsString; + /// The `includes()` method determines whether one string may be found /// within another string, returning true or false as appropriate. /// diff --git a/crates/js-sys/tests/wasm/JsString.rs b/crates/js-sys/tests/wasm/JsString.rs index 973f2cef..0c536c34 100644 --- a/crates/js-sys/tests/wasm/JsString.rs +++ b/crates/js-sys/tests/wasm/JsString.rs @@ -58,6 +58,19 @@ fn ends_with() { assert_eq!(js.ends_with("to be", 19), true); } +#[wasm_bindgen_test] +fn from_char_code() { + let s = "½+¾="; + let codes : Vec = s.chars() + .map(|char| char as u32 as f64) + .collect(); + + assert_eq!(JsString::from_char_code1(codes[0]), "½"); + assert_eq!(JsString::from_char_code2(codes[0], codes[1]), "½+"); + assert_eq!(JsString::from_char_code3(codes[0], codes[1], codes[2]), "½+¾"); + assert_eq!(JsString::from_char_code4(codes[0], codes[1], codes[2], codes[3]), "½+¾="); +} + #[wasm_bindgen_test] fn includes() { let str = JsString::from("Blue Whale"); From 30fc99b724935442a938395e998db83368d6b822 Mon Sep 17 00:00:00 2001 From: Danielle Pham Date: Mon, 13 Aug 2018 17:03:58 -0400 Subject: [PATCH 3/3] Use u32 params for String.from_char_code bindings --- crates/js-sys/src/lib.rs | 10 +++++----- crates/js-sys/tests/wasm/JsString.rs | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 60c92d87..79941579 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -2950,23 +2950,23 @@ extern "C" { /// There are a few bindings to `from_char_code` in `js-sys`: `from_char_code1`, `from_char_code2`, etc... /// with different arities. #[wasm_bindgen(static_method_of = JsString, js_class = "String", js_name = fromCharCode)] - pub fn from_char_code1(a: f64) -> JsString; + pub fn from_char_code1(a: u32) -> JsString; /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode #[wasm_bindgen(static_method_of = JsString, js_class = "String", js_name = fromCharCode)] - pub fn from_char_code2(a: f64, b: f64) -> JsString; + pub fn from_char_code2(a: u32, b: u32) -> JsString; /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode #[wasm_bindgen(static_method_of = JsString, js_class = "String", js_name = fromCharCode)] - pub fn from_char_code3(a: f64, b: f64, c: f64) -> JsString; + pub fn from_char_code3(a: u32, b: u32, c: u32) -> JsString; /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode #[wasm_bindgen(static_method_of = JsString, js_class = "String", js_name = fromCharCode)] - pub fn from_char_code4(a: f64, b: f64, c: f64, d: f64) -> JsString; + pub fn from_char_code4(a: u32, b: u32, c: u32, d: u32) -> JsString; /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode #[wasm_bindgen(static_method_of = JsString, js_class = "String", js_name = fromCharCode)] - pub fn from_char_code5(a: f64, b: f64, c: f64, d: f64, e: f64) -> JsString; + pub fn from_char_code5(a: u32, b: u32, c: u32, d: u32, e: u32) -> JsString; /// The `includes()` method determines whether one string may be found /// within another string, returning true or false as appropriate. diff --git a/crates/js-sys/tests/wasm/JsString.rs b/crates/js-sys/tests/wasm/JsString.rs index 0c536c34..4b276ba3 100644 --- a/crates/js-sys/tests/wasm/JsString.rs +++ b/crates/js-sys/tests/wasm/JsString.rs @@ -61,8 +61,8 @@ fn ends_with() { #[wasm_bindgen_test] fn from_char_code() { let s = "½+¾="; - let codes : Vec = s.chars() - .map(|char| char as u32 as f64) + let codes : Vec = s.chars() + .map(|char| char as u32) .collect(); assert_eq!(JsString::from_char_code1(codes[0]), "½");