/
TQBlockClosure.m
210 lines (182 loc) · 7.37 KB
/
TQBlockClosure.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#import "TQBlockClosure.h"
#import "TQRuntimePrivate.h"
#import "TQFFIType.h"
#import "TQBoxedObject.h"
#import "../../../Build/TQStubs.h"
#import <sys/mman.h>
static ffi_closure *_AllocateClosure(void **codePtr);
static void _DeallocateClosure(void *closure);
static ffi_status _PrepareClosure(ffi_closure *closure, ffi_cif *cif, void (*fun)(ffi_cif*,void*,void**,void*), void *user_data, void *codeloc);
static void _closureFunction(ffi_cif *closureCif, void *ret, void *args[], TQBlockClosure *closureObj);
static void _blockClosureFunction(ffi_cif *closureCif, void *ret, void *args[], TQBlockClosure *closureObj);
static void TQClosureBlock_copy(struct TQClosureBlockLiteral *dst, struct TQClosureBlockLiteral *src);
static void TQClosureBlock_dispose(struct TQClosureBlockLiteral *src);
struct TQClosureBlockDescriptor _BlockDescriptor = { 0, sizeof(struct TQBlockLiteral),
&TQClosureBlock_copy, &TQClosureBlock_dispose };
@implementation TQBlockClosure
@synthesize pointer=_pointer;
- (id)initWithBlock:(id)aBlock type:(const char *)aType
{
assert(*aType == _TQ_C_LAMBDA_B);
BOOL isBlock = *(aType+1) == _TQ_C_LAMBDA_BLOCK;
aType += 2;
_type = aType;
_block = [aBlock copy];
ffi_closure *closure = _AllocateClosure(&_functionPointer);
if(closure) {
const char *typeIterator = _type;
_ffiTypeObjects = [NSMutableArray new];
TQFFIType *retTypeObj = [TQFFIType typeWithEncoding:typeIterator nextType:&typeIterator];
[_ffiTypeObjects addObject:retTypeObj];
while(typeIterator && *typeIterator != _TQ_C_LAMBDA_E) {
[_ffiTypeObjects addObject:[TQFFIType typeWithEncoding:typeIterator nextType:&typeIterator]];
}
_cif = (ffi_cif *)malloc(sizeof(ffi_cif));
unsigned int nargs = [_ffiTypeObjects count] - 1;
if(isBlock)
++nargs;
_argTypes = (ffi_type**)malloc(sizeof(void*)*nargs);
int i = 0;
if(isBlock)
_argTypes[i++] = &ffi_type_pointer;
for(; i < nargs; ++i) {
_argTypes[i] = [[_ffiTypeObjects objectAtIndex:isBlock ? i : i+1] ffiType];
}
if(ffi_prep_cif(_cif, FFI_DEFAULT_ABI, nargs, [retTypeObj ffiType], _argTypes) == FFI_OK) {
void *closureFun = isBlock ? _blockClosureFunction : _closureFunction;
if(_PrepareClosure(closure, _cif, (void (*)(ffi_cif*,void*,void**,void*))closureFun, self, _functionPointer) == FFI_OK)
objc_setAssociatedObject(_block, (void*)_cif, self, OBJC_ASSOCIATION_RETAIN);
}
}
if(isBlock) {
_boxedBlock.isa = &_NSConcreteStackBlock;
_boxedBlock.flags = TQ_BLOCK_HAS_COPY_DISPOSE;
_boxedBlock.invoke = _functionPointer;
_boxedBlock.descriptor = &_BlockDescriptor;
_boxedBlock.closure = self;
}
_pointer = isBlock ? &_boxedBlock : _functionPointer;
return self;
}
- (void)dealloc
{
_DeallocateClosure(_closure);
free(_cif);
free(_argTypes);
[_ffiTypeObjects release];
[_block release];
[super dealloc];
}
@end
void _closureFunction(ffi_cif *closureCif, void *ret, void *args[], TQBlockClosure *closureObj)
{
// Construct an ffi call to the block that forwards the arguments passed to the closure
struct TQBlockLiteral *block = (struct TQBlockLiteral *)closureObj->_block;
unsigned int nargs = closureCif->nargs + 1;
ffi_type *retType = &ffi_type_pointer;
ffi_type *argTypes[nargs];
void *argPtrs[nargs];
const char *returnType = closureObj->_type;
const char *typeEncoding = TQGetSizeAndAlignment(returnType, NULL, NULL);
argTypes[0] = &ffi_type_pointer;
argPtrs[0] = (id*)█
id argValues[closureCif->nargs];
for(int i = 1; i <= closureCif->nargs; ++i) {
argTypes[i] = &ffi_type_pointer;
argValues[i-1] = [TQBoxedObject box:args[i-1] withType:typeEncoding];
argPtrs[i] = &argValues[i-1];
typeEncoding = TQGetSizeAndAlignment(typeEncoding, NULL, NULL);
}
// Call the block
ffi_cif callCif;
if(ffi_prep_cif(&callCif, FFI_DEFAULT_ABI, nargs, retType, argTypes) != FFI_OK) {
// TODO: be more graceful
TQLog(@"unable to wrap block call");
exit(1);
}
id retPtr;
void *dispatcher = TQBlockDispatchers[closureCif->nargs];
ffi_call(&callCif, FFI_FN(dispatcher), &retPtr, argPtrs);
if(*returnType == _C_ID)
*(id*)ret = retPtr;
else if(*returnType != _C_VOID)
[TQBoxedObject unbox:retPtr to:ret usingType:returnType];
}
void _blockClosureFunction(ffi_cif *closureCif, void *ret, void *args[], TQBlockClosure *closureObj)
{
// Construct an ffi call to the block that forwards the arguments passed to the closure
struct TQBlockLiteral *block = (struct TQBlockLiteral *)closureObj->_block;
unsigned int nargs = closureCif->nargs;
ffi_type *retType = &ffi_type_pointer;
ffi_type *argTypes[nargs];
void *argPtrs[nargs];
const char *returnType = closureObj->_type;
const char *typeEncoding = TQGetSizeAndAlignment(returnType, NULL, NULL);
argTypes[0] = &ffi_type_pointer;
argPtrs[0] = (id*)█
id argValues[closureCif->nargs];
for(int i = 2; i <= closureCif->nargs; ++i) {
argTypes[i-1] = &ffi_type_pointer;
argValues[i-1] = [TQBoxedObject box:args[i-1] withType:typeEncoding];
argPtrs[i-1] = &argValues[i-1];
typeEncoding = TQGetSizeAndAlignment(typeEncoding, NULL, NULL);
}
// Call the block
ffi_cif callCif;
if(ffi_prep_cif(&callCif, FFI_DEFAULT_ABI, nargs, retType, argTypes) != FFI_OK) {
// TODO: be more graceful
TQLog(@"unable to wrap block call");
exit(1);
}
id retPtr;
void *dispatcher = TQBlockDispatchers[closureCif->nargs-1];
ffi_call(&callCif, FFI_FN(dispatcher), &retPtr, argPtrs);
if(*returnType == _C_ID)
*(id*)ret = retPtr;
else if(*returnType != _C_VOID)
[TQBoxedObject unbox:retPtr to:ret usingType:returnType];
}
void TQClosureBlock_copy(struct TQClosureBlockLiteral *dst, struct TQClosureBlockLiteral *src)
{
_Block_object_assign(&dst->closure, src->closure, TQ_BLOCK_FIELD_IS_OBJECT);
}
void TQClosureBlock_dispose(struct TQClosureBlockLiteral *src)
{
_Block_object_dispose(src->closure, TQ_BLOCK_FIELD_IS_OBJECT);
}
#pragma mark - LibFFI Utilities
static ffi_closure *_AllocateClosure(void **codePtr)
{
#if USE_LIBFFI_CLOSURE_ALLOC
return ffi_closure_alloc(sizeof(ffi_closure), codePtr);
#else
ffi_closure *closure = (ffi_closure *)mmap(NULL, sizeof(ffi_closure), PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
if(closure == (void *)-1) {
perror("mmap");
return NULL;
}
*codePtr = closure;
return closure;
#endif
}
static void _DeallocateClosure(void *closure)
{
#if USE_LIBFFI_CLOSURE_ALLOC
ffi_closure_free(closure);
#else
munmap(closure, sizeof(ffi_closure));
#endif
}
static ffi_status _PrepareClosure(ffi_closure *closure, ffi_cif *cif, void (*fun)(ffi_cif*,void*,void**,void*), void *user_data, void *codeloc)
{
#if USE_LIBFFI_CLOSURE_ALLOC
return ffi_prep_closure_loc(closure, cif, fun, user_data, codeloc);
#else
ffi_status status = ffi_prep_closure(closure, cif, fun, user_data);
if(status != FFI_OK)
return status;
if(mprotect(closure, sizeof(closure), PROT_READ | PROT_EXEC) == -1)
return (ffi_status)1;
return FFI_OK;
#endif
}