2222#include " node_contextify.h"
2323
2424#include " base_object-inl.h"
25+ #include " cppgc/allocation.h"
2526#include " memory_tracker-inl.h"
2627#include " module_wrap.h"
2728#include " node_context_data.h"
@@ -960,6 +961,12 @@ void ContextifyScript::RegisterExternalReferences(
960961 registry->Register (RunInContext);
961962}
962963
964+ ContextifyScript* ContextifyScript::New (Environment* env,
965+ Local<Object> object) {
966+ return cppgc::MakeGarbageCollected<ContextifyScript>(
967+ env->isolate ()->GetCppHeap ()->GetAllocationHandle (), env, object);
968+ }
969+
963970void ContextifyScript::New (const FunctionCallbackInfo<Value>& args) {
964971 Environment* env = Environment::GetCurrent (args);
965972 Isolate* isolate = env->isolate ();
@@ -1010,8 +1017,7 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
10101017 id_symbol = args[7 ].As <Symbol>();
10111018 }
10121019
1013- ContextifyScript* contextify_script =
1014- new ContextifyScript (env, args.This ());
1020+ ContextifyScript* contextify_script = New (env, args.This ());
10151021
10161022 if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED (
10171023 TRACING_CATEGORY_NODE2 (vm, script)) != 0 ) {
@@ -1069,9 +1075,7 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
10691075 return ;
10701076 }
10711077
1072- contextify_script->script_ .Reset (isolate, v8_script);
1073- contextify_script->script_ .SetWeak ();
1074- contextify_script->object ()->SetInternalField (kUnboundScriptSlot , v8_script);
1078+ contextify_script->set_unbound_script (v8_script);
10751079
10761080 std::unique_ptr<ScriptCompiler::CachedData> new_cached_data;
10771081 if (produce_cached_data) {
@@ -1174,11 +1178,9 @@ void ContextifyScript::CreateCachedData(
11741178 const FunctionCallbackInfo<Value>& args) {
11751179 Environment* env = Environment::GetCurrent (args);
11761180 ContextifyScript* wrapped_script;
1177- ASSIGN_OR_RETURN_UNWRAP (&wrapped_script, args.This ());
1178- Local<UnboundScript> unbound_script =
1179- PersistentToLocal::Default (env->isolate (), wrapped_script->script_ );
1181+ ASSIGN_OR_RETURN_UNWRAP_CPPGC (&wrapped_script, args.This ());
11801182 std::unique_ptr<ScriptCompiler::CachedData> cached_data (
1181- ScriptCompiler::CreateCodeCache (unbound_script));
1183+ ScriptCompiler::CreateCodeCache (wrapped_script-> unbound_script () ));
11821184 if (!cached_data) {
11831185 args.GetReturnValue ().Set (Buffer::New (env, 0 ).ToLocalChecked ());
11841186 } else {
@@ -1192,9 +1194,8 @@ void ContextifyScript::CreateCachedData(
11921194
11931195void ContextifyScript::RunInContext (const FunctionCallbackInfo<Value>& args) {
11941196 Environment* env = Environment::GetCurrent (args);
1195-
11961197 ContextifyScript* wrapped_script;
1197- ASSIGN_OR_RETURN_UNWRAP (&wrapped_script, args.This ());
1198+ ASSIGN_OR_RETURN_UNWRAP_CPPGC (&wrapped_script, args.This ());
11981199
11991200 CHECK_EQ (args.Length (), 5 );
12001201 CHECK (args[0 ]->IsObject () || args[0 ]->IsNull ());
@@ -1264,10 +1265,9 @@ bool ContextifyScript::EvalMachine(Local<Context> context,
12641265
12651266 TryCatchScope try_catch (env);
12661267 ContextifyScript* wrapped_script;
1267- ASSIGN_OR_RETURN_UNWRAP (&wrapped_script, args.This (), false );
1268- Local<UnboundScript> unbound_script =
1269- PersistentToLocal::Default (env->isolate (), wrapped_script->script_ );
1270- Local<Script> script = unbound_script->BindToCurrentContext ();
1268+ ASSIGN_OR_RETURN_UNWRAP_CPPGC (&wrapped_script, args.This (), false );
1269+ Local<Script> script =
1270+ wrapped_script->unbound_script ()->BindToCurrentContext ();
12711271
12721272#if HAVE_INSPECTOR
12731273 if (break_on_first_line) {
@@ -1349,9 +1349,21 @@ bool ContextifyScript::EvalMachine(Local<Context> context,
13491349 return true ;
13501350}
13511351
1352- ContextifyScript::ContextifyScript (Environment* env, Local<Object> object)
1353- : BaseObject(env, object) {
1354- MakeWeak ();
1352+ Local<UnboundScript> ContextifyScript::unbound_script () const {
1353+ return script_.Get (env ()->isolate ());
1354+ }
1355+
1356+ void ContextifyScript::set_unbound_script (Local<UnboundScript> script) {
1357+ script_.Reset (env ()->isolate (), script);
1358+ }
1359+
1360+ void ContextifyScript::Trace (cppgc::Visitor* visitor) const {
1361+ CppgcMixin::Trace (visitor);
1362+ visitor->Trace (script_);
1363+ }
1364+
1365+ ContextifyScript::ContextifyScript (Environment* env, Local<Object> object) {
1366+ CppgcMixin::Wrap (this , env, object);
13551367}
13561368
13571369ContextifyScript::~ContextifyScript () {}
0 commit comments