浏览代码

[js] make Type.createInstance somewhat typed

the js.Syntax.code should not really be needed and js.Syntax.construct should instead automatically insert parens when needed, but this "when needed" is something i couldn't figure out yet
Dan Korostelev 5 年之前
父节点
当前提交
5b859240f8
共有 1 个文件被更改,包括 4 次插入4 次删除
  1. 4 4
      std/js/_std/Type.hx

+ 4 - 4
std/js/_std/Type.hx

@@ -133,10 +133,10 @@ enum ValueType {
 			return js.Syntax.code("new empty()");
 		}
 	#else
-	public static function createInstance<T>(cl:Class<T>, args:Array<Dynamic>):T
-		untyped {
-			return js.Syntax.code("new ({0})", Function.prototype.bind.apply(cl, [null].concat(args)));
-		}
+	public static function createInstance<T>(cl:Class<T>, args:Array<Dynamic>):T {
+		var ctor = ((cast js.lib.Function).prototype.bind : js.lib.Function).apply(cl, [null].concat(args));
+		return js.Syntax.code("new ({0})", ctor); // cannot use `js.Syntax.construct` because we need parens if `ctor` is fused in
+	}
 
 	public static inline function createEmptyInstance<T>(cl:Class<T>):T {
 		return js.lib.Object.create((cast cl).prototype);