Skip to content
Prev Previous commit
Next Next commit
print string literals with WTF-8
  • Loading branch information
tlively committed Mar 22, 2024
commit 73a66e1c84861df504687ab63c911863fa3d0c4e
14 changes: 12 additions & 2 deletions src/wasm/literal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "ir/bits.h"
#include "pretty_printing.h"
#include "support/bits.h"
#include "support/string.h"
#include "support/utilities.h"

namespace wasm {
Expand Down Expand Up @@ -639,10 +640,19 @@ std::ostream& operator<<(std::ostream& o, Literal literal) {
o << "nullstring";
} else {
o << "string(\"";
// Convert WTF-16 literals to WTF-16 string.
std::stringstream wtf16;
for (auto c : data->values) {
// TODO: more than ascii
o << char(c.getInteger());
auto u = c.getInteger();
assert(u < 0x10000);
wtf16 << uint8_t(u & 0xFF);
wtf16 << uint8_t(u >> 8);
}
// Convert to WTF-8 for printing.
// TODO: Use wtf16.view() once we have C++20.
[[maybe_unused]] bool valid =
String::convertWTF16ToWTF8(o, wtf16.str());
assert(valid);
o << "\")";
}
break;
Expand Down