Add example with lua built-in function
This commit is contained in:
58
Makefile
58
Makefile
@@ -12,6 +12,7 @@ WINDOWS_BINARY1=${MYNAME1}.exe
|
|||||||
WINDOWS_BINARY2=${MYNAME2}.exe
|
WINDOWS_BINARY2=${MYNAME2}.exe
|
||||||
EXAMPLE01=01-LoadJsonFile
|
EXAMPLE01=01-LoadJsonFile
|
||||||
EXAMPLE02=02-TestLuaScript
|
EXAMPLE02=02-TestLuaScript
|
||||||
|
EXAMPLE03=03-UseGoFuncInLua
|
||||||
|
|
||||||
RMCMD=rm -rf
|
RMCMD=rm -rf
|
||||||
|
|
||||||
@@ -71,29 +72,34 @@ clean:
|
|||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),Windows_NT)
|
||||||
ifeq ($(SHELL),sh.exe)
|
ifeq ($(SHELL),sh.exe)
|
||||||
# # Vanilla cmd.exe / powershell.
|
# # Vanilla cmd.exe / powershell.
|
||||||
if exist 01-LoadJsonFile.exe ${RMCMD} ${WINDOWS_BINARY1}
|
if exist ${WINDOWS_BINARY1} ${RMCMD} ${WINDOWS_BINARY1}
|
||||||
if exist 01-LoadJsonFile.exe ${RMCMD} ${UNIX_BINARY1}
|
if exist ${UNIX_BINARY1} ${RMCMD} ${UNIX_BINARY1}
|
||||||
if exist 01-LoadJsonFile.exe ${RMCMD} ${WINDOWS_BINARY2}
|
if exist ${WINDOWS_BINARY2} ${RMCMD} ${WINDOWS_BINARY2}
|
||||||
if exist 01-LoadJsonFile.exe ${RMCMD} ${UNIX_BINARY2}
|
if exist ${UNIX_BINARY2} ${RMCMD} ${UNIX_BINARY2}
|
||||||
if exist 01-LoadJsonFile.exe ${RMCMD} 01-LoadJsonFile.exe
|
if exist ${EXAMPLE01}.exe ${RMCMD} ${EXAMPLE01}.exe
|
||||||
if exist 01-LoadJsonFile ${RMCMD} 01-LoadJsonFile
|
if exist ${EXAMPLE01} ${RMCMD} ${EXAMPLE01}
|
||||||
if exist 02-TestLuaScript.exe ${RMCMD} 02-TestLuaScript.exe
|
if exist ${EXAMPLE02}.exe ${RMCMD} ${EXAMPLE02}.exe
|
||||||
if exist 02-TestLuaScript ${RMCMD} 02-TestLuaScript
|
if exist ${EXAMPLE02} ${RMCMD} ${EXAMPLE02}
|
||||||
|
if exist ${EXAMPLE03}.exe ${RMCMD} ${EXAMPLE03}.exe
|
||||||
|
if exist ${EXAMPLE03} ${RMCMD} ${EXAMPLE03}
|
||||||
else # not ifeq ($(SHELL),sh.exe)
|
else # not ifeq ($(SHELL),sh.exe)
|
||||||
${RMCMD} ${WINDOWS_BINARY1}
|
${RMCMD} ${WINDOWS_BINARY1}
|
||||||
${RMCMD} ${UNIX_BINARY1}
|
${RMCMD} ${UNIX_BINARY1}
|
||||||
${RMCMD} ${WINDOWS_BINARY2}
|
${RMCMD} ${WINDOWS_BINARY2}
|
||||||
${RMCMD} ${UNIX_BINARY2}
|
${RMCMD} ${UNIX_BINARY2}
|
||||||
${RMCMD} ./01-LoadJsonFile.exe
|
${RMCMD} ${EXAMPLE01}.exe
|
||||||
${RMCMD} ./01-LoadJsonFile
|
${RMCMD} ${EXAMPLE01}
|
||||||
${RMCMD} ./02-TestLuaScript.exe
|
${RMCMD} ${EXAMPLE02}.exe
|
||||||
${RMCMD} ./02-TestLuaScript
|
${RMCMD} ${EXAMPLE02}
|
||||||
|
${RMCMD} ${EXAMPLE03}.exe
|
||||||
|
${RMCMD} ${EXAMPLE03}
|
||||||
endif # ifeq ($(SHELL),sh.exe)
|
endif # ifeq ($(SHELL),sh.exe)
|
||||||
else # not ifeq ($(OS),Windows_NT)
|
else # not ifeq ($(OS),Windows_NT)
|
||||||
${RMCMD} ${BINARY1}
|
${RMCMD} ${BINARY1}
|
||||||
${RMCMD} ${BINARY2}
|
${RMCMD} ${BINARY2}
|
||||||
${RMCMD} ./01-LoadJsonFile
|
${RMCMD} ${EXAMPLE01}
|
||||||
${RMCMD} ./02-TestLuaScript
|
${RMCMD} ${EXAMPLE02}
|
||||||
|
${RMCMD} ${EXAMPLE03}
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
|
||||||
@@ -103,7 +109,7 @@ upgrade:
|
|||||||
go mod vendor
|
go mod vendor
|
||||||
|
|
||||||
|
|
||||||
01-LoadJsonFile: clean
|
${EXAMPLE01}: clean
|
||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),Windows_NT)
|
||||||
# Looks like on windows gnu make explicitly set SHELL to sh.exe, if it was not set.
|
# Looks like on windows gnu make explicitly set SHELL to sh.exe, if it was not set.
|
||||||
ifeq ($(SHELL), sh.exe)
|
ifeq ($(SHELL), sh.exe)
|
||||||
@@ -123,7 +129,7 @@ else # not ($(OS),Windows_NT)
|
|||||||
endif # ifeq ($(OS),Windows_NT)
|
endif # ifeq ($(OS),Windows_NT)
|
||||||
|
|
||||||
|
|
||||||
02-TestLuaScript: clean
|
${EXAMPLE02}: clean
|
||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),Windows_NT)
|
||||||
# Looks like on windows gnu make explicitly set SHELL to sh.exe, if it was not set.
|
# Looks like on windows gnu make explicitly set SHELL to sh.exe, if it was not set.
|
||||||
ifeq ($(SHELL), sh.exe)
|
ifeq ($(SHELL), sh.exe)
|
||||||
@@ -143,4 +149,24 @@ else # not ($(OS),Windows_NT)
|
|||||||
endif # ifeq ($(OS),Windows_NT)
|
endif # ifeq ($(OS),Windows_NT)
|
||||||
|
|
||||||
|
|
||||||
|
${EXAMPLE03}: clean
|
||||||
|
ifeq ($(OS),Windows_NT)
|
||||||
|
# Looks like on windows gnu make explicitly set SHELL to sh.exe, if it was not set.
|
||||||
|
ifeq ($(SHELL), sh.exe)
|
||||||
|
# # Vanilla cmd.exe / powershell.
|
||||||
|
SET "CGO_ENABLED=0"
|
||||||
|
go build ${BUILDOPTS} -o ${EXAMPLE03}.exe ./examples/${EXAMPLE03}
|
||||||
|
else ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
|
||||||
|
# # git-bash
|
||||||
|
CGO_ENABLED=0 go build ${BUILDOPTS} -o ${EXAMPLE03}.exe ./examples/${EXAMPLE03}
|
||||||
|
else # not ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
|
||||||
|
# # Some other shell.
|
||||||
|
# # TODO: handle it.
|
||||||
|
$(info "-- Dunno how to handle this shell: ${SHELL}")
|
||||||
|
endif # ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
|
||||||
|
else # not ($(OS),Windows_NT)
|
||||||
|
CGO_ENABLED=0 go build ${BUILDOPTS} -o ${EXAMPLE03} ./examples/${EXAMPLE03}
|
||||||
|
endif # ifeq ($(OS),Windows_NT)
|
||||||
|
|
||||||
|
|
||||||
# vim: set ft=make noet ai ts=4 sw=4 sts=4:
|
# vim: set ft=make noet ai ts=4 sw=4 sts=4:
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
print ("Hello World!")
|
print ("Hello World!")
|
||||||
|
|
||||||
-- vim: set ft=go noet ai ts=4 sw=4 sts=4:
|
-- vim: set ft=lua noet ai ts=4 sw=4 sts=4:
|
||||||
|
|||||||
3
example_data/03-test.lua
Normal file
3
example_data/03-test.lua
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
print("4 * 4 = " .. square(4))
|
||||||
|
|
||||||
|
-- vim: set ft=lua noet ai ts=4 sw=4 sts=4:
|
||||||
@@ -8,11 +8,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
file := "example_data/02-test.lua"
|
// Init log subsystem.
|
||||||
log.Init("info", os.Stderr)
|
log.Init("info", os.Stderr)
|
||||||
|
|
||||||
L := lua.NewState()
|
file := "example_data/02-test.lua"
|
||||||
defer L.Close()
|
|
||||||
|
|
||||||
// Load the Lua script file.
|
// Load the Lua script file.
|
||||||
log.Infof("Loading %s", file)
|
log.Infof("Loading %s", file)
|
||||||
@@ -22,11 +21,14 @@ func main() {
|
|||||||
luaCode, err := os.ReadFile(file)
|
luaCode, err := os.ReadFile(file)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error reading Lua file %s: %s", file, err)
|
log.Fatalf("Error reading Lua file %s: %s", file, err)
|
||||||
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Info("Creating new Lua state")
|
||||||
|
L := lua.NewState()
|
||||||
|
defer L.Close()
|
||||||
|
|
||||||
|
log.Info("Evaluating Lua code")
|
||||||
if err := L.DoString(string(luaCode)); err != nil {
|
if err := L.DoString(string(luaCode)); err != nil {
|
||||||
log.Errorf("Error evaluating Lua code: %s", err)
|
log.Errorf("Error evaluating Lua code: %s", err)
|
||||||
|
|
||||||
|
|||||||
59
examples/03-UseGoFuncInLua/main.go
Normal file
59
examples/03-UseGoFuncInLua/main.go
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// https://otm.github.io/2015/07/embedding-lua-in-go/
|
||||||
|
|
||||||
|
import (
|
||||||
|
"doll/internal/log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
lua "github.com/yuin/gopher-lua"
|
||||||
|
)
|
||||||
|
|
||||||
|
// square multiplies number by itself.
|
||||||
|
func square(L *lua.LState) int {
|
||||||
|
// get first (1) function argument and convert to int
|
||||||
|
i := L.ToInt(1)
|
||||||
|
|
||||||
|
// make calculation and cast to LNumber
|
||||||
|
ln := lua.LNumber(i * i)
|
||||||
|
|
||||||
|
// Push it to the stack
|
||||||
|
L.Push(ln)
|
||||||
|
|
||||||
|
// Notify that we pushed one value to the stack
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Init log subsystem.
|
||||||
|
log.Init("info", os.Stderr)
|
||||||
|
|
||||||
|
file := "example_data/03-test.lua"
|
||||||
|
|
||||||
|
// Load the Lua script file.
|
||||||
|
log.Infof("Loading %s", file)
|
||||||
|
|
||||||
|
// We can execute script directly via L.DoFile("./example_data/03-test.lua") but in practice we'll get scripts as
|
||||||
|
// strings - snippets from templates - so, testing/checking how it works is not use for project.
|
||||||
|
luaCode, err := os.ReadFile(file)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error reading Lua file %s: %s", file, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info("Creating new Lua state")
|
||||||
|
L := lua.NewState()
|
||||||
|
defer L.Close()
|
||||||
|
|
||||||
|
log.Info("Registering go function as lua built-in function")
|
||||||
|
L.SetGlobal("square", L.NewFunction(square))
|
||||||
|
|
||||||
|
log.Info("Evaluating Lua code")
|
||||||
|
if err := L.DoString(string(luaCode)); err != nil {
|
||||||
|
log.Errorf("Error evaluating Lua code: %s", err)
|
||||||
|
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */
|
||||||
Reference in New Issue
Block a user