Compare commits

...
1 Commits
Author SHA1 Message Date
Meo597 84ce7568bb Lua: Expose Go slices as read-only 2026-11-20 05:16:13 +08:00
5 changed files with 30 additions and 52 deletions
+1 -1
View File
@@ -23,7 +23,7 @@ func RegisterLua(L *lua.LState) {
return 0 return 0
} }
xlua.PushWithDirectMethods(L, matcher, map[string]xlua.DirectMethod{ xlua.PushWithDirectMethods(L, matcher, map[string]xlua.DirectMethod{
"Match": newLuaDomainMatch(xlua.NewSlicePusher[uint32](L)), "Match": newLuaDomainMatch(xlua.NewReadOnlySlicePusher[uint32](L)),
"MatchAny": luaDomainMatchAny, "MatchAny": luaDomainMatchAny,
}) })
return 1 return 1
+3 -1
View File
@@ -104,7 +104,9 @@ for _, value in ipairs({matcher, domain}) do
assert(not pcall(function() return value ^ {} end)) assert(not pcall(function() return value ^ {} end))
end end
assert(domain:matchAny("example.com")) assert(domain:matchAny("example.com"))
assert(#domain:Match("example.com") == 1) local indices = domain:Match("example.com")
assert(#indices == 1)
assert(not pcall(function() indices[1] = 7 end))
assert(domain:match("example.com")[1] == 0) assert(domain:match("example.com")[1] == 0)
assert(not pcall(function() matcher:AnyMatch() end)) assert(not pcall(function() matcher:AnyMatch() end))
assert(not pcall(function() matcher:AnyMatch(matched, true) end)) assert(not pcall(function() matcher:AnyMatch(matched, true) end))
+3 -3
View File
@@ -8,10 +8,10 @@ import (
luar "layeh.com/gopher-luar" luar "layeh.com/gopher-luar"
) )
// NewSlicePusher captures luar's slice metatable during state initialization. // newSlicePusher captures luar's slice metatable during state initialization.
// The returned function wraps slices without reflection or metatable lookup, // The returned function wraps slices without reflection or metatable lookup,
// and pushes nil for nil slices. Use it with this state or its coroutines. // and pushes nil for nil slices. Use it with this state or its coroutines.
func NewSlicePusher[T any](L *glua.LState) func(*glua.LState, []T) { func newSlicePusher[T any](L *glua.LState) func(*glua.LState, []T) {
metatable := luar.MT(L, []T(nil)).LTable metatable := luar.MT(L, []T(nil)).LTable
return func(L *glua.LState, values []T) { return func(L *glua.LState, values []T) {
if values == nil { if values == nil {
@@ -38,7 +38,7 @@ func NewReadOnlySlicePusher[T any](L *glua.LState) func(*glua.LState, []T) {
}) })
setSliceReadOnly[[]T](L, readOnly) setSliceReadOnly[[]T](L, readOnly)
} }
return NewSlicePusher[T](L) return newSlicePusher[T](L)
} }
func setSliceReadOnly[S ~[]E, E any](L *glua.LState, readOnly *glua.LFunction) { func setSliceReadOnly[S ~[]E, E any](L *glua.LState, readOnly *glua.LFunction) {
+21 -43
View File
@@ -9,53 +9,20 @@ import (
luar "layeh.com/gopher-luar" luar "layeh.com/gopher-luar"
) )
func TestSlicePusher(t *testing.T) { func TestReadOnlySlicePusher(t *testing.T) {
L := glua.NewState() L := glua.NewState()
defer L.Close() defer L.Close()
push := NewSlicePusher[int](L) push := NewReadOnlySlicePusher[int](L)
values := []int{3, 5} values := []int{3, 5}
L.SetGlobal("getValues", L.NewFunction(func(L *glua.LState) int { L.SetGlobal("getValues", L.NewFunction(func(L *glua.LState) int {
push(L, values) push(L, values)
return 1 return 1
})) }))
if err := L.DoString(` if err := L.DoString(`
local values = getValues() local co = coroutine.create(function() return getValues() end)
assert(#values == 2 and values[1] == 3 and values[2] == 5)
values[2] = 7
local co = coroutine.create(function()
local values = getValues()
assert(#values == 2 and values[1] == 3 and values[2] == 7)
return true
end)
local ok, result = coroutine.resume(co) local ok, result = coroutine.resume(co)
assert(ok and result == true) assert(ok, result)
`); err != nil { values = result
t.Fatal(err)
}
if values[1] != 7 {
t.Fatal("slice storage was copied")
}
push(L, nil)
if L.Get(-1) != glua.LNil {
t.Fatal("nil slice must push Lua nil")
}
L.Pop(1)
push(L, []int{})
L.SetGlobal("empty", L.Get(-1))
L.Pop(1)
if err := L.DoString(`assert(type(empty) == "userdata" and #empty == 0)`); err != nil {
t.Fatal(err)
}
}
func TestReadOnlySlicePusher(t *testing.T) {
L := glua.NewState()
defer L.Close()
values := []int{3, 5}
NewReadOnlySlicePusher[int](L)(L, values)
L.SetGlobal("values", L.Get(-1))
L.Pop(1)
if err := L.DoString(`
assert(#values == 2 and values[1] == 3 and values[2] == 5) assert(#values == 2 and values[1] == 3 and values[2] == 5)
assert(not pcall(function() values[1] = 7 end)) assert(not pcall(function() values[1] = 7 end))
assert(not pcall(function() return values + 7 end)) assert(not pcall(function() return values + 7 end))
@@ -69,6 +36,17 @@ assert(sum == 8)
if &got[0] != &values[0] || values[0] != 3 || values[1] != 5 { if &got[0] != &values[0] || values[0] != 3 || values[1] != 5 {
t.Fatal("read-only binding copied or modified the slice") t.Fatal("read-only binding copied or modified the slice")
} }
push(L, nil)
if L.Get(-1) != glua.LNil {
t.Fatal("nil slice must push Lua nil")
}
L.Pop(1)
push(L, []int{})
L.SetGlobal("empty", L.Get(-1))
L.Pop(1)
if err := L.DoString(`assert(type(empty) == "userdata" and #empty == 0)`); err != nil {
t.Fatal(err)
}
} }
func TestReadOnlyIPSlicePusher(t *testing.T) { func TestReadOnlyIPSlicePusher(t *testing.T) {
@@ -230,23 +208,23 @@ assert(not pcall(function() return nested + nested end))
} }
} }
func TestSlicePusherMetatablePerState(t *testing.T) { func TestReadOnlySlicePusherMetatablePerState(t *testing.T) {
first := glua.NewState() first := glua.NewState()
defer first.Close() defer first.Close()
second := glua.NewState() second := glua.NewState()
defer second.Close() defer second.Close()
NewSlicePusher[int](first)(first, []int{1}) NewReadOnlySlicePusher[int](first)(first, []int{1})
NewSlicePusher[int](second)(second, []int{1}) NewReadOnlySlicePusher[int](second)(second, []int{1})
if first.Get(-1).(*glua.LUserData).Metatable == second.Get(-1).(*glua.LUserData).Metatable { if first.Get(-1).(*glua.LUserData).Metatable == second.Get(-1).(*glua.LUserData).Metatable {
t.Fatal("independent states share a slice metatable") t.Fatal("independent states share a slice metatable")
} }
} }
func BenchmarkSlicePusher(b *testing.B) { func BenchmarkReadOnlySlicePusher(b *testing.B) {
L := glua.NewState() L := glua.NewState()
defer L.Close() defer L.Close()
ips := []net.IP{net.ParseIP("127.0.0.1")} ips := []net.IP{net.ParseIP("127.0.0.1")}
pushIPs := NewSlicePusher[net.IP](L) pushIPs := NewReadOnlySlicePusher[net.IP](L)
for _, benchmark := range []struct { for _, benchmark := range []struct {
name string name string
push func(*glua.LState, []net.IP) push func(*glua.LState, []net.IP)
+2 -4
View File
@@ -671,7 +671,7 @@ string.__tostring = nil
L.SetGlobal("testNativeError", L.Get(-1)) L.SetGlobal("testNativeError", L.Get(-1))
L.Pop(1) L.Pop(1)
L.SetGlobal("testGoStruct", luar.New(L, &struct{ Value string }{"readable field"})) L.SetGlobal("testGoStruct", luar.New(L, &struct{ Value string }{"readable field"}))
NewSlicePusher[net.IP](L)(L, []net.IP{net.ParseIP("192.0.2.1")}) NewReadOnlySlicePusher[net.IP](L)(L, []net.IP{net.ParseIP("192.0.2.1")})
L.SetGlobal("testIPs", L.Get(-1)) L.SetGlobal("testIPs", L.Get(-1))
L.Pop(1) L.Pop(1)
userdata := L.NewUserData() userdata := L.NewUserData()
@@ -865,9 +865,7 @@ assert(not ok and a == "{<address> <address>}")
PushUserData(L, struct{ Pointer *int }{&target}) PushUserData(L, struct{ Pointer *int }{&target})
L.SetGlobal("testObject", L.Get(-1)) L.SetGlobal("testObject", L.Get(-1))
L.Pop(1) L.Pop(1)
NewSlicePusher[int](L)(L, []int{1}) L.SetGlobal("testValues", luar.New(L, []int{1}))
L.SetGlobal("testValues", L.Get(-1))
L.Pop(1)
L.SetGlobal("testPanic", L.NewFunction(func(L *glua.LState) int { L.SetGlobal("testPanic", L.NewFunction(func(L *glua.LState) int {
panic(struct{ First, Second *int }{&target, &target}) panic(struct{ First, Second *int }{&target, &target})
})) }))