Merge pull request #692 from quelledanielle/js_string_bindings

Add bindings for static method String.from_char_code
This commit is contained in:
Nick Fitzgerald 2018-08-13 14:27:00 -07:00 committed by GitHub
commit ea27a172d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 1 deletions

View File

@ -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: 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: 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: 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: 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: 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.
///

View File

@ -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<u32> = s.chars()
.map(|char| char as u32)
.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");

View File

@ -473,7 +473,10 @@ impl<'a> ConvertToAst<(BindgenAttrs, &'a Option<String>)> 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 {