summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/s2wasm.h24
-rw-r--r--test/dot_s/asm_const.wast2
2 files changed, 24 insertions, 2 deletions
diff --git a/src/s2wasm.h b/src/s2wasm.h
index bdeb78fdd..5fccef19b 100644
--- a/src/s2wasm.h
+++ b/src/s2wasm.h
@@ -880,11 +880,33 @@ public:
if (curr->target == EMSCRIPTEN_ASM_CONST) {
auto arg = curr->operands[0]->cast<Const>();
size_t segmentIndex = parent->addressSegments[arg->value.geti32()];
- std::string code = parent->wasm.memory.segments[segmentIndex].data;
+ std::string code = escape(parent->wasm.memory.segments[segmentIndex].data);
std::string sig = getSig(curr);
sigsForCode[code].insert(sig);
}
}
+
+ std::string escape(const char *input) {
+ std::string code = input;
+ // replace newlines quotes with escaped newlines
+ size_t curr = 0;
+ while ((curr = code.find("\\n", curr)) != std::string::npos) {
+ code = code.replace(curr, 2, "\\\\n");
+ curr += 3; // skip this one
+ }
+ // replace double quotes with escaped single quotes
+ curr = 0;
+ while ((curr = code.find('"', curr)) != std::string::npos) {
+ if (curr == 0 || code[curr-1] != '\\') {
+ code = code.replace(curr, 1, "\\" "\"");
+ curr += 2; // skip this one
+ } else { // already escaped, escape the slash as well
+ code = code.replace(curr, 1, "\\" "\\" "\"");
+ curr += 3; // skip this one
+ }
+ }
+ return code;
+ }
};
AsmConstWalker walker(this);
walker.startWalk(&wasm);
diff --git a/test/dot_s/asm_const.wast b/test/dot_s/asm_const.wast
index 695c480b3..37ff45061 100644
--- a/test/dot_s/asm_const.wast
+++ b/test/dot_s/asm_const.wast
@@ -15,4 +15,4 @@
)
)
)
-; METADATA: { "asmConsts": {"{ Module.print("hello, world!"); }":["vi"]} } \ No newline at end of file
+; METADATA: { "asmConsts": {"{ Module.print(\"hello, world!\"); }":["vi"]} } \ No newline at end of file