vm: implement CLEAR opcode

This commit is contained in:
Evgenii Stratonikov 2020-05-06 11:32:22 +03:00
parent 525527f750
commit c8a1188ee1
4 changed files with 94 additions and 80 deletions

View file

@ -66,11 +66,11 @@ const (
CALLL Opcode = 0x35
// Stack
DEPTH Opcode = 0x43
DROP Opcode = 0x45
NIP Opcode = 0x46
XDROP Opcode = 0x48
// CLEAR Opcode = 0x49
DEPTH Opcode = 0x43
DROP Opcode = 0x45
NIP Opcode = 0x46
XDROP Opcode = 0x48
CLEAR Opcode = 0x49
DUP Opcode = 0x4A
OVER Opcode = 0x4B
PICK Opcode = 0x4D

View file

@ -63,6 +63,7 @@ func _() {
_ = x[DROP-69]
_ = x[NIP-70]
_ = x[XDROP-72]
_ = x[CLEAR-73]
_ = x[DUP-74]
_ = x[OVER-75]
_ = x[PICK-77]
@ -140,7 +141,7 @@ func _() {
_ = x[THROWIFNOT-241]
}
const _Opcode_name = "PUSHINT8PUSHINT16PUSHINT32PUSHINT64PUSHINT128PUSHINT256PUSHNULLPUSHDATA1PUSHDATA2PUSHDATA4PUSHM1PUSH0PUSH1PUSH2PUSH3PUSH4PUSH5PUSH6PUSH7PUSH8PUSH9PUSH10PUSH11PUSH12PUSH13PUSH14PUSH15PUSH16NOPJMPJMPLJMPIFJMPIFLJMPIFNOTJMPIFNOTLJMPEQJMPEQLJMPNEJMPNELJMPGTJMPGTLJMPGEJMPGELJMPLTJMPLTLJMPLEJMPLELCALLCALLLDEPTHDROPNIPXDROPDUPOVERPICKTUCKSWAPOLDPUSH1ROLLRETAPPCALLSYSCALLTAILCALLDUPFROMALTSTACKTOALTSTACKFROMALTSTACKXSWAPXTUCKCATSUBSTRLEFTRIGHTINVERTANDORXOREQUALNOTEQUALSIGNABSNEGATEINCDECADDSUBMULDIVMODSHLSHRNOTBOOLANDBOOLORNZNUMEQUALNUMNOTEQUALLTLTEGTGTEMINMAXWITHINPACKUNPACKNEWARRAY0NEWARRAYNEWARRAYTNEWSTRUCT0NEWSTRUCTNEWMAPSIZEHASKEYKEYSVALUESPICKITEMAPPENDSETITEMREVERSEITEMSREMOVECLEARITEMSISNULLISTYPECONVERTTHROWTHROWIFNOT"
const _Opcode_name = "PUSHINT8PUSHINT16PUSHINT32PUSHINT64PUSHINT128PUSHINT256PUSHNULLPUSHDATA1PUSHDATA2PUSHDATA4PUSHM1PUSH0PUSH1PUSH2PUSH3PUSH4PUSH5PUSH6PUSH7PUSH8PUSH9PUSH10PUSH11PUSH12PUSH13PUSH14PUSH15PUSH16NOPJMPJMPLJMPIFJMPIFLJMPIFNOTJMPIFNOTLJMPEQJMPEQLJMPNEJMPNELJMPGTJMPGTLJMPGEJMPGELJMPLTJMPLTLJMPLEJMPLELCALLCALLLDEPTHDROPNIPXDROPCLEARDUPOVERPICKTUCKSWAPOLDPUSH1ROLLRETAPPCALLSYSCALLTAILCALLDUPFROMALTSTACKTOALTSTACKFROMALTSTACKXSWAPXTUCKCATSUBSTRLEFTRIGHTINVERTANDORXOREQUALNOTEQUALSIGNABSNEGATEINCDECADDSUBMULDIVMODSHLSHRNOTBOOLANDBOOLORNZNUMEQUALNUMNOTEQUALLTLTEGTGTEMINMAXWITHINPACKUNPACKNEWARRAY0NEWARRAYNEWARRAYTNEWSTRUCT0NEWSTRUCTNEWMAPSIZEHASKEYKEYSVALUESPICKITEMAPPENDSETITEMREVERSEITEMSREMOVECLEARITEMSISNULLISTYPECONVERTTHROWTHROWIFNOT"
var _Opcode_map = map[Opcode]string{
0: _Opcode_name[0:8],
@ -196,80 +197,81 @@ var _Opcode_map = map[Opcode]string{
69: _Opcode_name[306:310],
70: _Opcode_name[310:313],
72: _Opcode_name[313:318],
74: _Opcode_name[318:321],
75: _Opcode_name[321:325],
77: _Opcode_name[325:329],
78: _Opcode_name[329:333],
80: _Opcode_name[333:337],
81: _Opcode_name[337:345],
82: _Opcode_name[345:349],
102: _Opcode_name[349:352],
103: _Opcode_name[352:359],
104: _Opcode_name[359:366],
105: _Opcode_name[366:374],
106: _Opcode_name[374:389],
107: _Opcode_name[389:399],
108: _Opcode_name[399:411],
114: _Opcode_name[411:416],
115: _Opcode_name[416:421],
126: _Opcode_name[421:424],
127: _Opcode_name[424:430],
128: _Opcode_name[430:434],
129: _Opcode_name[434:439],
144: _Opcode_name[439:445],
145: _Opcode_name[445:448],
146: _Opcode_name[448:450],
147: _Opcode_name[450:453],
151: _Opcode_name[453:458],
152: _Opcode_name[458:466],
153: _Opcode_name[466:470],
154: _Opcode_name[470:473],
155: _Opcode_name[473:479],
156: _Opcode_name[479:482],
157: _Opcode_name[482:485],
158: _Opcode_name[485:488],
159: _Opcode_name[488:491],
160: _Opcode_name[491:494],
161: _Opcode_name[494:497],
162: _Opcode_name[497:500],
168: _Opcode_name[500:503],
169: _Opcode_name[503:506],
170: _Opcode_name[506:509],
171: _Opcode_name[509:516],
172: _Opcode_name[516:522],
177: _Opcode_name[522:524],
179: _Opcode_name[524:532],
180: _Opcode_name[532:543],
181: _Opcode_name[543:545],
182: _Opcode_name[545:548],
183: _Opcode_name[548:550],
184: _Opcode_name[550:553],
185: _Opcode_name[553:556],
186: _Opcode_name[556:559],
187: _Opcode_name[559:565],
192: _Opcode_name[565:569],
193: _Opcode_name[569:575],
194: _Opcode_name[575:584],
195: _Opcode_name[584:592],
196: _Opcode_name[592:601],
197: _Opcode_name[601:611],
198: _Opcode_name[611:620],
200: _Opcode_name[620:626],
202: _Opcode_name[626:630],
203: _Opcode_name[630:636],
204: _Opcode_name[636:640],
205: _Opcode_name[640:646],
206: _Opcode_name[646:654],
207: _Opcode_name[654:660],
208: _Opcode_name[660:667],
209: _Opcode_name[667:679],
210: _Opcode_name[679:685],
211: _Opcode_name[685:695],
216: _Opcode_name[695:701],
217: _Opcode_name[701:707],
219: _Opcode_name[707:714],
240: _Opcode_name[714:719],
241: _Opcode_name[719:729],
73: _Opcode_name[318:323],
74: _Opcode_name[323:326],
75: _Opcode_name[326:330],
77: _Opcode_name[330:334],
78: _Opcode_name[334:338],
80: _Opcode_name[338:342],
81: _Opcode_name[342:350],
82: _Opcode_name[350:354],
102: _Opcode_name[354:357],
103: _Opcode_name[357:364],
104: _Opcode_name[364:371],
105: _Opcode_name[371:379],
106: _Opcode_name[379:394],
107: _Opcode_name[394:404],
108: _Opcode_name[404:416],
114: _Opcode_name[416:421],
115: _Opcode_name[421:426],
126: _Opcode_name[426:429],
127: _Opcode_name[429:435],
128: _Opcode_name[435:439],
129: _Opcode_name[439:444],
144: _Opcode_name[444:450],
145: _Opcode_name[450:453],
146: _Opcode_name[453:455],
147: _Opcode_name[455:458],
151: _Opcode_name[458:463],
152: _Opcode_name[463:471],
153: _Opcode_name[471:475],
154: _Opcode_name[475:478],
155: _Opcode_name[478:484],
156: _Opcode_name[484:487],
157: _Opcode_name[487:490],
158: _Opcode_name[490:493],
159: _Opcode_name[493:496],
160: _Opcode_name[496:499],
161: _Opcode_name[499:502],
162: _Opcode_name[502:505],
168: _Opcode_name[505:508],
169: _Opcode_name[508:511],
170: _Opcode_name[511:514],
171: _Opcode_name[514:521],
172: _Opcode_name[521:527],
177: _Opcode_name[527:529],
179: _Opcode_name[529:537],
180: _Opcode_name[537:548],
181: _Opcode_name[548:550],
182: _Opcode_name[550:553],
183: _Opcode_name[553:555],
184: _Opcode_name[555:558],
185: _Opcode_name[558:561],
186: _Opcode_name[561:564],
187: _Opcode_name[564:570],
192: _Opcode_name[570:574],
193: _Opcode_name[574:580],
194: _Opcode_name[580:589],
195: _Opcode_name[589:597],
196: _Opcode_name[597:606],
197: _Opcode_name[606:616],
198: _Opcode_name[616:625],
200: _Opcode_name[625:631],
202: _Opcode_name[631:635],
203: _Opcode_name[635:641],
204: _Opcode_name[641:645],
205: _Opcode_name[645:651],
206: _Opcode_name[651:659],
207: _Opcode_name[659:665],
208: _Opcode_name[665:672],
209: _Opcode_name[672:684],
210: _Opcode_name[684:690],
211: _Opcode_name[690:700],
216: _Opcode_name[700:706],
217: _Opcode_name[706:712],
219: _Opcode_name[712:719],
240: _Opcode_name[719:724],
241: _Opcode_name[724:734],
}
func (i Opcode) String() string {

View file

@ -679,6 +679,9 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
panic("bad index")
}
case opcode.CLEAR:
v.estack.Clear()
case opcode.DUP:
v.estack.Push(v.estack.Dup(0))

View file

@ -1700,6 +1700,15 @@ func TestXDROPgood(t *testing.T) {
assert.Equal(t, int64(1), vm.estack.Peek(1).BigInt().Int64())
}
func TestCLEAR(t *testing.T) {
prog := makeProgram(opcode.CLEAR)
v := load(prog)
v.estack.PushVal(123)
require.Equal(t, 1, v.estack.Len())
require.NoError(t, v.Run())
require.Equal(t, 0, v.estack.Len())
}
func TestINVERTbadNoitem(t *testing.T) {
prog := makeProgram(opcode.INVERT)
runWithArgs(t, prog, nil)