1 use js_sys::*;
2 use wasm_bindgen::prelude::*;
3 use wasm_bindgen_test::*;
4
5 #[wasm_bindgen(module = "tests/wasm/Proxy.js")]
6 extern "C" {
proxy_target() -> JsValue7 fn proxy_target() -> JsValue;
proxy_handler() -> Object8 fn proxy_handler() -> Object;
9
10 type Custom;
11 #[wasm_bindgen(method, getter, structural, catch)]
a(this: &Custom) -> Result<u32, JsValue>12 fn a(this: &Custom) -> Result<u32, JsValue>;
13 #[wasm_bindgen(method, getter, structural, catch)]
b(this: &Custom) -> Result<u32, JsValue>14 fn b(this: &Custom) -> Result<u32, JsValue>;
15
16 type RevocableResult;
17 #[wasm_bindgen(method, getter, structural)]
proxy(this: &RevocableResult) -> JsValue18 fn proxy(this: &RevocableResult) -> JsValue;
19 #[wasm_bindgen(method, getter, structural)]
revoke(this: &RevocableResult) -> Function20 fn revoke(this: &RevocableResult) -> Function;
21 }
22
23 #[wasm_bindgen_test]
new()24 fn new() {
25 let proxy = Proxy::new(&proxy_target(), &proxy_handler());
26 let proxy = Custom::from(JsValue::from(proxy));
27 assert_eq!(proxy.a().unwrap(), 100);
28 assert_eq!(proxy.b().unwrap(), 37);
29 }
30
31 #[wasm_bindgen_test]
revocable()32 fn revocable() {
33 let result = Proxy::revocable(&proxy_target(), &proxy_handler());
34 let result = RevocableResult::from(JsValue::from(result));
35 let proxy = result.proxy();
36 let revoke = result.revoke();
37
38 let obj = Custom::from(proxy);
39 assert_eq!(obj.a().unwrap(), 100);
40 assert_eq!(obj.b().unwrap(), 37);
41 revoke.apply(&JsValue::undefined(), &Array::new()).unwrap();
42 assert!(obj.a().is_err());
43 assert!(obj.b().is_err());
44 assert!(JsValue::from(obj).is_object());
45 }
46