Comments (4)
agree,should support for blocks
from wax.
I was just wondering how to invoke the 'NSGlobalBlock' if I make it through from Objc to Lua as a parameter. Help!
from wax.
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.
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;
@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)
- Twitter example fails due changed API
- Possible small bug in wax_xml.m?
- error in wax_sqlite_operations.m ? HOT 1
- wax 3rd-party framework & Catalog supporting issue in hybrid project HOT 2
- Synchronous calls to wax.http.request
- rake framework fail
- no dealloc in iphone SDK 5.1 HOT 1
- UIAlertView:initWithTitle_message_delegate("title", "message", nil) error HOT 1
- selector question
- Missing git semantic version tag
- how to load my byte code files when using wax.framework HOT 5
- I can't initialize NSDictionary or NSMutableDictionary in lua HOT 1
- How do I compile wax_stdlib about 64bit HOT 35
- How to using Objective-C Block HOT 8
- Wax is being maintained by Alibaba. send issue to https://github.com/alibaba/wax
- write Masonry in lua HOT 1
- HTTP with parameters (header) request how to achieve
- is it possible to patch for calling a c fucntion? HOT 1
- Wax not displaying lua errors on Swift
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wax.