wasm.exceptions.pas 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. {
  2. This file is part of the Free Component Library
  3. Export last exception info from Webassembly API.
  4. Copyright (c) 2025 by Michael Van Canneyt [email protected]
  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. unit wasm.exceptions;
  12. {$mode objfpc}
  13. interface
  14. uses
  15. {$IFDEF FPC_DOTTEDUNITS}
  16. System.SysUtils;
  17. {$ELSE}
  18. SysUtils;
  19. {$ENDIF}
  20. Type
  21. TLastExceptionInfo = record
  22. ClassName : AnsiString;
  23. ClassNameLen : Integer;
  24. Message : UTF8String;
  25. MessageLen : Integer;
  26. more : boolean;
  27. end;
  28. PLastExceptionInfo = ^TLastExceptionInfo;
  29. function GetLastExceptionInfo : PLastExceptionInfo;
  30. procedure FreeLastExceptionInfo(aInfo : PLastExceptionInfo);
  31. implementation
  32. function PopObjectStack : TObject; external name 'FPC_POPOBJECTSTACK';
  33. function GetLastExceptionInfo : PLastExceptionInfo;
  34. var
  35. lExc : PExceptObject;
  36. lMsg : UTF8String;
  37. Obj : TObject;
  38. begin
  39. Result:=nil;
  40. lExc:=RaiseList;
  41. if lExc=Nil then
  42. exit;
  43. Result:=New(PLastExceptionInfo);
  44. Result^:=Default(TLastExceptionInfo);
  45. if assigned(lExc^.FObject) then
  46. begin
  47. Result^.ClassName:=LExc^.FObject.ClassName;
  48. if LExc^.FObject is Exception then
  49. begin
  50. {$IF SizeOf(Char)=2}
  51. Result^.Message:=UTF8Encode(Exception(LExc^.FObject).Message);
  52. {$ELSE}
  53. Result^.Message:=Exception(LExc^.FObject).Message;
  54. {$ENDIF}
  55. end
  56. else
  57. Result^.Message:=LExc^.FObject.ToString;
  58. Result^.ClassNameLen:=Length(Result^.ClassName);
  59. Result^.MessageLen:=Length(Result^.Message);
  60. end;
  61. Result^.More:=Assigned(lExc^.Next);
  62. ReleaseExceptionObject;
  63. Obj:=PopObjectStack;
  64. Obj.Free;
  65. end;
  66. procedure FreeLastExceptionInfo(aInfo : PLastExceptionInfo);
  67. begin
  68. Dispose(aInfo);
  69. end;
  70. exports
  71. GetLastExceptionInfo,
  72. FreeLastExceptionInfo;
  73. end.