vm: make SerializeItem/DeserializeItem public APIs
They're useful as wrappers around EncodeBinaryStackItem/DecodeBinaryStackItem.
This commit is contained in:
parent
7e7ce7f19f
commit
b805b1a71b
3 changed files with 7 additions and 5 deletions
|
@ -93,7 +93,7 @@ func runtimeNotify(vm *VM) error {
|
||||||
// RuntimeSerialize handles syscalls System.Runtime.Serialize and Neo.Runtime.Serialize.
|
// RuntimeSerialize handles syscalls System.Runtime.Serialize and Neo.Runtime.Serialize.
|
||||||
func RuntimeSerialize(vm *VM) error {
|
func RuntimeSerialize(vm *VM) error {
|
||||||
item := vm.Estack().Pop()
|
item := vm.Estack().Pop()
|
||||||
data, err := serializeItem(item.value)
|
data, err := SerializeItem(item.value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else if len(data) > MaxItemSize {
|
} else if len(data) > MaxItemSize {
|
||||||
|
@ -109,7 +109,7 @@ func RuntimeSerialize(vm *VM) error {
|
||||||
func RuntimeDeserialize(vm *VM) error {
|
func RuntimeDeserialize(vm *VM) error {
|
||||||
data := vm.Estack().Pop().Bytes()
|
data := vm.Estack().Pop().Bytes()
|
||||||
|
|
||||||
item, err := deserializeItem(data)
|
item, err := DeserializeItem(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,8 @@ const (
|
||||||
mapT stackItemType = 0x82
|
mapT stackItemType = 0x82
|
||||||
)
|
)
|
||||||
|
|
||||||
func serializeItem(item StackItem) ([]byte, error) {
|
// SerializeItem encodes given StackItem into the byte slice.
|
||||||
|
func SerializeItem(item StackItem) ([]byte, error) {
|
||||||
w := io.NewBufBinWriter()
|
w := io.NewBufBinWriter()
|
||||||
EncodeBinaryStackItem(item, w.BinWriter)
|
EncodeBinaryStackItem(item, w.BinWriter)
|
||||||
if w.Err != nil {
|
if w.Err != nil {
|
||||||
|
@ -78,7 +79,8 @@ func serializeItemTo(item StackItem, w *io.BinWriter, seen map[StackItem]bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func deserializeItem(data []byte) (StackItem, error) {
|
// DeserializeItem decodes StackItem from the given byte slice.
|
||||||
|
func DeserializeItem(data []byte) (StackItem, error) {
|
||||||
r := io.NewBinReaderFromBuf(data)
|
r := io.NewBinReaderFromBuf(data)
|
||||||
item := DecodeBinaryStackItem(r)
|
item := DecodeBinaryStackItem(r)
|
||||||
if r.Err != nil {
|
if r.Err != nil {
|
||||||
|
|
|
@ -653,7 +653,7 @@ func TestDeserializeUnknown(t *testing.T) {
|
||||||
prog := append(getSyscallProg("Neo.Runtime.Deserialize"), byte(opcode.RET))
|
prog := append(getSyscallProg("Neo.Runtime.Deserialize"), byte(opcode.RET))
|
||||||
vm := load(prog)
|
vm := load(prog)
|
||||||
|
|
||||||
data, err := serializeItem(NewBigIntegerItem(123))
|
data, err := SerializeItem(NewBigIntegerItem(123))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
data[0] = 0xFF
|
data[0] = 0xFF
|
||||||
|
|
Loading…
Reference in a new issue