68
c/lua/_demo/dump/load/load.go
Normal file
68
c/lua/_demo/dump/load/load.go
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"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 {
|
||||||
|
file := (*os.File)(data)
|
||||||
|
|
||||||
|
fileInfo, err := file.Stat()
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
fileSize := fileInfo.Size()
|
||||||
|
|
||||||
|
buffer := make([]byte, fileSize)
|
||||||
|
bytesRead, err := file.Read(buffer)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
*size = c.Ulong(bytesRead)
|
||||||
|
|
||||||
|
if bytesRead > 0 {
|
||||||
|
return (*c.Char)(unsafe.Pointer(unsafe.SliceData(buffer)))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
L := lua.Newstate()
|
||||||
|
defer L.Close()
|
||||||
|
L.Openlibs()
|
||||||
|
|
||||||
|
file, err := os.Open("../llgofunc.luac")
|
||||||
|
if err != nil {
|
||||||
|
c.Printf(c.Str("Failed to open file for reading\n"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
if L.Load(reader, c.Pointer(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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Expected output:
|
||||||
|
Stack size before call: 1
|
||||||
|
Top element type after call: function
|
||||||
|
Result: Hello, World!
|
||||||
|
*/
|
||||||
56
c/lua/_demo/dump/save/save.go
Normal file
56
c/lua/_demo/dump/save/save.go
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"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 {
|
||||||
|
file := (*os.File)(ud)
|
||||||
|
data := unsafe.Slice((*byte)(p), sz)
|
||||||
|
|
||||||
|
n, err := file.Write(data)
|
||||||
|
if err != nil || n != int(sz) {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return lua.OK
|
||||||
|
}
|
||||||
|
|
||||||
|
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, err := os.Create("../llgofunc.luac")
|
||||||
|
if err != nil {
|
||||||
|
c.Printf(c.Str("Failed to open file for writing\n"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
if L.Dump(writer, c.Pointer(file), 0) != lua.OK {
|
||||||
|
c.Printf(c.Str("Failed to dump Lua function\n"))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
15
c/lua/lua.go
15
c/lua/lua.go
@@ -78,8 +78,6 @@ const (
|
|||||||
type Number = c.Double
|
type Number = c.Double
|
||||||
|
|
||||||
// /* type for integer functions */
|
// /* type for integer functions */
|
||||||
// TODO(zzy):consider dynamic size
|
|
||||||
|
|
||||||
type Integer = c.Int
|
type Integer = c.Int
|
||||||
|
|
||||||
// /* unsigned integer type */
|
// /* 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
|
// ** Type for functions that read/write blocks when loading/dumping Lua chunks
|
||||||
// */
|
// */
|
||||||
|
|
||||||
// typedef const char * (*lua_Reader) (State *L, void *ud, size_t *sz);
|
// llgo:type C
|
||||||
// typedef int (*lua_Writer) (State *L, const void *p, size_t sz, void *ud);
|
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
|
// ** 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)
|
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
|
// ** coroutine functions
|
||||||
|
|||||||
Reference in New Issue
Block a user