Ver Fonte

[java] Initial support for CallStack; for now, just output the Java VM call stack; requires -debug for line numbers in Haxe generated classes

Jonas Malaco Filho há 11 anos atrás
pai
commit
3c3590cae1
2 ficheiros alterados com 60 adições e 9 exclusões
  1. 38 0
      std/haxe/CallStack.hx
  2. 22 9
      std/java/internal/Exceptions.hx

+ 38 - 0
std/haxe/CallStack.hx

@@ -80,6 +80,25 @@ class CallStack {
 			a.shift(); // remove Stack.callStack()
 			(untyped Error).prepareStackTrace = oldValue;
 			return a;
+		#elseif java
+			var stack = [];
+			for ( el in java.lang.Thread.currentThread().getStackTrace() ) {
+				var className = el.getClassName();
+				var methodName = el.getMethodName();
+				var fileName = el.getFileName();
+				var lineNumber = el.getLineNumber();
+				var method = Method( className, methodName );
+				if ( fileName != null || lineNumber >= 0 ) {
+					stack.push( FilePos( method, fileName, lineNumber ) );
+				}
+				else {
+					stack.push( method );
+				}
+			}
+			stack.shift();
+			stack.shift();
+			stack.pop();
+			return stack;
 		#else
 			return []; // Unsupported
 		#end
@@ -117,6 +136,25 @@ class CallStack {
 		#elseif cpp
 			var s:Array<String> = untyped __global__.__hxcpp_get_exception_stack();
 			return makeStack(s);
+		#elseif java
+			var stack = [];
+			for ( el in java.internal.Exceptions.currentException().getStackTrace() ) {
+				var className = el.getClassName();
+				var methodName = el.getMethodName();
+				var fileName = el.getFileName();
+				var lineNumber = el.getLineNumber();
+				var method = Method( className, methodName );
+				if ( fileName != null || lineNumber >= 0 ) {
+					stack.push( FilePos( method, fileName, lineNumber ) );
+				}
+				else {
+					stack.push( method );
+				}
+			}
+			// stack.shift();
+			stack.shift();
+			stack.pop();
+			return stack;
 		#else
 			return []; // Unsupported
 		#end

+ 22 - 9
std/java/internal/Exceptions.hx

@@ -24,6 +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 function currentException() {
+		return exception.get();
+	}
+}
+
 @:nativeGen @:keep @:native("haxe.lang.HaxeException") private class HaxeException extends RuntimeException
 {
 	private var obj:Dynamic;
@@ -53,14 +64,16 @@ import java.lang.Exception;
 
 	public static function wrap(obj:Dynamic):RuntimeException
 	{
-		if (Std.is(obj, RuntimeException))
-			return obj;
-
-		if (Std.is(obj, String))
-			return new HaxeException(obj, obj, null);
-		else if (Std.is(obj, Throwable))
-			return new HaxeException(obj, null, obj);
-
-		return new HaxeException(obj, null, null);
+		var ret:RuntimeException = null;
+ 		if (Std.is(obj, RuntimeException))
+			ret = obj;
+		else if (Std.is(obj, String))
+			ret = new HaxeException(obj, obj, null);
+ 		else if (Std.is(obj, Throwable))
+			ret = new HaxeException(obj, null, obj);
+		else
+			ret = new HaxeException(obj, null, null);
+		Exceptions.exception.set( ret );
+		return ret;
 	}
 }