forked from TrueCloudLab/neoneo-go
Merge pull request #3237 from nspcc-dev/emit-smth
vm: allow to emit `any` based on its type
This commit is contained in:
commit
98de5b9ccb
1 changed files with 71 additions and 65 deletions
|
@ -98,7 +98,21 @@ func bigInt(w *io.BinWriter, n *big.Int, trySmall bool) {
|
||||||
w.WriteBytes(padRight(1<<padSize, buf))
|
w.WriteBytes(padRight(1<<padSize, buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Array emits an array of elements to the given buffer. It accepts elements of the following types:
|
// Array emits an array of elements to the given buffer. It accepts everything that
|
||||||
|
// Any accepts.
|
||||||
|
func Array(w *io.BinWriter, es ...any) {
|
||||||
|
if len(es) == 0 {
|
||||||
|
Opcodes(w, opcode.NEWARRAY0)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for i := len(es) - 1; i >= 0; i-- {
|
||||||
|
Any(w, es[i])
|
||||||
|
}
|
||||||
|
Int(w, int64(len(es)))
|
||||||
|
Opcodes(w, opcode.PACK)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Any emits element if supported. It accepts elements of the following types:
|
||||||
// - int8, int16, int32, int64, int
|
// - int8, int16, int32, int64, int
|
||||||
// - uint8, uint16, uint32, uint64, uint
|
// - uint8, uint16, uint32, uint64, uint
|
||||||
// - *big.Int
|
// - *big.Int
|
||||||
|
@ -108,13 +122,8 @@ func bigInt(w *io.BinWriter, n *big.Int, trySmall bool) {
|
||||||
// - stackitem.Convertible, stackitem.Item
|
// - stackitem.Convertible, stackitem.Item
|
||||||
// - nil
|
// - nil
|
||||||
// - []any
|
// - []any
|
||||||
func Array(w *io.BinWriter, es ...any) {
|
func Any(w *io.BinWriter, something any) {
|
||||||
if len(es) == 0 {
|
switch e := something.(type) {
|
||||||
Opcodes(w, opcode.NEWARRAY0)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for i := len(es) - 1; i >= 0; i-- {
|
|
||||||
switch e := es[i].(type) {
|
|
||||||
case []any:
|
case []any:
|
||||||
Array(w, e...)
|
Array(w, e...)
|
||||||
case int64:
|
case int64:
|
||||||
|
@ -166,16 +175,13 @@ func Array(w *io.BinWriter, es ...any) {
|
||||||
case stackitem.Item:
|
case stackitem.Item:
|
||||||
StackItem(w, e)
|
StackItem(w, e)
|
||||||
default:
|
default:
|
||||||
if es[i] != nil {
|
if something != nil {
|
||||||
w.Err = fmt.Errorf("unsupported type: %T", e)
|
w.Err = fmt.Errorf("unsupported type: %T", e)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
Opcodes(w, opcode.PUSHNULL)
|
Opcodes(w, opcode.PUSHNULL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Int(w, int64(len(es)))
|
|
||||||
Opcodes(w, opcode.PACK)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convertible converts provided stackitem.Convertible to the stackitem.Item and
|
// Convertible converts provided stackitem.Convertible to the stackitem.Item and
|
||||||
// emits the item to the given buffer.
|
// emits the item to the given buffer.
|
||||||
|
|
Loading…
Reference in a new issue