Skip to content

Commit

Permalink
Make the REPL catch CONTROL-c when executing
Browse files Browse the repository at this point in the history
Pressing CONTROL-c while something is executing, will not wait for
the result of that code to appear.  The code *will* however continue
to execute at the moment, because we do not have a way to force an
exception to be thrown in another thread.

Fixes RT #128900  *and* RT #128595
  • Loading branch information
lizmat committed Aug 12, 2016
1 parent 4cef9ee commit be7ce04
Showing 1 changed file with 44 additions and 11 deletions.
55 changes: 44 additions & 11 deletions src/core/REPL.pm
Expand Up @@ -318,6 +318,18 @@ do {
my $prompt = self.interactive_prompt;
my $code = "";

my $ctrl-c = Promise.new;
my $running = False;
signal(SIGINT).tap: {
if $running {
$running = False;
$ctrl-c.keep("\nAborted")
}
else {
exit 0;
}
}

REPL: loop {

my $newcode = self.repl-read(~$prompt);
Expand All @@ -335,13 +347,36 @@ do {
next;
}

sub reset(--> Nil) {
$code = '';
$prompt = self.interactive_prompt;
}

my $*CTXSAVE := self;
my $*MAIN_CTX;

my $output := self.repl-eval(
$code,
:outer_ctx($!save_ctx),
|%adverbs);
my $output;

await Promise.anyof(
start {
ENTER $running = True;
LEAVE $running = False;
CATCH {
say $_;
reset;
}
$output :=
self.repl-eval($code,:outer_ctx($!save_ctx),|%adverbs);
},
$ctrl-c
);

if $ctrl-c.status == Kept {
# XXX somehow kill the running thread
say $ctrl-c.result;
$ctrl-c = Promise.new;
reset;
next;
}

if self.input-incomplete($output) {
$prompt = '* ';
Expand All @@ -352,18 +387,16 @@ do {
$!save_ctx := $*MAIN_CTX;
}

$code = "";
$prompt = self.interactive_prompt;
reset;

# Only print the result if there wasn't some other output
if $initial_out_position == $*OUT.tell {
self.repl-print($output);
self.repl-print($output);
}

CATCH {
say $_;
$code = '';
$prompt = self.interactive_prompt;
reset;
next REPL;
}
}
Expand Down

0 comments on commit be7ce04

Please sign in to comment.