Selaa lähdekoodia

Check for __customStack on haxe.Exception (#782)

---------

Co-authored-by: Yuxiao Mao <[email protected]>
Simon Krajewski 4 kuukautta sitten
vanhempi
commit
2ed23a24f5
4 muutettua tiedostoa jossa 33 lisäystä ja 8 poistoa
  1. 1 0
      src/hl.h
  2. 6 4
      src/hlc_main.c
  3. 6 4
      src/main.c
  4. 20 0
      src/std/error.c

+ 1 - 0
src/hl.h

@@ -642,6 +642,7 @@ HL_API HL_NO_RETURN( void hl_null_access( void ) );
 HL_API void hl_setup_longjump( void *j );
 HL_API void hl_setup_exception( void *resolve_symbol, void *capture_stack );
 HL_API void hl_dump_stack( void );
+HL_API bool hl_maybe_print_custom_stack( vdynamic *exc );
 HL_API varray *hl_exception_stack( void );
 HL_API bool hl_detect_debugger( void );
 

+ 6 - 4
src/hlc_main.c

@@ -171,11 +171,13 @@ int main(int argc, char *argv[]) {
 	cl.fun = hl_entry_point;
 	ret = hl_dyn_call_safe(&cl, NULL, 0, &isExc);
 	if( isExc ) {
-		varray *a = hl_exception_stack();
-		int i;
 		uprintf(USTR("Uncaught exception: %s\n"), hl_to_string(ret));
-		for (i = 0; i<a->size; i++)
-			uprintf(USTR("Called from %s\n"), hl_aptr(a, uchar*)[i]);
+		if( !hl_maybe_print_custom_stack(ret) ) {
+			varray *a = hl_exception_stack();
+			int i;
+			for( i = 0; i < a->size; i++ )
+				uprintf(USTR("Called from %s\n"), hl_aptr(a, uchar*)[i]);
+		}
 	}
 	hl_global_free();
 	sys_global_exit();

+ 6 - 4
src/main.c

@@ -240,11 +240,13 @@ int main(int argc, pchar *argv[]) {
 	ctx.ret = hl_dyn_call_safe(&cl,NULL,0,&isExc);
 	hl_profile_end();
 	if( isExc ) {
-		varray *a = hl_exception_stack();
-		int i;
 		uprintf(USTR("Uncaught exception: %s\n"), hl_to_string(ctx.ret));
-		for(i=0;i<a->size;i++)
-			uprintf(USTR("Called from %s\n"), hl_aptr(a,uchar*)[i]);
+		if( !hl_maybe_print_custom_stack(ctx.ret) ) {
+			varray *a = hl_exception_stack();
+			int i;
+			for( i = 0; i < a->size; i++ )
+				uprintf(USTR("Called from %s\n"), hl_aptr(a, uchar*)[i]);
+		}
 		hl_debug_break();
 		hl_global_free();
 		return 1;

+ 20 - 0
src/std/error.c

@@ -142,6 +142,26 @@ HL_PRIM void hl_dump_stack() {
 	fflush(stdout);
 }
 
+HL_PRIM bool hl_maybe_print_custom_stack( vdynamic *exc ) {
+	hl_type *ot = exc->t;
+	while( ot->kind == HOBJ ) {
+		if( ot->obj->super == NULL ) {
+			if( ucmp(ot->obj->name, USTR("haxe.Exception")) == 0 ) {
+				hl_field_lookup *f = hl_lookup_find(ot->obj->rt->lookup, ot->obj->rt->nlookup, hl_hash_gen(USTR("__customStack"), true));
+				if( f == NULL || f->field_index < 0 ) break;
+				vdynamic *customStack = *(vdynamic**)((char*)(exc) + f->field_index);
+				if( customStack != NULL ) {
+					uprintf(USTR("%s\n"), hl_to_string(customStack));
+					return true;
+				}
+			}
+			break;
+		}
+		ot = ot->obj->super;
+	}
+	return false;
+}
+
 HL_PRIM varray *hl_exception_stack() {
 	hl_thread_info *t = hl_get_thread();
 	varray *a = hl_alloc_array(&hlt_bytes, t->exc_stack_count);