From caa86a07a01c408a8b57e10a6d521d6c549614ac Mon Sep 17 00:00:00 2001 From: Samuel Warfield Date: Thu, 27 Jun 2019 15:48:18 -0600 Subject: [PATCH 1/3] Attempted to tackle #1622 --- crates/js-sys/src/lib.rs | 40 ++++++++++++++++++++++++++-- crates/js-sys/tests/wasm/Function.rs | 2 +- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 5dd1a8be395..c11902de7be 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -1076,8 +1076,44 @@ extern "C" { /// with a given sequence of arguments preceding any provided when the new function is called. /// /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) - #[wasm_bindgen(method)] - pub fn bind(this: &Function, context: &JsValue) -> Function; + #[wasm_bindgen(method, js_name = bind)] + pub fn bind0(this: &Function, context: &JsValue) -> Function; + + /// The bind() method creates a new function that, when called, has its this keyword set to the provided value, + /// with a given sequence of arguments preceding any provided when the new function is called. + /// + /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) + #[wasm_bindgen(method, js_name = bind)] + pub fn bind1( + this: &Function, + context: &JsValue, + arg1: &JsValue, + ) -> Function; + + /// The bind() method creates a new function that, when called, has its this keyword set to the provided value, + /// with a given sequence of arguments preceding any provided when the new function is called. + /// + /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) + #[wasm_bindgen(method, js_name = bind)] + pub fn bind2( + this: &Function, + context: &JsValue, + arg1: &JsValue, + arg2: &JsValue, + ) -> Function; + + /// The bind() method creates a new function that, when called, has its this keyword set to the provided value, + /// with a given sequence of arguments preceding any provided when the new function is called. + /// + /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) + #[wasm_bindgen(method, js_name = bind)] + pub fn bind3( + this: &Function, + context: &JsValue, + arg1: &JsValue, + arg2: &JsValue, + arg3: &JsValue, + ) -> Function; /// The length property indicates the number of arguments expected by the function. /// diff --git a/crates/js-sys/tests/wasm/Function.rs b/crates/js-sys/tests/wasm/Function.rs index b3ad413baa9..34faf1a98a1 100644 --- a/crates/js-sys/tests/wasm/Function.rs +++ b/crates/js-sys/tests/wasm/Function.rs @@ -40,7 +40,7 @@ extern "C" { #[wasm_bindgen_test] fn bind() { let f = get_function_to_bind(); - let new_f = f.bind(&get_value_to_bind_to()); + let new_f = f.bind0(&get_value_to_bind_to()); assert_eq!(call_function(f), 1); assert_eq!(call_function(new_f), 2); } From 06d0704cf825fa5bab6a13a36d3aa35da616126e Mon Sep 17 00:00:00 2001 From: Samuel Warfield Date: Fri, 28 Jun 2019 12:03:45 -0600 Subject: [PATCH 2/3] Added tests for bind1() --- crates/js-sys/src/lib.rs | 8 ++++++++ crates/js-sys/tests/wasm/Function.js | 9 +++++++++ crates/js-sys/tests/wasm/Function.rs | 25 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index c11902de7be..8a78d4c8513 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -1072,6 +1072,14 @@ extern "C" { arg3: &JsValue, ) -> Result; + /// The bind() method creates a new function that, when called, has its this keyword set to the provided value, + /// with a given sequence of arguments preceding any provided when the new function is called. + /// + /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) + #[wasm_bindgen(method, js_name = bind)] + pub fn bind(this: &Function, context: &JsValue) -> Function; + + /// The bind() method creates a new function that, when called, has its this keyword set to the provided value, /// with a given sequence of arguments preceding any provided when the new function is called. /// diff --git a/crates/js-sys/tests/wasm/Function.js b/crates/js-sys/tests/wasm/Function.js index f445792db1e..a687622de97 100644 --- a/crates/js-sys/tests/wasm/Function.js +++ b/crates/js-sys/tests/wasm/Function.js @@ -5,6 +5,15 @@ exports.get_function_to_bind = function() { exports.get_value_to_bind_to = function() { return { x: 2 }; }; +exports.list = function() { + return Array.prototype.slice.call(arguments); +}; +exports.add_arguments = function() { + return function(arg1, arg2) {return arg1 + arg2} +}; exports.call_function = function(f) { return f(); }; +exports.call_function_arg = function(f, arg1) { + return f(arg1); +}; diff --git a/crates/js-sys/tests/wasm/Function.rs b/crates/js-sys/tests/wasm/Function.rs index 34faf1a98a1..e83d6318340 100644 --- a/crates/js-sys/tests/wasm/Function.rs +++ b/crates/js-sys/tests/wasm/Function.rs @@ -34,17 +34,42 @@ fn apply() { extern "C" { fn get_function_to_bind() -> Function; fn get_value_to_bind_to() -> JsValue; + fn list() -> Function; + fn add_arguments() -> Function; fn call_function(f: Function) -> JsValue; + fn call_function_arg(f: &Function, arg0: JsValue) -> JsValue; + } #[wasm_bindgen_test] fn bind() { + let f = get_function_to_bind(); + let new_f = f.bind(&get_value_to_bind_to()); + assert_eq!(call_function(f), 1); + assert_eq!(call_function(new_f), 2); +} + +#[wasm_bindgen_test] +fn bind0() { let f = get_function_to_bind(); let new_f = f.bind0(&get_value_to_bind_to()); assert_eq!(call_function(f), 1); assert_eq!(call_function(new_f), 2); } +#[wasm_bindgen_test] +fn bind1() { + //let a_list = list(); + //let prepended_list = a_list.bind1(&JsValue::NULL, &JsValue::from(2)); + + let adder = add_arguments(); + let add_42 = adder.bind1(&JsValue::NULL, &JsValue::from(42)); + + assert_eq!(call_function_arg(&add_42, JsValue::from(1)), 43); + assert_eq!(call_function_arg(&add_42, JsValue::from(378)), 420); +} + + #[wasm_bindgen_test] fn length() { assert_eq!(MAX.length(), 2); From 367a56eb2514a9591e968606601cc8169bd011b5 Mon Sep 17 00:00:00 2001 From: Samuel Warfield Date: Fri, 28 Jun 2019 14:14:27 -0600 Subject: [PATCH 3/3] Test are fully implemented --- crates/js-sys/tests/wasm/Function.js | 4 +-- crates/js-sys/tests/wasm/Function.rs | 48 ++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/crates/js-sys/tests/wasm/Function.js b/crates/js-sys/tests/wasm/Function.js index a687622de97..14242a63ce7 100644 --- a/crates/js-sys/tests/wasm/Function.js +++ b/crates/js-sys/tests/wasm/Function.js @@ -6,7 +6,7 @@ exports.get_value_to_bind_to = function() { return { x: 2 }; }; exports.list = function() { - return Array.prototype.slice.call(arguments); + return function() {return Array.prototype.slice.call(arguments);} }; exports.add_arguments = function() { return function(arg1, arg2) {return arg1 + arg2} @@ -16,4 +16,4 @@ exports.call_function = function(f) { }; exports.call_function_arg = function(f, arg1) { return f(arg1); -}; +}; \ No newline at end of file diff --git a/crates/js-sys/tests/wasm/Function.rs b/crates/js-sys/tests/wasm/Function.rs index e83d6318340..a338ab24f5d 100644 --- a/crates/js-sys/tests/wasm/Function.rs +++ b/crates/js-sys/tests/wasm/Function.rs @@ -36,7 +36,7 @@ extern "C" { fn get_value_to_bind_to() -> JsValue; fn list() -> Function; fn add_arguments() -> Function; - fn call_function(f: Function) -> JsValue; + fn call_function(f: &Function) -> JsValue; fn call_function_arg(f: &Function, arg0: JsValue) -> JsValue; } @@ -45,22 +45,24 @@ extern "C" { fn bind() { let f = get_function_to_bind(); let new_f = f.bind(&get_value_to_bind_to()); - assert_eq!(call_function(f), 1); - assert_eq!(call_function(new_f), 2); + assert_eq!(call_function(&f), 1); + assert_eq!(call_function(&new_f), 2); } #[wasm_bindgen_test] fn bind0() { let f = get_function_to_bind(); let new_f = f.bind0(&get_value_to_bind_to()); - assert_eq!(call_function(f), 1); - assert_eq!(call_function(new_f), 2); + assert_eq!(call_function(&f), 1); + assert_eq!(call_function(&new_f), 2); } #[wasm_bindgen_test] fn bind1() { - //let a_list = list(); - //let prepended_list = a_list.bind1(&JsValue::NULL, &JsValue::from(2)); + let a_list = list(); + let prepended_list = a_list.bind1(&JsValue::NULL, &JsValue::from(2)); + + assert_eq!(Array::from(&call_function(&prepended_list)).pop(), 2); let adder = add_arguments(); let add_42 = adder.bind1(&JsValue::NULL, &JsValue::from(42)); @@ -69,6 +71,38 @@ fn bind1() { assert_eq!(call_function_arg(&add_42, JsValue::from(378)), 420); } +#[wasm_bindgen_test] +fn bind2() { + let a_list = list(); + let prepended_list = a_list.bind2(&JsValue::NULL, &JsValue::from(2), &JsValue::from(3)); + + let arr = Array::from(&call_function(&prepended_list)); + + assert_eq!(arr.pop(), 3); + assert_eq!(arr.pop(), 2); + + let adder = add_arguments(); + let always_69 = adder.bind2(&JsValue::NULL, &JsValue::from(66), &JsValue::from(3)); + + assert_eq!(call_function(&always_69), 69); +} + +#[wasm_bindgen_test] +fn bind3() { + let a_list = list(); + let prepended_list = a_list.bind3(&JsValue::NULL, &JsValue::from(2), &JsValue::from(3), &JsValue::from(4)); + + let arr = Array::from(&call_function(&prepended_list)); + + assert_eq!(arr.pop(), 4); + assert_eq!(arr.pop(), 3); + assert_eq!(arr.pop(), 2); + + let adder = add_arguments(); + let always_69 = adder.bind2(&JsValue::NULL, &JsValue::from(66), &JsValue::from(3)); + + assert_eq!(call_function(&always_69), 69); +} #[wasm_bindgen_test] fn length() {