summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/d5/tengo/stdlib/os_process.go
blob: 0f4a9f771ace8bb2a0ac65e2cd35615ce0f82250 (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
package stdlib

import (
	"os"
	"syscall"

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

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

func makeOSProcess(proc *os.Process) *objects.ImmutableMap {
	return &objects.ImmutableMap{
		Value: map[string]objects.Object{
			"kill":    &objects.UserFunction{Name: "kill", Value: FuncARE(proc.Kill)},       //
			"release": &objects.UserFunction{Name: "release", Value: FuncARE(proc.Release)}, //
			"signal": &objects.UserFunction{
				Value: func(args ...objects.Object) (ret objects.Object, err error) {
					if len(args) != 1 {
						return nil, objects.ErrWrongNumArguments
					}

					i1, ok := objects.ToInt64(args[0])
					if !ok {
						return nil, objects.ErrInvalidArgumentType{
							Name:     "first",
							Expected: "int(compatible)",
							Found:    args[0].TypeName(),
						}
					}

					return wrapError(proc.Signal(syscall.Signal(i1))), nil
				},
			},
			"wait": &objects.UserFunction{
				Value: func(args ...objects.Object) (ret objects.Object, err error) {
					if len(args) != 0 {
						return nil, objects.ErrWrongNumArguments
					}

					state, err := proc.Wait()
					if err != nil {
						return wrapError(err), nil
					}

					return makeOSProcessState(state), nil
				},
			},
		},
	}
}