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.cpp21
1 files changed, 19 insertions, 2 deletions
diff --git a/src/passes/StringLowering.cpp b/src/passes/StringLowering.cpp
index fa5c1c9a8..92e3268dc 100644
--- a/src/passes/StringLowering.cpp
+++ b/src/passes/StringLowering.cpp
@@ -194,8 +194,16 @@ struct StringLowering : public StringGathering {
// instead of emitting them into the JSON custom section.
bool useMagicImports;
- StringLowering(bool useMagicImports = false)
- : useMagicImports(useMagicImports) {}
+ // Whether to throw a fatal error on non-UTF8 strings that would not be able
+ // to use the "magic import" mechanism. Only usable in conjunction with magic
+ // imports.
+ bool assertUTF8;
+
+ StringLowering(bool useMagicImports = false, bool assertUTF8 = false)
+ : useMagicImports(useMagicImports), assertUTF8(assertUTF8) {
+ // If we are asserting valid UTF-8, we must be using magic imports.
+ assert(!assertUTF8 || useMagicImports);
+ }
void run(Module* module) override {
if (!module->features.has(FeatureSet::Strings)) {
@@ -238,6 +246,12 @@ struct StringLowering : public StringGathering {
global->module = "'";
global->base = Name(utf8.str());
} else {
+ if (assertUTF8) {
+ std::stringstream escaped;
+ String::printEscaped(escaped, utf8.str());
+ Fatal() << "Cannot lower non-UTF-16 string " << escaped.str()
+ << '\n';
+ }
global->module = "string.const";
global->base = std::to_string(jsonImportIndex);
if (first) {
@@ -534,5 +548,8 @@ struct StringLowering : public StringGathering {
Pass* createStringGatheringPass() { return new StringGathering(); }
Pass* createStringLoweringPass() { return new StringLowering(); }
Pass* createStringLoweringMagicImportPass() { return new StringLowering(true); }
+Pass* createStringLoweringMagicImportAssertPass() {
+ return new StringLowering(true, true);
+}
} // namespace wasm