Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
work
  • Loading branch information
kripken committed Feb 16, 2024
commit 2ebdec6fd9f46f4630409fec05e133f0a57355c7
21 changes: 16 additions & 5 deletions src/support/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,26 +159,36 @@ std::ostream& printEscapedJSON(std::ostream& os, const std::string_view str) {
os << "\\\"";
continue;
case '\'':
os << "\\'";
os << "'";
continue;
case '\\':
os << "\\\\";
continue;
default: {
auto uEscape = [&](uint32_t v) {
os << std::hex;
os << "\\u";
os << ((v >> 24) & 0xff);
os << ((v >> 16) & 0xff);
os << ((v >> 8) & 0xff);
os << (v & 0xff);
os << std::dec;
};

// Based off of
// https://github.com/emscripten-core/emscripten/blob/59e6b8f1262d75585d8416b728e8cbb3db176fe2/src/library_strings.js#L72-L91
if (!(u0 & 0x80)) {
if (u0 >= 32 && u0 < 127) {
os << char(u0);
} else {
os << std::hex << "\\u00" << (u0 / 16) << (u0 % 16) << std::dec;
uEscape(u0);
}
continue;
}
i++;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to check that we haven't run off the end of the string whenever we increment i.

int u1 = str[i] & 63;
if ((u0 & 0xE0) == 0xC0) {
os << std::hex << "\\u" << (((u0 & 31) << 6) | u1) << std::dec;
uEscape((((u0 & 31) << 6) | u1));
continue;
}
i++;
Expand All @@ -191,10 +201,11 @@ std::ostream& printEscapedJSON(std::ostream& os, const std::string_view str) {
}

if (u0 < 0x10000) {
os << std::hex << "\\u" << u0 << std::dec;
uEscape(u0);
} else {
auto ch = u0 - 0x10000;
os << std::hex << "\\u" << (0xD800 | (ch >> 10)) << "\\u" << (0xDC00 | (ch & 0x3FF)) << std::dec;
uEscape(0xD800 | (ch >> 10));
uEscape(0xDC00 | (ch & 0x3FF));
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions test/lit/passes/string-lowering-instructions.wast
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@

;; CHECK: (import "wasm:js-string" "substring" (func $substring (type $24) (param externref i32 i32) (result (ref extern))))

;; CHECK: (export "export.1" (func $exported-string-returner))

;; CHECK: (export "export.2" (func $exported-string-receiver))

;; CHECK: (func $string.as (type $18) (param $a externref) (param $a_nn (ref extern)) (param $b externref) (param $c externref) (param $d externref) (param $nn_view (ref extern))
;; CHECK-NEXT: (local.set $b
;; CHECK-NEXT: (ref.as_non_null
Expand Down
2 changes: 2 additions & 0 deletions test/lit/passes/string-lowering.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ var sections = WebAssembly.Module.customSections(module, 'string.consts');
var array = new Uint8Array(sections[0]);
var string = new TextDecoder('utf-8').decode(array);
console.log("JSON:", string);
var json = JSON.parse(string);
//console.log("JSON:", json);