2018-07-04 10:27:32 +03:00
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
|
|
|
use project;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn new() {
|
|
|
|
project()
|
|
|
|
.file("src/lib.rs", r#"
|
2018-07-17 17:11:30 +03:00
|
|
|
#![feature(use_extern_macros, wasm_custom_section)]
|
2018-07-04 10:27:32 +03:00
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
use wasm_bindgen::js;
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn new_proxy(target: JsValue, handler: js::Object) -> js::Proxy {
|
|
|
|
js::Proxy::new(&target, &handler)
|
|
|
|
}
|
|
|
|
"#)
|
2018-07-05 10:03:16 +03:00
|
|
|
.file("test.js", r#"
|
2018-07-04 10:27:32 +03:00
|
|
|
import * as assert from "assert";
|
|
|
|
import * as wasm from "./out";
|
|
|
|
|
|
|
|
export function test() {
|
|
|
|
const target = { a: 100 };
|
|
|
|
const handler = {
|
2018-07-05 10:03:16 +03:00
|
|
|
get: function(obj, prop) {
|
2018-07-04 10:27:32 +03:00
|
|
|
return prop in obj ? obj[prop] : 37;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const proxy = wasm.new_proxy(target, handler);
|
|
|
|
assert.equal(proxy.a, 100);
|
|
|
|
assert.equal(proxy.b, 37);
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.test()
|
|
|
|
}
|
2018-07-04 11:05:42 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn revocable() {
|
|
|
|
project()
|
|
|
|
.file("src/lib.rs", r#"
|
2018-07-17 17:11:30 +03:00
|
|
|
#![feature(use_extern_macros, wasm_custom_section)]
|
2018-07-04 11:05:42 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
"#)
|
2018-07-05 10:03:16 +03:00
|
|
|
.file("test.js", r#"
|
2018-07-04 11:05:42 +03:00
|
|
|
import * as assert from "assert";
|
|
|
|
import * as wasm from "./out";
|
|
|
|
|
|
|
|
export function test() {
|
|
|
|
const target = { a: 100 };
|
|
|
|
const handler = {
|
2018-07-05 10:03:16 +03:00
|
|
|
get: function(obj, prop) {
|
2018-07-04 11:05:42 +03:00
|
|
|
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()
|
|
|
|
}
|