Procházet zdrojové kódy

[Swf9] use native for...in on ObjectMap (fixed issue #1673)

Simon Krajewski před 12 roky
rodič
revize
2d0d98ec50
1 změnil soubory, kde provedl 75 přidání a 4 odebrání
  1. 75 4
      std/flash/_std/haxe/ds/ObjectMap.hx

+ 75 - 4
std/flash/_std/haxe/ds/ObjectMap.hx

@@ -21,16 +21,29 @@ class ObjectMap<K,V> extends flash.utils.Dictionary {
 		return has;
 	}
 
-	public function keys() : Iterator<K> {
+	#if as3
+	
+ 	public function keys() : Iterator<K> {
 		return untyped __keys__(this).iterator();
-	}
+ 	}
 
-	public function iterator() : Iterator<V> {
+ 	public function iterator() : Iterator<V> {
 		var ret = [];
 		for (i in keys())
 			ret.push(get(i));
 		return ret.iterator();
+ 	}	
+	#else
+	
+	public function keys() : Iterator<K> {
+		return NativePropertyIterator.iterator(this);
+	}
+
+	public function iterator() : Iterator<V> {
+		return NativeValueIterator.iterator(this);
 	}
+	
+	#end
 
 	public function toString() : String {
 		var s = "";
@@ -42,4 +55,62 @@ class ObjectMap<K,V> extends flash.utils.Dictionary {
 		}
 		return s + "}";
 	}
-}
+}
+
+private class NativePropertyIterator {
+	var collection:Dynamic;
+	var index:Int = 0;
+
+	public static inline function iterator(collection:Dynamic):NativePropertyIterator {
+		var result = new NativePropertyIterator();
+		result.collection = collection;
+		return result;
+	}
+
+	function new() {}
+
+	public inline function hasNext():Bool {
+		var c = collection;
+		var i = index;
+		var result = untyped __has_next__(c, i);
+		collection = c;
+		index = i;
+		return result;
+	}
+
+	public inline function next():Dynamic {
+		var i = index;
+		var result = untyped __forin__(collection, i);
+		index = i;
+		return result;
+	}
+}
+
+private class NativeValueIterator {
+	var collection:Dynamic;
+	var index:Int = 0;
+
+	public static inline function iterator(collection:Dynamic):NativeValueIterator {
+		var result = new NativeValueIterator();
+		result.collection = collection;
+		return result;
+	}
+
+	function new() {}
+
+	public inline function hasNext():Bool {
+		var c = collection;
+		var i = index;
+		var result = untyped __has_next__(c, i);
+		collection = c;
+		index = i;
+		return result;
+	}
+
+	public inline function next():Dynamic {
+		var i = index;
+		var result = untyped __foreach__(collection, i);
+		index = i;
+		return result;
+	}
+}