Skip to content

Commit

Permalink
Refactor significantly:
Browse files Browse the repository at this point in the history
- return "warn" sub to its previous state
- put .WARN logic into the control exception handler (this was the
  place I was looking for earlier, it could use a different name)
- the .WARN methods are now also expected to receive a Backtrace
  object as the second positional argument
- this restores the capability of having a CONTROL block catching
  warnings if $*WARNINGS is something else than CX::Warn
  • Loading branch information
lizmat committed Feb 20, 2023
1 parent 467ce81 commit d8e177d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
30 changes: 13 additions & 17 deletions src/core.c/Exception.pm6
Expand Up @@ -452,33 +452,29 @@ my class CX::Warn does X::Control {
nqp::p6box_n(nqp::div_In($nu,$de))
}

method WARN(str $message --> Nil) is hidden-from-backtrace {
my $ex := nqp::newexception();
nqp::setmessage($ex,$message);
nqp::setextype($ex,nqp::const::CONTROL_WARN);
nqp::throw($ex);
method WARN(str $message, $bt --> Nil) is hidden-from-backtrace {
note "$message\n$bt.first-none-setting-line()";
}
}
my class CX::Warn::Quietly {
method WARN($ --> Nil) is hidden-from-backtrace { }
method WARN($, $ --> Nil) is hidden-from-backtrace { }
}
my class CX::Warn::Fatal {
method WARN(str $message --> Nil) is hidden-from-backtrace {
method WARN(str $message, $bt --> Nil) is hidden-from-backtrace {
X::AdHoc.new(payload => $message).throw;
}
}
my class CX::Warn::Verbose {
method WARN(str $message --> Nil) is hidden-from-backtrace {
my $bt := Backtrace.new;
note $message ~ "\n" ~ Backtrace.new.Str;
method WARN( str $message, $bt --> Nil) is hidden-from-backtrace {
note "$message\n$bt";
}
}
my class CX::Warn::Collect {
my $lock := Lock.new;
my $messages := nqp::hash;

method WARN(str $message --> Nil) is hidden-from-backtrace {
my str $key = $message ~ "\n" ~ Backtrace.new.Str;
method WARN(str $message, $bt --> Nil) is hidden-from-backtrace {
my str $key = "$message\n$bt";
$lock.protect: {
nqp::bindkey($messages,$key,
nqp::add_i(nqp::ifnull(nqp::atkey($messages,$key),0),1)
Expand Down Expand Up @@ -641,15 +637,15 @@ do {
nqp::if(
nqp::iseq_i($type,nqp::const::CONTROL_WARN),
nqp::stmts(
(my Mu $err := $*ERR),
(my str $msg = nqp::getmessage($ex)),
$err.say(nqp::if(nqp::chars($msg),$msg,"Warning")),
$err.print($backtrace.first-none-setting-line),
(nqp::istype((my $class := $*WARNINGS),Failure)
?? CX::Warn
!! $class
).WARN(nqp::getmessage($ex) || "Warning", $backtrace),
nqp::resume($ex)
)
);

my $label = $type +& nqp::const::CONTROL_LABELED ?? "labeled " !! "";
my $label := $type +& nqp::const::CONTROL_LABELED ?? "labeled " !! "";
if $type +& nqp::const::CONTROL_LAST {
X::ControlFlow.new(illegal => "{$label}last", enclosing => 'loop construct', :$backtrace).throw;
}
Expand Down
9 changes: 5 additions & 4 deletions src/core.c/control.pm6
Expand Up @@ -263,10 +263,11 @@ multi sub die(|cap ( *@msg ) --> Nil) {

proto sub warn(|) {*}
multi sub warn(*@msg --> 0) {
(nqp::istype((my $class := $*WARNINGS),Failure)
?? CX::Warn
!! $class
).WARN(@msg.join || "Warning: something's wrong")
my str $message = @msg.join || "Warning: something's wrong";
my $ex := nqp::newexception();
nqp::setmessage($ex,$message);
nqp::setextype($ex,nqp::const::CONTROL_WARN);
nqp::throw($ex);
}
multi sub warn(Junction:D $j) { $j.THREAD: &warn }

Expand Down

0 comments on commit d8e177d

Please sign in to comment.