fix: attempt to implement more/fix go library loading

midnight-edition
sammyette 2024-12-22 00:03:21 -04:00
parent 094e795d01
commit 3afd1c518a
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
7 changed files with 55 additions and 19 deletions

18
api.go
View File

@ -39,25 +39,19 @@ var hshMod *moonlight.Table
func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value { func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value {
println("hilbish loader called") println("hilbish loader called")
var exports = map[string]moonlight.Export{ var exports = map[string]moonlight.Export{
/*
"alias": {hlalias, 2, false}, "alias": {hlalias, 2, false},
"appendPath": {hlappendPath, 1, false}, "appendPath": {hlappendPath, 1, false},
/*
"complete": {hlcomplete, 2, false}, "complete": {hlcomplete, 2, false},
*/
"cwd": {hlcwd, 0, false}, "cwd": {hlcwd, 0, false},
/*
"exec": {hlexec, 1, false}, "exec": {hlexec, 1, false},
*/
"runnerMode": {hlrunnerMode, 1, false}, "runnerMode": {hlrunnerMode, 1, false},
/*
"goro": {hlgoro, 1, true}, "goro": {hlgoro, 1, true},
"highlighter": {hlhighlighter, 1, false}, "highlighter": {hlhighlighter, 1, false},
"hinter": {hlhinter, 1, false}, "hinter": {hlhinter, 1, false},
"multiprompt": {hlmultiprompt, 1, false}, "multiprompt": {hlmultiprompt, 1, false},
"prependPath": {hlprependPath, 1, false}, "prependPath": {hlprependPath, 1, false},
*/
"prompt": {hlprompt, 1, true}, "prompt": {hlprompt, 1, true},
/*
"inputMode": {hlinputMode, 1, false}, "inputMode": {hlinputMode, 1, false},
"interval": {hlinterval, 2, false}, "interval": {hlinterval, 2, false},
"read": {hlread, 1, false}, "read": {hlread, 1, false},
@ -76,7 +70,9 @@ 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))
@ -111,8 +107,8 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value {
//mod.Set(rt.StringValue("completions"), rt.TableValue(hshcomp)) //mod.Set(rt.StringValue("completions"), rt.TableValue(hshcomp))
// hilbish.runner table // hilbish.runner table
runnerModule := runnerModeLoader(mlr) //runnerModule := runnerModeLoader(mlr)
hshMod.SetField("runner", moonlight.TableValue(runnerModule)) //hshMod.SetField("runner", moonlight.TableValue(runnerModule))
// hilbish.jobs table // hilbish.jobs table
jobs = newJobHandler() jobs = newJobHandler()
@ -135,8 +131,8 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value {
//mod.Set(rt.StringValue("version"), rt.TableValue(versionModule)) //mod.Set(rt.StringValue("version"), rt.TableValue(versionModule))
// very meta // very meta
moduleModule := moduleLoader(mlr) //moduleModule := moduleLoader(mlr)
hshMod.SetField("module", moonlight.TableValue(moduleModule)) //hshMod.SetField("module", moonlight.TableValue(moduleModule))
return moonlight.TableValue(hshMod) return moonlight.TableValue(hshMod)
} }

View File

@ -33,6 +33,7 @@ func New(runner *interp.Runner) *fs {
} }
func (f *fs) Loader(rtm *moonlight.Runtime) moonlight.Value { func (f *fs) Loader(rtm *moonlight.Runtime) moonlight.Value {
println("fs loader called")
exports := map[string]moonlight.Export{ exports := map[string]moonlight.Export{
/* /*
"cd": util.LuaExport{f.fcd, 1, false}, "cd": util.LuaExport{f.fcd, 1, false},

3
lua.go
View File

@ -52,7 +52,8 @@ 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)")
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

@ -15,7 +15,8 @@ func (mlr *Runtime) LoadLibrary(ldr Loader, name string) {
} }
mlr.state.GetGlobal("package") mlr.state.GetGlobal("package")
mlr.state.GetField(-1, "loaded") mlr.state.GetField(-1, "preload")
mlr.state.PushGoFunction(cluaLoader) mlr.state.PushGoClosure(cluaLoader)
mlr.state.SetField(-2, name) mlr.state.SetField(-2, name)
mlr.state.Pop(1)
} }

View File

@ -45,5 +45,10 @@ func (mlr *Runtime) pushToState(v Value) {
switch v.Type() { switch v.Type() {
case NilType: mlr.state.PushNil() case NilType: mlr.state.PushNil()
case StringType: mlr.state.PushString(v.AsString()) case StringType: mlr.state.PushString(v.AsString())
case TableType:
tbl := v.AsTable()
tbl.SetRuntime(mlr)
mlr.state.RawGeti(lua.LUA_REGISTRYINDEX, tbl.refIdx)
default: mlr.state.PushNil()
} }
} }

View File

@ -1,15 +1,32 @@
//go:build midnight //go:build midnight
package moonlight package moonlight
//import "github.com/aarzilli/golua/lua" import (
"fmt"
"github.com/aarzilli/golua/lua"
)
type Table struct{ type Table struct{
refIdx int refIdx int
mlr *Runtime
nativeFields map[Value]Value
} }
func NewTable() *Table { func NewTable() *Table {
return &Table{ return &Table{
refIdx: -1, refIdx: -1,
nativeFields: make(map[Value]Value),
}
}
func (t *Table) SetRuntime(mlr *Runtime) {
t.mlr = mlr
if t.refIdx == -1 {
mlr.state.NewTable()
t.refIdx = mlr.state.Ref(lua.LUA_REGISTRYINDEX)
mlr.state.Pop(1)
} }
} }
@ -17,18 +34,32 @@ func (t *Table) Get(val Value) Value {
return NilValue return NilValue
} }
func (t *Table) Push() {
t.mlr.state.RawGeti(lua.LUA_REGISTRYINDEX, t.refIdx)
}
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())
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")
} }
func (t *Table) Set(key Value, value Value) { func (t *Table) Set(key Value, value Value) {
t.nativeFields[key] = value
} }
func ForEach(tbl *Table, cb func(key Value, val Value)) { func ForEach(tbl *Table, cb func(key Value, val Value)) {
} }
func (mlr *Runtime) GlobalTable() *Table { func (mlr *Runtime) GlobalTable() *Table {
mlr.state.GetGlobal("_G")
return &Table{ return &Table{
refIdx: -1, refIdx: mlr.state.Ref(lua.LUA_REGISTRYINDEX),
} }
} }

View File

@ -67,7 +67,7 @@ func (v Value) Type() ValueType {
switch v.iface.(type) { switch v.iface.(type) {
case bool: return BoolType case bool: return BoolType
case int: return IntType case int64: return IntType
case string: return StringType case string: return StringType
case *Table: return TableType case *Table: return TableType
case *Closure: return FunctionType case *Closure: return FunctionType
@ -88,7 +88,7 @@ func (v Value) AsString() string {
} }
func (v Value) AsTable() *Table { func (v Value) AsTable() *Table {
panic("Value.AsTable unimplemented in midnight") return v.iface.(*Table)
} }
func ToString(v Value) string { func ToString(v Value) string {
@ -98,9 +98,10 @@ func ToString(v Value) string {
func (v Value) TypeName() string { func (v Value) TypeName() string {
switch v.iface.(type) { switch v.iface.(type) {
case bool: return "bool" case bool: return "bool"
case int: return "number" case int64: return "number"
case string: return "string" case string: return "string"
case *Table: return "table" case *Table: return "table"
case *Closure: return "function"
default: return "<unknown type>" default: return "<unknown type>"
} }
} }