summaryrefslogtreecommitdiff
path: root/src/passes/StringLowering.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/passes/StringLowering.cpp')
-rw-r--r--src/passes/StringLowering.cpp35
1 files changed, 24 insertions, 11 deletions
diff --git a/src/passes/StringLowering.cpp b/src/passes/StringLowering.cpp
index df2d66860..dd7428546 100644
--- a/src/passes/StringLowering.cpp
+++ b/src/passes/StringLowering.cpp
@@ -189,6 +189,13 @@ struct StringGathering : public Pass {
};
struct StringLowering : public StringGathering {
+ // If true, then encode well-formed strings as (import "'" "string...")
+ // instead of emitting them into the JSON custom section.
+ bool useMagicImports;
+
+ StringLowering(bool useMagicImports = false)
+ : useMagicImports(useMagicImports) {}
+
void run(Module* module) override {
if (!module->features.has(FeatureSet::Strings)) {
return;
@@ -217,25 +224,30 @@ struct StringLowering : public StringGathering {
}
void makeImports(Module* module) {
- Index importIndex = 0;
+ Index jsonImportIndex = 0;
std::stringstream json;
json << '[';
bool first = true;
- std::vector<Name> importedStrings;
for (auto& global : module->globals) {
if (global->init) {
if (auto* c = global->init->dynCast<StringConst>()) {
- global->module = "string.const";
- global->base = std::to_string(importIndex);
- importIndex++;
- global->init = nullptr;
-
- if (first) {
- first = false;
+ std::stringstream utf8;
+ if (useMagicImports &&
+ String::convertUTF16ToUTF8(utf8, c->string.str)) {
+ global->module = "'";
+ global->base = Name(utf8.str());
} else {
- json << ',';
+ global->module = "string.const";
+ global->base = std::to_string(jsonImportIndex);
+ if (first) {
+ first = false;
+ } else {
+ json << ',';
+ }
+ String::printEscapedJSON(json, c->string.str);
+ jsonImportIndex++;
}
- String::printEscapedJSON(json, c->string.str);
+ global->init = nullptr;
}
}
}
@@ -516,5 +528,6 @@ struct StringLowering : public StringGathering {
Pass* createStringGatheringPass() { return new StringGathering(); }
Pass* createStringLoweringPass() { return new StringLowering(); }
+Pass* createStringLoweringMagicImportPass() { return new StringLowering(true); }
} // namespace wasm