@interface WaxFunction (Blocks)
- (void (^)())asVoidNiladicBlock;
- (void (^)( NSObject *))asVoidMonadicBlock;
- (void (^)( NSObject *, NSObject *))asVoidDyadicBlock;
@end
@implementation WaxFunction (Blocks)
-(void (^)())asVoidNiladicBlock {
return [[^() {
lua_State *L = wax_currentLuaState();
wax_fromInstance(L, self);
lua_call(L, 0, 0);
} copy] autorelease];
}
-(void (^)(NSObject *p))asVoidMonadicBlock {
return [[^(NSObject *param) {
lua_State *L = wax_currentLuaState();
wax_fromInstance(L, self);
wax_fromInstance(L, param);
lua_call(L, 1, 0);
} copy] autorelease];
}
-(void (^)(NSObject *p1, NSObject * p2))asVoidDyadicBlock {
return [[^(NSObject *param1, NSObject *param2) {
lua_State *L = wax_currentLuaState();
wax_fromInstance(L, self);
wax_fromInstance(L, param1);
wax_fromInstance(L, param2);
lua_call(L, 2, 0);
} copy] autorelease];
}
Now up in the Lua you can make a call passing a block handler like this (e.g. a typical GameKit call):
currentMatch:endTurnWithNextParticipant_matchData_completionHandler(nextParticipant, data,
toobjc(function(error)
if error then
self:dispatchGenericError(error)
end
end):asVoidMonadicBlock())
Alternatively, if the hander expects zero or two parameter you should use asNiladicBlock or asDyadicBlock respectively. I hope this helps.
If anyone (Corey) can suggest how this could be integrated to provide automatic coercion, without the need to call asMonadicBlock() etc, that would be most helpful.
best regards