Here is my way to resolve this problem: using inline asm and FASM macro directive.
Code: Select all
Declare AtExitHandler()
Global NewList atexit_list.i()
Global exit_code.i, *exit_handler = @AtExitHandler()
Procedure AtExit(*callback)
AddElement(atexit_list())
atexit_list() = *callback
EndProcedure
Procedure AtExitHandler()
ForEach atexit_list()
CallFunctionFast(atexit_list())
Next
EndProcedure
!macro _PB_EndFunctions nul
!{
!label _PB_EndFunctions
EnableASM
mov eax, dword [esp+4]
mov exit_code, eax
call *exit_handler
DisableASM
!}
!macro PB_EndFunctions nul
!{
!label PB_EndFunctions
EnableASM
mov rax, [PB_ExitCode]
mov exit_code, rax
sub rsp, 40
call *exit_handler
add rsp, 40
DisableASM
!}
Procedure Test1()
MessageRequester("AtExit1", "exit code: " + Str(exit_code))
EndProcedure
Procedure Test2()
MessageRequester("AtExit2", "exit code: " + Str(exit_code))
EndProcedure
AtExit(@Test1())
AtExit(@Test2())
End 123