-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from luoliwoshang/demo/lua
demo test:loadcall,metatable for lua
- Loading branch information
Showing
3 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
_ "unsafe" | ||
|
||
"lua" | ||
|
||
"github.com/goplus/llgo/c" | ||
) | ||
|
||
func main() { | ||
L := lua.Newstate__1() | ||
defer L.Close() | ||
|
||
L.Openlibs() | ||
if res := L.Loadstring(c.Str("print('hello world')")); res != lua.OK { | ||
println("error") | ||
} | ||
if res := L.Pcallk(0, 0, 0, 0, nil); res != lua.OK { | ||
println("error") | ||
} | ||
|
||
} | ||
|
||
/* Expected output: | ||
hello world | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package main | ||
|
||
import ( | ||
"lua" | ||
|
||
"github.com/goplus/llgo/c" | ||
) | ||
|
||
func toString(L *lua.State) c.Int { | ||
L.Pushstring(c.Str("Hello from metatable!")) | ||
return 1 | ||
} | ||
|
||
func printStack(L *lua.State, message string) { | ||
top := L.Gettop() | ||
c.Printf(c.Str("%s - Stack size: %d\n"), c.AllocaCStr(message), c.Int(top)) | ||
for i := c.Int(1); i <= top; i++ { | ||
t := L.Type(i) | ||
switch t { | ||
case c.Int(lua.STRING): | ||
c.Printf(c.Str(" %d: string: %s\n"), c.Int(i), L.Tolstring(i, nil)) | ||
case c.Int(lua.BOOLEAN): | ||
c.Printf(c.Str(" %d: boolean: %v\n"), c.Int(i), L.Toboolean(i)) | ||
case c.Int(lua.NUMBER): | ||
c.Printf(c.Str(" %d: number: %f\n"), c.Int(i), L.Tonumberx(i, nil)) | ||
default: | ||
c.Printf(c.Str(" %d: %s\n"), c.Int(i), L.Typename(t)) | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
L := lua.Newstate__1() | ||
defer L.Close() | ||
|
||
L.Openlibs() | ||
|
||
L.Createtable(0, 0) | ||
printStack(L, "After creating main table") | ||
|
||
L.Createtable(0, 0) | ||
printStack(L, "After creating metatable") | ||
|
||
L.Pushcclosure(toString, 0) | ||
printStack(L, "After Push CFunction") | ||
|
||
L.Setfield(-2, c.Str("__tostring")) | ||
printStack(L, "After setting __tostring") | ||
|
||
if L.Setmetatable(-2) == 0 { | ||
c.Printf(c.Str("Failed to set metatable\n")) | ||
} | ||
printStack(L, "After setting metatable") | ||
|
||
L.Setglobal(c.Str("obj")) | ||
printStack(L, "After setting global obj") | ||
|
||
testcode := c.Str(` | ||
if obj == nil then | ||
print('obj is not defined') | ||
else | ||
local mt = getmetatable(obj) | ||
if not mt then | ||
print('Metatable not set') | ||
elseif not mt.__tostring then | ||
print('__tostring not set in metatable') | ||
else | ||
print(mt.__tostring(obj)) | ||
end | ||
end | ||
`) | ||
|
||
if L.Loadstring(testcode) != lua.OK { | ||
c.Printf(c.Str("Error: %s\n"), L.Tolstring(-1, nil)) | ||
} | ||
|
||
if L.Pcallk(0, -1, 0, 0, nil) != lua.OK { | ||
c.Printf(c.Str("Error: %s\n"), L.Tolstring(-1, nil)) | ||
} | ||
|
||
L.Getglobal(c.Str("obj")) | ||
if L.Getmetatable(-1) != 0 { | ||
c.Printf(c.Str("Metatable get success\n")) | ||
L.Pushstring(c.Str("__tostring")) | ||
L.Gettable(-2) | ||
if L.Type(c.Int(-1)) == c.Int(lua.FUNCTION) { | ||
c.Printf(c.Str("__tostring function found in metatable\n")) | ||
if L.Iscfunction(-1) != 0 { | ||
c.Printf(c.Str("__tostring is a C function\n")) | ||
cfunc := L.Tocfunction(-1) | ||
if cfunc != nil { | ||
c.Printf(c.Str("Successfully retrieved __tostring C function pointer\n")) | ||
L.Pushcclosure(cfunc, 0) | ||
L.Callk(0, 1, 0, nil) | ||
result := L.Tolstring(-1, nil) | ||
c.Printf(c.Str("Result of calling __tostring: %s\n"), result) | ||
} | ||
} | ||
} else { | ||
c.Printf(c.Str("__tostring function not found in metatable\n")) | ||
} | ||
} else { | ||
c.Printf(c.Str("No metatable found using GetTable\n")) | ||
} | ||
} | ||
|
||
/* Expected output: | ||
After creating main table - Stack size: 1 | ||
1: table | ||
After creating metatable - Stack size: 2 | ||
1: table | ||
2: table | ||
After Push CFunction - Stack size: 3 | ||
1: table | ||
2: table | ||
3: function | ||
After setting __tostring - Stack size: 2 | ||
1: table | ||
2: table | ||
After setting metatable - Stack size: 1 | ||
1: table | ||
After setting global obj - Stack size: 0 | ||
Hello from metatable! | ||
Metatable get success | ||
__tostring function found in metatable | ||
__tostring is a C function | ||
Successfully retrieved __tostring C function pointer | ||
Result of calling __tostring: Hello from metatable! | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters