Skip to content

Commit 9398d84

Browse files
addaleaxBridgeAR
authored andcommitted
lib,src: remove usage of _externalStream
Since 4697e1b, it is no longer necessary to use `v8::External`s to pass `StreamBase` instances to native functions. PR-URL: #26510 Refs: #25142 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 5ad9929 commit 9398d84

12 files changed

+29
-37
lines changed

Diff for: lib/_http_server.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,11 @@ function connectionListenerInternal(server, socket) {
386386
socket.on = socketOnWrap;
387387

388388
// We only consume the socket if it has never been consumed before.
389-
if (socket._handle) {
390-
var external = socket._handle._externalStream;
391-
if (!socket._handle._consumed && external) {
392-
parser._consumed = true;
393-
socket._handle._consumed = true;
394-
parser.consume(external);
395-
}
389+
if (socket._handle && socket._handle.isStreamBase &&
390+
!socket._handle._consumed) {
391+
parser._consumed = true;
392+
socket._handle._consumed = true;
393+
parser.consume(socket._handle);
396394
}
397395
parser[kOnExecute] =
398396
onParserExecute.bind(undefined, server, socket, parser, state);

Diff for: lib/_tls_wrap.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -441,14 +441,10 @@ TLSSocket.prototype._wrapHandle = function(wrap) {
441441
const context = options.secureContext ||
442442
options.credentials ||
443443
tls.createSecureContext(options);
444-
const externalStream = handle._externalStream;
445-
assert(typeof externalStream === 'object',
446-
'handle must be a LibuvStreamWrap');
444+
assert(handle.isStreamBase, 'handle must be a StreamBase');
447445
assert(context.context instanceof NativeSecureContext,
448446
'context.context must be a NativeSecureContext');
449-
const res = tls_wrap.wrap(externalStream,
450-
context.context,
451-
!!options.isServer);
447+
const res = tls_wrap.wrap(handle, context.context, !!options.isServer);
452448
res._parent = handle; // C++ "wrap" object: TCPWrap, JSStream, ...
453449
res._parentWrap = wrap; // JS object: net.Socket, JSStreamSocket, ...
454450
res._secureContext = context;

Diff for: lib/internal/http2/core.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ function setupHandle(socket, type, options) {
847847

848848
if (typeof options.selectPadding === 'function')
849849
this[kSelectPadding] = options.selectPadding;
850-
handle.consume(socket._handle._externalStream);
850+
handle.consume(socket._handle);
851851

852852
this[kHandle] = handle;
853853

@@ -937,7 +937,7 @@ class Http2Session extends EventEmitter {
937937
constructor(type, options, socket) {
938938
super();
939939

940-
if (!socket._handle || !socket._handle._externalStream) {
940+
if (!socket._handle || !socket._handle.isStreamBase) {
941941
socket = new JSStreamSocket(socket);
942942
}
943943

@@ -2097,8 +2097,7 @@ function startFilePipe(self, fd, offset, length) {
20972097
handle.onread = onPipedFileHandleRead;
20982098
handle.stream = self;
20992099

2100-
const pipe = new StreamPipe(handle._externalStream,
2101-
self[kHandle]._externalStream);
2100+
const pipe = new StreamPipe(handle, self[kHandle]);
21022101
pipe.onunpipe = onFileUnpipe;
21032102
pipe.start();
21042103

Diff for: src/node_http2.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -1837,8 +1837,8 @@ bool Http2Session::HasWritesOnSocketForStream(Http2Stream* stream) {
18371837
// (typically a net.Socket or tls.TLSSocket). The lifecycle of the two is
18381838
// tightly coupled with all data transfer between the two happening at the
18391839
// C++ layer via the StreamBase API.
1840-
void Http2Session::Consume(Local<External> external) {
1841-
StreamBase* stream = static_cast<StreamBase*>(external->Value());
1840+
void Http2Session::Consume(Local<Object> stream_obj) {
1841+
StreamBase* stream = StreamBase::FromObject(stream_obj);
18421842
stream->PushStreamListener(this);
18431843
Debug(this, "i/o stream consumed");
18441844
}
@@ -2429,8 +2429,8 @@ void Http2Session::New(const FunctionCallbackInfo<Value>& args) {
24292429
void Http2Session::Consume(const FunctionCallbackInfo<Value>& args) {
24302430
Http2Session* session;
24312431
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder());
2432-
CHECK(args[0]->IsExternal());
2433-
session->Consume(args[0].As<External>());
2432+
CHECK(args[0]->IsObject());
2433+
session->Consume(args[0].As<Object>());
24342434
}
24352435

24362436
// Destroys the Http2Session instance and renders it unusable

Diff for: src/node_http2.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ class Http2Session : public AsyncWrap, public StreamListener {
696696

697697
void Close(uint32_t code = NGHTTP2_NO_ERROR,
698698
bool socket_closed = false);
699-
void Consume(Local<External> external);
699+
void Consume(Local<Object> stream);
700700
void Goaway(uint32_t code, int32_t lastStreamID,
701701
const uint8_t* data, size_t len);
702702
void AltSvc(int32_t id,

Diff for: src/node_http_parser_impl.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,8 @@ class Parser : public AsyncWrap, public StreamListener {
556556
static void Consume(const FunctionCallbackInfo<Value>& args) {
557557
Parser* parser;
558558
ASSIGN_OR_RETURN_UNWRAP(&parser, args.Holder());
559-
CHECK(args[0]->IsExternal());
560-
Local<External> stream_obj = args[0].As<External>();
561-
StreamBase* stream = static_cast<StreamBase*>(stream_obj->Value());
559+
CHECK(args[0]->IsObject());
560+
StreamBase* stream = StreamBase::FromObject(args[0].As<Object>());
562561
CHECK_NOT_NULL(stream);
563562
stream->PushStreamListener(parser);
564563
}

Diff for: src/stream_base-inl.h

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace node {
1313

1414
using v8::Signature;
15-
using v8::External;
1615
using v8::FunctionCallbackInfo;
1716
using v8::FunctionTemplate;
1817
using v8::HandleScope;

Diff for: src/stream_base.cc

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace node {
1717
using v8::Array;
1818
using v8::ArrayBuffer;
1919
using v8::Context;
20+
using v8::External;
2021
using v8::FunctionCallbackInfo;
2122
using v8::HandleScope;
2223
using v8::Integer;
@@ -368,6 +369,9 @@ void StreamBase::AddMethods(Environment* env, Local<FunctionTemplate> t) {
368369
t, "writeUcs2String", JSMethod<&StreamBase::WriteString<UCS2>>);
369370
env->SetProtoMethod(
370371
t, "writeLatin1String", JSMethod<&StreamBase::WriteString<LATIN1>>);
372+
t->PrototypeTemplate()->Set(FIXED_ONE_BYTE_STRING(env->isolate(),
373+
"isStreamBase"),
374+
True(env->isolate()));
371375
}
372376

373377
void StreamBase::GetFD(const FunctionCallbackInfo<Value>& args) {

Diff for: src/stream_pipe.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "node_buffer.h"
44

55
using v8::Context;
6-
using v8::External;
76
using v8::Function;
87
using v8::FunctionCallbackInfo;
98
using v8::FunctionTemplate;
@@ -226,10 +225,10 @@ void StreamPipe::WritableListener::OnStreamRead(ssize_t nread,
226225

227226
void StreamPipe::New(const FunctionCallbackInfo<Value>& args) {
228227
CHECK(args.IsConstructCall());
229-
CHECK(args[0]->IsExternal());
230-
CHECK(args[1]->IsExternal());
231-
auto source = static_cast<StreamBase*>(args[0].As<External>()->Value());
232-
auto sink = static_cast<StreamBase*>(args[1].As<External>()->Value());
228+
CHECK(args[0]->IsObject());
229+
CHECK(args[1]->IsObject());
230+
StreamBase* source = StreamBase::FromObject(args[0].As<Object>());
231+
StreamBase* sink = StreamBase::FromObject(args[1].As<Object>());
233232

234233
new StreamPipe(source, sink, args.This());
235234
}

Diff for: src/tls_wrap.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,11 @@ void TLSWrap::Wrap(const FunctionCallbackInfo<Value>& args) {
150150
CHECK(args[1]->IsObject());
151151
CHECK(args[2]->IsBoolean());
152152

153-
Local<External> stream_obj = args[0].As<External>();
154153
Local<Object> sc = args[1].As<Object>();
155154
Kind kind = args[2]->IsTrue() ? SSLWrap<TLSWrap>::kServer :
156155
SSLWrap<TLSWrap>::kClient;
157156

158-
StreamBase* stream = static_cast<StreamBase*>(stream_obj->Value());
157+
StreamBase* stream = StreamBase::FromObject(args[0].As<Object>());
159158
CHECK_NOT_NULL(stream);
160159

161160
Local<Object> obj;

Diff for: test/sequential/test-async-wrap-getasyncid.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,7 @@ if (common.hasCrypto) { // eslint-disable-line node-core/crypto-check
268268

269269
// TLSWrap is exposed, but needs to be instantiated via tls_wrap.wrap().
270270
const tls_wrap = internalBinding('tls_wrap');
271-
testInitialized(
272-
tls_wrap.wrap(tcp._externalStream, credentials.context, true), 'TLSWrap');
271+
testInitialized(tls_wrap.wrap(tcp, credentials.context, true), 'TLSWrap');
273272
}
274273

275274
{

Diff for: test/sequential/test-http-regr-gh-2928.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ function execAndClose() {
4040
throw e;
4141
});
4242

43-
parser.consume(socket._handle._externalStream);
43+
parser.consume(socket._handle);
4444

4545
parser.onIncoming = function onIncoming() {
4646
process.stdout.write('+');
4747
gotResponses++;
48-
parser.unconsume(socket._handle._externalStream);
48+
parser.unconsume();
4949
httpCommon.freeParser(parser);
5050
socket.destroy();
5151
setImmediate(execAndClose);

0 commit comments

Comments
 (0)