| 1 | 2 | ▶ | use strict; use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | package Lingy::ClojureREPL; | ||||||||||||
| 3 | |||||||||||||
| 4 | 2 | ▶ | use File::Which; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | ▶ | use IO::Select; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 2 | ▶ | use IPC::Open3; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use Symbol 'gensym'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use Time::HiRes 'usleep'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 2 | ▶ | use XXX; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 2 | my $done = '3416ebc19a42578b8ebc3f59ea1806266cea4290'; | |||||||||||
| 12 | 2 | my $pid; | |||||||||||
| 13 | 2 | my ($in, $out, $err); | |||||||||||
| 14 | 2 | my ($select_out, $select_err); | |||||||||||
| 15 | |||||||||||||
| 16 | 2 | my $Y = "\e[0;33m"; | |||||||||||
| 17 | 2 | my $Z = "\e[0m"; | |||||||||||
| 18 | |||||||||||||
| 19 | 2 | my $clojure_jar = $ENV{LINGY_CLOJURE_JAR}; | |||||||||||
| 20 | 2 | my $already_searched = 0; | |||||||||||
| 21 | 2 | my $main_repl = <<'...'; | |||||||||||
| 22 | (require '[clojure.main :as main]) | ||||||||||||
| 23 | (use 'clojure.repl | ||||||||||||
| 24 | ' clojure.pprint) | ||||||||||||
| 25 | (apply main/repl [ | ||||||||||||
| 26 | :prompt #() | ||||||||||||
| 27 | :caught (fn [e] | ||||||||||||
| 28 | (let [ | ||||||||||||
| 29 | e-via (binding | ||||||||||||
| 30 | [ *data-readers* {'error identity} ] | ||||||||||||
| 31 | (let [ err-data (read-string (pr-str e))] | ||||||||||||
| 32 | (:via err-data))) | ||||||||||||
| 33 | [m1 m2 m3] e-via ] | ||||||||||||
| 34 | (if m1 (println (:message m1))) | ||||||||||||
| 35 | (if m2 (println (:message m2)))))]) | ||||||||||||
| 36 | ... | ||||||||||||
| 37 | # ... | ||||||||||||
| 38 | 2 | my $server_opt = | |||||||||||
| 39 | '-Dclojure.server.repl=' . | ||||||||||||
| 40 | '{:port 5555 :accept clojure.core.server/repl}'; | ||||||||||||
| 41 | |||||||||||||
| 42 | 0 | ▶ | sub start { | ||||||||||
|
Subroutine
start: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||
| 43 | 0 | my ($class, $newline) = @_; | |||||||||||
| 44 | 0 | ▶ | if (not $clojure_jar) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | 0 | ▶ | return if $already_searched++; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 46 | 0 | ▶ | my $file = which('clojure') or do { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 47 | 0 | print "${Y}Can't find 'clojure' on this system$Z\n"; | |||||||||||
| 48 | 0 | return; | |||||||||||
| 49 | }; | ||||||||||||
| 50 | 0 | ▶ | open my $fh, $file or | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | die "Can't open '$file' for input: $!"; | ||||||||||||
| 52 | 0 | my $text = do {local $/; <$fh>}; | |||||||||||
| 53 | 0 | ▶ | $text =~ /java -cp +(.+?\.jar)/ or do { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | 0 | print "${Y}Can't find the Clojure jar file on this system.$Z\n"; | |||||||||||
| 55 | 0 | print "${Y}Try setting the LINGY_CLOJURE_JAR env variable.$Z\n"; | |||||||||||
| 56 | 0 | return; | |||||||||||
| 57 | }; | ||||||||||||
| 58 | 0 | $clojure_jar = $1; | |||||||||||
| 59 | } | ||||||||||||
| 60 | |||||||||||||
| 61 | 0 | $pid = open3( | |||||||||||
| 62 | $in, | ||||||||||||
| 63 | $out, | ||||||||||||
| 64 | $err = gensym, | ||||||||||||
| 65 | ( | ||||||||||||
| 66 | 'java', '-cp', $clojure_jar, | ||||||||||||
| 67 | $server_opt, | ||||||||||||
| 68 | 'clojure.main', | ||||||||||||
| 69 | '-e', $main_repl, | ||||||||||||
| 70 | ) | ||||||||||||
| 71 | ); | ||||||||||||
| 72 | |||||||||||||
| 73 | 0 | ▶ | print "\n" if $newline; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | 0 | print "$Y*** Started Clojure REPL server ($pid)$Z\n\n"; | |||||||||||
| 75 | |||||||||||||
| 76 | 0 | $select_out = new IO::Select(); | |||||||||||
| 77 | 0 | $select_out->add($out); | |||||||||||
| 78 | 0 | $select_err = new IO::Select(); | |||||||||||
| 79 | 0 | $select_err->add($err); | |||||||||||
| 80 | } | ||||||||||||
| 81 | |||||||||||||
| 82 | 0 | ▶ | sub rep { | ||||||||||
|
Subroutine
rep: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||
| 83 | 0 | my ($class, $input) = @_; | |||||||||||
| 84 | 0 | ▶ | $class->start(1) unless $pid; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 85 | 0 | ▶ | return unless $pid; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | |||||||||||||
| 87 | 0 | ▶ | return if $input =~ /^\s*\(\s*clojure-repl-on\s*\)\s*$/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | |||||||||||||
| 89 | 0 | print $in qq<$input\n"$done"\n>; | |||||||||||
| 90 | |||||||||||||
| 91 | 0 | my $output = ''; | |||||||||||
| 92 | 0 | my $string = ''; | |||||||||||
| 93 | 0 | my $count = 0; | |||||||||||
| 94 | 0 | my $rc = 0; | |||||||||||
| 95 | |||||||||||||
| 96 | 0 | usleep 500_000; | |||||||||||
| 97 | 0 | while (1) { | |||||||||||
| 98 | 0 | ▶ | if ($select_out->can_read(0)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 99 | 0 | sysread($out, $string, 4096); | |||||||||||
| 100 | 0 | ▶ | $output .= $string // ''; | ||||||||||
|
Condition0%0/2
$string // ""
MC/DC0%0/1
$string // ""
$string
Truth table0%0/2
A
A= $string
| |||||||||||||
| 101 | 0 | ▶ | last if $output =~ s/"$done"\n+//; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 102 | } | ||||||||||||
| 103 | 0 | ▶ | if ($select_err->can_read(0)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | 0 | sysread($err, $string, 4096); | |||||||||||
| 105 | 0 | ▶ | $output .= $string // ''; | ||||||||||
|
Condition0%0/2
$string // ""
MC/DC0%0/1
$string // ""
$string
Truth table0%0/2
A
A= $string
| |||||||||||||
| 106 | 0 | last; | |||||||||||
| 107 | } | ||||||||||||
| 108 | |||||||||||||
| 109 | 0 | ▶ | if (++$count >= 3) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 110 | 0 | $output = 'timeout'; | |||||||||||
| 111 | 0 | kill -9, $pid; | |||||||||||
| 112 | 0 | print "$Y*** Killed Clojure REPL server ($pid)$Z\n"; | |||||||||||
| 113 | 0 | undef $pid; | |||||||||||
| 114 | 0 | $rc = 255; | |||||||||||
| 115 | 0 | last; | |||||||||||
| 116 | } | ||||||||||||
| 117 | |||||||||||||
| 118 | 0 | usleep 500_000; | |||||||||||
| 119 | } | ||||||||||||
| 120 | |||||||||||||
| 121 | 0 | $output =~ s/\n*\z//; | |||||||||||
| 122 | 0 | ▶ | $output .= "\n" if length $output; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 123 | |||||||||||||
| 124 | 0 | print STDOUT "${Y}Clojure:$Z\n$output\n"; | |||||||||||
| 125 | } | ||||||||||||
| 126 | |||||||||||||
| 127 | 2 | ▶ | END { | ||||||||||
|
Subroutine
END: called CC=3 SCAR=20.3
| |||||||||||||
| 128 | 2 | ▶ | if (defined $pid) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 129 | 0 | print $in '(java.lang.System/exit 0)', "\n"; | |||||||||||
| 130 | 0 | waitpid( $pid, 0 ); | |||||||||||
| 131 | 0 | my $rc = $? >> 8; | |||||||||||
| 132 | 0 | print "$Y*** Stopped Clojure REPL server ($pid)$Z\n"; | |||||||||||
| 133 | 0 | ▶ | exit $rc unless $rc == 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 134 | } | ||||||||||||
| 135 | } | ||||||||||||
| 136 | |||||||||||||
| 137 | 2 | 1; | |||||||||||