fcontext.monkey2 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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"
  20. #Import "native/asm/android-$(TARGET_ARCH)/make.S"
  21. #Import "native/asm/android-$(TARGET_ARCH)/jump.S"
  22. #Import "native/asm/android-$(TARGET_ARCH)/ontop.S"
  23. #Else If __TARGET__="raspbian"
  24. #import "native/asm/make_arm_aapcs_elf_gas.S"
  25. #import "native/asm/jump_arm_aapcs_elf_gas.S"
  26. #import "native/asm/ontop_arm_aapcs_elf_gas.S"
  27. #Else If __TARGET__="ios"
  28. #import "native/asm/make_arm_aapcs_macho_gas.S"
  29. #import "native/asm/jump_arm_aapcs_macho_gas.S"
  30. #import "native/asm/ontop_arm_aapcs_macho_gas.S"
  31. #import "native/asm/make_arm64_aapcs_macho_gas.S"
  32. #import "native/asm/jump_arm64_aapcs_macho_gas.S"
  33. #import "native/asm/ontop_arm64_aapcs_macho_gas.S"
  34. #Endif
  35. Extern Private
  36. Alias fcontext_t:Void Ptr
  37. Struct transfer_t
  38. Field fcontext:fcontext_t
  39. Field data:Void Ptr
  40. End
  41. Function alloc_fcontext_stack:UByte Ptr( size:ULong )
  42. Function free_fcontext_stack( stack:Void Ptr,size:ULong )
  43. Function jump_fcontext:transfer_t( fcontext:fcontext_t,data:Void Ptr )
  44. Function make_fcontext:fcontext_t( stack:Void Ptr,stack_size:ULong,func:Void( transfer_t ) )
  45. Function ontop_fcontext:transfer_t( fcontext:fcontext_t,vp:Void ptr,func:transfer_t(transfer_t) )
  46. Public
  47. #rem
  48. Function Test( t:transfer_t )
  49. Print "Test 1"
  50. Print ULong( t.fcontext )
  51. t=jump_fcontext( t.fcontext,Null )
  52. Print "Test 2"
  53. Print ULong( t.fcontext )
  54. jump_fcontext( t.fcontext,Null )
  55. End
  56. Function Test2( fcontext:fcontext_t )
  57. jump_fcontext( fcontext,Null )
  58. End
  59. Function Main()
  60. Local stack:=alloc_fcontext_stack( 65536 )
  61. Local fcontext:=make_fcontext( stack+65536,65536,Test )
  62. fcontext=jump_fcontext( fcontext,Null ).fcontext
  63. Test2( fcontext )
  64. ' fcontext=jump_fcontext( fcontext,Null ).fcontext
  65. Return
  66. Print "Main"
  67. fcontext=jump_fcontext( fcontext,Null ).fcontext
  68. Print "Main"
  69. Test2( fcontext )
  70. End
  71. #end