os_file.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package stdlib
  2. import (
  3. "os"
  4. "surdeus.su/core/xgo/v2"
  5. )
  6. func makeOSFile(file *os.File) *xgo.ImmutableMap {
  7. return &xgo.ImmutableMap{
  8. Value: map[string]xgo.Object{
  9. // chdir() => true/error
  10. "chdir": &xgo.UserFunction{
  11. Name: "chdir",
  12. Value: FuncARE(file.Chdir),
  13. }, //
  14. // chown(uid int, gid int) => true/error
  15. "chown": &xgo.UserFunction{
  16. Name: "chown",
  17. Value: FuncAIIRE(file.Chown),
  18. }, //
  19. // close() => error
  20. "close": &xgo.UserFunction{
  21. Name: "close",
  22. Value: FuncARE(file.Close),
  23. }, //
  24. // name() => string
  25. "name": &xgo.UserFunction{
  26. Name: "name",
  27. Value: FuncARS(file.Name),
  28. }, //
  29. // readdirnames(n int) => array(string)/error
  30. "readdirnames": &xgo.UserFunction{
  31. Name: "readdirnames",
  32. Value: FuncAIRSsE(file.Readdirnames),
  33. }, //
  34. // sync() => error
  35. "sync": &xgo.UserFunction{
  36. Name: "sync",
  37. Value: FuncARE(file.Sync),
  38. }, //
  39. // write(bytes) => int/error
  40. "write": &xgo.UserFunction{
  41. Name: "write",
  42. Value: FuncAYRIE(file.Write),
  43. }, //
  44. // write(string) => int/error
  45. "write_string": &xgo.UserFunction{
  46. Name: "write_string",
  47. Value: FuncASRIE(file.WriteString),
  48. }, //
  49. // read(bytes) => int/error
  50. "read": &xgo.UserFunction{
  51. Name: "read",
  52. Value: FuncAYRIE(file.Read),
  53. }, //
  54. // chmod(mode int) => error
  55. "chmod": &xgo.UserFunction{
  56. Name: "chmod",
  57. Value: func(args ...xgo.Object) (xgo.Object, error) {
  58. if len(args) != 1 {
  59. return nil, xgo.ErrWrongNumArguments
  60. }
  61. i1, ok := xgo.ToInt64(args[0])
  62. if !ok {
  63. return nil, xgo.ErrInvalidArgumentType{
  64. Name: "first",
  65. Expected: "int(compatible)",
  66. Found: args[0].TypeName(),
  67. }
  68. }
  69. return wrapError(file.Chmod(os.FileMode(i1))), nil
  70. },
  71. },
  72. // seek(offset int, whence int) => int/error
  73. "seek": &xgo.UserFunction{
  74. Name: "seek",
  75. Value: func(args ...xgo.Object) (xgo.Object, error) {
  76. if len(args) != 2 {
  77. return nil, xgo.ErrWrongNumArguments
  78. }
  79. i1, ok := xgo.ToInt64(args[0])
  80. if !ok {
  81. return nil, xgo.ErrInvalidArgumentType{
  82. Name: "first",
  83. Expected: "int(compatible)",
  84. Found: args[0].TypeName(),
  85. }
  86. }
  87. i2, ok := xgo.ToInt(args[1])
  88. if !ok {
  89. return nil, xgo.ErrInvalidArgumentType{
  90. Name: "second",
  91. Expected: "int(compatible)",
  92. Found: args[1].TypeName(),
  93. }
  94. }
  95. res, err := file.Seek(i1, i2)
  96. if err != nil {
  97. return wrapError(err), nil
  98. }
  99. return &xgo.Int{Value: res}, nil
  100. },
  101. },
  102. // stat() => imap(fileinfo)/error
  103. "stat": &xgo.UserFunction{
  104. Name: "stat",
  105. Value: func(args ...xgo.Object) (xgo.Object, error) {
  106. if len(args) != 0 {
  107. return nil, xgo.ErrWrongNumArguments
  108. }
  109. return osStat(&xgo.String{Value: file.Name()})
  110. },
  111. },
  112. },
  113. }
  114. }