diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2020-04-20 03:51:05 +0800 |
---|---|---|
committer | Shelley Vohr <shelley.vohr@gmail.com> | 2020-05-07 08:24:55 -0700 |
commit | a292630baf0dc21fde11c9fd6a5eaf018e2b7eb5 (patch) | |
tree | 650d6a185133db583e73b972d28976332df5f6a2 /src/node_http_parser.cc | |
parent | 07372e9d5b785793706728299f9a92e330e084e3 (diff) | |
download | node-new-a292630baf0dc21fde11c9fd6a5eaf018e2b7eb5.tar.gz |
src: retrieve binding data from the context
Instead of passing them through the data bound to function
templates, store references to them in a list embedded inside
the context.
This makes the function templates more context-independent,
and makes it possible to embed binding data in non-main contexts.
Co-authored-by: Anna Henningsen <anna@addaleax.net>
PR-URL: https://github.com/nodejs/node/pull/33139
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Diffstat (limited to 'src/node_http_parser.cc')
-rw-r--r-- | src/node_http_parser.cc | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/src/node_http_parser.cc b/src/node_http_parser.cc index f98e9f5c7b..c7a3df8d06 100644 --- a/src/node_http_parser.cc +++ b/src/node_http_parser.cc @@ -84,7 +84,10 @@ inline bool IsOWS(char c) { class BindingData : public BaseObject { public: - BindingData(Environment* env, Local<Object> obj) : BaseObject(env, obj) {} + BindingData(Environment* env, Local<Object> obj) + : BaseObject(env, obj) {} + + static constexpr FastStringKey binding_data_name { "http_parser" }; std::vector<char> parser_buffer; bool parser_buffer_in_use = false; @@ -96,6 +99,9 @@ class BindingData : public BaseObject { SET_MEMORY_INFO_NAME(BindingData) }; +// TODO(addaleax): Remove once we're on C++17. +constexpr FastStringKey BindingData::binding_data_name; + // helper class for the Parser struct StringPtr { StringPtr() { @@ -444,7 +450,7 @@ class Parser : public AsyncWrap, public StreamListener { } static void New(const FunctionCallbackInfo<Value>& args) { - BindingData* binding_data = Unwrap<BindingData>(args.Data()); + BindingData* binding_data = Environment::GetBindingData<BindingData>(args); new Parser(binding_data, args.This()); } @@ -920,8 +926,9 @@ void InitializeHttpParser(Local<Object> target, Local<Context> context, void* priv) { Environment* env = Environment::GetCurrent(context); - Environment::BindingScope<BindingData> binding_scope(env); - if (!binding_scope) return; + BindingData* const binding_data = + env->AddBindingData<BindingData>(context, target); + if (binding_data == nullptr) return; Local<FunctionTemplate> t = env->NewFunctionTemplate(Parser::New); t->InstanceTemplate()->SetInternalFieldCount(Parser::kInternalFieldCount); |