@@ -79,41 +79,45 @@ struct StringLifting : public Pass {
79
79
}
80
80
81
81
// Imported strings may also be found in the string section.
82
- for (auto & section : module->customSections ) {
83
- if (section.name == " string.consts" ) {
84
- // We found the string consts section. Parse it.
85
- auto copy = section.data ;
86
- json::Value array;
87
- array.parse (copy.data (), json::Value::WTF16);
88
- if (!array.isArray ()) {
82
+ auto stringSectionIter = std::find_if (
83
+ module->customSections .begin (),
84
+ module->customSections .end (),
85
+ [&](CustomSection& section) { return section.name == " string.consts" ; });
86
+ if (stringSectionIter != module->customSections .end ()) {
87
+ // We found the string consts section. Parse it.
88
+ auto & section = *stringSectionIter;
89
+ auto copy = section.data ;
90
+ json::Value array;
91
+ array.parse (copy.data (), json::Value::WTF16);
92
+ if (!array.isArray ()) {
93
+ Fatal () << " StringLifting: string.const section should be a JSON array" ;
94
+ }
95
+
96
+ // We have the array of constants from the section. Find globals that
97
+ // refer to it.
98
+ for (auto & global : module->globals ) {
99
+ if (!global->imported () || global->module != " string.const" ) {
100
+ continue ;
101
+ }
102
+ // The index in the array is the basename.
103
+ Index index = std::stoi (std::string (global->base .str ));
104
+ if (index >= array.size ()) {
105
+ Fatal () << " StringLifting: bad index in string.const section" ;
106
+ }
107
+ auto item = array[index ];
108
+ if (!item->isString ()) {
89
109
Fatal ()
90
- << " StringLifting: string.const section should be a JSON array " ;
110
+ << " StringLifting: string.const section entry is not a string " ;
91
111
}
92
-
93
- // We have the array of constants from the section. Find globals that
94
- // refer to it.
95
- for (auto & global : module->globals ) {
96
- if (!global->imported () || global->module != " string.const" ) {
97
- continue ;
98
- }
99
- // The index in the array is the basename.
100
- Index index = std::stoi (std::string (global->base .str ));
101
- if (index >= array.size ()) {
102
- Fatal () << " StringLifting: bad index in string.const section" ;
103
- }
104
- auto item = array[index ];
105
- if (!item->isString ()) {
106
- Fatal ()
107
- << " StringLifting: string.const section entry is not a string" ;
108
- }
109
- if (importedStrings.count (global->name )) {
110
- Fatal ()
111
- << " StringLifting: string.const section tramples other const" ;
112
- }
113
- importedStrings[global->name ] = item->getIString ();
112
+ if (importedStrings.count (global->name )) {
113
+ Fatal () << " StringLifting: string.const section tramples other const" ;
114
114
}
115
- break ;
115
+ importedStrings[global-> name ] = item-> getIString () ;
116
116
}
117
+
118
+ // Remove the custom section: After lifting it has no purpose (and could
119
+ // cause problems with repeated lifting/lowering).
120
+ module->customSections .erase (stringSectionIter);
117
121
}
118
122
119
123
auto array16 = Type (Array (Field (Field::i16, Mutable)), Nullable);
0 commit comments