mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-25 11:02:11 +03:00
Merge pull request #322 from belfz/expose-bindings/object-seal
implements Object.seal() binding
This commit is contained in:
commit
c836639229
@ -431,6 +431,14 @@ extern {
|
||||
#[wasm_bindgen(method, js_name = propertyIsEnumerable)]
|
||||
pub fn property_is_enumerable(this: &Object, property: &JsValue) -> bool;
|
||||
|
||||
/// The Object.seal() method seals an object, preventing new properties
|
||||
/// from being added to it and marking all existing properties as non-configurable.
|
||||
/// Values of present properties can still be changed as long as they are writable.
|
||||
///
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal
|
||||
#[wasm_bindgen(static_method_of = Object)]
|
||||
pub fn seal(value: &JsValue) -> JsValue;
|
||||
|
||||
/// The toLocaleString() method returns a string representing the object.
|
||||
/// This method is meant to be overridden by derived objects for
|
||||
/// locale-specific purposes.
|
||||
|
@ -185,6 +185,57 @@ fn property_is_enumerable() {
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn seal() {
|
||||
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 seal(value: &JsValue) -> JsValue {
|
||||
js::Object::seal(&value)
|
||||
}
|
||||
"#)
|
||||
.file("test.ts", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const object: any = { foo: 'bar' };
|
||||
const sealedObject = wasm.seal(object);
|
||||
assert.strictEqual(object, sealedObject);
|
||||
assert.throws(() => {
|
||||
'use strict';
|
||||
sealedObject.bar = 'foo';
|
||||
}, TypeError);
|
||||
assert.throws(() => {
|
||||
'use strict';
|
||||
delete sealedObject.foo;
|
||||
}, TypeError);
|
||||
|
||||
const primitive = 42;
|
||||
assert.doesNotThrow(() => {
|
||||
'use strict';
|
||||
// according to ES2015, this should not throw anymore
|
||||
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal#Notes
|
||||
wasm.seal(primitive);
|
||||
});
|
||||
|
||||
const array = [1, 2, 3];
|
||||
const sealedArray = wasm.seal(array);
|
||||
assert.throws(() => {
|
||||
'use strict';
|
||||
sealedArray.push(42);
|
||||
}, TypeError);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_locale_string() {
|
||||
project()
|
||||
|
Loading…
Reference in New Issue
Block a user