2019-01-18 12:43:46 +03:00
|
|
|
package stdlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/d5/tengo/objects"
|
|
|
|
)
|
|
|
|
|
2019-01-30 06:52:00 +03:00
|
|
|
func makeOSFile(file *os.File) *objects.ImmutableMap {
|
2019-01-18 12:43:46 +03:00
|
|
|
return &objects.ImmutableMap{
|
|
|
|
Value: map[string]objects.Object{
|
|
|
|
// chdir() => true/error
|
|
|
|
"chdir": FuncARE(file.Chdir),
|
|
|
|
// chown(uid int, gid int) => true/error
|
|
|
|
"chown": FuncAIIRE(file.Chown),
|
|
|
|
// close() => error
|
|
|
|
"close": FuncARE(file.Close),
|
|
|
|
// name() => string
|
|
|
|
"name": FuncARS(file.Name),
|
2019-01-18 12:57:14 +03:00
|
|
|
// readdirnames(n int) => array(string)/error
|
2019-01-18 12:43:46 +03:00
|
|
|
"readdirnames": FuncAIRSsE(file.Readdirnames),
|
|
|
|
// sync() => error
|
|
|
|
"sync": FuncARE(file.Sync),
|
|
|
|
// write(bytes) => int/error
|
|
|
|
"write": FuncAYRIE(file.Write),
|
|
|
|
// write(string) => int/error
|
|
|
|
"write_string": FuncASRIE(file.WriteString),
|
|
|
|
// read(bytes) => int/error
|
|
|
|
"read": FuncAYRIE(file.Read),
|
2019-01-18 12:57:14 +03:00
|
|
|
// chmod(mode int) => error
|
2019-01-18 12:43:46 +03:00
|
|
|
"chmod": &objects.UserFunction{
|
|
|
|
Value: func(args ...objects.Object) (ret objects.Object, err error) {
|
|
|
|
if len(args) != 1 {
|
|
|
|
return nil, objects.ErrWrongNumArguments
|
|
|
|
}
|
|
|
|
|
|
|
|
i1, ok := objects.ToInt64(args[0])
|
|
|
|
if !ok {
|
|
|
|
return nil, objects.ErrInvalidTypeConversion
|
|
|
|
}
|
|
|
|
|
|
|
|
return wrapError(file.Chmod(os.FileMode(i1))), nil
|
|
|
|
},
|
|
|
|
},
|
2019-01-18 12:57:14 +03:00
|
|
|
// seek(offset int, whence int) => int/error
|
2019-01-18 12:43:46 +03:00
|
|
|
"seek": &objects.UserFunction{
|
|
|
|
Value: func(args ...objects.Object) (ret objects.Object, err error) {
|
|
|
|
if len(args) != 2 {
|
|
|
|
return nil, objects.ErrWrongNumArguments
|
|
|
|
}
|
|
|
|
|
|
|
|
i1, ok := objects.ToInt64(args[0])
|
|
|
|
if !ok {
|
|
|
|
return nil, objects.ErrInvalidTypeConversion
|
|
|
|
}
|
|
|
|
i2, ok := objects.ToInt(args[1])
|
|
|
|
if !ok {
|
|
|
|
return nil, objects.ErrInvalidTypeConversion
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := file.Seek(i1, i2)
|
|
|
|
if err != nil {
|
|
|
|
return wrapError(err), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &objects.Int{Value: res}, nil
|
|
|
|
},
|
|
|
|
},
|
2019-02-05 20:45:27 +03:00
|
|
|
// stat() => imap(fileinfo)/error
|
|
|
|
"stat": &objects.UserFunction{
|
|
|
|
Value: func(args ...objects.Object) (ret objects.Object, err error) {
|
|
|
|
if len(args) != 0 {
|
|
|
|
return nil, objects.ErrWrongNumArguments
|
|
|
|
}
|
|
|
|
|
|
|
|
return osStat(&objects.String{Value: file.Name()})
|
|
|
|
},
|
|
|
|
},
|
2019-01-18 12:43:46 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|