mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-23 01:31:34 +00:00
feat: add Reflect.deleteProperty
This commit is contained in:
@ -1004,7 +1004,14 @@ extern "C" {
|
||||
///
|
||||
/// 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>;
|
||||
pub fn define_property(target: &Object, property_key: &JsValue, attributes: &Object) -> Result<JsValue, JsValue>;
|
||||
|
||||
/// The static Reflect.deleteProperty() method allows to delete properties.
|
||||
/// It is like the delete operator as a function.
|
||||
///
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/deleteProperty
|
||||
#[wasm_bindgen(static_method_of = Reflect, js_name = deleteProperty, catch)]
|
||||
pub fn delete_property(target: &JsValue, key: &JsValue) -> Result<JsValue, JsValue>;
|
||||
}
|
||||
|
||||
// Set
|
||||
|
@ -203,4 +203,52 @@ fn define_property() {
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_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 delete_property(target: &JsValue, property_key: &JsValue) -> JsValue {
|
||||
let result = js::Reflect::delete_property(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
|
||||
};
|
||||
|
||||
wasm.delete_property(object, 'property');
|
||||
|
||||
assert.equal(object.property, undefined);
|
||||
|
||||
const array = [1, 2, 3, 4, 5];
|
||||
wasm.delete_property(array, 3);
|
||||
|
||||
assert.equal(array[3], undefined);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
Reference in New Issue
Block a user