lua: standardize hooks and host APIs on PascalCase

This commit is contained in:
Meo597
2026-09-28 22:52:14 +08:00
parent 3d3306503d
commit 5724db08f4
9 changed files with 66 additions and 66 deletions
+2 -2
View File
@@ -10,7 +10,7 @@ func RegisterLua(L *lua.LState) {
L.PreloadModule("xray.geodata", func(L *lua.LState) int {
module := L.NewTable()
module.RawSetString("domainMatcher", L.NewFunction(func(L *lua.LState) int {
module.RawSetString("DomainMatcher", L.NewFunction(func(L *lua.LState) int {
parsed, err := ParseDomainRules(luaRules(L), Domain_Domain)
if err != nil {
L.RaiseError("%v", err)
@@ -25,7 +25,7 @@ func RegisterLua(L *lua.LState) {
return 1
}))
module.RawSetString("ipMatcher", L.NewFunction(func(L *lua.LState) int {
module.RawSetString("IPMatcher", L.NewFunction(func(L *lua.LState) int {
parsed, err := ParseIPRules(luaRules(L))
if err != nil {
L.RaiseError("%v", err)
+13 -13
View File
@@ -18,11 +18,11 @@ func TestLuaIPMatcher(t *testing.T) {
ips.Value = []net.IP{ip.Value.(net.IP), net.ParseIP("8.8.8.8")}
L.SetGlobal("ips", ips)
if err := L.DoString(`
local matcher = require("xray.geodata").ipMatcher("127.0.0.0/8", "::1")
assert(matcher:match(ip))
assert(matcher:anyMatch(ips))
assert(not matcher:matches(ips))
local matched, unmatched = matcher:filterIPs(ips)
local matcher = require("xray.geodata").IPMatcher("127.0.0.0/8", "::1")
assert(matcher:Match(ip))
assert(matcher:AnyMatch(ips))
assert(not matcher:Matches(ips))
local matched, unmatched = matcher:FilterIPs(ips)
assert(type(matched) == "userdata" and type(unmatched) == "userdata")
assert(#matched == 1 and #unmatched == 1)
`); err != nil {
@@ -35,12 +35,12 @@ func TestLuaDomainMatcher(t *testing.T) {
defer L.Close()
RegisterLua(L)
if err := L.DoString(`
local matcher = require("xray.geodata").domainMatcher("example.com", "full:other.com")
assert(matcher:matchAny("example.com"))
assert(matcher:matchAny("www.example.com"))
assert(matcher:matchAny("other.com"))
assert(not matcher:matchAny("www.other.com"))
assert(#(matcher:match("www.example.com")) == 1)
local matcher = require("xray.geodata").DomainMatcher("example.com", "full:other.com")
assert(matcher:MatchAny("example.com"))
assert(matcher:MatchAny("www.example.com"))
assert(matcher:MatchAny("other.com"))
assert(not matcher:MatchAny("www.other.com"))
assert(#(matcher:Match("www.example.com")) == 1)
`); err != nil {
t.Fatal(err)
}
@@ -51,8 +51,8 @@ func TestLuaMatchersRejectInvalidRules(t *testing.T) {
name string
script string
}{
{"IP rule", `require("xray.geodata").ipMatcher("not-an-ip")`},
{"non-string domain rule", `require("xray.geodata").domainMatcher("example.com", true)`},
{"IP rule", `require("xray.geodata").IPMatcher("not-an-ip")`},
{"non-string domain rule", `require("xray.geodata").DomainMatcher("example.com", true)`},
} {
t.Run(tc.name, func(t *testing.T) {
L := lua.NewState()
+4 -4
View File
@@ -12,10 +12,10 @@ func RegisterLua(L *lua.LState) {
L.PreloadModule("xray.log", func(L *lua.LState) int {
module := L.NewTable()
for name, severity := range map[string]Severity{
"debug": Severity_Debug,
"info": Severity_Info,
"warning": Severity_Warning,
"error": Severity_Error,
"Debug": Severity_Debug,
"Info": Severity_Info,
"Warning": Severity_Warning,
"Error": Severity_Error,
} {
module.RawSetString(name, L.NewFunction(func(L *lua.LState) int {
var content strings.Builder
+9 -9
View File
@@ -36,19 +36,19 @@ func TestLuaLog(t *testing.T) {
if err := os.WriteFile(path, []byte(`
local log = require("xray.log")
assert(log == require("xray.log"))
log.debug("query: ", "example.com")
log.info("count=", 42, ", enabled=", true, ", value=", nil)
log.warning(setmetatable({}, {
log.Debug("query: ", "example.com")
log.Info("count=", 42, ", enabled=", true, ", value=", nil)
log.Warning(setmetatable({}, {
__tostring = function() return "fallback" end
}))
assert(select("#", log.error("failed")) == 0)
log.error("DNS failed: ", nativeError)
log.warning(nativeError)
assert(select("#", log.Error("failed")) == 0)
log.Error("DNS failed: ", nativeError)
log.Warning(nativeError)
local ok, err = pcall(function() error("Lua failure", 0) end)
assert(not ok)
log.error(err)
log.Error(err)
function logHook()
log.info("hook")
log.Info("hook")
end
`), 0o600); err != nil {
t.Fatal(err)
@@ -58,7 +58,7 @@ func TestLuaLog(t *testing.T) {
}
if err := L.DoString(`
logHook()
require("xray.log").info("anonymous")
require("xray.log").Info("anonymous")
`); err != nil {
t.Fatal(err)
}