c/lua:custom alloc for state

This commit is contained in:
luoliwoshang
2024-09-16 23:31:17 +08:00
parent e3cb4ebfdc
commit 4defe734e2

View File

@@ -0,0 +1,36 @@
package main
import (
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/lua"
)
func alloc(ud c.Pointer, ptr c.Pointer, osize c.Ulong, nsize c.Ulong) c.Pointer {
if nsize == 0 {
c.Free(ptr)
return nil
} else {
return c.Realloc(ptr, uintptr(nsize))
}
}
func main() {
L := lua.Newstate__0(alloc, nil)
defer L.Close()
L.Openlibs()
if res := L.Dostring(c.Str("print('new state success')")); res != lua.OK {
println("newstate error")
}
allocf := L.Getallocf(nil)
L.Setallocf(allocf, nil)
if res := L.Dostring(c.Str("print('set newstate success')")); res != lua.OK {
println("set newstate error")
}
}
/* Expected output:
new state success
set newstate success
*/