Skip to content

Commit

Permalink
[cs] haxe.CallStack implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nadako committed Mar 14, 2014
1 parent b6d2c51 commit 98ff4bd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
6 changes: 3 additions & 3 deletions gencommon.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion gencs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
7 changes: 7 additions & 0 deletions std/cs/internal/Exceptions.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
24 changes: 23 additions & 1 deletion std/haxe/CallStack.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 98ff4bd

Please sign in to comment.