Browse Source

[js] fix haxe.Exception stacks for IE (closes #9248)

Aleksandr Kuzmenko 5 years ago
parent
commit
b4e4585a77
2 changed files with 21 additions and 3 deletions
  1. 5 0
      std/js/_std/haxe/Exception.hx
  2. 16 3
      std/js/_std/haxe/NativeStackTrace.hx

+ 5 - 0
std/js/_std/haxe/Exception.hx

@@ -55,6 +55,11 @@ class Exception extends NativeException {
 					e = cast this;
 					e = cast this;
 				} else {
 				} else {
 					e = new Error();
 					e = new Error();
+					//Internet Explorer provides call stack only if error was thrown
+					if(js.Syntax.typeof(e.stack) == "undefined") {
+						js.Syntax.code('try { throw {0}; } catch(_) {}', e);
+						__skipStack++;
+					}
 				}
 				}
 				(cast this).stack = e.stack;
 				(cast this).stack = e.stack;
 			}
 			}

+ 16 - 3
std/js/_std/haxe/NativeStackTrace.hx

@@ -36,7 +36,14 @@ class NativeStackTrace {
 	}
 	}
 
 
 	static public function callStack():Any {
 	static public function callStack():Any {
-		return normalize(tryHaxeStack(new Error()), 2);
+		var e:Null<Error> = new Error('');
+		var stack = tryHaxeStack(e);
+		//Internet Explorer provides call stack only if error was thrown
+		if(Syntax.typeof(stack) == "undefined") {
+			try throw e catch(e:Exception) {}
+			stack = e.stack;
+		}
+		return normalize(stack, 2);
 	}
 	}
 
 
 	static public function exceptionStack():Any {
 	static public function exceptionStack():Any {
@@ -58,14 +65,18 @@ class NativeStackTrace {
 				var matched:Null<Array<String>> = Syntax.code('{0}.match(/^    at ([A-Za-z0-9_. ]+) \\(([^)]+):([0-9]+):([0-9]+)\\)$/)', line);
 				var matched:Null<Array<String>> = Syntax.code('{0}.match(/^    at ([A-Za-z0-9_. ]+) \\(([^)]+):([0-9]+):([0-9]+)\\)$/)', line);
 				if (matched != null) {
 				if (matched != null) {
 					var path = matched[1].split(".");
 					var path = matched[1].split(".");
+					if(path[0] == "$hxClasses") {
+						path.shift();
+					}
 					var meth = path.pop();
 					var meth = path.pop();
 					var file = matched[2];
 					var file = matched[2];
 					var line = Std.parseInt(matched[3]);
 					var line = Std.parseInt(matched[3]);
 					var column = Std.parseInt(matched[4]);
 					var column = Std.parseInt(matched[4]);
 					m.push(FilePos(meth == "Anonymous function" ? LocalFunction() : meth == "Global code" ? null : Method(path.join("."), meth), file, line,
 					m.push(FilePos(meth == "Anonymous function" ? LocalFunction() : meth == "Global code" ? null : Method(path.join("."), meth), file, line,
 						column));
 						column));
-				} else
+				} else {
 					m.push(Module(StringTools.trim(line))); // A little weird, but better than nothing
 					m.push(Module(StringTools.trim(line))); // A little weird, but better than nothing
+				}
 			}
 			}
 			return m;
 			return m;
 		} else if(skip > 0 && Syntax.code('Array.isArray({0})', s)) {
 		} else if(skip > 0 && Syntax.code('Array.isArray({0})', s)) {
@@ -97,9 +108,11 @@ class NativeStackTrace {
 			if (fullName != null) {
 			if (fullName != null) {
 				var idx = fullName.lastIndexOf(".");
 				var idx = fullName.lastIndexOf(".");
 				if (idx >= 0) {
 				if (idx >= 0) {
-					var className = fullName.substring(0, idx - 1);
+					var className = fullName.substring(0, idx);
 					var methodName = fullName.substring(idx + 1);
 					var methodName = fullName.substring(idx + 1);
 					method = Method(className, methodName);
 					method = Method(className, methodName);
+				} else {
+					method = Method(null, fullName);
 				}
 				}
 			}
 			}
 			var fileName = site.getFileName();
 			var fileName = site.getFileName();