123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- package stdlib
- import (
- "os"
- "surdeus.su/core/xgo/v2"
- )
- func makeOSFile(file *os.File) *xgo.ImmutableMap {
- return &xgo.ImmutableMap{
- Value: map[string]xgo.Object{
- // chdir() => true/error
- "chdir": &xgo.UserFunction{
- Name: "chdir",
- Value: FuncARE(file.Chdir),
- }, //
- // chown(uid int, gid int) => true/error
- "chown": &xgo.UserFunction{
- Name: "chown",
- Value: FuncAIIRE(file.Chown),
- }, //
- // close() => error
- "close": &xgo.UserFunction{
- Name: "close",
- Value: FuncARE(file.Close),
- }, //
- // name() => string
- "name": &xgo.UserFunction{
- Name: "name",
- Value: FuncARS(file.Name),
- }, //
- // readdirnames(n int) => array(string)/error
- "readdirnames": &xgo.UserFunction{
- Name: "readdirnames",
- Value: FuncAIRSsE(file.Readdirnames),
- }, //
- // sync() => error
- "sync": &xgo.UserFunction{
- Name: "sync",
- Value: FuncARE(file.Sync),
- }, //
- // write(bytes) => int/error
- "write": &xgo.UserFunction{
- Name: "write",
- Value: FuncAYRIE(file.Write),
- }, //
- // write(string) => int/error
- "write_string": &xgo.UserFunction{
- Name: "write_string",
- Value: FuncASRIE(file.WriteString),
- }, //
- // read(bytes) => int/error
- "read": &xgo.UserFunction{
- Name: "read",
- Value: FuncAYRIE(file.Read),
- }, //
- // chmod(mode int) => error
- "chmod": &xgo.UserFunction{
- Name: "chmod",
- Value: func(args ...xgo.Object) (xgo.Object, error) {
- if len(args) != 1 {
- return nil, xgo.ErrWrongNumArguments
- }
- i1, ok := xgo.ToInt64(args[0])
- if !ok {
- return nil, xgo.ErrInvalidArgumentType{
- Name: "first",
- Expected: "int(compatible)",
- Found: args[0].TypeName(),
- }
- }
- return wrapError(file.Chmod(os.FileMode(i1))), nil
- },
- },
- // seek(offset int, whence int) => int/error
- "seek": &xgo.UserFunction{
- Name: "seek",
- Value: func(args ...xgo.Object) (xgo.Object, error) {
- if len(args) != 2 {
- return nil, xgo.ErrWrongNumArguments
- }
- i1, ok := xgo.ToInt64(args[0])
- if !ok {
- return nil, xgo.ErrInvalidArgumentType{
- Name: "first",
- Expected: "int(compatible)",
- Found: args[0].TypeName(),
- }
- }
- i2, ok := xgo.ToInt(args[1])
- if !ok {
- return nil, xgo.ErrInvalidArgumentType{
- Name: "second",
- Expected: "int(compatible)",
- Found: args[1].TypeName(),
- }
- }
- res, err := file.Seek(i1, i2)
- if err != nil {
- return wrapError(err), nil
- }
- return &xgo.Int{Value: res}, nil
- },
- },
- // stat() => imap(fileinfo)/error
- "stat": &xgo.UserFunction{
- Name: "stat",
- Value: func(args ...xgo.Object) (xgo.Object, error) {
- if len(args) != 0 {
- return nil, xgo.ErrWrongNumArguments
- }
- return osStat(&xgo.String{Value: file.Name()})
- },
- },
- },
- }
- }
|