/src/node/src/stream_base-inl.h
Line | Count | Source |
1 | | #ifndef SRC_STREAM_BASE_INL_H_ |
2 | | #define SRC_STREAM_BASE_INL_H_ |
3 | | |
4 | | #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
5 | | |
6 | | #include "async_wrap-inl.h" |
7 | | #include "base_object-inl.h" |
8 | | #include "node.h" |
9 | | #include "stream_base.h" |
10 | | #include "v8.h" |
11 | | |
12 | | namespace node { |
13 | | |
14 | | StreamReq::StreamReq( |
15 | | StreamBase* stream, |
16 | 0 | v8::Local<v8::Object> req_wrap_obj) : stream_(stream) { |
17 | 0 | AttachToObject(req_wrap_obj); |
18 | 0 | } |
19 | | |
20 | 0 | void StreamReq::AttachToObject(v8::Local<v8::Object> req_wrap_obj) { |
21 | 0 | CHECK_EQ(req_wrap_obj->GetAlignedPointerFromInternalField( |
22 | 0 | StreamReq::kStreamReqField, EmbedderDataTag::kDefault), |
23 | 0 | nullptr); |
24 | 0 | req_wrap_obj->SetAlignedPointerInInternalField( |
25 | 0 | StreamReq::kStreamReqField, this, EmbedderDataTag::kDefault); |
26 | 0 | } |
27 | | |
28 | 0 | StreamReq* StreamReq::FromObject(v8::Local<v8::Object> req_wrap_obj) { |
29 | 0 | return static_cast<StreamReq*>( |
30 | 0 | req_wrap_obj->GetAlignedPointerFromInternalField( |
31 | 0 | StreamReq::kStreamReqField, EmbedderDataTag::kDefault)); |
32 | 0 | } |
33 | | |
34 | 0 | void StreamReq::Dispose() { |
35 | 0 | BaseObjectPtr<AsyncWrap> destroy_me{GetAsyncWrap()}; |
36 | 0 | object()->SetAlignedPointerInInternalField( |
37 | 0 | StreamReq::kStreamReqField, nullptr, EmbedderDataTag::kDefault); |
38 | 0 | destroy_me->Detach(); |
39 | 0 | } |
40 | | |
41 | 0 | v8::Local<v8::Object> StreamReq::object() { |
42 | 0 | return GetAsyncWrap()->object(); |
43 | 0 | } |
44 | | |
45 | | ShutdownWrap::ShutdownWrap( |
46 | | StreamBase* stream, |
47 | | v8::Local<v8::Object> req_wrap_obj) |
48 | 0 | : StreamReq(stream, req_wrap_obj) { } |
49 | | |
50 | | WriteWrap::WriteWrap( |
51 | | StreamBase* stream, |
52 | | v8::Local<v8::Object> req_wrap_obj) |
53 | 0 | : StreamReq(stream, req_wrap_obj) { } |
54 | | |
55 | 0 | void StreamListener::PassReadErrorToPreviousListener(ssize_t nread) { |
56 | 0 | CHECK_NOT_NULL(previous_listener_); |
57 | 0 | previous_listener_->OnStreamRead(nread, uv_buf_init(nullptr, 0)); |
58 | 0 | } |
59 | | |
60 | 0 | void StreamResource::PushStreamListener(StreamListener* listener) { |
61 | 0 | CHECK_NOT_NULL(listener); |
62 | 0 | CHECK_NULL(listener->stream_); |
63 | | |
64 | 0 | listener->previous_listener_ = listener_; |
65 | 0 | listener->stream_ = this; |
66 | |
|
67 | 0 | listener_ = listener; |
68 | 0 | } |
69 | | |
70 | 0 | uv_buf_t StreamResource::EmitAlloc(size_t suggested_size) { |
71 | 0 | DebugSealHandleScope seal_handle_scope; |
72 | 0 | return listener_->OnStreamAlloc(suggested_size); |
73 | 0 | } |
74 | | |
75 | 0 | void StreamResource::EmitRead(ssize_t nread, const uv_buf_t& buf) { |
76 | 0 | DebugSealHandleScope seal_handle_scope; |
77 | 0 | if (nread > 0) |
78 | 0 | bytes_read_ += static_cast<uint64_t>(nread); |
79 | 0 | listener_->OnStreamRead(nread, buf); |
80 | 0 | } |
81 | | |
82 | 0 | void StreamResource::EmitAfterWrite(WriteWrap* w, int status) { |
83 | 0 | DebugSealHandleScope seal_handle_scope; |
84 | 0 | listener_->OnStreamAfterWrite(w, status); |
85 | 0 | } |
86 | | |
87 | 0 | void StreamResource::EmitAfterShutdown(ShutdownWrap* w, int status) { |
88 | 0 | DebugSealHandleScope seal_handle_scope; |
89 | 0 | listener_->OnStreamAfterShutdown(w, status); |
90 | 0 | } |
91 | | |
92 | 0 | void StreamResource::EmitWantsWrite(size_t suggested_size) { |
93 | 0 | DebugSealHandleScope seal_handle_scope; |
94 | 0 | listener_->OnStreamWantsWrite(suggested_size); |
95 | 0 | } |
96 | | |
97 | 0 | StreamBase::StreamBase(Environment* env) : env_(env) { |
98 | 0 | PushStreamListener(&default_listener_); |
99 | 0 | } |
100 | | |
101 | | template <typename OtherBase> |
102 | | SimpleShutdownWrap<OtherBase>::SimpleShutdownWrap( |
103 | | StreamBase* stream, |
104 | | v8::Local<v8::Object> req_wrap_obj) |
105 | 0 | : ShutdownWrap(stream, req_wrap_obj), |
106 | 0 | OtherBase(stream->stream_env(), |
107 | 0 | req_wrap_obj, |
108 | 0 | AsyncWrap::PROVIDER_SHUTDOWNWRAP) { |
109 | 0 | } Unexecuted instantiation: node::SimpleShutdownWrap<node::ReqWrap<uv_fs_s> >::SimpleShutdownWrap(node::StreamBase*, v8::Local<v8::Object>) Unexecuted instantiation: node::SimpleShutdownWrap<node::AsyncWrap>::SimpleShutdownWrap(node::StreamBase*, v8::Local<v8::Object>) Unexecuted instantiation: node::SimpleShutdownWrap<node::ReqWrap<uv_shutdown_s> >::SimpleShutdownWrap(node::StreamBase*, v8::Local<v8::Object>) |
110 | | |
111 | | template <typename OtherBase> |
112 | | SimpleWriteWrap<OtherBase>::SimpleWriteWrap( |
113 | | StreamBase* stream, |
114 | | v8::Local<v8::Object> req_wrap_obj) |
115 | 0 | : WriteWrap(stream, req_wrap_obj), |
116 | 0 | OtherBase(stream->stream_env(), |
117 | 0 | req_wrap_obj, |
118 | 0 | AsyncWrap::PROVIDER_WRITEWRAP) { |
119 | 0 | } Unexecuted instantiation: node::SimpleWriteWrap<node::AsyncWrap>::SimpleWriteWrap(node::StreamBase*, v8::Local<v8::Object>) Unexecuted instantiation: node::SimpleWriteWrap<node::ReqWrap<uv_write_s> >::SimpleWriteWrap(node::StreamBase*, v8::Local<v8::Object>) |
120 | | |
121 | 0 | void StreamBase::AttachToObject(v8::Local<v8::Object> obj) { |
122 | 0 | obj->SetAlignedPointerInInternalField( |
123 | 0 | StreamBase::kStreamBaseField, this, EmbedderDataTag::kDefault); |
124 | 0 | } |
125 | | |
126 | 0 | StreamBase* StreamBase::FromObject(v8::Local<v8::Object> obj) { |
127 | 0 | if (obj->GetAlignedPointerFromInternalField( |
128 | 0 | StreamBase::kSlot, EmbedderDataTag::kDefault) == nullptr) |
129 | 0 | return nullptr; |
130 | | |
131 | 0 | return static_cast<StreamBase*>(obj->GetAlignedPointerFromInternalField( |
132 | 0 | StreamBase::kStreamBaseField, EmbedderDataTag::kDefault)); |
133 | 0 | } |
134 | | |
135 | 0 | WriteWrap* WriteWrap::FromObject(v8::Local<v8::Object> req_wrap_obj) { |
136 | 0 | return static_cast<WriteWrap*>(StreamReq::FromObject(req_wrap_obj)); |
137 | 0 | } |
138 | | |
139 | | template <typename T, bool kIsWeak> |
140 | | WriteWrap* WriteWrap::FromObject( |
141 | 0 | const BaseObjectPtrImpl<T, kIsWeak>& base_obj) { |
142 | 0 | if (!base_obj) return nullptr; |
143 | 0 | return FromObject(base_obj->object()); |
144 | 0 | } |
145 | | |
146 | 0 | ShutdownWrap* ShutdownWrap::FromObject(v8::Local<v8::Object> req_wrap_obj) { |
147 | 0 | return static_cast<ShutdownWrap*>(StreamReq::FromObject(req_wrap_obj)); |
148 | 0 | } |
149 | | |
150 | | template <typename T, bool kIsWeak> |
151 | | ShutdownWrap* ShutdownWrap::FromObject( |
152 | | const BaseObjectPtrImpl<T, kIsWeak>& base_obj) { |
153 | | if (!base_obj) return nullptr; |
154 | | return FromObject(base_obj->object()); |
155 | | } |
156 | | |
157 | 0 | void WriteWrap::SetBackingStore(std::unique_ptr<v8::BackingStore> bs) { |
158 | 0 | CHECK(!backing_store_); |
159 | 0 | backing_store_ = std::move(bs); |
160 | 0 | } |
161 | | |
162 | 0 | void StreamReq::ResetObject(v8::Local<v8::Object> obj) { |
163 | 0 | DCHECK_GT(obj->InternalFieldCount(), StreamReq::kStreamReqField); |
164 | |
|
165 | 0 | obj->SetAlignedPointerInInternalField( |
166 | 0 | StreamReq::kSlot, nullptr, EmbedderDataTag::kDefault); |
167 | 0 | obj->SetAlignedPointerInInternalField( |
168 | 0 | StreamReq::kStreamReqField, nullptr, EmbedderDataTag::kDefault); |
169 | 0 | } |
170 | | |
171 | | } // namespace node |
172 | | |
173 | | #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
174 | | |
175 | | #endif // SRC_STREAM_BASE_INL_H_ |