2323#include " node_buffer.h"
2424#include " util.h"
2525
26- #include " async_wrap-inl.h"
2726#include " env-inl.h"
2827#include " llhttp.h"
2928#include " memory_tracker-inl.h"
@@ -64,7 +63,6 @@ using v8::Integer;
6463using v8::Isolate;
6564using v8::Local;
6665using v8::LocalVector;
67- using v8::MaybeLocal;
6866using v8::Number;
6967using v8::Object;
7068using v8::ObjectTemplate;
@@ -250,17 +248,16 @@ class ConnectionsList : public BaseObject {
250248 std::set<Parser*, ParserComparator> active_connections_;
251249};
252250
253- class Parser : public AsyncWrap , public StreamListener {
251+ class Parser : public BaseObject , public StreamListener {
254252 friend class ConnectionsList ;
255253 friend struct ParserComparator ;
256254
257255 public:
258256 Parser (BindingData* binding_data, Local<Object> wrap)
259- : AsyncWrap (binding_data->env (), wrap),
257+ : BaseObject (binding_data->env (), wrap),
260258 current_buffer_len_(0 ),
261259 current_buffer_data_(nullptr ),
262- binding_data_(binding_data) {
263- }
260+ binding_data_(binding_data) {}
264261
265262 SET_NO_MEMORY_INFO ()
266263 SET_MEMORY_INFO_NAME(Parser)
@@ -289,13 +286,7 @@ class Parser : public AsyncWrap, public StreamListener {
289286 Local<Value> cb = object ()->Get (env ()->context (), kOnMessageBegin )
290287 .ToLocalChecked ();
291288 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 );
297-
298- if (r.IsEmpty ()) callback_scope.MarkAsFailed ();
289+ USE (cb.As <Function>()->Call (env ()->context (), object (), 0 , nullptr ));
299290 }
300291
301292 return 0 ;
@@ -442,14 +433,8 @@ class Parser : public AsyncWrap, public StreamListener {
442433
443434 argv[A_UPGRADE] = Boolean::New (env ()->isolate (), parser_.upgrade );
444435
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- }
436+ auto head_response = cb.As <Function>()->Call (
437+ env ()->context (), object (), arraysize (argv), argv);
453438
454439 int64_t val;
455440
@@ -478,9 +463,10 @@ class Parser : public AsyncWrap, public StreamListener {
478463
479464 Local<Value> buffer = Buffer::Copy (env, at, length).ToLocalChecked ();
480465
481- MaybeLocal<Value> r = MakeCallback (cb.As <Function>(), 1 , &buffer);
466+ v8::TryCatch try_catch (env->isolate ());
467+ USE (cb.As <Function>()->Call (env->context (), object (), 1 , &buffer));
482468
483- if (r. IsEmpty ()) {
469+ if (try_catch. HasCaught ()) {
484470 got_exception_ = true ;
485471 llhttp_set_error_reason (&parser_, " HPE_JS_EXCEPTION:JS Exception" );
486472 return HPE_USER;
@@ -516,15 +502,10 @@ class Parser : public AsyncWrap, public StreamListener {
516502 if (!cb->IsFunction ())
517503 return 0 ;
518504
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- }
505+ v8::TryCatch try_catch (env ()->isolate ());
506+ USE (cb.As <Function>()->Call (env ()->context (), object (), 0 , nullptr ));
526507
527- if (r. IsEmpty ()) {
508+ if (try_catch. HasCaught ()) {
528509 got_exception_ = true ;
529510 return -1 ;
530511 }
@@ -571,17 +552,6 @@ class Parser : public AsyncWrap, public StreamListener {
571552 delete parser;
572553 }
573554
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-
585555 // TODO(@anonrig): Add V8 Fast API
586556 static void Remove (const FunctionCallbackInfo<Value>& args) {
587557 Parser* parser;
@@ -639,25 +609,24 @@ class Parser : public AsyncWrap, public StreamListener {
639609 ConnectionsList* connectionsList = nullptr ;
640610
641611 CHECK (args[0 ]->IsInt32 ());
642- CHECK (args[1 ]->IsObject ());
643612
644- if (args.Length () > 2 ) {
645- CHECK (args[2 ]->IsNumber ());
613+ if (args.Length () > 1 ) {
614+ CHECK (args[1 ]->IsNumber ());
646615 max_http_header_size =
647- static_cast <uint64_t >(args[2 ].As <Number>()->Value ());
616+ static_cast <uint64_t >(args[1 ].As <Number>()->Value ());
648617 }
649618 if (max_http_header_size == 0 ) {
650619 max_http_header_size = env->options ()->max_http_header_size ;
651620 }
652621
653- if (args.Length () > 3 ) {
654- CHECK (args[3 ]->IsInt32 ());
655- lenient_flags = args[3 ].As <Int32>()->Value ();
622+ if (args.Length () > 2 ) {
623+ CHECK (args[2 ]->IsInt32 ());
624+ lenient_flags = args[2 ].As <Int32>()->Value ();
656625 }
657626
658- if (args.Length () > 4 && !args[4 ]->IsNullOrUndefined ()) {
659- CHECK (args[4 ]->IsObject ());
660- ASSIGN_OR_RETURN_UNWRAP (&connectionsList, args[4 ]);
627+ if (args.Length () > 3 && !args[3 ]->IsNullOrUndefined ()) {
628+ CHECK (args[3 ]->IsObject ());
629+ ASSIGN_OR_RETURN_UNWRAP (&connectionsList, args[3 ]);
661630 }
662631
663632 llhttp_type_t type =
@@ -669,13 +638,6 @@ class Parser : public AsyncWrap, public StreamListener {
669638 // Should always be called from the same context.
670639 CHECK_EQ (env, parser->env ());
671640
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>());
679641 parser->Init (type, max_http_header_size, lenient_flags);
680642
681643 if (connectionsList != nullptr ) {
@@ -802,7 +764,13 @@ class Parser : public AsyncWrap, public StreamListener {
802764 current_buffer_len_ = nread;
803765 current_buffer_data_ = buf.base ;
804766
805- MakeCallback (cb.As <Function>(), 1 , &ret);
767+ v8::TryCatch try_catch (env ()->isolate ());
768+ USE (cb.As <Function>()->Call (env ()->context (), object (), 1 , &ret));
769+
770+ if (try_catch.HasCaught ()) {
771+ got_exception_ = true ;
772+ return ;
773+ }
806774
807775 current_buffer_len_ = 0 ;
808776 current_buffer_data_ = nullptr ;
@@ -917,12 +885,11 @@ class Parser : public AsyncWrap, public StreamListener {
917885 url_.ToString (env ())
918886 };
919887
920- MaybeLocal<Value> r = MakeCallback (cb. As <Function>(),
921- arraysize (argv),
922- argv);
888+ v8::TryCatch try_catch ( env ()-> isolate ());
889+ USE (cb. As <Function>()-> Call (
890+ env ()-> context (), object (), arraysize (argv), argv) );
923891
924- if (r.IsEmpty ())
925- got_exception_ = true ;
892+ if (try_catch.HasCaught ()) got_exception_ = true ;
926893
927894 url_.Reset ();
928895 have_flushed_ = true ;
@@ -1287,9 +1254,7 @@ void CreatePerIsolateProperties(IsolateData* isolate_data,
12871254 t->Set (FIXED_ONE_BYTE_STRING (isolate, " kLenientAll" ),
12881255 Integer::NewFromUnsigned (isolate, kLenientAll ));
12891256
1290- t->Inherit (AsyncWrap::GetConstructorTemplate (isolate_data));
12911257 SetProtoMethod (isolate, t, " close" , Parser::Close);
1292- SetProtoMethod (isolate, t, " free" , Parser::Free);
12931258 SetProtoMethod (isolate, t, " remove" , Parser::Remove);
12941259 SetProtoMethod (isolate, t, " execute" , Parser::Execute);
12951260 SetProtoMethod (isolate, t, " finish" , Parser::Finish);
@@ -1358,7 +1323,6 @@ void CreatePerContextProperties(Local<Object> target,
13581323void RegisterExternalReferences (ExternalReferenceRegistry* registry) {
13591324 registry->Register (Parser::New);
13601325 registry->Register (Parser::Close);
1361- registry->Register (Parser::Free);
13621326 registry->Register (Parser::Remove);
13631327 registry->Register (Parser::Execute);
13641328 registry->Register (Parser::Finish);
0 commit comments