fix: implement go lua library loading

and remove debug symbol stripping flags when building
on midnight
midnight-edition
sammyette 2024-12-22 01:08:56 -04:00
parent 3afd1c518a
commit 0a0f2e2c38
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
6 changed files with 30 additions and 16 deletions

View File

@ -30,7 +30,7 @@ tasks:
cmds: cmds:
- go build -tags midnight,{{.LUA}} {{.GOFLAGS}} - go build -tags midnight,{{.LUA}} {{.GOFLAGS}}
vars: 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: build:
cmds: cmds:

3
api.go
View File

@ -61,6 +61,7 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value {
*/ */
} }
hshMod = moonlight.NewTable() hshMod = moonlight.NewTable()
hshMod.SetRuntime(mlr)
mlr.SetExports(hshMod, exports) mlr.SetExports(hshMod, exports)
host, _ := os.Hostname() 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 username = strings.Split(username, "\\")[1] // for some reason Username includes the hostname on windows
} }
println("setting ver field")
hshMod.SetField("ver", moonlight.StringValue(getVersion())) hshMod.SetField("ver", moonlight.StringValue(getVersion()))
println("setting goversion field")
hshMod.SetField("goVersion", moonlight.StringValue(runtime.Version())) hshMod.SetField("goVersion", moonlight.StringValue(runtime.Version()))
hshMod.SetField("user", moonlight.StringValue(username)) hshMod.SetField("user", moonlight.StringValue(username))
hshMod.SetField("host", moonlight.StringValue(host)) hshMod.SetField("host", moonlight.StringValue(host))

2
lua.go
View File

@ -53,7 +53,7 @@ func luaInit() {
// Add more paths that Lua can require from // 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)); 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 { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, "Could not add Hilbish require paths! Libraries will be missing. This shouldn't happen.") fmt.Fprintln(os.Stderr, "Could not add Hilbish require paths! Libraries will be missing. This shouldn't happen.")

View File

@ -18,5 +18,4 @@ func (mlr *Runtime) LoadLibrary(ldr Loader, name string) {
mlr.state.GetField(-1, "preload") mlr.state.GetField(-1, "preload")
mlr.state.PushGoClosure(cluaLoader) mlr.state.PushGoClosure(cluaLoader)
mlr.state.SetField(-2, name) mlr.state.SetField(-2, name)
mlr.state.Pop(1)
} }

View File

@ -48,7 +48,7 @@ func (mlr *Runtime) pushToState(v Value) {
case TableType: case TableType:
tbl := v.AsTable() tbl := v.AsTable()
tbl.SetRuntime(mlr) tbl.SetRuntime(mlr)
mlr.state.RawGeti(lua.LUA_REGISTRYINDEX, tbl.refIdx) tbl.Push()
default: mlr.state.PushNil() default: mlr.state.PushNil()
} }
} }

View File

@ -1,13 +1,12 @@
//go:build midnight //go:build midnight
package moonlight package moonlight
import ( import (
"fmt"
"github.com/aarzilli/golua/lua" "github.com/aarzilli/golua/lua"
) )
type Table struct{ type Table struct {
refIdx int refIdx int
mlr *Runtime mlr *Runtime
nativeFields map[Value]Value nativeFields map[Value]Value
@ -26,6 +25,8 @@ func (t *Table) SetRuntime(mlr *Runtime) {
if t.refIdx == -1 { if t.refIdx == -1 {
mlr.state.NewTable() mlr.state.NewTable()
t.refIdx = mlr.state.Ref(lua.LUA_REGISTRYINDEX) t.refIdx = mlr.state.Ref(lua.LUA_REGISTRYINDEX)
t.Push() // because Ref pops off the stack
t.syncToLua()
mlr.state.Pop(1) mlr.state.Pop(1)
} }
} }
@ -39,20 +40,35 @@ func (t *Table) Push() {
} }
func (t *Table) SetField(key string, value Value) { 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() t.Push()
defer t.mlr.state.Pop(1) defer t.mlr.state.Pop(1)
t.mlr.pushToState(value) t.mlr.pushToState(value)
t.mlr.state.SetField(-1, key) t.mlr.state.SetField(-2, key)
t.mlr.state.Pop(1) }
println("what")
func (t *Table) setInGo(key string, value Value) {
t.nativeFields[StringValue(key)] = value
} }
func (t *Table) Set(key Value, value Value) { func (t *Table) Set(key Value, value Value) {
t.nativeFields[key] = 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)) { func ForEach(tbl *Table, cb func(key Value, val Value)) {
} }