Skip to content

Commit e94cf59

Browse files
committed
vm: migrate ContextifyScript to cppgc
1 parent 0c7e2a5 commit e94cf59

File tree

3 files changed

+49
-36
lines changed

3 files changed

+49
-36
lines changed

benchmark/vm/compile-script-in-isolate-cache.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
const common = require('../common.js');
66
const fs = require('fs');
77
const vm = require('vm');
8-
const fixtures = require('../../test/common/fixtures.js');
9-
const scriptPath = fixtures.path('snapshot', 'typescript.js');
8+
const path = require('path');
109

1110
const bench = common.createBenchmark(main, {
1211
type: ['with-dynamic-import-callback', 'without-dynamic-import-callback'],
13-
n: [100],
12+
filename: ['test/fixtures/snapshot/typescript.js', 'test/fixtures/syntax/good_syntax.js'],
13+
n: [1000],
1414
});
1515

16-
const scriptSource = fs.readFileSync(scriptPath, 'utf8');
17-
18-
function main({ n, type }) {
16+
function main({ n, type, filename }) {
17+
const scriptPath = path.resolve(__dirname, '..', '..', filename);
18+
const scriptSource = fs.readFileSync(scriptPath, 'utf8');
1919
let script;
2020
bench.start();
2121
const options = {};

src/node_contextify.cc

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
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+
918925
void 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,
11281132
void 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

11481150
void 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

13121324
ContextifyScript::~ContextifyScript() {}

src/node_contextify.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

66
#include "base_object-inl.h"
7+
#include "cppgc_helpers.h"
78
#include "node_context_data.h"
89
#include "node_errors.h"
910

@@ -139,23 +140,23 @@ class ContextifyContext : public BaseObject {
139140
std::unique_ptr<v8::MicrotaskQueue> microtask_queue_;
140141
};
141142

142-
class ContextifyScript : public BaseObject {
143+
class ContextifyScript final : public cppgc::GarbageCollected<ContextifyScript>,
144+
public cppgc::NameProvider,
145+
public CppgcMixin {
143146
public:
144-
enum InternalFields {
145-
kUnboundScriptSlot = BaseObject::kInternalFieldCount,
146-
kInternalFieldCount
147-
};
148-
149-
SET_NO_MEMORY_INFO()
150-
SET_MEMORY_INFO_NAME(ContextifyScript)
151-
SET_SELF_SIZE(ContextifyScript)
147+
SET_CPPGC_NAME(ContextifyScript)
148+
void Trace(cppgc::Visitor* visitor) const final;
152149

153150
ContextifyScript(Environment* env, v8::Local<v8::Object> object);
154151
~ContextifyScript() override;
155152

153+
v8::Local<v8::UnboundScript> unbound_script() const;
154+
void set_unbound_script(v8::Local<v8::UnboundScript>);
155+
156156
static void CreatePerIsolateProperties(IsolateData* isolate_data,
157157
v8::Local<v8::ObjectTemplate> target);
158158
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
159+
static ContextifyScript* New(Environment* env, v8::Local<v8::Object> object);
159160
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
160161
static bool InstanceOf(Environment* env, const v8::Local<v8::Value>& args);
161162
static void CreateCachedData(const v8::FunctionCallbackInfo<v8::Value>& args);
@@ -170,7 +171,7 @@ class ContextifyScript : public BaseObject {
170171
const v8::FunctionCallbackInfo<v8::Value>& args);
171172

172173
private:
173-
v8::Global<v8::UnboundScript> script_;
174+
v8::TracedReference<v8::UnboundScript> script_;
174175
};
175176

176177
v8::Maybe<bool> StoreCodeCacheResult(

0 commit comments

Comments
 (0)