xgo/stdlib/os_file.go

118 lines
2.9 KiB
Go
Raw Normal View History

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