xgo/compiler/stdlib/os_process.go

57 lines
1.3 KiB
Go
Raw Normal View History

2019-01-18 13:28:58 +03:00
package stdlib
import (
"os"
"syscall"
"github.com/d5/tengo/objects"
)
2019-01-30 06:52:00 +03:00
func makeOSProcessState(state *os.ProcessState) *objects.ImmutableMap {
2019-01-18 13:28:58 +03:00
return &objects.ImmutableMap{
Value: map[string]objects.Object{
"exited": FuncARB(state.Exited),
"pid": FuncARI(state.Pid),
"string": FuncARS(state.String),
"success": FuncARB(state.Success),
},
}
}
2019-01-30 06:52:00 +03:00
func makeOSProcess(proc *os.Process) *objects.ImmutableMap {
2019-01-18 13:28:58 +03:00
return &objects.ImmutableMap{
Value: map[string]objects.Object{
"kill": FuncARE(proc.Kill),
"release": 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.ErrInvalidTypeConversion
}
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
}
2019-01-30 06:52:00 +03:00
return makeOSProcessState(state), nil
2019-01-18 13:28:58 +03:00
},
},
},
}
}