diff --git a/src/js.rs b/src/js.rs index c3ab4646..b7fead95 100644 --- a/src/js.rs +++ b/src/js.rs @@ -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 diff --git a/tests/all/js_globals/Proxy.rs b/tests/all/js_globals/Proxy.rs index 990409e6..c92d46a1 100644 --- a/tests/all/js_globals/Proxy.rs +++ b/tests/all/js_globals/Proxy.rs @@ -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() +}