Skip to content

Commit 4865b08

Browse files
authored
Merge pull request #1491 from RReverser/more-variadic
Add missing variadic bindings for string methods
2 parents 26f9d86 + 85eea18 commit 4865b08

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

crates/js-sys/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3400,6 +3400,14 @@ extern "C" {
34003400
///
34013401
/// There are a few bindings to `from_char_code` in `js-sys`: `from_char_code1`, `from_char_code2`, etc...
34023402
/// with different arities.
3403+
///
3404+
/// Additionally, this function accepts `u16` for character codes, but
3405+
/// fixing others requires a breaking change release
3406+
/// (see https://github.com/rustwasm/wasm-bindgen/issues/1460 for details).
3407+
#[wasm_bindgen(static_method_of = JsString, js_class = "String", js_name = fromCharCode, variadic)]
3408+
pub fn from_char_code(char_codes: &[u16]) -> JsString;
3409+
3410+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode)
34033411
#[wasm_bindgen(static_method_of = JsString, js_class = "String", js_name = fromCharCode)]
34043412
pub fn from_char_code1(a: u32) -> JsString;
34053413

@@ -3432,6 +3440,10 @@ extern "C" {
34323440
///
34333441
/// There are a few bindings to `from_code_point` in `js-sys`: `from_code_point1`, `from_code_point2`, etc...
34343442
/// with different arities.
3443+
#[wasm_bindgen(catch, static_method_of = JsString, js_class = "String", js_name = fromCodePoint, variadic)]
3444+
pub fn from_code_point(code_points: &[u32]) -> Result<JsString, JsValue>;
3445+
3446+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint)
34353447
#[wasm_bindgen(catch, static_method_of = JsString, js_class = "String", js_name = fromCodePoint)]
34363448
pub fn from_code_point1(a: u32) -> Result<JsString, JsValue>;
34373449

crates/js-sys/tests/wasm/JsString.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ fn from_char_code() {
9191
JsString::from_char_code4(codes[0], codes[1], codes[2], codes[3]),
9292
"½+¾="
9393
);
94+
95+
let codes_u16: Vec<u16> = codes.into_iter().map(|code| {
96+
assert!(code <= u32::from(u16::max_value()));
97+
code as u16
98+
}).collect();
99+
100+
assert_eq!(
101+
JsString::from_char_code(&codes_u16),
102+
"½+¾="
103+
);
94104
}
95105

96106
#[wasm_bindgen_test]
@@ -111,6 +121,10 @@ fn from_code_point() {
111121
JsString::from_code_point4(codes[0], codes[1], codes[2], codes[3]).unwrap(),
112122
"☃★♲你"
113123
);
124+
assert_eq!(
125+
JsString::from_code_point(&codes).unwrap(),
126+
"☃★♲你"
127+
);
114128

115129
assert!(!JsString::from_code_point1(0x10FFFF).is_err());
116130
assert!(JsString::from_code_point1(0x110000).is_err());

0 commit comments

Comments
 (0)