@@ -83,6 +83,16 @@ void WasmBinaryWriter::finishSection(int32_t start) {
83
83
o.writeAt (start, U32LEB (size));
84
84
}
85
85
86
+ int32_t WasmBinaryWriter::startSubsection (BinaryConsts::UserSections::Subsection code) {
87
+ o << U32LEB (code);
88
+ return writeU32LEBPlaceholder (); // section size to be filled in later
89
+ }
90
+
91
+ void WasmBinaryWriter::finishSubsection (int32_t start) {
92
+ int32_t size = o.size () - start - 5 ; // section size does not include the 5 bytes of the size field itself
93
+ o.writeAt (start, U32LEB (size));
94
+ }
95
+
86
96
void WasmBinaryWriter::writeStart () {
87
97
if (!wasm->start .is ()) return ;
88
98
if (debug) std::cerr << " == writeStart" << std::endl;
@@ -389,21 +399,24 @@ void WasmBinaryWriter::writeNames() {
389
399
if (debug) std::cerr << " == writeNames" << std::endl;
390
400
auto start = startSection (BinaryConsts::Section::User);
391
401
writeInlineString (BinaryConsts::UserSections::Name);
402
+ auto substart = startSubsection (BinaryConsts::UserSections::Subsection::NameFunction);
392
403
o << U32LEB (mappedFunctions.size ());
393
404
Index emitted = 0 ;
394
405
for (auto & import : wasm->imports ) {
395
406
if (import->kind == ExternalKind::Function) {
407
+ o << U32LEB (emitted);
396
408
writeInlineString (import->name .str );
397
- o << U32LEB (0 ); // TODO: locals
398
409
emitted++;
399
410
}
400
411
}
401
412
for (auto & curr : wasm->functions ) {
413
+ o << U32LEB (emitted);
402
414
writeInlineString (curr->name .str );
403
- o << U32LEB (0 ); // TODO: locals
404
415
emitted++;
405
416
}
406
417
assert (emitted == mappedFunctions.size ());
418
+ finishSubsection (substart);
419
+ /* TODO: locals */
407
420
finishSection (start);
408
421
}
409
422
@@ -969,7 +982,7 @@ void WasmBinaryBuilder::readUserSection(size_t payloadLen) {
969
982
auto oldPos = pos;
970
983
Name sectionName = getInlineString ();
971
984
if (sectionName.equals (BinaryConsts::UserSections::Name)) {
972
- readNames ();
985
+ readNames (payloadLen - (pos - oldPos) );
973
986
} else {
974
987
// an unfamiliar custom section
975
988
wasm.userSections .resize (wasm.userSections .size () + 1 );
@@ -1510,25 +1523,36 @@ void WasmBinaryBuilder::readTableElements() {
1510
1523
}
1511
1524
}
1512
1525
1513
- void WasmBinaryBuilder::readNames () {
1526
+ void WasmBinaryBuilder::readNames (size_t payloadLen ) {
1514
1527
if (debug) std::cerr << " == readNames" << std::endl;
1515
- auto num = getU32LEB ();
1516
- if (num == 0 ) return ;
1517
- for (auto & import : wasm.imports ) {
1518
- if (import->kind == ExternalKind::Function) {
1519
- getInlineString (); // TODO: use this
1520
- auto numLocals = getU32LEB ();
1521
- WASM_UNUSED (numLocals);
1522
- assert (numLocals == 0 ); // TODO
1523
- if (--num == 0 ) return ;
1528
+ auto sectionPos = pos;
1529
+ while (pos < sectionPos + payloadLen) {
1530
+ auto nameType = getU32LEB ();
1531
+ auto subsectionSize = getU32LEB ();
1532
+ auto subsectionPos = pos;
1533
+ if (nameType != BinaryConsts::UserSections::Subsection::NameFunction) {
1534
+ // TODO: locals
1535
+ std::cerr << " unknown name subsection at " << pos << std::endl;
1536
+ pos = subsectionPos + subsectionSize;
1537
+ continue ;
1524
1538
}
1539
+ auto num = getU32LEB ();
1540
+ uint32_t importedFunctions = 0 ;
1541
+ for (auto & import : wasm.imports ) {
1542
+ if (import->kind != ExternalKind::Function) continue ;
1543
+ importedFunctions++;
1544
+ }
1545
+ for (size_t i = 0 ; i < num; i++) {
1546
+ auto index = getU32LEB ();
1547
+ if (index < importedFunctions) {
1548
+ getInlineString (); // TODO: use this
1549
+ } else if (index - importedFunctions < functions.size ()) {
1550
+ functions[index - importedFunctions]->name = getInlineString ();
1551
+ }
1552
+ }
1553
+ assert (pos == subsectionPos + subsectionSize);
1525
1554
}
1526
- for (size_t i = 0 ; i < num; i++) {
1527
- functions[i]->name = getInlineString ();
1528
- auto numLocals = getU32LEB ();
1529
- WASM_UNUSED (numLocals);
1530
- assert (numLocals == 0 ); // TODO
1531
- }
1555
+ assert (pos == sectionPos + payloadLen);
1532
1556
}
1533
1557
1534
1558
BinaryConsts::ASTNodes WasmBinaryBuilder::readExpression (Expression*& curr) {
0 commit comments