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, comparisons are done strictly by object equality. Both ES6 and the java.util.WeakHashMap class have the same basic structure -- entries are removed au
| 24 | * same semantics. |
| 25 | */ |
| 26 | public class NativeWeakMap extends ScriptableObject { |
| 27 | @Serial private static final long serialVersionUID = 8670434366883930453L; |
| 28 | |
| 29 | private static final String CLASS_NAME = "WeakMap"; |
| 30 | |
| 31 | private static final ClassDescriptor DESCRIPTOR; |
| 32 | |
| 33 | static { |
| 34 | DESCRIPTOR = |
| 35 | new ClassDescriptor.Builder( |
| 36 | CLASS_NAME, |
| 37 | 0, |
| 38 | ClassDescriptor.typeError(), |
| 39 | NativeWeakMap::jsConstructor) |
| 40 | .withMethod(PROTO, "set", 2, NativeWeakMap::js_set) |
| 41 | .withMethod(PROTO, "delete", 1, NativeWeakMap::js_delete) |
| 42 | .withMethod(PROTO, "get", 1, NativeWeakMap::js_get) |
| 43 | .withMethod(PROTO, "has", 1, NativeWeakMap::js_has) |
| 44 | .withProp( |
| 45 | PROTO, |
| 46 | SymbolKey.TO_STRING_TAG, |
| 47 | value(CLASS_NAME, DONTENUM | READONLY)) |
| 48 | .withProp(CTOR, SymbolKey.SPECIES, ScriptRuntimeES6::symbolSpecies) |
| 49 | .build(); |
| 50 | } |
| 51 | |
| 52 | private boolean instanceOfWeakMap = false; |
| 53 | |
| 54 | private transient WeakHashMap<Object, Object> map = new WeakHashMap<>(); |
| 55 | |
| 56 | private static final Object NULL_VALUE = new Object(); |
| 57 | |
| 58 | static Object init(Context cx, VarScope scope, boolean sealed) { |
| 59 | return DESCRIPTOR.buildConstructor(cx, scope, new NativeObject(), sealed); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public String getClassName() { |
| 64 | return CLASS_NAME; |
| 65 | } |
| 66 | |
| 67 | private static Scriptable jsConstructor( |
| 68 | Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) { |
| 69 | NativeWeakMap nm = new NativeWeakMap(); |
| 70 | nm.instanceOfWeakMap = true; |
| 71 | if (args.length > 0) { |
| 72 | NativeMap.loadFromIterable(cx, f.getDeclarationScope(), nm, NativeMap.key(args)); |
| 73 | } |
| 74 | nm.setParentScope(f.getDeclarationScope()); |
| 75 | nm.setPrototype((Scriptable) f.getPrototypeProperty()); |
| 76 | return nm; |
| 77 | } |
| 78 | |
| 79 | private static Object js_delete( |
| 80 | Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) { |
| 81 | return realThis(thisObj, "delete").js_delete(NativeMap.key(args)); |
| 82 | } |
| 83 |
nothing calls this directly
no test coverage detected