فهرست منبع

[flash] add flash.Vector.typeReference (closes #8493)

Dan Korostelev 6 سال پیش
والد
کامیت
e9fece1d51
4فایلهای تغییر یافته به همراه59 افزوده شده و 1 حذف شده
  1. 3 0
      src/generators/genas3.ml
  2. 3 0
      src/generators/genswf9.ml
  3. 31 1
      std/flash/Vector.hx
  4. 22 0
      tests/unit/src/unit/issues/Issue8493.hx

+ 3 - 0
src/generators/genas3.ml

@@ -482,6 +482,9 @@ and gen_call ctx e el r =
 		spr ctx ")";
 	| TIdent "__unprotect__", [e] ->
 		gen_value ctx e
+	| TIdent "__vector__", [] ->
+		let t = match r with TAbstract ({a_path = [],"Class"}, [vt]) -> vt | _ -> assert false in
+		spr ctx (type_str ctx t e.epos);
 	| TIdent "__vector__", [e] ->
 		spr ctx (type_str ctx r e.epos);
 		spr ctx "(";

+ 3 - 0
src/generators/genswf9.ml

@@ -1518,6 +1518,9 @@ and gen_call ctx retval e el r =
 			| 2l -> A3OSign16
 			| _ -> assert false
 		))
+	| TIdent "__vector__", [] ->
+		let t = match r with TAbstract ({a_path = [],"Class"}, [vt]) -> vt | _ -> assert false in
+		gen_type ctx (type_id ctx t)
 	| TIdent "__vector__", [ep] ->
 		gen_type ctx (type_id ctx r);
 		write ctx HGetGlobalScope;

+ 31 - 1
std/flash/Vector.hx

@@ -43,7 +43,7 @@ package flash;
 	function toString() : String;
 	function indexOf( x : T, ?from : Int ) : Int;
 	function lastIndexOf( x : T, ?from : Int ) : Int;
-	
+
 	#if flash19
 	function insertAt(index : Int, element : T) : Void;
 	#else
@@ -61,4 +61,34 @@ package flash;
 		return untyped __vector__(v);
 	}
 
+	/**
+		Get a run-time value referencing the `Vector` class with concrete type parameters.
+
+		Normally in Haxe, for most of the types, type parameters are eliminated at run-time,
+		so there is no way to check if a value is of a type with specific type parameters.
+
+		However, on the Flash target, the `flash.Vector<T>` values carry type parameter
+		information at run-time all the type-checks (such as `Std.is` and `Std.downcast`) on them
+		must be done using a `Class<T>` value that also carries the type parameters. However,
+		Haxe syntax does not allow creating such values and this function exists to mitigate
+		this limitation.
+
+		It should be used as such:
+		```haxe
+		var specificVectorType:Class<Vector<Int>> = Vector.typeReference();
+		trace(Std.is(vec, specificVectorType));
+		```
+		or using the type-check syntax:
+		```haxe
+		trace(Std.is(vec, (Vector.typeReference() : Class<Vector<Int>>)));
+		```
+
+		It's also helpful when working with native Flash libraries, that receive Class instances:
+		```haxe
+		new Signal((Vector.typeReference() : Class<Vector<Int>>));
+		```
+	**/
+	public inline static function typeReference<T>() : Class<Vector<T>> {
+		return untyped __vector__();
+	}
 }

+ 22 - 0
tests/unit/src/unit/issues/Issue8493.hx

@@ -0,0 +1,22 @@
+package unit.issues;
+
+#if flash
+import flash.Vector;
+#end
+
+class Issue8493 extends unit.Test {
+	#if flash
+	static var v = new Vector<Vector<Array<Int>>>();
+	static var typeRef = (Vector.typeReference() : Class<Vector<Vector<Array<Int>>>>);
+
+	function test() {
+		t(Std.is(v, (Vector.typeReference() : Class<Vector<Vector<Array<Int>>>>)));
+		t(Std.downcast(v, (Vector.typeReference() : Class<Vector<Vector<Array<Int>>>>)) == v);
+		t(flash.Lib.as(v, (Vector.typeReference() : Class<Vector<Vector<Array<Int>>>>)) == v);
+
+		t(Std.is(v, typeRef));
+		t(Std.downcast(v, typeRef) == v);
+		t(flash.Lib.as(v, typeRef) == v);
+	}
+	#end
+}