1. # IterationBuffer is used when the list/iteration implementation needs a
  2. # lightweight way to store/transmit values. Replaces the use of nqp::list in
  3. # the list guts, which is an impediment to introspectability and also to
  4. # allowing the implementation of custom iterators (though in reality most
  5. # folks won't implement Iterator directly, but instead use gather/take or lazy
  6. # loops). It doesn't make Scalar containers, and only supports mutation
  7. # through implementing push and BIND-POS, and access by implementing AT-POS.
  8. # Hot-paths are free to use the nqp:: op set directly on this, and do things
  9. # outside the scope of the method API it exposes. This type is engineered for
  10. # performance over friendliness, and should not be encountered in normal use
  11. # of Perl 6. Do NOT add any checks and validation to methods in here. They
  12. # need to remain trivially inlineable for performance reasons.
  13. my class IterationBuffer {
  14. method clear(IterationBuffer:D: --> Nil) {
  15. nqp::setelems(self, 0)
  16. }
  17. multi method elems(IterationBuffer:D:) {
  18. nqp::elems(self)
  19. }
  20. multi method push(IterationBuffer:D: Mu \value) {
  21. nqp::push(self, value)
  22. }
  23. multi method AT-POS(IterationBuffer:D: int $pos) is raw {
  24. nqp::atpos(self, $pos)
  25. }
  26. multi method AT-POS(IterationBuffer:D: Int $pos) is raw {
  27. nqp::atpos(self, $pos)
  28. }
  29. proto method BIND-POS(|) { * }
  30. multi method BIND-POS(IterationBuffer:D: int $pos, Mu \value) {
  31. nqp::bindpos(self, $pos, value)
  32. }
  33. multi method BIND-POS(IterationBuffer:D: Int $pos, Mu \value) {
  34. nqp::bindpos(self, $pos, value)
  35. }
  36. # For core debugging purposes only: basically warp the IterationBuffer
  37. # into a full-fledged List and .perl that. We don't care that it will
  38. # not round-trip.
  39. multi method perl(IterationBuffer:D:) {
  40. nqp::p6bindattrinvres(nqp::create(List),List,'$!reified',self).perl
  41. }
  42. }