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"
@@ -915,6 +916,12 @@ void ContextifyScript::RegisterExternalReferences(
915916 registry->Register (RunInContext);
916917}
917918
919+ ContextifyScript* ContextifyScript::New (Environment* env,
920+ Local<Object> object) {
921+ return cppgc::MakeGarbageCollected<ContextifyScript>(
922+ env->isolate ()->GetCppHeap ()->GetAllocationHandle (), env, object);
923+ }
924+
918925void ContextifyScript::New (const FunctionCallbackInfo<Value>& args) {
919926 Environment* env = Environment::GetCurrent (args);
920927 Isolate* isolate = env->isolate ();
@@ -965,8 +972,7 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
965972 id_symbol = args[7 ].As <Symbol>();
966973 }
967974
968- ContextifyScript* contextify_script =
969- new ContextifyScript (env, args.This ());
975+ ContextifyScript* contextify_script = New (env, args.This ());
970976
971977 if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED (
972978 TRACING_CATEGORY_NODE2 (vm, script)) != 0 ) {
@@ -1025,8 +1031,6 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
10251031 }
10261032
10271033 contextify_script->script_ .Reset (isolate, v8_script);
1028- contextify_script->script_ .SetWeak ();
1029- contextify_script->object ()->SetInternalField (kUnboundScriptSlot , v8_script);
10301034
10311035 std::unique_ptr<ScriptCompiler::CachedData> new_cached_data;
10321036 if (produce_cached_data) {
@@ -1128,12 +1132,10 @@ bool ContextifyScript::InstanceOf(Environment* env,
11281132void ContextifyScript::CreateCachedData (
11291133 const FunctionCallbackInfo<Value>& args) {
11301134 Environment* env = Environment::GetCurrent (args);
1131- ContextifyScript* wrapped_script;
1132- ASSIGN_OR_RETURN_UNWRAP (&wrapped_script, args.This ());
1133- Local<UnboundScript> unbound_script =
1134- PersistentToLocal::Default (env->isolate (), wrapped_script->script_ );
1135+ ContextifyScript* wrapped_script = wrapped_script =
1136+ Unwrap<ContextifyScript>(args.This ());
11351137 std::unique_ptr<ScriptCompiler::CachedData> cached_data (
1136- ScriptCompiler::CreateCodeCache (unbound_script));
1138+ ScriptCompiler::CreateCodeCache (wrapped_script-> unbound_script () ));
11371139 if (!cached_data) {
11381140 args.GetReturnValue ().Set (Buffer::New (env, 0 ).ToLocalChecked ());
11391141 } else {
@@ -1147,9 +1149,8 @@ void ContextifyScript::CreateCachedData(
11471149
11481150void ContextifyScript::RunInContext (const FunctionCallbackInfo<Value>& args) {
11491151 Environment* env = Environment::GetCurrent (args);
1150-
1151- ContextifyScript* wrapped_script;
1152- ASSIGN_OR_RETURN_UNWRAP (&wrapped_script, args.This ());
1152+ ContextifyScript* wrapped_script = wrapped_script =
1153+ Unwrap<ContextifyScript>(args.This ());
11531154
11541155 CHECK_EQ (args.Length (), 5 );
11551156 CHECK (args[0 ]->IsObject () || args[0 ]->IsNull ());
@@ -1218,11 +1219,10 @@ bool ContextifyScript::EvalMachine(Local<Context> context,
12181219 }
12191220
12201221 TryCatchScope try_catch (env);
1221- ContextifyScript* wrapped_script;
1222- ASSIGN_OR_RETURN_UNWRAP (&wrapped_script, args.This (), false );
1223- Local<UnboundScript> unbound_script =
1224- PersistentToLocal::Default (env->isolate (), wrapped_script->script_ );
1225- Local<Script> script = unbound_script->BindToCurrentContext ();
1222+ ContextifyScript* wrapped_script = wrapped_script =
1223+ Unwrap<ContextifyScript>(args.This ());
1224+ Local<Script> script =
1225+ wrapped_script->unbound_script ()->BindToCurrentContext ();
12261226
12271227#if HAVE_INSPECTOR
12281228 if (break_on_first_line) {
@@ -1304,9 +1304,21 @@ bool ContextifyScript::EvalMachine(Local<Context> context,
13041304 return true ;
13051305}
13061306
1307- ContextifyScript::ContextifyScript (Environment* env, Local<Object> object)
1308- : BaseObject(env, object) {
1309- MakeWeak ();
1307+ Local<UnboundScript> ContextifyScript::unbound_script () const {
1308+ return script_.Get (env ()->isolate ());
1309+ }
1310+
1311+ void ContextifyScript::set_unbound_script (Local<UnboundScript> script) {
1312+ script_.Reset (env ()->isolate (), script);
1313+ }
1314+
1315+ void ContextifyScript::Trace (cppgc::Visitor* visitor) const {
1316+ CppgcMixin::Trace (visitor);
1317+ visitor->Trace (script_);
1318+ }
1319+
1320+ ContextifyScript::ContextifyScript (Environment* env, Local<Object> object) {
1321+ InitializeCppgc (this , env, object);
13101322}
13111323
13121324ContextifyScript::~ContextifyScript () {}
0 commit comments