1. class CompUnit::PrecompilationId {
  2. has $.id;
  3. my $cache-lock = Lock.new;
  4. my %cache;
  5. method new(Str:D $id) {
  6. $cache-lock.protect: {
  7. %cache{$id} //= 2 < $id.chars < 64 && $id ~~ /^<[A..Za..z0..9._-]>+$/
  8. ?? self.bless(:$id)
  9. !! die "Invalid precompilation id: $id"
  10. }
  11. }
  12. method Str() { $!id }
  13. method IO() { $!id.IO }
  14. method substr(|c) { $!id.substr(|c) }
  15. }
  16. role CompUnit::PrecompilationDependency {
  17. method id(--> CompUnit::PrecompilationId:D) { ... }
  18. method src(--> Str:D) { ... }
  19. method spec(--> CompUnit::DependencySpecification:D) { ... }
  20. method Str() {
  21. "$.id $.src $.spec"
  22. }
  23. method serialize(--> Str:D) { ... }
  24. method deserialize(Str, --> CompUnit::PrecompilationDependency:D) { ... }
  25. }
  26. role CompUnit::PrecompilationUnit {
  27. method id(--> CompUnit::PrecompilationId:D) { ... }
  28. method path(--> IO::Path:D) { ... }
  29. method modified(--> Instant:D) { ... }
  30. method dependencies(--> Array[CompUnit::PrecompilationDependency]) { ... }
  31. method bytecode(--> Buf:D) { ... }
  32. method checksum(--> Str:D) { ... }
  33. method bytecode-handle(--> IO::Handle:D) { ... }
  34. method close(--> Nil) { ... }
  35. }
  36. class CompUnit::PrecompilationDependency::File does CompUnit::PrecompilationDependency {
  37. has CompUnit::PrecompilationId $.id;
  38. has Str $.src;
  39. has Str $.checksum is rw;
  40. has Str $!serialized-spec;
  41. has CompUnit::DependencySpecification $.spec;
  42. method source-name() {
  43. "$.src ($.spec.short-name())"
  44. }
  45. method deserialize(Str $str) {
  46. my ($id, $src, $checksum, $spec) = $str.split("\0", 4);
  47. nqp::p6bindattrinvres(
  48. self.new(:id(CompUnit::PrecompilationId.new($id)), :$src, :$checksum),
  49. CompUnit::PrecompilationDependency::File,
  50. '$!serialized-spec',
  51. $spec,
  52. );
  53. }
  54. method spec(--> CompUnit::DependencySpecification:D) {
  55. $!spec //= $!serialized-spec
  56. ?? do {
  57. use MONKEY-SEE-NO-EVAL;
  58. EVAL $!serialized-spec;
  59. }
  60. !! Nil;
  61. }
  62. method serialize(--> Str:D) {
  63. "$.id\0$.src\0$.checksum\0{$.spec.perl}"
  64. }
  65. method Str() {
  66. "$.id $.src $.checksum $.spec"
  67. }
  68. }