Proxy.rs 1.4 KB

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