ソースを参照

[cs] haxe.CallStack implementation

Dan Korostelev 11 年 前
コミット
98ff4bd792
4 ファイル変更41 行追加5 行削除
  1. 3 3
      gencommon.ml
  2. 8 1
      gencs.ml
  3. 7 0
      std/cs/internal/Exceptions.hx
  4. 23 1
      std/haxe/CallStack.hx

+ 3 - 3
gencommon.ml

@@ -2599,10 +2599,10 @@ struct
               match follow v.v_type with
                 | TDynamic _ ->
                   assert (is_none catchall);
-                  (nowrap_catches, must_wrap_catches, Some(v,catch_map v (run catch)))
+                  (nowrap_catches, must_wrap_catches, Some(v,run catch))
                 (* see if we should unwrap it *)
                 | _ when should_wrap (follow v.v_type) ->
-                  (nowrap_catches, (v,catch_map v (run catch)) :: must_wrap_catches, catchall)
+                  (nowrap_catches, (v,run catch) :: must_wrap_catches, catchall)
                 | _ ->
                   ( (v,catch_map v (run catch)) :: nowrap_catches, must_wrap_catches, catchall )
             ) ([], [], None) catches
@@ -2648,7 +2648,7 @@ struct
                       | None ->
                         mk_block (rethrow_expr temp_local)
                 in
-                [ ( temp_var, { e with eexpr = TBlock([ catchall_decl; if_is_wrapper_expr; loop must_wrap_catches ]) } ) ]
+                [ ( temp_var, catch_map temp_var { e with eexpr = TBlock([ catchall_decl; if_is_wrapper_expr; loop must_wrap_catches ]) } ) ]
               | _ ->
                 []
             in

+ 8 - 1
gencs.ml

@@ -2580,7 +2580,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 "exception" e.epos [] in
+        let esetstack = mk (TBinop(Ast.OpAssign, exc_field, mk_local v e.epos)) v.v_type e.epos in
+
+        Codegen.concat esetstack e;
+      )
   );
 
   let get_typeof e =

+ 7 - 0
std/cs/internal/Exceptions.hx

@@ -22,6 +22,13 @@
 package cs.internal;
 import cs.system.Exception;
 
+@:nativeGen @:keep @:native("haxe.lang.Exceptions") class Exceptions {
+
+	@:allow(haxe.CallStack)
+	@:meta(System.ThreadStaticAttribute)
+	static var exception:cs.system.Exception;
+}
+
 //should NOT be usable inside haxe code
 @:nativeGen @:keep @:native("haxe.lang.HaxeException") private class HaxeException extends Exception
 {

+ 23 - 1
std/haxe/CallStack.hx

@@ -99,6 +99,8 @@ class CallStack {
 			stack.shift();
 			stack.pop();
 			return stack;
+		#elseif cs
+			return makeStack(new cs.system.diagnostics.StackTrace(1, true));
 		#else
 			return []; // Unsupported
 		#end
@@ -155,6 +157,8 @@ class CallStack {
 			stack.shift();
 			stack.pop();
 			return stack;
+		#elseif cs
+			return makeStack(new cs.system.diagnostics.StackTrace(cs.internal.Exceptions.exception, true));
 		#else
 			return []; // Unsupported
 		#end
@@ -199,7 +203,7 @@ class CallStack {
 	}
 
 	#if cpp @:noStack #end /* Do not mess up the exception stack */
-	private static function makeStack(s) {
+	private static function makeStack(s #if cs : cs.system.diagnostics.StackTrace #end) {
 		#if neko
 			var a = new Array();
 			var l = untyped __dollar__asize(s);
@@ -278,6 +282,24 @@ class CallStack {
 			} else {
 				return cast s;
 			}
+		#elseif cs
+			var stack = [];
+			for (i in 0...s.FrameCount)
+			{
+				var frame = s.GetFrame(i);
+				var m = frame.GetMethod();
+
+				var method = StackItem.Method(m.ReflectedType.ToString(), m.Name);
+
+				var fileName = frame.GetFileName();
+				var lineNumber = frame.GetFileLineNumber();
+
+				if (fileName != null || lineNumber >= 0)
+					stack.push(FilePos(method, fileName, lineNumber));
+				else
+					stack.push(method);
+			}
+			return stack;
 		#else
 			return null;
 		#end