@@ -7,16 +7,17 @@ import (
7
7
"github.com/stretchr/testify/require"
8
8
)
9
9
10
- func testShortenJumps (t * testing.T , before , after []opcode.Opcode , indices []int ) {
10
+ func testShortenJumps (t * testing.T , before , after []opcode.Opcode , indices []int , spBefore , spAfter map [ string ][] DebugSeqPoint ) {
11
11
prog := make ([]byte , len (before ))
12
12
for i := range before {
13
13
prog [i ] = byte (before [i ])
14
14
}
15
- raw := removeNOPs (prog , indices )
15
+ raw := removeNOPs (prog , indices , spBefore )
16
16
actual := make ([]opcode.Opcode , len (raw ))
17
17
for i := range raw {
18
18
actual [i ] = opcode .Opcode (raw [i ])
19
19
}
20
+ require .Equal (t , spAfter , spBefore )
20
21
require .Equal (t , after , actual )
21
22
}
22
23
@@ -45,18 +46,32 @@ func TestShortenJumps(t *testing.T) {
45
46
op , 3 , 12 , 0 , 0 , opcode .PUSH1 , opcode .NOP ,
46
47
sop , 249 , sop , 0xFF - 2 ,
47
48
}
48
- testShortenJumps (t , before , after , []int {2 , 3 , 4 , 16 , 17 , 18 , 21 , 22 , 23 })
49
+ spBefore := map [string ][]DebugSeqPoint {
50
+ "test" : {
51
+ DebugSeqPoint {Opcode : 0 }, DebugSeqPoint {Opcode : 5 },
52
+ DebugSeqPoint {Opcode : 7 }, DebugSeqPoint {Opcode : 12 },
53
+ DebugSeqPoint {Opcode : 14 }, DebugSeqPoint {Opcode : 19 },
54
+ },
55
+ }
56
+ spAfter := map [string ][]DebugSeqPoint {
57
+ "test" : {
58
+ DebugSeqPoint {Opcode : 0 }, DebugSeqPoint {Opcode : 2 },
59
+ DebugSeqPoint {Opcode : 4 }, DebugSeqPoint {Opcode : 9 },
60
+ DebugSeqPoint {Opcode : 11 }, DebugSeqPoint {Opcode : 13 },
61
+ },
62
+ }
63
+ testShortenJumps (t , before , after , []int {2 , 3 , 4 , 16 , 17 , 18 , 21 , 22 , 23 }, spBefore , spAfter )
49
64
})
50
65
}
51
66
t .Run ("NoReplace" , func (t * testing.T ) {
52
67
b := []byte {0 , 1 , 2 , 3 , 4 , 5 }
53
68
expected := []byte {0 , 1 , 2 , 3 , 4 , 5 }
54
- require .Equal (t , expected , removeNOPs (b , nil ))
69
+ require .Equal (t , expected , removeNOPs (b , nil , map [ string ][] DebugSeqPoint {} ))
55
70
})
56
71
t .Run ("InvalidIndex" , func (t * testing.T ) {
57
72
before := []byte {byte (opcode .PUSH1 ), 0 , 0 , 0 , 0 }
58
73
require .Panics (t , func () {
59
- removeNOPs (before , []int {0 })
74
+ removeNOPs (before , []int {0 }, map [ string ][] DebugSeqPoint {} )
60
75
})
61
76
})
62
77
t .Run ("SideConditions" , func (t * testing.T ) {
@@ -69,7 +84,19 @@ func TestShortenJumps(t *testing.T) {
69
84
opcode .JMP , 2 ,
70
85
opcode .JMP , 2 ,
71
86
}
72
- testShortenJumps (t , before , after , []int {2 , 3 , 4 , 7 , 8 , 9 })
87
+ spBefore := map [string ][]DebugSeqPoint {
88
+ "test" : {
89
+ DebugSeqPoint {Opcode : 0 },
90
+ DebugSeqPoint {Opcode : 5 },
91
+ },
92
+ }
93
+ spAfter := map [string ][]DebugSeqPoint {
94
+ "test" : {
95
+ DebugSeqPoint {Opcode : 0 },
96
+ DebugSeqPoint {Opcode : 2 },
97
+ },
98
+ }
99
+ testShortenJumps (t , before , after , []int {2 , 3 , 4 , 7 , 8 , 9 }, spBefore , spAfter )
73
100
})
74
101
t .Run ("Backwards" , func (t * testing.T ) {
75
102
before := []opcode.Opcode {
@@ -82,7 +109,21 @@ func TestShortenJumps(t *testing.T) {
82
109
opcode .JMP , 0xFF - 1 ,
83
110
opcode .JMP , 0xFF - 1 ,
84
111
}
85
- testShortenJumps (t , before , after , []int {2 , 3 , 4 , 7 , 8 , 9 , 12 , 13 , 14 })
112
+ spBefore := map [string ][]DebugSeqPoint {
113
+ "test" : {
114
+ DebugSeqPoint {Opcode : 0 },
115
+ DebugSeqPoint {Opcode : 5 },
116
+ DebugSeqPoint {Opcode : 10 },
117
+ },
118
+ }
119
+ spAfter := map [string ][]DebugSeqPoint {
120
+ "test" : {
121
+ DebugSeqPoint {Opcode : 0 },
122
+ DebugSeqPoint {Opcode : 2 },
123
+ DebugSeqPoint {Opcode : 4 },
124
+ },
125
+ }
126
+ testShortenJumps (t , before , after , []int {2 , 3 , 4 , 7 , 8 , 9 , 12 , 13 , 14 }, spBefore , spAfter )
86
127
})
87
128
})
88
129
}
@@ -100,6 +141,17 @@ func TestWriteJumps(t *testing.T) {
100
141
"main" : {rng : DebugRange {Start : 4 , End : 9 }},
101
142
"method" : {rng : DebugRange {Start : 10 , End : 11 }},
102
143
}
144
+ c .sequencePoints = map [string ][]DebugSeqPoint {
145
+ "init" : {
146
+ DebugSeqPoint {Opcode : 1 }, DebugSeqPoint {Opcode : 3 },
147
+ },
148
+ "main" : {
149
+ DebugSeqPoint {Opcode : 4 }, DebugSeqPoint {Opcode : 9 },
150
+ },
151
+ "method" : {
152
+ DebugSeqPoint {Opcode : 10 }, DebugSeqPoint {Opcode : 11 },
153
+ },
154
+ }
103
155
104
156
expProg := []byte {
105
157
byte (opcode .NOP ), byte (opcode .JMP ), 2 , byte (opcode .RET ),
@@ -111,11 +163,23 @@ func TestWriteJumps(t *testing.T) {
111
163
"main" : {rng : DebugRange {Start : 4 , End : 6 }},
112
164
"method" : {rng : DebugRange {Start : 7 , End : 8 }},
113
165
}
166
+ expSeqPoints := map [string ][]DebugSeqPoint {
167
+ "init" : {
168
+ DebugSeqPoint {Opcode : 1 }, DebugSeqPoint {Opcode : 3 },
169
+ },
170
+ "main" : {
171
+ DebugSeqPoint {Opcode : 4 }, DebugSeqPoint {Opcode : 6 },
172
+ },
173
+ "method" : {
174
+ DebugSeqPoint {Opcode : 7 }, DebugSeqPoint {Opcode : 8 },
175
+ },
176
+ }
114
177
115
178
buf , err := c .writeJumps (before )
116
179
require .NoError (t , err )
117
180
require .Equal (t , expProg , buf )
118
181
require .Equal (t , expFuncs , c .funcs )
182
+ require .Equal (t , expSeqPoints , c .sequencePoints )
119
183
}
120
184
121
185
func TestWriteJumpsLastJump (t * testing.T ) {
0 commit comments