os_file.go 2.9 KB

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