1/*
2 * Copyright (c) 2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LLVM_LICENSE_HEADER@
5 */
6
7/*
8 *  objectassign.c
9 *  testObjects
10 *
11 *  Created by Blaine Garst on 10/28/08.
12 *  Copyright 2008 __MyCompanyName__. All rights reserved.
13 */
14
15// TEST_CONFIG
16// rdar://6175959
17// This just tests that the compiler is issuing the proper helper routines
18
19#include <stdio.h>
20#include <Block_private.h>
21#include "test.h"
22
23int AssignCalled = 0;
24int DisposeCalled = 0;
25
26// local copy instead of libSystem.B.dylib copy
27void _Block_object_assign(void *destAddr __unused, const void *object __unused, const int isWeak __unused) {
28    //printf("_Block_object_assign(%p, %p, %d) called\n", destAddr, object, isWeak);
29    AssignCalled = 1;
30}
31
32void _Block_object_dispose(const void *object __unused, const int isWeak __unused) {
33    //printf("_Block_object_dispose(%p, %d) called\n", object, isWeak);
34    DisposeCalled = 1;
35}
36
37struct MyStruct {
38    long isa;
39    long field;
40};
41
42typedef struct MyStruct *__attribute__((NSObject)) MyStruct_t;
43
44int main() {
45    // create a block
46    struct MyStruct X;
47    MyStruct_t xp = (MyStruct_t)&X;
48    xp->field = 10;
49    void (^myBlock)(void) = ^{ printf("field is %ld\n", xp->field); };
50    // should be a copy helper generated with a calls to above routines
51    // Lets find out!
52    struct Block_layout *bl = (struct Block_layout *)(void *)myBlock;
53    if ((bl->flags & BLOCK_HAS_COPY_DISPOSE) != BLOCK_HAS_COPY_DISPOSE) {
54        fail("no copy dispose!!!!");
55    }
56    // call helper routines directly.  These will, in turn, we hope, call the stubs above
57    long destBuffer[256];
58    //printf("destbuffer is at %p, block at %p\n", destBuffer, (void *)bl);
59    //printf("dump is %s\n", _Block_dump(myBlock));
60    struct Block_descriptor_2 *desc2 =
61        (struct Block_descriptor_2 *)(bl->descriptor + 1);
62    desc2->copy(destBuffer, bl);
63    desc2->dispose(bl);
64    if (AssignCalled == 0) {
65        fail("did not call assign helper!");
66    }
67    if (DisposeCalled == 0) {
68        fail("did not call dispose helper");
69    }
70
71    succeed(__FILE__);
72}
73