我想将Objective-C的http请求包装到Lua,但是我不知道如何异步调用传入的函数参数。当我在回调函数中使用lua_State
时,它是无效的。
有人可以帮忙吗?
/*
Usage in Lua:
function successHandler(result)
print('success: '..result)
end
function failureHandler(error)
print('failure: '..error)
end
httpRequest(urlString, successHandler, failureHandler)
*/
static int httpRequestProxy(lua_State *L)
{
size_t l;
const char * url_ = luaL_checklstring(L, 1, &l);
NSString *url = [NSString stringWithUTF8String:url_];
[HttpManager request:url success:^(NSString * result) {
NSLog(@">>>>>>>> lua state top: %d", lua_gettop(L)); // return 0
NSLog(@">>>>>>>> success: %@", result);
if (lua_isfunction(L, 2))
{
lua_pushstring(L, [result cStringUsingEncoding:NSUTF8StringEncoding]);
lua_call(L, 1, 0);
}
} failure:^(NSError * _Nonnull error) {
NSLog(@">>>>>>>> failure: %@", error.localizedDescription);
if (lua_isfunction(L, 3))
{
lua_pushstring(L, [error.localizedDescription cStringUsingEncoding:NSUTF8StringEncoding]);
lua_call(L, 1, 0);
}
}];
return 0;
}