Nicolas Cannasse 9 năm trước cách đây
mục cha
commit
bed22bb875
5 tập tin đã thay đổi với 53 bổ sung95 xóa
  1. 4 4
      hlc.vcxproj
  2. 1 1
      src/hl.h
  3. 44 0
      src/hlc_main.c
  4. 1 1
      src/module.c
  5. 3 89
      src/std/error.c

+ 4 - 4
hlc.vcxproj

@@ -97,7 +97,7 @@
     <Link>
       <SubSystem>Console</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
-      <AdditionalDependencies>std.lib;sdl.lib;fmt.lib</AdditionalDependencies>
+      <AdditionalDependencies>std.lib;sdl.lib;fmt.lib;ui.lib</AdditionalDependencies>
       <StackReserveSize>0x200000</StackReserveSize>
     </Link>
   </ItemDefinitionGroup>
@@ -112,7 +112,7 @@
     <Link>
       <SubSystem>Console</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
-      <AdditionalDependencies>std.lib;sdl.lib;fmt.lib</AdditionalDependencies>
+      <AdditionalDependencies>std.lib;sdl.lib;fmt.lib;ui.lib</AdditionalDependencies>
       <StackReserveSize>0x200000</StackReserveSize>
     </Link>
   </ItemDefinitionGroup>
@@ -134,7 +134,7 @@
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
-      <AdditionalDependencies>std.lib;sdl.lib;fmt.lib</AdditionalDependencies>
+      <AdditionalDependencies>std.lib;sdl.lib;fmt.lib;ui.lib</AdditionalDependencies>
       <StackReserveSize>0x200000</StackReserveSize>
     </Link>
   </ItemDefinitionGroup>
@@ -155,7 +155,7 @@
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
-      <AdditionalDependencies>std.lib;sdl.lib;fmt.lib</AdditionalDependencies>
+      <AdditionalDependencies>std.lib;sdl.lib;fmt.lib;ui.lib</AdditionalDependencies>
       <StackReserveSize>0x200000</StackReserveSize>
     </Link>
   </ItemDefinitionGroup>

+ 1 - 1
src/hl.h

@@ -439,7 +439,7 @@ HL_API const uchar *hl_field_name( int hash );
 HL_API void hl_error_msg( const uchar *msg, ... );
 HL_API void hl_throw( vdynamic *v );
 HL_API void hl_rethrow( vdynamic *v );
-HL_API void hl_exception_setup( void *resolve_symbol, void *capture_stack );
+HL_API void hl_setup_exception( void *resolve_symbol, void *capture_stack );
 
 HL_API varray *hl_exception_stack();
 HL_API int hl_get_stack_hash();

+ 44 - 0
src/hlc_main.c

@@ -21,6 +21,12 @@
  */
 #include <hlc.h>
 
+#ifdef _WIN32
+#	pragma warning(disable:4091)
+#	include <DbgHelp.h>
+#	pragma comment(lib, "Dbghelp.lib")
+#endif
+
 #ifdef HL_VCC
 #	include <crtdbg.h>
 #else
@@ -28,6 +34,43 @@
 #	define _CrtCheckMemory()
 #endif
 
+static uchar *hlc_resolve_symbol( void *addr, uchar *out, int *outSize ) {
+#ifdef _WIN32
+	static HANDLE stack_process_handle = NULL;
+	DWORD64 index;
+	IMAGEHLP_LINEW64 line;
+	struct {
+		SYMBOL_INFOW sym;
+		uchar buffer[256];
+	} data;
+	data.sym.SizeOfStruct = sizeof(data.sym);
+	data.sym.MaxNameLen = 255;
+	if( !stack_process_handle ) {
+		stack_process_handle = GetCurrentProcess();
+		SymSetOptions(SYMOPT_LOAD_LINES);
+		SymInitialize(stack_process_handle,NULL,TRUE);
+	}
+	if( SymFromAddrW(stack_process_handle,(DWORD64)(int_val)addr,&index,&data.sym) ) {
+		DWORD offset = 0;
+		line.SizeOfStruct = sizeof(line);
+		line.FileName = USTR("\\?");
+		line.LineNumber = 0;
+		SymGetLineFromAddrW64(stack_process_handle, (DWORD64)(int_val)addr, &offset, &line);
+		*outSize = usprintf(out,*outSize,USTR("%s(%s) line %d"),data.sym.Name,wcsrchr(line.FileName,'\\')+1,(int)line.LineNumber);
+		return out;
+	}
+#endif
+	return NULL;
+}
+
+static int hlc_capture_stack( void **stack, int size ) {
+	int count = 0;
+#	ifdef _WIN32
+	count = CaptureStackBackTrace(2, size, stack, NULL) - 8; // 8 startup
+	if( count < 0 ) count = 0;
+#	endif
+	return count;
+}
 
 #ifdef HL_WIN
 int wmain(int argc, uchar *argv[]) {
@@ -37,6 +80,7 @@ int main(int argc, char *argv[]) {
 	hl_trap_ctx ctx;
 	vdynamic *exc;
 	hl_global_init(&ctx);
+	hl_setup_exception(hlc_resolve_symbol,hlc_capture_stack);
 	hl_setup_callbacks(hlc_static_call, hlc_get_wrapper);
 	hl_sys_init(argv + 1,argc - 1);
 #	ifdef _DEBUG

+ 1 - 1
src/module.c

@@ -257,7 +257,7 @@ int hl_module_init( hl_module *m ) {
 	hl_callback_init(((unsigned char*)m->jit_code) + entry);
 	cur_module = m;
 	stack_top = &m;
-	hl_exception_setup(module_resolve_symbol, module_capture_stack);
+	hl_setup_exception(module_resolve_symbol, module_capture_stack);
 	hl_jit_free(ctx);
 	return 1;
 }

+ 3 - 89
src/std/error.c

@@ -23,21 +23,12 @@
 #include <stdarg.h>
 #include <string.h>
 
-#ifdef _WIN32
-#	pragma warning(disable:4091)
-#	include <DbgHelp.h>
-#	pragma comment(lib, "Dbghelp.lib")
-#endif
-
 HL_PRIM hl_trap_ctx *hl_current_trap = NULL;
 HL_PRIM vdynamic *hl_current_exc = NULL;
 
 static vdynamic *stack_last_exc = NULL;
 static void *stack_trace[0x1000];
 static int stack_count = 0;
-#ifdef _WIN32
-static HANDLE stack_process_handle = NULL;
-#endif
 
 HL_PRIM void *hl_fatal_error( const char *msg, const char *file, int line ) {
 	printf("%s(%d) : FATAL ERROR : %s\n",file,line,msg);
@@ -46,91 +37,14 @@ HL_PRIM void *hl_fatal_error( const char *msg, const char *file, int line ) {
 	return NULL;
 }
 
-static hl_field_lookup *stack_hashes = NULL;
-static int stack_hashes_count = 0;
-static int stack_hashes_size = 0;
-
-HL_PRIM void **hl_stack_from_hash( int hash, int *count ) {
-	hl_field_lookup *f = hl_lookup_find(stack_hashes,stack_hashes_count,hash);
-	if( !f ) {
-		*count = 0;
-		return NULL;
-	}
-	*count = f->field_index;
-	return (void**)f->t;
-}
-
-HL_PRIM int hl_get_stack_hash() {
-	void *stack[16], **nstack;
-	int hash = 0;
-	int count = 0;
-#ifdef _WIN32
-	count = CaptureStackBackTrace(1, 16, stack, (PDWORD)&hash);
-#endif
-	// lookup if we already know this hash
-	if( hl_lookup_find(stack_hashes,stack_hashes_count,hash) != NULL )
-		return hash;
-	if( stack_hashes_count == stack_hashes_size ) {
-		int nsize = stack_hashes_size ? stack_hashes_size << 1 : 128;
-		hl_field_lookup *ns = (hl_field_lookup*)malloc(sizeof(hl_field_lookup)*nsize);
-		memcpy(ns,stack_hashes,stack_hashes_size*sizeof(hl_field_lookup));
-		free(stack_hashes);
-		stack_hashes = ns;
-		stack_hashes_size = nsize;
-	}
-	nstack = (void**)malloc(sizeof(void*)*count);
-	memcpy(nstack,stack,count*sizeof(void*));
-	hl_lookup_insert(stack_hashes,stack_hashes_count,hash,(hl_type*)nstack,count);
-	stack_hashes_count++;
-	return hash;
-}
-
-
-HL_PRIM uchar *hl_resolve_symbol( void *addr, uchar *out, int *outSize ) {
-#ifdef _WIN32
-	DWORD64 index;
-	IMAGEHLP_LINEW64 line;
-	struct {
-		SYMBOL_INFOW sym;
-		uchar buffer[256];
-	} data;
-	data.sym.SizeOfStruct = sizeof(data.sym);
-	data.sym.MaxNameLen = 255;
-	if( !stack_process_handle ) {
-		stack_process_handle = GetCurrentProcess();
-		SymSetOptions(SYMOPT_LOAD_LINES);
-		SymInitialize(stack_process_handle,NULL,TRUE);
-	}
-	if( SymFromAddrW(stack_process_handle,(DWORD64)(int_val)addr,&index,&data.sym) ) {
-		DWORD offset = 0;
-		line.SizeOfStruct = sizeof(line);
-		line.FileName = USTR("\\?");
-		line.LineNumber = 0;
-		SymGetLineFromAddrW64(stack_process_handle, (DWORD64)(int_val)addr, &offset, &line);
-		*outSize = usprintf(out,*outSize,USTR("%s(%s) line %d"),data.sym.Name,wcsrchr(line.FileName,'\\')+1,(int)line.LineNumber);
-		return out;
-	}
-#endif
-	return NULL;
-}
-
-static int hl_capture_stack( void **stack, int size ) {
-	int count = 0;
-#	ifdef _WIN32
-	count = CaptureStackBackTrace(2, size, stack, NULL) - 8; // 8 startup
-	if( count < 0 ) count = 0;
-#	endif
-	return count;
-}
-
 typedef uchar *(*resolve_symbol_type)( void *addr, uchar *out, int *outSize );
 typedef int (*capture_stack_type)( void **stack, int size );
 
-static resolve_symbol_type resolve_symbol_func = hl_resolve_symbol;
-static capture_stack_type capture_stack_func = hl_capture_stack;
+static resolve_symbol_type resolve_symbol_func = NULL;
+static capture_stack_type capture_stack_func = NULL;
 static vclosure *hl_error_handler = NULL;
 
-HL_PRIM void hl_exception_setup( void *resolve_symbol, void *capture_stack ) {
+HL_PRIM void hl_setup_exception( void *resolve_symbol, void *capture_stack ) {
 	resolve_symbol_func = resolve_symbol;
 	capture_stack_func = capture_stack;
 }