Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempted to tackle #1622 #1633

Merged
merged 3 commits into from
Jun 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,9 +1076,53 @@ 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)]
#[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.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind)
#[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.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length)
Expand Down
9 changes: 9 additions & 0 deletions crates/js-sys/tests/wasm/Function.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ exports.get_function_to_bind = function() {
exports.get_value_to_bind_to = function() {
return { x: 2 };
};
exports.list = function() {
return 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);
};
65 changes: 62 additions & 3 deletions crates/js-sys/tests/wasm/Function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,74 @@ fn apply() {
extern "C" {
fn get_function_to_bind() -> Function;
fn get_value_to_bind_to() -> JsValue;
fn call_function(f: Function) -> 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);
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));

assert_eq!(Array::from(&call_function(&prepended_list)).pop(), 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 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]
Expand Down