digitalmars.D - i hope a new type (scope type) the scope type would have properties scopeexit(binary exit address), scopefailure
- davidl (141/141) Oct 31 2006 I'm considerring a OS-independ exception handle system.
I'm considerring a OS-independ exception handle system. I need to know the scope(success) and scope(failure) 's binary address. I use the thrower to retn to those scopes, and thrower is implemented by looking handler mapping, and some code would be like this class exceptionbase { this(){} } struct handler{ void delegate () handler; scope *parentfunc; } handler handlermap[char[]]; // example usage of thower would be something like this : thower( exceptionclasstype, &thisfunc); // exceptionclasstype is inherited from exceptionbase void thrower(exceptionbase)(exceptionbase exptype,scopetype *scope) { int i = &thrower; asm { mov EAX, i; mov EAX, CS:[EAX]; // get the first instruction of thrower // possibly a enter 8 or enter number; // if others we get the orignal ESP directly } //let me ignore some code to get the ESP which point to the caller //assume Seg = CS , Offset=EIP of the caller if (typeid(typeof(exptype)).classinfo.classname in handlermap.keys) { handlermap[typeid(typeof(exptype)).classinfo.classname].handler; //call the handler as a delegate\ if (handlermap[typeid(typeof(exptype)).classinfo.classname].parentfunc<>func) // we don't have a handler in the caller scope; { //hack the stack CS,EIP to make thrower return to the parentfunc.scopefailue. //even more we can set a global var to let the caller to determine //whether jump to scopefailure or return from thrower directly //and we can pass the exptype.msgtype to the handler if we have msgtype as a member in base class } } } All i need is the address of a func.scopefailure, then a exception handle system without OS support could be done Though more should be considered, like thread-safe. but I think this implementation would get the exception throw faster and get the exception more flexible, we don't always break the procedure if no handler available in this scope, this is determined by the parent scope's handler. Think about the following(which is from KTNE) class exception:exceptionbase { this(){} } void delete_directory(char[]sdir) { foreach(eachfile; filelist) { result=delete_file; if (result=fail) thrower(exception, &delete_directory); // call the example psuedo code above } } void caller(char[] sdir) { int i=0; void myhandler() { i++; if (i>threshold) throw Exception("can't skip so many files "); //original exception } //exception handler registry(myhandler,caller ) delete_diretory(sdir); } we can have threshold controlled in my parent code. and consider the following(which is original idea from me , i meet a probelm in GUI programming) class Form1 { TMemo memo1; void method(); } class Form2 { TMemo memo1; void method(); } Form1.method() and Form2.method() would do some thing and show some thing on memo1. It's quite common GUI develop style. But what's not so good is that actually method shouldn't do those interactive jobs. That makes the code coupled together so tightly then we are unable to call Form2.method() in Form1.method() which i want the info show back all in Form1.memo1. with our "exception mechanism" we can have something like: void Form1.methodcaller() { void myhandler() { //showing messages on Form1.memo1 //set a global var to let thrower in method just return to method not method.scopefailure; } //exception handler registry(myhandler,Form1.methodcaller ) method(); // it only throw so called exception to the methodcaller's handler; } void Form1.method() { //do something ... //we want to show something out; exceptiondisplay.msgtype = "info"; thower(exceptiondisplay, &method) //do another thing ... //we want to show something out; exceptiondisplay.msgtype = "info"; thower(exceptiondisplay, &method) // actually just calling a delegate // but we don't need to send the delegate in // we have the delegate "registered" in the caller func } All above i just want to state a OS-independent exception handle system is useful, i want to call it action instead of exception :p and What i need is scope ID, and scope failure binary address in that scope ID and i hope add a new type of scope like we have ifscope, whilescope, something like that. and thisscope would mean the currenty scope type var which has properties of addresses of scope exit scope success scope failure , etc. -- 使用 Opera 革命性的电子邮件客户程序: http://www.opera.com/mail/
Oct 31 2006