1/*
2 * Copyright (c) 2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LLVM_LICENSE_HEADER@
5 */
6
7// TEST_CONFIG SDK=macosx MEM=gc
8// TEST_CFLAGS -framework Foundation
9
10#import <objc/objc-auto.h>
11#import <Foundation/Foundation.h>
12#import "test.h"
13
14int GlobalInt = 0;
15int GlobalInt2 = 0;
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20id objc_assign_weak(id value, id *location) {
21    GlobalInt = 1;
22    *location = value;
23    return value;
24}
25
26id objc_read_weak(id *location) {
27    GlobalInt2 = 1;
28    return *location;
29}
30#ifdef __cplusplus
31}
32#endif
33
34@interface Foo : NSObject {
35@public
36    void (^__weak ivar)(void);
37}
38@end
39@implementation Foo
40@end
41
42
43int main() {
44    // an object should not be retained within a stack Block
45    __block int i = 0;
46    void (^local)(void);
47    Foo *foo = [[Foo alloc] init];
48    foo->ivar = ^ {  ++i; };
49    local = foo->ivar;
50    if (GlobalInt2 != 1) {
51        fail("problem with weak read of ivar");
52    }
53
54    succeed(__FILE__);
55}
56
57