feat: add Reflect.defineProperty

This commit is contained in:
Jannik Keye
2018-07-04 12:17:01 +02:00
parent 13b3b0d87a
commit 07a726b9dc
2 changed files with 47 additions and 0 deletions

View File

@ -998,6 +998,13 @@ extern "C" {
pub fn construct(target: &Function, arguments_list: &Array) -> Result<JsValue, JsValue>;
#[wasm_bindgen(static_method_of = Reflect, js_name = construct, catch)]
pub fn construct_with_new_target(target: &Function, arguments_list: &Array, new_target: &Function) -> Result<JsValue, JsValue>;
/// The static Reflect.defineProperty() method is like Object.defineProperty()
/// but returns a Boolean.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/defineProperty
#[wasm_bindgen(static_method_of = Reflect, js_name = defineProperty, catch)]
pub fn define_property(target: &Object, property_key: &JsString, attributes: &Object) -> Result<JsValue, JsValue>;
}
// Set

View File

@ -164,3 +164,43 @@ fn construct_with_new_target() {
)
.test()
}
#[test]
fn define_property() {
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 define_property(target: &js::Object, property_key: &js::JsString, attributes: &js::Object) -> JsValue {
let result = js::Reflect::define_property(target, property_key, attributes);
let result = match result {
Ok(val) => val,
Err(_err) => "TypeError".into()
};
result
}
"#,
)
.file(
"test.ts",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const object = {};
assert.equal(wasm.define_property(object, "key", { value: 42}), true)
assert.equal(wasm.define_property("", "key", { value: 42 }), "TypeError");
}
"#,
)
.test()
}