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,19 @@ class Parser : public AsyncWrap, public StreamListener {
286
283
connectionsList_->PushActive (this );
287
284
}
288
285
289
- Local<Value> cb = object ()->Get (env ()->context (), kOnMessageBegin )
286
+ auto context = env ()->context ();
287
+
288
+ Local<Value> cb = object ()->Get (context, kOnMessageBegin )
290
289
.ToLocalChecked ();
291
290
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 );
291
+ v8::TryCatch try_catch (env ()->isolate ());
292
+ USE (cb.As <Function>()->Call (context, object (), 0 , nullptr ));
297
293
298
- if (r.IsEmpty ()) callback_scope.MarkAsFailed ();
294
+ if (try_catch.HasCaught ()) {
295
+ got_exception_ = true ;
296
+ llhttp_set_error_reason (&parser_, " HPE_JS_EXCEPTION:JS Exception" );
297
+ return HPE_USER;
298
+ }
299
299
}
300
300
301
301
return 0 ;
@@ -442,14 +442,8 @@ class Parser : public AsyncWrap, public StreamListener {
442
442
443
443
argv[A_UPGRADE] = Boolean::New (env ()->isolate (), parser_.upgrade );
444
444
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 ();
452
- }
445
+ auto head_response = cb.As <Function>()->Call (
446
+ env ()->context (), object (), arraysize (argv), argv);
453
447
454
448
int64_t val;
455
449
@@ -478,9 +472,10 @@ class Parser : public AsyncWrap, public StreamListener {
478
472
479
473
Local<Value> buffer = Buffer::Copy (env, at, length).ToLocalChecked ();
480
474
481
- MaybeLocal<Value> r = MakeCallback (cb.As <Function>(), 1 , &buffer);
475
+ v8::TryCatch try_catch (env->isolate ());
476
+ USE (cb.As <Function>()->Call (env->context (), object (), 1 , &buffer));
482
477
483
- if (r. IsEmpty ()) {
478
+ if (try_catch. HasCaught ()) {
484
479
got_exception_ = true ;
485
480
llhttp_set_error_reason (&parser_, " HPE_JS_EXCEPTION:JS Exception" );
486
481
return HPE_USER;
@@ -516,15 +511,10 @@ class Parser : public AsyncWrap, public StreamListener {
516
511
if (!cb->IsFunction ())
517
512
return 0 ;
518
513
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
- }
514
+ v8::TryCatch try_catch (env ()->isolate ());
515
+ USE (cb.As <Function>()->Call (env ()->context (), object (), 0 , nullptr ));
526
516
527
- if (r. IsEmpty ()) {
517
+ if (try_catch. HasCaught ()) {
528
518
got_exception_ = true ;
529
519
return -1 ;
530
520
}
@@ -571,17 +561,6 @@ class Parser : public AsyncWrap, public StreamListener {
571
561
delete parser;
572
562
}
573
563
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
564
// TODO(@anonrig): Add V8 Fast API
586
565
static void Remove (const FunctionCallbackInfo<Value>& args) {
587
566
Parser* parser;
@@ -639,25 +618,24 @@ class Parser : public AsyncWrap, public StreamListener {
639
618
ConnectionsList* connectionsList = nullptr ;
640
619
641
620
CHECK (args[0 ]->IsInt32 ());
642
- CHECK (args[1 ]->IsObject ());
643
621
644
- if (args.Length () > 2 ) {
645
- CHECK (args[2 ]->IsNumber ());
622
+ if (args.Length () > 1 ) {
623
+ CHECK (args[1 ]->IsNumber ());
646
624
max_http_header_size =
647
- static_cast <uint64_t >(args[2 ].As <Number>()->Value ());
625
+ static_cast <uint64_t >(args[1 ].As <Number>()->Value ());
648
626
}
649
627
if (max_http_header_size == 0 ) {
650
628
max_http_header_size = env->options ()->max_http_header_size ;
651
629
}
652
630
653
- if (args.Length () > 3 ) {
654
- CHECK (args[3 ]->IsInt32 ());
655
- lenient_flags = args[3 ].As <Int32>()->Value ();
631
+ if (args.Length () > 2 ) {
632
+ CHECK (args[2 ]->IsInt32 ());
633
+ lenient_flags = args[2 ].As <Int32>()->Value ();
656
634
}
657
635
658
- if (args.Length () > 4 && !args[4 ]->IsNullOrUndefined ()) {
659
- CHECK (args[4 ]->IsObject ());
660
- ASSIGN_OR_RETURN_UNWRAP (&connectionsList, args[4 ]);
636
+ if (args.Length () > 3 && !args[3 ]->IsNullOrUndefined ()) {
637
+ CHECK (args[3 ]->IsObject ());
638
+ ASSIGN_OR_RETURN_UNWRAP (&connectionsList, args[3 ]);
661
639
}
662
640
663
641
llhttp_type_t type =
@@ -669,13 +647,6 @@ class Parser : public AsyncWrap, public StreamListener {
669
647
// Should always be called from the same context.
670
648
CHECK_EQ (env, parser->env ());
671
649
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
650
parser->Init (type, max_http_header_size, lenient_flags);
680
651
681
652
if (connectionsList != nullptr ) {
@@ -802,7 +773,13 @@ class Parser : public AsyncWrap, public StreamListener {
802
773
current_buffer_len_ = nread;
803
774
current_buffer_data_ = buf.base ;
804
775
805
- MakeCallback (cb.As <Function>(), 1 , &ret);
776
+ v8::TryCatch try_catch (env ()->isolate ());
777
+ USE (cb.As <Function>()->Call (env ()->context (), object (), 1 , &ret));
778
+
779
+ if (try_catch.HasCaught ()) {
780
+ got_exception_ = true ;
781
+ return ;
782
+ }
806
783
807
784
current_buffer_len_ = 0 ;
808
785
current_buffer_data_ = nullptr ;
@@ -917,12 +894,11 @@ class Parser : public AsyncWrap, public StreamListener {
917
894
url_.ToString (env ())
918
895
};
919
896
920
- MaybeLocal<Value> r = MakeCallback (cb. As <Function>(),
921
- arraysize (argv),
922
- argv);
897
+ v8::TryCatch try_catch ( env ()-> isolate ());
898
+ USE (cb. As <Function>()-> Call (
899
+ env ()-> context (), object (), arraysize (argv), argv) );
923
900
924
- if (r.IsEmpty ())
925
- got_exception_ = true ;
901
+ if (try_catch.HasCaught ()) got_exception_ = true ;
926
902
927
903
url_.Reset ();
928
904
have_flushed_ = true ;
@@ -1287,9 +1263,7 @@ void CreatePerIsolateProperties(IsolateData* isolate_data,
1287
1263
t->Set (FIXED_ONE_BYTE_STRING (isolate, " kLenientAll" ),
1288
1264
Integer::NewFromUnsigned (isolate, kLenientAll ));
1289
1265
1290
- t->Inherit (AsyncWrap::GetConstructorTemplate (isolate_data));
1291
1266
SetProtoMethod (isolate, t, " close" , Parser::Close);
1292
- SetProtoMethod (isolate, t, " free" , Parser::Free);
1293
1267
SetProtoMethod (isolate, t, " remove" , Parser::Remove);
1294
1268
SetProtoMethod (isolate, t, " execute" , Parser::Execute);
1295
1269
SetProtoMethod (isolate, t, " finish" , Parser::Finish);
@@ -1358,7 +1332,6 @@ void CreatePerContextProperties(Local<Object> target,
1358
1332
void RegisterExternalReferences (ExternalReferenceRegistry* registry) {
1359
1333
registry->Register (Parser::New);
1360
1334
registry->Register (Parser::Close);
1361
- registry->Register (Parser::Free);
1362
1335
registry->Register (Parser::Remove);
1363
1336
registry->Register (Parser::Execute);
1364
1337
registry->Register (Parser::Finish);
0 commit comments