Skip to content

Commit

Permalink
src: switch from ToLocalChecked to ToLocal in node_webstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Jul 21, 2024
1 parent 6fc0218 commit 795c8a4
Showing 1 changed file with 69 additions and 29 deletions.
98 changes: 69 additions & 29 deletions src/node_webstorage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,19 @@ Local<Array> Storage::Enumerate() {
CHECK_ERROR_OR_THROW(env(), r, SQLITE_OK, Local<Array>());
auto stmt = stmt_unique_ptr(s);
std::vector<Local<Value>> values;
Local<Value> value;
while ((r = sqlite3_step(stmt.get())) == SQLITE_ROW) {
CHECK(sqlite3_column_type(stmt.get(), 0) == SQLITE_BLOB);
auto size = sqlite3_column_bytes(stmt.get(), 0) / sizeof(uint16_t);
values.emplace_back(
String::NewFromTwoByte(env()->isolate(),
reinterpret_cast<const uint16_t*>(
sqlite3_column_blob(stmt.get(), 0)),
v8::NewStringType::kNormal,
size)
.ToLocalChecked());
if (!String::NewFromTwoByte(env()->isolate(),
reinterpret_cast<const uint16_t*>(
sqlite3_column_blob(stmt.get(), 0)),
v8::NewStringType::kNormal,
size)
.ToLocal(&value)) {
return Local<Array>();
}
values.emplace_back(value);
}
CHECK_ERROR_OR_THROW(env(), r, SQLITE_DONE, Local<Array>());
return Array::New(env()->isolate(), values.data(), values.size());
Expand Down Expand Up @@ -298,12 +301,14 @@ Local<Value> Storage::Load(Local<Name> key) {
if (r == SQLITE_ROW) {
CHECK(sqlite3_column_type(stmt.get(), 0) == SQLITE_BLOB);
auto size = sqlite3_column_bytes(stmt.get(), 0) / sizeof(uint16_t);
value = String::NewFromTwoByte(env()->isolate(),
reinterpret_cast<const uint16_t*>(
sqlite3_column_blob(stmt.get(), 0)),
v8::NewStringType::kNormal,
size)
.ToLocalChecked();
if (!String::NewFromTwoByte(env()->isolate(),
reinterpret_cast<const uint16_t*>(
sqlite3_column_blob(stmt.get(), 0)),
v8::NewStringType::kNormal,
size)
.ToLocal(&value)) {
return {};
}
} else if (r != SQLITE_DONE) {
THROW_SQLITE_ERROR(env(), r);
}
Expand All @@ -313,7 +318,7 @@ Local<Value> Storage::Load(Local<Name> key) {

Local<Value> Storage::LoadKey(const int index) {
if (!Open()) {
return Local<Value>();
return {};
}

static constexpr std::string_view sql =
Expand All @@ -330,12 +335,14 @@ Local<Value> Storage::LoadKey(const int index) {
if (r == SQLITE_ROW) {
CHECK(sqlite3_column_type(stmt.get(), 0) == SQLITE_BLOB);
auto size = sqlite3_column_bytes(stmt.get(), 0) / sizeof(uint16_t);
value = String::NewFromTwoByte(env()->isolate(),
reinterpret_cast<const uint16_t*>(
sqlite3_column_blob(stmt.get(), 0)),
v8::NewStringType::kNormal,
size)
.ToLocalChecked();
if (!String::NewFromTwoByte(env()->isolate(),
reinterpret_cast<const uint16_t*>(
sqlite3_column_blob(stmt.get(), 0)),
v8::NewStringType::kNormal,
size)
.ToLocal(&value)) {
return {};
}
} else if (r != SQLITE_DONE) {
THROW_SQLITE_ERROR(env(), r);
}
Expand Down Expand Up @@ -411,10 +418,8 @@ bool Storage::Store(Local<Name> key, Local<Value> value) {
return true;
}

static Local<Name> Uint32ToName(Local<Context> context, uint32_t index) {
return Uint32::New(context->GetIsolate(), index)
->ToString(context)
.ToLocalChecked();
static MaybeLocal<String> Uint32ToName(Local<Context> context, uint32_t index) {
return Uint32::New(context->GetIsolate(), index)->ToString(context);
}

static void Clear(const FunctionCallbackInfo<Value>& info) {
Expand Down Expand Up @@ -615,33 +620,68 @@ static Intercepted StorageDefiner(Local<Name> property,
static Intercepted IndexedGetter(uint32_t index,
const PropertyCallbackInfo<Value>& info) {
Environment* env = Environment::GetCurrent(info);
return StorageGetter(Uint32ToName(env->context(), index), info);
Local<Name> name;
if (!Uint32ToName(env->context(), index).ToLocal(&name)) {
// There was an error converting the index to a name.
// We aren't going to return a result but let's indicate
// that we intercepted the operation.
return Intercepted::kYes;
}
return StorageGetter(name, info);
}

static Intercepted IndexedSetter(uint32_t index,
Local<Value> value,
const PropertyCallbackInfo<void>& info) {
Environment* env = Environment::GetCurrent(info);
return StorageSetter(Uint32ToName(env->context(), index), value, info);
Local<Name> name;
if (!Uint32ToName(env->context(), index).ToLocal(&name)) {
// There was an error converting the index to a name.
// We aren't going to return a result but let's indicate
// that we intercepted the operation.
return Intercepted::kYes;
}
return StorageSetter(name, value, info);
}

static Intercepted IndexedQuery(uint32_t index,
const PropertyCallbackInfo<Integer>& info) {
Environment* env = Environment::GetCurrent(info);
return StorageQuery(Uint32ToName(env->context(), index), info);
Local<Name> name;
if (!Uint32ToName(env->context(), index).ToLocal(&name)) {
// There was an error converting the index to a name.
// We aren't going to return a result but let's indicate
// that we intercepted the operation.
return Intercepted::kYes;
}
return StorageQuery(name, info);
}

static Intercepted IndexedDeleter(uint32_t index,
const PropertyCallbackInfo<Boolean>& info) {
Environment* env = Environment::GetCurrent(info);
return StorageDeleter(Uint32ToName(env->context(), index), info);
Local<Name> name;
if (!Uint32ToName(env->context(), index).ToLocal(&name)) {
// There was an error converting the index to a name.
// We aren't going to return a result but let's indicate
// that we intercepted the operation.
return Intercepted::kYes;
}
return StorageDeleter(name, info);
}

static Intercepted IndexedDefiner(uint32_t index,
const PropertyDescriptor& desc,
const PropertyCallbackInfo<void>& info) {
Environment* env = Environment::GetCurrent(info);
return StorageDefiner(Uint32ToName(env->context(), index), desc, info);
Local<Name> name;
if (!Uint32ToName(env->context(), index).ToLocal(&name)) {
// There was an error converting the index to a name.
// We aren't going to return a result but let's indicate
// that we intercepted the operation.
return Intercepted::kYes;
}
return StorageDefiner(name, desc, info);
}

static void StorageLengthGetter(const FunctionCallbackInfo<Value>& info) {
Expand Down

0 comments on commit 795c8a4

Please sign in to comment.