Programming Technology

C语言的强大(C语言实现异常机制)

一直知道C很强大,可以编写操作系统等等,但是一直没有概念

今天看了一下KVM的CLDC1.1的源代码,太酷了,一切都是C,加载class,运行class,类多态实现.....加载JAR,运行JAR....实现线程.....还包括实现了异常机制,好强大!

下载地址: j2megame.cn下载

一切的一切,继续学习.....距离....

虽然看不懂,但记录一下实现的,看代码

global.h (异常的实现)

  1. /*
  2.  * There must not be any allocation guard active when entering a TRY block
  3.  * as an exception throw will always cause at least one allocation (for the
  4.  * exception object) thus potentially invalidating any pointers being
  5.  * guarded by the allocation guard. Note however that this does not preclude
  6.  * using an allocation guard inside a TRY block.
  7.  */
  8. #define ASSERT_NO_ALLOCATION_GUARD \
  9.     if (NoAllocation > 0) { \
  10.         fatalError(KVM_MSG_TRY_BLOCK_ENTERED_WHEN_ALLOCATION_FORBIDDEN); \
  11.     }
  12. #else
  13. #define TRACE_EXCEPTION(name)
  14. #define ASSERT_NO_ALLOCATION_GUARD
  15. #endif
  16.  
  17. #define TRY                                                    \
  18.     {                                                          \
  19.         struct throwableScopeStruct __scope__;                 \
  20.         int __state__;                                         \
  21.         jmp_buf __env__;                                       \
  22.         __scope__.outer = ThrowableScope;                      \
  23.         ThrowableScope = &__scope__;                           \
  24.         ThrowableScope->env = &__env__;                        \
  25.         ThrowableScope->tmpRootsCount = TemporaryRootsLength;  \
  26.         ASSERT_NO_ALLOCATION_GUARD                             \
  27.         TRACE_EXCEPTION("TRY")                                 \
  28.         if ((__state__ = setjmp(__env__)) == 0) {
  29.  
  30. /*
  31.  * Any non-null THROWABLE_INSTANCE passed into a CATCH clause
  32.  * is protected as a temporary root.
  33.  */
  34. #define CATCH(__throwable__)                                   \
  35.         }                                                      \
  36.         TRACE_EXCEPTION("CATCH")                               \
  37.         ThrowableScope = __scope__.outer;                      \
  38.         TemporaryRootsLength = __scope__.tmpRootsCount;        \
  39.         if (__state__ != 0) {                                  \
  40.             START_TEMPORARY_ROOTS                              \
  41.                  DECLARE_TEMPORARY_ROOT(THROWABLE_INSTANCE,    \
  42.                      __throwable__,__scope__.throwable);
  43.  
  44. #define END_CATCH                                              \
  45.             END_TEMPORARY_ROOTS                                \
  46.         }                                                      \
  47.     }
  48.  
  49. /*
  50.  * This macro is required for jumping out of a CATCH block with a goto.
  51.  * This is used in FastInterpret so that the interpreter loop is re-entered
  52.  * with a fresh TRY statement.
  53.  */
  54. #define END_CATCH_AND_GOTO(label)                              \
  55.             END_TEMPORARY_ROOTS                                \
  56.             goto label;                                        \
  57.         }                                                      \
  58.     }
  59.  
  60. #define THROW(__throwable__)                                   \
  61.     {                                                          \
  62.         THROWABLE_INSTANCE __t__ = __throwable__;              \
  63.         TRACE_EXCEPTION("THROW")                               \
  64.         if (__t__ == NULL)                                     \
  65.             fatalVMError("THROW called with NULL");            \
  66.         ThrowableScope->throwable = __t__;                     \
  67.         longjmp(*((jmp_buf*)ThrowableScope->env),1);           \
  68.     }

class.c (异常的使用)

  1. /*===========================================
  2.  * FUNCTION:      initializeClass()
  3.  * TYPE:          constructor
  4.  * OVERVIEW:      After loading a class, it must be initialized
  5.  *                by executing the possible internal static
  6.  *                constructor '<clinit>'. This will initialize the
  7.  *                necessary static structures.
  8.  *
  9.  *                This function sets up the necessary Class.runClinit
  10.  *                frame and returns to the interpreter.
  11.  * INTERFACE:
  12.  *   parameters:  class pointer
  13.  *   returns:     <nothing>
  14.  *===========================================*/
  15.  
  16. void initializeClass(INSTANCE_CLASS thisClass)
  17. {
  18.     if (thisClass->status == CLASS_ERROR) {
  19.         raiseException(NoClassDefFoundError);
  20.     } else if (thisClass->status < CLASS_READY) {
  21.         if (thisClass->status < CLASS_VERIFIED) {
  22.             verifyClass(thisClass);
  23.         }
  24.         /*
  25.          * VerifyError will have been thrown or status will be
  26.          * CLASS_VERIFIED. We can skip execution of <clinit> altogether if
  27.          * it does not exists AND the superclass is already initialised.
  28.          */
  29.         if ((thisClass->superClass == NULL ||
  30.             thisClass->superClass->status == CLASS_READY) &&
  31.             getSpecialMethod(thisClass,clinitNameAndType) == NULL) {
  32.             setClassStatus(thisClass,CLASS_READY);
  33.         }
  34.         else {
  35.             TRY {
  36.                 pushFrame(RunCustomCodeMethod);
  37.                 pushStackAsType(CustomCodeCallbackFunction, &runClinit);
  38.                 pushStackAsType(INSTANCE_CLASS, thisClass);
  39.                 pushStackAsType(long, 1);
  40.             } CATCH (e) {
  41.                 /* Stack overflow */
  42.                 setClassStatus(thisClass, CLASS_ERROR);
  43.                 THROW(e);
  44.             } END_CATCH
  45.         }
  46.     }
  47. }

 

发表评论 ( 如果您刚刚提交过评论,但是还没有被显示出来,请点击这里刷新一下: 刷新评论 )

标题

内容*

昵称*

电子邮件

个人网页


 authimage