Browse Source

[js] fix __id__ handling for the first time `ObjectMap.set(obj, value)` (fixes #9026)

Aleksandr Kuzmenko 5 năm trước cách đây
mục cha
commit
9f9fc023d1
2 tập tin đã thay đổi với 20 bổ sung6 xóa
  1. 8 6
      std/js/_std/haxe/ds/ObjectMap.hx
  2. 12 0
      tests/unit/src/unit/issues/Issue9026.hx

+ 8 - 6
std/js/_std/haxe/ds/ObjectMap.hx

@@ -43,18 +43,20 @@ class ObjectMap<K:{}, V> implements haxe.Constraints.IMap<K, V> {
 		return untyped obj.__id__;
 	}
 
-	var h:{};
+	var h:{__keys__:{}};
 
 	public function new():Void {
 		h = {__keys__: {}};
 	}
 
-	public function set(key:K, value:V):Void
-		untyped {
-			var id:Int = getId(key) || assignId(key);
-			h[id] = value;
-			h.__keys__[id] = key;
+	public function set(key:K, value:V):Void {
+		var id = getId(key);
+		if(id == null) {
+			id = assignId(key);
 		}
+		Syntax.code('{0}[{1}] = {2}', h, id, value);
+		Syntax.code('{0}[{1}] = {2}', h.__keys__, id, key);
+	}
 
 	public inline function get(key:K):Null<V> {
 		return untyped h[getId(key)];

+ 12 - 0
tests/unit/src/unit/issues/Issue9026.hx

@@ -0,0 +1,12 @@
+package unit.issues;
+
+class Issue9026 extends unit.Test {
+	function test() {
+		var key = {};
+		var map: Map<{}, Int> = new Map();
+		map[key] = 10;
+		map[key] = 20;
+		eq(20, map.get(key));
+		eq(1, Lambda.count(map));
+	}
+}