@@ -268,8 +268,13 @@ private sealed class BinaryWriter(module: Module, emitDebugInfo: Boolean) {
268
268
}
269
269
270
270
private def writeNameCustomSection (): Unit = {
271
- // Currently, we only emit the function names
271
+ writeFunctionNamesSubSection()
272
+ writeLocalNamesSubSection()
273
+ writeTypeNamesSubSection()
274
+ writeFieldNamesSubSection()
275
+ }
272
276
277
+ private def writeFunctionNamesSubSection (): Unit = {
273
278
val importFunctionNames = module.imports.collect {
274
279
case Import (_, _, ImportDesc .Func (id, origName, _)) if origName.isDefined =>
275
280
id -> origName
@@ -287,6 +292,61 @@ private sealed class BinaryWriter(module: Module, emitDebugInfo: Boolean) {
287
292
}
288
293
}
289
294
295
+ private def writeLocalNamesSubSection (): Unit = {
296
+ buf.byte(0x02 ) // local names
297
+ buf.byteLengthSubSection {
298
+ /* For simplicity, we generate one group for every defined function,
299
+ * even if they don't declare any named local.
300
+ */
301
+ buf.vec(module.funcs) { func =>
302
+ writeFuncIdx(func.id)
303
+ val namedLocals =
304
+ (func.params ::: func.locals).zipWithIndex.filter(_._1.originalName.isDefined)
305
+ buf.vec(namedLocals) { localAndIndex =>
306
+ buf.u32(localAndIndex._2)
307
+ buf.name(localAndIndex._1.originalName.get)
308
+ }
309
+ }
310
+ }
311
+ }
312
+
313
+ private def writeTypeNamesSubSection (): Unit = {
314
+ buf.byte(0x04 ) // type names
315
+ buf.byteLengthSubSection {
316
+ val namedTypes = module.types.flatMap(_.subTypes.filter(_.originalName.isDefined))
317
+ buf.vec(namedTypes) { subType =>
318
+ writeTypeIdx(subType.id)
319
+ buf.name(subType.originalName.get)
320
+ }
321
+ }
322
+ }
323
+
324
+ private def writeFieldNamesSubSection (): Unit = {
325
+ buf.byte(0x0a ) // field names
326
+ buf.byteLengthSubSection {
327
+ /* For simplicity, we generate one group for every struct type, even if
328
+ * they don't declare any named field.
329
+ */
330
+ val structSubTypes = for {
331
+ recType <- module.types
332
+ subType <- recType.subTypes
333
+ if subType.compositeType.isInstanceOf [StructType ]
334
+ } yield {
335
+ subType
336
+ }
337
+
338
+ buf.vec(structSubTypes) { subType =>
339
+ writeTypeIdx(subType.id)
340
+ val namedFields = subType.compositeType.asInstanceOf [StructType ].fields
341
+ .zipWithIndex.filter(_._1.originalName.isDefined)
342
+ buf.vec(namedFields) { fieldAndIndex =>
343
+ buf.u32(fieldAndIndex._2)
344
+ buf.name(fieldAndIndex._1.originalName.get)
345
+ }
346
+ }
347
+ }
348
+ }
349
+
290
350
private def writeFunc (func : Function ): Unit = {
291
351
emitStartFuncPosition(func.pos)
292
352
0 commit comments