Added a log builtin
This commit is contained in:
parent
23f0f16d26
commit
485e839ae3
5 changed files with 167 additions and 22 deletions
|
|
@ -20,6 +20,8 @@ func main() {
|
|||
instRepl := repl.New(
|
||||
ucl.WithModule(builtins.OS()),
|
||||
ucl.WithModule(builtins.FS(nil)),
|
||||
ucl.WithModule(builtins.Log(nil)),
|
||||
ucl.WithModule(builtins.Strs()),
|
||||
)
|
||||
ctx := context.Background()
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
//}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue