mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-10-05 07:13:36 +00:00
geodata: reduce Lua matcher build allocations
This commit is contained in:
+15
-10
@@ -7,6 +7,19 @@ import (
|
||||
luar "layeh.com/gopher-luar"
|
||||
)
|
||||
|
||||
var (
|
||||
luaDomainDirectMethods = map[string]xlua.DirectMethod{
|
||||
"Match": luaDomainMatch,
|
||||
"MatchAny": luaDomainMatchAny,
|
||||
}
|
||||
luaIPDirectMethods = map[string]xlua.DirectMethod{
|
||||
"Match": luaIPMatch,
|
||||
"AnyMatch": luaIPAnyMatch,
|
||||
"Matches": luaIPMatches,
|
||||
"FilterIPs": luaIPFilterIPs,
|
||||
}
|
||||
)
|
||||
|
||||
// RegisterLua makes xray.geodata available to require in an LState.
|
||||
func RegisterLua(L *lua.LState) {
|
||||
L.PreloadModule("xray.geodata", func(L *lua.LState) int {
|
||||
@@ -23,10 +36,7 @@ func RegisterLua(L *lua.LState) {
|
||||
L.RaiseError("%v", err)
|
||||
return 0
|
||||
}
|
||||
xlua.PushWithDirectMethods(L, matcher, map[string]xlua.DirectMethod{
|
||||
"Match": luaDomainMatch,
|
||||
"MatchAny": luaDomainMatchAny,
|
||||
})
|
||||
xlua.PushWithDirectMethods(L, matcher, luaDomainDirectMethods)
|
||||
return 1
|
||||
}))
|
||||
|
||||
@@ -41,12 +51,7 @@ func RegisterLua(L *lua.LState) {
|
||||
L.RaiseError("%v", err)
|
||||
return 0
|
||||
}
|
||||
xlua.PushWithDirectMethods(L, matcher, map[string]xlua.DirectMethod{
|
||||
"Match": luaIPMatch,
|
||||
"AnyMatch": luaIPAnyMatch,
|
||||
"Matches": luaIPMatches,
|
||||
"FilterIPs": luaIPFilterIPs,
|
||||
})
|
||||
xlua.PushWithDirectMethods(L, matcher, luaIPDirectMethods)
|
||||
return 1
|
||||
}))
|
||||
L.Push(module)
|
||||
|
||||
+2
-2
@@ -29,11 +29,11 @@ func PushWithDirectMethods(L *glua.LState, value any, directMethods map[string]D
|
||||
return callLuarMethod(L, original)
|
||||
})
|
||||
// Keep luar's method aliases on the same direct binding.
|
||||
methods.ForEach(func(key, method glua.LValue) {
|
||||
for key, method := methods.Next(glua.LNil); key != glua.LNil; key, method = methods.Next(key) {
|
||||
if method == original {
|
||||
methods.RawSet(key, fn)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
metatable.RawSetString("__index", methods)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user