Skip to content

Commit 18c6e61

Browse files
WAT: Fix element expression encoding
1 parent 5a6266c commit 18c6e61

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

Sources/WAT/Encoder.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,6 @@ struct ElementExprCollector: AnyInstructionVisitor {
158158
typealias Output = Void
159159

160160
var isAllRefFunc: Bool = true
161-
var useExpression: Bool {
162-
// if all instructions are ref.func, use function indices representation
163-
return !isAllRefFunc
164-
}
165161
var instructions: [Instruction] = []
166162

167163
mutating func parse(indices: WatParser.ElementDecl.Indices, wat: inout Wat) throws {
@@ -234,8 +230,11 @@ extension WAT.WatParser.ElementDecl {
234230

235231
var collector = ElementExprCollector()
236232
try collector.parse(indices: indices, wat: &wat)
237-
238-
if collector.useExpression {
233+
var useExpression: Bool {
234+
// if all instructions are ref.func, use function indices representation
235+
return !collector.isAllRefFunc || self.type != .funcRef
236+
}
237+
if useExpression {
239238
// use expression
240239
flags |= 0b0100
241240
}
@@ -262,15 +261,15 @@ extension WAT.WatParser.ElementDecl {
262261
}
263262
}
264263
if isPassive || hasTableIndex {
265-
if collector.useExpression {
264+
if useExpression {
266265
encoder.encode(type)
267266
} else {
268267
// Write ExternKind.func
269268
encoder.writeUnsignedLEB128(UInt8(0x00))
270269
}
271270
}
272271

273-
if collector.useExpression {
272+
if useExpression {
274273
try encoder.encodeVector(collector.instructions) { instruction, encoder in
275274
var exprEncoder = ExpressionEncoder()
276275
switch instruction {

0 commit comments

Comments
 (0)