Skip to content

Commit 06d0704

Browse files
author
Samuel Warfield
committed
Added tests for bind1()
1 parent caa86a0 commit 06d0704

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

crates/js-sys/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,14 @@ extern "C" {
10721072
arg3: &JsValue,
10731073
) -> Result<JsValue, JsValue>;
10741074

1075+
/// The bind() method creates a new function that, when called, has its this keyword set to the provided value,
1076+
/// with a given sequence of arguments preceding any provided when the new function is called.
1077+
///
1078+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind)
1079+
#[wasm_bindgen(method, js_name = bind)]
1080+
pub fn bind(this: &Function, context: &JsValue) -> Function;
1081+
1082+
10751083
/// The bind() method creates a new function that, when called, has its this keyword set to the provided value,
10761084
/// with a given sequence of arguments preceding any provided when the new function is called.
10771085
///

crates/js-sys/tests/wasm/Function.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ exports.get_function_to_bind = function() {
55
exports.get_value_to_bind_to = function() {
66
return { x: 2 };
77
};
8+
exports.list = function() {
9+
return Array.prototype.slice.call(arguments);
10+
};
11+
exports.add_arguments = function() {
12+
return function(arg1, arg2) {return arg1 + arg2}
13+
};
814
exports.call_function = function(f) {
915
return f();
1016
};
17+
exports.call_function_arg = function(f, arg1) {
18+
return f(arg1);
19+
};

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,42 @@ fn apply() {
3434
extern "C" {
3535
fn get_function_to_bind() -> Function;
3636
fn get_value_to_bind_to() -> JsValue;
37+
fn list() -> Function;
38+
fn add_arguments() -> Function;
3739
fn call_function(f: Function) -> JsValue;
40+
fn call_function_arg(f: &Function, arg0: JsValue) -> JsValue;
41+
3842
}
3943

4044
#[wasm_bindgen_test]
4145
fn bind() {
46+
let f = get_function_to_bind();
47+
let new_f = f.bind(&get_value_to_bind_to());
48+
assert_eq!(call_function(f), 1);
49+
assert_eq!(call_function(new_f), 2);
50+
}
51+
52+
#[wasm_bindgen_test]
53+
fn bind0() {
4254
let f = get_function_to_bind();
4355
let new_f = f.bind0(&get_value_to_bind_to());
4456
assert_eq!(call_function(f), 1);
4557
assert_eq!(call_function(new_f), 2);
4658
}
4759

60+
#[wasm_bindgen_test]
61+
fn bind1() {
62+
//let a_list = list();
63+
//let prepended_list = a_list.bind1(&JsValue::NULL, &JsValue::from(2));
64+
65+
let adder = add_arguments();
66+
let add_42 = adder.bind1(&JsValue::NULL, &JsValue::from(42));
67+
68+
assert_eq!(call_function_arg(&add_42, JsValue::from(1)), 43);
69+
assert_eq!(call_function_arg(&add_42, JsValue::from(378)), 420);
70+
}
71+
72+
4873
#[wasm_bindgen_test]
4974
fn length() {
5075
assert_eq!(MAX.length(), 2);

0 commit comments

Comments
 (0)