From 37fc159061a1a8cc7b4eb4182decc0bc8a65be73 Mon Sep 17 00:00:00 2001 From: Marcin Baraniecki Date: Mon, 2 Jul 2018 17:31:40 +0200 Subject: [PATCH] implements Object.preventExtensions() binding (#364) --- src/js.rs | 8 ++++++++ tests/all/js_globals/Object.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/js.rs b/src/js.rs index 91a5af7a4..e4df40b68 100644 --- a/src/js.rs +++ b/src/js.rs @@ -790,6 +790,14 @@ extern "C" { #[wasm_bindgen(constructor)] pub fn new() -> Object; + /// The Object.preventExtensions() method prevents + /// new properties from ever being added to an object + /// (i.e. prevents future extensions to the object). + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/preventExtensions + #[wasm_bindgen(static_method_of = Object, js_name = preventExtensions)] + pub fn prevent_extensions(object: &Object); + /// The propertyIsEnumerable() method returns a Boolean indicating /// whether the specified property is enumerable. /// diff --git a/tests/all/js_globals/Object.rs b/tests/all/js_globals/Object.rs index b3ceb3127..1357fbf27 100644 --- a/tests/all/js_globals/Object.rs +++ b/tests/all/js_globals/Object.rs @@ -180,6 +180,39 @@ fn keys() { .test() } +#[test] +fn prevent_extensions() { + 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 prevent_extensions(obj: &js::Object) { + js::Object::prevent_extensions(obj); + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const object = {}; + wasm.prevent_extensions(object); + + assert(!Object.isExtensible(object)); + assert.throws(() => { + 'use strict'; + Object.defineProperty(object, 'foo', { value: 42 }); + }, TypeError); + } + "#) + .test() +} + #[test] fn property_is_enumerable() { project()