Add file operations
This commit is contained in:
40
builtins/read.go
Normal file
40
builtins/read.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package builtins
|
||||
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// DefaultBufferSize is the default buffer size
|
||||
const DefaultBufferSize = 4096
|
||||
|
||||
// Read ...
|
||||
func Read(args ...object.Object) object.Object {
|
||||
if err := typing.Check(
|
||||
"read", args,
|
||||
typing.RangeOfArgs(1, 2),
|
||||
typing.WithTypes(object.INTEGER_OBJ, object.INTEGER_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
var (
|
||||
fd int
|
||||
n = DefaultBufferSize
|
||||
)
|
||||
|
||||
fd = int(args[0].(*object.Integer).Value)
|
||||
|
||||
if len(args) == 2 {
|
||||
n = int(args[1].(*object.Integer).Value)
|
||||
}
|
||||
|
||||
buf := make([]byte, n)
|
||||
n, err := syscall.Read(syscall.Handle(fd), buf)
|
||||
if err != nil {
|
||||
return newError("IOError: %s", err)
|
||||
}
|
||||
|
||||
return &object.String{Value: string(buf[:n])}
|
||||
}
|
||||
Reference in New Issue
Block a user