From 4fdc99db886f123e0878b743a434fcb1ccc9fb61 Mon Sep 17 00:00:00 2001 From: sammyette Date: Sat, 20 Jul 2024 15:11:45 -0400 Subject: [PATCH] refactor: rewrite appendPath to use moonlight --- api.go | 20 +++++++++++--------- moonlight/table_golua.go | 19 +++++++++++++++++++ moonlight/value_golua.go | 9 +++++++++ 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/api.go b/api.go index 52c4209..73f8b3d 100644 --- a/api.go +++ b/api.go @@ -39,8 +39,8 @@ var hshMod *moonlight.Table func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value { var exports = map[string]moonlight.Export{ "alias": {hlalias, 2, false}, - /* "appendPath": {hlappendPath, 1, false}, + /* "complete": {hlcomplete, 2, false}, */ "cwd": {hlcwd, 0, false}, @@ -468,20 +468,21 @@ hilbish.appendPath { '~/.local/bin' } #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 } - arg := c.Arg(0) + arg := mlr.Arg(c, 0) // check if dir is a table or a string - if arg.Type() == rt.TableType { - util.ForEach(arg.AsTable(), func(k rt.Value, v rt.Value) { - if v.Type() == rt.StringType { - appendPath(v.AsString()) + if moonlight.Type(arg) == moonlight.TableType { + moonlight.ForEach(moonlight.ToTable(arg), func(_ moonlight.Value, v moonlight.Value) { + if moonlight.Type(v) == moonlight.StringType { + appendPath(moonlight.ToString(v)) } }) - } else if arg.Type() == rt.StringType { + } else if moonlight.Type(arg) == moonlight.StringType { appendPath(arg.AsString()) } else { 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) // Replaces the currently running Hilbish instance with the supplied command. // This can be used to do an in-place restart. diff --git a/moonlight/table_golua.go b/moonlight/table_golua.go index 4f2ff36..8c0238f 100644 --- a/moonlight/table_golua.go +++ b/moonlight/table_golua.go @@ -26,8 +26,27 @@ func (t *Table) Set(key Value, value 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 { return &Table{ lt: mlr.rt.GlobalEnv(), } } + +func convertToMoonlightTable(t *rt.Table) *Table { + return &Table{ + lt: t, + } +} diff --git a/moonlight/value_golua.go b/moonlight/value_golua.go index 1233563..2c74394 100644 --- a/moonlight/value_golua.go +++ b/moonlight/value_golua.go @@ -9,6 +9,7 @@ type ValueType = rt.ValueType const ( StringType = rt.StringType FunctionType = rt.FunctionType + TableType = rt.TableType ) func StringValue(str string) Value { @@ -30,3 +31,11 @@ func TableValue(t *Table) Value { func Type(v Value) ValueType { return ValueType(v.Type()) } + +func ToString(v Value) string { + return v.AsString() +} + +func ToTable(v Value) *Table { + return convertToMoonlightTable(v.AsTable()) +}