refactor: rewrite appendPath to use moonlight

midnight-edition
sammyette 2024-07-20 15:11:45 -04:00
parent 69fcd8e348
commit 4fdc99db88
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
3 changed files with 39 additions and 9 deletions

20
api.go
View File

@ -39,8 +39,8 @@ var hshMod *moonlight.Table
func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value { func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value {
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},
@ -468,20 +468,21 @@ hilbish.appendPath {
'~/.local/bin' '~/.local/bin'
} }
#example #example
func hlappendPath(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { */
if err := c.Check1Arg(); err != nil { func hlappendPath(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
if err := mlr.Check1Arg(c); err != nil {
return nil, err return nil, err
} }
arg := c.Arg(0) arg := mlr.Arg(c, 0)
// check if dir is a table or a string // check if dir is a table or a string
if arg.Type() == rt.TableType { if moonlight.Type(arg) == moonlight.TableType {
util.ForEach(arg.AsTable(), func(k rt.Value, v rt.Value) { moonlight.ForEach(moonlight.ToTable(arg), func(_ moonlight.Value, v moonlight.Value) {
if v.Type() == rt.StringType { if moonlight.Type(v) == moonlight.StringType {
appendPath(v.AsString()) appendPath(moonlight.ToString(v))
} }
}) })
} else if arg.Type() == rt.StringType { } else if moonlight.Type(arg) == moonlight.StringType {
appendPath(arg.AsString()) appendPath(arg.AsString())
} else { } else {
return nil, errors.New("bad argument to appendPath (expected string or table, got " + arg.TypeName() + ")") return nil, errors.New("bad argument to appendPath (expected string or table, got " + arg.TypeName() + ")")
@ -500,6 +501,7 @@ func appendPath(dir string) {
} }
} }
/*
// exec(cmd) // exec(cmd)
// Replaces the currently running Hilbish instance with the supplied command. // Replaces the currently running Hilbish instance with the supplied command.
// This can be used to do an in-place restart. // This can be used to do an in-place restart.

View File

@ -26,8 +26,27 @@ func (t *Table) Set(key Value, value Value) {
t.lt.Set(key, value) t.lt.Set(key, value)
} }
func ForEach(tbl *Table, cb func(key Value, val Value)) {
nextVal := rt.NilValue
for {
key, val, _ := tbl.lt.Next(nextVal)
if key == rt.NilValue {
break
}
nextVal = key
cb(Value(key), Value(val))
}
}
func (mlr *Runtime) GlobalTable() *Table { func (mlr *Runtime) GlobalTable() *Table {
return &Table{ return &Table{
lt: mlr.rt.GlobalEnv(), lt: mlr.rt.GlobalEnv(),
} }
} }
func convertToMoonlightTable(t *rt.Table) *Table {
return &Table{
lt: t,
}
}

View File

@ -9,6 +9,7 @@ type ValueType = rt.ValueType
const ( const (
StringType = rt.StringType StringType = rt.StringType
FunctionType = rt.FunctionType FunctionType = rt.FunctionType
TableType = rt.TableType
) )
func StringValue(str string) Value { func StringValue(str string) Value {
@ -30,3 +31,11 @@ func TableValue(t *Table) Value {
func Type(v Value) ValueType { func Type(v Value) ValueType {
return ValueType(v.Type()) return ValueType(v.Type())
} }
func ToString(v Value) string {
return v.AsString()
}
func ToTable(v Value) *Table {
return convertToMoonlightTable(v.AsTable())
}