diff --git a/src/js.rs b/src/js.rs index 66afb54b..3171daca 100644 --- a/src/js.rs +++ b/src/js.rs @@ -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. diff --git a/tests/all/js_globals/Object.rs b/tests/all/js_globals/Object.rs index 2ca6a093..48526636 100755 --- a/tests/all/js_globals/Object.rs +++ b/tests/all/js_globals/Object.rs @@ -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()