Merge pull request #1633 from Warfields/master

Attempted to tackle #1622
This commit is contained in:
Alex Crichton 2019-06-29 11:29:01 +02:00 committed by GitHub
commit bf631eda1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 116 additions and 4 deletions

View File

@ -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)

View File

@ -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);
};

View File

@ -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]