| 1 | package Devel::IPerl::Kernel::Callback::REPL; | ||
| 2 | $Devel::IPerl::Kernel::Callback::REPL::VERSION = '0.012'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Moo; | ||
| 7 | use Devel::IPerl::Message::Helper; | ||
| 8 | #use Devel::IPerl::Kernel::Backend::DevelREPL; | ||
| 9 | use Devel::IPerl::Kernel::Backend::Reply; | ||
| 10 | use Try::Tiny; | ||
| 11 | use Devel::IPerl::DisplayableHandler; | ||
| 12 | use namespace::autoclean; | ||
| 13 | use List::AllUtils; | ||
| 14 | use Scalar::Util qw(blessed); | ||
| 15 | |||
| 16 | use constant REPL_OUTPUT_TOO_LONG => 1024; | ||
| 17 | |||
| 18 | use Log::Any qw($log); | ||
| 19 | |||
| 20 | extends qw(Devel::IPerl::Kernel::Callback); | ||
| 21 | |||
| 22 | with qw(Devel::IPerl::Kernel::Callback::Role::REPL); | ||
| 23 | |||
| 24 | #has backend => ( is => 'rw', default => sub { Devel::IPerl::Kernel::Backend::DevelREPL->new } ); | ||
| 25 | has backend => ( is => 'rw', default => sub { | ||
| 26 | my $backend = Devel::IPerl::Kernel::Backend::Reply->new; | ||
| 27 | $backend->run_line( q|use IPerl; IPerl->load_plugin('Default')| ); | ||
| 28 | $backend; | ||
| 29 | } ); | ||
| 30 | |||
| 31 | sub execute { | ||
| 32 | my ($self, $kernel, $msg) = @_; | ||
| 33 | |||
| 34 | # This is so that the current state is available for IPerl->display() [ see display_data() ]. | ||
| 35 | local $IPerl::REPL = $self; | ||
| 36 | local $IPerl::current_msg = $msg; | ||
| 37 | local $IPerl::current_kernel = $kernel; | ||
| 38 | |||
| 39 | ### Run code | ||
| 40 | ### Store execution status | ||
| 41 | ### e.g., any errors, exceptions | ||
| 42 | my $exec_result = $self->backend->run_line( $msg->content->{code} ); | ||
| 43 | |||
| 44 | ### Send back stdout/stderr | ||
| 45 | # send display_data / execute_result | ||
| 46 | if( defined $exec_result->stdout && length $exec_result->stdout ) { | ||
| 47 | my $stream_stdout = $msg->new_reply_to( | ||
| 48 | msg_type => 'stream', | ||
| 49 | content => { name => 'stdout', text => $exec_result->stdout, } | ||
| 50 | ); | ||
| 51 | $kernel->send_message( $kernel->iopub, $stream_stdout ); | ||
| 52 | } | ||
| 53 | |||
| 54 | if( defined $exec_result->stderr && length $exec_result->stderr ) { | ||
| 55 | my $stream_stderr = $msg->new_reply_to( | ||
| 56 | msg_type => 'stream', | ||
| 57 | content => { name => 'stderr', text => $exec_result->stderr, } | ||
| 58 | ); | ||
| 59 | $kernel->send_message( $kernel->iopub, $stream_stderr ); | ||
| 60 | } | ||
| 61 | |||
| 62 | # REPL output | ||
| 63 | # NOTE using execute_result | ||
| 64 | my $results_all_displayable = List::AllUtils::all | ||
| 65 | { blessed($_) && $_->can('iperl_data_representations') } | ||
| 66 | @{ $exec_result->results // [] }; | ||
| 67 | if( defined $exec_result->last_output | ||
| 68 | && !$results_all_displayable | ||
| 69 | && length $exec_result->last_output > 0 | ||
| 70 | && length $exec_result->last_output < REPL_OUTPUT_TOO_LONG ) { | ||
| 71 | |||
| 72 | my $output_str = $exec_result->last_output; | ||
| 73 | chomp($output_str); | ||
| 74 | my $repl_output = $msg->new_reply_to( | ||
| 75 | msg_type => 'execute_result', | ||
| 76 | content => { | ||
| 77 | execution_count => $self->execution_count, | ||
| 78 | data => { | ||
| 79 | 'text/plain' => $output_str, | ||
| 80 | }, | ||
| 81 | metadata => {}, | ||
| 82 | } | ||
| 83 | ); | ||
| 84 | $kernel->send_message( $kernel->iopub, $repl_output ); | ||
| 85 | } | ||
| 86 | |||
| 87 | ### Send back data representations | ||
| 88 | $self->display_data_from_exec_result( $kernel, $msg, $exec_result ); | ||
| 89 | |||
| 90 | ### Send back warnings | ||
| 91 | if( defined $exec_result->warning ) { | ||
| 92 | # send back exception | ||
| 93 | my $err = $msg->new_reply_to( | ||
| 94 | msg_type => 'error', | ||
| 95 | content => { | ||
| 96 | ename => $exec_result->warning_name, | ||
| 97 | evalue => "@{[ $exec_result->warning_value ]}", # must be string | ||
| 98 | traceback => $exec_result->warning_traceback, | ||
| 99 | } | ||
| 100 | ); | ||
| 101 | $kernel->send_message( $kernel->iopub, $err ); | ||
| 102 | } | ||
| 103 | |||
| 104 | ### Send back errors | ||
| 105 | if( defined $exec_result->error ) { | ||
| 106 | # send back exception | ||
| 107 | my $err = $msg->new_reply_to( | ||
| 108 | msg_type => 'error', | ||
| 109 | content => { | ||
| 110 | ename => $exec_result->exception_name, | ||
| 111 | evalue => "@{[ $exec_result->exception_value ]}", # must be string | ||
| 112 | traceback => $exec_result->exception_traceback, | ||
| 113 | } | ||
| 114 | ); | ||
| 115 | $kernel->send_message( $kernel->iopub, $err ); | ||
| 116 | } | ||
| 117 | |||
| 118 | $exec_result; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub display_data { | ||
| 122 | my ($self, @data) = @_; | ||
| 123 | my $msg = $IPerl::current_msg; | ||
| 124 | my $kernel = $IPerl::current_kernel; | ||
| 125 | for my $data (@data) { | ||
| 126 | my $data_formats = Devel::IPerl::DisplayableHandler->display_data_format_handler( $data ); | ||
| 127 | if( defined $data_formats ) { | ||
| 128 | my $display_data_msg = $msg->new_reply_to( | ||
| 129 | msg_type => 'display_data', | ||
| 130 | content => { | ||
| 131 | data => $data_formats, | ||
| 132 | metadata => {}, | ||
| 133 | }, | ||
| 134 | metadata => {}, | ||
| 135 | ); | ||
| 136 | $kernel->send_message( $kernel->iopub, $display_data_msg ); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | sub display_data_from_exec_result { | ||
| 142 | my ($self, $kernel, $msg, $exec_result) = @_; | ||
| 143 | $self->display_data( @{ $exec_result->results || [] } ); | ||
| 144 | } | ||
| 145 | |||
| 146 | |||
| 147 | sub msg_execute_request { | ||
| 148 | my ($self, $kernel, $msg ) = @_; | ||
| 149 | |||
| 150 | ### send kernel status : busy | ||
| 151 | my $status_busy = Devel::IPerl::Message::Helper->kernel_status( $msg, 'busy' ); | ||
| 152 | $log->tracef('send kernel status: %s', 'busy'); | ||
| 153 | $kernel->send_message( $kernel->iopub, $status_busy ); | ||
| 154 | |||
| 155 | ### Send back execution status | ||
| 156 | my $exec_result = $self->execute( $kernel, $msg ); | ||
| 157 | $self->execute_reply( $kernel, $msg, $exec_result ); | ||
| 158 | |||
| 159 | ### send kernel status : idle | ||
| 160 | my $status_idle = Devel::IPerl::Message::Helper->kernel_status( $msg, 'idle' ); | ||
| 161 | $log->tracef('send kernel status: %s', 'idle'); | ||
| 162 | $kernel->send_message( $kernel->iopub, $status_idle ); | ||
| 163 | } | ||
| 164 | |||
| 165 | sub execute_reply { | ||
| 166 | my ($self, $kernel, $msg, $exec_result) = @_; | ||
| 167 | $log->tracef('send back execution result: %s', $exec_result); | ||
| 168 | my %extra_fields; | ||
| 169 | if( $exec_result->is_status_ok ) { | ||
| 170 | %extra_fields = ( | ||
| 171 | payload => [], | ||
| 172 | user_variables => {}, | ||
| 173 | user_expressions => {}, | ||
| 174 | ); | ||
| 175 | } elsif( $exec_result->is_status_error ) { | ||
| 176 | my @ename; | ||
| 177 | my @evalue; | ||
| 178 | my @traceback; | ||
| 179 | if( $exec_result->warning_name ) { | ||
| 180 | push @ename, $exec_result->warning_name; | ||
| 181 | push @evalue, $exec_result->warning_value; | ||
| 182 | push @traceback, @{ $exec_result->warning_traceback }; | ||
| 183 | } | ||
| 184 | if( $exec_result->error ) { | ||
| 185 | push @ename, $exec_result->exception_name; | ||
| 186 | push @evalue, $exec_result->exception_value; | ||
| 187 | push @traceback, @{ $exec_result->exception_traceback }; | ||
| 188 | } | ||
| 189 | %extra_fields = ( | ||
| 190 | ename => (join " ", @ename), | ||
| 191 | evalue => (join "\n", @evalue), # must be string | ||
| 192 | traceback => \@traceback, | ||
| 193 | ); | ||
| 194 | } | ||
| 195 | my $execute_reply = $msg->new_reply_to( | ||
| 196 | msg_type => 'execute_reply', | ||
| 197 | content => { | ||
| 198 | status => $exec_result->status, | ||
| 199 | execution_count => $self->execution_count, | ||
| 200 | %extra_fields, | ||
| 201 | } | ||
| 202 | ); | ||
| 203 | $kernel->send_message( $kernel->shell, $execute_reply ); | ||
| 204 | } | ||
| 205 | |||
| 206 | sub msg_complete_request { | ||
| 207 | my ($self, $kernel, $msg, $socket ) = @_; | ||
| 208 | |||
| 209 | ### send kernel status : busy | ||
| 210 | my $status_busy = Devel::IPerl::Message::Helper->kernel_status( $msg, 'busy' ); | ||
| 211 | $kernel->send_message( $kernel->iopub, $status_busy ); | ||
| 212 | |||
| 213 | my $code = $msg->content->{code}; | ||
| 214 | my $cursor_pos = $msg->content->{cursor_pos}; | ||
| 215 | |||
| 216 | my @matches = (); | ||
| 217 | my $metadata = {}; | ||
| 218 | my ($status, $cursor_start, $cursor_end); | ||
| 219 | my $matched_text = ""; | ||
| 220 | if( $self->backend->can('completion') ) { | ||
| 221 | try { | ||
| 222 | @matches = $self->backend->completion( $code, $cursor_pos ); | ||
| 223 | $cursor_start = $cursor_pos; | ||
| 224 | my $len = 0; | ||
| 225 | if( @matches ) { | ||
| 226 | my $line_end = substr $msg->content->{code}, 0, $cursor_pos; | ||
| 227 | my $first_match = $matches[0]; | ||
| 228 | my $first_match_len = length $first_match; | ||
| 229 | for my $z ( reverse 0..$first_match_len ) { | ||
| 230 | my $suffix = substr $line_end, -$z; | ||
| 231 | my $prefix = substr $first_match, 0, $z; | ||
| 232 | if( $suffix eq $prefix ) { | ||
| 233 | $cursor_start = $cursor_pos - $z; | ||
| 234 | $len = $z; | ||
| 235 | last; | ||
| 236 | } | ||
| 237 | } | ||
| 238 | $matched_text = substr($msg->content->{code}, $cursor_start, $len) | ||
| 239 | } | ||
| 240 | $cursor_end = $cursor_pos; | ||
| 241 | $status = 'ok'; | ||
| 242 | } catch { | ||
| 243 | $status = $_; | ||
| 244 | }; | ||
| 245 | } else { | ||
| 246 | $cursor_start = $cursor_pos; | ||
| 247 | $cursor_end = $cursor_pos; | ||
| 248 | $matched_text = ""; | ||
| 249 | $status = 'ok'; | ||
| 250 | } | ||
| 251 | my $content = { | ||
| 252 | status => $status, | ||
| 253 | cursor_start => $cursor_start, | ||
| 254 | cursor_end => $cursor_end, | ||
| 255 | metadata => $metadata, | ||
| 256 | #matched_text => $matched_text, | ||
| 257 | matches => \@matches, | ||
| 258 | }; | ||
| 259 | my $complete_reply = $msg->new_reply_to( | ||
| 260 | msg_type => 'complete_reply', | ||
| 261 | content => $content, | ||
| 262 | ); | ||
| 263 | #use DDP; p $complete_reply; | ||
| 264 | $kernel->send_message( $kernel->shell, $complete_reply ); | ||
| 265 | |||
| 266 | ### send kernel status : idle | ||
| 267 | my $status_idle = Devel::IPerl::Message::Helper->kernel_status( $msg, 'idle' ); | ||
| 268 | $kernel->send_message( $kernel->iopub, $status_idle ); | ||
| 269 | } | ||
| 270 | |||
| 271 | sub msg_is_complete_request { | ||
| 272 | my ($self, $kernel, $msg, $socket ) = @_; | ||
| 273 | |||
| 274 | ### send kernel status : busy | ||
| 275 | my $status_busy = Devel::IPerl::Message::Helper->kernel_status( $msg, 'busy' ); | ||
| 276 | $kernel->send_message( $kernel->iopub, $status_busy ); | ||
| 277 | |||
| 278 | my $content; | ||
| 279 | if ($self->backend->is_complete( $msg->{content}{code} )) { | ||
| 280 | $content = { | ||
| 281 | status => 'complete', | ||
| 282 | }; | ||
| 283 | } else { | ||
| 284 | $content = { | ||
| 285 | status => 'incomplete', | ||
| 286 | indent => '', | ||
| 287 | }; | ||
| 288 | } | ||
| 289 | my $is_complete_reply = $msg->new_reply_to( | ||
| 290 | msg_type => 'is_complete_reply', | ||
| 291 | content => $content, | ||
| 292 | ); | ||
| 293 | $kernel->send_message( $kernel->shell, $is_complete_reply ); | ||
| 294 | |||
| 295 | ### send kernel status : idle | ||
| 296 | my $status_idle = Devel::IPerl::Message::Helper->kernel_status( $msg, 'idle' ); | ||
| 297 | $kernel->send_message( $kernel->iopub, $status_idle ); | ||
| 298 | } | ||
| 299 | |||
| 300 | |||
| 301 | 1; | ||
| 302 | |||
| 303 | __END__ |