Compare commits

..

2 Commits

1 changed files with 25 additions and 1 deletions

26
sink.go
View File

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"io" "io"
"os"
"hilbish/util" "hilbish/util"
@ -18,6 +19,7 @@ var sinkMetaKey = rt.StringValue("hshsink")
type sink struct{ type sink struct{
writer *bufio.Writer writer *bufio.Writer
reader *bufio.Reader reader *bufio.Reader
file *os.File
ud *rt.UserData ud *rt.UserData
autoFlush bool autoFlush bool
} }
@ -27,16 +29,34 @@ func setupSinkType(rtm *rt.Runtime) {
sinkMethods := rt.NewTable() sinkMethods := rt.NewTable()
sinkFuncs := map[string]util.LuaExport{ sinkFuncs := map[string]util.LuaExport{
"read": {luaSinkRead, 0, false}, "flush": {luaSinkFlush, 1, false},
"read": {luaSinkRead, 1, false},
"write": {luaSinkWrite, 2, false}, "write": {luaSinkWrite, 2, false},
"writeln": {luaSinkWriteln, 2, false}, "writeln": {luaSinkWriteln, 2, false},
} }
util.SetExports(l, sinkMethods, sinkFuncs) util.SetExports(l, sinkMethods, sinkFuncs)
sinkIndex := func(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { sinkIndex := func(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
s, _ := sinkArg(c, 0)
arg := c.Arg(1) arg := c.Arg(1)
val := sinkMethods.Get(arg) val := sinkMethods.Get(arg)
if val != rt.NilValue {
return c.PushingNext1(t.Runtime, val), nil
}
keyStr, _ := arg.TryString()
switch keyStr {
case "pipe":
val = rt.BoolValue(false)
if s.file != nil {
fileInfo, _ := s.file.Stat();
val = rt.BoolValue(fileInfo.Mode() & os.ModeCharDevice == 0)
}
}
return c.PushingNext1(t.Runtime, val), nil return c.PushingNext1(t.Runtime, val), nil
} }
@ -137,6 +157,10 @@ func newSinkInput(r io.Reader) *sink {
} }
s.ud = sinkUserData(s) s.ud = sinkUserData(s)
if f, ok := r.(*os.File); ok {
s.file = f
}
return s return s
} }