From 0a0f2e2c38a1b44fb0506e92f0042fd96d7cc46f Mon Sep 17 00:00:00 2001 From: sammyette Date: Sun, 22 Dec 2024 01:08:56 -0400 Subject: [PATCH] fix: implement go lua library loading and remove debug symbol stripping flags when building on midnight --- Taskfile.yaml | 2 +- api.go | 3 +-- lua.go | 2 +- moonlight/loader_clua.go | 1 - moonlight/runtime_clua.go | 2 +- moonlight/table_clua.go | 36 ++++++++++++++++++++++++++---------- 6 files changed, 30 insertions(+), 16 deletions(-) diff --git a/Taskfile.yaml b/Taskfile.yaml index 04f7667..8e885ab 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -30,7 +30,7 @@ tasks: cmds: - go build -tags midnight,{{.LUA}} {{.GOFLAGS}} vars: - GOFLAGS: '-ldflags "-s -w -X main.dataDir={{.LIBDIR}} -X main.gitCommit=$(git rev-parse --short HEAD) -X main.gitBranch=$(git rev-parse --abbrev-ref HEAD)"' + GOFLAGS: '-ldflags "-X main.dataDir={{.LIBDIR}} -X main.gitCommit=$(git rev-parse --short HEAD) -X main.gitBranch=$(git rev-parse --abbrev-ref HEAD)"' build: cmds: diff --git a/api.go b/api.go index f47df04..697b523 100644 --- a/api.go +++ b/api.go @@ -61,6 +61,7 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value { */ } hshMod = moonlight.NewTable() + hshMod.SetRuntime(mlr) mlr.SetExports(hshMod, exports) host, _ := os.Hostname() @@ -70,9 +71,7 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value { username = strings.Split(username, "\\")[1] // for some reason Username includes the hostname on windows } - println("setting ver field") hshMod.SetField("ver", moonlight.StringValue(getVersion())) - println("setting goversion field") hshMod.SetField("goVersion", moonlight.StringValue(runtime.Version())) hshMod.SetField("user", moonlight.StringValue(username)) hshMod.SetField("host", moonlight.StringValue(host)) diff --git a/lua.go b/lua.go index 117e972..8b41deb 100644 --- a/lua.go +++ b/lua.go @@ -53,7 +53,7 @@ func luaInit() { // Add more paths that Lua can require from //_, err := l.DoString("print(type(hilbish)); package.path = package.path .. " + requirePaths) - _, err := l.DoString("print(type(hilbish)); print(hilbish.userDir.config)") + _, err := l.DoString("print(type(hilbish)); print(hilbish); print(hilbish.userDir.config)") if err != nil { fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, "Could not add Hilbish require paths! Libraries will be missing. This shouldn't happen.") diff --git a/moonlight/loader_clua.go b/moonlight/loader_clua.go index 4fbcf6d..84ec395 100644 --- a/moonlight/loader_clua.go +++ b/moonlight/loader_clua.go @@ -18,5 +18,4 @@ func (mlr *Runtime) LoadLibrary(ldr Loader, name string) { mlr.state.GetField(-1, "preload") mlr.state.PushGoClosure(cluaLoader) mlr.state.SetField(-2, name) - mlr.state.Pop(1) } diff --git a/moonlight/runtime_clua.go b/moonlight/runtime_clua.go index c8b4d86..e42e186 100644 --- a/moonlight/runtime_clua.go +++ b/moonlight/runtime_clua.go @@ -48,7 +48,7 @@ func (mlr *Runtime) pushToState(v Value) { case TableType: tbl := v.AsTable() tbl.SetRuntime(mlr) - mlr.state.RawGeti(lua.LUA_REGISTRYINDEX, tbl.refIdx) + tbl.Push() default: mlr.state.PushNil() } } diff --git a/moonlight/table_clua.go b/moonlight/table_clua.go index 28a5281..58cbcac 100644 --- a/moonlight/table_clua.go +++ b/moonlight/table_clua.go @@ -1,21 +1,20 @@ //go:build midnight + package moonlight import ( - "fmt" - "github.com/aarzilli/golua/lua" ) -type Table struct{ - refIdx int - mlr *Runtime +type Table struct { + refIdx int + mlr *Runtime nativeFields map[Value]Value } func NewTable() *Table { return &Table{ - refIdx: -1, + refIdx: -1, nativeFields: make(map[Value]Value), } } @@ -26,6 +25,8 @@ func (t *Table) SetRuntime(mlr *Runtime) { if t.refIdx == -1 { mlr.state.NewTable() t.refIdx = mlr.state.Ref(lua.LUA_REGISTRYINDEX) + t.Push() // because Ref pops off the stack + t.syncToLua() mlr.state.Pop(1) } } @@ -39,20 +40,35 @@ func (t *Table) Push() { } func (t *Table) SetField(key string, value Value) { - fmt.Printf("key: %s, value: %s\n", key, value.TypeName()) + if t.refIdx != -1 { + t.setInLua(key, value) + return + } + + t.setInGo(key, value) +} + +func (t *Table) setInLua(key string, value Value) { t.Push() defer t.mlr.state.Pop(1) t.mlr.pushToState(value) - t.mlr.state.SetField(-1, key) - t.mlr.state.Pop(1) - println("what") + t.mlr.state.SetField(-2, key) +} + +func (t *Table) setInGo(key string, value Value) { + t.nativeFields[StringValue(key)] = value } func (t *Table) Set(key Value, value Value) { t.nativeFields[key] = value } +func (t *Table) syncToLua() { + for k, v := range t.nativeFields { + t.SetField(k.AsString(), v) + } +} func ForEach(tbl *Table, cb func(key Value, val Value)) { }