-
Notifications
You must be signed in to change notification settings - Fork 0
/
std.go
86 lines (71 loc) · 1.43 KB
/
std.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package component
import (
auxlib2 "github.com/vela-ssoc/vela-kit/auxlib"
"github.com/vela-ssoc/vela-kit/lua"
"io"
"os"
"time"
)
type stdio struct {
lua.SuperVelaData
name string
w io.Writer
}
func newStdio(name string, w io.Writer) *stdio {
s := &stdio{name: name, w: w}
s.V(lua.VTRun, time.Now(), "std."+name)
return s
}
func (st *stdio) Name() string {
return "std" + st.name
}
func (st *stdio) Close() error {
return nil
}
func (st *stdio) Start() error {
return nil
}
func (st *stdio) Type() string {
return "std." + st.name
}
func (st *stdio) toLValue() *lua.VelaData {
return lua.NewVelaData(st)
}
func (st *stdio) Write(p []byte) (int, error) {
return os.Stderr.Write(p)
}
func (st *stdio) Push(v interface{}) error {
_, err := auxlib2.Push(st, v)
return err
}
func (st *stdio) printL(L *lua.LState) int {
st.Push(auxlib2.Format(L, 0))
return 0
}
func (st *stdio) printlnL(L *lua.LState) int {
n := L.GetTop()
if n == 0 {
return 0
}
for i := 1; i <= n; i++ {
st.Push(L.Get(1).String())
}
st.Push("\n")
return 0
}
func (st *stdio) Index(L *lua.LState, key string) lua.LValue {
switch key {
case "print":
return L.NewFunction(st.printL)
case "println":
return L.NewFunction(st.printlnL)
default:
return lua.LNil
}
}
func newLuaStdIndex() lua.LValue {
std := lua.NewUserKV()
std.Set("out", newStdio("out", os.Stdout).toLValue())
std.Set("err", newStdio("err", os.Stderr).toLValue())
return std
}