access to enclosing private attributes in Proxy FETCH/STORE method Revision 316633663836 (Mon Jul 16 2012 at 21:48) - Diff Link to this snippet: https://friendpaste.com/53MKnWboUIKj7JwCqIQ5FW Embed: manni perldoc borland colorful default murphy trac fruity autumn bw emacs pastie friendly Show line numbers Wrap lines 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687#! /usr/bin/env perl6use 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);}