From a27fac10be5b5946caf43391d7f1aca8bd015d67 Mon Sep 17 00:00:00 2001 From: rbri Date: Thu, 18 Jul 2019 20:56:04 +0200 Subject: [PATCH] fix for Map/Set working with ConsString as key also; closes #583 --- src/org/mozilla/javascript/Hashtable.java | 2 + testsrc/jstests/es6/map.js | 42 ++++++++++++ testsrc/jstests/es6/set.js | 66 +++++++++++++++++++ .../javascript/tests/es6/NativeSetTest.java | 16 +++++ 4 files changed, 126 insertions(+) create mode 100644 testsrc/jstests/es6/set.js create mode 100644 testsrc/org/mozilla/javascript/tests/es6/NativeSetTest.java diff --git a/src/org/mozilla/javascript/Hashtable.java b/src/org/mozilla/javascript/Hashtable.java index 586a8ae3d3..abf6603a19 100644 --- a/src/org/mozilla/javascript/Hashtable.java +++ b/src/org/mozilla/javascript/Hashtable.java @@ -55,6 +55,8 @@ public static final class Entry if ((k instanceof Number) && ( ! ( k instanceof Double))) { // Hash comparison won't work if we don't do this this.key = ((Number)k).doubleValue(); + } else if (k instanceof ConsString) { + this.key = k.toString(); } else { this.key = k; } diff --git a/testsrc/jstests/es6/map.js b/testsrc/jstests/es6/map.js index b154eb567e..e7d3689124 100644 --- a/testsrc/jstests/es6/map.js +++ b/testsrc/jstests/es6/map.js @@ -34,4 +34,46 @@ function logElement(value, key, m) { assertEquals("c) map[key1] = '' map[key2] = 'undefined' map[key3] = 'undefined' map[key4] = 'null' ", res); })(); +(function TestGetConcatenatedStrings() { + var myMap = new Map([['key1', 'value1'], ['key2', 17]]); + for(let i = 1; i <= 1; i++) { + let key = 'key' + i; + + assertEquals("value1", myMap.get(key)); + assertEquals("value1", myMap.get('key1')); + } +})(); + +(function TestSetConcatenatedStrings() { + var myMap = new Map([['key1', 'value1'], ['key2', 17]]); + for(let i = 1; i <= 1; i++) { + let key = 'key' + i; + + myMap.set(key, 'value2') + assertEquals("value2", myMap.get(key)); + assertEquals("value2", myMap.get('key1')); + } +})(); + +(function TestHasConcatenatedStrings() { + var myMap = new Map([['key1', 'value1'], ['key2', 17]]); + for(let i = 1; i <= 1; i++) { + let key = 'key' + i; + + assertTrue(myMap.has(key)); + assertTrue(myMap.has('key1')); + } +})(); + +(function TestDeleteConcatenatedStrings() { + var myMap = new Map([['key1', 'value1'], ['key2', 17]]); + for(let i = 1; i <= 1; i++) { + let key = 'key' + i; + + assertEquals(2, myMap.size); + myMap.delete(key) + assertEquals(1, myMap.size); + } +})(); + "success"; \ No newline at end of file diff --git a/testsrc/jstests/es6/set.js b/testsrc/jstests/es6/set.js new file mode 100644 index 0000000000..978035520f --- /dev/null +++ b/testsrc/jstests/es6/set.js @@ -0,0 +1,66 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +load("testsrc/assert.js"); + +res = ""; + +function logElement(value, key) { + res += "set[" + key + "] "; +} + +(function TestForEach() { + res = "a) "; + var mySet = new Set(['key1', 17]); + mySet.forEach(logElement); + + assertEquals("a) set[key1] set[17] ", res); +})(); + +(function TestForEachNoKey() { + res = "b) "; + var mySet = new Set(['', undefined, null, 19]); + mySet.forEach(logElement); + + assertEquals("b) set[] set[undefined] set[null] set[19] ", res); +})(); + +(function TestAddConcatenatedStrings() { + var mySet = new Set(['key1']); + var mySet2 = new Set(['key2']); + for(let i = 1; i <= 1; i++) { + let key = 'key' + i; + + assertEquals(1, mySet.size); + mySet.add(key) + assertEquals(1, mySet.size); + + assertEquals(1, mySet2.size); + mySet2.add(key) + assertEquals(2, mySet2.size); + } +})(); + +(function TestHasConcatenatedStrings() { + var mySet = new Set(['key1', 'key2']); + for(let i = 1; i <= 1; i++) { + let key = 'key' + i; + + assertTrue(mySet.has(key)); + assertTrue(mySet.has('key1')); + } +})(); + +(function TestDeleteConcatenatedStrings() { + var mySet = new Set(['key1', 'key2']); + for(let i = 1; i <= 1; i++) { + let key = 'key' + i; + + assertEquals(2, mySet.size); + mySet.delete(key) + assertEquals(1, mySet.size); + } +})(); + +"success"; \ No newline at end of file diff --git a/testsrc/org/mozilla/javascript/tests/es6/NativeSetTest.java b/testsrc/org/mozilla/javascript/tests/es6/NativeSetTest.java new file mode 100644 index 0000000000..e8c0011c53 --- /dev/null +++ b/testsrc/org/mozilla/javascript/tests/es6/NativeSetTest.java @@ -0,0 +1,16 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.javascript.tests.es6; + +import org.mozilla.javascript.Context; +import org.mozilla.javascript.drivers.LanguageVersion; +import org.mozilla.javascript.drivers.RhinoTest; +import org.mozilla.javascript.drivers.ScriptTestsBase; + +@RhinoTest("testsrc/jstests/es6/set.js") +@LanguageVersion(Context.VERSION_ES6) +public class NativeSetTest extends ScriptTestsBase +{ +}