Revision 316633663836 () - Diff

Link to this snippet: https://friendpaste.com/53MKnWboUIKj7JwCqIQ5FW
Embed:
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
#! /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);
}