GeezerVison is showing github.com — stream_base: various improvements · nodejs/node@2d3f09b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/_tls_wrap.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -319,7 +319,9 @@ TLSSocket.prototype._wrapHandle = function(wrap) { | 319 | 319 | var context = options.secureContext || | 320 | 320 | options.credentials || | 321 | 321 | tls.createSecureContext(); | 322 | | - res = tls_wrap.wrap(handle, context.context, options.isServer); | | 322 | + res = tls_wrap.wrap(handle._externalStream, | | 323 | + context.context, | | 324 | + options.isServer); | 323 | 325 | res._parent = handle; | 324 | 326 | res._parentWrap = wrap; | 325 | 327 | res._secureContext = context; | |
Collapse file Copy file name to clipboardExpand all lines: src/env.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -88,6 +88,7 @@ namespace node { | 88 | 88 | V(exponent_string, "exponent") \ | 89 | 89 | V(exports_string, "exports") \ | 90 | 90 | V(ext_key_usage_string, "ext_key_usage") \ | | 91 | + V(external_stream_string, "_externalStream") \ | 91 | 92 | V(family_string, "family") \ | 92 | 93 | V(fatal_exception_string, "_fatalException") \ | 93 | 94 | V(fd_string, "fd") \ | |
Collapse file Copy file name to clipboardExpand all lines: src/node_wrap.h -15Lines changed: 0 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -34,21 +34,6 @@ namespace node { | 34 | 34 | } \ | 35 | 35 | } while (0) | 36 | 36 |
| 37 | | -#define WITH_GENERIC_STREAM(env, obj, BODY) \ | 38 | | - do { \ | 39 | | - WITH_GENERIC_UV_STREAM(env, obj, BODY, { \ | 40 | | - if (env->tls_wrap_constructor_template().IsEmpty() == false && \ | 41 | | - env->tls_wrap_constructor_template()->HasInstance(obj)) { \ | 42 | | - TLSWrap* const wrap = Unwrap<TLSWrap>(obj); \ | 43 | | - BODY \ | 44 | | - } else if (env->jsstream_constructor_template().IsEmpty() == false && \ | 45 | | - env->jsstream_constructor_template()->HasInstance(obj)) { \ | 46 | | - JSStream* const wrap = Unwrap<JSStream>(obj); \ | 47 | | - BODY \ | 48 | | - } \ | 49 | | - }); \ | 50 | | - } while (0) | 51 | | - | 52 | 37 | inline uv_stream_t* HandleToStream(Environment* env, | 53 | 38 | v8::Local<v8::Object> obj) { | 54 | 39 | v8::HandleScope scope(env->isolate()); | |
Collapse file Copy file name to clipboardExpand all lines: src/stream_base-inl.h +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 10 | 10 |
| 11 | 11 | namespace node { | 12 | 12 |
| | 13 | +using v8::External; | 13 | 14 | using v8::FunctionCallbackInfo; | 14 | 15 | using v8::FunctionTemplate; | 15 | 16 | using v8::Handle; | @@ -36,6 +37,13 @@ void StreamBase::AddMethods(Environment* env, | 36 | 37 | v8::DEFAULT, | 37 | 38 | attributes); | 38 | 39 |
| | 40 | + t->InstanceTemplate()->SetAccessor(env->external_stream_string(), | | 41 | + GetExternal<Base>, | | 42 | + nullptr, | | 43 | + env->as_external(), | | 44 | + v8::DEFAULT, | | 45 | + attributes); | | 46 | + | 39 | 47 | env->SetProtoMethod(t, "readStart", JSMethod<Base, &StreamBase::ReadStart>); | 40 | 48 | env->SetProtoMethod(t, "readStop", JSMethod<Base, &StreamBase::ReadStop>); | 41 | 49 | if ((flags & kFlagNoShutdown) == 0) | @@ -72,6 +80,16 @@ void StreamBase::GetFD(Local<String> key, | 72 | 80 | } | 73 | 81 |
| 74 | 82 |
| | 83 | +template <class Base> | | 84 | +void StreamBase::GetExternal(Local<String> key, | | 85 | + const PropertyCallbackInfo<Value>& args) { | | 86 | + StreamBase* wrap = Unwrap<Base>(args.Holder()); | | 87 | + | | 88 | + Local<External> ext = External::New(args.GetIsolate(), wrap); | | 89 | + args.GetReturnValue().Set(ext); | | 90 | +} | | 91 | + | | 92 | + | 75 | 93 | template <class Base, | 76 | 94 | int (StreamBase::*Method)(const FunctionCallbackInfo<Value>& args)> | 77 | 95 | void StreamBase::JSMethod(const FunctionCallbackInfo<Value>& args) { | |
Collapse file Copy file name to clipboardExpand all lines: src/stream_base.cc +22-5Lines changed: 22 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -72,15 +72,14 @@ void StreamBase::AfterShutdown(ShutdownWrap* req_wrap, int status) { | 72 | 72 |
| 73 | 73 | // The wrap and request objects should still be there. | 74 | 74 | CHECK_EQ(req_wrap->persistent().IsEmpty(), false); | 75 | | - CHECK_EQ(wrap->GetAsyncWrap()->persistent().IsEmpty(), false); | 76 | 75 |
| 77 | 76 | HandleScope handle_scope(env->isolate()); | 78 | 77 | Context::Scope context_scope(env->context()); | 79 | 78 |
| 80 | 79 | Local<Object> req_wrap_obj = req_wrap->object(); | 81 | 80 | Local<Value> argv[3] = { | 82 | 81 | Integer::New(env->isolate(), status), | 83 | | - wrap->GetAsyncWrap()->object(), | | 82 | + wrap->GetObject(), | 84 | 83 | req_wrap_obj | 85 | 84 | }; | 86 | 85 |
| @@ -370,7 +369,6 @@ void StreamBase::AfterWrite(WriteWrap* req_wrap, int status) { | 370 | 369 |
| 371 | 370 | // The wrap and request objects should still be there. | 372 | 371 | CHECK_EQ(req_wrap->persistent().IsEmpty(), false); | 373 | | - CHECK_EQ(wrap->GetAsyncWrap()->persistent().IsEmpty(), false); | 374 | 372 |
| 375 | 373 | // Unref handle property | 376 | 374 | Local<Object> req_wrap_obj = req_wrap->object(); | @@ -379,7 +377,7 @@ void StreamBase::AfterWrite(WriteWrap* req_wrap, int status) { | 379 | 377 |
| 380 | 378 | Local<Value> argv[] = { | 381 | 379 | Integer::New(env->isolate(), status), | 382 | | - wrap->GetAsyncWrap()->object(), | | 380 | + wrap->GetObject(), | 383 | 381 | req_wrap_obj, | 384 | 382 | Undefined(env->isolate()) | 385 | 383 | }; | @@ -414,7 +412,16 @@ void StreamBase::EmitData(ssize_t nread, | 414 | 412 | if (argv[2].IsEmpty()) | 415 | 413 | argv[2] = Undefined(env->isolate()); | 416 | 414 |
| 417 | | - GetAsyncWrap()->MakeCallback(env->onread_string(), ARRAY_SIZE(argv), argv); | | 415 | + AsyncWrap* async = GetAsyncWrap(); | | 416 | + if (async == nullptr) { | | 417 | + node::MakeCallback(env, | | 418 | + GetObject(), | | 419 | + env->onread_string(), | | 420 | + ARRAY_SIZE(argv), | | 421 | + argv); | | 422 | + } else { | | 423 | + async->MakeCallback(env->onread_string(), ARRAY_SIZE(argv), argv); | | 424 | + } | 418 | 425 | } | 419 | 426 |
| 420 | 427 |
| @@ -428,6 +435,16 @@ int StreamBase::GetFD() { | 428 | 435 | } | 429 | 436 |
| 430 | 437 |
| | 438 | +AsyncWrap* StreamBase::GetAsyncWrap() { | | 439 | + return nullptr; | | 440 | +} | | 441 | + | | 442 | + | | 443 | +Local<Object> StreamBase::GetObject() { | | 444 | + return GetAsyncWrap()->object(); | | 445 | +} | | 446 | + | | 447 | + | 431 | 448 | int StreamResource::DoTryWrite(uv_buf_t** bufs, size_t* count) { | 432 | 449 | // No TryWrite by default | 433 | 450 | return 0; | |
Collapse file Copy file name to clipboardExpand all lines: src/stream_base.h +42-30Lines changed: 42 additions & 30 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -106,18 +106,31 @@ class WriteWrap: public ReqWrap<uv_write_t>, | 106 | 106 |
| 107 | 107 | class StreamResource { | 108 | 108 | public: | | 109 | + template <class T> | | 110 | + struct Callback { | | 111 | + Callback() : fn(nullptr), ctx(nullptr) {} | | 112 | + Callback(T fn, void* ctx) : fn(fn), ctx(ctx) {} | | 113 | + Callback(const Callback&) = default; | | 114 | + | | 115 | + inline bool is_empty() { return fn == nullptr; } | | 116 | + inline void clear() { | | 117 | + fn = nullptr; | | 118 | + ctx = nullptr; | | 119 | + } | | 120 | + | | 121 | + T fn; | | 122 | + void* ctx; | | 123 | + }; | | 124 | + | 109 | 125 | typedef void (*AfterWriteCb)(WriteWrap* w, void* ctx); | 110 | 126 | typedef void (*AllocCb)(size_t size, uv_buf_t* buf, void* ctx); | 111 | 127 | typedef void (*ReadCb)(ssize_t nread, | 112 | 128 | const uv_buf_t* buf, | 113 | 129 | uv_handle_type pending, | 114 | 130 | void* ctx); | 115 | 131 |
| 116 | | - StreamResource() : after_write_cb_(nullptr), | 117 | | - alloc_cb_(nullptr), | 118 | | - read_cb_(nullptr) { | | 132 | + StreamResource() { | 119 | 133 | } | 120 | | - | 121 | 134 | virtual ~StreamResource() = default; | 122 | 135 |
| 123 | 136 | virtual int DoShutdown(ShutdownWrap* req_wrap) = 0; | @@ -131,44 +144,37 @@ class StreamResource { | 131 | 144 |
| 132 | 145 | // Events | 133 | 146 | inline void OnAfterWrite(WriteWrap* w) { | 134 | | - if (after_write_cb_ != nullptr) | 135 | | - after_write_cb_(w, after_write_ctx_); | | 147 | + if (!after_write_cb_.is_empty()) | | 148 | + after_write_cb_.fn(w, after_write_cb_.ctx); | 136 | 149 | } | 137 | 150 |
| 138 | 151 | inline void OnAlloc(size_t size, uv_buf_t* buf) { | 139 | | - if (alloc_cb_ != nullptr) | 140 | | - alloc_cb_(size, buf, alloc_ctx_); | | 152 | + if (!alloc_cb_.is_empty()) | | 153 | + alloc_cb_.fn(size, buf, alloc_cb_.ctx); | 141 | 154 | } | 142 | 155 |
| 143 | 156 | inline void OnRead(size_t nread, | 144 | 157 | const uv_buf_t* buf, | 145 | 158 | uv_handle_type pending = UV_UNKNOWN_HANDLE) { | 146 | | - if (read_cb_ != nullptr) | 147 | | - read_cb_(nread, buf, pending, read_ctx_); | | 159 | + if (!read_cb_.is_empty()) | | 160 | + read_cb_.fn(nread, buf, pending, read_cb_.ctx); | 148 | 161 | } | 149 | 162 |
| 150 | | - inline void set_after_write_cb(AfterWriteCb cb, void* ctx) { | 151 | | - after_write_ctx_ = ctx; | 152 | | - after_write_cb_ = cb; | | 163 | + inline void set_after_write_cb(Callback<AfterWriteCb> c) { | | 164 | + after_write_cb_ = c; | 153 | 165 | } | 154 | 166 |
| 155 | | - inline void set_alloc_cb(AllocCb cb, void* ctx) { | 156 | | - alloc_cb_ = cb; | 157 | | - alloc_ctx_ = ctx; | 158 | | - } | | 167 | + inline void set_alloc_cb(Callback<AllocCb> c) { alloc_cb_ = c; } | | 168 | + inline void set_read_cb(Callback<ReadCb> c) { read_cb_ = c; } | 159 | 169 |
| 160 | | - inline void set_read_cb(ReadCb cb, void* ctx) { | 161 | | - read_cb_ = cb; | 162 | | - read_ctx_ = ctx; | 163 | | - } | | 170 | + inline Callback<AfterWriteCb> after_write_cb() { return after_write_cb_; } | | 171 | + inline Callback<AllocCb> alloc_cb() { return alloc_cb_; } | | 172 | + inline Callback<ReadCb> read_cb() { return read_cb_; } | 164 | 173 |
| 165 | 174 | private: | 166 | | - AfterWriteCb after_write_cb_; | 167 | | - void* after_write_ctx_; | 168 | | - AllocCb alloc_cb_; | 169 | | - void* alloc_ctx_; | 170 | | - ReadCb read_cb_; | 171 | | - void* read_ctx_; | | 175 | + Callback<AfterWriteCb> after_write_cb_; | | 176 | + Callback<AllocCb> alloc_cb_; | | 177 | + Callback<ReadCb> read_cb_; | 172 | 178 | }; | 173 | 179 |
| 174 | 180 | class StreamBase : public StreamResource { | @@ -211,7 +217,9 @@ class StreamBase : public StreamResource { | 211 | 217 |
| 212 | 218 | virtual ~StreamBase() = default; | 213 | 219 |
| 214 | | - virtual AsyncWrap* GetAsyncWrap() = 0; | | 220 | + // One of these must be implemented | | 221 | + virtual AsyncWrap* GetAsyncWrap(); | | 222 | + virtual v8::Local<v8::Object> GetObject(); | 215 | 223 |
| 216 | 224 | // Libuv callbacks | 217 | 225 | static void AfterShutdown(ShutdownWrap* req, int status); | @@ -227,8 +235,12 @@ class StreamBase : public StreamResource { | 227 | 235 | int WriteString(const v8::FunctionCallbackInfo<v8::Value>& args); | 228 | 236 |
| 229 | 237 | template <class Base> | 230 | | - static void GetFD(v8::Local<v8::String>, | 231 | | - const v8::PropertyCallbackInfo<v8::Value>&); | | 238 | + static void GetFD(v8::Local<v8::String> key, | | 239 | + const v8::PropertyCallbackInfo<v8::Value>& args); | | 240 | + | | 241 | + template <class Base> | | 242 | + static void GetExternal(v8::Local<v8::String> key, | | 243 | + const v8::PropertyCallbackInfo<v8::Value>& args); | 232 | 244 |
| 233 | 245 | template <class Base, | 234 | 246 | int (StreamBase::*Method)( // NOLINT(whitespace/parens) | |
Collapse file Copy file name to clipboardExpand all lines: src/stream_wrap.cc +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -74,9 +74,9 @@ StreamWrap::StreamWrap(Environment* env, | 74 | 74 | parent), | 75 | 75 | StreamBase(env), | 76 | 76 | stream_(stream) { | 77 | | - set_after_write_cb(OnAfterWriteImpl, this); | 78 | | - set_alloc_cb(OnAllocImpl, this); | 79 | | - set_read_cb(OnReadImpl, this); | | 77 | + set_after_write_cb({ OnAfterWriteImpl, this }); | | 78 | + set_alloc_cb({ OnAllocImpl, this }); | | 79 | + set_read_cb({ OnReadImpl, this }); | 80 | 80 | } | 81 | 81 |
| 82 | 82 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/tls_wrap.cc +7-11Lines changed: 7 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | #include "node_crypto_bio.h" // NodeBIO | 7 | 7 | #include "node_crypto_clienthello.h" // ClientHelloParser | 8 | 8 | #include "node_crypto_clienthello-inl.h" | 9 | | -#include "node_wrap.h" // WithGenericStream | 10 | 9 | #include "node_counters.h" | 11 | 10 | #include "node_internals.h" | 12 | 11 | #include "stream_base.h" | @@ -63,12 +62,12 @@ TLSWrap::TLSWrap(Environment* env, | 63 | 62 | SSL_CTX_sess_set_new_cb(sc_->ctx_, SSLWrap<TLSWrap>::NewSessionCallback); | 64 | 63 |
| 65 | 64 | stream_->Consume(); | 66 | | - stream_->set_after_write_cb(OnAfterWriteImpl, this); | 67 | | - stream_->set_alloc_cb(OnAllocImpl, this); | 68 | | - stream_->set_read_cb(OnReadImpl, this); | | 65 | + stream_->set_after_write_cb({ OnAfterWriteImpl, this }); | | 66 | + stream_->set_alloc_cb({ OnAllocImpl, this }); | | 67 | + stream_->set_read_cb({ OnReadImpl, this }); | 69 | 68 |
| 70 | | - set_alloc_cb(OnAllocSelf, this); | 71 | | - set_read_cb(OnReadSelf, this); | | 69 | + set_alloc_cb({ OnAllocSelf, this }); | | 70 | + set_read_cb({ OnReadSelf, this }); | 72 | 71 |
| 73 | 72 | InitSSL(); | 74 | 73 | } | @@ -177,15 +176,12 @@ void TLSWrap::Wrap(const FunctionCallbackInfo<Value>& args) { | 177 | 176 | if (args.Length() < 3 || !args[2]->IsBoolean()) | 178 | 177 | return env->ThrowTypeError("Third argument should be boolean"); | 179 | 178 |
| 180 | | - Local<Object> stream_obj = args[0].As<Object>(); | | 179 | + Local<External> stream_obj = args[0].As<External>(); | 181 | 180 | Local<Object> sc = args[1].As<Object>(); | 182 | 181 | Kind kind = args[2]->IsTrue() ? SSLWrap<TLSWrap>::kServer : | 183 | 182 | SSLWrap<TLSWrap>::kClient; | 184 | 183 |
| 185 | | - StreamBase* stream = nullptr; | 186 | | - WITH_GENERIC_STREAM(env, stream_obj, { | 187 | | - stream = wrap; | 188 | | - }); | | 184 | + StreamBase* stream = static_cast<StreamBase*>(stream_obj->Value()); | 189 | 185 | CHECK_NE(stream, nullptr); | 190 | 186 |
| 191 | 187 | TLSWrap* res = new TLSWrap(env, kind, stream, Unwrap<SecureContext>(sc)); | |
|