Skip to content

Commit

Permalink
Object 102
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed Jan 4, 2025
1 parent ce5fd42 commit 29c19fb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions lua/fittencode/http/libcurl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 _()
Expand Down
10 changes: 9 additions & 1 deletion lua/fittencode/http/libcurl/src/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions lua/fittencode/types.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
---@class FittenCode.HTTP.RequestOptions
---@field method string
---@field url string
---@field url? string
---@field headers table<string, string>
---@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
Expand Down

0 comments on commit 29c19fb

Please sign in to comment.