summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/d5/tengo/v2/stdlib/os_process.go
blob: 7fcf27a462e43cde3b9bf93e3c65dd44a4c0e94e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package stdlib

import (
	"os"
	"syscall"

	"github.com/d5/tengo/v2"
)

func makeOSProcessState(state *os.ProcessState) *tengo.ImmutableMap {
	return &tengo.ImmutableMap{
		Value: map[string]tengo.Object{
			"exited": &tengo.UserFunction{
				Name:  "exited",
				Value: FuncARB(state.Exited),
			},
			"pid": &tengo.UserFunction{
				Name:  "pid",
				Value: FuncARI(state.Pid),
			},
			"string": &tengo.UserFunction{
				Name:  "string",
				Value: FuncARS(state.String),
			},
			"success": &tengo.UserFunction{
				Name:  "success",
				Value: FuncARB(state.Success),
			},
		},
	}
}

func makeOSProcess(proc *os.Process) *tengo.ImmutableMap {
	return &tengo.ImmutableMap{
		Value: map[string]tengo.Object{
			"kill": &tengo.UserFunction{
				Name:  "kill",
				Value: FuncARE(proc.Kill),
			},
			"release": &tengo.UserFunction{
				Name:  "release",
				Value: FuncARE(proc.Release),
			},
			"signal": &tengo.UserFunction{
				Name: "signal",
				Value: func(args ...tengo.Object) (tengo.Object, error) {
					if len(args) != 1 {
						return nil, tengo.ErrWrongNumArguments
					}
					i1, ok := tengo.ToInt64(args[0])
					if !ok {
						return nil, tengo.ErrInvalidArgumentType{
							Name:     "first",
							Expected: "int(compatible)",
							Found:    args[0].TypeName(),
						}
					}
					return wrapError(proc.Signal(syscall.Signal(i1))), nil
				},
			},
			"wait": &tengo.UserFunction{
				Name: "wait",
				Value: func(args ...tengo.Object) (tengo.Object, error) {
					if len(args) != 0 {
						return nil, tengo.ErrWrongNumArguments
					}
					state, err := proc.Wait()
					if err != nil {
						return wrapError(err), nil
					}
					return makeOSProcessState(state), nil
				},
			},
		},
	}
}