Coroutine.hx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package lua;
  23. import haxe.extern.Rest;
  24. import haxe.Constraints.Function;
  25. /**
  26. Externs for native Lua coroutines.
  27. **/
  28. @:native("_G.coroutine")
  29. extern class Coroutine<T:Function> extends Thread {
  30. /**
  31. Creates a new coroutine, with body `f`. `f` must be a Lua function.
  32. **/
  33. static function create<T:Function>(f:T):Coroutine<T>;
  34. /**
  35. Returns the running coroutine plus a boolean, true when the running coroutine is the main one.
  36. **/
  37. static function running():CoroutineRunning;
  38. /**
  39. Returns the status of coroutine.
  40. **/
  41. static function status(c:Coroutine<Dynamic>):CoroutineState;
  42. /**
  43. Starts or continues the execution of coroutine.
  44. The first time you resume a coroutine, it starts running its body.
  45. The values `args` are passed as the arguments to the body function.
  46. If the coroutine has yielded, `resume` restarts it;
  47. the values `args` are passed as the results from the yield.
  48. If the coroutine runs without any errors, `resume` returns `true` plus any
  49. values passed to `yield` (if the coroutine yields) or any values returned
  50. by the body function (if the coroutine terminates). If there is any error,
  51. `resume` returns `false` plus the error message.
  52. **/
  53. static function resume<A, B>(c:Coroutine<Dynamic>, args:Rest<A>):CoroutineResume<B>;
  54. /**
  55. Suspends the execution of the calling coroutine.
  56. The coroutine cannot be running a C function, a metamethod, or an iterator.
  57. Any arguments to `yield` are passed as extra results to `resume`.
  58. **/
  59. static function yield<T>(args:Rest<Dynamic>):T;
  60. /**
  61. Creates a new coroutine, with body `f`.
  62. Returns a function that resumes the coroutine each time it is called.
  63. Any arguments passed to the function behave as the extra arguments to `resume`.
  64. Returns the same values returned by `resume`, except the first boolean.
  65. In case of error, propagates the error.
  66. **/
  67. static function wrap<T:Function>(f:T):T;
  68. }
  69. /**
  70. A enumerator that describes the output of `Coroutine.status()`.
  71. **/
  72. enum abstract CoroutineState(String) {
  73. /**
  74. If the coroutine is suspended in a call to yield, or if it has not started
  75. running yet.
  76. **/
  77. var Suspended = "suspended";
  78. /**
  79. If the coroutine is running.
  80. **/
  81. var Running = "running";
  82. /**
  83. If the coroutine is active but not running. That is, it has resumed another
  84. coroutine.
  85. **/
  86. var Normal = "normal";
  87. /**
  88. If the coroutine has finished its body function or if it has stopped with
  89. an error.
  90. **/
  91. var Dead = "dead";
  92. }
  93. @:multiReturn
  94. extern class CoroutineResume<T> {
  95. var success:Bool;
  96. var result:T;
  97. }
  98. @:multiReturn
  99. extern class CoroutineRunning {
  100. var coroutine:Coroutine<Dynamic>;
  101. var status:Bool;
  102. }