浏览代码

[cs] changes to fixed and "checked" block test case

Caue Waneck 13 年之前
父节点
当前提交
e7f7cde0bd
共有 3 个文件被更改,包括 26 次插入6 次删除
  1. 10 5
      gencs.ml
  2. 2 0
      std/cs/Lib.hx
  3. 14 1
      tests/unit/TestCSharp.hx

+ 10 - 5
gencs.ml

@@ -866,15 +866,20 @@ let configure gen =
           write w ")";
           expr_s w (mk_block eblock)
         | TCall ({ eexpr = TLocal( { v_name = "__fixed__" } ) }, [ e ] ) ->
-          write w "fixed(";
           let first = ref true in
           let rec loop = function
             | ({ eexpr = TVars([v, Some({ eexpr = TCast( { eexpr = TCast(e, _) }, _) }) ]) } as expr) :: tl when is_pointer v.v_type ->
-              (if !first then first := false else write w ", ");
-              expr_s w { expr with eexpr = TVars([v, Some e]) };
-              loop tl
-            | el when not !first ->
+              (if !first then first := false);
+              write w "fixed(";
+              let vf = mk_temp gen "fixed" v.v_type in
+              expr_s w { expr with eexpr = TVars([vf, Some e]) };
               write w ")";
+              begin_block w;
+              expr_s w { expr with eexpr = TVars([v, Some (mk_local vf expr.epos)]) };
+              write w ";";
+              loop tl;
+              end_block w
+            | el when not !first ->
               expr_s w { e with eexpr = TBlock el }
             | _ -> 
               trace (debug_expr e);

+ 2 - 0
std/cs/Lib.hx

@@ -159,6 +159,8 @@ class Lib
 				var obj2 = cs.Lib.pointerOfArray(someArray2);
 				var obj3 = cs.Lib.pointerOfArray(someArray3);
 				//from now on, obj1, obj2 and obj3 are fixed
+				//we cannot change obj1, obj2 or obj3 variables like this:
+				//obj1++;
 			});
 		
 		This method only exists at compile-time, so it can't be called via reflection.

+ 14 - 1
tests/unit/TestCSharp.hx

@@ -31,6 +31,20 @@ class TestCSharp extends Test
 		eq(i, 20);
 	}
 	
+	public function testChecked()
+	{
+		exc(function()
+		{
+			cs.Lib.checked({
+				var x = 1000;
+				while(true)
+				{
+					x *= x;
+				}
+			});
+		});
+	}
+	
 	#if unsafe
 	
 	@:unsafe public function testUnsafe()
@@ -38,7 +52,6 @@ class TestCSharp extends Test
 		var x:cs.NativeArray<Int> = new cs.NativeArray(10);
 		cs.Lib.fixed({
 			var p = cs.Lib.pointerOfArray(x);
-			var p = p;
 			for (i in 0...10)
 			{
 				p[0] = i;