Add. Allow set null to callbacks.

This commit is contained in:
Alexey Melnichuk 2018-05-02 21:00:11 +03:00
parent e5f68e09cb
commit 886f696866
2 changed files with 57 additions and 2 deletions

View File

@ -171,8 +171,6 @@ int lcurl_set_callback(lua_State *L, lcurl_callback_t *c, int i, const char *met
luaL_argcheck(L, !lua_isnoneornil(L, i), i, "no function present"); luaL_argcheck(L, !lua_isnoneornil(L, i), i, "no function present");
luaL_argcheck(L, (top < (i + 2)), i + 2, "no arguments expected"); luaL_argcheck(L, (top < (i + 2)), i + 2, "no arguments expected");
// if(top > (i + 1)) lua_settop(L, i + 1); // this for force ignore other arguments
assert((top == i)||(top == (i + 1))); assert((top == i)||(top == (i + 1)));
if(c->ud_ref != LUA_NOREF){ if(c->ud_ref != LUA_NOREF){
@ -185,6 +183,19 @@ int lcurl_set_callback(lua_State *L, lcurl_callback_t *c, int i, const char *met
c->cb_ref = LUA_NOREF; c->cb_ref = LUA_NOREF;
} }
if(lutil_is_null(L, i)){
if(top == (i + 1)){
// Do we can just ignore this?
luaL_argcheck(L,
lua_isnoneornil(L, i + 1) || lutil_is_null(L, i + 1)
,i + 1, "no context allowed when set callback to null"
);
}
lua_pop(L, top - i + 1);
return 1;
}
if(lua_gettop(L) == (i + 1)){// function + context if(lua_gettop(L) == (i + 1)){// function + context
c->ud_ref = luaL_ref(L, LCURL_LUA_REGISTRY); c->ud_ref = luaL_ref(L, LCURL_LUA_REGISTRY);
c->cb_ref = luaL_ref(L, LCURL_LUA_REGISTRY); c->cb_ref = luaL_ref(L, LCURL_LUA_REGISTRY);

View File

@ -153,9 +153,16 @@ function test_reset_write_callback()
assert_equal(c, c:setopt_writefunction(f)) assert_equal(c, c:setopt_writefunction(f))
assert_equal(c, c:setopt_writefunction(f.write, f)) assert_equal(c, c:setopt_writefunction(f.write, f))
assert_equal(c, c:setopt_writefunction(print)) assert_equal(c, c:setopt_writefunction(print))
assert_equal(c, c:setopt_writefunction(print, null))
assert_equal(c, c:setopt_writefunction(null))
assert_equal(c, c:setopt_writefunction(null, nil))
assert_equal(c, c:setopt_writefunction(null, null))
assert_error(function()c:setopt_writefunction()end) assert_error(function()c:setopt_writefunction()end)
assert_error(function()c:setopt_writefunction(nil)end) assert_error(function()c:setopt_writefunction(nil)end)
assert_error(function()c:setopt_writefunction(nil, f)end) assert_error(function()c:setopt_writefunction(nil, f)end)
assert_error(function()c:setopt_writefunction(null, {})end)
assert_error(function()c:setopt_writefunction(print, {}, nil)end)
assert_error(function()c:setopt_writefunction(print, {}, null)end)
end end
function test_write_pass_01() function test_write_pass_01()
@ -210,6 +217,38 @@ function test_write_coro()
assert_equal(co2, called) assert_equal(co2, called)
end end
function test_write_pass_null_context()
c = assert(curl.easy{
url = GET_URL;
})
local context
assert_equal(c, c:setopt_writefunction(function(ctx)
context = ctx
return true
end, null))
assert_equal(c, c:perform())
assert_equal(null, context)
end
function test_write_pass_nil_context()
c = assert(curl.easy{
url = GET_URL;
})
local context, called
assert_equal(c, c:setopt_writefunction(function(ctx)
context = ctx
called = true
return true
end, nil))
assert_equal(c, c:perform())
assert_true(called)
assert_nil(context)
end
end end
local _ENV = TEST_CASE'progress_callback' if ENABLE then local _ENV = TEST_CASE'progress_callback' if ENABLE then
@ -381,9 +420,14 @@ function test_reset_header_callback()
assert_equal(c, c:setopt_headerfunction(f)) assert_equal(c, c:setopt_headerfunction(f))
assert_equal(c, c:setopt_headerfunction(f.header, f)) assert_equal(c, c:setopt_headerfunction(f.header, f))
assert_equal(c, c:setopt_headerfunction(print)) assert_equal(c, c:setopt_headerfunction(print))
assert_equal(c, c:setopt_headerfunction(null))
assert_equal(c, c:setopt_headerfunction(null, nil))
assert_equal(c, c:setopt_headerfunction(null, null))
assert_error(function()c:setopt_headerfunction()end) assert_error(function()c:setopt_headerfunction()end)
assert_error(function()c:setopt_headerfunction(nil)end) assert_error(function()c:setopt_headerfunction(nil)end)
assert_error(function()c:setopt_headerfunction(nil, f)end) assert_error(function()c:setopt_headerfunction(nil, f)end)
assert_error(function()c:setopt_headerfunction(null, {})end)
assert_error(function()c:setopt_headerfunction(print, {}, nil)end)
end end
function test_header_pass_01() function test_header_pass_01()