fcontext.monkey2 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'For testing purposes only - use Fiber instead!
  2. '
  3. 'Will generally cause havoc on GC and debugger as they wont know you're messing with the stack...
  4. Namespace std.fiber
  5. #import "native/fcontext.cpp"
  6. #import "native/fcontext.h"
  7. #If __TARGET__="windows"
  8. #import "native/asm/make_i386_ms_pe_gas.asm"
  9. #import "native/asm/jump_i386_ms_pe_gas.asm"
  10. #import "native/asm/ontop_i386_ms_pe_gas.asm"
  11. #Else If __TARGET__="macos"
  12. #import "native/asm/make_x86_64_sysv_macho_gas.S"
  13. #import "native/asm/jump_x86_64_sysv_macho_gas.S"
  14. #import "native/asm/ontop_x86_64_sysv_macho_gas.S"
  15. #Else If __TARGET__="linux"
  16. #import "native/asm/make_x86_64_sysv_elf_gas.S"
  17. #import "native/asm/jump_x86_64_sysv_elf_gas.S"
  18. #import "native/asm/ontop_x86_64_sysv_elf_gas.S"
  19. #Else If __TARGET__="android" or __TARGET__="raspbian"
  20. #import "native/asm/make_arm_aapcs_elf_gas.S"
  21. #import "native/asm/jump_arm_aapcs_elf_gas.S"
  22. #import "native/asm/ontop_arm_aapcs_elf_gas.S"
  23. #Else If __TARGET__="ios"
  24. #import "native/asm/make_arm_aapcs_macho_gas.S"
  25. #import "native/asm/jump_arm_aapcs_macho_gas.S"
  26. #import "native/asm/ontop_arm_aapcs_macho_gas.S"
  27. #Endif
  28. Extern Private
  29. Alias fcontext_t:Void Ptr
  30. Struct transfer_t
  31. Field fcontext:fcontext_t
  32. Field data:Void Ptr
  33. End
  34. Function alloc_fcontext_stack:UByte Ptr( size:ULong )
  35. Function free_fcontext_stack( stack:Void Ptr,size:ULong )
  36. Function jump_fcontext:transfer_t( fcontext:fcontext_t,data:Void Ptr )
  37. Function make_fcontext:fcontext_t( stack:Void Ptr,stack_size:ULong,func:Void( transfer_t ) )
  38. Function ontop_fcontext:transfer_t( fcontext:fcontext_t,vp:Void ptr,func:transfer_t(transfer_t) )
  39. Public
  40. #rem
  41. Function Test( t:transfer_t )
  42. Print "Test 1"
  43. Print ULong( t.fcontext )
  44. t=jump_fcontext( t.fcontext,Null )
  45. Print "Test 2"
  46. Print ULong( t.fcontext )
  47. jump_fcontext( t.fcontext,Null )
  48. End
  49. Function Test2( fcontext:fcontext_t )
  50. jump_fcontext( fcontext,Null )
  51. End
  52. Function Main()
  53. Local stack:=alloc_fcontext_stack( 65536 )
  54. Local fcontext:=make_fcontext( stack+65536,65536,Test )
  55. fcontext=jump_fcontext( fcontext,Null ).fcontext
  56. Test2( fcontext )
  57. ' fcontext=jump_fcontext( fcontext,Null ).fcontext
  58. Return
  59. Print "Main"
  60. fcontext=jump_fcontext( fcontext,Null ).fcontext
  61. Print "Main"
  62. Test2( fcontext )
  63. End
  64. #end