Bindings for Proxy.revocable()

Signed-off-by: Stephan Renatus <srenatus@chef.io>
This commit is contained in:
Stephan Renatus
2018-07-04 10:05:42 +02:00
parent 2b8e789c9c
commit b704ceeb3a
2 changed files with 45 additions and 0 deletions

View File

@ -1042,6 +1042,12 @@ extern {
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
#[wasm_bindgen(constructor)] #[wasm_bindgen(constructor)]
pub fn new(target: &JsValue, handler: &Object) -> Proxy; 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 // Set

View File

@ -35,3 +35,42 @@ fn new() {
"#) "#)
.test() .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()
}