Skip to content

Commit

Permalink
llgo/c/lua:link style
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed Jul 13, 2024
1 parent 9edeee4 commit 1cdbb93
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 138 deletions.
16 changes: 8 additions & 8 deletions c/lua/_demo/coroutine/coroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ import (
)

func coroutineFunc(L *lua.State) {
L.LoadString(c.Str(`
L.Loadstring(c.Str(`
function coro_func()
for i = 1, 5 do
coroutine.yield(i)
end
end
`))
L.PCall(0, 0, 0)
L.GetGlobal(c.Str("coro_func"))
L.Getglobal(c.Str("coro_func"))
}

func main() {
L := lua.NewState()
L := lua.Newstate()
defer L.Close()

L.OpenLibs()
L.Openlibs()

coroutineFunc(L) // Load and get the coroutine function

co := L.NewThread() // Create a new coroutine/thread
L.PushValue(-2) // Move the function to the top of the stack
L.XMove(co, 1) // Move the function to the new coroutine
co := L.Newthread() // Create a new coroutine/thread
L.Pushvalue(-2) // Move the function to the top of the stack
L.Xmove(co, 1) // Move the function to the new coroutine

var nres c.Int
var status c.Int
Expand All @@ -43,7 +43,7 @@ func main() {
co.Pop(1) // Clean up the stack

// Check if the coroutine is yieldable
if co.IsYieldable() != 0 {
if co.Isyieldable() != 0 {
c.Printf(c.Str("Coroutine is yieldable.\n"))
} else {
c.Printf(c.Str("Coroutine is not yieldable.\n"))
Expand Down
6 changes: 3 additions & 3 deletions c/lua/_demo/error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
)

func main() {
L := lua.NewState()
L := lua.Newstate()
defer L.Close()
L.OpenLibs()
if res := L.LoadString(c.Str("function doubleNumber(x) ! return x * 2 end")); res != lua.OK {
L.Openlibs()
if res := L.Loadstring(c.Str("function doubleNumber(x) ! return x * 2 end")); res != lua.OK {
c.Printf(c.Str("error: %s\n"), L.ToString(-1))
}
}
Expand Down
12 changes: 6 additions & 6 deletions c/lua/_demo/funccall-concat/funccall.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ import (
)

func main() {
L := lua.NewState()
L := lua.Newstate()
defer L.Close()

L.OpenLibs()
L.Openlibs()
if res := L.DoString(c.Str("function combineParams(num, str) return 'Result: ' .. str .. ' ' .. num end")); res != lua.OK {
c.Printf(c.Str("error: %s\n"), L.ToString(-1))
}
L.GetGlobal(c.Str("combineParams"))
L.PushNumber(3.14159)
L.PushString(c.Str("Hello, World!"))
L.Getglobal(c.Str("combineParams"))
L.Pushnumber(3.14159)
L.Pushstring(c.Str("Hello, World!"))
if res := L.PCall(2, 1, 0); res != lua.OK {
c.Printf(c.Str("error: %s\n"), L.ToString(-1))
}
if res := L.IsString(-1); res != 0 {
if res := L.Isstring(-1); res != 0 {
result := L.ToString(-1)
c.Printf(result)
}
Expand Down
12 changes: 6 additions & 6 deletions c/lua/_demo/funccall-number/funccall.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ import (
)

func main() {
L := lua.NewState()
L := lua.Newstate()
defer L.Close()

L.OpenLibs()
if res := L.LoadString(c.Str("function doubleNumber(x) return x * 2 end")); res != lua.OK {
L.Openlibs()
if res := L.Loadstring(c.Str("function doubleNumber(x) return x * 2 end")); res != lua.OK {
c.Printf(c.Str("error: %s\n"), L.ToString(-1))
}
if res := L.PCall(0, 0, 0); res != lua.OK {
c.Printf(c.Str("error: %s\n"), L.ToString(-1))
}

L.GetGlobal(c.Str("doubleNumber"))
L.PushNumber(10)
L.Getglobal(c.Str("doubleNumber"))
L.Pushnumber(10)

if res := L.PCall(1, 1, 0); res != lua.OK {
c.Printf(c.Str("error: %s\n"), L.ToString(-1))
}

if res := L.IsNumber(-1); res != 0 {
if res := L.Isnumber(-1); res != 0 {
result := L.ToInteger(-1)
c.Printf(c.Str("result: %lld\n"), result)
} else {
Expand Down
14 changes: 7 additions & 7 deletions c/lua/_demo/funccall-string/funccall.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
)

func main() {
L := lua.NewState()
L := lua.Newstate()
defer L.Close()

L.OpenLibs()
L.Openlibs()
code := c.Str(
`function processStrings(a, b, c)
print('Received string a: ' .. a)
Expand All @@ -24,17 +24,17 @@ end`)
c.Printf(c.Str("error: %s\n"), L.ToString(-1))
}

L.GetGlobal(c.Str("processStrings"))
L.Getglobal(c.Str("processStrings"))

L.PushString(c.Str("Hello, World!"))
L.PushLString(c.Str(`Hello Lua In LLGO`), 17)
L.PushFString(c.Str(`Hello %s In %d`), c.Str("LLGO"), 2024)
L.Pushstring(c.Str("Hello, World!"))
L.Pushlstring(c.Str(`Hello Lua In LLGO`), 17)
L.Pushfstring(c.Str(`Hello %s In %d`), c.Str("LLGO"), 2024)

if res := L.PCall(3, 1, 0); res != lua.OK {
c.Printf(c.Str("error: %s\n"), L.ToString(-1))
}

if res := L.IsString(-1); res != 0 {
if res := L.Isstring(-1); res != 0 {
result := L.ToString(-1)
c.Printf(c.Str("result: %s\n"), result)
}
Expand Down
8 changes: 6 additions & 2 deletions c/lua/_demo/hello/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import (
)

func main() {
L := lua.NewState()
L := lua.Newstate()
defer L.Close()
L.OpenLibs()
L.Openlibs()
if res := L.DoString(c.Str("print('hello world')")); res != lua.OK {
println("error")
}
}

/* Expected output:
hello world
*/
10 changes: 7 additions & 3 deletions c/lua/_demo/loadcall/loadcall.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ import (
)

func main() {
L := lua.NewState()
L := lua.Newstate()
defer L.Close()

L.OpenLibs()
if res := L.LoadString(c.Str("print('hello world')")); res != lua.OK {
L.Openlibs()
if res := L.Loadstring(c.Str("print('hello world')")); res != lua.OK {
println("error")
}
if res := L.PCall(0, 0, 0); res != lua.OK {
println("error")
}

}

/* Expected output:
hello world
*/
53 changes: 39 additions & 14 deletions c/lua/_demo/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// printStack prints the current stack of the given Lua state.
func printStack(L *lua.State, stateName *c.Char) {
top := L.GetTop()
top := L.Gettop()
c.Printf(c.Str("%s stack (top=%d):"), stateName, top)
for i := 1; i <= int(top); i++ {
c.Printf(c.Str("%s "), L.ToString(c.Int(i)))
Expand All @@ -17,26 +17,26 @@ func printStack(L *lua.State, stateName *c.Char) {

func main() {
// Create a new Lua state and open libraries
L := lua.NewState()
L := lua.Newstate()
defer L.Close()
L.OpenLibs()
L.Openlibs()

// Push initial values onto the stack
L.PushString(c.Str("Hello"))
L.PushString(c.Str("LLGO"))
L.PushNumber(2024)
L.Pushstring(c.Str("Hello"))
L.Pushstring(c.Str("LLGO"))
L.Pushnumber(2024)

// Print initial stack
c.Printf(c.Str("Initial stack:\n"))
printStack(L, c.Str("L1"))

// Use absindex to ensure the index is positive
idx := -2
absIdx := L.AbsIndex(c.Int(idx))
absIdx := L.Absindex(c.Int(idx))
c.Printf(c.Str("Absolute index of 'LLGO': %d\n"), absIdx)

// Copy 'LLGO' to the top of the stack
L.PushValue(absIdx)
L.Pushvalue(absIdx)
c.Printf(c.Str("\nAfter pushing 'LLGO' to the top:\n"))
printStack(L, c.Str("L1"))

Expand All @@ -51,29 +51,54 @@ func main() {
printStack(L, c.Str("L1"))

// Check if we can grow the stack
if L.CheckStack(c.Int(2)) == 0 {
if L.Checkstack(c.Int(2)) == 0 {
c.Printf(c.Str("Cannot grow stack\n"))
return
}

// Push additional elements
L.PushNumber(3.14)
L.PushString(c.Str("Lua"))
L.Pushnumber(3.14)
L.Pushstring(c.Str("Lua"))
c.Printf(c.Str("\nAfter pushing more elements:\n"))
printStack(L, c.Str("L1"))

// Set the top of the stack, clearing extra elements
L.SetTop(c.Int(5))
L.Settop(c.Int(5))
c.Printf(c.Str("\nAfter setting top to 5:\n"))
printStack(L, c.Str("L1"))

// Create a second Lua state
L1 := lua.NewState()
L1 := lua.Newstate()
defer L1.Close()

// Move two elements to the new state
L.XMove(L1, c.Int(2))
L.Xmove(L1, c.Int(2))
c.Printf(c.Str("\nAfter moving two elements to L1:\n"))
printStack(L, c.Str("L1"))
printStack(L1, c.Str("L2"))
}

/* Expected output:
Initial stack:
L1 stack (top=3):Hello LLGO 2024.0
Absolute index of 'LLGO': 2
After pushing 'LLGO' to the top:
L1 stack (top=4):Hello LLGO 2024.0 LLGO
After rotating the stack:
L1 stack (top=4):LLGO 2024.0 LLGO Hello
After copying the top element to index 2:
L1 stack (top=4):LLGO Hello LLGO Hello
After pushing more elements:
L1 stack (top=6):LLGO Hello LLGO Hello 3.14 Lua
After setting top to 5:
L1 stack (top=5):LLGO Hello LLGO Hello 3.14
After moving two elements to L1:
L1 stack (top=3):LLGO Hello LLGO
L2 stack (top=2):Hello 3.14
*/
37 changes: 23 additions & 14 deletions c/lua/_demo/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func printTable(L *lua.State) {
L.PushNil()
L.Pushnil()
for L.Next(-2) != 0 {
key := L.ToString(-2)
value := L.ToString(-1)
Expand All @@ -17,30 +17,30 @@ func printTable(L *lua.State) {
}

func main() {
L := lua.NewState()
L := lua.Newstate()
defer L.Close()

L.OpenLibs()
L.Openlibs()

L.NewTable()

L.PushString(c.Str("name"))
L.PushString(c.Str("John"))
L.SetTable(-3)
L.Pushstring(c.Str("name"))
L.Pushstring(c.Str("John"))
L.Settable(-3)

L.PushString(c.Str("age"))
L.PushNumber(30)
L.SetTable(-3)
L.Pushstring(c.Str("age"))
L.Pushnumber(30)
L.Settable(-3)

L.PushString(c.Str("John Doe"))
L.SetField(-2, c.Str("fullname"))
L.Pushstring(c.Str("John Doe"))
L.Setfield(-2, c.Str("fullname"))

L.GetField(-1, c.Str("name"))
L.Getfield(-1, c.Str("name"))
c.Printf(c.Str("%s\n"), L.ToString(-1))
L.Pop(1)

L.PushString(c.Str("age"))
L.GetTable(-2)
L.Pushstring(c.Str("age"))
L.Gettable(-2)
age := int(L.ToNumber(-1))
c.Printf(c.Str("Age: %d\n"), age)
L.Pop(1)
Expand All @@ -49,3 +49,12 @@ func main() {
printTable(L)

}

/* Expected output:
John
Age: 30
All entries in the table:
age - 30.0
fullname - John Doe
name - John
*/
16 changes: 8 additions & 8 deletions c/lua/lauxlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ import (

// /* predefined references */

// llgo:link (*State).LoadFilex C.luaL_loadfilex
func (L *State) LoadFilex(filename *c.Char, mode *c.Char) c.Int { return 0 }
// llgo:link (*State).Loadfilex C.luaL_loadfilex
func (L *State) Loadfilex(filename *c.Char, mode *c.Char) c.Int { return 0 }

func (L *State) LoadFile(filename *c.Char) c.Int { return L.LoadFilex(filename, nil) }
func (L *State) LoadFile(filename *c.Char) c.Int { return L.Loadfilex(filename, nil) }

// llgo:link (*State).LoadString C.luaL_loadstring
func (L *State) LoadString(s *c.Char) c.Int { return 0 }
// llgo:link (*State).Loadstring C.luaL_loadstring
func (L *State) Loadstring(s *c.Char) c.Int { return 0 }

//go:linkname NewState C.luaL_newstate
func NewState() *State
//go:linkname Newstate C.luaL_newstate
func Newstate() *State

// /*
// ** ===============================================================
Expand All @@ -41,7 +41,7 @@ func (L *State) DoFile(filename *c.Char) c.Int {
}

func (L *State) DoString(str *c.Char) c.Int {
if loadResult := L.LoadString(str); loadResult != 0 {
if loadResult := L.Loadstring(str); loadResult != 0 {
return loadResult
}
return L.PCall(c.Int(0), c.Int(MULTRET), c.Int(0))
Expand Down
Loading

0 comments on commit 1cdbb93

Please sign in to comment.