Added a log builtin

This commit is contained in:
Leon Mika 2025-01-15 22:22:17 +11:00
parent 23f0f16d26
commit 485e839ae3
5 changed files with 167 additions and 22 deletions

View file

@ -8,6 +8,7 @@ import (
"github.com/alecthomas/participle/v2"
"strings"
"syscall/js"
"ucl.lmika.dev/repl"
"ucl.lmika.dev/ucl"
)
@ -20,9 +21,12 @@ func invokeUCLCallback(name string, args ...any) {
func initJS(ctx context.Context) {
uclObj := make(map[string]any)
inst := ucl.New(ucl.WithOut(ucl.LineHandler(func(line string) {
invokeUCLCallback("onOutLine", line)
})))
replInst := repl.New(
ucl.WithModule(builtins.Log(nil)),
ucl.WithModule(builtins.Strs()),
ucl.WithOut(ucl.LineHandler(func(line string) {
invokeUCLCallback("onOutLine", line)
})))
uclObj["eval"] = js.FuncOf(func(this js.Value, args []js.Value) any {
if len(args) != 2 {
@ -36,7 +40,7 @@ func initJS(ctx context.Context) {
}
wantContinue := args[1].Bool()
if err := ucl.EvalAndDisplay(ctx, inst, cmdLine); err != nil {
if err := replInst.EvalAndDisplay(ctx, cmdLine); err != nil {
var p participle.Error
if errors.As(err, &p) && wantContinue {
invokeUCLCallback("onContinue")
@ -50,21 +54,3 @@ func initJS(ctx context.Context) {
})
js.Global().Set("ucl", uclObj)
}
//
//type uclOut struct {
// lineBuffer *bytes.Buffer
// writeLine func(line string)
//}
//
//func (uo *uclOut) Write(p []byte) (n int, err error) {
// for _, b := range p {
// if b == '\n' {
// uo.writeLine(uo.lineBuffer.String())
// uo.lineBuffer.Reset()
// } else {
// uo.lineBuffer.WriteByte(b)
// }
// }
// return len(p), nil
//}