xgo/stdlib/os_process.go

77 lines
1.7 KiB
Go
Raw Normal View History

2019-01-18 13:28:58 +03:00
package stdlib
import (
"os"
"syscall"
2019-12-30 00:38:51 +03:00
"github.com/d5/tengo/v2"
2019-01-18 13:28:58 +03:00
)
2019-12-20 22:40:38 +03:00
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),
},
2019-01-18 13:28:58 +03:00
},
}
}
2019-12-20 22:40:38 +03:00
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",
2019-12-20 22:40:38 +03:00
Value: func(args ...tengo.Object) (tengo.Object, error) {
2019-01-18 13:28:58 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
return nil, tengo.ErrWrongNumArguments
2019-01-18 13:28:58 +03:00
}
2019-12-20 22:40:38 +03:00
i1, ok := tengo.ToInt64(args[0])
2019-01-18 13:28:58 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
return nil, tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "int(compatible)",
Found: args[0].TypeName(),
}
2019-01-18 13:28:58 +03:00
}
return wrapError(proc.Signal(syscall.Signal(i1))), nil
},
},
2019-12-20 22:40:38 +03:00
"wait": &tengo.UserFunction{
Name: "wait",
2019-12-20 22:40:38 +03:00
Value: func(args ...tengo.Object) (tengo.Object, error) {
2019-01-18 13:28:58 +03:00
if len(args) != 0 {
2019-12-20 22:40:38 +03:00
return nil, tengo.ErrWrongNumArguments
2019-01-18 13:28:58 +03:00
}
state, err := proc.Wait()
if err != nil {
return wrapError(err), nil
}
2019-01-30 06:52:00 +03:00
return makeOSProcessState(state), nil
2019-01-18 13:28:58 +03:00
},
},
},
}
}