From 29c19fbc611d0743dcc600b3b8e72513d84cc1e5 Mon Sep 17 00:00:00 2001 From: luozhiya Date: Sat, 4 Jan 2025 23:11:31 +0800 Subject: [PATCH] Object 102 --- lua/fittencode/http/libcurl/init.lua | 1 + lua/fittencode/http/libcurl/src/api.cc | 10 +++++++++- lua/fittencode/types.lua | 13 +++++++------ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lua/fittencode/http/libcurl/init.lua b/lua/fittencode/http/libcurl/init.lua index 21bcd32..093c6ac 100644 --- a/lua/fittencode/http/libcurl/init.lua +++ b/lua/fittencode/http/libcurl/init.lua @@ -7,6 +7,7 @@ CC.global_init() local M = {} +---@param options FittenCode.HTTP.RequestOptions ---@return FittenCode.HTTP.RequestHandle? function M.fetch(url, options) local function _() diff --git a/lua/fittencode/http/libcurl/src/api.cc b/lua/fittencode/http/libcurl/src/api.cc index bb52253..438df9a 100644 --- a/lua/fittencode/http/libcurl/src/api.cc +++ b/lua/fittencode/http/libcurl/src/api.cc @@ -52,7 +52,13 @@ static int l_fetch(lua_State *L) { std::cout << lua_gettop(L) << std::endl; lua_pushnil(L); // 第一个key,nil表示从第一个元素开始 std::cout << lua_gettop(L) << std::endl; - while (lua_next(L, 2) != 0) { + // 因为 fetch 第二个参数是options table,所以从2开始遍历 + // lua_next(L, 2) 返回0表示遍历结束 + // lua_next(L, 2) 返回1表示成功 + // lua_next 会先弹出一个top,然后再push一个key-value对,所以需要先push一个nil + int v = lua_next(L, 2); + std::cout << "lua_next: " << v << std::endl; + while (v != 0) { std::cout << lua_gettop(L) << std::endl; // key在-2位置,value在-1位置 std::string_view key = lua_tostring(L, -2); // 获取key @@ -91,6 +97,8 @@ static int l_fetch(lua_State *L) { } // 弹出value,保留key用于下一次迭代 lua_pop(L, 1); + v = lua_next(L, 2); + std::cout << "lua_next: " << v << std::endl; } // Set the callback function to write data to string diff --git a/lua/fittencode/types.lua b/lua/fittencode/types.lua index bf5b540..ad50505 100644 --- a/lua/fittencode/types.lua +++ b/lua/fittencode/types.lua @@ -1,15 +1,16 @@ ---@class FittenCode.HTTP.RequestOptions ---@field method string ----@field url string +---@field url? string ---@field headers table ---@field body table ---@field compress? boolean ---@field no_buffer? boolean ----@field on_create function ----@field on_stream function ----@field on_once function ----@field on_error function ----@field on_exit function +---@field on_create? function +---@field on_input? function +---@field on_stream? function +---@field on_once? function +---@field on_error? function +---@field on_exit? function ---@class FittenCode.HTTP.RequestHandle ---@field abort function