Skip to content

Commit 78c82a3

Browse files
addaleaxBethGriggs
authored andcommitted
src: move fs state out of Environment
Moves state that is specific to the `fs` binding into the `fs` binding implementation as a cleanup. PR-URL: #32538 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 7005670 commit 78c82a3

9 files changed

+175
-143
lines changed

src/env-inl.h

-13
Original file line numberDiff line numberDiff line change
@@ -580,19 +580,6 @@ inline double Environment::get_default_trigger_async_id() {
580580
return default_trigger_async_id;
581581
}
582582

583-
inline AliasedFloat64Array* Environment::fs_stats_field_array() {
584-
return &fs_stats_field_array_;
585-
}
586-
587-
inline AliasedBigUint64Array* Environment::fs_stats_field_bigint_array() {
588-
return &fs_stats_field_bigint_array_;
589-
}
590-
591-
inline std::vector<std::unique_ptr<fs::FileHandleReadWrap>>&
592-
Environment::file_handle_read_wrap_freelist() {
593-
return file_handle_read_wrap_freelist_;
594-
}
595-
596583
inline std::shared_ptr<EnvironmentOptions> Environment::options() {
597584
return options_;
598585
}

src/env.cc

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
#include "env.h"
22

33
#include "async_wrap.h"
4+
#include "base_object-inl.h"
45
#include "debug_utils-inl.h"
56
#include "memory_tracker-inl.h"
67
#include "node_buffer.h"
78
#include "node_context_data.h"
89
#include "node_errors.h"
9-
#include "node_file.h"
1010
#include "node_internals.h"
1111
#include "node_options-inl.h"
1212
#include "node_process.h"
1313
#include "node_v8_platform-inl.h"
1414
#include "node_worker.h"
1515
#include "req_wrap-inl.h"
16+
#include "stream_base.h"
1617
#include "tracing/agent.h"
1718
#include "tracing/traced_value.h"
1819
#include "util-inl.h"
@@ -341,8 +342,6 @@ Environment::Environment(IsolateData* isolate_data,
341342
flags_(flags),
342343
thread_id_(thread_id.id == static_cast<uint64_t>(-1) ?
343344
AllocateEnvironmentThreadId().id : thread_id.id),
344-
fs_stats_field_array_(isolate_, kFsStatsBufferLength),
345-
fs_stats_field_bigint_array_(isolate_, kFsStatsBufferLength),
346345
context_(context->GetIsolate(), context) {
347346
// We'll be creating new objects so make sure we've entered the context.
348347
HandleScope handle_scope(isolate());
@@ -444,10 +443,6 @@ Environment::~Environment() {
444443
isolate()->GetHeapProfiler()->RemoveBuildEmbedderGraphCallback(
445444
BuildEmbedderGraph, this);
446445

447-
// Make sure there are no re-used libuv wrapper objects.
448-
// CleanupHandles() should have removed all of them.
449-
CHECK(file_handle_read_wrap_freelist_.empty());
450-
451446
HandleScope handle_scope(isolate());
452447

453448
#if HAVE_INSPECTOR
@@ -613,8 +608,6 @@ void Environment::CleanupHandles() {
613608
!handle_wrap_queue_.IsEmpty()) {
614609
uv_run(event_loop(), UV_RUN_ONCE);
615610
}
616-
617-
file_handle_read_wrap_freelist_.clear();
618611
}
619612

620613
void Environment::StartProfilerIdleNotifier() {
@@ -1096,9 +1089,6 @@ void Environment::MemoryInfo(MemoryTracker* tracker) const {
10961089
tracker->TrackField("should_abort_on_uncaught_toggle",
10971090
should_abort_on_uncaught_toggle_);
10981091
tracker->TrackField("stream_base_state", stream_base_state_);
1099-
tracker->TrackField("fs_stats_field_array", fs_stats_field_array_);
1100-
tracker->TrackField("fs_stats_field_bigint_array",
1101-
fs_stats_field_bigint_array_);
11021092
tracker->TrackField("cleanup_hooks", cleanup_hooks_);
11031093
tracker->TrackField("async_hooks", async_hooks_);
11041094
tracker->TrackField("immediate_info", immediate_info_);

src/env.h

-16
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ class ContextifyScript;
5656
class CompiledFnEntry;
5757
}
5858

59-
namespace fs {
60-
class FileHandleReadWrap;
61-
}
62-
6359
namespace performance {
6460
class PerformanceState;
6561
}
@@ -1009,12 +1005,6 @@ class Environment : public MemoryRetainer {
10091005

10101006
EnabledDebugList* enabled_debug_list() { return &enabled_debug_list_; }
10111007

1012-
inline AliasedFloat64Array* fs_stats_field_array();
1013-
inline AliasedBigUint64Array* fs_stats_field_bigint_array();
1014-
1015-
inline std::vector<std::unique_ptr<fs::FileHandleReadWrap>>&
1016-
file_handle_read_wrap_freelist();
1017-
10181008
inline performance::PerformanceState* performance_state();
10191009
inline std::unordered_map<std::string, uint64_t>* performance_marks();
10201010

@@ -1362,12 +1352,6 @@ class Environment : public MemoryRetainer {
13621352

13631353
EnabledDebugList enabled_debug_list_;
13641354

1365-
AliasedFloat64Array fs_stats_field_array_;
1366-
AliasedBigUint64Array fs_stats_field_bigint_array_;
1367-
1368-
std::vector<std::unique_ptr<fs::FileHandleReadWrap>>
1369-
file_handle_read_wrap_freelist_;
1370-
13711355
std::list<node_module> extra_linked_bindings_;
13721356
Mutex extra_linked_bindings_mutex_;
13731357

src/node_dir.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ void DirHandle::Close(const FunctionCallbackInfo<Value>& args) {
152152
dir->closing_ = false;
153153
dir->closed_ = true;
154154

155-
FSReqBase* req_wrap_async = GetReqWrap(env, args[0]);
155+
FSReqBase* req_wrap_async = GetReqWrap(args, 0);
156156
if (req_wrap_async != nullptr) { // close(req)
157157
AsyncCall(env, req_wrap_async, args, "closedir", UTF8, AfterClose,
158158
uv_fs_closedir, dir->dir());
@@ -252,7 +252,7 @@ void DirHandle::Read(const FunctionCallbackInfo<Value>& args) {
252252
dir->dir_->dirents = dir->dirents_.data();
253253
}
254254

255-
FSReqBase* req_wrap_async = GetReqWrap(env, args[2]);
255+
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
256256
if (req_wrap_async != nullptr) { // dir.read(encoding, bufferSize, req)
257257
AsyncCall(env, req_wrap_async, args, "readdir", encoding,
258258
AfterDirRead, uv_fs_readdir, dir->dir());
@@ -320,7 +320,7 @@ static void OpenDir(const FunctionCallbackInfo<Value>& args) {
320320

321321
const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8);
322322

323-
FSReqBase* req_wrap_async = GetReqWrap(env, args[2]);
323+
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
324324
if (req_wrap_async != nullptr) { // openDir(path, encoding, req)
325325
AsyncCall(env, req_wrap_async, args, "opendir", encoding, AfterOpenDir,
326326
uv_fs_opendir, *path);

src/node_file-inl.h

+37-20
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ void FSContinuationData::Done(int result) {
3939
done_cb_(req_);
4040
}
4141

42-
FSReqBase::FSReqBase(Environment* env,
43-
v8::Local<v8::Object> req,
44-
AsyncWrap::ProviderType type,
45-
bool use_bigint)
46-
: ReqWrap(env, req, type), use_bigint_(use_bigint) {
42+
FSReqBase::FSReqBase(BindingData* binding_data,
43+
v8::Local<v8::Object> req,
44+
AsyncWrap::ProviderType type,
45+
bool use_bigint)
46+
: ReqWrap(binding_data->env(), req, type),
47+
use_bigint_(use_bigint),
48+
binding_data_(binding_data) {
4749
}
4850

4951
void FSReqBase::Init(const char* syscall,
@@ -72,9 +74,13 @@ FSReqBase::Init(const char* syscall, size_t len, enum encoding encoding) {
7274
return buffer_;
7375
}
7476

75-
FSReqCallback::FSReqCallback(Environment* env,
76-
v8::Local<v8::Object> req, bool use_bigint)
77-
: FSReqBase(env, req, AsyncWrap::PROVIDER_FSREQCALLBACK, use_bigint) {}
77+
FSReqCallback::FSReqCallback(BindingData* binding_data,
78+
v8::Local<v8::Object> req,
79+
bool use_bigint)
80+
: FSReqBase(binding_data,
81+
req,
82+
AsyncWrap::PROVIDER_FSREQCALLBACK,
83+
use_bigint) {}
7884

7985
template <typename NativeT, typename V8T>
8086
void FillStatsArray(AliasedBufferBase<NativeT, V8T>* fields,
@@ -112,26 +118,28 @@ void FillStatsArray(AliasedBufferBase<NativeT, V8T>* fields,
112118
#undef SET_FIELD_WITH_STAT
113119
}
114120

115-
v8::Local<v8::Value> FillGlobalStatsArray(Environment* env,
121+
v8::Local<v8::Value> FillGlobalStatsArray(BindingData* binding_data,
116122
const bool use_bigint,
117123
const uv_stat_t* s,
118124
const bool second) {
119125
const ptrdiff_t offset =
120126
second ? static_cast<ptrdiff_t>(FsStatsOffset::kFsStatsFieldsNumber) : 0;
121127
if (use_bigint) {
122-
auto* const arr = env->fs_stats_field_bigint_array();
128+
auto* const arr = &binding_data->stats_field_bigint_array;
123129
FillStatsArray(arr, s, offset);
124130
return arr->GetJSArray();
125131
} else {
126-
auto* const arr = env->fs_stats_field_array();
132+
auto* const arr = &binding_data->stats_field_array;
127133
FillStatsArray(arr, s, offset);
128134
return arr->GetJSArray();
129135
}
130136
}
131137

132138
template <typename AliasedBufferT>
133139
FSReqPromise<AliasedBufferT>*
134-
FSReqPromise<AliasedBufferT>::New(Environment* env, bool use_bigint) {
140+
FSReqPromise<AliasedBufferT>::New(BindingData* binding_data,
141+
bool use_bigint) {
142+
Environment* env = binding_data->env();
135143
v8::Local<v8::Object> obj;
136144
if (!env->fsreqpromise_constructor_template()
137145
->NewInstance(env->context())
@@ -143,7 +151,7 @@ FSReqPromise<AliasedBufferT>::New(Environment* env, bool use_bigint) {
143151
obj->Set(env->context(), env->promise_string(), resolver).IsNothing()) {
144152
return nullptr;
145153
}
146-
return new FSReqPromise(env, obj, use_bigint);
154+
return new FSReqPromise(binding_data, obj, use_bigint);
147155
}
148156

149157
template <typename AliasedBufferT>
@@ -154,12 +162,15 @@ FSReqPromise<AliasedBufferT>::~FSReqPromise() {
154162

155163
template <typename AliasedBufferT>
156164
FSReqPromise<AliasedBufferT>::FSReqPromise(
157-
Environment* env,
165+
BindingData* binding_data,
158166
v8::Local<v8::Object> obj,
159167
bool use_bigint)
160-
: FSReqBase(env, obj, AsyncWrap::PROVIDER_FSREQPROMISE, use_bigint),
168+
: FSReqBase(binding_data,
169+
obj,
170+
AsyncWrap::PROVIDER_FSREQPROMISE,
171+
use_bigint),
161172
stats_field_array_(
162-
env->isolate(),
173+
env()->isolate(),
163174
static_cast<size_t>(FsStatsOffset::kFsStatsFieldsNumber)) {}
164175

165176
template <typename AliasedBufferT>
@@ -208,15 +219,21 @@ void FSReqPromise<AliasedBufferT>::MemoryInfo(MemoryTracker* tracker) const {
208219
tracker->TrackField("stats_field_array", stats_field_array_);
209220
}
210221

211-
FSReqBase* GetReqWrap(Environment* env, v8::Local<v8::Value> value,
222+
FSReqBase* GetReqWrap(const v8::FunctionCallbackInfo<v8::Value>& args,
223+
int index,
212224
bool use_bigint) {
225+
v8::Local<v8::Value> value = args[index];
213226
if (value->IsObject()) {
214227
return Unwrap<FSReqBase>(value.As<v8::Object>());
215-
} else if (value->StrictEquals(env->fs_use_promises_symbol())) {
228+
}
229+
230+
BindingData* binding_data = Unwrap<BindingData>(args.Data());
231+
Environment* env = binding_data->env();
232+
if (value->StrictEquals(env->fs_use_promises_symbol())) {
216233
if (use_bigint) {
217-
return FSReqPromise<AliasedBigUint64Array>::New(env, use_bigint);
234+
return FSReqPromise<AliasedBigUint64Array>::New(binding_data, use_bigint);
218235
} else {
219-
return FSReqPromise<AliasedFloat64Array>::New(env, use_bigint);
236+
return FSReqPromise<AliasedFloat64Array>::New(binding_data, use_bigint);
220237
}
221238
}
222239
return nullptr;

0 commit comments

Comments
 (0)