From 986522e417406956e83362e08adbd6a18ec904a5 Mon Sep 17 00:00:00 2001 From: unwox Date: Sat, 14 Sep 2024 15:51:23 +0600 Subject: better handling of args coming from lua with l.Scan --- main.go | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 96e8a38..bde394e 100644 --- a/main.go +++ b/main.go @@ -37,9 +37,14 @@ func main() { // define luna.router module routeModule := make(map[string]any) routeModule["route"] = func (l *Lua) int { - fn := l.PopToRef() - route := l.ToString(-1) - l.Pop(1) + var route string + var fn LuaRef + err := l.Scan(&route, &fn) + if err != nil { + fmt.Println("error:", err) + // FIXME: handle + return 0 + } // find corresponding worker for the lua context var wrk *Worker for _, wrk = range wrks { @@ -84,26 +89,14 @@ func main() { // define luna.http module httpModule := make(map[string]any) httpModule["request"] = func (l *Lua) int { - body := l.ToString(-1) - url := l.ToString(-3) - method := l.ToString(-4) - l.Pop(1) + var method, url, body string headers := make(map[string]string) - l.PushNil() - for l.Next() { - if !l.IsString(-2) || !l.IsString(-2) { - l.Pop(1) - continue - } - v := l.ToString(-1) - l.Pop(1) - // We must not pop the item key from the stack - // because otherwise C.lua_next won't work - // properly. - k := l.ToString(-1) - headers[k] = v + err := l.Scan(&method, &url, &headers, &body) + if err != nil { + // FIXME: handle + fmt.Println("error:", err) + return 0 } - l.Pop(2) req, err := http.NewRequest(method, url, strings.NewReader(body)) if err != nil { -- cgit v1.2.3