Browse Source

Merge branch 'development' of https://github.com/HaxeFoundation/haxe into development

hughsando 11 years ago
parent
commit
aa174b2044

+ 18 - 3
gencommon.ml

@@ -6176,19 +6176,34 @@ struct
 
 		let in_value = ref false in
 
+		let rec clean_cast e = match e.eexpr with
+		 | TCast(e,_) -> clean_cast e
+		 | TParenthesis(e) | TMeta(_,e) -> clean_cast e
+		 | _ -> e
+		in
+
 		let rec run ?(just_type = false) e =
 			let handle = if not just_type then handle else fun e t1 t2 -> { e with etype = gen.greal_type t2 } in
 			let was_in_value = !in_value in
 			in_value := true;
 			match e.eexpr with
-				| TConst ( TInt _ | TFloat _ | TBool _ ) ->
+				| TConst ( TInt _ | TFloat _ | TBool _ as const ) ->
 					(* take off any Null<> that it may have *)
-					{ e with etype = follow (run_follow gen e.etype) }
+					let t = follow (run_follow gen e.etype) in
+					(* do not allow constants typed as Single - need to cast them *)
+					let real_t = match const with
+						| TInt _ -> gen.gcon.basic.tint
+						| TFloat _ -> gen.gcon.basic.tfloat
+						| TBool _ -> gen.gcon.basic.tbool
+						| _ -> assert false
+					in
+					handle e t real_t
 				| TCast( { eexpr = TCall( { eexpr = TLocal { v_name = "__delegate__" } } as local, [del] ) } as e2, _) ->
 					{ e with eexpr = TCast({ e2 with eexpr = TCall(local, [Type.map_expr run del]) }, None) }
 
 				| TBinop ( (Ast.OpAssign | Ast.OpAssignOp _ as op), e1, e2 ) ->
-					{ e with eexpr = TBinop(op, run ~just_type:true e1, run e2) }
+					let e1 = run ~just_type:true e1 in
+					{ e with eexpr = TBinop(op, clean_cast e1, run e2) }
 				| TField(ef, f) ->
 					handle_type_parameter gen None e (run ef) ~clean_ef:ef ~overloads_cast_to_base:overloads_cast_to_base f [] calls_parameters_explicitly
 				| TArrayDecl el ->

+ 23 - 0
gencs.ml

@@ -1050,6 +1050,29 @@ let configure gen =
 			let was_in_value = !in_value in
 			in_value := true;
 			(match e.eexpr with
+				| TCall({ eexpr = TField(ef,f) }, (_ :: _ as args) ) when (field_name f) = "get_Item" ->
+					expr_s w ef;
+					write w "[";
+					let first = ref true in
+					List.iter (fun f ->
+						if !first then first := false else write w ", ";
+						expr_s w f
+					) args;
+					write w "]"
+				| TCall({ eexpr = TField(ef,f) }, (_ :: _ :: _ as args) ) when (field_name f) = "set_Item" ->
+					expr_s w ef;
+					write w "[";
+					let args, value = match List.rev args with
+						| v :: args -> List.rev args, v
+						| _ -> assert false
+					in
+					let first = ref true in
+					List.iter (fun f ->
+						if !first then first := false else write w ", ";
+						expr_s w f
+					) args;
+					write w "] = ";
+					expr_s w value
 				| TCall( ({ eexpr = TField(ef,f) } as e), [ev] ) when String.starts_with (field_name f) "add_" ->
 					let name = field_name f in
 					let propname = String.sub name 4 (String.length name - 4) in

+ 8 - 1
genjava.ml

@@ -2064,7 +2064,14 @@ let configure gen =
 			)
 			(base_exception_t)
 			(hx_exception_t)
-			(fun v e -> e)
+			(fun v e ->
+
+				let exc_cl = get_cl (get_type gen (["haxe";"lang"],"Exceptions")) in
+				let exc_field = mk_static_field_access_infer exc_cl "setException" e.epos [] in
+				let esetstack = { eexpr = TCall(exc_field,[mk_local v e.epos]); etype = gen.gcon.basic.tvoid; epos = e.epos } in
+
+				Type.concat esetstack e;
+			)
 	);
 
 	let get_typeof e =

+ 30 - 10
genxml.ml

@@ -23,6 +23,7 @@
 open Ast
 open Type
 open Common
+open ExtString
 
 type xml =
 	| Node of string * (string * string) list * xml list
@@ -292,9 +293,27 @@ let conv_path p =
 	| x :: l when x.[0] = '_' -> List.rev (("priv" ^ x) :: l), snd p
 	| _ -> p
 
+let get_real_path meta path =
+	try
+		let real_path = match Meta.get Meta.RealPath meta with
+			| (_,[(EConst(String s),_)],_) ->
+				s
+			| _ -> raise Not_found
+		in
+		match List.rev (String.nsplit real_path ".") with
+			| name :: pack ->
+				(List.rev pack), name
+			| _ -> raise Not_found
+	with | Not_found ->
+		path
+
+
 let generate_type com t =
 	let base_path = "hxclasses" in
-	let pack , name = conv_path (t_path t) in
+	let pack, name =
+		let info = t_infos t in
+		get_real_path info.mt_meta info.mt_path
+	in
 	create_dir "." (base_path :: pack);
 	match pack, name with
 	| ["flash";"net"], "NetStreamPlayTransitions"
@@ -318,8 +337,8 @@ let generate_type com t =
 		| _ ->
 			t
 	in
-	let rec path p tl =
-		let p = conv_path p in
+	let rec path meta p tl =
+		let p = conv_path (get_real_path meta p) in
 		(if fst p = pack then snd p else s_type_path p) ^ (match tl with [] -> "" | _ -> "<" ^ String.concat "," (List.map stype tl) ^ ">")
 	and stype t =
 		match t with
@@ -328,15 +347,15 @@ let generate_type com t =
 			| None -> "Unknown"
 			| Some t -> stype t)
 		| TInst ({ cl_kind = KTypeParameter _ } as c,tl) ->
-			path ([],snd c.cl_path) tl
+			path [] ([],snd c.cl_path) tl
 		| TInst (c,tl) ->
-			path c.cl_path tl
+			path c.cl_meta c.cl_path tl
 		| TEnum (e,tl) ->
-			path e.e_path tl
+			path e.e_meta e.e_path tl
 		| TType (t,tl) ->
-			path t.t_path tl
+			path t.t_meta t.t_path tl
 		| TAbstract (a,tl) ->
-			path a.a_path tl
+			path a.a_meta a.a_path tl
 		| TAnon a ->
 			let fields = PMap.fold (fun f acc -> (f.cf_name ^ " : " ^ stype f.cf_type) :: acc) a.a_fields [] in
 			"{" ^ String.concat ", " fields ^ "}"
@@ -392,7 +411,7 @@ let generate_type com t =
 		| AccNever, "flash" :: _ -> "null"
 		| _ -> s_access is_read a
 	in
-	let print_field stat f =
+	let rec print_field stat f =
 		p "\t";
 		print_meta f.cf_meta;
 		if stat then p "static ";
@@ -430,7 +449,8 @@ let generate_type com t =
 			let tparams = (match f.cf_params with [] -> "" | l -> "<" ^ String.concat "," (List.map fst l) ^ ">") in
 			p "function %s%s(%s) : %s" f.cf_name tparams (String.concat ", " (List.map sparam params)) (stype ret);
 		);
-		p ";\n"
+		p ";\n";
+		if Meta.has Meta.Overload f.cf_meta then List.iter (fun f -> print_field stat f) f.cf_overloads
 	in
 	(match t with
 	| TClassDecl c ->

+ 1 - 1
std/java/_std/haxe/ds/WeakMap.hx

@@ -390,7 +390,7 @@ import java.lang.ref.ReferenceQueue;
 				{
 					if (!isEither(hashes[j]))
 					{
-						var entry = entries[i];
+						var entry = entries[j];
 						var last = entry.get();
 						if (last != null)
 						{

+ 8 - 5
std/java/internal/Exceptions.hx

@@ -24,13 +24,17 @@ import java.lang.Throwable;
 import java.lang.RuntimeException;
 import java.lang.Exception;
 
-@:allow(haxe.CallStack)
-@:allow(java.lang.RuntimeException)
 @:native("haxe.lang.Exceptions")
 class Exceptions {
-	private static var exception = new java.lang.ThreadLocal<java.lang.RuntimeException>();
+	private static var exception = new java.lang.ThreadLocal<java.lang.Throwable>();
 
-	private static function currentException() {
+	@:keep private static function setException(exc:Throwable)
+	{
+		exception.set(exc);
+	}
+
+	public static function currentException()
+	{
 		return exception.get();
 	}
 }
@@ -73,7 +77,6 @@ class Exceptions {
 			ret = new HaxeException(obj, null, obj);
 		else
 			ret = new HaxeException(obj, null, null);
-		Exceptions.exception.set( ret );
 		return ret;
 	}
 }

+ 39 - 5
std/js/Boot.hx

@@ -70,8 +70,15 @@ class Boot {
 	static inline function getClass(o:Dynamic) : Dynamic {
 		if (Std.is(o, Array))
 			return Array;
-		else
-			return untyped __define_feature__("js.Boot.getClass", o.__class__);
+		else {
+			var cl = untyped __define_feature__("js.Boot.getClass", o.__class__);
+			if (cl != null)
+				return cl;
+			var name = __nativeClassName(o);
+			if (name != null)
+				return __resolveNativeClass(name);
+			return null;
+		}
 	}
 
 	@:ifFeature("may_print_enum")
@@ -180,14 +187,17 @@ class Boot {
 			return true;
 		default:
 			if( o != null ) {
-				// Check if o is an instance of a Haxe class
+				// Check if o is an instance of a Haxe class or a native JS object
 				if( (untyped __js__("typeof"))(cl) == "function" ) {
-					if( untyped __js__("o instanceof cl") ) {
+					if( untyped __js__("o instanceof cl") )
 						return true;
-					}
 					if( __interfLoop(getClass(o),cl) )
 						return true;
 				}
+				else if ( (untyped __js__("typeof"))(cl) == "object" && __isNativeObj(cl) ) {
+					if( untyped __js__("o instanceof cl") )
+						return true;
+				}
 			} else {
 				return false;
 			}
@@ -202,5 +212,29 @@ class Boot {
 		if (__instanceof(o, t)) return o;
 		else throw "Cannot cast " +Std.string(o) + " to " +Std.string(t);
 	}
+	
+	static var __toStr = untyped __js__("{}.toString");
+	// get native JS [[Class]]
+	static function __nativeClassName(o:Dynamic):String {
+		var name = untyped __toStr.call(o).slice(8, -1);
+		// exclude general Object and Function
+		// also exclude Math and JSON, because instanceof cannot be called on them
+		if (name == "Object" || name == "Function" || name == "Math" || name == "JSON")
+			return null;
+		return name;
+	}
+	
+	// check for usable native JS object
+	static function __isNativeObj(o:Dynamic):Bool {
+		return __nativeClassName(o) != null;
+	}
+	
+	// resolve native JS class (with window or global):
+	static function __resolveNativeClass(name:String) untyped {
+		if (__js__("typeof window") != "undefined")
+			return window[name];
+		else
+			return global[name];
+	}
 
 }

+ 2 - 0
std/js/_std/Type.hx

@@ -52,6 +52,8 @@ enum ValueType {
 
 	public static function getClassName( c : Class<Dynamic> ) : String {
 		var a : Array<String> = untyped c.__name__;
+		if (a == null)
+			return null;
 		return a.join(".");
 	}
 

+ 16 - 0
tests/unit/issues/Issue2857.hx

@@ -0,0 +1,16 @@
+package unit.issues;
+
+class Issue2857 extends unit.Test {
+#if js
+	function testElement() {
+		if (js.Browser.supported) {
+			var vid = js.Browser.document.createVideoElement();
+			t(Std.is(vid, js.html.VideoElement));
+			t(Std.is(vid, js.html.Element));
+			f(Std.is(vid, haxe.Http));
+			f(Std.is(vid, js.html.ArrayBuffer));
+			eq(Type.getClass(vid), js.html.VideoElement);
+		}
+	}
+#end
+}

+ 26 - 0
tests/unit/issues/Issue3118.hx

@@ -0,0 +1,26 @@
+package unit.issues;
+
+class Issue3118 extends Test
+{
+	public function test()
+	{
+		var data = new Final();
+		data.a2 = [];
+		data.a2.push(1);
+		eq(data.a2[0],1);
+		eq(data.a2.length,1);
+	}
+}
+
+class Base<A>
+{
+  public function new() {}
+
+  public var a2:A;
+
+}
+
+@:final
+class Final extends Base<Array<Int>>
+{
+}

+ 2 - 2
tests/unit/unitstd/haxe/ds/WeakMap.unit.hx

@@ -1,4 +1,4 @@
-#if flash
+#if (flash || java)
 var k1 = new IntWrap(1);
 var k2 = new IntWrap(2);
 var k3 = new IntWrap(3);
@@ -84,4 +84,4 @@ a.length == 2;
 a[0] in ["9", "7"];
 a[1] in ["9", "7"];
 o.remove(k2) == false;
-#end
+#end