feat: add read method to sinks

sink-enhance
sammyette 2023-02-18 19:39:49 -04:00
parent 26c8f28034
commit 83f851dc93
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
1 changed files with 17 additions and 4 deletions

21
sink.go
View File

@ -1,6 +1,7 @@
package main
import (
"bufio"
"fmt"
"io"
@ -15,8 +16,8 @@ var sinkMetaKey = rt.StringValue("hshsink")
// A sink is a structure that has input and/or output to/from
// a desination.
type sink struct{
writer io.Writer
reader io.Reader
writer *bufio.Writer
reader *bufio.Reader
ud *rt.UserData
}
@ -25,6 +26,7 @@ func setupSinkType(rtm *rt.Runtime) {
sinkMethods := rt.NewTable()
sinkFuncs := map[string]util.LuaExport{
"read": {luaSinkRead, 0, false},
"write": {luaSinkWrite, 2, false},
"writeln": {luaSinkWriteln, 2, false},
}
@ -41,6 +43,17 @@ func setupSinkType(rtm *rt.Runtime) {
l.SetRegistry(sinkMetaKey, rt.TableValue(sinkMeta))
}
func luaSinkRead(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
s, err := sinkArg(c, 0)
if err != nil {
return nil, err
}
str, _ := s.reader.ReadString('\n')
return c.PushingNext1(t.Runtime, rt.StringValue(str)), nil
}
// #member
// write(str)
// Writes data to a sink.
@ -87,7 +100,7 @@ func luaSinkWriteln(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
func newSinkInput(r io.Reader) *sink {
s := &sink{
reader: r,
reader: bufio.NewReader(r),
}
s.ud = sinkUserData(s)
@ -96,7 +109,7 @@ func newSinkInput(r io.Reader) *sink {
func newSinkOutput(w io.Writer) *sink {
s := &sink{
writer: w,
writer: bufio.NewWriter(w),
}
s.ud = sinkUserData(s)