Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
typed exceptions from pack and unpack
  • Loading branch information
moritz committed May 27, 2012
1 parent 50d40a6 commit e7d4e87
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/core/Buf.pm
Expand Up @@ -3,6 +3,7 @@
# the Real Thing.

my class X::Buf::AsStr { ... };
my class X::Buf::Pack { ... };

my class Buf does Positional {
has Mu $!buffer;
Expand Down Expand Up @@ -38,8 +39,8 @@ my class Buf does Positional {
nqp::p6box_i(nqp::elems($!buffer));
}
method bytes(Buf:D:) { self.elems }
method chars() { die X::Buf::AsStr.new(method => 'chars') }
multi method Str() { die X::Buf::AsStr.new(method => 'Str' ) }
method chars() { X::Buf::AsStr.new(method => 'chars').throw }
multi method Str() { X::Buf::AsStr.new(method => 'Str' ).throw }


method Numeric { self.elems }
Expand Down Expand Up @@ -136,7 +137,7 @@ my class Buf does Positional {
+ (shift(@bytes) +< 0x08)
+ shift(@bytes);
}
die "Unrecognized directive $directive";
X::Buf::Pack.new(:$directive).throw;
}
}

Expand Down Expand Up @@ -257,7 +258,7 @@ multi sub pack(Str $template, *@items) {
@bytes.push: ($number +> 0x18, $number +> 0x10,
$number +> 0x08, $number) >>%>> 0x100;
}
die "Unrecognized directive $directive";
X::Buf::Pack.new(:$directive).throw;
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/core/Exception.pm
Expand Up @@ -312,6 +312,12 @@ my class X::Buf::AsStr is Exception {
"Cannot use a Buf as a string, but you called the $.method method on it";
}
}
my class X::Buf::Pack is Exception {
has $.directive;
method message() {
"Unrecognized directive '$.directive'";
}
}

my class X::Signature::Placeholder does X::Comp {
method message() {
Expand Down

0 comments on commit e7d4e87

Please sign in to comment.