fork download
  1. #!/usr/bin/perl6
  2. # your code goes here
  3.  
  4. #`(
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <time.h>
  8.  
  9. int array_w(char array[128])
  10. {
  11. srand(time(NULL));
  12. for (int i = 0;i < 128;i ++) {
  13. array[i] = "1234567890ABCDEFGHIJKLM"[rand() % 23];
  14. }
  15. array[127] = 0;
  16. return 0;
  17. }
  18.  
  19. int array_r(char array[128]) {
  20. return printf("%s\n", array);
  21. }
  22.  
  23. typedef struct Array {
  24. long flag;
  25. char array[128];
  26. } Array;
  27.  
  28. Array getArray()
  29. {
  30. Array a = { .flag = 20 };
  31.  
  32. array_w(a.array);
  33.  
  34. return a;
  35. }
  36.  
  37. int getArray_ptr(Array *a)
  38. {
  39. a->flag = 21;
  40. array_w(a->array);
  41. return a->flag;
  42. }
  43. )
  44.  
  45. use NativeCall;
  46.  
  47. constant LIB="./rw";
  48.  
  49. sub array_w(CArray[int8]) of int32 is native(LIB) { * }
  50.  
  51. sub array_r(CArray[int8]) of int32 is native(LIB) { * }
  52.  
  53. my CArray[int8] $a .= new;
  54.  
  55. $a[$_] = 0 for ^ 128;
  56.  
  57. say array_w($a);
  58.  
  59. say array_r($a);
  60.  
  61. class XArray is repr('CStruct') {
  62. has long $.flag;
  63. has CArray[int8] $.array;
  64.  
  65. submethod TWEAK() {
  66. $!array = CArray[int8].new;
  67. $!array[$_] = 0 for ^128;
  68. }
  69.  
  70. method array() {
  71. my Buf $buf .= new;
  72.  
  73. $buf[$_] = $!array[$_] for ^128;
  74. $buf.decode('utf8');
  75. }
  76. }
  77. sub getArray() of XArray is native(LIB) { * }
  78.  
  79. sub getArray_ptr(Array is rw) of int32 is native(LIB) { * }
  80.  
  81. my XArray $x = getArray();
  82.  
  83. say $x.array;
Runtime error #stdin #stdout #stderr 2.5s 181760KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Potential difficulties:
    In 'getArray_ptr' routine declaration - Not an accepted NativeCall type for parameter [1]  : Array
     --> For Numerical type, use the appropriate int32/int64/num64...
    at /home/Dp0QNW/prog.pl:81
    ------> ray is rw) of int32 is native(LIB) { * }⏏<EOL>
lock requires a concrete object with REPR ReentrantMutex
  in method setup at /usr/share/perl6/sources/24DD121B5B4774C04A7084827BFAD92199756E03 line 295
  in method CALL-ME at /usr/share/perl6/sources/24DD121B5B4774C04A7084827BFAD92199756E03 line 322
  in block <unit> at prog.pl line 57