@@ -118,8 +118,9 @@ Local<Name> Uint32ToName(Local<Context> context, uint32_t index) {
118118
119119} // anonymous namespace
120120
121- BaseObjectPtr<ContextifyContext> ContextifyContext::New (
122- Environment* env, Local<Object> sandbox_obj, ContextOptions* options) {
121+ ContextifyContext* ContextifyContext::New (Environment* env,
122+ Local<Object> sandbox_obj,
123+ ContextOptions* options) {
123124 Local<ObjectTemplate> object_template;
124125 HandleScope scope (env->isolate ());
125126 CHECK_IMPLIES (sandbox_obj.IsEmpty (), options->vanilla );
@@ -140,41 +141,32 @@ BaseObjectPtr<ContextifyContext> ContextifyContext::New(
140141 if (!(CreateV8Context (env->isolate (), object_template, snapshot_data, queue)
141142 .ToLocal (&v8_context))) {
142143 // Allocation failure, maximum call stack size reached, termination, etc.
143- return BaseObjectPtr<ContextifyContext>() ;
144+ return {} ;
144145 }
145146 return New (v8_context, env, sandbox_obj, options);
146147}
147148
148- void ContextifyContext::MemoryInfo (MemoryTracker* tracker) const {}
149+ void ContextifyContext::Trace (cppgc::Visitor* visitor) const {
150+ CppgcMixin::Trace (visitor);
151+ visitor->Trace (context_);
152+ }
149153
150154ContextifyContext::ContextifyContext (Environment* env,
151155 Local<Object> wrapper,
152156 Local<Context> v8_context,
153157 ContextOptions* options)
154- : BaseObject(env, wrapper),
155- microtask_queue_ (options->own_microtask_queue
158+ : microtask_queue_(options->own_microtask_queue
156159 ? options->own_microtask_queue.release()
157160 : nullptr ) {
161+ CppgcMixin::Wrap (this , env, wrapper);
162+
158163 context_.Reset (env->isolate (), v8_context);
159164 // This should only be done after the initial initializations of the context
160165 // global object is finished.
161166 DCHECK_NULL (v8_context->GetAlignedPointerFromEmbedderData (
162167 ContextEmbedderIndex::kContextifyContext ));
163168 v8_context->SetAlignedPointerInEmbedderData (
164169 ContextEmbedderIndex::kContextifyContext , this );
165- // It's okay to make this reference weak - V8 would create an internal
166- // reference to this context via the constructor of the wrapper.
167- // As long as the wrapper is alive, it's constructor is alive, and so
168- // is the context.
169- context_.SetWeak ();
170- }
171-
172- ContextifyContext::~ContextifyContext () {
173- Isolate* isolate = env ()->isolate ();
174- HandleScope scope (isolate);
175-
176- env ()->UnassignFromContext (PersistentToLocal::Weak (isolate, context_));
177- context_.Reset ();
178170}
179171
180172void ContextifyContext::InitializeGlobalTemplates (IsolateData* isolate_data) {
@@ -251,19 +243,18 @@ MaybeLocal<Context> ContextifyContext::CreateV8Context(
251243 return scope.Escape (ctx);
252244}
253245
254- BaseObjectPtr<ContextifyContext> ContextifyContext::New (
255- Local<Context> v8_context,
256- Environment* env,
257- Local<Object> sandbox_obj,
258- ContextOptions* options) {
246+ ContextifyContext* ContextifyContext::New (Local<Context> v8_context,
247+ Environment* env,
248+ Local<Object> sandbox_obj,
249+ ContextOptions* options) {
259250 HandleScope scope (env->isolate ());
260251 CHECK_IMPLIES (sandbox_obj.IsEmpty (), options->vanilla );
261252 // This only initializes part of the context. The primordials are
262253 // only initialized when needed because even deserializing them slows
263254 // things down significantly and they are only needed in rare occasions
264255 // in the vm contexts.
265256 if (InitializeContextRuntime (v8_context).IsNothing ()) {
266- return BaseObjectPtr<ContextifyContext>() ;
257+ return {} ;
267258 }
268259
269260 Local<Context> main_context = env->context ();
@@ -300,7 +291,7 @@ BaseObjectPtr<ContextifyContext> ContextifyContext::New(
300291 info.origin = *origin_val;
301292 }
302293
303- BaseObjectPtr< ContextifyContext> result;
294+ ContextifyContext* result;
304295 Local<Object> wrapper;
305296 {
306297 Context::Scope context_scope (v8_context);
@@ -315,7 +306,7 @@ BaseObjectPtr<ContextifyContext> ContextifyContext::New(
315306 ctor_name,
316307 static_cast <v8::PropertyAttribute>(v8::DontEnum))
317308 .IsNothing ()) {
318- return BaseObjectPtr<ContextifyContext>() ;
309+ return {} ;
319310 }
320311 }
321312
@@ -328,21 +319,23 @@ BaseObjectPtr<ContextifyContext> ContextifyContext::New(
328319 env->host_defined_option_symbol (),
329320 options->host_defined_options_id )
330321 .IsNothing ()) {
331- return BaseObjectPtr<ContextifyContext>() ;
322+ return {} ;
332323 }
333324
334325 env->AssignToContext (v8_context, nullptr , info);
335326
336327 if (!env->contextify_wrapper_template ()
337328 ->NewInstance (v8_context)
338329 .ToLocal (&wrapper)) {
339- return BaseObjectPtr<ContextifyContext>() ;
330+ return {} ;
340331 }
341332
342- result =
343- MakeBaseObject<ContextifyContext>(env, wrapper, v8_context, options);
344- // The only strong reference to the wrapper will come from the sandbox.
345- result->MakeWeak ();
333+ result = cppgc::MakeGarbageCollected<ContextifyContext>(
334+ env->isolate ()->GetCppHeap ()->GetAllocationHandle (),
335+ env,
336+ wrapper,
337+ v8_context,
338+ options);
346339 }
347340
348341 Local<Object> wrapper_holder =
@@ -352,7 +345,7 @@ BaseObjectPtr<ContextifyContext> ContextifyContext::New(
352345 ->SetPrivate (
353346 v8_context, env->contextify_context_private_symbol (), wrapper)
354347 .IsNothing ()) {
355- return BaseObjectPtr<ContextifyContext>() ;
348+ return {} ;
356349 }
357350
358351 // Assign host_defined_options_id to the sandbox object or the global object
@@ -364,7 +357,7 @@ BaseObjectPtr<ContextifyContext> ContextifyContext::New(
364357 env->host_defined_option_symbol (),
365358 options->host_defined_options_id )
366359 .IsNothing ()) {
367- return BaseObjectPtr<ContextifyContext>() ;
360+ return {} ;
368361 }
369362 return result;
370363}
@@ -438,7 +431,7 @@ void ContextifyContext::MakeContext(const FunctionCallbackInfo<Value>& args) {
438431 options.host_defined_options_id = args[6 ].As <Symbol>();
439432
440433 TryCatchScope try_catch (env);
441- BaseObjectPtr< ContextifyContext> context_ptr =
434+ ContextifyContext* context_ptr =
442435 ContextifyContext::New (env, sandbox, &options);
443436
444437 if (try_catch.HasCaught ()) {
@@ -469,6 +462,10 @@ ContextifyContext* ContextifyContext::ContextFromContextifiedSandbox(
469462
470463template <typename T>
471464ContextifyContext* ContextifyContext::Get (const PropertyCallbackInfo<T>& args) {
465+ // TODO(joyeecheung): it should be fine to simply use
466+ // args.GetIsolate()->GetCurrentContext() and take the pointer at
467+ // ContextEmbedderIndex::kContextifyContext, as V8 is supposed to
468+ // push the creation context before invoking these callbacks.
472469 return Get (args.This ());
473470}
474471
0 commit comments