mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-23 01:31:34 +00:00
feat: add Reflect.getOwnPropertyDescriptor
This commit is contained in:
@ -1019,6 +1019,14 @@ extern "C" {
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/get
|
||||
#[wasm_bindgen(static_method_of = Reflect, catch)]
|
||||
pub fn get(target: &JsValue, key: &JsValue) -> Result<JsValue, JsValue>;
|
||||
|
||||
/// The static Reflect.getOwnPropertyDescriptor() method is similar to
|
||||
/// Object.getOwnPropertyDescriptor(). It returns a property descriptor
|
||||
/// of the given property if it exists on the object, undefined otherwise.
|
||||
///
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/getOwnPropertyDescriptor
|
||||
#[wasm_bindgen(static_method_of = Reflect, js_name = getOwnPropertyDescriptor, catch)]
|
||||
pub fn get_own_property_descriptor(target: &JsValue, property_key: &JsValue) -> Result<JsValue, JsValue>;
|
||||
}
|
||||
|
||||
// Set
|
||||
|
@ -300,4 +300,47 @@ fn get() {
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_own_property_descriptor() {
|
||||
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 get_own_property_descriptor(target: &JsValue, property_key: &JsValue) -> JsValue {
|
||||
let result = js::Reflect::get_own_property_descriptor(target, property_key);
|
||||
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 = {
|
||||
property: 42
|
||||
};
|
||||
|
||||
assert.equal(wasm.get_own_property_descriptor(object, "property").value, 42);
|
||||
assert.equal(wasm.get_own_property_descriptor(object, "property1"), undefined);
|
||||
assert.equal(wasm.get_own_property_descriptor("", "property1"), "TypeError");
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
Reference in New Issue
Block a user