1/* 2 * Copyright (c) 2010 Apple Inc. All rights reserved. 3 * 4 * @APPLE_LLVM_LICENSE_HEADER@ 5 */ 6 7/* 8 * parameters.c 9 * ClosureTest 10 * 11 * Created by Blaine Garst on 1/28/08. 12 * Copyright 2008 __MyCompanyName__. All rights reserved. 13 * 14 */ 15 16#include "driver.h" 17 18// 19// Example closure code generated for specific Blocks 20// 21 22// 23// Partially bound closure referencing const and byref args 24// 25 26#if __BLOCKS__ 27int parameters_example_real(int verbose) { 28 int desiredValue = 100; 29 void (^myClosure)(int); 30 myClosure = ^ (int param) { 31 setGlobalInt(param); 32 }; 33 myClosure(desiredValue); 34 int globalValue = getGlobalInt(); 35 if (error_found("parameters_real", globalValue, desiredValue, verbose)) return 1; 36 return 0; 37} 38 39#endif __BLOCKS__ 40 41struct parameters_example_struct { 42 struct Block_basic base; 43}; 44 45// the "thunks" compiled for the invoke entry point of the parameters_example 46 47void invoke_parameters_example(struct parameters_example_struct *aBlock, int param) { 48 { 49 setGlobalInt(param); 50 } 51} 52 53 54// The rewritten version of the code above 55 56int parameters_example(int verbose) { 57 int desiredValue = 100; 58 struct parameters_example_struct literal = { 59 { 0, 0, sizeof(struct parameters_example_struct), 60 (void (*)(void *))invoke_parameters_example, 61 }, 62 }; 63 struct parameters_example_struct *myClosure = &literal; 64 65 // get a type correct function pointer for the invocation function 66 void (*correct)(struct parameters_example_struct *, int); 67 correct = (void (*)(struct parameters_example_struct *, int))myClosure->base.Block_invoke; 68 // call the closure with itself as first arg and the parameter 100 69 correct(myClosure, desiredValue); 70 71 int globalValue = getGlobalInt(); 72 if (error_found("parameters_real", globalValue, desiredValue, verbose)) return 1; 73 return 0; 74 75} 76