diff options
| author | Joyee Cheung <joyeec9h3@gmail.com> | 2022-09-26 21:40:45 +0800 |
|---|---|---|
| committer | Joyee Cheung <joyeec9h3@gmail.com> | 2022-10-04 19:34:29 +0800 |
| commit | 5b1bcf82f1d6990644285cd8dfc709340f7abb5b (patch) | |
| tree | c3b808424ea24beb9b89c9df9ea0105a0346fcf4 /src/node_contextify.h | |
| parent | b3531bf735afcd87db718095681d04ef8269f70f (diff) | |
| download | node-new-5b1bcf82f1d6990644285cd8dfc709340f7abb5b.tar.gz | |
vm: make ContextifyContext a BaseObject
Instead of adding a reference to the ContextifyContext by using
a v8::External, we make ContextifyContext a weak BaseObject that
whose wrapper is referenced by the sandbox via a private symbol.
This makes it easier to snapshot the contexts, in addition to
reusing the BaseObject lifetime management for ContextifyContexts.
PR-URL: https://github.com/nodejs/node/pull/44796
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/node_contextify.h')
| -rw-r--r-- | src/node_contextify.h | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/src/node_contextify.h b/src/node_contextify.h index a424c56d7e..8744016950 100644 --- a/src/node_contextify.h +++ b/src/node_contextify.h @@ -41,23 +41,23 @@ struct ContextOptions { BaseObjectPtr<MicrotaskQueueWrap> microtask_queue_wrap; }; -class ContextifyContext { +class ContextifyContext : public BaseObject { public: ContextifyContext(Environment* env, - v8::Local<v8::Object> sandbox_obj, + v8::Local<v8::Object> wrapper, + v8::Local<v8::Context> v8_context, const ContextOptions& options); ~ContextifyContext(); - static void CleanupHook(void* arg); + + void MemoryInfo(MemoryTracker* tracker) const override; + SET_MEMORY_INFO_NAME(ContextifyContext) + SET_SELF_SIZE(ContextifyContext) static v8::MaybeLocal<v8::Context> CreateV8Context( v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> object_template, const SnapshotData* snapshot_data, v8::MicrotaskQueue* queue); - bool InitializeContext(v8::Local<v8::Context> ctx, - Environment* env, - v8::Local<v8::Object> sandbox_obj, - const ContextOptions& options); static void Init(Environment* env, v8::Local<v8::Object> target); static void RegisterExternalReferences(ExternalReferenceRegistry* registry); @@ -65,10 +65,6 @@ class ContextifyContext { Environment* env, const v8::Local<v8::Object>& sandbox); - inline Environment* env() const { - return env_; - } - inline v8::Local<v8::Context> context() const { return PersistentToLocal::Default(env()->isolate(), context_); } @@ -89,11 +85,20 @@ class ContextifyContext { template <typename T> static ContextifyContext* Get(const v8::PropertyCallbackInfo<T>& args); + static ContextifyContext* Get(v8::Local<v8::Object> object); - static v8::Local<v8::ObjectTemplate> CreateGlobalTemplate( - v8::Isolate* isolate); + static void InitializeGlobalTemplates(IsolateData* isolate_data); private: + static BaseObjectPtr<ContextifyContext> New(Environment* env, + v8::Local<v8::Object> sandbox_obj, + const ContextOptions& options); + // Initialize a context created from CreateV8Context() + static BaseObjectPtr<ContextifyContext> New(v8::Local<v8::Context> ctx, + Environment* env, + v8::Local<v8::Object> sandbox_obj, + const ContextOptions& options); + static bool IsStillInitializing(const ContextifyContext* ctx); static void MakeContext(const v8::FunctionCallbackInfo<v8::Value>& args); static void IsContext(const v8::FunctionCallbackInfo<v8::Value>& args); @@ -137,7 +142,7 @@ class ContextifyContext { static void IndexedPropertyDeleterCallback( uint32_t index, const v8::PropertyCallbackInfo<v8::Boolean>& args); - Environment* const env_; + v8::Global<v8::Context> context_; BaseObjectPtr<MicrotaskQueueWrap> microtask_queue_wrap_; }; |
