Skip to content

Commit d897bd3

Browse files
ryuhei shimaryuhei shima
authored andcommitted
fix review
1 parent 1e514ca commit d897bd3

2 files changed

Lines changed: 27 additions & 16 deletions

File tree

src/inspector/dom_storage_agent.cc

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include "dom_storage_agent.h"
2+
#include <optional>
23
#include "env-inl.h"
34
#include "inspector/inspector_object_utils.h"
5+
#include "util.h"
6+
#include "v8-exception.h"
47
#include "v8-isolate.h"
58

69
namespace node {
@@ -85,19 +88,19 @@ protocol::DispatchResponse DOMStorageAgent::getDOMStorageItems(
8588
"DOMStorage domain is not enabled");
8689
}
8790
bool is_local_storage = storageId->getIsLocalStorage();
88-
std::unique_ptr<std::unordered_map<std::string, std::string>> storage_map =
91+
std::optional<std::unordered_map<std::string, std::string>> storage_map =
8992
is_local_storage
90-
? std::make_unique<std::unordered_map<std::string, std::string>>(
93+
? std::make_optional<std::unordered_map<std::string, std::string>>(
9194
local_storage_map_)
92-
: std::make_unique<std::unordered_map<std::string, std::string>>(
95+
: std::make_optional<std::unordered_map<std::string, std::string>>(
9396
session_storage_map_);
9497
if (storage_map->empty()) {
9598
auto web_storage_obj = getWebStorage(is_local_storage);
9699
if (web_storage_obj) {
97100
std::unordered_map<std::string, std::string> all_items =
98101
web_storage_obj.value()->GetAll();
99102
storage_map =
100-
std::make_unique<std::unordered_map<std::string, std::string>>(
103+
std::make_optional<std::unordered_map<std::string, std::string>>(
101104
std::move(all_items));
102105
}
103106
}
@@ -258,17 +261,18 @@ void DOMStorageAgent::registerStorage(Local<Context> context,
258261

259262
std::optional<node::webstorage::Storage*> DOMStorageAgent::getWebStorage(
260263
bool is_local_storage) {
261-
std::string var_name = is_local_storage ? "localStorage" : "sessionStorage";
262264
v8::Isolate* isolate = env_->isolate();
263265
v8::HandleScope handle_scope(isolate);
264266
v8::Local<v8::Object> global = env_->context()->Global();
265267
v8::Local<v8::Value> web_storage_val;
268+
v8::TryCatch try_catch(isolate);
266269
if (!global
267270
->Get(env_->context(),
268-
v8::String::NewFromUtf8(env_->isolate(), var_name.c_str())
269-
.ToLocalChecked())
271+
is_local_storage
272+
? FIXED_ONE_BYTE_STRING(isolate, "localStorage")
273+
: FIXED_ONE_BYTE_STRING(isolate, "sessionStorage"))
270274
.ToLocal(&web_storage_val) ||
271-
!web_storage_val->IsObject()) {
275+
!web_storage_val->IsObject() || try_catch.HasCaught()) {
272276
return std::nullopt;
273277
} else {
274278
node::webstorage::Storage* storage;

src/node_webstorage.cc

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "node_errors.h"
1010
#include "node_mem-inl.h"
1111
#include "path.h"
12+
#include "simdutf.h"
1213
#include "sqlite3.h"
1314
#include "util-inl.h"
1415

@@ -297,17 +298,23 @@ std::unordered_map<std::string, std::string> Storage::GetAll() {
297298
auto key_size = sqlite3_column_bytes(stmt.get(), 0) / sizeof(uint16_t);
298299
auto value_size = sqlite3_column_bytes(stmt.get(), 1) / sizeof(uint16_t);
299300
auto key_uint16(
300-
reinterpret_cast<const uint16_t*>(sqlite3_column_blob(stmt.get(), 0)));
301+
reinterpret_cast<const char16_t*>(sqlite3_column_blob(stmt.get(), 0)));
301302
auto value_uint16(
302-
reinterpret_cast<const uint16_t*>(sqlite3_column_blob(stmt.get(), 1)));
303+
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);
303306
std::string key;
304-
for (size_t i = 0; i < key_size; ++i) {
305-
key.push_back(static_cast<char>(key_uint16[i]));
306-
}
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);
307313
std::string value;
308-
for (size_t i = 0; i < value_size; ++i) {
309-
value.push_back(static_cast<char>(value_uint16[i]));
310-
}
314+
value.resize(value_utf8_size);
315+
written =
316+
simdutf::convert_utf16_to_utf8(value_uint16, value_size, value.data());
317+
value.resize(written);
311318

312319
result.emplace(std::move(key), std::move(value));
313320
}

0 commit comments

Comments
 (0)