@@ -10,55 +10,55 @@ enum TableInstruction: Equatable {
10
10
case `init`( TableIndex , ElementIndex )
11
11
case elementDrop( ElementIndex )
12
12
13
- func execute( runtime: Runtime ) throws {
13
+ func execute( runtime: Runtime , execution : inout ExecutionState ) throws {
14
14
switch self {
15
15
case let . get( tableIndex) :
16
- let ( _, table) = try runtime . getTable ( tableIndex)
16
+ let ( _, table) = try execution . getTable ( tableIndex, store : runtime . store )
17
17
18
- let elementIndex = try runtime . getElementIndex ( table)
18
+ let elementIndex = try execution . getElementIndex ( table)
19
19
20
20
guard let reference = table. elements [ Int ( elementIndex) ] else {
21
21
throw Trap . readingDroppedReference ( index: elementIndex)
22
22
}
23
23
24
- runtime . stack. push ( value: . ref( reference) )
24
+ execution . stack. push ( value: . ref( reference) )
25
25
26
26
case let . set( tableIndex) :
27
- let ( tableAddress, table) = try runtime . getTable ( tableIndex)
27
+ let ( tableAddress, table) = try execution . getTable ( tableIndex, store : runtime . store )
28
28
29
- let reference = try runtime . stack. getReference ( )
30
- let elementIndex = try runtime . getElementIndex ( table)
31
- runtime . setTableElement ( tableAddress: tableAddress, elementIndex, reference)
29
+ let reference = try execution . stack. getReference ( )
30
+ let elementIndex = try execution . getElementIndex ( table)
31
+ setTableElement ( store : runtime . store , tableAddress: tableAddress, elementIndex, reference)
32
32
33
33
case let . size( tableIndex) :
34
- let ( _, table) = try runtime . getTable ( tableIndex)
34
+ let ( _, table) = try execution . getTable ( tableIndex, store : runtime . store )
35
35
36
- runtime . stack. push ( value: . i32( UInt32 ( table. elements. count) ) )
36
+ execution . stack. push ( value: . i32( UInt32 ( table. elements. count) ) )
37
37
38
38
case let . grow( tableIndex) :
39
- let ( tableAddress, table) = try runtime . getTable ( tableIndex)
39
+ let ( tableAddress, table) = try execution . getTable ( tableIndex, store : runtime . store )
40
40
41
- let growthSize = try runtime . stack. popValue ( )
41
+ let growthSize = try execution . stack. popValue ( )
42
42
43
43
guard case let . i32( growthSize) = growthSize else {
44
44
fatalError ( " invalid value at the top of the stack \( growthSize) " )
45
45
}
46
46
47
- let growthValue = try runtime . stack. getReference ( )
47
+ let growthValue = try execution . stack. getReference ( )
48
48
49
49
let oldSize = UInt32 ( table. elements. count)
50
50
guard runtime. store. tables [ tableAddress] . grow ( by: growthSize, value: growthValue) else {
51
- runtime . stack. push ( value: . i32( Int32 ( - 1 ) . unsigned) )
51
+ execution . stack. push ( value: . i32( Int32 ( - 1 ) . unsigned) )
52
52
break
53
53
}
54
54
55
- runtime . stack. push ( value: . i32( oldSize) )
55
+ execution . stack. push ( value: . i32( oldSize) )
56
56
57
57
case let . fill( tableIndex) :
58
- let ( tableAddress, table) = try runtime . getTable ( tableIndex)
59
- let fillCounter = try runtime . stack. popValue ( ) . i32
60
- let fillValue = try runtime . stack. getReference ( )
61
- let startIndex = try runtime . stack. popValue ( ) . i32
58
+ let ( tableAddress, table) = try execution . getTable ( tableIndex, store : runtime . store )
59
+ let fillCounter = try execution . stack. popValue ( ) . i32
60
+ let fillValue = try execution . stack. getReference ( )
61
+ let startIndex = try execution . stack. popValue ( ) . i32
62
62
63
63
guard fillCounter > 0 else {
64
64
break
@@ -69,16 +69,16 @@ enum TableInstruction: Equatable {
69
69
}
70
70
71
71
for i in 0 ..< fillCounter {
72
- runtime . setTableElement ( tableAddress: tableAddress, startIndex + i, fillValue)
72
+ setTableElement ( store : runtime . store , tableAddress: tableAddress, startIndex + i, fillValue)
73
73
}
74
74
75
75
case let . copy( destinationTableIndex, sourceTableIndex) :
76
- let ( _, sourceTable) = try runtime . getTable ( sourceTableIndex)
77
- let ( destinationTableAddress, destinationTable) = try runtime . getTable ( destinationTableIndex)
76
+ let ( _, sourceTable) = try execution . getTable ( sourceTableIndex, store : runtime . store )
77
+ let ( destinationTableAddress, destinationTable) = try execution . getTable ( destinationTableIndex, store : runtime . store )
78
78
79
- let copyCounter = try runtime . stack. popValue ( ) . i32
80
- let sourceIndex = try runtime . stack. popValue ( ) . i32
81
- let destinationIndex = try runtime . stack. popValue ( ) . i32
79
+ let copyCounter = try execution . stack. popValue ( ) . i32
80
+ let sourceIndex = try execution . stack. popValue ( ) . i32
81
+ let destinationIndex = try execution . stack. popValue ( ) . i32
82
82
83
83
guard copyCounter > 0 else {
84
84
break
@@ -97,21 +97,22 @@ enum TableInstruction: Equatable {
97
97
}
98
98
99
99
for i in 0 ..< copyCounter {
100
- runtime. setTableElement (
100
+ setTableElement (
101
+ store: runtime. store,
101
102
tableAddress: destinationTableAddress,
102
103
destinationIndex + i,
103
104
sourceTable. elements [ Int ( sourceIndex + i) ]
104
105
)
105
106
}
106
107
107
108
case let . `init`( tableIndex, elementIndex) :
108
- let ( destinationTableAddress, destinationTable) = try runtime . getTable ( tableIndex)
109
- let elementAddress = runtime . stack. currentFrame. module. elementAddresses [ Int ( elementIndex) ]
109
+ let ( destinationTableAddress, destinationTable) = try execution . getTable ( tableIndex, store : runtime . store )
110
+ let elementAddress = execution . stack. currentFrame. module. elementAddresses [ Int ( elementIndex) ]
110
111
let sourceElement = runtime. store. elements [ elementAddress]
111
112
112
- let copyCounter = try runtime . stack. popValue ( ) . i32
113
- let sourceIndex = try runtime . stack. popValue ( ) . i32
114
- let destinationIndex = try runtime . stack. popValue ( ) . i32
113
+ let copyCounter = try execution . stack. popValue ( ) . i32
114
+ let sourceIndex = try execution . stack. popValue ( ) . i32
115
+ let destinationIndex = try execution . stack. popValue ( ) . i32
115
116
116
117
guard copyCounter > 0 else {
117
118
break
@@ -133,23 +134,37 @@ enum TableInstruction: Equatable {
133
134
for i in 0 ..< copyCounter {
134
135
let reference = sourceElement. references [ Int ( sourceIndex + i) ]
135
136
136
- runtime. setTableElement ( tableAddress: destinationTableAddress, destinationIndex + i, reference)
137
+ setTableElement (
138
+ store: runtime. store,
139
+ tableAddress: destinationTableAddress,
140
+ destinationIndex + i,
141
+ reference
142
+ )
137
143
}
138
144
139
145
case let . elementDrop( elementIndex) :
140
- let elementAddress = runtime . stack. currentFrame. module. elementAddresses [ Int ( elementIndex) ]
146
+ let elementAddress = execution . stack. currentFrame. module. elementAddresses [ Int ( elementIndex) ]
141
147
runtime. store. elements [ elementAddress] . drop ( )
142
148
}
143
149
}
150
+
151
+ fileprivate func setTableElement(
152
+ store: Store ,
153
+ tableAddress: TableAddress ,
154
+ _ elementIndex: ElementIndex ,
155
+ _ reference: Reference ?
156
+ ) {
157
+ store. tables [ tableAddress] . elements [ Int ( elementIndex) ] = reference
158
+ }
144
159
}
145
160
146
- extension Runtime {
147
- fileprivate func getTable( _ tableIndex: UInt32 ) throws -> ( TableAddress , TableInstance ) {
161
+ extension ExecutionState {
162
+ fileprivate func getTable( _ tableIndex: UInt32 , store : Store ) throws -> ( TableAddress , TableInstance ) {
148
163
let address = stack. currentFrame. module. tableAddresses [ Int ( tableIndex) ]
149
164
return ( address, store. tables [ address] )
150
165
}
151
166
152
- fileprivate func getElementIndex( _ table: TableInstance ) throws -> ElementIndex {
167
+ fileprivate mutating func getElementIndex( _ table: TableInstance ) throws -> ElementIndex {
153
168
let elementIndex = try stack. popValue ( ) . i32
154
169
155
170
guard elementIndex < table. elements. count else {
@@ -158,10 +173,6 @@ extension Runtime {
158
173
159
174
return elementIndex
160
175
}
161
-
162
- fileprivate func setTableElement( tableAddress: TableAddress , _ elementIndex: ElementIndex , _ reference: Reference ? ) {
163
- store. tables [ tableAddress] . elements [ Int ( elementIndex) ] = reference
164
- }
165
176
}
166
177
167
178
extension Stack {
0 commit comments