Coroutine.hx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C)2005-2016 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.Constraints.Function;
  24. import haxe.extern.Rest;
  25. /**
  26. Externs for native Lua coroutines.
  27. **/
  28. @:native("_G.coroutine")
  29. extern class Coroutine {
  30. /**
  31. Creates a new coroutine, with body `f`. `f` must be a Lua function.
  32. **/
  33. public static function create(f : Function) : Thread;
  34. /**
  35. Returns the status of coroutine.
  36. **/
  37. public static function status(c : Coroutine) : ThreadState;
  38. /**
  39. Starts or continues the execution of coroutine.
  40. The first time you resume a coroutine, it starts running its body.
  41. The values `args` are passed as the arguments to the body function.
  42. If the coroutine has yielded, `resume` restarts it;
  43. the values `args` are passed as the results from the yield.
  44. If the coroutine runs without any errors, `resume` returns `true` plus any
  45. values passed to `yield` (if the coroutine yields) or any values returned
  46. by the body function (if the coroutine terminates). If there is any error,
  47. `resume` returns `false` plus the error message.
  48. **/
  49. public static function resume(c : Coroutine, args : Rest<Dynamic>) : Dynamic;
  50. /**
  51. Suspends the execution of the calling coroutine.
  52. The coroutine cannot be running a C function, a metamethod, or an iterator.
  53. Any arguments to `yield` are passed as extra results to `resume`.
  54. **/
  55. public static function yield(args : Rest<Dynamic>) : Dynamic;
  56. /**
  57. Creates a new coroutine, with body `f`.
  58. Returns a function that resumes the coroutine each time it is called.
  59. Any arguments passed to the function behave as the extra arguments to `resume`.
  60. Returns the same values returned by `resume`, except the first boolean.
  61. In case of error, propagates the error.
  62. **/
  63. public static function wrap(f : Function) : Thread;
  64. }
  65. /**
  66. A enumerator that describes the output of `Coroutine.status()`.
  67. **/
  68. @:enum
  69. abstract ThreadState(String) {
  70. /**
  71. If the coroutine is suspended in a call to yield, or if it has not started
  72. running yet.
  73. **/
  74. var Suspended = "suspended";
  75. /**
  76. If the coroutine is running.
  77. **/
  78. var Running = "running";
  79. /**
  80. If the coroutine is active but not running. That is, it has resumed another
  81. coroutine.
  82. **/
  83. var Normal = "normal";
  84. /**
  85. If the coroutine has finished its body function or if it has stopped with
  86. an error.
  87. **/
  88. var Dead = "dead";
  89. }