lolly/internal/lua/helpers.go
xfy b1e1547e36 fix(lint): resolve errcheck and goconst issues
- Add nolint comments for sync.Pool.Get() type assertions (pool always returns valid pointers)
- Extract TLS version strings to constants in sslutil/tlsconfig.go
- Extract expires directive strings to constants in handler/static.go

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-30 13:41:15 +08:00

57 lines
1.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package lua 提供 Lua API 辅助函数。
//
// 该文件包含 ngx 表操作的共享辅助函数,用于减少代码重复。
// 所有函数保持并发安全设计(使用 RawGetString/RawSetString
//
// 作者xfy
package lua
import glua "github.com/yuin/gopher-lua"
// GetOrCreateNgxTable 获取或创建全局 ngx 表。
//
// 如果全局 ngx 表已存在,则返回现有表;否则创建新表并设置为全局变量。
// 该函数是并发安全的,使用 RawGetString/RawSetString 操作。
//
// 参数:
// - L: Lua 状态机
//
// 返回值:
// - *glua.LTable: ngx 表
func GetOrCreateNgxTable(L *glua.LState) *glua.LTable {
ngx := L.GetGlobal("ngx")
if ngx != nil && ngx.Type() == glua.LTTable {
if tbl, ok := ngx.(*glua.LTable); ok {
return tbl
}
}
// 创建新的 ngx 表
tbl := L.NewTable()
L.SetGlobal("ngx", tbl)
return tbl
}
// GetOrCreateNgxSubTable 获取或创建 ngx 子表。
//
// 如果子表已存在,则返回现有表;否则创建新子表并设置到父表。
// 该函数是并发安全的,使用 RawGetString/RawSetString 操作。
//
// 参数:
// - ngx: 父表(通常是 ngx 表)
// - L: Lua 状态机
// - name: 子表名称(如 "req", "resp", "socket"
//
// 返回值:
// - *glua.LTable: 子表
func GetOrCreateNgxSubTable(ngx *glua.LTable, L *glua.LState, name string) *glua.LTable {
existing := ngx.RawGetString(name)
if existing == glua.LNil {
// 首次创建子表
sub := L.NewTable()
ngx.RawSetString(name, sub)
return sub
}
return existing.(*glua.LTable) //nolint:errcheck // RawGetString returns LNil or valid LValue
}