Skip to content

Commit a2cc905

Browse files
committed
vm: migrate ContextifyScript to cppgc
1 parent 3f2bc78 commit a2cc905

File tree

4 files changed

+53
-41
lines changed

4 files changed

+53
-41
lines changed

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

+6-6
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/heap_utils.cc

+20-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#endif
1919

2020
using v8::Array;
21+
using v8::Data;
2122
using v8::Boolean;
2223
using v8::Context;
2324
using v8::EmbedderGraph;
@@ -49,17 +50,20 @@ class JSGraphJSNode : public EmbedderGraph::Node {
4950
const char* Name() override { return "<JS Node>"; }
5051
size_t SizeInBytes() override { return 0; }
5152
bool IsEmbedderNode() override { return false; }
52-
Local<Value> JSValue() { return PersistentToLocal::Strong(persistent_); }
53+
Local<Data> V8Value() { return PersistentToLocal::Strong(persistent_); }
5354

5455
int IdentityHash() {
55-
Local<Value> v = JSValue();
56+
Local<Data> d = V8Value();
57+
// TODO(joyeecheung): return something better?
58+
if (!d->IsValue()) return reinterpret_cast<std::uintptr_t>(this);
59+
Local<Value> v = d.As<Value>();
5660
if (v->IsObject()) return v.As<Object>()->GetIdentityHash();
5761
if (v->IsName()) return v.As<v8::Name>()->GetIdentityHash();
5862
if (v->IsInt32()) return v.As<v8::Int32>()->Value();
5963
return 0;
6064
}
6165

62-
JSGraphJSNode(Isolate* isolate, Local<Value> val)
66+
JSGraphJSNode(Isolate* isolate, Local<Data> val)
6367
: persistent_(isolate, val) {
6468
CHECK(!val.IsEmpty());
6569
}
@@ -72,19 +76,27 @@ class JSGraphJSNode : public EmbedderGraph::Node {
7276

7377
struct Equal {
7478
inline bool operator()(JSGraphJSNode* a, JSGraphJSNode* b) const {
75-
return a->JSValue()->SameValue(b->JSValue());
79+
Local<Data> data_a = a->V8Value();
80+
Local<Data> data_b = a->V8Value();
81+
if (data_a->IsValue()) {
82+
if (!data_b->IsValue()) {
83+
return false;
84+
}
85+
return data_a.As<Value>()->SameValue(data_b.As<Value>());
86+
}
87+
return data_a == data_b;
7688
}
7789
};
7890

7991
private:
80-
Global<Value> persistent_;
92+
Global<Data> persistent_;
8193
};
8294

8395
class JSGraph : public EmbedderGraph {
8496
public:
8597
explicit JSGraph(Isolate* isolate) : isolate_(isolate) {}
8698

87-
Node* V8Node(const Local<Value>& value) override {
99+
Node* V8Node(const Local<v8::Data>& value) override {
88100
std::unique_ptr<JSGraphJSNode> n { new JSGraphJSNode(isolate_, value) };
89101
auto it = engine_nodes_.find(n.get());
90102
if (it != engine_nodes_.end())
@@ -153,8 +165,8 @@ class JSGraph : public EmbedderGraph {
153165
if (nodes->Set(context, i++, obj).IsNothing())
154166
return MaybeLocal<Array>();
155167
if (!n->IsEmbedderNode()) {
156-
value = static_cast<JSGraphJSNode*>(n.get())->JSValue();
157-
if (obj->Set(context, value_string, value).IsNothing())
168+
Local<Data> data = static_cast<JSGraphJSNode*>(n.get())->V8Value();
169+
if (data->IsValue() && obj->Set(context, value_string, data.As<Value>()).IsNothing())
158170
return MaybeLocal<Array>();
159171
}
160172
}

src/node_contextify.cc

+19-17
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "node_contextify.h"
2323

24+
#include "cppgc/allocation.h"
2425
#include "base_object-inl.h"
2526
#include "memory_tracker-inl.h"
2627
#include "module_wrap.h"
@@ -826,8 +827,9 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
826827
id_symbol = args[7].As<Symbol>();
827828
}
828829

829-
ContextifyScript* contextify_script =
830-
new ContextifyScript(env, args.This());
830+
831+
ContextifyScript* contextify_script = cppgc::MakeGarbageCollected<ContextifyScript>(
832+
env->isolate()->GetCppHeap()->GetAllocationHandle(), env, args.This());
831833

832834
if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
833835
TRACING_CATEGORY_NODE2(vm, script)) != 0) {
@@ -887,8 +889,6 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
887889
}
888890

889891
contextify_script->script_.Reset(isolate, v8_script);
890-
contextify_script->script_.SetWeak();
891-
contextify_script->object()->SetInternalField(kUnboundScriptSlot, v8_script);
892892

893893
std::unique_ptr<ScriptCompiler::CachedData> new_cached_data;
894894
if (produce_cached_data) {
@@ -990,10 +990,9 @@ bool ContextifyScript::InstanceOf(Environment* env,
990990
void ContextifyScript::CreateCachedData(
991991
const FunctionCallbackInfo<Value>& args) {
992992
Environment* env = Environment::GetCurrent(args);
993-
ContextifyScript* wrapped_script;
994-
ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.Holder());
995-
Local<UnboundScript> unbound_script =
996-
PersistentToLocal::Default(env->isolate(), wrapped_script->script_);
993+
ContextifyScript* wrapped_script = CppgcMixin::Unwrap<ContextifyScript>(args.Holder());
994+
CHECK_NOT_NULL(wrapped_script);
995+
Local<UnboundScript> unbound_script = wrapped_script->script_.Get(env->isolate());
997996
std::unique_ptr<ScriptCompiler::CachedData> cached_data(
998997
ScriptCompiler::CreateCodeCache(unbound_script));
999998
if (!cached_data) {
@@ -1010,8 +1009,8 @@ void ContextifyScript::CreateCachedData(
10101009
void ContextifyScript::RunInContext(const FunctionCallbackInfo<Value>& args) {
10111010
Environment* env = Environment::GetCurrent(args);
10121011

1013-
ContextifyScript* wrapped_script;
1014-
ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.Holder());
1012+
ContextifyScript* wrapped_script = CppgcMixin::Unwrap<ContextifyScript>(args.Holder());
1013+
CHECK_NOT_NULL(wrapped_script);
10151014

10161015
CHECK_EQ(args.Length(), 5);
10171016
CHECK(args[0]->IsObject() || args[0]->IsNull());
@@ -1081,10 +1080,9 @@ bool ContextifyScript::EvalMachine(Local<Context> context,
10811080

10821081
TryCatchScope try_catch(env);
10831082
Isolate::SafeForTerminationScope safe_for_termination(env->isolate());
1084-
ContextifyScript* wrapped_script;
1085-
ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.Holder(), false);
1086-
Local<UnboundScript> unbound_script =
1087-
PersistentToLocal::Default(env->isolate(), wrapped_script->script_);
1083+
ContextifyScript* wrapped_script = CppgcMixin::Unwrap<ContextifyScript>(args.Holder());
1084+
CHECK_NOT_NULL(wrapped_script);
1085+
Local<UnboundScript> unbound_script = wrapped_script->script_.Get(env->isolate());
10881086
Local<Script> script = unbound_script->BindToCurrentContext();
10891087

10901088
#if HAVE_INSPECTOR
@@ -1152,9 +1150,13 @@ bool ContextifyScript::EvalMachine(Local<Context> context,
11521150
return true;
11531151
}
11541152

1155-
ContextifyScript::ContextifyScript(Environment* env, Local<Object> object)
1156-
: BaseObject(env, object) {
1157-
MakeWeak();
1153+
void ContextifyScript::Trace(cppgc::Visitor* visitor) const {
1154+
CppgcMixin::Trace(visitor);
1155+
visitor->Trace(script_);
1156+
}
1157+
1158+
ContextifyScript::ContextifyScript(Environment* env, Local<Object> object) {
1159+
InitializeCppgc(this, env, object);
11581160
}
11591161

11601162
ContextifyScript::~ContextifyScript() {}

src/node_contextify.h

+8-10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "base_object-inl.h"
77
#include "node_context_data.h"
88
#include "node_errors.h"
9+
#include "cppgc_helpers.h"
910

1011
namespace node {
1112
class ExternalReferenceRegistry;
@@ -152,16 +153,13 @@ class ContextifyContext : public BaseObject {
152153
std::unique_ptr<v8::MicrotaskQueue> microtask_queue_;
153154
};
154155

155-
class ContextifyScript : public BaseObject {
156+
class ContextifyScript final
157+
: public cppgc::GarbageCollected<ContextifyScript>,
158+
public cppgc::NameProvider,
159+
public CppgcMixin {
156160
public:
157-
enum InternalFields {
158-
kUnboundScriptSlot = BaseObject::kInternalFieldCount,
159-
kInternalFieldCount
160-
};
161-
162-
SET_NO_MEMORY_INFO()
163-
SET_MEMORY_INFO_NAME(ContextifyScript)
164-
SET_SELF_SIZE(ContextifyScript)
161+
SET_CPPGC_NAME(ContextifyScript)
162+
void Trace(cppgc::Visitor* visitor) const final;
165163

166164
ContextifyScript(Environment* env, v8::Local<v8::Object> object);
167165
~ContextifyScript() override;
@@ -183,7 +181,7 @@ class ContextifyScript : public BaseObject {
183181
const v8::FunctionCallbackInfo<v8::Value>& args);
184182

185183
private:
186-
v8::Global<v8::UnboundScript> script_;
184+
v8::TracedReference<v8::UnboundScript> script_;
187185
};
188186

189187
v8::Maybe<bool> StoreCodeCacheResult(

0 commit comments

Comments
 (0)