Coder Social home page Coder Social logo

Comments (4)

iamdaiyuan avatar iamdaiyuan commented on August 23, 2024

agree,should support for blocks

from wax.

mysteriouss avatar mysteriouss commented on August 23, 2024

I was just wondering how to invoke the 'NSGlobalBlock' if I make it through from Objc to Lua as a parameter. Help!

from wax.

probablycorey avatar probablycorey commented on August 23, 2024

I'm not actively maintaining Wax anymore, so I'm not sure if that is possible. But I have a hunch that it is.

from wax.

mysteriouss avatar mysteriouss commented on August 23, 2024

void *wax_copyToObjc(lua_State *L, const char *typeDescription, int stackIndex, int *outsize)
there is a prob. about this function, I think, that stops the NSStackBlock from copying from Lua to Objc.
So I gave up passing blocks into Lua and made a class named 'NSBlockInvacation'.
@interface NSBlockInvocation : NSObject
@Property (nonatomic,copy) void(^block)();
+(NSBlockInvocation *)invocationWithBlock:(void(^)())ablock;
-(void)invoke;
-(void)invoke:(id)anObject;
-(void)invoke:(id)anObject :(id)anotherObject;
@EnD
@implementation NSBlockInvocation
+(NSBlockInvocation *)invocationWithBlock:(void(^)())ablock{
NSBlockInvocation * invo = [[[NSBlockInvocation alloc] init] autorelease];
[invo setBlock:ablock];
return invo;
}
-(void)invoke{
self.block();
}
-(void)invoke:(id)anObject{
self.block(anObject);
}
-(void)invoke:(id)anObject :(id)anotherObject{
self.block(anObject,anotherObject);
}
-(void)dealloc{
[_block release];
[super dealloc];
}
//immobSDK.h
@protocol immobSDK
@optional
+(void)getScore:(NSString *)adunitID :(NSDictionary *)info :(NSBlockInvocation *)handler;
@EnD
@interface immobSDK ()
+(void)getScore:(NSString *)adunitID
accountInfo:(NSDictionary *)info
completionHandler:(void (^)(id result)) handler;
@EnD

//immobSDK.m
@implementation immobSDK
+(void)getScore:(NSString *)adunitID
accountInfo:(NSDictionary *)info
completionHandler:(void (^)(id result)) handler{
if ([self instancesRespondToSelector:@selector(getScore:::)]) {
[self getScore:adunitID:info:[NSBlockInvocation invocationWithBlock:handler]];
}
}
@EnD
--immobSDK.lua
function immobSDK:getScore__(adu,aid,handler)
url = self:QUERYSCORE(adu,aid)
wax.http.request{url, format = "json",callback = function(body,response)
--puts(url)
--puts(body)
if response == nil or body == nil then
handler:invoke(nil)
return
end
if response:statusCode() == 200 then
handler:invoke(body) end
end}
end

_older__no use__see above*_***************************
This is my stupid way for Objc to call the method declared in the Lua code.

typedef void(^completionHandler)(id); //only used for this kind of NSGlobalBlock
int wax_call(id aObserver, SEL aSelector, const char *aName, id aObject,completionHandler handler);

int wax_call(id aObserver, SEL aSelector, const char *aName,id aObject, completionHandler handler){

if ([aObserver respondsToSelector:aSelector]) {
    NSString * name = [NSString stringWithUTF8String:aName];
    [[NSNotificationCenter defaultCenter] addObserver:aObserver selector:aSelector name:name object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:name object:aObject];
    [[NSNotificationCenter defaultCenter] removeObserver:aObserver name:name object:nil];
    if (handler) {
        __block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:name object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
            handler([note object]);
            [[NSNotificationCenter defaultCenter] removeObserver:observer name:name object:nil];
        }];
    }
    return 1;
}
return 0;

}

And the Lua code should post the notification to return.
NSNotificationCenter:defaultCenter() :postNotificationName_object(notification:name(),aObjct);

e.g.

[immobSDK getScore:@"123456" :^(id score) {
NSLog(@"%@",score);
}];

@interface immobSDK : NSObject

+(void)getScore:(NSString *)adunitID :(void (^)(id score)) handler;

@EnD

@implementation immobSDK
+(void)initialize{
wax_start("immobSDK.lua",luaopen_wax_http,nil);
}

+(void)getScore:(NSString *)adunitID :(void (^)(id score)) handler{
wax_call(self, @selector(Score:),FUNCTION,adunitID,handler);
}
@EnD

immobSDK.lua

function immobSDK:Score(adunitID)
--puts(adunitID:object())
wax.http.request{"http://www.google.com", callback = function(body,response)
if response:statusCode() == 200 then
NSNotificationCenter:defaultCenter()
:postNotificationName_object(adunitID:name(),200);
end
end}
end

Better ideas? Thank U

I've tried the NSStackBlock,and it cannnot be forced to NSGlobalBlock,otherwise there will be a crash.

from wax.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.