Browse Source

- blocks unset used vars

Franco Ponticelli 15 years ago
parent
commit
580ad1c3fe
3 changed files with 35 additions and 16 deletions
  1. 23 15
      genphp.ml
  2. 5 0
      tests/unit/TestBasetypes.hx
  3. 7 1
      tests/unit/TestMisc.hx

+ 23 - 15
genphp.ml

@@ -828,7 +828,19 @@ and gen_inline_function ctx f hasthis p =
 	ctx.locals <- old_l;
 	ctx.inv_locals <- old_li;
 	ctx.local_types <- old_t
-
+	
+and unset_locals ctx old_l =
+	let lst = ref [] in
+	PMap.iter (fun n _ ->
+		if not (PMap.exists n old_l) then
+			lst := ["$" ^  n] @ !lst;
+	) ctx.inv_locals;
+	if (List.length !lst) > 0 then begin
+		spr ctx "unset(";
+		concat ctx "," (fun (s) -> spr ctx s; ) !lst;
+		spr ctx ")"
+	end
+	
 and gen_while_expr ctx e =
 	match e.eexpr with
 	| TBlock (el) ->	
@@ -838,17 +850,8 @@ and gen_while_expr ctx e =
 		let bend = open_block ctx in
 		List.iter (fun e -> newline ctx; gen_expr ctx e) el;
 		newline ctx;
-		let lst = ref [] in
-		PMap.iter (fun n _ ->
-		    if not (PMap.exists n old_l) then
-				lst := ["$" ^  n] @ !lst;
-		) ctx.inv_locals;
 		
-		if (List.length !lst) > 0 then begin
-			spr ctx "unset(";
-			concat ctx "," (fun (s) -> spr ctx s; ) !lst;
-			spr ctx ")"
-		end;
+		unset_locals ctx old_l;
 		
 		bend();
 		newline ctx;
@@ -1120,6 +1123,7 @@ and gen_expr ctx e =
 	| TBlock [] ->
 		spr ctx ""
 	| TBlock el ->
+		let old_l = ctx.inv_locals in
 		let b = save_locals ctx in
 		print ctx "{";
 		let bend = open_block ctx in
@@ -1158,7 +1162,8 @@ and gen_expr ctx e =
 					(match e.eexpr with
 					| TIf _
 					| TSwitch _
-					| TThrow _ -> 
+					| TThrow _ 
+					| TBlock _ ->
 						gen_expr ctx e
 					| TReturn Some e1 ->
 						(match e1.eexpr with
@@ -1166,7 +1171,7 @@ and gen_expr ctx e =
 						| TSwitch _
 						| TThrow _ -> ()
 						| _ ->
-							spr ctx "return "
+							spr ctx "return z"
 						);
 						gen_expr ctx e1;
 					| _ -> 
@@ -1180,7 +1185,9 @@ and gen_expr ctx e =
 			in
 			loop el
 		end else
-			List.iter (fun e -> newline ctx; gen_expr ctx e) el);
+			List.iter (fun e -> newline ctx; gen_expr ctx e) el;
+			newline ctx;
+			unset_locals ctx old_l);
 		bend();
 		newline ctx;
 		cb();
@@ -1532,7 +1539,7 @@ and gen_value ctx e =
 	| TNew _
 	| TCast _
 	| TFunction _
-	| TReturn _ ->
+(*	| TReturn _*) ->
 		gen_expr ctx e
 	| TBlock [] ->
 		()
@@ -1543,6 +1550,7 @@ and gen_value ctx e =
 	| TContinue
 	| TVars _
 	| TFor _
+	| TReturn _
 	| TWhile _
 	| TThrow _
 	| TSwitch _

+ 5 - 0
tests/unit/TestBasetypes.hx

@@ -31,8 +31,13 @@ class TestBasetypes extends Test {
 		unspec(function() String.fromCharCode(0));
 		unspec(function() String.fromCharCode(-1));
 		unspec(function() String.fromCharCode(256));
+#if php
+		eq( Std.string(null) + "x", "nullx" );
+		eq( "x" + Std.string(null), "xnull" );
+#else
 		eq( null + "x", "nullx" );
 		eq( "x" + null, "xnull" );
+#end
 
 		var abc = "abc".split("");
 		eq( abc.length, 3 );

+ 7 - 1
tests/unit/TestMisc.hx

@@ -20,13 +20,19 @@ class MyDynamicClass {
 		return v + x + y;
 	}
 
+#if php
+	static var Z = 10;
 
+	public dynamic static function staticDynamic(x,y) {
+		return Z + x + y;
+	}
+#else
 	static var V = 10;
 
 	public dynamic static function staticDynamic(x,y) {
 		return V + x + y;
 	}
-
+#end
 }
 
 class MyDynamicSubClass extends MyDynamicClass {