Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistency between NULL CPointer behavior jvm vs moar #3765

Closed
p6rt opened this issue Mar 31, 2015 · 4 comments
Closed

Inconsistency between NULL CPointer behavior jvm vs moar #3765

p6rt opened this issue Mar 31, 2015 · 4 comments

Comments

@p6rt
Copy link

p6rt commented Mar 31, 2015

Migrated from rt.perl.org#124214 (status was 'resolved')

Searchable as RT124214$

@p6rt
Copy link
Author

p6rt commented Mar 31, 2015

From @skids

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)

Inline Patch
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;

@p6rt
Copy link
Author

p6rt commented Mar 31, 2015

From @FROGGS

The jvm got aligned to moar in​: Raku/nqp@0ccec451ed

@p6rt
Copy link
Author

p6rt commented Mar 31, 2015

@FROGGS - Status changed from 'new' to 'resolved'

@p6rt p6rt closed this as completed Mar 31, 2015
@p6rt
Copy link
Author

p6rt commented Mar 31, 2015

From @FROGGS

Tests​: rakudo/rakudo@76ba0a26e3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant