23
23
#include " node_buffer.h"
24
24
#include " util.h"
25
25
26
- #include " async_wrap-inl.h"
27
26
#include " env-inl.h"
28
27
#include " llhttp.h"
29
28
#include " memory_tracker-inl.h"
@@ -64,7 +63,6 @@ using v8::Integer;
64
63
using v8::Isolate;
65
64
using v8::Local;
66
65
using v8::LocalVector;
67
- using v8::MaybeLocal;
68
66
using v8::Number;
69
67
using v8::Object;
70
68
using v8::ObjectTemplate;
@@ -250,17 +248,16 @@ class ConnectionsList : public BaseObject {
250
248
std::set<Parser*, ParserComparator> active_connections_;
251
249
};
252
250
253
- class Parser : public AsyncWrap , public StreamListener {
251
+ class Parser : public BaseObject , public StreamListener {
254
252
friend class ConnectionsList ;
255
253
friend struct ParserComparator ;
256
254
257
255
public:
258
256
Parser (BindingData* binding_data, Local<Object> wrap)
259
- : AsyncWrap (binding_data->env (), wrap),
257
+ : BaseObject (binding_data->env (), wrap),
260
258
current_buffer_len_(0 ),
261
259
current_buffer_data_(nullptr ),
262
- binding_data_(binding_data) {
263
- }
260
+ binding_data_(binding_data) {}
264
261
265
262
SET_NO_MEMORY_INFO ()
266
263
SET_MEMORY_INFO_NAME(Parser)
@@ -286,16 +283,20 @@ class Parser : public AsyncWrap, public StreamListener {
286
283
connectionsList_->PushActive (this );
287
284
}
288
285
289
- Local<Value> cb = object ()->Get (env ()->context (), kOnMessageBegin )
290
- .ToLocalChecked ();
291
- if (cb->IsFunction ()) {
292
- InternalCallbackScope callback_scope (
293
- this , InternalCallbackScope::kSkipTaskQueues );
294
-
295
- MaybeLocal<Value> r = cb.As <Function>()->Call (
296
- env ()->context (), object (), 0 , nullptr );
286
+ auto context = env ()->context ();
297
287
298
- if (r.IsEmpty ()) callback_scope.MarkAsFailed ();
288
+ Local<Value> cb = object ()->Get (context, kOnMessageBegin ).ToLocalChecked ();
289
+ if (cb->IsFunction ()) {
290
+ v8::TryCatch try_catch (env ()->isolate ());
291
+ USE (cb.As <Function>()->Call (context, object (), 0 , nullptr ));
292
+
293
+ if (try_catch.HasCaught ()) {
294
+ got_exception_ = true ;
295
+ // This is part of http parsing flow. We need to set the proper error
296
+ // reason for the parser.
297
+ llhttp_set_error_reason (&parser_, " HPE_JS_EXCEPTION:JS Exception" );
298
+ return HPE_USER;
299
+ }
299
300
}
300
301
301
302
return 0 ;
@@ -442,15 +443,14 @@ class Parser : public AsyncWrap, public StreamListener {
442
443
443
444
argv[A_UPGRADE] = Boolean::New (env ()->isolate (), parser_.upgrade );
444
445
445
- MaybeLocal<Value> head_response ;
446
- {
447
- InternalCallbackScope callback_scope (
448
- this , InternalCallbackScope:: kSkipTaskQueues );
449
- head_response = cb. As <Function>()-> Call (
450
- env ()-> context (), object (), arraysize (argv), argv );
451
- if (head_response. IsEmpty ()) callback_scope. MarkAsFailed () ;
446
+ v8::TryCatch try_catch ( env ()-> isolate ()) ;
447
+ auto head_response = cb. As <Function>()-> Call (
448
+ env ()-> context (), object (), arraysize (argv), argv);
449
+ if (try_catch. HasCaught ()) {
450
+ got_exception_ = true ;
451
+ llhttp_set_error_reason (&parser_, " HPE_JS_EXCEPTION:JS Exception " );
452
+ return HPE_USER ;
452
453
}
453
-
454
454
int64_t val;
455
455
456
456
if (head_response.IsEmpty () || !head_response.ToLocalChecked ()
@@ -478,9 +478,10 @@ class Parser : public AsyncWrap, public StreamListener {
478
478
479
479
Local<Value> buffer = Buffer::Copy (env, at, length).ToLocalChecked ();
480
480
481
- MaybeLocal<Value> r = MakeCallback (cb.As <Function>(), 1 , &buffer);
481
+ v8::TryCatch try_catch (env->isolate ());
482
+ USE (cb.As <Function>()->Call (env->context (), object (), 1 , &buffer));
482
483
483
- if (r. IsEmpty ()) {
484
+ if (try_catch. HasCaught ()) {
484
485
got_exception_ = true ;
485
486
llhttp_set_error_reason (&parser_, " HPE_JS_EXCEPTION:JS Exception" );
486
487
return HPE_USER;
@@ -516,15 +517,10 @@ class Parser : public AsyncWrap, public StreamListener {
516
517
if (!cb->IsFunction ())
517
518
return 0 ;
518
519
519
- MaybeLocal<Value> r;
520
- {
521
- InternalCallbackScope callback_scope (
522
- this , InternalCallbackScope::kSkipTaskQueues );
523
- r = cb.As <Function>()->Call (env ()->context (), object (), 0 , nullptr );
524
- if (r.IsEmpty ()) callback_scope.MarkAsFailed ();
525
- }
520
+ v8::TryCatch try_catch (env ()->isolate ());
521
+ USE (cb.As <Function>()->Call (env ()->context (), object (), 0 , nullptr ));
526
522
527
- if (r. IsEmpty ()) {
523
+ if (try_catch. HasCaught ()) {
528
524
got_exception_ = true ;
529
525
return -1 ;
530
526
}
@@ -571,17 +567,6 @@ class Parser : public AsyncWrap, public StreamListener {
571
567
delete parser;
572
568
}
573
569
574
- // TODO(@anonrig): Add V8 Fast API
575
- static void Free (const FunctionCallbackInfo<Value>& args) {
576
- Parser* parser;
577
- ASSIGN_OR_RETURN_UNWRAP (&parser, args.This ());
578
-
579
- // Since the Parser destructor isn't going to run the destroy() callbacks
580
- // it needs to be triggered manually.
581
- parser->EmitTraceEventDestroy ();
582
- parser->EmitDestroy ();
583
- }
584
-
585
570
// TODO(@anonrig): Add V8 Fast API
586
571
static void Remove (const FunctionCallbackInfo<Value>& args) {
587
572
Parser* parser;
@@ -639,25 +624,24 @@ class Parser : public AsyncWrap, public StreamListener {
639
624
ConnectionsList* connectionsList = nullptr ;
640
625
641
626
CHECK (args[0 ]->IsInt32 ());
642
- CHECK (args[1 ]->IsObject ());
643
627
644
- if (args.Length () > 2 ) {
645
- CHECK (args[2 ]->IsNumber ());
628
+ if (args.Length () > 1 ) {
629
+ CHECK (args[1 ]->IsNumber ());
646
630
max_http_header_size =
647
- static_cast <uint64_t >(args[2 ].As <Number>()->Value ());
631
+ static_cast <uint64_t >(args[1 ].As <Number>()->Value ());
648
632
}
649
633
if (max_http_header_size == 0 ) {
650
634
max_http_header_size = env->options ()->max_http_header_size ;
651
635
}
652
636
653
- if (args.Length () > 3 ) {
654
- CHECK (args[3 ]->IsInt32 ());
655
- lenient_flags = args[3 ].As <Int32>()->Value ();
637
+ if (args.Length () > 2 ) {
638
+ CHECK (args[2 ]->IsInt32 ());
639
+ lenient_flags = args[2 ].As <Int32>()->Value ();
656
640
}
657
641
658
- if (args.Length () > 4 && !args[4 ]->IsNullOrUndefined ()) {
659
- CHECK (args[4 ]->IsObject ());
660
- ASSIGN_OR_RETURN_UNWRAP (&connectionsList, args[4 ]);
642
+ if (args.Length () > 3 && !args[3 ]->IsNullOrUndefined ()) {
643
+ CHECK (args[3 ]->IsObject ());
644
+ ASSIGN_OR_RETURN_UNWRAP (&connectionsList, args[3 ]);
661
645
}
662
646
663
647
llhttp_type_t type =
@@ -669,13 +653,6 @@ class Parser : public AsyncWrap, public StreamListener {
669
653
// Should always be called from the same context.
670
654
CHECK_EQ (env, parser->env ());
671
655
672
- AsyncWrap::ProviderType provider =
673
- (type == HTTP_REQUEST ?
674
- AsyncWrap::PROVIDER_HTTPINCOMINGMESSAGE
675
- : AsyncWrap::PROVIDER_HTTPCLIENTREQUEST);
676
-
677
- parser->set_provider_type (provider);
678
- parser->AsyncReset (args[1 ].As <Object>());
679
656
parser->Init (type, max_http_header_size, lenient_flags);
680
657
681
658
if (connectionsList != nullptr ) {
@@ -802,7 +779,14 @@ class Parser : public AsyncWrap, public StreamListener {
802
779
current_buffer_len_ = nread;
803
780
current_buffer_data_ = buf.base ;
804
781
805
- MakeCallback (cb.As <Function>(), 1 , &ret);
782
+ v8::TryCatch try_catch (env ()->isolate ());
783
+ USE (cb.As <Function>()->Call (env ()->context (), object (), 1 , &ret));
784
+
785
+ if (try_catch.HasCaught ()) {
786
+ got_exception_ = true ;
787
+ llhttp_set_error_reason (&parser_, " HPE_JS_EXCEPTION:JS Exception" );
788
+ return ;
789
+ }
806
790
807
791
current_buffer_len_ = 0 ;
808
792
current_buffer_data_ = nullptr ;
@@ -917,12 +901,11 @@ class Parser : public AsyncWrap, public StreamListener {
917
901
url_.ToString (env ())
918
902
};
919
903
920
- MaybeLocal<Value> r = MakeCallback (cb. As <Function>(),
921
- arraysize (argv),
922
- argv);
904
+ v8::TryCatch try_catch ( env ()-> isolate ());
905
+ USE (cb. As <Function>()-> Call (
906
+ env ()-> context (), object (), arraysize (argv), argv) );
923
907
924
- if (r.IsEmpty ())
925
- got_exception_ = true ;
908
+ if (try_catch.HasCaught ()) got_exception_ = true ;
926
909
927
910
url_.Reset ();
928
911
have_flushed_ = true ;
@@ -1287,9 +1270,7 @@ void CreatePerIsolateProperties(IsolateData* isolate_data,
1287
1270
t->Set (FIXED_ONE_BYTE_STRING (isolate, " kLenientAll" ),
1288
1271
Integer::NewFromUnsigned (isolate, kLenientAll ));
1289
1272
1290
- t->Inherit (AsyncWrap::GetConstructorTemplate (isolate_data));
1291
1273
SetProtoMethod (isolate, t, " close" , Parser::Close);
1292
- SetProtoMethod (isolate, t, " free" , Parser::Free);
1293
1274
SetProtoMethod (isolate, t, " remove" , Parser::Remove);
1294
1275
SetProtoMethod (isolate, t, " execute" , Parser::Execute);
1295
1276
SetProtoMethod (isolate, t, " finish" , Parser::Finish);
@@ -1358,7 +1339,6 @@ void CreatePerContextProperties(Local<Object> target,
1358
1339
void RegisterExternalReferences (ExternalReferenceRegistry* registry) {
1359
1340
registry->Register (Parser::New);
1360
1341
registry->Register (Parser::Close);
1361
- registry->Register (Parser::Free);
1362
1342
registry->Register (Parser::Remove);
1363
1343
registry->Register (Parser::Execute);
1364
1344
registry->Register (Parser::Finish);
0 commit comments