| 1 |
use js_sys::*; |
| 2 |
use wasm_bindgen::prelude::*; |
| 3 |
use wasm_bindgen::JsCast; |
| 4 |
use wasm_bindgen_test::*; |
| 5 |
|
| 6 |
#[wasm_bindgen] |
| 7 |
extern "C" { |
| 8 |
#[wasm_bindgen(js_name = max, js_namespace = Math)] |
| 9 |
static MAX: Function; |
| 10 |
|
| 11 |
type ArrayPrototype; |
| 12 |
#[wasm_bindgen(method, getter, structural)] |
| 13 |
pub fn push(this: &ArrayPrototype) -> Function; |
| 14 |
#[wasm_bindgen(js_name = prototype, js_namespace = Array)] |
| 15 |
static ARRAY_PROTOTYPE2: ArrayPrototype; |
| 16 |
} |
| 17 |
|
| 18 |
#[wasm_bindgen_test] |
| 19 |
fn apply() { |
| 20 |
let args = Array::new(); |
| 21 |
args.push(&1.into()); |
| 22 |
args.push(&2.into()); |
| 23 |
args.push(&3.into()); |
| 24 |
assert_eq!(MAX.apply(&JsValue::undefined(), &args).unwrap(), 3); |
| 25 |
|
| 26 |
let arr = JsValue::from(Array::new()); |
| 27 |
let args = Array::new(); |
| 28 |
args.push(&1.into()); |
| 29 |
ARRAY_PROTOTYPE2.push().apply(&arr, &args).unwrap(); |
| 30 |
assert_eq!(Array::from(&arr).length(), 1); |
| 31 |
} |
| 32 |
|
| 33 |
#[wasm_bindgen(module = "tests/wasm/Function.js")] |
| 34 |
extern "C" { |
| 35 |
fn get_function_to_bind() -> Function; |
| 36 |
fn get_value_to_bind_to() -> 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 |
|
| 42 |
} |
| 43 |
|
| 44 |
#[wasm_bindgen_test] |
| 45 |
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() { |
| 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( |
| 94 |
&JsValue::NULL, |
| 95 |
&JsValue::from(2), |
| 96 |
&JsValue::from(3), |
| 97 |
&JsValue::from(4), |
| 98 |
); |
| 99 |
|
| 100 |
let arr = Array::from(&call_function(&prepended_list)); |
| 101 |
|
| 102 |
assert_eq!(arr.pop(), 4); |
| 103 |
assert_eq!(arr.pop(), 3); |
| 104 |
assert_eq!(arr.pop(), 2); |
| 105 |
|
| 106 |
let adder = add_arguments(); |
| 107 |
let always_69 = adder.bind2(&JsValue::NULL, &JsValue::from(66), &JsValue::from(3)); |
| 108 |
|
| 109 |
assert_eq!(call_function(&always_69), 69); |
| 110 |
} |
| 111 |
|
| 112 |
#[wasm_bindgen_test] |
| 113 |
fn length() { |
| 114 |
assert_eq!(MAX.length(), 2); |
| 115 |
assert_eq!(ARRAY_PROTOTYPE2.push().length(), 1); |
| 116 |
} |
| 117 |
|
| 118 |
#[wasm_bindgen_test] |
| 119 |
fn name() { |
| 120 |
assert_eq!(JsValue::from(MAX.name()), "max"); |
| 121 |
assert_eq!(JsValue::from(ARRAY_PROTOTYPE2.push().name()), "push"); |
| 122 |
} |
| 123 |
|
| 124 |
#[wasm_bindgen_test] |
| 125 |
fn to_string() { |
| 126 |
assert!(MAX.to_string().length() > 0); |
| 127 |
} |
| 128 |
|
| 129 |
#[wasm_bindgen_test] |
| 130 |
fn function_inheritance() { |
| 131 |
assert!(MAX.is_instance_of::<Function>()); |
| 132 |
assert!(MAX.is_instance_of::<Object>()); |
| 133 |
let _: &Object = MAX.as_ref(); |
| 134 |
} |