Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
20 changes: 12 additions & 8 deletions src/passes/StringLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include "ir/type-updating.h"
#include "ir/utils.h"
#include "pass.h"
#include "support/json.h"
#include "support/string.h"
#include "wasm-builder.h"
#include "wasm.h"

Expand Down Expand Up @@ -205,8 +205,9 @@ struct StringLowering : public StringGathering {

void makeImports(Module* module) {
Index importIndex = 0;
json::Value stringArray;
stringArray.setArray();
std::stringstream json;
json << '[';
bool first = true;
std::vector<Name> importedStrings;
for (auto& global : module->globals) {
if (global->init) {
Expand All @@ -216,16 +217,19 @@ struct StringLowering : public StringGathering {
importIndex++;
global->init = nullptr;

auto str = json::Value::make(std::string(c->string.str).c_str());
stringArray.push_back(str);
if (first) {
first = false;
} else {
json << ',';
}
String::printEscaped(json, c->string.str, String::EscapeMode::JSON);
}
}
}

// Add a custom section with the JSON.
std::stringstream stream;
stringArray.stringify(stream);
auto str = stream.str();
json << ']';
auto str = json.str();
auto vec = std::vector<char>(str.begin(), str.end());
module->customSections.emplace_back(
CustomSection{"string.consts", std::move(vec)});
Expand Down
6 changes: 4 additions & 2 deletions src/support/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
*/

#include "support/json.h"
#include "support/string.h"

namespace json {

void Value::stringify(std::ostream& os, bool pretty) {
if (isString()) {
// TODO: escaping
os << '"' << getCString() << '"';
wasm::String::printEscaped(os,
getCString(),
wasm::String::EscapeMode::JSON);
} else if (isArray()) {
os << '[';
auto first = true;
Expand Down
10 changes: 8 additions & 2 deletions src/support/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ std::string trim(const std::string& input) {
return input.substr(0, size);
}

std::ostream& printEscaped(std::ostream& os, std::string_view str) {
std::ostream& printEscaped(std::ostream& os, std::string_view str, EscapeMode mode) {
os << '"';
for (unsigned char c : str) {
switch (c) {
Expand All @@ -132,7 +132,13 @@ std::ostream& printEscaped(std::ostream& os, std::string_view str) {
if (c >= 32 && c < 127) {
os << c;
} else {
os << std::hex << '\\' << (c / 16) << (c % 16) << std::dec;
if (mode == EscapeMode::Normal) {
os << std::hex << '\\' << (c / 16) << (c % 16) << std::dec;
} else if (mode == EscapeMode::JSON) {
os << std::hex << "\\u00" << (c / 16) << (c % 16) << std::dec;
Copy link
Member

Choose a reason for hiding this comment

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

JSON requires exactly 4 hex digits representing a UTF-16 code unit (i.e. 2 bytes at once), so it's going to be slightly more complicated than this, unfortunately.

} else {
WASM_UNREACHABLE("bad mode");
}
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/support/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@ inline bool isNumber(const std::string& str) {
return !str.empty() && std::all_of(str.begin(), str.end(), ::isdigit);
}

std::ostream& printEscaped(std::ostream& os, std::string_view str);
enum EscapeMode {
Normal,
JSON
};

std::ostream& printEscaped(std::ostream& os,
Copy link
Member

Choose a reason for hiding this comment

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

I think having a separate printJSONEscaped function would make more sense for this interface, even if the implementation still uses a flag.

std::string_view str,
EscapeMode mode = Normal);

} // namespace wasm::String

Expand Down
6 changes: 6 additions & 0 deletions test/lit/passes/string-lowering.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var filename = process.argv[2];
var module = new WebAssembly.Module(require('fs').readFileSync(filename));
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);
22 changes: 18 additions & 4 deletions test/lit/passes/string-lowering.wast
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
;; operations are tested in string-gathering.wast (which is auto-updated, unlike
;; this which is manual).

;; RUN: foreach %s %t wasm-opt --string-lowering -all -S -o - | filecheck %s

(module
(func $consts
(drop
Expand All @@ -15,9 +13,25 @@
(drop
(string.const "foo")
)
(drop
(string.const "needs\tescaping\00.")
)
)
)

;; The custom section should contain foo and bar, and foo only once.
;; CHECK: custom section "string.consts", size 13, contents: "[\"bar\",\"foo\"]"
;; The custom section should contain foo and bar, and foo only once, and the
;; string with \t should be escaped.
;;
;; RUN: wasm-opt %s --string-lowering -all -S -o - | filecheck %s
;;
;; CHECK: custom section "string.consts", size 38, contents: "[\"bar\",\"foo\",\"needs\\tescaping\\u0000.\"]"

;; The custom section should parse OK using JSON.parse from node.
;; (Note we run --remove-unused-module-elements to remove externref-using
;; imports, which require a newer version of node.)
;;
;; RUN: wasm-opt %s --string-lowering --remove-unused-module-elements -all -o %t.wasm
;; RUN: node %S/string-lowering.js %t.wasm | filecheck %s --check-prefix=CHECK-JS
;;
;; CHECK-JS: JSON: ["bar","foo","needs\tescaping\u0000."]