From 886f69686645ae7a8914f5ec09ccd77b1430533f Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Wed, 2 May 2018 21:00:11 +0300 Subject: [PATCH] Add. Allow set null to callbacks. --- src/lcutils.c | 15 +++++++++++++-- test/test_easy.lua | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/lcutils.c b/src/lcutils.c index e4428fb..cbf1865 100644 --- a/src/lcutils.c +++ b/src/lcutils.c @@ -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, (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))); 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; } + 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 c->ud_ref = luaL_ref(L, LCURL_LUA_REGISTRY); c->cb_ref = luaL_ref(L, LCURL_LUA_REGISTRY); diff --git a/test/test_easy.lua b/test/test_easy.lua index 3195b61..be9e08f 100644 --- a/test/test_easy.lua +++ b/test/test_easy.lua @@ -153,9 +153,16 @@ function test_reset_write_callback() assert_equal(c, c:setopt_writefunction(f)) assert_equal(c, c:setopt_writefunction(f.write, f)) 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(nil)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 function test_write_pass_01() @@ -210,6 +217,38 @@ function test_write_coro() assert_equal(co2, called) 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 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.header, f)) 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(nil)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 function test_header_pass_01()