@@ -29,7 +29,6 @@ using v8::Maybe; |
29 | 29 | using v8::MaybeLocal; |
30 | 30 | using v8::Name; |
31 | 31 | using v8::NamedPropertyHandlerConfiguration; |
32 | | -using v8::Null; |
33 | 32 | using v8::Object; |
34 | 33 | using v8::PropertyAttribute; |
35 | 34 | using v8::PropertyCallbackInfo; |
@@ -40,7 +39,7 @@ using v8::Uint32; |
40 | 39 | using v8::Value; |
41 | 40 |
|
42 | 41 | #define THROW_SQLITE_ERROR(env, r) \ |
43 | | - node::THROW_ERR_INVALID_STATE((env), sqlite3_errstr((r))) |
| 42 | + THROW_ERR_INVALID_STATE((env), sqlite3_errstr((r))) |
44 | 43 |
|
45 | 44 | #define CHECK_ERROR_OR_THROW(env, expr, expected, ret) \ |
46 | 45 | do { \ |
@@ -80,7 +79,7 @@ static void ThrowQuotaExceededException(Local<Context> context) { |
80 | 79 | Storage::Storage(Environment* env, Local<Object> object, Local<String> location) |
81 | 80 | : BaseObject(env, object) { |
82 | 81 | MakeWeak(); |
83 | | - node::Utf8Value utf8_location(env->isolate(), location); |
| 82 | + Utf8Value utf8_location(env->isolate(), location); |
84 | 83 | symbols_.Reset(env->isolate(), Map::New(env->isolate())); |
85 | 84 | db_ = nullptr; |
86 | 85 | location_ = utf8_location.ToString(); |
@@ -97,9 +96,9 @@ void Storage::MemoryInfo(MemoryTracker* tracker) const { |
97 | 96 |
|
98 | 97 | bool Storage::Open() { |
99 | 98 | static const int kCurrentSchemaVersion = 1; |
100 | | - static const char get_schema_version_sql[] = |
| 99 | + static constexpr std::string_view get_schema_version_sql = |
101 | 100 | "SELECT schema_version FROM nodejs_webstorage_state"; |
102 | | - static const char init_sql_v0[] = |
| 101 | + static constexpr std::string_view init_sql_v0 = |
103 | 102 | "PRAGMA encoding = 'UTF-16le';" |
104 | 103 | "PRAGMA busy_timeout = 3000;" |
105 | 104 | "PRAGMA journal_mode = WAL;" |
@@ -165,13 +164,14 @@ bool Storage::Open() { |
165 | 164 |
|
166 | 165 | int r = sqlite3_open(location_.c_str(), &db); |
167 | 166 | CHECK_ERROR_OR_THROW(env(), r, SQLITE_OK, false); |
168 | | - r = sqlite3_exec(db, init_sql_v0, 0, 0, nullptr); |
| 167 | + r = sqlite3_exec(db, init_sql_v0.data(), 0, 0, nullptr); |
169 | 168 | CHECK_ERROR_OR_THROW(env(), r, SQLITE_OK, false); |
170 | 169 |
|
171 | 170 | // Get the current schema version, used to determine schema migrations. |
172 | 171 | sqlite3_stmt* s = nullptr; |
173 | | - r = sqlite3_prepare_v2(db, get_schema_version_sql, -1, &s, 0); |
174 | | - r = sqlite3_exec(db, init_sql_v0, 0, 0, nullptr); |
| 172 | + r = sqlite3_prepare_v2( |
| 173 | + db, get_schema_version_sql.data(), get_schema_version_sql.size(), &s, 0); |
| 174 | + r = sqlite3_exec(db, init_sql_v0.data(), 0, 0, nullptr); |
175 | 175 | CHECK_ERROR_OR_THROW(env(), r, SQLITE_OK, false); |
176 | 176 | auto stmt = stmt_unique_ptr(s); |
177 | 177 | CHECK_ERROR_OR_THROW(env(), sqlite3_step(stmt.get()), SQLITE_ROW, false); |
@@ -180,7 +180,7 @@ bool Storage::Open() { |
180 | 180 | stmt = nullptr; // Force finalization. |
181 | 181 |
|
182 | 182 | if (schema_version > kCurrentSchemaVersion) { |
183 | | - node::THROW_ERR_INVALID_STATE( |
| 183 | + THROW_ERR_INVALID_STATE( |
184 | 184 | env(), "localStorage was created with a newer version of Node.js"); |
185 | 185 | return false; |
186 | 186 | } |
@@ -217,10 +217,13 @@ void Storage::Clear() { |
217 | 217 | return; |
218 | 218 | } |
219 | 219 |
|
220 | | - static const char sql[] = "DELETE FROM nodejs_webstorage"; |
| 220 | + static constexpr std::string_view sql = "DELETE FROM nodejs_webstorage"; |
221 | 221 | sqlite3_stmt* s = nullptr; |
222 | 222 | CHECK_ERROR_OR_THROW( |
223 | | - env(), sqlite3_prepare_v2(db_.get(), sql, -1, &s, 0), SQLITE_OK, void()); |
| 223 | + env(), |
| 224 | + sqlite3_prepare_v2(db_.get(), sql.data(), sql.size(), &s, 0), |
| 225 | + SQLITE_OK, |
| 226 | + void()); |
224 | 227 | auto stmt = stmt_unique_ptr(s); |
225 | 228 | CHECK_ERROR_OR_THROW(env(), sqlite3_step(stmt.get()), SQLITE_DONE, void()); |
226 | 229 | } |
@@ -230,9 +233,9 @@ Local<Array> Storage::Enumerate() { |
230 | 233 | return Local<Array>(); |
231 | 234 | } |
232 | 235 |
|
233 | | - static const char sql[] = "SELECT key FROM nodejs_webstorage"; |
| 236 | + static constexpr std::string_view sql = "SELECT key FROM nodejs_webstorage"; |
234 | 237 | sqlite3_stmt* s = nullptr; |
235 | | - int r = sqlite3_prepare_v2(db_.get(), sql, -1, &s, 0); |
| 238 | + int r = sqlite3_prepare_v2(db_.get(), sql.data(), sql.size(), &s, 0); |
236 | 239 | CHECK_ERROR_OR_THROW(env(), r, SQLITE_OK, Local<Array>()); |
237 | 240 | auto stmt = stmt_unique_ptr(s); |
238 | 241 | std::vector<Local<Value>> values; |
@@ -253,12 +256,13 @@ Local<Array> Storage::Enumerate() { |
253 | 256 |
|
254 | 257 | Local<Value> Storage::Length() { |
255 | 258 | if (!Open()) { |
256 | | - return Local<Value>(); |
| 259 | + return {}; |
257 | 260 | } |
258 | 261 |
|
259 | | - static const char sql[] = "SELECT count(*) FROM nodejs_webstorage"; |
| 262 | + static constexpr std::string_view sql = |
| 263 | + "SELECT count(*) FROM nodejs_webstorage"; |
260 | 264 | sqlite3_stmt* s = nullptr; |
261 | | - int r = sqlite3_prepare_v2(db_.get(), sql, -1, &s, 0); |
| 265 | + int r = sqlite3_prepare_v2(db_.get(), sql.data(), sql.size(), &s, 0); |
262 | 266 | CHECK_ERROR_OR_THROW(env(), r, SQLITE_OK, Local<Value>()); |
263 | 267 | auto stmt = stmt_unique_ptr(s); |
264 | 268 | CHECK_ERROR_OR_THROW( |
@@ -276,16 +280,16 @@ Local<Value> Storage::Load(Local<Name> key) { |
276 | 280 | } |
277 | 281 |
|
278 | 282 | if (!Open()) { |
279 | | - return Local<Value>(); |
| 283 | + return {}; |
280 | 284 | } |
281 | 285 |
|
282 | | - static const char sql[] = |
| 286 | + static constexpr std::string_view sql = |
283 | 287 | "SELECT value FROM nodejs_webstorage WHERE key = ? LIMIT 1"; |
284 | 288 | sqlite3_stmt* s = nullptr; |
285 | | - int r = sqlite3_prepare_v2(db_.get(), sql, -1, &s, 0); |
| 289 | + int r = sqlite3_prepare_v2(db_.get(), sql.data(), sql.size(), &s, 0); |
286 | 290 | CHECK_ERROR_OR_THROW(env(), r, SQLITE_OK, Local<Value>()); |
287 | 291 | auto stmt = stmt_unique_ptr(s); |
288 | | - node::TwoByteValue utf16key(env()->isolate(), key); |
| 292 | + TwoByteValue utf16key(env()->isolate(), key); |
289 | 293 | auto key_size = utf16key.length() * sizeof(uint16_t); |
290 | 294 | r = sqlite3_bind_blob(stmt.get(), 1, utf16key.out(), key_size, SQLITE_STATIC); |
291 | 295 | CHECK_ERROR_OR_THROW(env(), r, SQLITE_OK, Local<Value>()); |
@@ -312,10 +316,10 @@ Local<Value> Storage::LoadKey(const int index) { |
312 | 316 | return Local<Value>(); |
313 | 317 | } |
314 | 318 |
|
315 | | - static const char sql[] = |
| 319 | + static constexpr std::string_view sql = |
316 | 320 | "SELECT key FROM nodejs_webstorage LIMIT 1 OFFSET ?"; |
317 | 321 | sqlite3_stmt* s = nullptr; |
318 | | - int r = sqlite3_prepare_v2(db_.get(), sql, -1, &s, 0); |
| 322 | + int r = sqlite3_prepare_v2(db_.get(), sql.data(), sql.size(), &s, 0); |
319 | 323 | CHECK_ERROR_OR_THROW(env(), r, SQLITE_OK, Local<Value>()); |
320 | 324 | auto stmt = stmt_unique_ptr(s); |
321 | 325 | r = sqlite3_bind_int(stmt.get(), 1, index); |
@@ -350,12 +354,13 @@ bool Storage::Remove(Local<Name> key) { |
350 | 354 | return false; |
351 | 355 | } |
352 | 356 |
|
353 | | - static const char sql[] = "DELETE FROM nodejs_webstorage WHERE key = ?"; |
| 357 | + static constexpr std::string_view sql = |
| 358 | + "DELETE FROM nodejs_webstorage WHERE key = ?"; |
354 | 359 | sqlite3_stmt* s = nullptr; |
355 | | - int r = sqlite3_prepare_v2(db_.get(), sql, -1, &s, 0); |
| 360 | + int r = sqlite3_prepare_v2(db_.get(), sql.data(), sql.size(), &s, 0); |
356 | 361 | CHECK_ERROR_OR_THROW(env(), r, SQLITE_OK, false); |
357 | 362 | auto stmt = stmt_unique_ptr(s); |
358 | | - node::TwoByteValue utf16key(env()->isolate(), key); |
| 363 | + TwoByteValue utf16key(env()->isolate(), key); |
359 | 364 | auto key_size = utf16key.length() * sizeof(uint16_t); |
360 | 365 | r = sqlite3_bind_blob(stmt.get(), 1, utf16key.out(), key_size, SQLITE_STATIC); |
361 | 366 | CHECK_ERROR_OR_THROW(env(), r, SQLITE_OK, false); |
@@ -379,14 +384,14 @@ bool Storage::Store(Local<Name> key, Local<Value> value) { |
379 | 384 | return false; |
380 | 385 | } |
381 | 386 |
|
382 | | - static const char sql[] = |
| 387 | + static constexpr std::string_view sql = |
383 | 388 | "INSERT INTO nodejs_webstorage (key, value) VALUES (?, ?)" |
384 | 389 | " ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value" |
385 | 390 | " WHERE EXCLUDED.key = key"; |
386 | 391 | sqlite3_stmt* s = nullptr; |
387 | | - node::TwoByteValue utf16key(env()->isolate(), key); |
388 | | - node::TwoByteValue utf16val(env()->isolate(), val); |
389 | | - int r = sqlite3_prepare_v2(db_.get(), sql, -1, &s, 0); |
| 392 | + TwoByteValue utf16key(env()->isolate(), key); |
| 393 | + TwoByteValue utf16val(env()->isolate(), val); |
| 394 | + int r = sqlite3_prepare_v2(db_.get(), sql.data(), sql.size(), &s, 0); |
390 | 395 | CHECK_ERROR_OR_THROW(env(), r, SQLITE_OK, false); |
391 | 396 | auto stmt = stmt_unique_ptr(s); |
392 | 397 | auto key_size = utf16key.length() * sizeof(uint16_t); |
@@ -435,7 +440,7 @@ static void GetItem(const FunctionCallbackInfo<Value>& info) { |
435 | 440 |
|
436 | 441 | Local<Value> result = storage->Load(prop); |
437 | 442 | if (result.IsEmpty()) { |
438 | | - info.GetReturnValue().Set(Null(env->isolate())); |
| 443 | + info.GetReturnValue().SetNull(); |
439 | 444 | } else { |
440 | 445 | info.GetReturnValue().Set(result); |
441 | 446 | } |
@@ -457,13 +462,13 @@ static void Key(const FunctionCallbackInfo<Value>& info) { |
457 | 462 | } |
458 | 463 |
|
459 | 464 | if (index < 0) { |
460 | | - info.GetReturnValue().Set(Null(env->isolate())); |
| 465 | + info.GetReturnValue().SetNull(); |
461 | 466 | return; |
462 | 467 | } |
463 | 468 |
|
464 | 469 | Local<Value> result = storage->LoadKey(index); |
465 | 470 | if (result.IsEmpty()) { |
466 | | - info.GetReturnValue().Set(Null(env->isolate())); |
| 471 | + info.GetReturnValue().SetNull(); |
467 | 472 | } else { |
468 | 473 | info.GetReturnValue().Set(result); |
469 | 474 | } |
|