From db7e541e092dbf0d8713df9a83d69b7a5309d71c Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Wed, 3 Sep 2014 11:16:10 +0500 Subject: [PATCH] Fix. Remove values from storage on unset. Fix. Set callback with context. --- src/lcutils.c | 16 ++++++++++++++-- src/lcutils.h | 2 ++ test/test_easy.lua | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/lcutils.c b/src/lcutils.c index 0a758a8..aea6519 100644 --- a/src/lcutils.c +++ b/src/lcutils.c @@ -53,11 +53,23 @@ void lcurl_storage_remove_i(lua_State *L, int storage, int i){ lua_rawgeti(L, -1, LCURL_STORAGE_KV); if(lua_istable(L, -1)){ lua_pushnil(L); - lua_rawseti(L, -3, i); + lua_rawseti(L, -2, i); } lua_pop(L, 2); } +void lcurl_storage_get_i(lua_State *L, int storage, int i){ + lua_rawgeti(L, LCURL_LUA_REGISTRY, storage); + lua_rawgeti(L, -1, LCURL_STORAGE_KV); + if(lua_istable(L, -1)){ + lua_rawgeti(L, -2, i); + lua_remove(L, -2); + lua_remove(L, -2); + } + else + lua_pop(L, 2); +} + struct curl_slist* lcurl_storage_remove_slist(lua_State *L, int storage, int idx){ struct curl_slist* list; assert(idx != LUA_NOREF); @@ -141,7 +153,7 @@ int lcurl_set_callback(lua_State *L, lcurl_callback_t *c, int i, const char *met i = lua_absindex(L, i); luaL_argcheck(L, !lua_isnoneornil(L, i), i, "no function present"); - luaL_argcheck(L, (top < (i + 1)), 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 diff --git a/src/lcutils.h b/src/lcutils.h index 727b94d..2a9c7bf 100644 --- a/src/lcutils.h +++ b/src/lcutils.h @@ -33,6 +33,8 @@ void lcurl_storage_preserve_iv(lua_State *L, int storage, int i, int v); void lcurl_storage_remove_i(lua_State *L, int storage, int i); +void lcurl_storage_get_i(lua_State *L, int storage, int i); + int lcurl_storage_free(lua_State *L, int storage); struct curl_slist* lcurl_util_array_to_slist(lua_State *L, int t); diff --git a/test/test_easy.lua b/test/test_easy.lua index 04b07c7..5eeded9 100644 --- a/test/test_easy.lua +++ b/test/test_easy.lua @@ -52,6 +52,14 @@ function test_write_to_file_abort() end +function test_reset_write_callback() + f = assert(io.open(fname, "w+b")) + c = assert(curl.easy{url = url}) + assert_equal(c, c:setopt_writefunction(f)) + assert_equal(c, c:setopt_writefunction(f.write, f)) + assert_equal(c, c:setopt_writefunction(print)) +end + end local _ENV = TEST_CASE'escape' do @@ -99,8 +107,41 @@ function test() assert(not pfrom.value) end +function test_unset() + local pfrom, e + do + local form = curl.form() + e = curl.easy{httppost = form} + pfrom = weak_ptr(form) + end + + gc_collect() + assert(pfrom.value) + + e:unsetopt_httppost() + + gc_collect() + assert(not pfrom.value) +end + +function test_reset() + local pfrom, e + do + local form = curl.form() + e = curl.easy{httppost = form} + pfrom = weak_ptr(form) + end + + gc_collect() + assert(pfrom.value) + + e:reset() + + gc_collect() + assert(not pfrom.value) end +end if not HAS_RUNNER then lunit.run() end