Add WeakMap and WeakSet on top of the Map and Set work.
diff --git a/src/org/mozilla/javascript/Hashtable.java b/src/org/mozilla/javascript/Hashtable.java index 53f8fdc..586a8ae 100644 --- a/src/org/mozilla/javascript/Hashtable.java +++ b/src/org/mozilla/javascript/Hashtable.java
@@ -1,6 +1,12 @@ package org.mozilla.javascript; import java.io.Serializable; +/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * 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/. */ + import java.util.HashMap; import java.util.Iterator; import java.util.NoSuchElementException;
diff --git a/src/org/mozilla/javascript/NativeMap.java b/src/org/mozilla/javascript/NativeMap.java index 53e1e7d..4cd94ba 100644 --- a/src/org/mozilla/javascript/NativeMap.java +++ b/src/org/mozilla/javascript/NativeMap.java
@@ -1,3 +1,9 @@ +/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * 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; import java.util.Iterator; @@ -8,6 +14,8 @@ private static final Object MAP_TAG = "Map"; static final String ITERATOR_TAG = "Map Iterator"; + private static final Object NULL_VALUE = new Object(); + private final Hashtable entries = new Hashtable(); private boolean instanceOfMap = false; @@ -47,7 +55,7 @@ NativeMap nm = new NativeMap(); nm.instanceOfMap = true; if (args.length > 0) { - nm.js_load(cx, scope, args[0]); + loadFromIterable(cx, scope, nm, args[0]); } return nm; } else { @@ -81,9 +89,18 @@ throw new IllegalArgumentException("Map.prototype has no method: " + f.getFunctionName()); } - private Object js_set(Object arg1, Object arg2) + private Object js_set(Object k, Object v) { - entries.put(arg1, arg2); + // Map.get() does not distinguish between "not found" and a null value. So, + // replace true null here with a marker so that we can re-convert in "get". + final Object value = (v == null ? NULL_VALUE : v); + // Special handling of "negative zero" from the spec. + Object key = k; + if ((key instanceof Number) && + ((Number)key).doubleValue() == ScriptRuntime.negativeZero) { + key = 0.0; + } + entries.put(key, value); return this; } @@ -96,7 +113,13 @@ private Object js_get(Object arg) { final Object val = entries.get(arg); - return val == null ? Undefined.instance : val; + if (val == null) { + return Undefined.instance; + } + if (val == NULL_VALUE) { + return null; + } + return val; } private Object js_has(Object arg) @@ -143,9 +166,9 @@ /** * If an "iterable" object was passed to the constructor, there are many many things - * to do... + * to do... Make this static because NativeWeakMap has the exact same requirement. */ - private void js_load(Context cx, Scriptable scope, Object arg1) + static void loadFromIterable(Context cx, Scriptable scope, ScriptableObject map, Object arg1) { if ((arg1 == null) || Undefined.instance.equals(arg1)) { return; @@ -161,7 +184,7 @@ // Find the "add" function of our own prototype, since it might have // been replaced. Since we're not fully constructed yet, create a dummy instance // so that we can get our own prototype. - ScriptableObject dummy = ensureScriptableObject(cx.newObject(scope, getClassName())); + ScriptableObject dummy = ensureScriptableObject(cx.newObject(scope, map.getClassName())); final Callable set = ScriptRuntime.getPropFunctionAndThis(dummy.getPrototype(), "set", cx, scope); ScriptRuntime.lastStoredScriptable(cx); @@ -175,7 +198,7 @@ } Object finalKey = sVal.get(0, sVal); Object finalVal = sVal.get(1, sVal); - set.call(cx, scope, this, new Object[] { finalKey, finalVal }); + set.call(cx, scope, map, new Object[] { finalKey, finalVal }); } } }
diff --git a/src/org/mozilla/javascript/NativeSet.java b/src/org/mozilla/javascript/NativeSet.java index 292fcbe..60c9be9c 100644 --- a/src/org/mozilla/javascript/NativeSet.java +++ b/src/org/mozilla/javascript/NativeSet.java
@@ -1,3 +1,9 @@ +/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * 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; import java.util.Iterator; @@ -49,7 +55,7 @@ NativeSet ns = new NativeSet(); ns.instanceOfSet = true; if (args.length > 0) { - ns.js_load(cx, scope, args[0]); + loadFromIterable(cx, scope, ns, args[0]); } return ns; } else { @@ -77,9 +83,15 @@ throw new IllegalArgumentException("Set.prototype has no method: " + f.getFunctionName()); } - private Object js_add(Object arg) + private Object js_add(Object k) { - entries.put(arg, arg); + // Special handling of "negative zero" from the spec. + Object key = k; + if ((key instanceof Number) && + ((Number)key).doubleValue() == ScriptRuntime.negativeZero) { + key = 0.0; + } + entries.put(key, key); return this; } @@ -133,9 +145,9 @@ /** * If an "iterable" object was passed to the constructor, there are many many things - * to do... + * to do. This is common code with NativeWeakSet. */ - private void js_load(Context cx, Scriptable scope, Object arg1) + static void loadFromIterable(Context cx, Scriptable scope, ScriptableObject set, Object arg1) { if ((arg1 == null) || Undefined.instance.equals(arg1)) { return; @@ -144,23 +156,24 @@ // Call the "[Symbol.iterator]" property as a function. Object ito = ScriptRuntime.callIterator(arg1, cx, scope); if (Undefined.instance.equals(ito)) { - // Per spec, ignore if the iterator is undefined + // Per spec, ignore if the iterator returns undefined return; } // Find the "add" function of our own prototype, since it might have // been replaced. Since we're not fully constructed yet, create a dummy instance // so that we can get our own prototype. - ScriptableObject dummy = ensureScriptableObject(cx.newObject(scope, getClassName())); + ScriptableObject dummy = ensureScriptableObject(cx.newObject(scope, set.getClassName())); final Callable add = ScriptRuntime.getPropFunctionAndThis(dummy.getPrototype(), "add", cx, scope); + // Clean up the value left around by the previous function ScriptRuntime.lastStoredScriptable(cx); // Finally, run through all the iterated values and add them! try (IteratorLikeIterable it = new IteratorLikeIterable(cx, scope, ito)) { for (Object val : it) { final Object finalVal = val == Scriptable.NOT_FOUND ? Undefined.instance : val; - add.call(cx, scope, this, new Object[]{finalVal}); + add.call(cx, scope, set, new Object[]{finalVal}); } } }
diff --git a/src/org/mozilla/javascript/NativeWeakMap.java b/src/org/mozilla/javascript/NativeWeakMap.java new file mode 100644 index 0000000..92c6bf3 --- /dev/null +++ b/src/org/mozilla/javascript/NativeWeakMap.java
@@ -0,0 +1,196 @@ +/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * 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; + +import java.util.WeakHashMap; + +/** + * This is an implementation of the ES6 WeakMap class. As per the spec, keys must be + * ordinary objects. Since there is no defined "equality" for objects, comparisions + * are done strictly by object equality. Both ES6 and the java.util.WeakHashMap class + * have the same basic structure -- entries are removed automatically when the sole + * remaining reference to the key is a weak reference. Therefore, we can use + * WeakHashMap as the basis of this implementation and preserve the same semantics. + */ +public class NativeWeakMap + extends IdScriptableObject +{ + private static final Object MAP_TAG = "WeakMap"; + + private boolean instanceOfWeakMap = false; + + private final WeakHashMap<Scriptable, Object> map = new WeakHashMap<>(); + + private static final Object NULL_VALUE = new Object(); + + static void init(Scriptable scope, boolean sealed) { + NativeWeakMap m = new NativeWeakMap(); + m.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed); + } + + @Override + public String getClassName() { + return "WeakMap"; + } + + @Override + public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, + Scriptable thisObj, Object[] args) { + + if (!f.hasTag(MAP_TAG)) { + return super.execIdCall(f, cx, scope, thisObj, args); + } + int id = f.methodId(); + switch (id) { + case Id_constructor: + if (thisObj == null) { + NativeWeakMap nm = new NativeWeakMap(); + nm.instanceOfWeakMap = true; + if (args.length > 0) { + NativeMap.loadFromIterable(cx, scope, nm, args[0]); + } + return nm; + } else { + throw ScriptRuntime.typeError1("msg.no.new", "WeakMap"); + } + case Id_delete: + return realThis(thisObj, f).js_delete(args.length > 0 ? args[0] : Undefined.instance); + case Id_get: + return realThis(thisObj, f).js_get(args.length > 0 ? args[0] : Undefined.instance); + case Id_has: + return realThis(thisObj, f).js_has(args.length > 0 ? args[0] : Undefined.instance); + case Id_set: + return realThis(thisObj, f).js_set( + args.length > 0 ? args[0] : Undefined.instance, + args.length > 1 ? args[1] : Undefined.instance); + } + throw new IllegalArgumentException("WeakMap.prototype has no method: " + f.getFunctionName()); + } + + private Object js_delete(Object key) { + if (!ScriptRuntime.isObject(key)) { + return false; + } + final Object oldVal = map.remove((Scriptable)key); + return (oldVal != null); + } + + private Object js_get(Object key) { + if (!ScriptRuntime.isObject(key)) { + return Undefined.instance; + } + Object result = map.get((Scriptable)key); + if (result == null) { + return Undefined.instance; + } else if (result == NULL_VALUE) { + return null; + } + return result; + } + + private Object js_has(Object key) { + if (!ScriptRuntime.isObject(key)) { + return false; + } + return map.containsKey((Scriptable)key); + } + + private Object js_set(Object key, Object v) { + // As the spec says, only a true "Object" can be the key to a WeakMap. + // Use the default object equality here. ScriptableObject does not override + // equals or hashCode, which means that in effect we are only keying on object identity. + // This is all correct according to the ECMAscript spec. + if (!ScriptRuntime.isObject(key)) { + throw ScriptRuntime.typeError1("msg.arg.not.object", ScriptRuntime.typeof(key)); + } + // Map.get() does not distinguish between "not found" and a null value. So, + // replace true null here with a marker so that we can re-convert in "get". + final Object value = (v == null ? NULL_VALUE : v); + map.put((Scriptable)key, value); + return this; + } + + private NativeWeakMap realThis(Scriptable thisObj, IdFunctionObject f) { + if (thisObj == null) { + throw incompatibleCallError(f); + } + try { + final NativeWeakMap nm = (NativeWeakMap)thisObj; + if (!nm.instanceOfWeakMap) { + // Check for "Map internal data tag" + throw incompatibleCallError(f); + } + return nm; + } catch (ClassCastException cce) { + throw incompatibleCallError(f); + } + } + + @Override + protected void initPrototypeId(int id) { + if (id == SymbolId_toStringTag) { + initPrototypeValue(SymbolId_toStringTag, SymbolKey.TO_STRING_TAG, + getClassName(), DONTENUM | READONLY); + return; + } + + String s, fnName = null; + int arity; + switch (id) { + case Id_constructor: arity = 0; s = "constructor"; break; + case Id_delete: arity = 1; s = "delete"; break; + case Id_get: arity = 1; s = "get"; break; + case Id_has: arity = 1; s = "has"; break; + case Id_set: arity = 2; s = "set"; break; + default: throw new IllegalArgumentException(String.valueOf(id)); + } + initPrototypeMethod(MAP_TAG, id, s, fnName, arity); + } + + @Override + protected int findPrototypeId(Symbol k) + { + if (SymbolKey.TO_STRING_TAG.equals(k)) { + return SymbolId_toStringTag; + } + return 0; + } + +// #string_id_map# + + @Override + protected int findPrototypeId(String s) { + int id; +// #generated# Last update: 2018-08-24 15:27:45 PDT + L0: { id = 0; String X = null; int c; + int s_length = s.length(); + if (s_length==3) { + c=s.charAt(0); + if (c=='g') { if (s.charAt(2)=='t' && s.charAt(1)=='e') {id=Id_get; break L0;} } + else if (c=='h') { if (s.charAt(2)=='s' && s.charAt(1)=='a') {id=Id_has; break L0;} } + else if (c=='s') { if (s.charAt(2)=='t' && s.charAt(1)=='e') {id=Id_set; break L0;} } + } + else if (s_length==6) { X="delete";id=Id_delete; } + else if (s_length==11) { X="constructor";id=Id_constructor; } + if (X!=null && X!=s && !X.equals(s)) id = 0; + break L0; + } +// #/generated# + return id; + } + + private static final int + Id_constructor = 1, + Id_delete = 2, + Id_get = 3, + Id_has = 4, + Id_set = 5, + SymbolId_toStringTag = 6, + MAX_PROTOTYPE_ID = SymbolId_toStringTag; + +// #/string_id_map# +}
diff --git a/src/org/mozilla/javascript/NativeWeakSet.java b/src/org/mozilla/javascript/NativeWeakSet.java new file mode 100644 index 0000000..9273b93 --- /dev/null +++ b/src/org/mozilla/javascript/NativeWeakSet.java
@@ -0,0 +1,172 @@ +/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * 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; + +import java.util.WeakHashMap; + +/** + * This is an implementation of the ES6 WeakSet class. It is very similar to + * NativeWeakMap, with the exception being that it doesn't store any values. + * Java will GC the key only when there is no longer any reference to it other + * than the weak reference. That means that it is important that the "value" + * that we put in the WeakHashMap here is not one that contains the key. + */ +public class NativeWeakSet + extends IdScriptableObject +{ + private static final Object MAP_TAG = "WeakSet"; + + private boolean instanceOfWeakSet = false; + + private final WeakHashMap<Scriptable, Boolean> map = new WeakHashMap<>(); + + static void init(Scriptable scope, boolean sealed) { + NativeWeakSet m = new NativeWeakSet(); + m.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed); + } + + @Override + public String getClassName() { + return "WeakSet"; + } + + @Override + public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, + Scriptable thisObj, Object[] args) { + + if (!f.hasTag(MAP_TAG)) { + return super.execIdCall(f, cx, scope, thisObj, args); + } + int id = f.methodId(); + switch (id) { + case Id_constructor: + if (thisObj == null) { + NativeWeakSet ns = new NativeWeakSet(); + ns.instanceOfWeakSet = true; + if (args.length > 0) { + NativeSet.loadFromIterable(cx, scope, ns, args[0]); + } + return ns; + } else { + throw ScriptRuntime.typeError1("msg.no.new", "WeakSet"); + } + case Id_add: + return realThis(thisObj, f).js_add(args.length > 0 ? args[0] : Undefined.instance); + case Id_delete: + return realThis(thisObj, f).js_delete(args.length > 0 ? args[0] : Undefined.instance); + case Id_has: + return realThis(thisObj, f).js_has(args.length > 0 ? args[0] : Undefined.instance); + } + throw new IllegalArgumentException("WeakMap.prototype has no method: " + f.getFunctionName()); + } + + private Object js_add(Object key) { + // As the spec says, only a true "Object" can be the key to a WeakSet. + // Use the default object equality here. ScriptableObject does not override + // equals or hashCode, which means that in effect we are only keying on object identity. + // This is all correct according to the ECMAscript spec. + if (!ScriptRuntime.isObject(key)) { + throw ScriptRuntime.typeError1("msg.arg.not.object", ScriptRuntime.typeof(key)); + } + // Add a value to the map, but don't make it the key -- otherwise the WeakHashMap + // will never GC anything. + map.put((Scriptable)key, Boolean.TRUE); + return this; + } + + private Object js_delete(Object key) { + if (!ScriptRuntime.isObject(key)) { + return false; + } + final Object oldVal = map.remove((Scriptable)key); + return (oldVal != null); + } + + private Object js_has(Object key) { + if (!ScriptRuntime.isObject(key)) { + return false; + } + return map.containsKey((Scriptable)key); + } + + private NativeWeakSet realThis(Scriptable thisObj, IdFunctionObject f) { + if (thisObj == null) { + throw incompatibleCallError(f); + } + try { + final NativeWeakSet ns = (NativeWeakSet)thisObj; + if (!ns.instanceOfWeakSet) { + // Check for "Set internal data tag" + throw incompatibleCallError(f); + } + return ns; + } catch (ClassCastException cce) { + throw incompatibleCallError(f); + } + } + + @Override + protected void initPrototypeId(int id) { + if (id == SymbolId_toStringTag) { + initPrototypeValue(SymbolId_toStringTag, SymbolKey.TO_STRING_TAG, + getClassName(), DONTENUM | READONLY); + return; + } + + String s, fnName = null; + int arity; + switch (id) { + case Id_constructor: arity = 0; s = "constructor"; break; + case Id_add: arity = 1; s = "add"; break; + case Id_delete: arity = 1; s = "delete"; break; + case Id_has: arity = 1; s = "has"; break; + default: throw new IllegalArgumentException(String.valueOf(id)); + } + initPrototypeMethod(MAP_TAG, id, s, fnName, arity); + } + + @Override + protected int findPrototypeId(Symbol k) + { + if (SymbolKey.TO_STRING_TAG.equals(k)) { + return SymbolId_toStringTag; + } + return 0; + } + +// #string_id_map# + + @Override + protected int findPrototypeId(String s) { + int id; +// #generated# Last update: 2018-08-27 10:45:54 PDT + L0: { id = 0; String X = null; int c; + int s_length = s.length(); + if (s_length==3) { + c=s.charAt(0); + if (c=='a') { if (s.charAt(2)=='d' && s.charAt(1)=='d') {id=Id_add; break L0;} } + else if (c=='h') { if (s.charAt(2)=='s' && s.charAt(1)=='a') {id=Id_has; break L0;} } + } + else if (s_length==6) { X="delete";id=Id_delete; } + else if (s_length==11) { X="constructor";id=Id_constructor; } + if (X!=null && X!=s && !X.equals(s)) id = 0; + break L0; + } +// #/generated# + return id; + } + + private static final int + Id_constructor = 1, + Id_add = 2, + Id_delete = 3, + Id_has = 4, + SymbolId_toStringTag = 5, + MAX_PROTOTYPE_ID = SymbolId_toStringTag; + +// #/string_id_map# +}
diff --git a/src/org/mozilla/javascript/ScriptRuntime.java b/src/org/mozilla/javascript/ScriptRuntime.java index 72f1f48..c905045 100644 --- a/src/org/mozilla/javascript/ScriptRuntime.java +++ b/src/org/mozilla/javascript/ScriptRuntime.java
@@ -263,6 +263,8 @@ NativeCollectionIterator.init(scope, NativeMap.ITERATOR_TAG, sealed); NativeMap.init(cx, scope, sealed); NativeSet.init(cx, scope, sealed); + NativeWeakMap.init(scope, sealed); + NativeWeakSet.init(scope, sealed); } if (scope instanceof TopLevel) { @@ -2872,6 +2874,23 @@ return typeof(getObjectProp(val, id, cx)); } + public static boolean isObject(Object value) + { + if (value == null) { + return false; + } + if (Undefined.instance.equals(value)) { + return false; + } + if (value instanceof ScriptableObject) { + return "object".equals(((ScriptableObject)value).getTypeOf()); + } + if (value instanceof Scriptable) { + return (!(value instanceof Callable)); + } + return false; + } + // neg: // implement the '-' operator inline in the caller // as "-toNumber(val)"
diff --git a/testsrc/jstests/es6/collections.js b/testsrc/jstests/es6/collections.js index 393d238..38eed66 100644 --- a/testsrc/jstests/es6/collections.js +++ b/testsrc/jstests/es6/collections.js
@@ -43,7 +43,7 @@ assertDoesNotThrow(function () { m.delete(new Object) }); } TestValidSetCalls(new Set); -//TestValidSetCalls(new WeakSet); +TestValidSetCalls(new WeakSet); // Test valid getter and setter calls on Maps and WeakMaps @@ -69,7 +69,7 @@ assertDoesNotThrow(function () { m.delete(Symbol()) }); } TestValidMapCalls(new Map); -//TestValidMapCalls(new WeakMap); +TestValidMapCalls(new WeakMap); // Test invalid getter and setter calls for WeakMap only @@ -80,7 +80,7 @@ assertThrows(function () { m.set('a-key', 0) }, TypeError); assertThrows(function () { m.set(Symbol(), 0) }, TypeError); } -//TestInvalidCalls(new WeakMap); +TestInvalidCalls(new WeakMap); // Test expected behavior for Sets and WeakSets @@ -115,7 +115,7 @@ } } TestSetBehavior(new Set); -//TestSetBehavior(new WeakSet); +TestSetBehavior(new WeakSet); // Test expected mapping behavior for Maps and WeakMaps @@ -123,12 +123,12 @@ assertFalse(map.has(key)); assertSame(undefined, map.get(key)); assertFalse(map.delete(key)); - /*if (typeof key === 'object' && !(map instanceof WeakMap)) { + if (typeof key === 'object' && !(map instanceof WeakMap)) { assertSame(map, map.set(key, value)); assertSame(value, map.get(key)); assertTrue(map.has(key)); assertTrue(map.delete(key)); - }*/ + } assertFalse(map.has(key)); assertSame(undefined, map.get(key)); assertFalse(map.delete(key)); @@ -158,7 +158,7 @@ } } TestMapBehavior(new Map); -//TestMapBehavior(new WeakMap); +TestMapBehavior(new WeakMap); // Test expected querying behavior of Maps and WeakMaps @@ -170,7 +170,7 @@ } } TestQuery(new Map); -//TestQuery(new WeakMap); +TestQuery(new WeakMap); // Test expected deletion behavior of Maps and WeakMaps @@ -182,7 +182,7 @@ assertSame(m.get(key), undefined); } TestDelete(new Map); -//TestDelete(new WeakMap); +TestDelete(new WeakMap); // Test GC of Maps and WeakMaps with entry @@ -193,7 +193,7 @@ assertSame('not-collected', m.get(key)); } TestGC1(new Map); -//TestGC1(new WeakMap); +TestGC1(new WeakMap); // Test GC of Maps and WeakMaps with chained entries @@ -210,7 +210,7 @@ assertEquals(11, count); } TestGC2(new Map); -//TestGC2(new WeakMap); +TestGC2(new WeakMap); // Test property attribute [[Enumerable]] @@ -226,8 +226,8 @@ } TestEnumerable(Set); TestEnumerable(Map); -//TestEnumerable(WeakMap); -//TestEnumerable(WeakSet); +TestEnumerable(WeakMap); +TestEnumerable(WeakSet); // Test arbitrary properties on Maps and WeakMaps @@ -243,14 +243,14 @@ TestMapping(m, new Object, 'foobar'); } TestArbitrary(new Map); -//TestArbitrary(new WeakMap); +TestArbitrary(new WeakMap); // Test direct constructor call assertThrows(function() { Set(); }, TypeError); assertThrows(function() { Map(); }, TypeError); -//assertThrows(function() { WeakMap(); }, TypeError); -//assertThrows(function() { WeakSet(); }, TypeError); +assertThrows(function() { WeakMap(); }, TypeError); +assertThrows(function() { WeakSet(); }, TypeError); // Test whether NaN values as keys are treated correctly. @@ -291,7 +291,7 @@ assertTrue(Map.prototype.clear instanceof Function) -/* + // Test some common JavaScript idioms for WeakMaps var m = new WeakMap; assertTrue(m instanceof WeakMap); @@ -307,14 +307,13 @@ assertTrue(WeakSet.prototype.add instanceof Function) assertTrue(WeakSet.prototype.has instanceof Function) assertTrue(WeakSet.prototype.delete instanceof Function) -*/ // Test name of constructor. assertEquals("Set", Set.name); assertEquals("Map", Map.name); -//assertEquals("WeakMap", WeakMap.name); -//assertEquals("WeakSet", WeakSet.name); +assertEquals("WeakMap", WeakMap.name); +assertEquals("WeakSet", WeakSet.name); // Test prototype property of Set, Map, WeakMap and WeakSet. @@ -329,8 +328,8 @@ } TestPrototype(Set); TestPrototype(Map); -//TestPrototype(WeakMap); -//TestPrototype(WeakSet); +TestPrototype(WeakMap); +TestPrototype(WeakSet); // Test constructor property of the Set, Map, WeakMap and WeakSet prototype. @@ -342,8 +341,8 @@ } TestConstructor(Set); TestConstructor(Map); -//TestConstructor(WeakMap); -//TestConstructor(WeakSet); +TestConstructor(WeakMap); +TestConstructor(WeakSet); // Test the Set, Map, WeakMap and WeakSet global properties themselves. @@ -364,13 +363,13 @@ */ // Regression test for WeakMap prototype. -//assertTrue(WeakMap.prototype.constructor === WeakMap) -//assertTrue(Object.getPrototypeOf(WeakMap.prototype) === Object.prototype) +assertTrue(WeakMap.prototype.constructor === WeakMap) +assertTrue(Object.getPrototypeOf(WeakMap.prototype) === Object.prototype) // Regression test for issue 1617: The prototype of the WeakMap constructor // needs to be unique (i.e. different from the one of the Object constructor). -//assertFalse(WeakMap.prototype === Object.prototype); +assertFalse(WeakMap.prototype === Object.prototype); var o = Object.create({}); assertFalse("get" in o); assertFalse("set" in o); @@ -1110,7 +1109,7 @@ assertTrue(s.has(k2)); } TestSetConstructor(Set); -//TestSetConstructor(WeakSet); +TestSetConstructor(WeakSet); function TestSetConstructorAddNotCallable(ctor) { @@ -1122,7 +1121,7 @@ ctor.prototype.add = originalPrototypeAdd; } TestSetConstructorAddNotCallable(Set); -//TestSetConstructorAddNotCallable(WeakSet); +TestSetConstructorAddNotCallable(WeakSet); function TestSetConstructorGetAddOnce(ctor) { var originalPrototypeAdd = ctor.prototype.add; @@ -1144,7 +1143,7 @@ }); } TestSetConstructorGetAddOnce(Set); -//TestSetConstructorGetAddOnce(WeakSet); +TestSetConstructorGetAddOnce(WeakSet); function TestSetConstructorAddReplaced(ctor) { @@ -1161,7 +1160,7 @@ ctor.prototype.add = originalPrototypeAdd; } TestSetConstructorAddReplaced(Set); -//TestSetConstructorAddReplaced(WeakSet); +TestSetConstructorAddReplaced(WeakSet); function TestSetConstructorOrderOfDoneValue(ctor) { @@ -1189,7 +1188,7 @@ assertEquals(0, valueCount); } TestSetConstructorOrderOfDoneValue(Set); -//TestSetConstructorOrderOfDoneValue(WeakSet); +TestSetConstructorOrderOfDoneValue(WeakSet); function TestSetConstructorNextNotAnObject(ctor) { var iterator = { @@ -1205,15 +1204,13 @@ }, TypeError); } TestSetConstructorNextNotAnObject(Set); -//TestSetConstructorNextNotAnObject(WeakSet); +TestSetConstructorNextNotAnObject(WeakSet); -/* (function TestWeakSetConstructorNonObjectKeys() { assertThrows(function() { new WeakSet([1]); }, TypeError); })(); -*/ /* * TODO missing stuff around strict mode @@ -1293,7 +1290,7 @@ assertEquals('c', m.get(k2)); } TestMapConstructor(Map); -//TestMapConstructor(WeakMap); +TestMapConstructor(WeakMap); function TestMapConstructorSetNotCallable(ctor) { @@ -1305,7 +1302,7 @@ ctor.prototype.set = originalPrototypeSet; } TestMapConstructorSetNotCallable(Map); -//TestMapConstructorSetNotCallable(WeakMap); +TestMapConstructorSetNotCallable(WeakMap); function TestMapConstructorGetAddOnce(ctor) { var originalPrototypeSet = ctor.prototype.set; @@ -1327,7 +1324,7 @@ }); } TestMapConstructorGetAddOnce(Map); -//TestMapConstructorGetAddOnce(WeakMap); +TestMapConstructorGetAddOnce(WeakMap); function TestMapConstructorSetReplaced(ctor) { var originalPrototypeSet = ctor.prototype.set; @@ -1343,7 +1340,7 @@ ctor.prototype.set = originalPrototypeSet; } TestMapConstructorSetReplaced(Map); -//TestMapConstructorSetReplaced(WeakMap); +TestMapConstructorSetReplaced(WeakMap); function TestMapConstructorOrderOfDoneValue(ctor) { @@ -1372,7 +1369,7 @@ assertEquals(0, valueCount); } TestMapConstructorOrderOfDoneValue(Map); -//TestMapConstructorOrderOfDoneValue(WeakMap); +TestMapConstructorOrderOfDoneValue(WeakMap); function TestMapConstructorNextNotAnObject(ctor) { @@ -1389,7 +1386,7 @@ }, TypeError); } TestMapConstructorNextNotAnObject(Map); -//TestMapConstructorNextNotAnObject(WeakMap); +TestMapConstructorNextNotAnObject(WeakMap); function TestMapConstructorIteratorNotObjectValues(ctor) { @@ -1398,15 +1395,13 @@ }, TypeError); } TestMapConstructorIteratorNotObjectValues(Map); -//TestMapConstructorIteratorNotObjectValues(WeakMap); +TestMapConstructorIteratorNotObjectValues(WeakMap); -/* (function TestWeakMapConstructorNonObjectKeys() { assertThrows(function() { new WeakMap([[1, 2]]) }, TypeError); })(); -*/ /* * TODO Rhino this kind of overriding of the iterator just isnt' available yet. @@ -1432,7 +1427,7 @@ delete Number.prototype[Symbol.iterator]; } TestMapConstructorIterableValue(Map); -//TestMapConstructorIterableValue(WeakMap); +TestMapConstructorIterableValue(WeakMap); */ function TestCollectionToString(C) { @@ -1441,8 +1436,8 @@ } TestCollectionToString(Map); TestCollectionToString(Set); -//TestCollectionToString(WeakMap); -//TestCollectionToString(WeakSet); +TestCollectionToString(WeakMap); +TestCollectionToString(WeakSet); /* @@ -1478,8 +1473,8 @@ } TestConstructorOrderOfAdderIterator(Map, 'set'); TestConstructorOrderOfAdderIterator(Set, 'add'); -//TestConstructorOrderOfAdderIterator(WeakMap, 'set'); -//TestConstructorOrderOfAdderIterator(WeakSet, 'add'); +TestConstructorOrderOfAdderIterator(WeakMap, 'set'); +TestConstructorOrderOfAdderIterator(WeakSet, 'add'); */ "success";
diff --git a/testsrc/org/mozilla/javascript/tests/Test262SuiteTest.java b/testsrc/org/mozilla/javascript/tests/Test262SuiteTest.java index 0a675c5..8d1a97d 100644 --- a/testsrc/org/mozilla/javascript/tests/Test262SuiteTest.java +++ b/testsrc/org/mozilla/javascript/tests/Test262SuiteTest.java
@@ -51,8 +51,6 @@ "Reflect.set", "Reflect.setPrototypeOf", "SharedArrayBuffer", - "WeakMap", - "WeakSet", "async-functions", "async-iteration", "class",
diff --git a/testsrc/test262.properties b/testsrc/test262.properties index 4d3cee6..996efa8 100644 --- a/testsrc/test262.properties +++ b/testsrc/test262.properties
@@ -577,14 +577,6 @@ # Lots of work to do on strict mode ! prototype/forEach/callback-this-non-strict.js ! prototype/forEach/this-non-strict.js - # We don't have WeakSet yet - ! prototype/add/does-not-have-setdata-internal-slot-weakset.js - ! prototype/clear/does-not-have-setdata-internal-slot-weakset.js - ! prototype/delete/does-not-have-setdata-internal-slot-weakset.js - ! prototype/entries/does-not-have-setdata-internal-slot-weakset.js - ! prototype/forEach/does-not-have-setdata-internal-slot-weakset.js - ! prototype/has/does-not-have-setdata-internal-slot-weakset.js - ! prototype/values/does-not-have-setdata-internal-slot-weakset.js built-ins/String ! numeric-properties.js @@ -685,6 +677,9 @@ built-ins/undefined +built-ins/WeakMap + +built-ins/WeakSet ######### LANGUAGE #########