; _______________________________________________________________________________________
;|                                                                                       |
;| ..::FreshLib::..  Free, open source. Licensed under "BSD 2-clause" license."          |
;|_______________________________________________________________________________________|
;
;  Description: Win32 exception handling macros.
;
;  Target OS: Win32
;
;  Dependencies:
;
;  Notes:
;_________________________________________________________________________________________

..TryLevel = 0

macro beginTry {
  common
    ptrExceptionContext equ esp+$0c
    ptrExceptionRecord  equ esp+$04

    local ..handler, ..endtry
    end@try equ ..endtry

  if defined ..handler
        ..TryLevel = ..TryLevel+1
        pushd   ..handler
        pushd   [fs:0]
        mov     [fs:0], esp
  end if

  macro onException \{
        jmp     ..endtry
    ..handler:
  \}

  macro Ignore \{
        mov     eax, [ptrExceptionContext]
        mov     [eax+CONTEXT.regEip], ..endtry
        xor     eax, eax
        retn
  \}

  macro Retry \{
        xor     eax, eax
        retn
  \}


  macro Next \{
        mov     eax, 1
        retn
  \}
}



macro endTry {
common
  label end@try
  if ..TryLevel > 0
        popd    [fs:0]
        add     esp, 4
        ..TryLevel = ..TryLevel - 1
  end if
  purge onException
  purge Ignore
  purge Next
  purge Retry
  restore end@try
  restore ptrExceptionContext
  restore ptrExceptionRecord
}




