Skip to content

Commit daf4fca

Browse files
authored
Format metadata json using mutliple lines for readability (#1804)
1 parent e4b9f0f commit daf4fca

12 files changed

+400
-67
lines changed

auto_update_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,8 @@ def update_reduce_tests():
421421

422422

423423
def main():
424-
update_asm_js_tests()
425424
update_lld_tests()
425+
return
426426
update_wasm_opt_tests()
427427
update_bin_fmt_tests()
428428
update_example_tests()

src/tools/wasm-emscripten-finalize.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ int main(int argc, const char *argv[]) {
222222
if (emitBinary) {
223223
std::cout << metadata;
224224
} else {
225-
output << ";; METADATA: " << metadata;
225+
output << "(;\n";
226+
output << "--BEGIN METADATA --\n" << metadata << "-- END METADATA --\n";
227+
output << ";)\n";
226228
}
227229

228230
return 0;

src/wasm/wasm-emscripten.cpp

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

test/lld/duplicate_imports.wast.out

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,40 @@
101101
)
102102
)
103103
)
104-
;; METADATA: { "asmConsts": {},"staticBump": 13, "initializers": ["__post_instantiate"], "declares": ["puts"], "externs": [], "implementedFunctions": ["___post_instantiate","_main","_stackSave","_stackAlloc","_stackRestore","___growWasmMemory"], "exports": ["memory","__post_instantiate","main","__heap_base","__data_end","stackSave","stackAlloc","stackRestore","__growWasmMemory"], "invokeFuncs": ["invoke_ffd"] }
104+
(;
105+
--BEGIN METADATA --
106+
{
107+
"staticBump": 13,
108+
"initializers": [
109+
"__post_instantiate"
110+
],
111+
"declares": [
112+
"puts"
113+
],
114+
"externs": [
115+
],
116+
"implementedFunctions": [
117+
"___post_instantiate",
118+
"_main",
119+
"_stackSave",
120+
"_stackAlloc",
121+
"_stackRestore",
122+
"___growWasmMemory"
123+
],
124+
"exports": [
125+
"memory",
126+
"__post_instantiate",
127+
"main",
128+
"__heap_base",
129+
"__data_end",
130+
"stackSave",
131+
"stackAlloc",
132+
"stackRestore",
133+
"__growWasmMemory"
134+
],
135+
"invokeFuncs": [
136+
"invoke_ffd"
137+
]
138+
}
139+
-- END METADATA --
140+
;)

test/lld/em_asm.wast.out

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,43 @@
7575
)
7676
)
7777
)
78-
;; METADATA: { "asmConsts": {"2": ["{ Module.print(\"Got \" + $0); }", ["ii"], [""]],"0": ["{ Module.print(\"Hello world\"); }", ["i"], [""]],"1": ["{ return $0 + $1; }", ["iii"], [""]]},"staticBump": 84, "initializers": ["__post_instantiate"], "declares": [], "externs": [], "implementedFunctions": ["___post_instantiate","_main","_stackSave","_stackAlloc","_stackRestore","___growWasmMemory"], "exports": ["memory","__post_instantiate","main","__heap_base","__data_end","stackSave","stackAlloc","stackRestore","__growWasmMemory"], "invokeFuncs": [] }
78+
(;
79+
--BEGIN METADATA --
80+
{
81+
"asmConsts": {
82+
"2": ["{ Module.print(\"Got \" + $0); }", ["ii"], [""]],
83+
"0": ["{ Module.print(\"Hello world\"); }", ["i"], [""]],
84+
"1": ["{ return $0 + $1; }", ["iii"], [""]]
85+
},
86+
"staticBump": 84,
87+
"initializers": [
88+
"__post_instantiate"
89+
],
90+
"declares": [
91+
],
92+
"externs": [
93+
],
94+
"implementedFunctions": [
95+
"___post_instantiate",
96+
"_main",
97+
"_stackSave",
98+
"_stackAlloc",
99+
"_stackRestore",
100+
"___growWasmMemory"
101+
],
102+
"exports": [
103+
"memory",
104+
"__post_instantiate",
105+
"main",
106+
"__heap_base",
107+
"__data_end",
108+
"stackSave",
109+
"stackAlloc",
110+
"stackRestore",
111+
"__growWasmMemory"
112+
],
113+
"invokeFuncs": [
114+
]
115+
}
116+
-- END METADATA --
117+
;)

test/lld/em_asm_table.wast.out

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,34 @@
6161
)
6262
)
6363
)
64-
;; METADATA: { "asmConsts": {},"staticBump": 480, "initializers": [], "declares": ["emscripten_log"], "externs": [], "implementedFunctions": ["_stackSave","_stackAlloc","_stackRestore","___growWasmMemory","_dynCall_vii","_dynCall_iiii"], "exports": ["__data_end","stackSave","stackAlloc","stackRestore","__growWasmMemory","dynCall_vii","dynCall_iiii"], "invokeFuncs": [] }
64+
(;
65+
--BEGIN METADATA --
66+
{
67+
"staticBump": 480,
68+
"declares": [
69+
"emscripten_log"
70+
],
71+
"externs": [
72+
],
73+
"implementedFunctions": [
74+
"_stackSave",
75+
"_stackAlloc",
76+
"_stackRestore",
77+
"___growWasmMemory",
78+
"_dynCall_vii",
79+
"_dynCall_iiii"
80+
],
81+
"exports": [
82+
"__data_end",
83+
"stackSave",
84+
"stackAlloc",
85+
"stackRestore",
86+
"__growWasmMemory",
87+
"dynCall_vii",
88+
"dynCall_iiii"
89+
],
90+
"invokeFuncs": [
91+
]
92+
}
93+
-- END METADATA --
94+
;)

test/lld/hello_world.wast.mem.out

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,39 @@
5858
)
5959
)
6060
)
61-
;; METADATA: { "asmConsts": {},"staticBump": 13, "initializers": ["__post_instantiate"], "declares": ["puts"], "externs": [], "implementedFunctions": ["___post_instantiate","_main","_stackSave","_stackAlloc","_stackRestore","___growWasmMemory"], "exports": ["memory","__post_instantiate","main","__heap_base","__data_end","stackSave","stackAlloc","stackRestore","__growWasmMemory"], "invokeFuncs": [] }
61+
(;
62+
--BEGIN METADATA --
63+
{
64+
"staticBump": 13,
65+
"initializers": [
66+
"__post_instantiate"
67+
],
68+
"declares": [
69+
"puts"
70+
],
71+
"externs": [
72+
],
73+
"implementedFunctions": [
74+
"___post_instantiate",
75+
"_main",
76+
"_stackSave",
77+
"_stackAlloc",
78+
"_stackRestore",
79+
"___growWasmMemory"
80+
],
81+
"exports": [
82+
"memory",
83+
"__post_instantiate",
84+
"main",
85+
"__heap_base",
86+
"__data_end",
87+
"stackSave",
88+
"stackAlloc",
89+
"stackRestore",
90+
"__growWasmMemory"
91+
],
92+
"invokeFuncs": [
93+
]
94+
}
95+
-- END METADATA --
96+
;)

0 commit comments

Comments
 (0)