Skip to content

Commit 5c82977

Browse files
ryuhei shimaryuhei shima
authored andcommitted
support utf16
1 parent d897bd3 commit 5c82977

File tree

4 files changed

+28
-35
lines changed

4 files changed

+28
-35
lines changed

src/inspector/dom_storage_agent.cc

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,29 +88,29 @@ protocol::DispatchResponse DOMStorageAgent::getDOMStorageItems(
8888
"DOMStorage domain is not enabled");
8989
}
9090
bool is_local_storage = storageId->getIsLocalStorage();
91-
std::optional<std::unordered_map<std::string, std::string>> storage_map =
91+
std::optional<StorageMap> storage_map =
9292
is_local_storage
93-
? std::make_optional<std::unordered_map<std::string, std::string>>(
94-
local_storage_map_)
95-
: std::make_optional<std::unordered_map<std::string, std::string>>(
96-
session_storage_map_);
93+
? std::make_optional<StorageMap>(local_storage_map_)
94+
: std::make_optional<StorageMap>(session_storage_map_);
9795
if (storage_map->empty()) {
9896
auto web_storage_obj = getWebStorage(is_local_storage);
9997
if (web_storage_obj) {
100-
std::unordered_map<std::string, std::string> all_items =
101-
web_storage_obj.value()->GetAll();
98+
StorageMap all_items = web_storage_obj.value()->GetAll();
10299
storage_map =
103-
std::make_optional<std::unordered_map<std::string, std::string>>(
104-
std::move(all_items));
100+
std::make_optional<StorageMap>(std::move(all_items));
105101
}
106102
}
107103

108104
auto result =
109105
std::make_unique<protocol::Array<protocol::Array<protocol::String>>>();
110106
for (const auto& pair : *storage_map) {
111107
auto item = std::make_unique<protocol::Array<protocol::String>>();
112-
item->push_back(pair.first);
113-
item->push_back(pair.second);
108+
item->push_back(
109+
protocol::StringUtil::fromUTF16(reinterpret_cast<const uint16_t*>(pair.first.data()),
110+
pair.first.size()));
111+
item->push_back(
112+
protocol::StringUtil::fromUTF16(reinterpret_cast<const uint16_t*>(pair.second.data()),
113+
pair.second.size()));
114114
result->push_back(std::move(item));
115115
}
116116
*items = std::move(result);
@@ -237,7 +237,7 @@ void DOMStorageAgent::registerStorage(Local<Context> context,
237237
.ToLocal(&storage_map_obj)) {
238238
return;
239239
}
240-
std::unordered_map<std::string, std::string>& storage_map =
240+
StorageMap& storage_map =
241241
is_local_storage ? local_storage_map_ : session_storage_map_;
242242
Local<Array> property_names;
243243
if (!storage_map_obj->GetOwnPropertyNames(context).ToLocal(&property_names)) {
@@ -253,10 +253,13 @@ void DOMStorageAgent::registerStorage(Local<Context> context,
253253
if (!storage_map_obj->Get(context, key_value).ToLocal(&value_value)) {
254254
return;
255255
}
256-
node::Utf8Value key_utf8(isolate, key_value);
257-
node::Utf8Value value_utf8(isolate, value_value);
258-
storage_map[*key_utf8] = *value_utf8;
259-
}
256+
node::TwoByteValue key_utf16(isolate, key_value);
257+
node::TwoByteValue value_utf16(isolate, value_value);
258+
storage_map[std::u16string(
259+
reinterpret_cast<const char16_t*>(*key_utf16), key_utf16.length())] =
260+
std::u16string(reinterpret_cast<const char16_t*>(*value_utf16),
261+
value_utf16.length());
262+
}
260263
}
261264

262265
std::optional<node::webstorage::Storage*> DOMStorageAgent::getWebStorage(

src/inspector/dom_storage_agent.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ class DOMStorageAgent : public protocol::DOMStorage::Backend,
5252
DOMStorageAgent& operator=(const DOMStorageAgent&) = delete;
5353

5454
private:
55+
typedef std::unordered_map<std::u16string, std::u16string> StorageMap;
5556
std::optional<node::webstorage::Storage*> getWebStorage(
5657
bool is_local_storage);
5758
std::unique_ptr<protocol::DOMStorage::Frontend> frontend_;
58-
std::unordered_map<std::string, std::string> local_storage_map_ = {};
59-
std::unordered_map<std::string, std::string> session_storage_map_ = {};
59+
StorageMap local_storage_map_ = {};
60+
StorageMap session_storage_map_ = {};
6061
bool enabled_ = false;
6162
Environment* env_;
6263
};

src/node_webstorage.cc

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ MaybeLocal<Array> Storage::Enumerate() {
281281
return Array::New(env()->isolate(), values.data(), values.size());
282282
}
283283

284-
std::unordered_map<std::string, std::string> Storage::GetAll() {
284+
std::unordered_map<std::u16string, std::u16string> Storage::GetAll() {
285285
if (!Open().IsJust()) {
286286
return {};
287287
}
@@ -291,7 +291,7 @@ std::unordered_map<std::string, std::string> Storage::GetAll() {
291291
sqlite3_stmt* s = nullptr;
292292
int r = sqlite3_prepare_v2(db_.get(), sql.data(), sql.size(), &s, nullptr);
293293
auto stmt = stmt_unique_ptr(s);
294-
std::unordered_map<std::string, std::string> result;
294+
std::unordered_map<std::u16string, std::u16string> result;
295295
while ((r = sqlite3_step(stmt.get())) == SQLITE_ROW) {
296296
CHECK(sqlite3_column_type(stmt.get(), 0) == SQLITE_BLOB);
297297
CHECK(sqlite3_column_type(stmt.get(), 1) == SQLITE_BLOB);
@@ -301,20 +301,9 @@ std::unordered_map<std::string, std::string> Storage::GetAll() {
301301
reinterpret_cast<const char16_t*>(sqlite3_column_blob(stmt.get(), 0)));
302302
auto value_uint16(
303303
reinterpret_cast<const char16_t*>(sqlite3_column_blob(stmt.get(), 1)));
304-
size_t key_utf8_size =
305-
simdutf::utf8_length_from_utf16(key_uint16, key_size);
306-
std::string key;
307-
key.resize(key_utf8_size);
308-
size_t written =
309-
simdutf::convert_utf16_to_utf8(key_uint16, key_size, key.data());
310-
key.resize(written);
311-
size_t value_utf8_size =
312-
simdutf::utf8_length_from_utf16(value_uint16, value_size);
313-
std::string value;
314-
value.resize(value_utf8_size);
315-
written =
316-
simdutf::convert_utf16_to_utf8(value_uint16, value_size, value.data());
317-
value.resize(written);
304+
305+
std::u16string key(key_uint16, key_size);
306+
std::u16string value(value_uint16, value_size);
318307

319308
result.emplace(std::move(key), std::move(value));
320309
}

src/node_webstorage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Storage : public BaseObject {
4141
v8::MaybeLocal<v8::Value> LoadKey(const int index);
4242
v8::Maybe<void> Remove(v8::Local<v8::Name> key);
4343
v8::Maybe<void> Store(v8::Local<v8::Name> key, v8::Local<v8::Value> value);
44-
std::unordered_map<std::string, std::string> GetAll();
44+
std::unordered_map<std::u16string, std::u16string> GetAll();
4545

4646
SET_MEMORY_INFO_NAME(Storage)
4747
SET_SELF_SIZE(Storage)

0 commit comments

Comments
 (0)