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 {
println("hilbish loader called")
var exports = map[string]moonlight.Export{
/*
"alias": {hlalias, 2, false},
"appendPath": {hlappendPath, 1, false},
/*
"complete": {hlcomplete, 2, false},
*/
"cwd": {hlcwd, 0, false},
/*
"exec": {hlexec, 1, false},
*/
"runnerMode": {hlrunnerMode, 1, false},
/*
"goro": {hlgoro, 1, true},
"highlighter": {hlhighlighter, 1, false},
"hinter": {hlhinter, 1, false},
"multiprompt": {hlmultiprompt, 1, false},
"prependPath": {hlprependPath, 1, false},
*/
"prompt": {hlprompt, 1, true},
/*
"inputMode": {hlinputMode, 1, false},
"interval": {hlinterval, 2, 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
}
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))
@ -111,8 +107,8 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value {
//mod.Set(rt.StringValue("completions"), rt.TableValue(hshcomp))
// hilbish.runner table
runnerModule := runnerModeLoader(mlr)
hshMod.SetField("runner", moonlight.TableValue(runnerModule))
//runnerModule := runnerModeLoader(mlr)
//hshMod.SetField("runner", moonlight.TableValue(runnerModule))
// hilbish.jobs table
jobs = newJobHandler()
@ -135,8 +131,8 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value {
//mod.Set(rt.StringValue("version"), rt.TableValue(versionModule))
// very meta
moduleModule := moduleLoader(mlr)
hshMod.SetField("module", moonlight.TableValue(moduleModule))
//moduleModule := moduleLoader(mlr)
//hshMod.SetField("module", moonlight.TableValue(moduleModule))
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 {
println("fs loader called")
exports := map[string]moonlight.Export{
/*
"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
_, 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 {
fmt.Fprintln(os.Stderr, err)
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.GetField(-1, "loaded")
mlr.state.PushGoFunction(cluaLoader)
mlr.state.GetField(-1, "preload")
mlr.state.PushGoClosure(cluaLoader)
mlr.state.SetField(-2, name)
mlr.state.Pop(1)
}

View File

@ -45,5 +45,10 @@ func (mlr *Runtime) pushToState(v Value) {
switch v.Type() {
case NilType: mlr.state.PushNil()
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
package moonlight
//import "github.com/aarzilli/golua/lua"
import (
"fmt"
"github.com/aarzilli/golua/lua"
)
type Table struct{
refIdx int
mlr *Runtime
nativeFields map[Value]Value
}
func NewTable() *Table {
return &Table{
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
}
func (t *Table) Push() {
t.mlr.state.RawGeti(lua.LUA_REGISTRYINDEX, t.refIdx)
}
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) {
t.nativeFields[key] = value
}
func ForEach(tbl *Table, cb func(key Value, val Value)) {
}
func (mlr *Runtime) GlobalTable() *Table {
mlr.state.GetGlobal("_G")
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) {
case bool: return BoolType
case int: return IntType
case int64: return IntType
case string: return StringType
case *Table: return TableType
case *Closure: return FunctionType
@ -88,7 +88,7 @@ func (v Value) AsString() string {
}
func (v Value) AsTable() *Table {
panic("Value.AsTable unimplemented in midnight")
return v.iface.(*Table)
}
func ToString(v Value) string {
@ -98,9 +98,10 @@ func ToString(v Value) string {
func (v Value) TypeName() string {
switch v.iface.(type) {
case bool: return "bool"
case int: return "number"
case int64: return "number"
case string: return "string"
case *Table: return "table"
case *Closure: return "function"
default: return "<unknown type>"
}
}