Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'cstr'

  • Loading branch information...
commit b35631073747a051284093e00e0cfabd3f3e0360 2 parents d9250b0 + a4f37b4
@moritz moritz authored
Showing with 40 additions and 1 deletion.
  1. +22 −0 lib/NativeCall.pm6
  2. +7 −0 t/02-simple-args.c
  3. +11 −1 t/02-simple-args.t
View
22 lib/NativeCall.pm6
@@ -229,3 +229,25 @@ multi trait_mod:<is>(Parameter $p, $name, :$encoded!) is export {
multi trait_mod:<is>(Routine $p, $name, :$encoded!) is export {
$p does NativeCallEncoded[$name];
}
+
+class CStr is repr('CStr') {
+ my role Encoding[$encoding] {
+ method encoding() { $encoding }
+ }
+
+ multi method PARAMETERIZE_TYPE(Str:D $encoding) {
+ die "Unknown string encoding for native call: $encoding" if not $encoding eq any('utf8', 'utf16', 'ascii');
+ self but Encoding[$encoding];
+ }
+}
+
+role ExplicitlyManagedString {
+ has CStr $.cstr is rw;
+}
+
+multi explicitly-manage(Str $x is rw, :$encoding = 'utf8') is export {
+ $x does ExplicitlyManagedString;
+ $x.cstr = pir::repr_box_str__PsP(nqp::unbox_s($x), CStr[$encoding]);
+}
+
+# vim:ft=perl6
View
7 t/02-simple-args.c
@@ -66,4 +66,11 @@ DLLEXPORT void TakeAString(char *pass_msg)
printf("%s\n", pass_msg);
}
+static char *cached_str = NULL;
+DLLEXPORT void SetString(char *str) {
+ cached_str = str;
+}
+DLLEXPORT void PrintString() {
+ printf("%s\n", cached_str);
+}
View
12 t/02-simple-args.t
@@ -1,7 +1,7 @@
use t::CompileTestLib;
use NativeCall;
-say "1..9";
+say "1..10";
compile_test_lib('02-simple-args');
@@ -22,3 +22,13 @@ TakeAFloat(4.2e0);
# String related
sub TakeAString(Str) is native('./02-simple-args') { * }
TakeAString('ok 9 - passed a string');
+
+# Explicitly managing strings
+sub SetString(Str) is native('./02-simple-args') { * }
+sub PrintString() is native('./02-simple-args') { * }
+my $str = 'ok 10 - delayed string print';
+explicitly-manage($str);
+SetString($str);
+PrintString();
+
+# vim:ft=perl6
Please sign in to comment.
Something went wrong with that request. Please try again.