2020-06-03 12:55:06 +00:00
|
|
|
package stackitem
|
|
|
|
|
2020-07-30 10:20:36 +00:00
|
|
|
import "errors"
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
// Type represents type of the stack item.
|
|
|
|
type Type byte
|
|
|
|
|
|
|
|
// This block defines all known stack item types.
|
|
|
|
const (
|
|
|
|
AnyT Type = 0x00
|
|
|
|
PointerT Type = 0x10
|
|
|
|
BooleanT Type = 0x20
|
|
|
|
IntegerT Type = 0x21
|
|
|
|
ByteArrayT Type = 0x28
|
|
|
|
BufferT Type = 0x30
|
|
|
|
ArrayT Type = 0x40
|
|
|
|
StructT Type = 0x41
|
|
|
|
MapT Type = 0x48
|
|
|
|
InteropT Type = 0x60
|
|
|
|
)
|
|
|
|
|
|
|
|
// String implements fmt.Stringer interface.
|
|
|
|
func (t Type) String() string {
|
|
|
|
switch t {
|
|
|
|
case AnyT:
|
|
|
|
return "Any"
|
|
|
|
case PointerT:
|
|
|
|
return "Pointer"
|
|
|
|
case BooleanT:
|
|
|
|
return "Boolean"
|
|
|
|
case IntegerT:
|
|
|
|
return "Integer"
|
|
|
|
case ByteArrayT:
|
2020-07-29 09:13:07 +00:00
|
|
|
return "ByteString"
|
2020-06-03 12:55:06 +00:00
|
|
|
case BufferT:
|
|
|
|
return "Buffer"
|
|
|
|
case ArrayT:
|
|
|
|
return "Array"
|
|
|
|
case StructT:
|
|
|
|
return "Struct"
|
|
|
|
case MapT:
|
|
|
|
return "Map"
|
|
|
|
case InteropT:
|
|
|
|
return "Interop"
|
|
|
|
default:
|
|
|
|
return "INVALID"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsValid checks if s is a well defined stack item type.
|
|
|
|
func (t Type) IsValid() bool {
|
|
|
|
switch t {
|
|
|
|
case AnyT, PointerT, BooleanT, IntegerT, ByteArrayT, BufferT, ArrayT, StructT, MapT, InteropT:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2020-07-30 10:20:36 +00:00
|
|
|
|
|
|
|
// FromString returns stackitem type from string.
|
|
|
|
func FromString(s string) (Type, error) {
|
|
|
|
switch s {
|
|
|
|
case "Any":
|
|
|
|
return AnyT, nil
|
|
|
|
case "Pointer":
|
|
|
|
return PointerT, nil
|
|
|
|
case "Boolean":
|
|
|
|
return BooleanT, nil
|
|
|
|
case "Integer":
|
|
|
|
return IntegerT, nil
|
|
|
|
case "ByteString":
|
|
|
|
return ByteArrayT, nil
|
|
|
|
case "Buffer":
|
|
|
|
return BufferT, nil
|
|
|
|
case "Array":
|
|
|
|
return ArrayT, nil
|
|
|
|
case "Struct":
|
|
|
|
return StructT, nil
|
|
|
|
case "Map":
|
|
|
|
return MapT, nil
|
|
|
|
case "Interop":
|
|
|
|
return InteropT, nil
|
|
|
|
default:
|
|
|
|
return 0xFF, errors.New("invalid type")
|
|
|
|
}
|
|
|
|
}
|