1. my class Supply { ... }
  2. my class Rakudo::Internals::VMBackedDecoder is repr('Decoder') {
  3. method new(str $encoding, :$translate-nl) {
  4. nqp::decoderconfigure(nqp::create(self), $encoding,
  5. $translate-nl ?? nqp::hash('translate_newlines', 1) !! nqp::null())
  6. }
  7. method add-bytes(Blob:D $bytes --> Nil) {
  8. nqp::decoderaddbytes(self, nqp::decont($bytes));
  9. }
  10. method consume-available-chars(--> Str:D) {
  11. nqp::decodertakeavailablechars(self)
  12. }
  13. method consume-all-chars(--> Str:D) {
  14. nqp::decodertakeallchars(self)
  15. }
  16. }
  17. augment class Rakudo::Internals {
  18. method BYTE_SUPPLY_DECODER(Supply:D $bin-supply, Str:D $enc, :$translate-nl) {
  19. my $norm-enc = self.NORMALIZE_ENCODING($enc);
  20. supply {
  21. my $decoder = Rakudo::Internals::VMBackedDecoder.new($norm-enc, :$translate-nl);
  22. whenever $bin-supply {
  23. $decoder.add-bytes($_);
  24. my $available = $decoder.consume-available-chars();
  25. emit $available if $available ne '';
  26. LAST {
  27. # XXX The `with` is required due to a bug where the
  28. # LAST phaser is not properly scoped if we don't get
  29. # any bytes. Since that means there's nothing to emit
  30. # anyway, we'll not worry about this case for now.
  31. #
  32. # --- or at least that was the the idea before we fixed
  33. # that bug: https://irclog.perlgeek.de/perl6/2016-12-07#i_13698178
  34. # and tried removing the `with` in 58cdfd8, but then the error
  35. # `No such method 'consume-all-chars' for invocant of type 'Any`
  36. # started popping up on Proc::Async tests, so...
  37. # there may be some other bug affecting this?
  38. with $decoder {
  39. my $rest = .consume-all-chars();
  40. emit $rest if $rest ne '';
  41. }
  42. }
  43. }
  44. }
  45. }
  46. }