clean up stdlib code

This commit is contained in:
Daniel Kang 2019-01-29 19:52:00 -08:00
parent a9a93801b7
commit 306fe1b24c
10 changed files with 1604 additions and 1744 deletions

View file

@ -71,10 +71,4 @@ var mathModule = map[string]objects.Object{
"y0": FuncAFRF(math.Y0), "y0": FuncAFRF(math.Y0),
"y1": FuncAFRF(math.Y1), "y1": FuncAFRF(math.Y1),
"yn": FuncAIFRF(math.Yn), "yn": FuncAIFRF(math.Yn),
// TODO: functions that have multiple returns
// Should these be tuple assignment? Or Map return?
//"frexp": nil,
//"lgamma": nil,
//"modf": nil,
//"sincos": nil,
} }

View file

@ -3,6 +3,7 @@ package stdlib
import ( import (
"io" "io"
"os" "os"
"os/exec"
"github.com/d5/tengo/objects" "github.com/d5/tengo/objects"
) )
@ -36,82 +37,110 @@ var osModule = map[string]objects.Object{
"seek_set": &objects.Int{Value: int64(io.SeekStart)}, "seek_set": &objects.Int{Value: int64(io.SeekStart)},
"seek_cur": &objects.Int{Value: int64(io.SeekCurrent)}, "seek_cur": &objects.Int{Value: int64(io.SeekCurrent)},
"seek_end": &objects.Int{Value: int64(io.SeekEnd)}, "seek_end": &objects.Int{Value: int64(io.SeekEnd)},
// args() => array(string) "args": &objects.UserFunction{Value: osArgs}, // args() => array(string)
"args": &objects.UserFunction{Value: osArgs}, "chdir": FuncASRE(os.Chdir), // chdir(dir string) => error
// chdir(dir string) => error "chmod": osFuncASFmRE(os.Chmod), // chmod(name string, mode int) => error
"chdir": FuncASRE(os.Chdir), "chown": FuncASIIRE(os.Chown), // chown(name string, uid int, gid int) => error
// chmod(name string, mode int) => error "clearenv": FuncAR(os.Clearenv), // clearenv()
"chmod": osFuncASFmRE(os.Chmod), "environ": FuncARSs(os.Environ), // environ() => array(string)
// chown(name string, uid int, gid int) => error "exit": FuncAIR(os.Exit), // exit(code int)
"chown": FuncASIIRE(os.Chown), "expand_env": FuncASRS(os.ExpandEnv), // expand_env(s string) => string
// clearenv() "getegid": FuncARI(os.Getegid), // getegid() => int
"clearenv": FuncAR(os.Clearenv), "getenv": FuncASRS(os.Getenv), // getenv(s string) => string
// environ() => array(string) "geteuid": FuncARI(os.Geteuid), // geteuid() => int
"environ": FuncARSs(os.Environ), "getgid": FuncARI(os.Getgid), // getgid() => int
// exit(code int) "getgroups": FuncARIsE(os.Getgroups), // getgroups() => array(string)/error
"exit": FuncAIR(os.Exit), "getpagesize": FuncARI(os.Getpagesize), // getpagesize() => int
// expand_env(s string) => string "getpid": FuncARI(os.Getpid), // getpid() => int
"expand_env": FuncASRS(os.ExpandEnv), "getppid": FuncARI(os.Getppid), // getppid() => int
// getegid() => int "getuid": FuncARI(os.Getuid), // getuid() => int
"getegid": FuncARI(os.Getegid), "getwd": FuncARSE(os.Getwd), // getwd() => string/error
// getenv(s string) => string "hostname": FuncARSE(os.Hostname), // hostname() => string/error
"getenv": FuncASRS(os.Getenv), "lchown": FuncASIIRE(os.Lchown), // lchown(name string, uid int, gid int) => error
// geteuid() => int "link": FuncASSRE(os.Link), // link(oldname string, newname string) => error
"geteuid": FuncARI(os.Geteuid), "lookup_env": &objects.UserFunction{Value: osLookupEnv}, // lookup_env(key string) => string/false
// getgid() => int "mkdir": osFuncASFmRE(os.Mkdir), // mkdir(name string, perm int) => error
"getgid": FuncARI(os.Getgid), "mkdir_all": osFuncASFmRE(os.MkdirAll), // mkdir_all(name string, perm int) => error
// getgroups() => array(string)/error "readlink": FuncASRSE(os.Readlink), // readlink(name string) => string/error
"getgroups": FuncARIsE(os.Getgroups), "remove": FuncASRE(os.Remove), // remove(name string) => error
// getpagesize() => int "remove_all": FuncASRE(os.RemoveAll), // remove_all(name string) => error
"getpagesize": FuncARI(os.Getpagesize), "rename": FuncASSRE(os.Rename), // rename(oldpath string, newpath string) => error
// getpid() => int "setenv": FuncASSRE(os.Setenv), // setenv(key string, value string) => error
"getpid": FuncARI(os.Getpid), "symlink": FuncASSRE(os.Symlink), // symlink(oldname string newname string) => error
// getppid() => int "temp_dir": FuncARS(os.TempDir), // temp_dir() => string
"getppid": FuncARI(os.Getppid), "truncate": FuncASI64RE(os.Truncate), // truncate(name string, size int) => error
// getuid() => int "unsetenv": FuncASRE(os.Unsetenv), // unsetenv(key string) => error
"getuid": FuncARI(os.Getuid), "create": &objects.UserFunction{Value: osCreate}, // create(name string) => imap(file)/error
// getwd() => string/error "open": &objects.UserFunction{Value: osOpen}, // open(name string) => imap(file)/error
"getwd": FuncARSE(os.Getwd), "open_file": &objects.UserFunction{Value: osOpenFile}, // open_file(name string, flag int, perm int) => imap(file)/error
// hostname() => string/error "find_process": &objects.UserFunction{Value: osFindProcess}, // find_process(pid int) => imap(process)/error
"hostname": FuncARSE(os.Hostname), "start_process": &objects.UserFunction{Value: osStartProcess}, // start_process(name string, argv array(string), dir string, env array(string)) => imap(process)/error
// lchown(name string, uid int, gid int) => error "exec_look_path": FuncASRSE(exec.LookPath), // exec_look_path(file) => string/error
"lchown": FuncASIIRE(os.Lchown), "exec": &objects.UserFunction{Value: osExec}, // exec(name, args...) => command
// link(oldname string, newname string) => error }
"link": FuncASSRE(os.Link),
// lookup_env(key string) => string/false func osCreate(args ...objects.Object) (objects.Object, error) {
"lookup_env": &objects.UserFunction{Value: osLookupEnv}, if len(args) != 1 {
// mkdir(name string, perm int) => error return nil, objects.ErrWrongNumArguments
"mkdir": osFuncASFmRE(os.Mkdir), }
// mkdir_all(name string, perm int) => error
"mkdir_all": osFuncASFmRE(os.MkdirAll), s1, ok := objects.ToString(args[0])
// readlink(name string) => string/error if !ok {
"readlink": FuncASRSE(os.Readlink), return nil, objects.ErrInvalidTypeConversion
// remove(name string) => error }
"remove": FuncASRE(os.Remove),
// remove_all(name string) => error res, err := os.Create(s1)
"remove_all": FuncASRE(os.RemoveAll), if err != nil {
// rename(oldpath string, newpath string) => error return wrapError(err), nil
"rename": FuncASSRE(os.Rename), }
// setenv(key string, value string) => error
"setenv": FuncASSRE(os.Setenv), return makeOSFile(res), nil
// symlink(oldname string newname string) => error }
"symlink": FuncASSRE(os.Symlink),
// temp_dir() => string func osOpen(args ...objects.Object) (objects.Object, error) {
"temp_dir": FuncARS(os.TempDir), if len(args) != 1 {
// truncate(name string, size int) => error return nil, objects.ErrWrongNumArguments
"truncate": FuncASI64RE(os.Truncate), }
// unsetenv(key string) => error
"unsetenv": FuncASRE(os.Unsetenv), s1, ok := objects.ToString(args[0])
// create(name string) => imap(file)/error if !ok {
"create": &objects.UserFunction{Value: osCreate}, return nil, objects.ErrInvalidTypeConversion
// open(name string) => imap(file)/error }
"open": &objects.UserFunction{Value: osOpen},
// open_file(name string, flag int, perm int) => imap(file)/error res, err := os.Open(s1)
"open_file": &objects.UserFunction{Value: osOpenFile}, if err != nil {
// find_process(pid int) => imap(process)/error return wrapError(err), nil
"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}, return makeOSFile(res), nil
}
func osOpenFile(args ...objects.Object) (objects.Object, error) {
if len(args) != 3 {
return nil, objects.ErrWrongNumArguments
}
s1, ok := objects.ToString(args[0])
if !ok {
return nil, objects.ErrInvalidTypeConversion
}
i2, ok := objects.ToInt(args[1])
if !ok {
return nil, objects.ErrInvalidTypeConversion
}
i3, ok := objects.ToInt(args[2])
if !ok {
return nil, objects.ErrInvalidTypeConversion
}
res, err := os.OpenFile(s1, i2, os.FileMode(i3))
if err != nil {
return wrapError(err), nil
}
return makeOSFile(res), nil
} }
func osArgs(args ...objects.Object) (objects.Object, error) { func osArgs(args ...objects.Object) (objects.Object, error) {
@ -148,19 +177,6 @@ func osFuncASFmRE(fn func(string, os.FileMode) error) *objects.UserFunction {
} }
} }
func osExecutable(args ...objects.Object) (objects.Object, error) {
if len(args) != 0 {
return nil, objects.ErrWrongNumArguments
}
res, err := os.Executable()
if err != nil {
return wrapError(err), nil
}
return &objects.String{Value: res}, nil
}
func osLookupEnv(args ...objects.Object) (objects.Object, error) { func osLookupEnv(args ...objects.Object) (objects.Object, error) {
if len(args) != 1 { if len(args) != 1 {
return nil, objects.ErrWrongNumArguments return nil, objects.ErrWrongNumArguments
@ -178,3 +194,99 @@ func osLookupEnv(args ...objects.Object) (objects.Object, error) {
return &objects.String{Value: res}, nil return &objects.String{Value: res}, nil
} }
func osExec(args ...objects.Object) (objects.Object, error) {
if len(args) == 0 {
return nil, objects.ErrWrongNumArguments
}
name, ok := objects.ToString(args[0])
if !ok {
return nil, objects.ErrInvalidTypeConversion
}
var execArgs []string
for _, arg := range args[1:] {
execArg, ok := objects.ToString(arg)
if !ok {
return nil, objects.ErrInvalidTypeConversion
}
execArgs = append(execArgs, execArg)
}
return makeOSExecCommand(exec.Command(name, execArgs...)), 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 makeOSProcess(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 makeOSProcess(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
}

View file

@ -6,14 +6,7 @@ import (
"github.com/d5/tengo/objects" "github.com/d5/tengo/objects"
) )
var execModule = map[string]objects.Object{ func makeOSExecCommand(cmd *exec.Cmd) *objects.ImmutableMap {
// look_path(file string) => string/error
"look_path": FuncASRSE(exec.LookPath),
// command(name string, args array(string)) => imap(cmd)
"command": &objects.UserFunction{Value: execCommand},
}
func execCmdImmutableMap(cmd *exec.Cmd) *objects.ImmutableMap {
return &objects.ImmutableMap{ return &objects.ImmutableMap{
Value: map[string]objects.Object{ Value: map[string]objects.Object{
// combined_output() => bytes/error // combined_output() => bytes/error
@ -84,33 +77,9 @@ func execCmdImmutableMap(cmd *exec.Cmd) *objects.ImmutableMap {
return nil, objects.ErrWrongNumArguments return nil, objects.ErrWrongNumArguments
} }
return osProcessImmutableMap(cmd.Process), nil return makeOSProcess(cmd.Process), nil
}, },
}, },
// TODO: implement pipes
//"stderr_pipe": nil,
//"stdin_pipe": nil,
//"stdout_pipe": nil,
}, },
} }
} }
func execCommand(args ...objects.Object) (objects.Object, error) {
if len(args) != 2 {
return nil, objects.ErrWrongNumArguments
}
name, ok := objects.ToString(args[0])
if !ok {
return nil, objects.ErrInvalidTypeConversion
}
arg, err := stringArray(args[1])
if err != nil {
return nil, err
}
res := exec.Command(name, arg...)
return execCmdImmutableMap(res), nil
}

View file

@ -6,7 +6,7 @@ import (
"github.com/d5/tengo/objects" "github.com/d5/tengo/objects"
) )
func osFileImmutableMap(file *os.File) *objects.ImmutableMap { func makeOSFile(file *os.File) *objects.ImmutableMap {
return &objects.ImmutableMap{ return &objects.ImmutableMap{
Value: map[string]objects.Object{ Value: map[string]objects.Object{
// chdir() => true/error // chdir() => true/error
@ -66,79 +66,6 @@ func osFileImmutableMap(file *os.File) *objects.ImmutableMap {
return &objects.Int{Value: res}, nil return &objects.Int{Value: res}, nil
}, },
}, },
// TODO: implement more functions
//"fd": nil,
//"read_at": nil,
//"readdir": nil,
//"set_deadline": nil,
//"set_read_deadline": nil,
//"set_write_deadline": nil,
//"stat": nil,
//"write_at": nil,
}, },
} }
} }
func osCreate(args ...objects.Object) (objects.Object, error) {
if len(args) != 1 {
return nil, objects.ErrWrongNumArguments
}
s1, ok := objects.ToString(args[0])
if !ok {
return nil, objects.ErrInvalidTypeConversion
}
res, err := os.Create(s1)
if err != nil {
return wrapError(err), nil
}
return osFileImmutableMap(res), nil
}
func osOpen(args ...objects.Object) (objects.Object, error) {
if len(args) != 1 {
return nil, objects.ErrWrongNumArguments
}
s1, ok := objects.ToString(args[0])
if !ok {
return nil, objects.ErrInvalidTypeConversion
}
res, err := os.Open(s1)
if err != nil {
return wrapError(err), nil
}
return osFileImmutableMap(res), nil
}
func osOpenFile(args ...objects.Object) (objects.Object, error) {
if len(args) != 3 {
return nil, objects.ErrWrongNumArguments
}
s1, ok := objects.ToString(args[0])
if !ok {
return nil, objects.ErrInvalidTypeConversion
}
i2, ok := objects.ToInt(args[1])
if !ok {
return nil, objects.ErrInvalidTypeConversion
}
i3, ok := objects.ToInt(args[2])
if !ok {
return nil, objects.ErrInvalidTypeConversion
}
res, err := os.OpenFile(s1, i2, os.FileMode(i3))
if err != nil {
return wrapError(err), nil
}
return osFileImmutableMap(res), nil
}

View file

@ -7,7 +7,7 @@ import (
"github.com/d5/tengo/objects" "github.com/d5/tengo/objects"
) )
func osProcessStateImmutableMap(state *os.ProcessState) *objects.ImmutableMap { func makeOSProcessState(state *os.ProcessState) *objects.ImmutableMap {
return &objects.ImmutableMap{ return &objects.ImmutableMap{
Value: map[string]objects.Object{ Value: map[string]objects.Object{
"exited": FuncARB(state.Exited), "exited": FuncARB(state.Exited),
@ -18,7 +18,7 @@ func osProcessStateImmutableMap(state *os.ProcessState) *objects.ImmutableMap {
} }
} }
func osProcessImmutableMap(proc *os.Process) *objects.ImmutableMap { func makeOSProcess(proc *os.Process) *objects.ImmutableMap {
return &objects.ImmutableMap{ return &objects.ImmutableMap{
Value: map[string]objects.Object{ Value: map[string]objects.Object{
"kill": FuncARE(proc.Kill), "kill": FuncARE(proc.Kill),
@ -48,82 +48,9 @@ func osProcessImmutableMap(proc *os.Process) *objects.ImmutableMap {
return wrapError(err), nil return wrapError(err), nil
} }
return osProcessStateImmutableMap(state), nil return makeOSProcessState(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
}

View file

@ -6,7 +6,6 @@ import "github.com/d5/tengo/objects"
var Modules = map[string]*objects.ImmutableMap{ var Modules = map[string]*objects.ImmutableMap{
"math": {Value: mathModule}, "math": {Value: mathModule},
"os": {Value: osModule}, "os": {Value: osModule},
"exec": {Value: execModule},
"text": {Value: textModule}, "text": {Value: textModule},
"times": {Value: timesModule}, "times": {Value: timesModule},
} }

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,167 @@
package stdlib
import (
"regexp"
"github.com/d5/tengo/objects"
)
func makeTextRegexp(re *regexp.Regexp) *objects.ImmutableMap {
return &objects.ImmutableMap{
Value: map[string]objects.Object{
// match(text) => bool
"match": &objects.UserFunction{
Value: func(args ...objects.Object) (ret objects.Object, err error) {
if len(args) != 1 {
err = objects.ErrWrongNumArguments
return
}
s1, ok := objects.ToString(args[0])
if !ok {
err = objects.ErrInvalidTypeConversion
return
}
if re.MatchString(s1) {
ret = objects.TrueValue
} else {
ret = objects.FalseValue
}
return
},
},
// find(text) => array(array({text:,begin:,end:}))/undefined
// find(text, maxCount) => array(array({text:,begin:,end:}))/undefined
"find": &objects.UserFunction{
Value: func(args ...objects.Object) (ret objects.Object, err error) {
numArgs := len(args)
if numArgs != 1 && numArgs != 2 {
err = objects.ErrWrongNumArguments
return
}
s1, ok := objects.ToString(args[0])
if !ok {
err = objects.ErrInvalidTypeConversion
return
}
if numArgs == 1 {
m := re.FindStringSubmatchIndex(s1)
if m == nil {
ret = objects.UndefinedValue
return
}
arr := &objects.Array{}
for i := 0; i < len(m); i += 2 {
arr.Value = append(arr.Value, &objects.ImmutableMap{Value: map[string]objects.Object{
"text": &objects.String{Value: s1[m[i]:m[i+1]]},
"begin": &objects.Int{Value: int64(m[i])},
"end": &objects.Int{Value: int64(m[i+1])},
}})
}
ret = &objects.Array{Value: []objects.Object{arr}}
return
}
i2, ok := objects.ToInt(args[1])
if !ok {
err = objects.ErrInvalidTypeConversion
return
}
m := re.FindAllStringSubmatchIndex(s1, i2)
if m == nil {
ret = objects.UndefinedValue
return
}
arr := &objects.Array{}
for _, m := range m {
subMatch := &objects.Array{}
for i := 0; i < len(m); i += 2 {
subMatch.Value = append(subMatch.Value, &objects.ImmutableMap{Value: map[string]objects.Object{
"text": &objects.String{Value: s1[m[i]:m[i+1]]},
"begin": &objects.Int{Value: int64(m[i])},
"end": &objects.Int{Value: int64(m[i+1])},
}})
}
arr.Value = append(arr.Value, subMatch)
}
ret = arr
return
},
},
// replace(src, repl) => string
"replace": &objects.UserFunction{
Value: func(args ...objects.Object) (ret objects.Object, err error) {
if len(args) != 2 {
err = objects.ErrWrongNumArguments
return
}
s1, ok := objects.ToString(args[0])
if !ok {
err = objects.ErrInvalidTypeConversion
return
}
s2, ok := objects.ToString(args[1])
if !ok {
err = objects.ErrInvalidTypeConversion
return
}
ret = &objects.String{Value: re.ReplaceAllString(s1, s2)}
return
},
},
// split(text) => array(string)
// split(text, maxCount) => array(string)
"split": &objects.UserFunction{
Value: func(args ...objects.Object) (ret objects.Object, err error) {
numArgs := len(args)
if numArgs != 1 && numArgs != 2 {
err = objects.ErrWrongNumArguments
return
}
s1, ok := objects.ToString(args[0])
if !ok {
err = objects.ErrInvalidTypeConversion
return
}
var i2 = -1
if numArgs > 1 {
i2, ok = objects.ToInt(args[1])
if !ok {
err = objects.ErrInvalidTypeConversion
return
}
}
arr := &objects.Array{}
for _, s := range re.Split(s1, i2) {
arr.Value = append(arr.Value, &objects.String{Value: s})
}
ret = arr
return
},
},
},
}
}

File diff suppressed because it is too large Load diff

View file

@ -47,17 +47,11 @@ os.remove("./temp")
// exec.command // exec.command
expect(t, ` expect(t, `
exec := import("exec") os := import("os")
cmd := os.exec("echo", "foo", "bar")
echo := func(args) { if !is_error(cmd) {
cmd := exec.command("echo", args) out = cmd.output()
if is_error(cmd) { return cmd.value }
output := cmd.output()
if is_error(output) { return output.value }
return output
} }
out = echo(["foo", "bar"])
`, []byte("foo bar\n")) `, []byte("foo bar\n"))
// user modules // user modules