@@ -120,13 +120,15 @@ inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate, int errcode) {
120
120
const char * errstr = sqlite3_errstr (errcode);
121
121
122
122
Environment* env = Environment::GetCurrent (isolate);
123
- auto error = CreateSQLiteError (isolate, errstr).ToLocalChecked ();
124
- error
125
- ->Set (isolate->GetCurrentContext (),
126
- env->errcode_string (),
127
- Integer::New (isolate, errcode))
128
- .ToChecked ();
129
- isolate->ThrowException (error);
123
+ Local<Object> error;
124
+ if (CreateSQLiteError (isolate, errstr).ToLocal (&error) &&
125
+ error
126
+ ->Set (isolate->GetCurrentContext (),
127
+ env->errcode_string (),
128
+ Integer::New (isolate, errcode))
129
+ .IsJust ()) {
130
+ isolate->ThrowException (error);
131
+ }
130
132
}
131
133
132
134
class UserDefinedFunction {
@@ -755,12 +757,14 @@ void DatabaseSync::CreateSession(const FunctionCallbackInfo<Value>& args) {
755
757
}
756
758
}
757
759
758
- Local<String> db_key =
759
- String::NewFromUtf8 (env->isolate (), " db" , NewStringType::kNormal )
760
- .ToLocalChecked ();
760
+ Local<String> db_key = FIXED_ONE_BYTE_STRING (env->isolate (), " db" );
761
+
761
762
if (options->HasOwnProperty (env->context (), db_key).FromJust ()) {
762
- Local<Value> db_value =
763
- options->Get (env->context (), db_key).ToLocalChecked ();
763
+ Local<Value> db_value;
764
+ if (!options->Get (env->context (), db_key).ToLocal (&db_value)) {
765
+ // An error will have been scheduled.
766
+ return ;
767
+ }
764
768
if (db_value->IsString ()) {
765
769
String::Utf8Value str (env->isolate (), db_value);
766
770
db_name = std::string (*str);
@@ -829,8 +833,12 @@ void DatabaseSync::ApplyChangeset(const FunctionCallbackInfo<Value>& args) {
829
833
}
830
834
831
835
Local<Object> options = args[1 ].As <Object>();
832
- Local<Value> conflictValue =
833
- options->Get (env->context (), env->onconflict_string ()).ToLocalChecked ();
836
+ Local<Value> conflictValue;
837
+ if (!options->Get (env->context (), env->onconflict_string ())
838
+ .ToLocal (&conflictValue)) {
839
+ // An error will have been scheduled.
840
+ return ;
841
+ }
834
842
835
843
if (!conflictValue->IsUndefined ()) {
836
844
if (!conflictValue->IsFunction ()) {
@@ -858,8 +866,12 @@ void DatabaseSync::ApplyChangeset(const FunctionCallbackInfo<Value>& args) {
858
866
859
867
if (options->HasOwnProperty (env->context (), env->filter_string ())
860
868
.FromJust ()) {
861
- Local<Value> filterValue =
862
- options->Get (env->context (), env->filter_string ()).ToLocalChecked ();
869
+ Local<Value> filterValue;
870
+ if (!options->Get (env->context (), env->filter_string ())
871
+ .ToLocal (&filterValue)) {
872
+ // An error will have been scheduled.
873
+ return ;
874
+ }
863
875
864
876
if (!filterValue->IsFunction ()) {
865
877
THROW_ERR_INVALID_ARG_TYPE (
@@ -871,6 +883,10 @@ void DatabaseSync::ApplyChangeset(const FunctionCallbackInfo<Value>& args) {
871
883
Local<Function> filterFunc = filterValue.As <Function>();
872
884
873
885
filterCallback = [env, filterFunc](std::string item) -> bool {
886
+ // TODO(@jasnell): The use of ToLocalChecked here means that if
887
+ // the filter function throws an error the process will crash.
888
+ // The filterCallback should be updated to avoid the check and
889
+ // propagate the error correctly.
874
890
Local<Value> argv[] = {String::NewFromUtf8 (env->isolate (),
875
891
item.c_str (),
876
892
NewStringType::kNormal )
@@ -1240,11 +1256,18 @@ void StatementSync::IterateReturnCallback(
1240
1256
1241
1257
auto self = args.This ();
1242
1258
// iterator has fetch all result or break, prevent next func to return result
1243
- self->Set (context, env->isfinished_string (), Boolean::New (isolate, true ))
1244
- .ToChecked ();
1259
+ if (self->Set (context, env->isfinished_string (), Boolean::New (isolate, true ))
1260
+ .IsNothing ()) {
1261
+ // An error will have been scheduled.
1262
+ return ;
1263
+ }
1245
1264
1246
- auto external_stmt = Local<External>::Cast (
1247
- self->Get (context, env->statement_string ()).ToLocalChecked ());
1265
+ Local<Value> val;
1266
+ if (!self->Get (context, env->statement_string ()).ToLocal (&val)) {
1267
+ // An error will have been scheduled.
1268
+ return ;
1269
+ }
1270
+ auto external_stmt = Local<External>::Cast (val);
1248
1271
auto stmt = static_cast <StatementSync*>(external_stmt->Value ());
1249
1272
if (!stmt->IsFinalized ()) {
1250
1273
sqlite3_reset (stmt->statement_ );
@@ -1268,28 +1291,38 @@ void StatementSync::IterateNextCallback(
1268
1291
1269
1292
auto self = args.This ();
1270
1293
1294
+ Local<Value> val;
1295
+ if (!self->Get (context, env->isfinished_string ()).ToLocal (&val)) {
1296
+ // An error will have been scheduled.
1297
+ return ;
1298
+ }
1299
+
1271
1300
// skip iteration if is_finished
1272
- auto is_finished = Local<Boolean >::Cast (
1273
- self->Get (context, env->isfinished_string ()).ToLocalChecked ());
1301
+ auto is_finished = Local<Boolean >::Cast (val);
1274
1302
if (is_finished->Value ()) {
1275
- LocalVector<Name> keys (isolate, {env->done_string (), env->value_string ()});
1276
- LocalVector<Value> values (isolate,
1277
- {Boolean::New (isolate, true ), Null (isolate)});
1278
-
1279
- DCHECK_EQ (keys.size (), values.size ());
1303
+ Local<Name> keys[] = {env->done_string (), env->value_string ()};
1304
+ Local<Value> values[] = {Boolean::New (isolate, true ), Null (isolate)};
1305
+ static_assert (arraysize (keys) == arraysize (values));
1280
1306
Local<Object> result = Object::New (
1281
- isolate, Null (isolate), keys. data (), values. data (), keys. size ( ));
1307
+ isolate, Null (isolate), & keys[ 0 ], & values[ 0 ], arraysize (keys ));
1282
1308
args.GetReturnValue ().Set (result);
1283
1309
return ;
1284
1310
}
1285
1311
1286
- auto external_stmt = Local<External>::Cast (
1287
- self->Get (context, env->statement_string ()).ToLocalChecked ());
1312
+ if (!self->Get (context, env->statement_string ()).ToLocal (&val)) {
1313
+ // An error will have been scheduled.
1314
+ return ;
1315
+ }
1316
+
1317
+ auto external_stmt = Local<External>::Cast (val);
1288
1318
auto stmt = static_cast <StatementSync*>(external_stmt->Value ());
1289
- auto num_cols =
1290
- Local<Integer>::Cast (
1291
- self->Get (context, env->num_cols_string ()).ToLocalChecked ())
1292
- ->Value ();
1319
+
1320
+ if (!self->Get (context, env->num_cols_string ()).ToLocal (&val)) {
1321
+ // An error will have been scheduled.
1322
+ return ;
1323
+ }
1324
+
1325
+ auto num_cols = Local<Integer>::Cast (val)->Value ();
1293
1326
1294
1327
THROW_AND_RETURN_ON_BAD_STATE (
1295
1328
env, stmt->IsFinalized (), " statement has been finalized" );
@@ -1301,8 +1334,12 @@ void StatementSync::IterateNextCallback(
1301
1334
1302
1335
// cleanup when no more rows to fetch
1303
1336
sqlite3_reset (stmt->statement_ );
1304
- self->Set (context, env->isfinished_string (), Boolean::New (isolate, true ))
1305
- .ToChecked ();
1337
+ if (self->Set (
1338
+ context, env->isfinished_string (), Boolean::New (isolate, true ))
1339
+ .IsNothing ()) {
1340
+ // An error would have been scheduled
1341
+ return ;
1342
+ }
1306
1343
1307
1344
LocalVector<Name> keys (isolate, {env->done_string (), env->value_string ()});
1308
1345
LocalVector<Value> values (isolate,
@@ -1356,15 +1393,19 @@ void StatementSync::Iterate(const FunctionCallbackInfo<Value>& args) {
1356
1393
return ;
1357
1394
}
1358
1395
1359
- Local<Function> next_func =
1360
- Function::New (context, StatementSync::IterateNextCallback)
1361
- .ToLocalChecked ();
1362
- Local<Function> return_func =
1363
- Function::New (context, StatementSync::IterateReturnCallback)
1364
- .ToLocalChecked ();
1396
+ Local<Function> next_func;
1397
+ Local<Function> return_func;
1398
+ if (!Function::New (context, StatementSync::IterateNextCallback)
1399
+ .ToLocal (&next_func) ||
1400
+ !Function::New (context, StatementSync::IterateReturnCallback)
1401
+ .ToLocal (&return_func)) {
1402
+ // An error will have been scheduled.
1403
+ return ;
1404
+ }
1365
1405
1366
- LocalVector<Name> keys (isolate, {env->next_string (), env->return_string ()});
1367
- LocalVector<Value> values (isolate, {next_func, return_func});
1406
+ Local<Name> keys[] = {env->next_string (), env->return_string ()};
1407
+ Local<Value> values[] = {next_func, return_func};
1408
+ static_assert (arraysize (keys) == arraysize (values));
1368
1409
1369
1410
Local<Object> global = context->Global ();
1370
1411
Local<Value> js_iterator;
@@ -1376,32 +1417,41 @@ void StatementSync::Iterate(const FunctionCallbackInfo<Value>& args) {
1376
1417
.ToLocal (&js_iterator_prototype))
1377
1418
return ;
1378
1419
1379
- DCHECK_EQ (keys.size (), values.size ());
1380
1420
Local<Object> iterable_iterator = Object::New (
1381
- isolate, js_iterator_prototype, keys. data (), values. data (), keys. size ( ));
1421
+ isolate, js_iterator_prototype, & keys[ 0 ], & values[ 0 ], arraysize (keys ));
1382
1422
1383
1423
auto num_cols_pd = v8::PropertyDescriptor (
1384
1424
v8::Integer::New (isolate, sqlite3_column_count (stmt->statement_ )), false );
1385
1425
num_cols_pd.set_enumerable (false );
1386
1426
num_cols_pd.set_configurable (false );
1387
- iterable_iterator
1388
- ->DefineProperty (context, env->num_cols_string (), num_cols_pd)
1389
- .ToChecked ();
1427
+ if (iterable_iterator
1428
+ ->DefineProperty (context, env->num_cols_string (), num_cols_pd)
1429
+ .IsNothing ()) {
1430
+ // An error will have been scheduled.
1431
+ return ;
1432
+ }
1390
1433
1391
1434
auto stmt_pd =
1392
1435
v8::PropertyDescriptor (v8::External::New (isolate, stmt), false );
1393
1436
stmt_pd.set_enumerable (false );
1394
1437
stmt_pd.set_configurable (false );
1395
- iterable_iterator->DefineProperty (context, env->statement_string (), stmt_pd)
1396
- .ToChecked ();
1438
+ if (iterable_iterator
1439
+ ->DefineProperty (context, env->statement_string (), stmt_pd)
1440
+ .IsNothing ()) {
1441
+ // An error will have been scheduled.
1442
+ return ;
1443
+ }
1397
1444
1398
1445
auto is_finished_pd =
1399
1446
v8::PropertyDescriptor (v8::Boolean::New (isolate, false ), true );
1400
1447
stmt_pd.set_enumerable (false );
1401
1448
stmt_pd.set_configurable (false );
1402
- iterable_iterator
1403
- ->DefineProperty (context, env->isfinished_string (), is_finished_pd)
1404
- .ToChecked ();
1449
+ if (iterable_iterator
1450
+ ->DefineProperty (context, env->isfinished_string (), is_finished_pd)
1451
+ .IsNothing ()) {
1452
+ // An error will have been scheduled.
1453
+ return ;
1454
+ }
1405
1455
1406
1456
args.GetReturnValue ().Set (iterable_iterator);
1407
1457
}
0 commit comments