except_native.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Michael Van Canneyt
  4. member of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {****************************************************************************
  12. Exception support
  13. ****************************************************************************}
  14. {$ifdef FPC_HAS_FEATURE_THREADING}
  15. ThreadVar
  16. {$else FPC_HAS_FEATURE_THREADING}
  17. Var
  18. {$endif FPC_HAS_FEATURE_THREADING}
  19. WASM_ExceptAddrStack : PExceptAddr;
  20. WASM_ExceptObjectStack : PExceptObject;
  21. WASM_ExceptTryLevel : ObjpasInt;
  22. { This routine is called only from fpc_raiseexception, which uses ExceptTryLevel
  23. flag to guard against repeated exceptions which can occur due to corrupted stack
  24. or heap. }
  25. function WASM_PushExceptObject(Obj : TObject; AnAddr : CodePointer; AFrame : Pointer): PExceptObject;
  26. var
  27. Newobj : PExceptObject;
  28. _ExceptObjectStack : ^PExceptObject;
  29. framebufsize,
  30. framecount : PtrInt;
  31. frames : PCodePointer;
  32. prev_frame,
  33. curr_frame : Pointer;
  34. curr_addr : CodePointer;
  35. begin
  36. {$ifdef excdebug}
  37. writeln ('In PushExceptObject');
  38. {$endif}
  39. _ExceptObjectStack:=@WASM_ExceptObjectStack;
  40. NewObj:=AllocMem(sizeof(TExceptObject));
  41. NewObj^.Next:=_ExceptObjectStack^;
  42. _ExceptObjectStack^:=NewObj;
  43. NewObj^.FObject:=Obj;
  44. NewObj^.Addr:=AnAddr;
  45. if assigned(get_frame) then
  46. begin
  47. NewObj^.refcount:=0;
  48. { Backtrace }
  49. curr_frame:=AFrame;
  50. curr_addr:=AnAddr;
  51. frames:=nil;
  52. framecount:=0;
  53. framebufsize:=0;
  54. { The frame pointer of this procedure is used as initial stack bottom value. }
  55. prev_frame:=get_frame;
  56. while (framecount<RaiseMaxFrameCount) and (curr_frame > prev_frame) and
  57. (curr_frame<StackTop) do
  58. Begin
  59. prev_frame:=curr_frame;
  60. get_caller_stackinfo(curr_frame,curr_addr);
  61. if (curr_addr=nil) or
  62. (curr_frame=nil) then
  63. break;
  64. if (framecount>=framebufsize) then
  65. begin
  66. inc(framebufsize,16);
  67. reallocmem(frames,framebufsize*sizeof(codepointer));
  68. end;
  69. frames[framecount]:=curr_addr;
  70. inc(framecount);
  71. End;
  72. NewObj^.framecount:=framecount;
  73. NewObj^.frames:=frames;
  74. end;
  75. Result:=NewObj;
  76. end;
  77. {$define FPC_SYSTEM_HAS_RAISEEXCEPTION}
  78. procedure fpc_RaiseException (Obj : TObject; AnAddr : CodePointer; AFrame : Pointer);[Public, Alias : 'FPC_RAISEEXCEPTION']; compilerproc;
  79. var
  80. _ExceptObjectStack : PExceptObject;
  81. _ExceptAddrstack : PExceptAddr;
  82. begin
  83. {$ifdef excdebug}
  84. writeln ('In RaiseException');
  85. {$endif}
  86. if WASM_ExceptTryLevel<>0 then
  87. Halt(217);
  88. WASM_ExceptTryLevel:=1;
  89. WASM_PushExceptObject(Obj,AnAddr,AFrame);
  90. { if PushExceptObject causes another exception, the following won't be executed,
  91. causing halt upon entering this routine recursively. }
  92. WASM_ExceptTryLevel:=0;
  93. //_ExceptAddrstack:=ExceptAddrStack;
  94. //If _ExceptAddrStack=Nil then
  95. // DoUnhandledException;
  96. _ExceptObjectStack:=WASM_ExceptObjectStack;
  97. if (RaiseProc <> nil) and (_ExceptObjectStack <> nil) then
  98. with _ExceptObjectStack^ do
  99. RaiseProc(FObject,Addr,FrameCount,Frames);
  100. // longjmp(_ExceptAddrStack^.Buf^,FPC_Exception);
  101. fpc_wasm32_throw_fpcexception;
  102. end;