diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2021-02-08 17:13:37 +0300 |
---|---|---|
committer | Michaƫl Zasso <targos@protonmail.com> | 2021-02-28 16:39:57 +0300 |
commit | 59861bac0e8ea6e58bbb3babd9902586bc95a524 (patch) | |
tree | fbf443f82953928b918d9b55c93c24733a2868a5 /src | |
parent | e400f8c9c8165db20d9baf39ac6d0ee09f1444c6 (diff) |
bootstrap: include fs module into the builtin snapshot
PR-URL: https://github.com/nodejs/node/pull/36943
Fixes: https://github.com/nodejs/node/issues/35930
Refs: https://github.com/nodejs/node/issues/35711
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/node_dir.cc | 9 | ||||
-rw-r--r-- | src/node_external_reference.h | 2 | ||||
-rw-r--r-- | src/node_file.cc | 104 | ||||
-rw-r--r-- | src/node_file.h | 15 | ||||
-rw-r--r-- | src/node_snapshotable.h | 3 | ||||
-rw-r--r-- | src/node_stat_watcher.cc | 8 | ||||
-rw-r--r-- | src/node_stat_watcher.h | 2 | ||||
-rw-r--r-- | src/stream_base.cc | 28 | ||||
-rw-r--r-- | src/stream_base.h | 3 |
9 files changed, 153 insertions, 21 deletions
diff --git a/src/node_dir.cc b/src/node_dir.cc index d94d78f560b..b103c08262a 100644 --- a/src/node_dir.cc +++ b/src/node_dir.cc @@ -1,4 +1,5 @@ #include "node_dir.h" +#include "node_external_reference.h" #include "node_file-inl.h" #include "node_process.h" #include "memory_tracker-inl.h" @@ -364,8 +365,16 @@ void Initialize(Local<Object> target, env->set_dir_instance_template(dirt); } +void RegisterExternalReferences(ExternalReferenceRegistry* registry) { + registry->Register(OpenDir); + registry->Register(DirHandle::New); + registry->Register(DirHandle::Read); + registry->Register(DirHandle::Close); +} + } // namespace fs_dir } // end namespace node NODE_MODULE_CONTEXT_AWARE_INTERNAL(fs_dir, node::fs_dir::Initialize) +NODE_MODULE_EXTERNAL_REFERENCE(fs_dir, node::fs_dir::RegisterExternalReferences) diff --git a/src/node_external_reference.h b/src/node_external_reference.h index 0544979dd9a..71d155cdb92 100644 --- a/src/node_external_reference.h +++ b/src/node_external_reference.h @@ -53,6 +53,8 @@ class ExternalReferenceRegistry { V(credentials) \ V(env_var) \ V(errors) \ + V(fs) \ + V(fs_dir) \ V(handle_wrap) \ V(messaging) \ V(native_module) \ diff --git a/src/node_file.cc b/src/node_file.cc index 31494e8d2e8..0eb882b49a8 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -23,6 +23,7 @@ #include "aliased_buffer.h" #include "memory_tracker-inl.h" #include "node_buffer.h" +#include "node_external_reference.h" #include "node_process.h" #include "node_stat_watcher.h" #include "util-inl.h" @@ -2398,6 +2399,47 @@ void BindingData::MemoryInfo(MemoryTracker* tracker) const { file_handle_read_wrap_freelist); } +BindingData::BindingData(Environment* env, v8::Local<v8::Object> wrap) + : SnapshotableObject(env, wrap, type_int), + stats_field_array(env->isolate(), kFsStatsBufferLength), + stats_field_bigint_array(env->isolate(), kFsStatsBufferLength) { + wrap->Set(env->context(), + FIXED_ONE_BYTE_STRING(env->isolate(), "statValues"), + stats_field_array.GetJSArray()) + .Check(); + + wrap->Set(env->context(), + FIXED_ONE_BYTE_STRING(env->isolate(), "bigintStatValues"), + stats_field_bigint_array.GetJSArray()) + .Check(); +} + +void BindingData::Deserialize(Local<Context> context, + Local<Object> holder, + int index, + InternalFieldInfo* info) { + DCHECK_EQ(index, BaseObject::kSlot); + HandleScope scope(context->GetIsolate()); + Environment* env = Environment::GetCurrent(context); + BindingData* binding = env->AddBindingData<BindingData>(context, holder); + CHECK_NOT_NULL(binding); +} + +void BindingData::PrepareForSerialization(Local<Context> context, + v8::SnapshotCreator* creator) { + CHECK(file_handle_read_wrap_freelist.empty()); + // We'll just re-initialize the buffers in the constructor since their + // contents can be thrown away once consumed in the previous call. + stats_field_array.Release(); + stats_field_bigint_array.Release(); +} + +InternalFieldInfo* BindingData::Serialize(int index) { + DCHECK_EQ(index, BaseObject::kSlot); + InternalFieldInfo* info = InternalFieldInfo::New(type()); + return info; +} + // TODO(addaleax): Remove once we're on C++17. constexpr FastStringKey BindingData::type_name; @@ -2461,14 +2503,6 @@ void Initialize(Local<Object> target, static_cast<int32_t>(FsStatsOffset::kFsStatsFieldsNumber))) .Check(); - target->Set(context, - FIXED_ONE_BYTE_STRING(isolate, "statValues"), - binding_data->stats_field_array.GetJSArray()).Check(); - - target->Set(context, - FIXED_ONE_BYTE_STRING(isolate, "bigintStatValues"), - binding_data->stats_field_bigint_array.GetJSArray()).Check(); - StatWatcher::Initialize(env, target); // Create FunctionTemplate for FSReqCallback @@ -2532,8 +2566,62 @@ void Initialize(Local<Object> target, BindingData* FSReqBase::binding_data() { return binding_data_.get(); } + +void RegisterExternalReferences(ExternalReferenceRegistry* registry) { + registry->Register(Access); + StatWatcher::RegisterExternalReferences(registry); + + registry->Register(Close); + registry->Register(Open); + registry->Register(OpenFileHandle); + registry->Register(Read); + registry->Register(ReadBuffers); + registry->Register(Fdatasync); + registry->Register(Fsync); + registry->Register(Rename); + registry->Register(FTruncate); + registry->Register(RMDir); + registry->Register(MKDir); + registry->Register(ReadDir); + registry->Register(InternalModuleReadJSON); + registry->Register(InternalModuleStat); + registry->Register(Stat); + registry->Register(LStat); + registry->Register(FStat); + registry->Register(Link); + registry->Register(Symlink); + registry->Register(ReadLink); + registry->Register(Unlink); + registry->Register(WriteBuffer); + registry->Register(WriteBuffers); + registry->Register(WriteString); + registry->Register(RealPath); + registry->Register(CopyFile); + + registry->Register(Chmod); + registry->Register(FChmod); + // registry->Register(LChmod); + + registry->Register(Chown); + registry->Register(FChown); + registry->Register(LChown); + + registry->Register(UTimes); + registry->Register(FUTimes); + registry->Register(LUTimes); + + registry->Register(Mkdtemp); + registry->Register(NewFSReqCallback); + + registry->Register(FileHandle::New); + registry->Register(FileHandle::Close); + registry->Register(FileHandle::ReleaseFD); + StreamBase::RegisterExternalReferences(registry); +} + } // namespace fs } // end namespace node NODE_MODULE_CONTEXT_AWARE_INTERNAL(fs, node::fs::Initialize) +NODE_MODULE_EXTERNAL_REFERENCE(fs, node::fs::RegisterExternalReferences) diff --git a/src/node_file.h b/src/node_file.h index 9e652dc7a4a..f1515a3e25d 100644 --- a/src/node_file.h +++ b/src/node_file.h @@ -3,23 +3,19 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS -#include "node.h" #include "aliased_buffer.h" #include "node_messaging.h" +#include "node_snapshotable.h" #include "stream_base.h" -#include <iostream> namespace node { namespace fs { class FileHandleReadWrap; -class BindingData : public BaseObject { +class BindingData : public SnapshotableObject { public: - explicit BindingData(Environment* env, v8::Local<v8::Object> wrap) - : BaseObject(env, wrap), - stats_field_array(env->isolate(), kFsStatsBufferLength), - stats_field_bigint_array(env->isolate(), kFsStatsBufferLength) {} + explicit BindingData(Environment* env, v8::Local<v8::Object> wrap); AliasedFloat64Array stats_field_array; AliasedBigUint64Array stats_field_bigint_array; @@ -27,7 +23,10 @@ class BindingData : public BaseObject { std::vector<BaseObjectPtr<FileHandleReadWrap>> file_handle_read_wrap_freelist; - static constexpr FastStringKey type_name { "fs" }; + SERIALIZABLE_OBJECT_METHODS() + static constexpr FastStringKey type_name{"node::fs::BindingData"}; + static constexpr EmbedderObjectType type_int = + EmbedderObjectType::k_fs_binding_data; void MemoryInfo(MemoryTracker* tracker) const override; SET_SELF_SIZE(BindingData) diff --git a/src/node_snapshotable.h b/src/node_snapshotable.h index 94a5a7a03ca..d0fbce800a6 100644 --- a/src/node_snapshotable.h +++ b/src/node_snapshotable.h @@ -12,7 +12,8 @@ namespace node { class Environment; struct EnvSerializeInfo; -#define SERIALIZABLE_OBJECT_TYPES(V) +#define SERIALIZABLE_OBJECT_TYPES(V) \ + V(fs_binding_data, fs::BindingData) enum class EmbedderObjectType : uint8_t { k_default = 0, diff --git a/src/node_stat_watcher.cc b/src/node_stat_watcher.cc index 344ea6bb7ea..b9f7903a2fd 100644 --- a/src/node_stat_watcher.cc +++ b/src/node_stat_watcher.cc @@ -19,10 +19,11 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "memory_tracker-inl.h" #include "node_stat_watcher.h" #include "async_wrap-inl.h" #include "env-inl.h" +#include "memory_tracker-inl.h" +#include "node_external_reference.h" #include "node_file-inl.h" #include "util-inl.h" @@ -55,6 +56,11 @@ void StatWatcher::Initialize(Environment* env, Local<Object> target) { env->SetConstructorFunction(target, "StatWatcher", t); } +void StatWatcher::RegisterExternalReferences( + ExternalReferenceRegistry* registry) { + registry->Register(StatWatcher::New); + registry->Register(StatWatcher::Start); +} StatWatcher::StatWatcher(fs::BindingData* binding_data, Local<Object> wrap, diff --git a/src/node_stat_watcher.h b/src/node_stat_watcher.h index c1d6ccce69b..7efd22fdfdb 100644 --- a/src/node_stat_watcher.h +++ b/src/node_stat_watcher.h @@ -35,10 +35,12 @@ class BindingData; } class Environment; +class ExternalReferenceRegistry; class StatWatcher : public HandleWrap { public: static void Initialize(Environment* env, v8::Local<v8::Object> target); + static void RegisterExternalReferences(ExternalReferenceRegistry* registry); protected: StatWatcher(fs::BindingData* binding_data, diff --git a/src/stream_base.cc b/src/stream_base.cc index 87781efb0e8..925dc3f152e 100644 --- a/src/stream_base.cc +++ b/src/stream_base.cc @@ -3,11 +3,12 @@ #include "stream_wrap.h" #include "allocated_buffer-inl.h" +#include "env-inl.h" +#include "js_stream.h" #include "node.h" #include "node_buffer.h" #include "node_errors.h" -#include "env-inl.h" -#include "js_stream.h" +#include "node_external_reference.h" #include "string_bytes.h" #include "util-inl.h" #include "v8.h" @@ -423,6 +424,29 @@ void StreamBase::AddMethods(Environment* env, Local<FunctionTemplate> t) { &Value::IsFunction>); } +void StreamBase::RegisterExternalReferences( + ExternalReferenceRegistry* registry) { + registry->Register(GetFD); + registry->Register(GetExternal); + registry->Register(GetBytesRead); + registry->Register(GetBytesWritten); + registry->Register(JSMethod<&StreamBase::ReadStartJS>); + registry->Register(JSMethod<&StreamBase::ReadStopJS>); + registry->Register(JSMethod<&StreamBase::Shutdown>); + registry->Register(JSMethod<&StreamBase::UseUserBuffer>); + registry->Register(JSMethod<&StreamBase::Writev>); + registry->Register(JSMethod<&StreamBase::WriteBuffer>); + registry->Register(JSMethod<&StreamBase::WriteString<ASCII>>); + registry->Register(JSMethod<&StreamBase::WriteString<UTF8>>); + registry->Register(JSMethod<&StreamBase::WriteString<UCS2>>); + registry->Register(JSMethod<&StreamBase::WriteString<LATIN1>>); + registry->Register( + BaseObject::InternalFieldGet<StreamBase::kOnReadFunctionField>); + registry->Register( + BaseObject::InternalFieldSet<StreamBase::kOnReadFunctionField, + &Value::IsFunction>); +} + void StreamBase::GetFD(const FunctionCallbackInfo<Value>& args) { // Mimic implementation of StreamBase::GetFD() and UDPWrap::GetFD(). StreamBase* wrap = StreamBase::FromObject(args.This().As<Object>()); diff --git a/src/stream_base.h b/src/stream_base.h index a5680ba8860..e0da891501a 100644 --- a/src/stream_base.h +++ b/src/stream_base.h @@ -19,6 +19,7 @@ class ShutdownWrap; class WriteWrap; class StreamBase; class StreamResource; +class ExternalReferenceRegistry; struct StreamWriteResult { bool async; @@ -308,7 +309,7 @@ class StreamBase : public StreamResource { static void AddMethods(Environment* env, v8::Local<v8::FunctionTemplate> target); - + static void RegisterExternalReferences(ExternalReferenceRegistry* registry); virtual bool IsAlive() = 0; virtual bool IsClosing() = 0; virtual bool IsIPCPipe(); |