1. class CompUnit::Loader is repr('Uninstantiable') {
  2. # Load a file from source and compile it
  3. method load-source-file(IO::Path $path --> CompUnit::Handle) {
  4. # Get the compiler and compile the code, then run it
  5. # (which runs the mainline and captures UNIT).
  6. my $?FILES := $path.Str;
  7. self.load-source($path.slurp(:bin))
  8. }
  9. # Decode the specified byte buffer as source code, and compile it
  10. method load-source(Blob:D $bytes --> CompUnit::Handle:D) {
  11. my $preserve_global := nqp::ifnull(nqp::gethllsym('perl6', 'GLOBAL'), Mu);
  12. my $handle := CompUnit::Handle.new;
  13. my $*CTXSAVE := $handle;
  14. my $eval := nqp::getcomp('perl6').compile($bytes.decode);
  15. $eval();
  16. nqp::bindhllsym('perl6', 'GLOBAL', $preserve_global);
  17. CATCH {
  18. default {
  19. nqp::bindhllsym('perl6', 'GLOBAL', $preserve_global);
  20. .throw;
  21. }
  22. }
  23. $handle
  24. }
  25. # Load a pre-compiled file
  26. proto method load-precompilation-file(|) { * }
  27. multi method load-precompilation-file(IO::Path $path --> CompUnit::Handle:D) {
  28. my $handle := CompUnit::Handle.new;
  29. my $*CTXSAVE := $handle;
  30. my %*COMPILING := nqp::hash();
  31. nqp::loadbytecode($path.Str);
  32. $handle
  33. }
  34. multi method load-precompilation-file(IO::Handle $file --> CompUnit::Handle:D) {
  35. my $handle := CompUnit::Handle.new;
  36. my $*CTXSAVE := $handle;
  37. my %*COMPILING := nqp::hash();
  38. nqp::loadbytecodefh(nqp::getattr($file, IO::Handle, '$!PIO'), $file.path.Str);
  39. $handle
  40. }
  41. # Load the specified byte buffer as if it was the contents of a
  42. # precompiled file
  43. method load-precompilation(Blob:D $bytes --> CompUnit::Handle:D) {
  44. my $handle := CompUnit::Handle.new;
  45. my $*CTXSAVE := $handle;
  46. my %*COMPILING := nqp::hash();
  47. nqp::loadbytecodebuffer($bytes);
  48. $handle
  49. }
  50. }