@@ -755,73 +755,76 @@ std::string EmscriptenGlueGenerator::generateEmscriptenMetadata(
755755 Address staticBump, std::vector<Name> const & initializerFunctions,
756756 unsigned numReservedFunctionPointers) {
757757 bool commaFirst;
758- auto maybeComma = [&commaFirst]() {
758+ auto nextElement = [&commaFirst]() {
759759 if (commaFirst) {
760760 commaFirst = false ;
761- return " " ;
761+ return " \n " ;
762762 } else {
763- return " ," ;
763+ return " ,\n " ;
764764 }
765765 };
766766
767767 std::stringstream meta;
768- meta << " { " ;
768+ meta << " {\n " ;
769769
770770 AsmConstWalker emAsmWalker = fixEmAsmConstsAndReturnWalker (wasm);
771771
772772 // print
773773 commaFirst = true ;
774- meta << " \" asmConsts\" : {" ;
775- for (auto & pair : emAsmWalker.sigsForCode ) {
776- auto & code = pair.first ;
777- auto & sigs = pair.second ;
778- meta << maybeComma ();
779- meta << ' "' << emAsmWalker.ids [code] << " \" : [\" " << code << " \" , " ;
780- printSet (meta, sigs);
781- meta << " , " ;
774+ if (!emAsmWalker.sigsForCode .empty ()) {
775+ meta << " \" asmConsts\" : {" ;
776+ for (auto & pair : emAsmWalker.sigsForCode ) {
777+ auto & code = pair.first ;
778+ auto & sigs = pair.second ;
779+ meta << nextElement ();
780+ meta << ' "' << emAsmWalker.ids [code] << " \" : [\" " << code << " \" , " ;
781+ printSet (meta, sigs);
782+ meta << " , " ;
782783
783- // TODO: proxying to main thread. Currently this is unsupported, so proxy
784- // mode is "none", represented by an empty string.
785- meta << " [\"\" ]" ;
784+ // TODO: proxying to main thread. Currently this is unsupported, so proxy
785+ // mode is "none", represented by an empty string.
786+ meta << " [\"\" ]" ;
786787
787- meta << " ]" ;
788+ meta << " ]" ;
789+ }
790+ meta << " \n },\n " ;
788791 }
789- meta << " }," ;
790792
791793 EmJsWalker emJsWalker = fixEmJsFuncsAndReturnWalker (wasm);
792- if (emJsWalker.codeByName .size () > 0 ) {
793- meta << " \" emJsFuncs\" : {" ;
794+ if (! emJsWalker.codeByName .empty () ) {
795+ meta << " \" emJsFuncs\" : {" ;
794796 commaFirst = true ;
795797 for (auto & pair : emJsWalker.codeByName ) {
796798 auto & name = pair.first ;
797799 auto & code = pair.second ;
798- meta << maybeComma ();
800+ meta << nextElement ();
799801 meta << ' "' << name << " \" : \" " << code << ' "' ;
800802 }
801- meta << " }, " ;
803+ meta << " \n }, \n " ;
802804 }
803805
804- meta << " \" staticBump\" : " << staticBump << " , " ;
806+ meta << " \" staticBump\" : " << staticBump << " ,\n " ;
805807
806- meta << " \" initializers\" : [" ;
807- commaFirst = true ;
808- for (const auto & func : initializerFunctions) {
809- meta << maybeComma ();
810- meta << " \" " << func.c_str () << " \" " ;
808+ if (!initializerFunctions.empty ()) {
809+ meta << " \" initializers\" : [" ;
810+ commaFirst = true ;
811+ for (const auto & func : initializerFunctions) {
812+ meta << nextElement ();
813+ meta << " \" " << func.c_str () << " \" " ;
814+ }
815+ meta << " \n ],\n " ;
811816 }
812- meta << " ]" ;
813817
814818 if (numReservedFunctionPointers) {
815819 JSCallWalker jsCallWalker = getJSCallWalker (wasm);
816- meta << " , " ;
817- meta << " \" jsCallStartIndex\" : " << jsCallWalker.jsCallStartIndex << " , " ;
818- meta << " \" jsCallFuncType\" : [" ;
820+ meta << " \" jsCallStartIndex\" : " << jsCallWalker.jsCallStartIndex << " ,\n " ;
821+ meta << " \" jsCallFuncType\" : [" ;
819822 commaFirst = true ;
820823 for (std::string sig : jsCallWalker.indirectlyCallableSigs ) {
821- meta << maybeComma ();
824+ meta << nextElement ();
822825 meta << " \" " << sig << " \" " ;
823826 }
824- meta << " ] " ;
827+ meta << " \n ], \n " ;
825828 }
826829
827830 // Avoid adding duplicate imports to `declares' or `invokeFuncs`. Even
@@ -833,55 +836,56 @@ std::string EmscriptenGlueGenerator::generateEmscriptenMetadata(
833836 // We use the `base` rather than the `name` of the imports here and below
834837 // becasue this is the externally visible name that the embedder (JS) will
835838 // see.
836- meta << " , \" declares\" : [" ;
839+ meta << " \" declares\" : [" ;
837840 commaFirst = true ;
838841 ModuleUtils::iterImportedFunctions (wasm, [&](Function* import ) {
839842 if (emJsWalker.codeByName .count (import ->base .str ) == 0 &&
840843 !import ->base .startsWith (EMSCRIPTEN_ASM_CONST.str ) &&
841844 !import ->base .startsWith (" invoke_" ) &&
842845 !import ->base .startsWith (" jsCall_" )) {
843846 if (declares.insert (import ->base .str ).second ) {
844- meta << maybeComma () << ' "' << import ->base .str << ' "' ;
847+ meta << nextElement () << ' "' << import ->base .str << ' "' ;
845848 }
846849 }
847850 });
848- meta << " ] " ;
851+ meta << " \n ], \n " ;
849852
850- meta << " , \" externs\" : [" ;
853+ meta << " \" externs\" : [" ;
851854 commaFirst = true ;
852855 ModuleUtils::iterImportedGlobals (wasm, [&](Global* import ) {
853- meta << maybeComma () << " \" _" << import ->base .str << ' "' ;
856+ meta << nextElement () << " \" _" << import ->base .str << ' "' ;
854857 });
855- meta << " ] " ;
858+ meta << " \n ], \n " ;
856859
857- meta << " , \" implementedFunctions\" : [" ;
858- commaFirst = true ;
859- for (const auto & ex : wasm.exports ) {
860- if (ex->kind == ExternalKind::Function) {
861- meta << maybeComma () << " \" _" << ex->name .str << ' "' ;
860+ if (!wasm.exports .empty ()) {
861+ meta << " \" implementedFunctions\" : [" ;
862+ commaFirst = true ;
863+ for (const auto & ex : wasm.exports ) {
864+ if (ex->kind == ExternalKind::Function) {
865+ meta << nextElement () << " \" _" << ex->name .str << ' "' ;
866+ }
862867 }
863- }
864- meta << " ]" ;
868+ meta << " \n ],\n " ;
865869
866- meta << " , \" exports\" : [" ;
867- commaFirst = true ;
868- for (const auto & ex : wasm.exports ) {
869- meta << maybeComma () << ' "' << ex->name .str << ' "' ;
870+ meta << " \" exports\" : [" ;
871+ commaFirst = true ;
872+ for (const auto & ex : wasm.exports ) {
873+ meta << nextElement () << ' "' << ex->name .str << ' "' ;
874+ }
875+ meta << " \n ],\n " ;
870876 }
871- meta << " ]" ;
872877
873- meta << " , \" invokeFuncs\" : [" ;
878+ meta << " \" invokeFuncs\" : [" ;
874879 commaFirst = true ;
875880 ModuleUtils::iterImportedFunctions (wasm, [&](Function* import ) {
876881 if (import ->base .startsWith (" invoke_" )) {
877882 if (invokeFuncs.insert (import ->base .str ).second ) {
878- meta << maybeComma () << ' "' << import ->base .str << ' "' ;
883+ meta << nextElement () << ' "' << import ->base .str << ' "' ;
879884 }
880885 }
881886 });
882- meta << " ]" ;
883-
884- meta << " }\n " ;
887+ meta << " \n ]\n " ;
888+ meta << " }\n " ;
885889
886890 return meta.str ();
887891}
0 commit comments