22
22
#include " node_contextify.h"
23
23
24
24
#include " base_object-inl.h"
25
+ #include " cppgc/allocation.h"
25
26
#include " memory_tracker-inl.h"
26
27
#include " module_wrap.h"
27
28
#include " node_context_data.h"
@@ -915,6 +916,12 @@ void ContextifyScript::RegisterExternalReferences(
915
916
registry->Register (RunInContext);
916
917
}
917
918
919
+ ContextifyScript* ContextifyScript::New (Environment* env,
920
+ Local<Object> object) {
921
+ return cppgc::MakeGarbageCollected<ContextifyScript>(
922
+ env->isolate ()->GetCppHeap ()->GetAllocationHandle (), env, object);
923
+ }
924
+
918
925
void ContextifyScript::New (const FunctionCallbackInfo<Value>& args) {
919
926
Environment* env = Environment::GetCurrent (args);
920
927
Isolate* isolate = env->isolate ();
@@ -965,8 +972,7 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
965
972
id_symbol = args[7 ].As <Symbol>();
966
973
}
967
974
968
- ContextifyScript* contextify_script =
969
- new ContextifyScript (env, args.This ());
975
+ ContextifyScript* contextify_script = New (env, args.This ());
970
976
971
977
if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED (
972
978
TRACING_CATEGORY_NODE2 (vm, script)) != 0 ) {
@@ -1025,8 +1031,6 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
1025
1031
}
1026
1032
1027
1033
contextify_script->script_ .Reset (isolate, v8_script);
1028
- contextify_script->script_ .SetWeak ();
1029
- contextify_script->object ()->SetInternalField (kUnboundScriptSlot , v8_script);
1030
1034
1031
1035
std::unique_ptr<ScriptCompiler::CachedData> new_cached_data;
1032
1036
if (produce_cached_data) {
@@ -1128,12 +1132,10 @@ bool ContextifyScript::InstanceOf(Environment* env,
1128
1132
void ContextifyScript::CreateCachedData (
1129
1133
const FunctionCallbackInfo<Value>& args) {
1130
1134
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 ());
1135
1137
std::unique_ptr<ScriptCompiler::CachedData> cached_data (
1136
- ScriptCompiler::CreateCodeCache (unbound_script));
1138
+ ScriptCompiler::CreateCodeCache (wrapped_script-> unbound_script () ));
1137
1139
if (!cached_data) {
1138
1140
args.GetReturnValue ().Set (Buffer::New (env, 0 ).ToLocalChecked ());
1139
1141
} else {
@@ -1147,9 +1149,8 @@ void ContextifyScript::CreateCachedData(
1147
1149
1148
1150
void ContextifyScript::RunInContext (const FunctionCallbackInfo<Value>& args) {
1149
1151
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 ());
1153
1154
1154
1155
CHECK_EQ (args.Length (), 5 );
1155
1156
CHECK (args[0 ]->IsObject () || args[0 ]->IsNull ());
@@ -1218,11 +1219,10 @@ bool ContextifyScript::EvalMachine(Local<Context> context,
1218
1219
}
1219
1220
1220
1221
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 ();
1226
1226
1227
1227
#if HAVE_INSPECTOR
1228
1228
if (break_on_first_line) {
@@ -1304,9 +1304,21 @@ bool ContextifyScript::EvalMachine(Local<Context> context,
1304
1304
return true ;
1305
1305
}
1306
1306
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);
1310
1322
}
1311
1323
1312
1324
ContextifyScript::~ContextifyScript () {}
0 commit comments