Skip to content

Commit bf631ed

Browse files
authored
Merge pull request #1633 from Warfields/master
Attempted to tackle #1622
2 parents 792ab40 + 367a56e commit bf631ed

File tree

3 files changed

+116
-4
lines changed

3 files changed

+116
-4
lines changed

crates/js-sys/src/lib.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,9 +1076,53 @@ extern "C" {
10761076
/// with a given sequence of arguments preceding any provided when the new function is called.
10771077
///
10781078
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind)
1079-
#[wasm_bindgen(method)]
1079+
#[wasm_bindgen(method, js_name = bind)]
10801080
pub fn bind(this: &Function, context: &JsValue) -> Function;
10811081

1082+
1083+
/// The bind() method creates a new function that, when called, has its this keyword set to the provided value,
1084+
/// with a given sequence of arguments preceding any provided when the new function is called.
1085+
///
1086+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind)
1087+
#[wasm_bindgen(method, js_name = bind)]
1088+
pub fn bind0(this: &Function, context: &JsValue) -> Function;
1089+
1090+
/// The bind() method creates a new function that, when called, has its this keyword set to the provided value,
1091+
/// with a given sequence of arguments preceding any provided when the new function is called.
1092+
///
1093+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind)
1094+
#[wasm_bindgen(method, js_name = bind)]
1095+
pub fn bind1(
1096+
this: &Function,
1097+
context: &JsValue,
1098+
arg1: &JsValue,
1099+
) -> Function;
1100+
1101+
/// The bind() method creates a new function that, when called, has its this keyword set to the provided value,
1102+
/// with a given sequence of arguments preceding any provided when the new function is called.
1103+
///
1104+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind)
1105+
#[wasm_bindgen(method, js_name = bind)]
1106+
pub fn bind2(
1107+
this: &Function,
1108+
context: &JsValue,
1109+
arg1: &JsValue,
1110+
arg2: &JsValue,
1111+
) -> Function;
1112+
1113+
/// The bind() method creates a new function that, when called, has its this keyword set to the provided value,
1114+
/// with a given sequence of arguments preceding any provided when the new function is called.
1115+
///
1116+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind)
1117+
#[wasm_bindgen(method, js_name = bind)]
1118+
pub fn bind3(
1119+
this: &Function,
1120+
context: &JsValue,
1121+
arg1: &JsValue,
1122+
arg2: &JsValue,
1123+
arg3: &JsValue,
1124+
) -> Function;
1125+
10821126
/// The length property indicates the number of arguments expected by the function.
10831127
///
10841128
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length)

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 function() {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: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,74 @@ fn apply() {
3434
extern "C" {
3535
fn get_function_to_bind() -> Function;
3636
fn get_value_to_bind_to() -> JsValue;
37-
fn call_function(f: Function) -> JsValue;
37+
fn list() -> Function;
38+
fn add_arguments() -> Function;
39+
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() {
4246
let f = get_function_to_bind();
4347
let new_f = f.bind(&get_value_to_bind_to());
44-
assert_eq!(call_function(f), 1);
45-
assert_eq!(call_function(new_f), 2);
48+
assert_eq!(call_function(&f), 1);
49+
assert_eq!(call_function(&new_f), 2);
50+
}
51+
52+
#[wasm_bindgen_test]
53+
fn bind0() {
54+
let f = get_function_to_bind();
55+
let new_f = f.bind0(&get_value_to_bind_to());
56+
assert_eq!(call_function(&f), 1);
57+
assert_eq!(call_function(&new_f), 2);
58+
}
59+
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+
assert_eq!(Array::from(&call_function(&prepended_list)).pop(), 2);
66+
67+
let adder = add_arguments();
68+
let add_42 = adder.bind1(&JsValue::NULL, &JsValue::from(42));
69+
70+
assert_eq!(call_function_arg(&add_42, JsValue::from(1)), 43);
71+
assert_eq!(call_function_arg(&add_42, JsValue::from(378)), 420);
72+
}
73+
74+
#[wasm_bindgen_test]
75+
fn bind2() {
76+
let a_list = list();
77+
let prepended_list = a_list.bind2(&JsValue::NULL, &JsValue::from(2), &JsValue::from(3));
78+
79+
let arr = Array::from(&call_function(&prepended_list));
80+
81+
assert_eq!(arr.pop(), 3);
82+
assert_eq!(arr.pop(), 2);
83+
84+
let adder = add_arguments();
85+
let always_69 = adder.bind2(&JsValue::NULL, &JsValue::from(66), &JsValue::from(3));
86+
87+
assert_eq!(call_function(&always_69), 69);
88+
}
89+
90+
#[wasm_bindgen_test]
91+
fn bind3() {
92+
let a_list = list();
93+
let prepended_list = a_list.bind3(&JsValue::NULL, &JsValue::from(2), &JsValue::from(3), &JsValue::from(4));
94+
95+
let arr = Array::from(&call_function(&prepended_list));
96+
97+
assert_eq!(arr.pop(), 4);
98+
assert_eq!(arr.pop(), 3);
99+
assert_eq!(arr.pop(), 2);
100+
101+
let adder = add_arguments();
102+
let always_69 = adder.bind2(&JsValue::NULL, &JsValue::from(66), &JsValue::from(3));
103+
104+
assert_eq!(call_function(&always_69), 69);
46105
}
47106

48107
#[wasm_bindgen_test]

0 commit comments

Comments
 (0)