Compare commits

..

2 Commits

10 changed files with 64 additions and 160 deletions

4
.gitignore vendored
View File

@@ -1,6 +1,2 @@
/01-LoadJsonFile /01-LoadJsonFile
/02-TestLuaScript
/03-UseGoFuncInLua
/doll
/doll-master
/*.exe /*.exe

View File

@@ -2,20 +2,21 @@
GOOPTS=CGO_ENABLED=0 GOOPTS=CGO_ENABLED=0
BUILDOPTS=-ldflags="-s -w" -a -gcflags=all=-l -trimpath -buildvcs=false BUILDOPTS=-ldflags="-s -w" -a -gcflags=all=-l -trimpath -buildvcs=false
MYNAME1=doll-master MYNAME1=doll
MYNAME2=doll-master
BINARY1=${MYNAME1} BINARY1=${MYNAME1}
UNIX_BINARY1=${MYNAME1}
WINDOWS_BINARY1=${MYNAME1}.exe
MYNAME2=doll
BINARY2=${MYNAME2} BINARY2=${MYNAME2}
UNIX_BINARY1=${MYNAME1}
UNIX_BINARY2=${MYNAME2} UNIX_BINARY2=${MYNAME2}
WINDOWS_BINARY1=${MYNAME1}.exe
WINDOWS_BINARY2=${MYNAME2}.exe WINDOWS_BINARY2=${MYNAME2}.exe
RMCMD=rm -rf
EXAMPLE01=01-LoadJsonFile EXAMPLE01=01-LoadJsonFile
EXAMPLE02=02-TestLuaScript EXAMPLE02=02-TestLuaScript
EXAMPLE03=03-UseGoFuncInLua
# On windows binary name can depend not only on platform selected, but target too, we need no .exe suffix for linux bin. RMCMD=rm -rf
# На windows имя бинарника может зависеть не только от платформы, но и от выбранной цели, для linux-а суффикс .exe
# не нужен
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
ifdef GOOS ifdef GOOS
ifeq ($(GOOS),windows) ifeq ($(GOOS),windows)
@@ -34,7 +35,7 @@ RMCMD=DEL /Q /F
endif endif
endif endif
# Set newline symbol explicitly to aboud undefined behaviour in windows. # Явно определяем символ новой строки, чтобы избежать неоднозначности на windows
define IFS define IFS
endef endef
@@ -62,7 +63,7 @@ else # not ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
endif # ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL)))) endif # ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
else # not ($(OS),Windows_NT) else # not ($(OS),Windows_NT)
CGO_ENABLED=0 go build ${BUILDOPTS} -o ${BINARY1} ./cmd/${MYNAME1} CGO_ENABLED=0 go build ${BUILDOPTS} -o ${BINARY1} ./cmd/${MYNAME1}
CGO_ENABLED=0 go build ${BUILDOPTS} -o ${BINARY1} ./cmd/${MYNAME2} CGO_ENABLED=0 go build ${BUILDOPTS} -o ${BINARY2} ./cmd/${MYNAME2}
endif # ifeq ($(OS),Windows_NT) endif # ifeq ($(OS),Windows_NT)
@@ -70,33 +71,29 @@ 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 ${WINDOWS_BINARY1} ${RMCMD} ${WINDOWS_BINARY1} if exist 01-LoadJsonFile.exe ${RMCMD} ${WINDOWS_BINARY1}
if exist ${UNIX_BINARY1} ${RMCMD} ${UNIX_BINARY1} if exist 01-LoadJsonFile.exe ${RMCMD} ${UNIX_BINARY1}
if exist ${WINDOWS_BINARY2} ${RMCMD} ${WINDOWS_BINARY2} if exist 01-LoadJsonFile.exe ${RMCMD} ${WINDOWS_BINARY2}
if exist ${EXAMPLE01}.exe ${RMCMD} ${EXAMPLE01}.exe if exist 01-LoadJsonFile.exe ${RMCMD} ${UNIX_BINARY2}
if exist ${EXAMPLE01} ${RMCMD} ${EXAMPLE01} if exist 01-LoadJsonFile.exe ${RMCMD} 01-LoadJsonFile.exe
if exist ${EXAMPLE02}.exe ${RMCMD} ${EXAMPLE02}.exe if exist 01-LoadJsonFile ${RMCMD} 01-LoadJsonFile
if exist ${EXAMPLE02} ${RMCMD} ${EXAMPLE02} if exist 02-TestLuaScript.exe ${RMCMD} 02-TestLuaScript.exe
if exist ${EXAMPLE03}.exe ${RMCMD} ${EXAMPLE03}.exe if exist 02-TestLuaScript ${RMCMD} 02-TestLuaScript
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} ./${EXAMPLE01}.exe ${RMCMD} ./01-LoadJsonFile.exe
${RMCMD} ./${EXAMPLE01} ${RMCMD} ./01-LoadJsonFile
${RMCMD} ./${EXAMPLE02}.exe ${RMCMD} ./02-TestLuaScript.exe
${RMCMD} ./${EXAMPLE02} ${RMCMD} ./02-TestLuaScript
${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} ./${UNIX_BINARY1} ${RMCMD} ${BINARY1}
${RMCMD} ./${UNIX_BINARY2} ${RMCMD} ${BINARY2}
${RMCMD} ${EXAMPLE01} ${RMCMD} ./01-LoadJsonFile
${RMCMD} ${EXAMPLE02} ${RMCMD} ./02-TestLuaScript
${RMCMD} ${EXAMPLE03}
endif endif
@@ -106,63 +103,43 @@ upgrade:
go mod vendor go mod vendor
${EXAMPLE01}: clean 01-LoadJsonFile: 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)
# # Vanilla cmd.exe / powershell. # # Vanilla cmd.exe / powershell.
SET "CGO_ENABLED=0" SET "CGO_ENABLED=0"
go build ${BUILDOPTS} -o ${EXAMPLE01}.exe ./cmd/${EXAMPLE01} go build ${BUILDOPTS} -o ${EXAMPLE01}.exe ./examples/${EXAMPLE01}
else ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL)))) else ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
# # git-bash # # git-bash
CGO_ENABLED=0 go build ${BUILDOPTS} -o ${EXAMPLE01}.exe ./cmd/${EXAMPLE01} CGO_ENABLED=0 go build ${BUILDOPTS} -o ${EXAMPLE01}.exe ./examples/${EXAMPLE01}
else # not ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL)))) else # not ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
# # Some other shell. # # Some other shell.
# # TODO: handle it. # # TODO: handle it.
$(info "-- Dunno how to handle this shell: ${SHELL}") $(info "-- Dunno how to handle this shell: ${SHELL}")
endif # ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL)))) endif # ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
else # not ($(OS),Windows_NT) else # not ($(OS),Windows_NT)
CGO_ENABLED=0 go build ${BUILDOPTS} -o ${EXAMPLE01} ./cmd/${EXAMPLE01} CGO_ENABLED=0 go build ${BUILDOPTS} -o ${EXAMPLE01} ./examples/${EXAMPLE01}
endif # ifeq ($(OS),Windows_NT) endif # ifeq ($(OS),Windows_NT)
${EXAMPLE02}: clean 02-TestLuaScript: 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)
# # Vanilla cmd.exe / powershell. # # Vanilla cmd.exe / powershell.
SET "CGO_ENABLED=0" SET "CGO_ENABLED=0"
go build ${BUILDOPTS} -o ${EXAMPLE02}.exe ./cmd/${EXAMPLE02} go build ${BUILDOPTS} -o ${EXAMPLE02}.exe ./examples/${EXAMPLE02}
else ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL)))) else ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
# # git-bash # # git-bash
CGO_ENABLED=0 go build ${BUILDOPTS} -o ${EXAMPLE02}.exe ./cmd/${EXAMPLE02} CGO_ENABLED=0 go build ${BUILDOPTS} -o ${EXAMPLE02}.exe ./examples/${EXAMPLE02}
else # not ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL)))) else # not ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
# # Some other shell. # # Some other shell.
# # TODO: handle it. # # TODO: handle it.
$(info "-- Dunno how to handle this shell: ${SHELL}") $(info "-- Dunno how to handle this shell: ${SHELL}")
endif # ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL)))) endif # ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
else # not ($(OS),Windows_NT) else # not ($(OS),Windows_NT)
CGO_ENABLED=0 go build ${BUILDOPTS} -o ${EXAMPLE02} ./cmd/${EXAMPLE02} CGO_ENABLED=0 go build ${BUILDOPTS} -o ${EXAMPLE02} ./examples/${EXAMPLE02}
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 ./cmd/${EXAMPLE03}
else ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
# # git-bash
CGO_ENABLED=0 go build ${BUILDOPTS} -o ${EXAMPLE03}.exe ./cmd/${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} ./cmd/${EXAMPLE03}
endif # ifeq ($(OS),Windows_NT) endif # ifeq ($(OS),Windows_NT)

View File

@@ -1,3 +1,3 @@
print ("Hello World!") print ("Hello World!")
-- vim: set ft=lua noet ai ts=4 sw=4 sts=4: -- vim: set ft=go noet ai ts=4 sw=4 sts=4:

View File

@@ -1,3 +0,0 @@
print("4 * 4 = " .. square(4))
-- vim: set ft=lua noet ai ts=4 sw=4 sts=4:

View File

@@ -9,21 +9,14 @@ import (
) )
func main() { func main() {
// Init log subsystem.
log.Init("info", os.Stderr)
file := "example_data/01-config.json" file := "example_data/01-config.json"
// Load the Lua script file. log.Init("info", os.Stderr)
log.Infof("Loading %s", file) log.Infof("loading %s", file)
data, err := doll.LoadJsonFile(file) data := doll.LoadJsonFile(file)
if err != nil { log.Infof("data from %s", file)
log.Fatalf("Unable to unmarshal %s to map: %s", file, err)
}
log.Infof("Map unmarshalled from %s", file)
spew.Dump(data) spew.Dump(data)
} }

View File

@@ -8,10 +8,11 @@ import (
) )
func main() { func main() {
// Init log subsystem. file := "example_data/02-test.lua"
log.Init("info", os.Stderr) log.Init("info", os.Stderr)
file := "example_data/02-test.lua" L := lua.NewState()
defer L.Close()
// Load the Lua script file. // Load the Lua script file.
log.Infof("Loading %s", file) log.Infof("Loading %s", file)
@@ -21,16 +22,15 @@ func main() {
luaCode, err := os.ReadFile(file) luaCode, err := os.ReadFile(file)
if err != nil { if err != nil {
log.Fatalf("Error reading Lua file %s: %s", file, err) log.Errorf("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.Fatalf("Error evaluating Lua code: %s", err) log.Errorf("Error evaluating Lua code: %s", err)
os.Exit(1)
} }
} }

View File

@@ -1,59 +0,0 @@
package main
// This exmple taken from 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: */

View File

@@ -1,30 +1,30 @@
package doll package doll
import ( import (
"doll/internal/log"
"encoding/json" "encoding/json"
"fmt"
"os" "os"
) )
// LoadJsonFile loads data from json to Instance global variable map. // LoadJsonFile loads data from json to Instance global variable map.
func LoadJsonFile(file string) (map[string]any, error) { func LoadJsonFile(file string) map[string]any {
var ( var (
err error err error
data []byte data []byte
myMap map[string]any
) )
data, err = os.ReadFile(file) data, err = os.ReadFile(file)
if err != nil { if err != nil {
return myMap, fmt.Errorf("Unable to load %s: %w", file, err) log.Fatalf("Unable to load %s: %s", file, err)
} }
if err = json.Unmarshal([]byte(data), &myMap); err != nil { err = json.Unmarshal([]byte(data), &Instance)
return myMap, fmt.Errorf("Unable to unmarshal JSON data from %s: %w", file, err) if err != nil {
log.Fatalf("Unable to marshal JSON data from %s: %s", file, err)
} }
return myMap, err return Instance
} }
/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */ /* vim: set ft=go noet ai ts=4 sw=4 sts=4: */

View File

@@ -1,4 +1,4 @@
package serverSonfig package serverConfig
import ( import (
"encoding/json" "encoding/json"