mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-11 20:11:22 +00:00
Port Proxy
tests to wasm
This commit is contained in:
11
crates/js-sys/tests/wasm/Proxy.js
Normal file
11
crates/js-sys/tests/wasm/Proxy.js
Normal file
@ -0,0 +1,11 @@
|
||||
exports.proxy_target = function() {
|
||||
return { a: 100 };
|
||||
};
|
||||
|
||||
exports.proxy_handler = function() {
|
||||
return {
|
||||
get: function(obj, prop) {
|
||||
return prop in obj ? obj[prop] : 37;
|
||||
}
|
||||
};
|
||||
};
|
46
crates/js-sys/tests/wasm/Proxy.rs
Normal file
46
crates/js-sys/tests/wasm/Proxy.rs
Normal file
@ -0,0 +1,46 @@
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen_test::*;
|
||||
use js_sys::*;
|
||||
|
||||
#[wasm_bindgen(module = "tests/wasm/Proxy.js", version = "*")]
|
||||
extern {
|
||||
fn proxy_target() -> JsValue;
|
||||
fn proxy_handler() -> Object;
|
||||
|
||||
type Custom;
|
||||
#[wasm_bindgen(method, getter, structural, catch)]
|
||||
fn a(this: &Custom) -> Result<u32, JsValue>;
|
||||
#[wasm_bindgen(method, getter, structural, catch)]
|
||||
fn b(this: &Custom) -> Result<u32, JsValue>;
|
||||
|
||||
|
||||
type RevocableResult;
|
||||
#[wasm_bindgen(method, getter, structural)]
|
||||
fn proxy(this: &RevocableResult) -> JsValue;
|
||||
#[wasm_bindgen(method, getter, structural)]
|
||||
fn revoke(this: &RevocableResult) -> Function;
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn new() {
|
||||
let proxy = Proxy::new(&proxy_target(), &proxy_handler());
|
||||
let proxy = Custom::from(JsValue::from(proxy));
|
||||
assert_eq!(proxy.a().unwrap(), 100);
|
||||
assert_eq!(proxy.b().unwrap(), 37);
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn revocable() {
|
||||
let result = Proxy::revocable(&proxy_target(), &proxy_handler());
|
||||
let result = RevocableResult::from(JsValue::from(result));
|
||||
let proxy = result.proxy();
|
||||
let revoke = result.revoke();
|
||||
|
||||
let obj = Custom::from(proxy);
|
||||
assert_eq!(obj.a().unwrap(), 100);
|
||||
assert_eq!(obj.b().unwrap(), 37);
|
||||
revoke.apply(&JsValue::undefined(), &Array::new()).unwrap();
|
||||
assert!(obj.a().is_err());
|
||||
assert!(obj.b().is_err());
|
||||
assert!(JsValue::from(obj).is_object());
|
||||
}
|
@ -22,3 +22,4 @@ pub mod MapIterator;
|
||||
pub mod Math;
|
||||
pub mod Number;
|
||||
pub mod Object;
|
||||
pub mod Proxy;
|
||||
|
Reference in New Issue
Block a user