add set and set_error methods and fix some bugs

This commit is contained in:
2025-08-10 09:49:35 +03:00
parent 2ceb236a53
commit e594d519a7

View File

@@ -43,7 +43,7 @@ func addInitiatorHeaders(sid string, req *http.Request, headers http.Header) {
// I will be only glad. // I will be only glad.
// TODO: make this huge function more harmonious by dividing responsibilities // TODO: make this huge function more harmonious by dividing responsibilities
func (h *HandlerV1) handleLUA(sid string, r *http.Request, req *rpc.RPCRequest, path string) *rpc.RPCResponse { func (h *HandlerV1) handleLUA(sid string, r *http.Request, req *rpc.RPCRequest, path string) *rpc.RPCResponse {
var __exit = -1 var __exit = 0
llog := h.x.SLog.With(slog.String("session-id", sid)) llog := h.x.SLog.With(slog.String("session-id", sid))
llog.Debug("handling LUA") llog.Debug("handling LUA")
@@ -193,25 +193,36 @@ func (h *HandlerV1) handleLUA(sid string, r *http.Request, req *rpc.RPCRequest,
} }
resFTable := scriptDataTable.RawGetString("result") resFTable := scriptDataTable.RawGetString("result")
if resPTable, ok := res.(*lua.LTable); ok {
// switch resTable.Type() { resPTable.ForEach(func(key, value lua.LValue) {
// case lua.LTTable: L.SetField(resFTable, key.String(), value)
if resPTable, ok := res.(*lua.LTable); ok { })
resPTable.ForEach(func(key, value lua.LValue) { } else {
L.SetField(resFTable, key.String(), value) L.SetField(scriptDataTable, "result", res)
}) }
} else {
L.SetField(scriptDataTable, "result", res)
}
// default:
// L.SetField(resTable, key.String(), value)
// }
__exit = 0 __exit = 0
L.RaiseError("__successfull") L.RaiseError("__successfull")
return 0 return 0
})) }))
L.SetField(outTable, "set", L.NewFunction(func(L *lua.LState) int {
res := L.Get(1)
if res == lua.LNil {
return 0
}
resFTable := scriptDataTable.RawGetString("result")
if resPTable, ok := res.(*lua.LTable); ok {
resPTable.ForEach(func(key, value lua.LValue) {
L.SetField(resFTable, key.String(), value)
})
} else {
L.SetField(scriptDataTable, "result", res)
}
return 0
}))
errTable := L.NewTable() errTable := L.NewTable()
L.SetField(scriptDataTable, "error", errTable) L.SetField(scriptDataTable, "error", errTable)
L.SetField(outTable, "send_error", L.NewFunction(func(L *lua.LState) int { L.SetField(outTable, "send_error", L.NewFunction(func(L *lua.LState) int {
@@ -243,6 +254,32 @@ func (h *HandlerV1) handleLUA(sid string, r *http.Request, req *rpc.RPCRequest,
return 0 return 0
})) }))
L.SetField(outTable, "set_error", L.NewFunction(func(L *lua.LState) int {
var params [3]lua.LValue
for i := range 3 {
params[i] = L.Get(i + 1)
}
if errTable, ok := scriptDataTable.RawGetString("error").(*lua.LTable); ok {
for _, v := range params {
switch v.Type() {
case lua.LTNumber:
if n, ok := v.(lua.LNumber); ok {
L.SetField(errTable, "code", n)
}
case lua.LTString:
if s, ok := v.(lua.LString); ok {
L.SetField(errTable, "message", s)
}
case lua.LTTable:
if tbl, ok := v.(*lua.LTable); ok {
L.SetField(errTable, "data", tbl)
}
}
}
}
return 0
}))
L.SetField(sessionMod, "request", inTable) L.SetField(sessionMod, "request", inTable)
L.SetField(sessionMod, "response", outTable) L.SetField(sessionMod, "response", outTable)
@@ -543,7 +580,8 @@ func (h *HandlerV1) handleLUA(sid string, r *http.Request, req *rpc.RPCRequest,
} }
} }
llog.Debug("executing script", slog.String("script", path)) llog.Debug("executing script", slog.String("script", path))
if err := L.DoFile(path); err != nil && __exit == -1 { err := L.DoFile(path)
if err != nil && __exit != 0 && __exit != 1 {
llog.Error("script error", slog.String("script", path), slog.String("error", err.Error())) llog.Error("script error", slog.String("script", path), slog.String("error", err.Error()))
return rpc.NewError(rpc.ErrInternalError, rpc.ErrInternalErrorS, nil, req.ID) return rpc.NewError(rpc.ErrInternalError, rpc.ErrInternalErrorS, nil, req.ID)
} }