From ff12748f86c562d3838fc3cdf2d726b5b207da89 Mon Sep 17 00:00:00 2001 From: Jonathan Stowe Date: Tue, 27 Sep 2016 23:33:11 +0100 Subject: [PATCH] fail rather than return a type object for null deref Also add a better Bool coercion for Pointer --- lib/NativeCall/Types.pm6 | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/NativeCall/Types.pm6 b/lib/NativeCall/Types.pm6 index 735829ea886..5d99447a8bb 100644 --- a/lib/NativeCall/Types.pm6 +++ b/lib/NativeCall/Types.pm6 @@ -33,7 +33,12 @@ our class Pointer is repr('CPointer') { nqp::p6box_i(nqp::unbox_i(nqp::decont(self))) } - method deref(::?CLASS:D \ptr:) { nativecast(void, ptr) } + proto method Bool() {*} + multi method Bool(::?CLASS:U: --> False) { } + multi method Bool(::?CLASS:D:) { so self.Int } + + + method deref(::?CLASS:D \ptr:) { self ?? nativecast(void, ptr) !! fail("Can't dereference a Null Pointer") } multi method gist(::?CLASS:U:) { '(' ~ self.^name ~ ')' } multi method gist(::?CLASS:D:) { @@ -50,7 +55,7 @@ our class Pointer is repr('CPointer') { my role TypedPointer[::TValue] { method of() { TValue } - method deref(::?CLASS:D \ptr:) { nativecast(TValue, ptr) } + method deref(::?CLASS:D \ptr:) { self ?? nativecast(TValue, ptr) !! fail("Can't dereference a Null Pointer"); } } method ^parameterize(Mu:U \p, Mu:U \t) { die "A typed pointer can only hold integers, numbers, strings, CStructs, CPointers or CArrays (not {t.^name})"