53MKnWboUIKj7JwCqIQ5FW changeset

Changeset316633663836 (b)
ParentNone (a)
ab
0+#! /usr/bin/env perl6
0+
0+use v6;
0+
0+use NativeCall;
0+
0+class Foo {
0+    has OpaquePointer $!repr;
0+
0+    sub make_foo() returns OpaquePointer
0+        is native('libstandalone.dylib') {}
0+    sub destroy_foo(OpaquePointer)
0+        is native('libstandalone.dylib') {}
0+    sub foo_getx(OpaquePointer) returns int
0+        is native('libstandalone.dylib') {}
0+    sub foo_setx(OpaquePointer, int)
0+        is native('libstandalone.dylib') {}
0+
0+    submethod BUILD() {
0+        $!repr = make_foo();
0+    }
0+    method end() {
0+        destroy_foo($!repr);
0+    }
0+
0+    method get() { return foo_getx($!repr); }
0+    method set(Int $val) { foo_setx($!repr, $val); }
0+
0+    method x() is rw {
0+        my $foo := $!repr;
0+        return Proxy.new:
0+            FETCH => method () {
0+                # Change this to $foo instead of $!repr, and it works
0+                return foo_getx($!repr);
0+            },
0+            STORE => method ($val) {
0+                foo_setx($foo, $val);
0+            };
0+    }
0+}
0+
0+sub MAIN() {
0+    my $foo = Foo.new;
0+    say "1: ", $foo.x;
0+    $foo.x = 42;
0+    say "2: ", $foo.x;
0+    $foo.end;
0+    say "Done.";
0+}
0+
0+# vim:set ft=perl6:
0+
0+
0+
0+
0+#include <assert.h>
0+#include <stdlib.h>
0+
0+/*
0+Stand-alone test case for segfault w/ NativeCall (tested w/ Rakudo* 2012.06)
0+
0+On OS X, compile and link with:
0+    gcc -c standalone.c && libtool -dynamic -o libstandalone.dylib standalone.o
0+*/
0+
0+struct Foo {
0+    int x;
0+};
0+
0+int foo_getx(struct Foo *foo) {
0+    return foo->x;
0+}
0+
0+void foo_setx(struct Foo *foo, int x) {
0+    foo->x = x;
0+}
0+
0+struct Foo *make_foo(void) {
0+    struct Foo *foo = (struct Foo *)malloc(sizeof(struct Foo));
0+    foo->x = 0;
0+}
0+
0+void destroy_foo(struct Foo *foo) {
0+    assert(foo != NULL && "destroy_foo(NULL)");
0+
0+    free(foo);
0+}
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
--- Revision None
+++ Revision 316633663836
@@ -0,0 +1,87 @@
+#! /usr/bin/env perl6
+
+use v6;
+
+use NativeCall;
+
+class Foo {
+ has OpaquePointer $!repr;
+
+ sub make_foo() returns OpaquePointer
+ is native('libstandalone.dylib') {}
+ sub destroy_foo(OpaquePointer)
+ is native('libstandalone.dylib') {}
+ sub foo_getx(OpaquePointer) returns int
+ is native('libstandalone.dylib') {}
+ sub foo_setx(OpaquePointer, int)
+ is native('libstandalone.dylib') {}
+
+ submethod BUILD() {
+ $!repr = make_foo();
+ }
+ method end() {
+ destroy_foo($!repr);
+ }
+
+ method get() { return foo_getx($!repr); }
+ method set(Int $val) { foo_setx($!repr, $val); }
+
+ method x() is rw {
+ my $foo := $!repr;
+ return Proxy.new:
+ FETCH => method () {
+ # Change this to $foo instead of $!repr, and it works
+ return foo_getx($!repr);
+ },
+ STORE => method ($val) {
+ foo_setx($foo, $val);
+ };
+ }
+}
+
+sub MAIN() {
+ my $foo = Foo.new;
+ say "1: ", $foo.x;
+ $foo.x = 42;
+ say "2: ", $foo.x;
+ $foo.end;
+ say "Done.";
+}
+
+# vim:set ft=perl6:
+
+
+
+
+#include <assert.h>
+#include <stdlib.h>
+
+/*
+Stand-alone test case for segfault w/ NativeCall (tested w/ Rakudo* 2012.06)
+
+On OS X, compile and link with:
+ gcc -c standalone.c && libtool -dynamic -o libstandalone.dylib standalone.o
+*/
+
+struct Foo {
+ int x;
+};
+
+int foo_getx(struct Foo *foo) {
+ return foo->x;
+}
+
+void foo_setx(struct Foo *foo, int x) {
+ foo->x = x;
+}
+
+struct Foo *make_foo(void) {
+ struct Foo *foo = (struct Foo *)malloc(sizeof(struct Foo));
+ foo->x = 0;
+}
+
+void destroy_foo(struct Foo *foo) {
+ assert(foo != NULL && "destroy_foo(NULL)");
+
+ free(foo);
+}