Skip to content

Commit

Permalink
Add more variadic bindings
Browse files Browse the repository at this point in the history
Adds variadic bindings in addition to fixed ones for:
 - `Array::of` - as a side benefit, this allow to more easily create Arrays from Rust slices of `[JsValue]`.
 - `Function::call` - similarly, this allows to call JS functions with Rust slices of JS values without first converting them to `Array` for `Function::apply`.
 - `JsString::from_code_point` - allows to create JS strings using slice of codes in WASM memory.
 - `JsString::from_char_code` - same as above, but also uses updated signature with `u16` instead of `u32` (partially helps with #1460 at least for the new method).
  • Loading branch information
RReverser committed Apr 26, 2019
1 parent 26f9d86 commit 5876dcf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
23 changes: 23 additions & 0 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ extern "C" {
///
/// There are a few bindings to `of` in `js-sys`: `of1`, `of2`, etc...
/// with different arities.
#[wasm_bindgen(variadic)]
pub fn of(values: &[JsValue]) -> Array;

/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of)
#[wasm_bindgen(static_method_of = Array, js_name = of)]
pub fn of1(a: &JsValue) -> Array;

Expand Down Expand Up @@ -1005,6 +1009,13 @@ extern "C" {
#[wasm_bindgen(method, catch)]
pub fn apply(this: &Function, context: &JsValue, args: &Array) -> Result<JsValue, JsValue>;

/// The `call()` method calls a function with a given this value and
/// arguments provided individually.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call)
#[wasm_bindgen(method, catch, js_name = call, variadic)]
pub fn call(this: &Function, context: &JsValue, args: &[JsValue]) -> Result<JsValue, JsValue>;

/// The `call()` method calls a function with a given this value and
/// arguments provided individually.
///
Expand Down Expand Up @@ -3400,6 +3411,14 @@ extern "C" {
///
/// There are a few bindings to `from_char_code` in `js-sys`: `from_char_code1`, `from_char_code2`, etc...
/// with different arities.
///
/// Additionally, this function accepts `u16` for character codes, but
/// fixing others requires a breaking change release
/// (see https://github.com/rustwasm/wasm-bindgen/issues/1460 for details).
#[wasm_bindgen(static_method_of = JsString, js_class = "String", js_name = fromCharCode, variadic)]
pub fn from_char_code(char_codes: &[u16]) -> JsString;

/// [MDN documentation](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_code1(a: u32) -> JsString;

Expand Down Expand Up @@ -3432,6 +3451,10 @@ extern "C" {
///
/// There are a few bindings to `from_code_point` in `js-sys`: `from_code_point1`, `from_code_point2`, etc...
/// with different arities.
#[wasm_bindgen(catch, static_method_of = JsString, js_class = "String", js_name = fromCodePoint, variadic)]
pub fn from_code_point(code_points: &[u32]) -> Result<JsString, JsValue>;

/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint)
#[wasm_bindgen(catch, static_method_of = JsString, js_class = "String", js_name = fromCodePoint)]
pub fn from_code_point1(a: u32) -> Result<JsString, JsValue>;

Expand Down
14 changes: 14 additions & 0 deletions crates/js-sys/tests/wasm/JsString.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ fn from_char_code() {
JsString::from_char_code4(codes[0], codes[1], codes[2], codes[3]),
"½+¾="
);

let codes_u16: Vec<u16> = codes.into_iter().map(|code| {
assert!(code <= u16::max_value());
code as u16
}).collect();

assert_eq!(
JsString::from_char_code(&codes_u16),
"½+¾="
);
}

#[wasm_bindgen_test]
Expand All @@ -111,6 +121,10 @@ fn from_code_point() {
JsString::from_code_point4(codes[0], codes[1], codes[2], codes[3]).unwrap(),
"☃★♲你"
);
assert_eq!(
JsString::from_code_point(&codes).unwrap(),
"☃★♲你"
);

assert!(!JsString::from_code_point1(0x10FFFF).is_err());
assert!(JsString::from_code_point1(0x110000).is_err());
Expand Down
14 changes: 7 additions & 7 deletions crates/webidl-tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ fn variadic() {
let f = Variadic::new().unwrap();
assert_eq!(f.sum_5(1, 2, 3, 4, 5), 15);
assert_eq!(
f.sum(&::js_sys::Array::of5(
&JsValue::from_f64(1f64),
&JsValue::from_f64(2f64),
&JsValue::from_f64(3f64),
&JsValue::from_f64(4f64),
&JsValue::from_f64(5f64),
)),
f.sum(&::js_sys::Array::of(&[
JsValue::from_f64(1f64),
JsValue::from_f64(2f64),
JsValue::from_f64(3f64),
JsValue::from_f64(4f64),
JsValue::from_f64(5f64),
])),
15
);
}
Expand Down

0 comments on commit 5876dcf

Please sign in to comment.