Skip to content

Commit

Permalink
src: snapshot node
Browse files Browse the repository at this point in the history
  • Loading branch information
joyeecheung committed Apr 21, 2020
1 parent 52d8832 commit c620a7c
Show file tree
Hide file tree
Showing 21 changed files with 322 additions and 15 deletions.
16 changes: 16 additions & 0 deletions src/async_wrap.cc
Expand Up @@ -23,6 +23,7 @@
#include "async_wrap-inl.h"
#include "env-inl.h"
#include "node_errors.h"
#include "node_external_reference.h"
#include "tracing/traced_value.h"
#include "util-inl.h"

Expand Down Expand Up @@ -576,6 +577,21 @@ void AsyncWrap::Initialize(Local<Object> target,
}
}

void AsyncWrap::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(SetupHooks);
registry->Register(PushAsyncContext);
registry->Register(PopAsyncContext);
registry->Register(QueueDestroyAsyncId);
registry->Register(EnablePromiseHook);
registry->Register(DisablePromiseHook);
registry->Register(PromiseWrap::getIsChainedPromise);
registry->Register(RegisterDestroyHook);
registry->Register(AsyncWrapObject::New);
registry->Register(AsyncWrap::GetAsyncId);
registry->Register(AsyncWrap::AsyncReset);
registry->Register(AsyncWrap::GetProviderType);
}

AsyncWrap::AsyncWrap(Environment* env,
Local<Object> object,
Expand Down
2 changes: 2 additions & 0 deletions src/async_wrap.h
Expand Up @@ -99,6 +99,7 @@ namespace node {

class Environment;
class DestroyParam;
class ExternalReferenceRegistry;

class AsyncWrap : public BaseObject {
public:
Expand Down Expand Up @@ -128,6 +129,7 @@ class AsyncWrap : public BaseObject {
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
Environment* env);

static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
static void Initialize(v8::Local<v8::Object> target,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
Expand Down
28 changes: 27 additions & 1 deletion src/inspector_js_api.cc
Expand Up @@ -2,9 +2,10 @@
#include "inspector_agent.h"
#include "inspector_io.h"
#include "memory_tracker-inl.h"
#include "node_external_reference.h"
#include "util-inl.h"
#include "v8.h"
#include "v8-inspector.h"
#include "v8.h"

#include <memory>

Expand Down Expand Up @@ -346,6 +347,31 @@ void Initialize(Local<Object> target, Local<Value> unused,
}

} // namespace

void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(InspectorConsoleCall);
registry->Register(SetConsoleExtensionInstaller);
registry->Register(CallAndPauseOnStart);
registry->Register(Open);
registry->Register(Url);
registry->Register(WaitForDebugger);

registry->Register(AsyncTaskScheduledWrapper);
registry->Register(InvokeAsyncTaskFnWithId<&Agent::AsyncTaskCanceled>);
registry->Register(InvokeAsyncTaskFnWithId<&Agent::AsyncTaskStarted>);
registry->Register(InvokeAsyncTaskFnWithId<&Agent::AsyncTaskFinished>);

registry->Register(RegisterAsyncHookWrapper);
registry->Register(IsEnabled);

registry->Register(JSBindingsConnection<LocalConnection>::New);
registry->Register(JSBindingsConnection<LocalConnection>::Dispatch);
registry->Register(JSBindingsConnection<LocalConnection>::Disconnect);
registry->Register(JSBindingsConnection<MainThreadConnection>::New);
registry->Register(JSBindingsConnection<MainThreadConnection>::Dispatch);
registry->Register(JSBindingsConnection<MainThreadConnection>::Disconnect);
}

} // namespace inspector
} // namespace node

Expand Down
38 changes: 38 additions & 0 deletions src/node_buffer.cc
Expand Up @@ -22,6 +22,7 @@
#include "node_buffer.h"
#include "node.h"
#include "node_errors.h"
#include "node_external_reference.h"
#include "node_internals.h"

#include "env-inl.h"
Expand Down Expand Up @@ -1197,6 +1198,7 @@ void Initialize(Local<Object> target,
env->SetMethod(target, "ucs2Write", StringWrite<UCS2>);
env->SetMethod(target, "utf8Write", StringWrite<UTF8>);

// TODO(joyee): re-initialize upon snapshot deserialization
// It can be a nullptr when running inside an isolate where we
// do not own the ArrayBuffer allocator.
if (NodeArrayBufferAllocator* allocator =
Expand All @@ -1222,6 +1224,42 @@ void Initialize(Local<Object> target,
}

} // anonymous namespace

void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(SetBufferPrototype);
registry->Register(CreateFromString);

registry->Register(ByteLengthUtf8);
registry->Register(Copy);
registry->Register(Compare);
registry->Register(CompareOffset);
registry->Register(Fill);
registry->Register(IndexOfBuffer);
registry->Register(IndexOfNumber);
registry->Register(IndexOfString);

registry->Register(Swap16);
registry->Register(Swap32);
registry->Register(Swap64);

registry->Register(EncodeInto);
registry->Register(EncodeUtf8String);

registry->Register(StringSlice<ASCII>);
registry->Register(StringSlice<BASE64>);
registry->Register(StringSlice<LATIN1>);
registry->Register(StringSlice<HEX>);
registry->Register(StringSlice<UCS2>);
registry->Register(StringSlice<UTF8>);

registry->Register(StringWrite<ASCII>);
registry->Register(StringWrite<BASE64>);
registry->Register(StringWrite<LATIN1>);
registry->Register(StringWrite<HEX>);
registry->Register(StringWrite<UCS2>);
registry->Register(StringWrite<UTF8>);
}

} // namespace Buffer
} // namespace node

Expand Down
20 changes: 20 additions & 0 deletions src/node_credentials.cc
@@ -1,4 +1,5 @@
#include "env-inl.h"
#include "node_external_reference.h"
#include "node_internals.h"
#include "util-inl.h"

Expand Down Expand Up @@ -373,6 +374,25 @@ static void InitGroups(const FunctionCallbackInfo<Value>& args) {

#endif // NODE_IMPLEMENTS_POSIX_CREDENTIALS

void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(SafeGetenv);

#ifdef NODE_IMPLEMENTS_POSIX_CREDENTIALS
registry->Register(GetUid);
registry->Register(GetEUid);
registry->Register(GetGid);
registry->Register(GetEGid);
registry->Register(GetGroups);

registry->Register(InitGroups);
registry->Register(SetEGid);
registry->Register(SetEUid);
registry->Register(SetGid);
registry->Register(SetUid);
registry->Register(SetGroups);
#endif // NODE_IMPLEMENTS_POSIX_CREDENTIALS
}

static void Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
Expand Down
10 changes: 10 additions & 0 deletions src/node_env_var.cc
@@ -1,6 +1,7 @@
#include "debug_utils-inl.h"
#include "env-inl.h"
#include "node_errors.h"
#include "node_external_reference.h"
#include "node_process.h"

#include <time.h> // tzset(), _tzset()
Expand Down Expand Up @@ -387,4 +388,13 @@ MaybeLocal<Object> CreateEnvVarProxy(Local<Context> context,
PropertyHandlerFlags::kHasNoSideEffect));
return scope.EscapeMaybe(env_proxy_template->NewInstance(context));
}

void RegisterEnvVarExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(EnvGetter);
registry->Register(EnvSetter);
registry->Register(EnvQuery);
registry->Register(EnvDeleter);
registry->Register(EnvEnumerator);
}

} // namespace node
10 changes: 9 additions & 1 deletion src/node_errors.cc
Expand Up @@ -3,9 +3,10 @@

#include "debug_utils-inl.h"
#include "node_errors.h"
#include "node_external_reference.h"
#include "node_internals.h"
#include "node_report.h"
#include "node_process.h"
#include "node_report.h"
#include "node_v8_platform-inl.h"
#include "util-inl.h"

Expand Down Expand Up @@ -833,6 +834,13 @@ static void TriggerUncaughtException(const FunctionCallbackInfo<Value>& args) {
errors::TriggerUncaughtException(isolate, exception, message, from_promise);
}

void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(SetPrepareStackTraceCallback);
registry->Register(SetEnhanceStackForFatalException);
registry->Register(NoSideEffectsToString);
registry->Register(TriggerUncaughtException);
}

void Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
Expand Down
3 changes: 3 additions & 0 deletions src/node_errors.h
Expand Up @@ -161,8 +161,11 @@ inline v8::Local<v8::Value> ERR_STRING_TOO_LONG(v8::Isolate* isolate) {
prefix " must be a string"); \
} while (0)

class ExternalReferenceRegistry;
namespace errors {

void RegisterExternalReferences(ExternalReferenceRegistry* registry);

class TryCatchScope : public v8::TryCatch {
public:
enum class CatchMode { kNormal, kFatal };
Expand Down
61 changes: 56 additions & 5 deletions src/node_external_reference.h
Expand Up @@ -5,6 +5,7 @@

#include <cinttypes>
#include <vector>
#include "v8.h"

namespace node {

Expand All @@ -14,21 +15,71 @@ class ExternalReferenceRegistry {
public:
ExternalReferenceRegistry() {}

template <typename T>
void Register(T* address) {
external_references_.push_back(reinterpret_cast<intptr_t>(address));
}
#define ALLOWED_EXTERNAL_REFERENCE_TYPES(V) \
V(v8::FunctionCallback) \
V(v8::AccessorGetterCallback) \
V(v8::AccessorSetterCallback) \
V(v8::AccessorNameGetterCallback) \
V(v8::AccessorNameSetterCallback) \
V(v8::GenericNamedPropertyDefinerCallback) \
V(v8::GenericNamedPropertyDeleterCallback) \
V(v8::GenericNamedPropertyEnumeratorCallback) \
V(v8::GenericNamedPropertyQueryCallback) \
V(v8::GenericNamedPropertySetterCallback)

#define V(ExternalReferenceType) \
void Register(ExternalReferenceType addr) { RegisterT(addr); }
ALLOWED_EXTERNAL_REFERENCE_TYPES(V)
#undef V

// This can be called only once.
const std::vector<intptr_t>& external_references();

bool is_empty() { return external_references_.empty(); }

private:
template <typename T>
void RegisterT(T* address) {
external_references_.push_back(reinterpret_cast<intptr_t>(address));
}
bool is_finalized_ = false;
std::vector<intptr_t> external_references_;
};

namespace errors {
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
}
#if HAVE_INSPECTOR
namespace inspector {
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
}
#endif // HAVE_INSPECTOR
namespace Buffer {
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
}
namespace credentials {
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
}
namespace i18n {
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
}
namespace task_queue {
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
}
namespace url {
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
}
namespace util {
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
}
void RegisterNodeCategorySetExternalReferences(
ExternalReferenceRegistry* registry);
void RegisterProcessMethodsExternalReferences(
ExternalReferenceRegistry* registry);
void RegisterTypesExternalReferences(ExternalReferenceRegistry* registry);
void RegisterStringDecoderExternalReferences(
ExternalReferenceRegistry* registry);
void RegisterTimerExternalReferences(ExternalReferenceRegistry* registry);
void RegisterEnvVarExternalReferences(ExternalReferenceRegistry* registry);
} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
Expand Down
12 changes: 12 additions & 0 deletions src/node_i18n.cc
Expand Up @@ -41,6 +41,7 @@


#include "node_i18n.h"
#include "node_external_reference.h"

#if defined(NODE_HAVE_I18N_SUPPORT)

Expand Down Expand Up @@ -824,6 +825,17 @@ void Initialize(Local<Object> target,
env->SetMethod(target, "hasConverter", ConverterObject::Has);
}

void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(ToUnicode);
registry->Register(ToASCII);
registry->Register(GetStringWidth);
registry->Register(ICUErrorName);
registry->Register(Transcode);
registry->Register(ConverterObject::Create);
registry->Register(ConverterObject::Decode);
registry->Register(ConverterObject::Has);
}

} // namespace i18n
} // namespace node

Expand Down
3 changes: 2 additions & 1 deletion src/node_i18n.h
Expand Up @@ -36,8 +36,9 @@
#include <string>

namespace node {

class ExternalReferenceRegistry;
namespace i18n {
void RegisterExternalReferences(ExternalReferenceRegistry* registry);

bool InitializeICUDirectory(const std::string& path);

Expand Down

0 comments on commit c620a7c

Please sign in to comment.