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`,
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",
Help: "Execute the current loaded script",
@ -270,6 +280,20 @@ func handleLoadGo(c *ishell.Context) {
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) {
v := getVMFromContext(c)
if len(c.Args) != 0 {
@ -408,7 +432,19 @@ func isMethodArg(s string) bool {
func parseArgs(args []string) ([]vm.StackItem, error) {
items := make([]vm.StackItem, len(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 item vm.StackItem
typeAndVal := strings.Split(arg, ":")
if len(typeAndVal) < 2 {
if typeAndVal[0] == boolFalse || typeAndVal[0] == boolTrue {
@ -427,9 +463,9 @@ func parseArgs(args []string) ([]vm.StackItem, error) {
switch typ {
case boolType:
if value == boolFalse {
items[i] = vm.NewBoolItem(false)
item = vm.NewBoolItem(false)
} else if value == boolTrue {
items[i] = vm.NewBoolItem(true)
item = vm.NewBoolItem(true)
} else {
return nil, errors.New("failed to parse bool parameter")
}
@ -438,13 +474,11 @@ func parseArgs(args []string) ([]vm.StackItem, error) {
if err != nil {
return nil, err
}
items[i] = vm.NewBigIntegerItem(val)
item = vm.NewBigIntegerItem(val)
case stringType:
items[i] = vm.NewByteArrayItem([]byte(value))
item = vm.NewByteArrayItem([]byte(value))
}
}
return items, nil
return item, nil
}
func printLogo(c *ishell.Shell) {