c/lua:dump & load

This commit is contained in:
luoliwoshang
2024-08-31 20:41:47 +08:00
parent 2434fd778f
commit d1f64d3059
3 changed files with 105 additions and 6 deletions

View File

@@ -0,0 +1,44 @@
package main
import (
"unsafe"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/lua"
)
func reader(L *lua.State, data c.Pointer, size *c.Ulong) *c.Char {
buffer := make([]c.Char, 4096)
*size = c.Ulong(c.Fread(c.Pointer(unsafe.SliceData(buffer)), uintptr(1), uintptr(unsafe.Sizeof(buffer)), data))
if *size > 0 {
return &buffer[0]
}
return nil
}
func main() {
L := lua.Newstate()
defer L.Close()
L.Openlibs()
file := c.Fopen(c.Str("../llgofunc.luac"), c.Str("rb"))
if file == nil {
c.Printf(c.Str("Failed to open file for writing\n"))
}
if L.Load(reader, file, c.Str("greet"), nil) != lua.OK {
c.Printf(c.Str("Failed to dump Lua function\n"))
}
c.Printf(c.Str("Stack size before call: %d\n"), L.Gettop())
c.Printf(c.Str("Top element type after call: %s\n"), L.Typename(L.Type(-1)))
L.Pushstring(c.Str("World"))
if L.Pcall(1, 1, 0) != lua.OK {
c.Printf(c.Str("Failed to call function: %s\n"))
}
if L.Isstring(-1) != 0 {
c.Printf(c.Str("Result: %s\n"), L.Tostring(-1))
}
}

View File

@@ -0,0 +1,52 @@
package main
import (
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/lua"
)
func writer(L *lua.State, p c.Pointer, sz c.Ulong, ud c.Pointer) c.Int {
if c.Fwrite(p, uintptr(sz), 1, ud) == 1 {
return lua.OK
}
return 1
}
func main() {
L := lua.Newstate()
defer L.Close()
L.Openlibs()
if res := L.Loadstring(c.Str(`
function greet(name)
return 'Hello, ' .. name .. '!'
end
return greet
`)); res != lua.OK {
c.Printf(c.Str("error: %s\n"), L.Tostring(-1))
}
if res := L.Pcall(0, 1, 0); res != lua.OK {
c.Printf(c.Str("error: %s\n"), L.Tostring(-1))
}
if !L.Isfunction(-1) {
c.Printf(c.Str("Expected a function, but got %s"), L.Typename(L.Type(-1)))
}
file := c.Fopen(c.Str("../llgofunc.luac"), c.Str("wb"))
if file == nil {
c.Printf(c.Str("Failed to open file for writing\n"))
}
if L.Dump(writer, file, 0) != lua.OK {
c.Printf(c.Str("Failed to dump Lua function\n"))
}
}
/* Expected output:
Stack size before call: 1
Top element type after call: function
Result: Hello, World!
*/

View File

@@ -78,8 +78,6 @@ const (
type Number = c.Double
// /* type for integer functions */
// TODO(zzy):consider dynamic size
type Integer = c.Int
// /* unsigned integer type */
@@ -106,8 +104,11 @@ type KFunction func(L *State, status c.Int, ctx KContext) c.Int
// ** Type for functions that read/write blocks when loading/dumping Lua chunks
// */
// typedef const char * (*lua_Reader) (State *L, void *ud, size_t *sz);
// typedef int (*lua_Writer) (State *L, const void *p, size_t sz, void *ud);
// llgo:type C
type Reader func(L *State, ud c.Pointer, sz *c.Ulong) *c.Char
// llgo:type C
type Writer func(L *State, p c.Pointer, sz c.Ulong, ud c.Pointer) c.Int
// /*
// ** Type for memory-allocation functions
@@ -352,9 +353,11 @@ func (L *State) Pcall(nargs c.Int, nresults c.Int, errfunc c.Int) c.Int {
return L.Pcallk(nargs, nresults, errfunc, nil, nil)
}
// int (lua_load) (State *L, lua_Reader reader, void *dt, const char *chunkname, const char *mode);
// llgo:link (*State).Load C.lua_load
func (L *State) Load(reader Reader, dt c.Pointer, chunkname *c.Char, mode *c.Char) c.Int { return 0 }
// int (lua_dump) (State *L, lua_Writer writer, void *data, int strip);
// llgo:link (*State).Dump C.lua_dump
func (L *State) Dump(writer Writer, data c.Pointer, strip c.Int) c.Int { return 0 }
// /*
// ** coroutine functions