os_process.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package stdlib
  2. import (
  3. "os"
  4. "syscall"
  5. "github.com/d5/tengo/v2"
  6. )
  7. func makeOSProcessState(state *os.ProcessState) *tengo.ImmutableMap {
  8. return &tengo.ImmutableMap{
  9. Value: map[string]tengo.Object{
  10. "exited": &tengo.UserFunction{
  11. Name: "exited",
  12. Value: FuncARB(state.Exited),
  13. },
  14. "pid": &tengo.UserFunction{
  15. Name: "pid",
  16. Value: FuncARI(state.Pid),
  17. },
  18. "string": &tengo.UserFunction{
  19. Name: "string",
  20. Value: FuncARS(state.String),
  21. },
  22. "success": &tengo.UserFunction{
  23. Name: "success",
  24. Value: FuncARB(state.Success),
  25. },
  26. },
  27. }
  28. }
  29. func makeOSProcess(proc *os.Process) *tengo.ImmutableMap {
  30. return &tengo.ImmutableMap{
  31. Value: map[string]tengo.Object{
  32. "kill": &tengo.UserFunction{
  33. Name: "kill",
  34. Value: FuncARE(proc.Kill),
  35. },
  36. "release": &tengo.UserFunction{
  37. Name: "release",
  38. Value: FuncARE(proc.Release),
  39. },
  40. "signal": &tengo.UserFunction{
  41. Name: "signal",
  42. Value: func(args ...tengo.Object) (tengo.Object, error) {
  43. if len(args) != 1 {
  44. return nil, tengo.ErrWrongNumArguments
  45. }
  46. i1, ok := tengo.ToInt64(args[0])
  47. if !ok {
  48. return nil, tengo.ErrInvalidArgumentType{
  49. Name: "first",
  50. Expected: "int(compatible)",
  51. Found: args[0].TypeName(),
  52. }
  53. }
  54. return wrapError(proc.Signal(syscall.Signal(i1))), nil
  55. },
  56. },
  57. "wait": &tengo.UserFunction{
  58. Name: "wait",
  59. Value: func(args ...tengo.Object) (tengo.Object, error) {
  60. if len(args) != 0 {
  61. return nil, tengo.ErrWrongNumArguments
  62. }
  63. state, err := proc.Wait()
  64. if err != nil {
  65. return wrapError(err), nil
  66. }
  67. return makeOSProcessState(state), nil
  68. },
  69. },
  70. },
  71. }
  72. }