os Process functions
This commit is contained in:
parent
9b1a0a36bf
commit
b5108b8065
3 changed files with 150 additions and 0 deletions
|
@ -34,6 +34,20 @@ func FuncARI(fn func() int) *objects.UserFunction {
|
|||
}
|
||||
}
|
||||
|
||||
// FuncARB transform a function of 'func() bool' signature
|
||||
// into a user function object.
|
||||
func FuncARB(fn func() bool) *objects.UserFunction {
|
||||
return &objects.UserFunction{
|
||||
Value: func(args ...objects.Object) (ret objects.Object, err error) {
|
||||
if len(args) != 0 {
|
||||
return nil, objects.ErrWrongNumArguments
|
||||
}
|
||||
|
||||
return &objects.Bool{Value: fn()}, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// FuncARE transform a function of 'func() error' signature
|
||||
// into a user function object.
|
||||
func FuncARE(fn func() error) *objects.UserFunction {
|
||||
|
|
|
@ -33,6 +33,9 @@ var osModule = map[string]objects.Object{
|
|||
"path_separator": &objects.Char{Value: os.PathSeparator},
|
||||
"path_list_separator": &objects.Char{Value: os.PathListSeparator},
|
||||
"dev_null": &objects.String{Value: os.DevNull},
|
||||
"seek_set": &objects.Int{Value: int64(os.SEEK_SET)},
|
||||
"seek_cur": &objects.Int{Value: int64(os.SEEK_CUR)},
|
||||
"seek_end": &objects.Int{Value: int64(os.SEEK_END)},
|
||||
// args() => array(string)
|
||||
"args": &objects.UserFunction{Value: osArgs},
|
||||
// chdir(dir string) => error
|
||||
|
@ -109,6 +112,10 @@ var osModule = map[string]objects.Object{
|
|||
"open": &objects.UserFunction{Value: osOpen},
|
||||
// open_file(name string, flag int, perm int) => imap(file)/error
|
||||
"open_file": &objects.UserFunction{Value: osOpenFile},
|
||||
// find_process(pid int) => imap(process)/error
|
||||
"find_process": &objects.UserFunction{Value: osFindProcess},
|
||||
// start_process(name string, argv array(string), dir string, env array(string)) => imap(process)/error
|
||||
"start_process": &objects.UserFunction{Value: osStartProcess},
|
||||
|
||||
// TODO: implemented more functions
|
||||
//"stdin": nil,
|
||||
|
|
129
compiler/stdlib/os_process.go
Normal file
129
compiler/stdlib/os_process.go
Normal file
|
@ -0,0 +1,129 @@
|
|||
package stdlib
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
|
||||
"github.com/d5/tengo/objects"
|
||||
)
|
||||
|
||||
func osProcessStateImmutableMap(state *os.ProcessState) *objects.ImmutableMap {
|
||||
return &objects.ImmutableMap{
|
||||
Value: map[string]objects.Object{
|
||||
"exited": FuncARB(state.Exited),
|
||||
"pid": FuncARI(state.Pid),
|
||||
"string": FuncARS(state.String),
|
||||
"success": FuncARB(state.Success),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func osProcessImmutableMap(proc *os.Process) *objects.ImmutableMap {
|
||||
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
|
||||
}
|
||||
|
||||
return osProcessStateImmutableMap(state), nil
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func osFindProcess(args ...objects.Object) (objects.Object, error) {
|
||||
if len(args) != 1 {
|
||||
return nil, objects.ErrWrongNumArguments
|
||||
}
|
||||
|
||||
i1, ok := objects.ToInt(args[0])
|
||||
if !ok {
|
||||
return nil, objects.ErrInvalidTypeConversion
|
||||
}
|
||||
|
||||
proc, err := os.FindProcess(i1)
|
||||
if err != nil {
|
||||
return wrapError(err), nil
|
||||
}
|
||||
|
||||
return osProcessImmutableMap(proc), nil
|
||||
}
|
||||
|
||||
func osStartProcess(args ...objects.Object) (objects.Object, error) {
|
||||
if len(args) != 4 {
|
||||
return nil, objects.ErrWrongNumArguments
|
||||
}
|
||||
|
||||
name, ok := objects.ToString(args[0])
|
||||
if !ok {
|
||||
return nil, objects.ErrInvalidTypeConversion
|
||||
}
|
||||
|
||||
argv, err := stringArray(args[1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dir, ok := objects.ToString(args[2])
|
||||
if !ok {
|
||||
return nil, objects.ErrInvalidTypeConversion
|
||||
}
|
||||
|
||||
env, err := stringArray(args[3])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
proc, err := os.StartProcess(name, argv, &os.ProcAttr{
|
||||
Dir: dir,
|
||||
Env: env,
|
||||
})
|
||||
if err != nil {
|
||||
return wrapError(err), nil
|
||||
}
|
||||
|
||||
return osProcessImmutableMap(proc), nil
|
||||
}
|
||||
|
||||
func stringArray(o objects.Object) ([]string, error) {
|
||||
arr, ok := o.(*objects.Array)
|
||||
if !ok {
|
||||
return nil, objects.ErrInvalidTypeConversion
|
||||
}
|
||||
|
||||
var sarr []string
|
||||
for _, elem := range arr.Value {
|
||||
str, ok := elem.(*objects.String)
|
||||
if !ok {
|
||||
return nil, objects.ErrInvalidTypeConversion
|
||||
}
|
||||
|
||||
sarr = append(sarr, str.Value)
|
||||
}
|
||||
|
||||
return sarr, nil
|
||||
}
|
Loading…
Reference in a new issue