mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-23 01:31:34 +00:00
feat: add Reflect.defineProperty
This commit is contained in:
@ -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
|
||||
|
@ -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()
|
||||
}
|
Reference in New Issue
Block a user