diff --git a/js/js.translator/testData/maps.js b/js/js.translator/testData/maps.js index bc222f7f629..565bc253777 100644 --- a/js/js.translator/testData/maps.js +++ b/js/js.translator/testData/maps.js @@ -508,7 +508,7 @@ Kotlin.PrimitiveHashMap = Kotlin.createClassNow(Kotlin.Map, function () { this.$size = 0; - this.map = {}; + this.map = Object.create(null); }, { size: function () { return this.$size; @@ -517,12 +517,14 @@ return this.$size === 0; }, containsKey_za3rmp$: function (key) { + // TODO: should process "__proto__" separately? return this.map[key] !== undefined; }, containsValue_za3rmp$: function (value) { var map = this.map; for (var key in map) { - if (map.hasOwnProperty(key) && map[key] === value) { + //noinspection JSUnfilteredForInLoop + if (map[key] === value) { return true; } } @@ -555,19 +557,17 @@ putAll_za3j1t$: function (fromMap) { var map = fromMap.map; for (var key in map) { - if (map.hasOwnProperty(key)) { - this.map[key] = map[key]; - this.$size++; - } + //noinspection JSUnfilteredForInLoop + this.map[key] = map[key]; + this.$size++; } }, entrySet: function () { var result = new Kotlin.ComplexHashSet(); var map = this.map; for (var key in map) { - if (map.hasOwnProperty(key)) { - result.add_za3rmp$(new Entry(key, map[key])); - } + //noinspection JSUnfilteredForInLoop + result.add_za3rmp$(new Entry(key, map[key])); } return result; @@ -576,9 +576,8 @@ var result = new Kotlin.PrimitiveHashSet(); var map = this.map; for (var key in map) { - if (map.hasOwnProperty(key)) { - result.add_za3rmp$(key); - } + //noinspection JSUnfilteredForInLoop + result.add_za3rmp$(key); } return result; diff --git a/libraries/stdlib/test/js/MapJsTest.kt b/libraries/stdlib/test/js/MapJsTest.kt index 4e288d97673..0ee938152ba 100644 --- a/libraries/stdlib/test/js/MapJsTest.kt +++ b/libraries/stdlib/test/js/MapJsTest.kt @@ -26,6 +26,8 @@ abstract class MapJsTest { val KEYS = array("zero", "one", "two", "three").toList() val VALUES = array(0, 1, 2, 3).toList() + val SPECIAL_NAMES = array("__proto__", "constructor", "toString", "toLocaleString", "valueOf", "hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable") + test fun getOrElse() { val data = emptyMap() val a = data.getOrElse("foo"){2} @@ -246,6 +248,42 @@ abstract class MapJsTest { assertEquals(VALUES.toNormalizedList(), actualValues.toNormalizedList()) } + test fun specialNamesNotContainsInEmptyMap() { + val map = emptyMap() + + for (key in SPECIAL_NAMES) { + assertFalse(map.containsKey(key), "unexpected key: $key") + } + } + + test fun specialNamesNotContainsInNonEmptyMap() { + val map = createTestMap() + + for (key in SPECIAL_NAMES) { + assertFalse(map.containsKey(key), "unexpected key: $key") + } + } + + test fun putAndGetSpecialNamesToMap() { + val map = createTestMutableMap() + var value = 0 + + for (key in SPECIAL_NAMES) { + assertFalse(map.containsKey(key), "unexpected key: $key") + + map.put(key, value) + assertTrue(map.containsKey(key), "key not found: $key") + + val actualValue = map.get(key) + assertEquals(value, actualValue, "wrong value fo key: $key") + + map.remove(key) + assertFalse(map.containsKey(key), "unexpected key after remove: $key") + + value += 3 + } + } + /* test fun createLinkedMap() { val map = linkedMapOf("c" to 3, "b" to 2, "a" to 1) diff --git a/libraries/stdlib/test/js/SetJsTest.kt b/libraries/stdlib/test/js/SetJsTest.kt index eb9e846d253..0d2f8719823 100644 --- a/libraries/stdlib/test/js/SetJsTest.kt +++ b/libraries/stdlib/test/js/SetJsTest.kt @@ -22,6 +22,8 @@ abstract class SetJsTest { val data: Set = createTestMutableSet() val empty: Set = createEmptyMutableSet() + val SPECIAL_NAMES = array("__proto__", "constructor", "toString", "toLocaleString", "valueOf", "hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable") + Test fun size() { assertEquals(2, data.size()) assertEquals(0, empty.size()) @@ -122,6 +124,32 @@ abstract class SetJsTest { assertTrue(data.isEmpty()) } + Test fun specialNamesNotContainsInEmptySet() { + for (element in SPECIAL_NAMES) { + assertFalse(empty.contains(element), "unexpected element: $element") + } + } + + Test fun specialNamesNotContainsInNonEmptySet() { + for (element in SPECIAL_NAMES) { + assertFalse(data.contains(element), "unexpected element: $element") + } + } + + Test fun putAndGetSpecialNamesToSet() { + val s = createTestMutableSet() + + for (element in SPECIAL_NAMES) { + assertFalse(s.contains(element), "unexpected element: $element") + + s.add(element) + assertTrue(s.contains(element), "element not found: $element") + + s.remove(element) + assertFalse(s.contains(element), "unexpected element after remove: $element") + } + } + //Helpers abstract fun createEmptyMutableSet(): MutableSet