Skip to content

Commit c12de34

Browse files
authored
Escape \t as well as \n when writing JSON output. (#4437)
As it happens, this doesn't (normally) break the resulting EM_ASM or EM_JS strings because (IIUC) JS supports the tab literal inside of strings as well as "\t". However, it's better to preserve the original text so that it looks the same in the JS file as it did in the original source.
1 parent 4952437 commit c12de34

File tree

5 files changed

+22
-17
lines changed

5 files changed

+22
-17
lines changed

src/wasm/wasm-emscripten.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ std::string escape(std::string code) {
7979
code = code.replace(curr, 2, "\\\\n");
8080
curr += 3; // skip this one
8181
}
82+
curr = 0;
83+
while ((curr = code.find("\\t", curr)) != std::string::npos) {
84+
code = code.replace(curr, 2, "\\\\t");
85+
curr += 3; // skip this one
86+
}
8287
// replace double quotes with escaped single quotes
8388
curr = 0;
8489
while ((curr = code.find('"', curr)) != std::string::npos) {

test/lld/em_asm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <emscripten/em_asm.h>
22

33
int main() {
4-
EM_ASM({ Module.print("Hello world"); });
4+
EM_ASM({ Module.print("Hello \\ world\t\n"); });
55
int x = EM_ASM_INT({ return $0 + $1; }, 13, 27);
66
EM_ASM_({ Module.print("Got " + $0); }, x);
77
return 0;

test/lld/em_asm.wat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
(import "env" "emscripten_asm_const_int" (func $emscripten_asm_const_int (param i32 i32 i32) (result i32)))
77
(memory $0 2)
88
(data $.rodata (i32.const 568) "\00ii\00i\00")
9-
(data $em_asm (i32.const 574) "{ Module.print(\"Hello world\"); }\00{ return $0 + $1; }\00{ Module.print(\"Got \" + $0); }\00")
9+
(data $em_asm (i32.const 574) "{ Module.print(\"Hello \\\\ world\\t\\n\"); }\00{ return $0 + $1; }\00{ Module.print(\"Got \" + $0); }\00")
1010
(table $0 1 1 funcref)
1111
(global $__stack_pointer (mut i32) (i32.const 66208))
1212
(global $global$1 i32 (i32.const 574))
13-
(global $global$2 i32 (i32.const 658))
13+
(global $global$2 i32 (i32.const 665))
1414
(export "memory" (memory $0))
1515
(export "__wasm_call_ctors" (func $__wasm_call_ctors))
1616
(export "main" (func $main))
@@ -42,7 +42,7 @@
4242
(i32.store
4343
(local.get $0)
4444
(call $emscripten_asm_const_int
45-
(i32.const 607)
45+
(i32.const 614)
4646
(i32.const 569)
4747
(i32.add
4848
(local.get $0)
@@ -52,7 +52,7 @@
5252
)
5353
(drop
5454
(call $emscripten_asm_const_int
55-
(i32.const 627)
55+
(i32.const 634)
5656
(i32.const 572)
5757
(local.get $0)
5858
)

test/lld/em_asm.wat.mem.out

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
(import "env" "emscripten_asm_const_int" (func $emscripten_asm_const_int (param i32 i32 i32) (result i32)))
77
(global $__stack_pointer (mut i32) (i32.const 66208))
88
(global $global$1 i32 (i32.const 574))
9-
(global $global$2 i32 (i32.const 658))
9+
(global $global$2 i32 (i32.const 665))
1010
(memory $0 2)
1111
(table $0 1 1 funcref)
1212
(export "memory" (memory $0))
@@ -39,7 +39,7 @@
3939
(i32.store
4040
(local.get $0)
4141
(call $emscripten_asm_const_int
42-
(i32.const 607)
42+
(i32.const 614)
4343
(i32.const 569)
4444
(i32.add
4545
(local.get $0)
@@ -49,7 +49,7 @@
4949
)
5050
(drop
5151
(call $emscripten_asm_const_int
52-
(i32.const 627)
52+
(i32.const 634)
5353
(i32.const 572)
5454
(local.get $0)
5555
)
@@ -70,9 +70,9 @@
7070
--BEGIN METADATA --
7171
{
7272
"asmConsts": {
73-
"574": "{ Module.print(\"Hello world\"); }",
74-
"607": "{ return $0 + $1; }",
75-
"627": "{ Module.print(\"Got \" + $0); }"
73+
"574": "{ Module.print(\"Hello \\ world\\t\\n\"); }",
74+
"614": "{ return $0 + $1; }",
75+
"634": "{ Module.print(\"Got \" + $0); }"
7676
},
7777
"declares": [
7878
"emscripten_asm_const_int"

test/lld/em_asm.wat.out

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
(import "env" "emscripten_asm_const_int" (func $emscripten_asm_const_int (param i32 i32 i32) (result i32)))
77
(global $__stack_pointer (mut i32) (i32.const 66208))
88
(global $global$1 i32 (i32.const 574))
9-
(global $global$2 i32 (i32.const 658))
9+
(global $global$2 i32 (i32.const 665))
1010
(memory $0 2)
1111
(data $.rodata (i32.const 568) "\00ii\00i\00")
1212
(data $em_asm (i32.const 574) "")
@@ -41,7 +41,7 @@
4141
(i32.store
4242
(local.get $0)
4343
(call $emscripten_asm_const_int
44-
(i32.const 607)
44+
(i32.const 614)
4545
(i32.const 569)
4646
(i32.add
4747
(local.get $0)
@@ -51,7 +51,7 @@
5151
)
5252
(drop
5353
(call $emscripten_asm_const_int
54-
(i32.const 627)
54+
(i32.const 634)
5555
(i32.const 572)
5656
(local.get $0)
5757
)
@@ -72,9 +72,9 @@
7272
--BEGIN METADATA --
7373
{
7474
"asmConsts": {
75-
"574": "{ Module.print(\"Hello world\"); }",
76-
"607": "{ return $0 + $1; }",
77-
"627": "{ Module.print(\"Got \" + $0); }"
75+
"574": "{ Module.print(\"Hello \\ world\\t\\n\"); }",
76+
"614": "{ return $0 + $1; }",
77+
"634": "{ Module.print(\"Got \" + $0); }"
7878
},
7979
"declares": [
8080
"emscripten_asm_const_int"

0 commit comments

Comments
 (0)