diff options
author | Alon Zakai <alonzakai@gmail.com> | 2015-12-14 19:14:00 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2015-12-14 19:14:00 -0800 |
commit | 3fe9c430f321a5cdf948a903509367ba872e37a6 (patch) | |
tree | 6767eef5743df9d6cbfa0c55341c1418e27c1866 | |
parent | 89e0212e5cb1824dc936a5994176de85f259df0b (diff) | |
download | binaryen-3fe9c430f321a5cdf948a903509367ba872e37a6.tar.gz binaryen-3fe9c430f321a5cdf948a903509367ba872e37a6.tar.bz2 binaryen-3fe9c430f321a5cdf948a903509367ba872e37a6.zip |
escape asm consts
-rw-r--r-- | src/s2wasm.h | 24 | ||||
-rw-r--r-- | test/dot_s/asm_const.wast | 2 |
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 |