Skip to content
This repository
Browse code

Tests and support functions for explicitly managing strings.

  • Loading branch information...
commit e94f45ca4dd5df3010ecb84051980f506e3cbe6d 1 parent 8619523
Arne Skjærholt authored May 01, 2012
13  lib/NativeCall.pm6
@@ -4,6 +4,11 @@ module NativeCall;
4 4
 # representation.
5 5
 my class native_callsite is repr('NativeCall') { }
6 6
 
  7
+class CStr is export is repr('CStr') { }
  8
+role ExplicitlyManagedString {
  9
+    has CStr $.cstr is rw;
  10
+}
  11
+
7 12
 # Maps a chosen string encoding to a type recognized by the native call engine.
8 13
 sub string_encoding_to_nci_type($enc) {
9 14
     given $enc {
@@ -230,4 +235,12 @@ multi trait_mod:<is>(Routine $p, $name, :$encoded!) is export {
230 235
     $p does NativeCallEncoded[$name];
231 236
 }
232 237
 
  238
+# TODO: Encodings
  239
+multi explicitly-manage(Str $s) is export {
  240
+    $s does ExplicitlyManagedString;
  241
+    # repr_box_str
  242
+    #$s.cstr = nqp::unbox_s($s);
  243
+    $s.cstr = pir::repr_box_str__PsP(nqp::unbox_s($s), CStr);
  244
+}
  245
+
233 246
 # vim:ft=perl6
7  t/02-simple-args.c
@@ -66,4 +66,11 @@ DLLEXPORT void TakeAString(char *pass_msg)
66 66
     printf("%s\n", pass_msg);
67 67
 }
68 68
 
  69
+static char *cached_str = NULL;
  70
+DLLEXPORT void SetString(char *str) {
  71
+    cached_str = str;
  72
+}
69 73
 
  74
+DLLEXPORT void PrintString() {
  75
+    printf("%s\n", cached_str);
  76
+}
12  t/02-simple-args.t
... ...
@@ -1,7 +1,7 @@
1 1
 use t::CompileTestLib;
2 2
 use NativeCall;
3 3
 
4  
-say "1..9";
  4
+say "1..10";
5 5
 
6 6
 compile_test_lib('02-simple-args');
7 7
 
@@ -22,3 +22,13 @@ TakeAFloat(4.2e0);
22 22
 # String related
23 23
 sub TakeAString(Str) is native('./02-simple-args') { * }
24 24
 TakeAString('ok 9 - passed a string');
  25
+
  26
+# Explicitly managing strings
  27
+sub SetString(Str) is native('./02-simple-args') { * }
  28
+sub PrintString() is native('./02-simple-args') { * }
  29
+my $str = 'ok 10 - delayed string print';
  30
+explicitly-manage($str);
  31
+SetString($str);
  32
+PrintString();
  33
+
  34
+# vim:ft=perl6

0 notes on commit e94f45c

Please sign in to comment.
Something went wrong with that request. Please try again.