mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-21 08:41:35 +00:00
Bindings for Proxy.revocable()
Signed-off-by: Stephan Renatus <srenatus@chef.io>
This commit is contained in:
@ -1042,6 +1042,12 @@ extern {
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new(target: &JsValue, handler: &Object) -> Proxy;
|
||||
|
||||
/// The Proxy.revocable() method is used to create a revocable Proxy object.
|
||||
///
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/revocable
|
||||
#[wasm_bindgen(static_method_of = Proxy)]
|
||||
pub fn revocable(target: &JsValue, handler: &Object) -> Object;
|
||||
}
|
||||
|
||||
// Set
|
||||
|
@ -35,3 +35,42 @@ fn new() {
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn revocable() {
|
||||
project()
|
||||
.file("src/lib.rs", r#"
|
||||
#![feature(proc_macro, wasm_custom_section)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::js;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn new_revocable_proxy(target: JsValue, handler: js::Object) -> js::Object {
|
||||
js::Proxy::revocable(&target, &handler)
|
||||
}
|
||||
"#)
|
||||
.file("test.ts", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const target = { a: 100 };
|
||||
const handler = {
|
||||
get: function(obj: any, prop: any) {
|
||||
return prop in obj ? obj[prop] : 37;
|
||||
}
|
||||
};
|
||||
const { proxy, revoke } =
|
||||
wasm.new_revocable_proxy(target, handler);
|
||||
assert.equal(proxy.a, 100);
|
||||
assert.equal(proxy.b, 37);
|
||||
revoke();
|
||||
assert.throws(() => { proxy.a }, TypeError);
|
||||
assert.throws(() => { proxy.b }, TypeError);
|
||||
assert.equal(typeof proxy, "object");
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
Reference in New Issue
Block a user