C语言的强大(C语言实现异常机制)
一直知道C很强大,可以编写操作系统等等,但是一直没有概念
今天看了一下KVM的CLDC1.1的源代码,太酷了,一切都是C,加载class,运行class,类多态实现.....加载JAR,运行JAR....实现线程.....还包括实现了异常机制,好强大!
下载地址: j2megame.cn下载
一切的一切,继续学习.....距离....
虽然看不懂,但记录一下实现的,看代码
global.h (异常的实现)
- /*
- * There must not be any allocation guard active when entering a TRY block
- * as an exception throw will always cause at least one allocation (for the
- * exception object) thus potentially invalidating any pointers being
- * guarded by the allocation guard. Note however that this does not preclude
- * using an allocation guard inside a TRY block.
- */
- #define ASSERT_NO_ALLOCATION_GUARD \
- if (NoAllocation > 0) { \
- fatalError(KVM_MSG_TRY_BLOCK_ENTERED_WHEN_ALLOCATION_FORBIDDEN); \
- }
- #else
- #define TRACE_EXCEPTION(name)
- #define ASSERT_NO_ALLOCATION_GUARD
- #endif
- #define TRY \
- { \
- struct throwableScopeStruct __scope__; \
- int __state__; \
- jmp_buf __env__; \
- __scope__.outer = ThrowableScope; \
- ThrowableScope = &__scope__; \
- ThrowableScope->env = &__env__; \
- ThrowableScope->tmpRootsCount = TemporaryRootsLength; \
- ASSERT_NO_ALLOCATION_GUARD \
- TRACE_EXCEPTION("TRY") \
- if ((__state__ = setjmp(__env__)) == 0) {
- /*
- * Any non-null THROWABLE_INSTANCE passed into a CATCH clause
- * is protected as a temporary root.
- */
- #define CATCH(__throwable__) \
- } \
- TRACE_EXCEPTION("CATCH") \
- ThrowableScope = __scope__.outer; \
- TemporaryRootsLength = __scope__.tmpRootsCount; \
- if (__state__ != 0) { \
- START_TEMPORARY_ROOTS \
- DECLARE_TEMPORARY_ROOT(THROWABLE_INSTANCE, \
- __throwable__,__scope__.throwable);
- #define END_CATCH \
- END_TEMPORARY_ROOTS \
- } \
- }
- /*
- * This macro is required for jumping out of a CATCH block with a goto.
- * This is used in FastInterpret so that the interpreter loop is re-entered
- * with a fresh TRY statement.
- */
- #define END_CATCH_AND_GOTO(label) \
- END_TEMPORARY_ROOTS \
- goto label; \
- } \
- }
- #define THROW(__throwable__) \
- { \
- THROWABLE_INSTANCE __t__ = __throwable__; \
- TRACE_EXCEPTION("THROW") \
- if (__t__ == NULL) \
- fatalVMError("THROW called with NULL"); \
- ThrowableScope->throwable = __t__; \
- longjmp(*((jmp_buf*)ThrowableScope->env),1); \
- }
class.c (异常的使用)
- /*===========================================
- * FUNCTION: initializeClass()
- * TYPE: constructor
- * OVERVIEW: After loading a class, it must be initialized
- * by executing the possible internal static
- * constructor '<clinit>'. This will initialize the
- * necessary static structures.
- *
- * This function sets up the necessary Class.runClinit
- * frame and returns to the interpreter.
- * INTERFACE:
- * parameters: class pointer
- * returns: <nothing>
- *===========================================*/
- void initializeClass(INSTANCE_CLASS thisClass)
- {
- if (thisClass->status == CLASS_ERROR) {
- raiseException(NoClassDefFoundError);
- } else if (thisClass->status < CLASS_READY) {
- if (thisClass->status < CLASS_VERIFIED) {
- verifyClass(thisClass);
- }
- /*
- * VerifyError will have been thrown or status will be
- * CLASS_VERIFIED. We can skip execution of <clinit> altogether if
- * it does not exists AND the superclass is already initialised.
- */
- if ((thisClass->superClass == NULL ||
- thisClass->superClass->status == CLASS_READY) &&
- getSpecialMethod(thisClass,clinitNameAndType) == NULL) {
- setClassStatus(thisClass,CLASS_READY);
- }
- else {
- TRY {
- pushFrame(RunCustomCodeMethod);
- pushStackAsType(CustomCodeCallbackFunction, &runClinit);
- pushStackAsType(INSTANCE_CLASS, thisClass);
- pushStackAsType(long, 1);
- } CATCH (e) {
- /* Stack overflow */
- setClassStatus(thisClass, CLASS_ERROR);
- THROW(e);
- } END_CATCH
- }
- }
- }
上一篇: 实现J2ME游戏的按键(键盘)缓冲





