vm/cli: add push command to push something to the estack

This commit is contained in:
Roman Khimov 2020-05-21 12:13:52 +03:00
parent 03cc8c118f
commit 318ca55982

View file

@ -87,6 +87,16 @@ var commands = []*ishell.Cmd{
> load /path/to/file.go`, > load /path/to/file.go`,
Func: handleLoadGo, Func: handleLoadGo,
}, },
{
Name: "push",
Help: "Push given item to the estack",
LongHelp: `Usage: push <parameter>
<parameter> is mandatory, example:
> push methodstring
See run command help for parameter syntax.`,
Func: handlePush,
},
{ {
Name: "run", Name: "run",
Help: "Execute the current loaded script", Help: "Execute the current loaded script",
@ -270,6 +280,20 @@ func handleLoadGo(c *ishell.Context) {
changePrompt(c, v) changePrompt(c, v)
} }
func handlePush(c *ishell.Context) {
v := getVMFromContext(c)
if len(c.Args) == 0 {
c.Err(errors.New("missing parameter"))
return
}
param, err := parseArg(c.Args[0])
if err != nil {
c.Err(err)
return
}
v.Estack().PushVal(param)
}
func handleRun(c *ishell.Context) { func handleRun(c *ishell.Context) {
v := getVMFromContext(c) v := getVMFromContext(c)
if len(c.Args) != 0 { if len(c.Args) != 0 {
@ -408,7 +432,19 @@ func isMethodArg(s string) bool {
func parseArgs(args []string) ([]vm.StackItem, error) { func parseArgs(args []string) ([]vm.StackItem, error) {
items := make([]vm.StackItem, len(args)) items := make([]vm.StackItem, len(args))
for i, arg := range args { for i, arg := range args {
item, err := parseArg(arg)
if err != nil {
return nil, err
}
items[i] = item
}
return items, nil
}
func parseArg(arg string) (vm.StackItem, error) {
var typ, value string var typ, value string
var item vm.StackItem
typeAndVal := strings.Split(arg, ":") typeAndVal := strings.Split(arg, ":")
if len(typeAndVal) < 2 { if len(typeAndVal) < 2 {
if typeAndVal[0] == boolFalse || typeAndVal[0] == boolTrue { if typeAndVal[0] == boolFalse || typeAndVal[0] == boolTrue {
@ -427,9 +463,9 @@ func parseArgs(args []string) ([]vm.StackItem, error) {
switch typ { switch typ {
case boolType: case boolType:
if value == boolFalse { if value == boolFalse {
items[i] = vm.NewBoolItem(false) item = vm.NewBoolItem(false)
} else if value == boolTrue { } else if value == boolTrue {
items[i] = vm.NewBoolItem(true) item = vm.NewBoolItem(true)
} else { } else {
return nil, errors.New("failed to parse bool parameter") return nil, errors.New("failed to parse bool parameter")
} }
@ -438,13 +474,11 @@ func parseArgs(args []string) ([]vm.StackItem, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
items[i] = vm.NewBigIntegerItem(val) item = vm.NewBigIntegerItem(val)
case stringType: case stringType:
items[i] = vm.NewByteArrayItem([]byte(value)) item = vm.NewByteArrayItem([]byte(value))
} }
} return item, nil
return items, nil
} }
func printLogo(c *ishell.Shell) { func printLogo(c *ishell.Shell) {