pass block from lua

398 views
Skip to first unread message

Sivansethu G

unread,
Jul 16, 2012, 9:05:30 AM7/16/12
to ipho...@googlegroups.com
Is there a way to pass block from lua as a parameter

Regards
Sethu

Corey Johnson

unread,
Jul 16, 2012, 11:37:00 AM7/16/12
to ipho...@googlegroups.com
I'm assuming you mean "pass a lua block to objective-c". In the past I've created a WaxClass takes a lua block and has a method on it to call the lua block. You can then pass that object to Objective-c.

But if you are trying to pass a lua block to an objective-c object that expects an objective-c block, that doesn't work.

Corey

Sivansethu G

unread,
Jul 20, 2012, 7:55:58 AM7/20/12
to ipho...@googlegroups.com
apologies for incomplete quary,  I have obj-c class with method asking for objc-c block as a parameter, I have created instance of this class in lua  and need to call this method. Is it possible to do so

Sethu

André Braga

unread,
Jul 20, 2012, 8:27:57 AM7/20/12
to ipho...@googlegroups.com
Not yet. You could try and write your own wrappers though.

I believe Corey will come up with a solution sooner or later, Apple is definitely moving towards creating block-only APIs.

Andy Bower

unread,
Sep 22, 2012, 6:46:19 AM9/22/12
to ipho...@googlegroups.com
Hi, I have recently been using wax as a means of calling out to ObjC from the Gideros Mobile framework. See this blog post. I too discovered that I needed to be able too pass anonymous Lua function to ObjC as blocks. I've come up with a temporary solution that works, although it would be good if Corey could comment on how to integrate it "properly".

What I've done is added a Blocks protocol to WaxFunction like this:

@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

Reply all
Reply to author
Forward
0 new messages