forked from Shopify/go-lua
-
Notifications
You must be signed in to change notification settings - Fork 1
/
io.go
324 lines (298 loc) · 7.25 KB
/
io.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package lua
import (
"fmt"
"io"
"io/ioutil"
"os"
)
const fileHandle = "FILE*"
const input = "_IO_input"
const output = "_IO_output"
type stream struct {
f *os.File
close Function
}
func toStream(l *State) *stream { return CheckUserData(l, 1, fileHandle).(*stream) }
func toFile(l *State) *os.File {
s := toStream(l)
if s.close == nil {
Errorf(l, "attempt to use a closed file")
}
l.assert(s.f != nil)
return s.f
}
func newStream(l *State, f *os.File, close Function) *stream {
s := &stream{f: f, close: close}
l.PushUserData(s)
SetMetaTableNamed(l, fileHandle)
return s
}
func newFile(l *State) *stream {
return newStream(l, nil, func(l *State) int { return FileResult(l, toStream(l).f.Close(), "") })
}
func ioFile(l *State, name string) *os.File {
l.Field(RegistryIndex, name)
s := l.ToUserData(-1).(*stream)
if s.close == nil {
Errorf(l, fmt.Sprintf("standard %s file is closed", name[len("_IO_"):]))
}
return s.f
}
func forceOpen(l *State, name, mode string) {
s := newFile(l)
flags, err := flags(mode)
if err == nil {
s.f, err = os.OpenFile(name, flags, 0666)
}
if err != nil {
Errorf(l, fmt.Sprintf("cannot open file '%s' (%s)", name, err.Error()))
}
}
func ioFileHelper(name, mode string) Function {
return func(l *State) int {
if !l.IsNoneOrNil(1) {
if name, ok := l.ToString(1); ok {
forceOpen(l, name, mode)
} else {
toFile(l)
l.PushValue(1)
}
l.SetField(RegistryIndex, name)
}
l.Field(RegistryIndex, name)
return 1
}
}
func closeHelper(l *State) int {
s := toStream(l)
close := s.close
s.close = nil
return close(l)
}
func close(l *State) int {
if l.IsNone(1) {
l.Field(RegistryIndex, output)
}
toFile(l)
return closeHelper(l)
}
func write(l *State, f *os.File, argIndex int) int {
var err error
for argCount := l.Top(); argIndex < argCount && err == nil; argIndex++ {
if n, ok := l.ToNumber(argIndex); ok {
_, err = f.WriteString(numberToString(n))
} else {
_, err = f.WriteString(CheckString(l, argIndex))
}
}
if err == nil {
return 1
}
return FileResult(l, err, "")
}
func readNumber(l *State, f *os.File) (err error) {
var n float64
if _, err = fmt.Fscanf(f, "%f", &n); err == nil {
l.PushNumber(n)
} else {
l.PushNil()
}
return
}
func read(l *State, f *os.File, argIndex int) int {
resultCount := 0
var err error
if argCount := l.Top() - 1; argCount == 0 {
// err = readLineHelper(l, f, true)
resultCount = argIndex + 1
} else {
// TODO
}
if err != nil {
return FileResult(l, err, "")
}
if err == io.EOF {
l.Pop(1)
l.PushNil()
}
return resultCount - argIndex
}
func readLine(l *State) int {
s := l.ToUserData(UpValueIndex(1)).(*stream)
argCount, _ := l.ToInteger(UpValueIndex(2))
if s.close == nil {
Errorf(l, "file is already closed")
}
l.SetTop(1)
for i := 1; i <= argCount; i++ {
l.PushValue(UpValueIndex(3 + i))
}
resultCount := read(l, s.f, 2)
l.assert(resultCount > 0)
if !l.IsNil(-resultCount) {
return resultCount
}
if resultCount > 1 {
m, _ := l.ToString(-resultCount + 1)
Errorf(l, m)
}
if l.ToBoolean(UpValueIndex(3)) {
l.SetTop(0)
l.PushValue(UpValueIndex(1))
closeHelper(l)
}
return 0
}
func lines(l *State, shouldClose bool) {
argCount := l.Top() - 1
ArgumentCheck(l, argCount <= MinStack-3, MinStack-3, "too many options")
l.PushValue(1)
l.PushInteger(argCount)
l.PushBoolean(shouldClose)
for i := 1; i <= argCount; i++ {
l.PushValue(i + 1)
}
l.PushGoClosure(readLine, uint8(3+argCount))
}
func flags(m string) (f int, err error) {
if len(m) > 0 && m[len(m)-1] == 'b' {
m = m[:len(m)-1]
}
switch m {
case "r":
f = os.O_RDONLY
case "r+":
f = os.O_RDWR
case "w":
f = os.O_WRONLY | os.O_CREATE | os.O_TRUNC
case "w+":
f = os.O_RDWR | os.O_CREATE | os.O_TRUNC
case "a":
f = os.O_WRONLY | os.O_CREATE | os.O_APPEND
case "a+":
f = os.O_RDWR | os.O_CREATE | os.O_APPEND
default:
err = os.ErrInvalid
}
return
}
var ioLibrary = []RegistryFunction{
{"close", close},
{"flush", func(l *State) int { return FileResult(l, ioFile(l, output).Sync(), "") }},
{"input", ioFileHelper(input, "r")},
{"lines", func(l *State) int {
if l.IsNone(1) {
l.PushNil()
}
if l.IsNil(1) { // No file name.
l.Field(RegistryIndex, input)
l.Replace(1)
toFile(l)
lines(l, false)
} else {
forceOpen(l, CheckString(l, 1), "r")
l.Replace(1)
lines(l, true)
}
return 1
}},
{"open", func(l *State) int {
name := CheckString(l, 1)
flags, err := flags(OptString(l, 2, "r"))
s := newFile(l)
ArgumentCheck(l, err == nil, 2, "invalid mode")
s.f, err = os.OpenFile(name, flags, 0666)
if err == nil {
return 1
}
return FileResult(l, err, name)
}},
{"output", ioFileHelper(output, "w")},
{"popen", func(l *State) int { Errorf(l, "'popen' not supported"); panic("unreachable") }},
{"read", func(l *State) int { return read(l, ioFile(l, input), 1) }},
{"tmpfile", func(l *State) int {
s := newFile(l)
f, err := ioutil.TempFile("", "")
if err == nil {
s.f = f
return 1
}
return FileResult(l, err, "")
}},
{"type", func(l *State) int {
CheckAny(l, 1)
if f, ok := TestUserData(l, 1, fileHandle).(*stream); !ok {
l.PushNil()
} else if f.close == nil {
l.PushString("closed file")
} else {
l.PushString("file")
}
return 1
}},
{"write", func(l *State) int { return write(l, ioFile(l, output), 1) }},
}
var fileHandleMethods = []RegistryFunction{
{"close", close},
{"flush", func(l *State) int { return FileResult(l, toFile(l).Sync(), "") }},
{"lines", func(l *State) int { toFile(l); lines(l, false); return 1 }},
{"read", func(l *State) int { return read(l, toFile(l), 2) }},
{"seek", func(l *State) int {
whence := []int{os.SEEK_SET, os.SEEK_CUR, os.SEEK_END}
f := toFile(l)
op := CheckOption(l, 2, "cur", []string{"set", "cur", "end"})
p3 := OptNumber(l, 3, 0)
offset := int64(p3)
ArgumentCheck(l, float64(offset) == p3, 3, "not an integer in proper range")
ret, err := f.Seek(offset, whence[op])
if err != nil {
return FileResult(l, err, "")
}
l.PushNumber(float64(ret))
return 1
}},
{"setvbuf", func(l *State) int { // Files are unbuffered in Go. Fake support for now.
// f := toFile(l)
// op := CheckOption(l, 2, "", []string{"no", "full", "line"})
// size := OptInteger(l, 3, 1024)
// TODO err := setvbuf(f, nil, mode[op], size)
return FileResult(l, nil, "")
}},
{"write", func(l *State) int { l.PushValue(1); return write(l, toFile(l), 2) }},
// {"__gc", },
{"__tostring", func(l *State) int {
if s := toStream(l); s.close == nil {
l.PushString("file (closed)")
} else {
l.PushString(fmt.Sprintf("file (%p)", s.f))
}
return 1
}},
}
func dontClose(l *State) int {
toStream(l).close = dontClose
l.PushNil()
l.PushString("cannot close standard file")
return 2
}
func registerStdFile(l *State, f *os.File, reg, name string) {
newStream(l, f, dontClose)
if reg != "" {
l.PushValue(-1)
l.SetField(RegistryIndex, reg)
}
l.SetField(-2, name)
}
// IOOpen opens the io library. Usually passed to Require.
func IOOpen(l *State) int {
NewLibrary(l, ioLibrary)
NewMetaTable(l, fileHandle)
l.PushValue(-1)
l.SetField(-2, "__index")
SetFunctions(l, fileHandleMethods, 0)
l.Pop(1)
registerStdFile(l, os.Stdin, input, "stdin")
registerStdFile(l, os.Stdout, output, "stdout")
registerStdFile(l, os.Stderr, "", "stderr")
return 1
}