27
27
#include " ir/utils.h"
28
28
#include " pass.h"
29
29
#include " passes/string-utils.h"
30
+ #include " support/json.h"
30
31
#include " support/string.h"
31
32
#include " wasm-builder.h"
32
33
#include " wasm.h"
@@ -58,8 +59,6 @@ struct StringLifting : public Pass {
58
59
//
59
60
// That is, they are imported from module "'" and the basename is the
60
61
// actual string. Find them all so we can apply them.
61
- //
62
- // TODO: parse the strings section for non-UTF16 strings.
63
62
Name stringConstsModule =
64
63
getArgumentOrDefault (" string-constants-module" , WasmStringConstsModule);
65
64
for (auto & global : module ->globals ) {
@@ -72,6 +71,44 @@ struct StringLifting : public Pass {
72
71
}
73
72
}
74
73
74
+ // Imported strings may also be found in the string section.
75
+ for (auto & section : module ->customSections ) {
76
+ if (section.name == " string.consts" ) {
77
+ // We found the string consts section. Parse it.
78
+ auto copy = section.data ;
79
+ json::Value array;
80
+ array.parse (copy.data ());
81
+ if (!array.isArray ()) {
82
+ Fatal ()
83
+ << " StringLifting: string.const section should be a JSON array" ;
84
+ }
85
+
86
+ // We have the array of constants from the section. Find globals that
87
+ // refer to it.
88
+ for (auto & global : module ->globals ) {
89
+ if (!global->imported () || global->module != " string.const" ) {
90
+ continue ;
91
+ }
92
+ // The index in the array is the basename.
93
+ Index index = std::stoi (std::string (global->base .str ));
94
+ if (index >= array.size ()) {
95
+ Fatal () << " StringLifting: bad index in string.const section" ;
96
+ }
97
+ auto item = array[index];
98
+ if (!item->isString ()) {
99
+ Fatal ()
100
+ << " StringLifting: string.const section entry is not a string" ;
101
+ }
102
+ if (importedStrings.count (global->name )) {
103
+ Fatal ()
104
+ << " StringLifting: string.const section tramples other const" ;
105
+ }
106
+ importedStrings[global->name ] = item->getIString ();
107
+ }
108
+ break ;
109
+ }
110
+ }
111
+
75
112
auto array16 = Type (Array (Field (Field::i16 , Mutable)), Nullable);
76
113
auto refExtern = Type (HeapType::ext, NonNullable);
77
114
auto externref = Type (HeapType::ext, Nullable);
0 commit comments