diff --git a/example/basic.go b/example/basic.go index 2276822b..b0842089 100644 --- a/example/basic.go +++ b/example/basic.go @@ -1,46 +1,86 @@ package main -import "lua51" +import lua "lua51" import "fmt" -func test(L *lua51.State) int { - fmt.Println("hello world! from go!"); - return 0; +// Simple execute code test +func test_simplestring(L *lua.State) { + L.DoString(` + print("test_simplestring: Hello, world!") + `); + // TODO: L.DoFile } +// --- -func test2(L *lua51.State) int { - arg := lua51.CheckInteger(L,-1); - argfrombottom := lua51.CheckInteger(L,1); - fmt.Print("test2 arg: "); - fmt.Println(arg); - fmt.Print("from bottom: "); - fmt.Println(argfrombottom); - return 0; +// Simple callback - no input parameters, no output parameters +func test_callback_self(L *lua.State) int { + fmt.Printf("test_callback_self: Callback ok\n") + return 0 } -func main() { - var L *lua51.State; +func test_callback_test(L *lua.State) { + L.Register("test_callback", test_callback_self) + L.DoString(` + test_callback() + `) +} +// --- - L = lua51.NewState(); - L.OpenLibs(); +// Callback with input parameters +func test_cbinput_self(L *lua.State) int { + param1 := L.ToString(1) + param2 := L.ToInteger(2) + fmt.Printf("test_cbinput_self: Callback with params: %s %d\n", param1, param2) + return 0 +} - L.GetField(lua51.LUA_GLOBALSINDEX, "print"); - L.PushString("Hello World!"); - L.Call(1,0); +func test_cbinput_test(L *lua.State) { + L.Register("test_cbinput", test_cbinput_self) - L.PushGoFunction(test); - L.PushGoFunction(test); - L.PushGoFunction(test); - L.PushGoFunction(test); + // call our function from lua + L.DoString(` + test_cbinput("test_string", 1234) + `) - L.PushGoFunction(test2); - L.PushInteger(42); - L.Call(1,0); + // call our function from go - new variant + L.GetField(lua.LUA_GLOBALSINDEX, "test_cbinput"); + L.PushAny("raw new") + L.PushAny(2345) + L.Call(2,0) + // call our function from go - old variant + L.GetField(lua.LUA_GLOBALSINDEX, "test_cbinput"); + L.PushString("raw old") + L.PushInteger(3456) + L.Call(2,0) +} +// --- + +// Callback with output params +func test_cboutput_self(L *lua.State) int { + L.PushAny("some string") + L.PushAny(12345) + return 2 +} - L.Call(0,0); - L.Call(0,0); - L.Call(0,0); +func test_cboutput_run(L *lua.State) { + L.Register("test_cboutput", test_cboutput_self) - L.Close(); + L.DoString(` + p_str, p_int = test_cboutput() + print("test_cboutput_self: p_str = ", p_str, " p_int = ", p_int) + `) } +// --- + +func main() { + L := lua.NewState() + defer L.Close() + L.OpenLibs(); + + test_simplestring(L) + test_callback_test(L) + test_cbinput_test(L) + test_cboutput_run(L) +} + diff --git a/lua51/Makefile b/lua51/Makefile index 8b95a45d..6bc5502f 100644 --- a/lua51/Makefile +++ b/lua51/Makefile @@ -34,7 +34,6 @@ CLEANFILES+=lua_defs.go LUA_HEADERS=lua.h lauxlib.h lualib.h LUA_HEADER_FILES:=$(patsubst %,$(LUA51_INCLUDE_DIR)/%,$(LUA_HEADERS)) -LUA_INCLUDE_DIRECTIVES:=$(patsubst %,//\#include <%>\n, $(LUA_HEADERS)) include $(GOROOT)/src/Make.pkg @@ -48,7 +47,7 @@ golua.o: golua.c lua_defs.go: echo "package lua51;" > lua_defs.go - echo "$(LUA_INCLUDE_DIRECTIVES)" "import \"C\"" >> lua_defs.go -# echo "import \"C\"" >> lua_defs.go + for a in $(LUA_HEADERS); do echo "// #include <$$a>" >> lua_defs.go; done + echo "import \"C\"" >> lua_defs.go cat $(LUA_HEADER_FILES) | grep '#define LUA' | sed 's/#define/const/' | sed 's/\([A-Z_][A-Z_]*\)[\t ].*/\1 = C.\1/' >> lua_defs.go diff --git a/lua51/lua.go b/lua51/lua.go index 9cb9e647..5d74461d 100644 --- a/lua51/lua.go +++ b/lua51/lua.go @@ -5,8 +5,6 @@ package lua51 import "C" import "unsafe" -//TODO: remove -import "fmt" @@ -101,7 +99,6 @@ func golua_callgofunction(L interface{}, fid uint) int { func golua_gchook(L interface{}, id uint) int { L1 := L.(*State); L1.unregister(id); - fmt.Printf("GC id: %d\n",id); return 0; } @@ -364,6 +361,16 @@ func (L *State) PushValue(index int) { C.lua_pushvalue(L.s, C.int(index)); } +func (L *State) PushAny(z interface{}) { + switch v := z.(type) { + case string: L.PushString(v) + case int: L.PushInteger(v) + case float64: L.PushNumber(v) + case bool: L.PushBoolean(v) + default: L.PushNil() + } +} + func (L *State) RawEqual(index1 int, index2 int) bool { return C.lua_rawequal(L.s, C.int(index1), C.int(index2)) != 0; } @@ -425,6 +432,13 @@ func (L *State) SetTable(index int) { C.lua_settable(L.s, C.int(index)); } +func (L *State) SetTableAny(index int, key interface {}, value interface {}){ + // don't forget about pushes while calling with relative index + L.PushAny(key) + L.PushAny(value) + L.SetTable(index) +} + func (L *State) SetTop(index int) { C.lua_settop(L.s, C.int(index)); }