coro.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Declarations for the coroutine type.
  2. This file is part of khipu.
  3. khipu is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #ifndef __KP_CORO__
  14. #define __KP_CORO__ 1
  15. #include "interp.hpp"
  16. KP_DECLS_BEGIN
  17. struct coroutine : public varobj
  18. {
  19. static const int code = typecode::CORO;
  20. static coroutine* alloc_raw ();
  21. static result<object> make (interpreter *interp, uint32_t bp);
  22. object value;
  23. object argv;
  24. object dbinds;
  25. int ip_offset;
  26. int sp_diff;
  27. uint32_t frame;
  28. uint32_t exc_off;
  29. bool valid_p () const
  30. {
  31. return (this->ip_offset >= 0);
  32. }
  33. void mark_invalid ()
  34. {
  35. this->ip_offset = -1;
  36. }
  37. };
  38. inline bool coro_p (object obj)
  39. {
  40. return (itype (obj) == typecode::CORO);
  41. }
  42. inline coroutine* as_coro (object obj)
  43. {
  44. return ((coroutine *)unmask (obj));
  45. }
  46. KP_EXPORT object alloc_coroutine (interpreter *interp);
  47. // Resume execution of a coroutine.
  48. KP_EXPORT result<object> call_coroutine (interpreter *interp,
  49. coroutine *crp, object value);
  50. // Entry point for the above call.
  51. KP_EXPORT result<object> coro_next (interpreter *interp,
  52. object *argv, int argc);
  53. struct stream;
  54. struct io_info;
  55. struct pack_info;
  56. // Pack a coroutine into a stream.
  57. KP_EXPORT result<int64_t> pack_G (interpreter *interp,
  58. stream *strm, object obj, pack_info& info);
  59. // Unpack a coroutine from a stream.
  60. KP_EXPORT result<object> unpack_G (interpreter *interp,
  61. stream *strm, pack_info& info, bool save);
  62. KP_DECLS_END
  63. #endif