#include "driver.h"
#if __BLOCKS__
int imports_example_real(int verbose) {
int x = rand();
int y = 15;
void (^myClosure)(void);
myClosure = ^(void) { setGlobalInt(x + y); };
x++;
y++;
callVoidVoid(myClosure);
int globalValue = getGlobalInt();
int desiredValue = x + y - 2;
if (error_found("imports_example", globalValue, desiredValue, verbose)) return 1;
void (^myClosureCopy)(void) = Block_copy(myClosure);
setGlobalInt(x - 1000);
callVoidVoid(myClosureCopy);
globalValue = getGlobalInt();
if (error_found("imports_example copy", globalValue, desiredValue, verbose)) return 1;
Block_release(myClosureCopy);
return 0;
}
#endif __BLOCKS__
struct imports_example_struct {
struct Block_basic base;
const int x;
const int y;
};
void invoke_imports_example(struct imports_example_struct *aBlock) {
{
setGlobalInt(aBlock->x + aBlock->y);
}
}
int imports_example(int verbose) {
int x = rand();
int y = x + 1000;
struct imports_example_struct onStack = {
{ 0, 0, sizeof(struct imports_example_struct),
(void (*)(void *))invoke_imports_example,
},
x, y };
struct imports_example_struct *myClosure = &onStack;
x++;
y++;
callVoidVoid(myClosure);
int globalValue = getGlobalInt();
int desiredValue = x + y - 2;
if (error_found("imports_example", globalValue, desiredValue, verbose)) return 1;
struct imports_example_struct *myClosure_copy = Block_copy(myClosure);
setGlobalInt(x - 1000);
callVoidVoid(myClosure_copy);
globalValue = getGlobalInt();
if (error_found("imports_example copy", globalValue, desiredValue, verbose)) return 1;
Block_release(myClosure_copy);
return 0;
}
#if 1
#if __BLOCKS__
int imports_example2_real(int verbose) {
int x = 10;
int y = 14;
void (^myImportedClosure)(void) = ^(void) { setGlobalInt(x + y); };
void (^anotherClosure)(void) = ^(void) {
myImportedClosure(); };
anotherClosure();
int globalValue = getGlobalInt();
int desiredValue = x + y;
if (error_found("imports_example2", globalValue, desiredValue, verbose)) return 1;
void (^anotherClosureCopy)(void) = Block_copy(anotherClosure);
globalValue = getGlobalInt();
if (error_found("imports_example2 copy", globalValue, desiredValue, verbose)) return 1;
Block_release(anotherClosureCopy);
return 0;
}
#endif __BLOCKS__
struct imports_example2_struct {
struct Block_basic base;
struct imports_example_struct *myImportedClosure;
const int y;
};
void copy_imports_example2(struct imports_example2_struct *dst, struct imports_example2_struct *src) {
_Block_copy_assign(&dst->myImportedClosure, src->myImportedClosure);
}
void destroy_imports_example2(struct imports_example2_struct *aBlock) {
Block_release(aBlock->myImportedClosure);
}
void invoke_imports_example2(struct imports_example2_struct *aBlock) {
(*aBlock->myImportedClosure->base.Block_invoke)(aBlock->myImportedClosure);
}
int imports_example2(int verbose) {
int x = 10;
int y = 14;
struct imports_example_struct myClosure = {
{ 0, 0, sizeof(struct imports_example_struct),
(void (*)(void *))invoke_imports_example,
},
x, y };
struct imports_example2_struct stackLocation2 = {
{ 0, BLOCK_HAS_COPY_DISPOSE, sizeof(struct imports_example2_struct),
(void (*)(void *))invoke_imports_example2,
(void (*)(void *, void *))copy_imports_example2,
(void (*)(void *))destroy_imports_example2 },
&myClosure
};
struct imports_example2_struct *anotherClosure = &stackLocation2;
(*anotherClosure->base.Block_invoke)(anotherClosure);
int globalValue = getGlobalInt();
int desiredValue = x + y;
if (error_found("imports_example2", globalValue, desiredValue, verbose)) return 1;
struct imports_example2_struct *anotherClosureCopy = Block_copy(anotherClosure);
(*anotherClosureCopy->base.Block_invoke)(anotherClosureCopy);
globalValue = getGlobalInt();
if (error_found("imports_example2 copy", globalValue, desiredValue, verbose)) return 1;
Block_release(anotherClosureCopy);
return 0;
}
#endif