# New Ticket Created by  "Brian S. Julin" 
# Please include the string:  [perl #124214]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org/Ticket/Display.html?id=124214 >


Pointer-based classes are behaving differently on moar and jvm:

(03/30/2015 10:49:07 PM) skids: r: use NativeCall; sub strstr(Str $n, Str $c) 
returns OpaquePointer is native { } ; my $d = strstr("c","aaa"); $d.say; 
$d.defined.say;
(10:49:08 PM) camelia: rakudo-moar 01e174: OUTPUT«(Pointer)␤False␤»
(10:49:16 PM) skids: j: use NativeCall; sub strstr(Str $n, Str $c) returns 
OpaquePointer is native { } ; my $d = strstr("c","aaa"); $d.say; $d.defined.say;
(10:49:24 PM) camelia: rakudo-jvm 01e174: OUTPUT«Pointer<NULL>␤True␤»
(11:04:32 PM) skids: j: use NativeCall; class f is repr('CPointer') { }; sub 
strstr(Str $n, Str $c) returns f is native { } ; my $d = strstr("c","aaa"); 
$d.say; $d.defined.say;
(11:04:37 PM) camelia: rakudo-jvm 6186f0: OUTPUT«f.new␤True␤»
(11:05:29 PM) skids: m: use NativeCall; class f is repr('CPointer') { }; sub 
strstr(Str $n, Str $c) returns f is native { } ; my $d = strstr("c","aaa"); 
$d.say; $d.defined.say;
(11:05:30 PM) camelia: rakudo-moar 6186f0: OUTPUT«(f)␤False␤»

This has broken previously-working assumptions about CPointer-based classes on 
JVM.

Since CPointer type objects passed to native functions cause a NULL pointer
to be passed, it would be consistent for the reverse to work as well,
and that would be consistent with the C/Perl failure case return value idioms.

If there is also a use case for a pointer class that is .defined when NULL, 
there
should be a way to distinguish between them and the idiomatic behavior.

This patch gets you halfway to tests, once the correct behavior is decided.
(Also fixes a minor typo)

diff --git a/t/04-nativecall/04-pointers.c b/t/04-nativecall/04-pointers.c
index 0b617c6..230515a 100644
--- a/t/04-nativecall/04-pointers.c
+++ b/t/04-nativecall/04-pointers.c
@@ -18,3 +18,13 @@ DLLEXPORT int CompareSomePointer(void *ptr)
     int x = strcmp("Got passed back the pointer I returned", ptr) == 0;
     return x;
 }
+
+DLLEXPORT void * ReturnNullPointer()
+{
+    return NULL;
+}
+
+DLLEXPORT void * ReturnNullOpaquePointer()
+{
+    return NULL;
+}
diff --git a/t/04-nativecall/04-pointers.t b/t/04-nativecall/04-pointers.t
index 0bdad20..1b1b59c 100644
--- a/t/04-nativecall/04-pointers.t
+++ b/t/04-nativecall/04-pointers.t
@@ -10,6 +10,9 @@ compile_test_lib('04-pointers');
 
 sub ReturnSomePointer()         returns Pointer is native("./04-pointers") { * 
}
 sub CompareSomePointer(Pointer) returns int32   is native("./04-pointers") { * 
}
+sub ReturnNullPointer() returns Pointer   is native("./04-pointers") { * }
+sub ReturnNullOpaquePointer() returns OpaquePointer is native("./04-pointers") 
{ * }
+
 
 my $x     = ReturnSomePointer();
 my int $a = 4321;
@@ -23,4 +26,9 @@ is Pointer.new.gist,       'Pointer<NULL>',   'Pointer.new 
gistifies to "Pointer
 is Pointer.new(0).gist,    'Pointer<NULL>',   'Pointer.new(0) gistifies to 
"Pointer<NULL>"';
 is Pointer.new(1234).gist, 'Pointer<0x4d2>',  'Pointer.new(1234) gistifies to 
"Pointer<0x4d2>"';
 is Pointer.new($a).gist,   'Pointer<0x10e1>', 'Pointer.new accepts a native 
int too';
-is Pointer.gist,           '(Pointer)',       'The Pointer type object 
gistifies ot "Pointer"';
+is Pointer.gist,           '(Pointer)',       'The Pointer type object 
gistifies to "Pointer"';
+
+ReturnNullPointer.say;
+ReturnNullOpaquePointer.say;
+ReturnNullPointer.defined.say;
+ReturnNullOpaquePointer.defined.say;

Reply via email to