Skip to content

Commit 184bbdd

Browse files
joyeecheungUlisesGascon
authored andcommitted
src: add per-realm GetBindingData() method
This version avoids the additional access to the embedder slot when we already have a reference to the realm. PR-URL: #49007 Refs: #48836 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent 08197aa commit 184bbdd

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

src/node_file.cc

+19-15
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,17 @@ FileHandle* FileHandle::New(BindingData* binding_data,
265265
}
266266

267267
void FileHandle::New(const FunctionCallbackInfo<Value>& args) {
268-
BindingData* binding_data = Realm::GetBindingData<BindingData>(args);
269-
Environment* env = binding_data->env();
270268
CHECK(args.IsConstructCall());
271269
CHECK(args[0]->IsInt32());
270+
Realm* realm = Realm::GetCurrent(args);
271+
BindingData* binding_data = realm->GetBindingData<BindingData>();
272272

273273
std::optional<int64_t> maybeOffset = std::nullopt;
274274
std::optional<int64_t> maybeLength = std::nullopt;
275275
if (args[1]->IsNumber())
276-
maybeOffset = args[1]->IntegerValue(env->context()).FromJust();
276+
maybeOffset = args[1]->IntegerValue(realm->context()).FromJust();
277277
if (args[2]->IsNumber())
278-
maybeLength = args[2]->IntegerValue(env->context()).FromJust();
278+
maybeLength = args[2]->IntegerValue(realm->context()).FromJust();
279279

280280
FileHandle::New(binding_data,
281281
args[0].As<Int32>()->Value(),
@@ -1143,13 +1143,14 @@ static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {
11431143
}
11441144

11451145
static void Stat(const FunctionCallbackInfo<Value>& args) {
1146-
BindingData* binding_data = Realm::GetBindingData<BindingData>(args);
1147-
Environment* env = binding_data->env();
1146+
Realm* realm = Realm::GetCurrent(args);
1147+
BindingData* binding_data = realm->GetBindingData<BindingData>();
1148+
Environment* env = realm->env();
11481149

11491150
const int argc = args.Length();
11501151
CHECK_GE(argc, 2);
11511152

1152-
BufferValue path(env->isolate(), args[0]);
1153+
BufferValue path(realm->isolate(), args[0]);
11531154
CHECK_NOT_NULL(*path);
11541155
THROW_IF_INSUFFICIENT_PERMISSIONS(
11551156
env, permission::PermissionScope::kFileSystemRead, path.ToStringView());
@@ -1178,13 +1179,14 @@ static void Stat(const FunctionCallbackInfo<Value>& args) {
11781179
}
11791180

11801181
static void LStat(const FunctionCallbackInfo<Value>& args) {
1181-
BindingData* binding_data = Realm::GetBindingData<BindingData>(args);
1182-
Environment* env = binding_data->env();
1182+
Realm* realm = Realm::GetCurrent(args);
1183+
BindingData* binding_data = realm->GetBindingData<BindingData>();
1184+
Environment* env = realm->env();
11831185

11841186
const int argc = args.Length();
11851187
CHECK_GE(argc, 3);
11861188

1187-
BufferValue path(env->isolate(), args[0]);
1189+
BufferValue path(realm->isolate(), args[0]);
11881190
CHECK_NOT_NULL(*path);
11891191

11901192
bool use_bigint = args[1]->IsTrue();
@@ -1212,8 +1214,9 @@ static void LStat(const FunctionCallbackInfo<Value>& args) {
12121214
}
12131215

12141216
static void FStat(const FunctionCallbackInfo<Value>& args) {
1215-
BindingData* binding_data = Realm::GetBindingData<BindingData>(args);
1216-
Environment* env = binding_data->env();
1217+
Realm* realm = Realm::GetCurrent(args);
1218+
BindingData* binding_data = realm->GetBindingData<BindingData>();
1219+
Environment* env = realm->env();
12171220

12181221
const int argc = args.Length();
12191222
CHECK_GE(argc, 2);
@@ -1244,13 +1247,14 @@ static void FStat(const FunctionCallbackInfo<Value>& args) {
12441247
}
12451248

12461249
static void StatFs(const FunctionCallbackInfo<Value>& args) {
1247-
BindingData* binding_data = Realm::GetBindingData<BindingData>(args);
1248-
Environment* env = binding_data->env();
1250+
Realm* realm = Realm::GetCurrent(args);
1251+
BindingData* binding_data = realm->GetBindingData<BindingData>();
1252+
Environment* env = realm->env();
12491253

12501254
const int argc = args.Length();
12511255
CHECK_GE(argc, 2);
12521256

1253-
BufferValue path(env->isolate(), args[0]);
1257+
BufferValue path(realm->isolate(), args[0]);
12541258
CHECK_NOT_NULL(*path);
12551259
THROW_IF_INSUFFICIENT_PERMISSIONS(
12561260
env, permission::PermissionScope::kFileSystemRead, path.ToStringView());

src/node_realm-inl.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,17 @@ inline T* Realm::GetBindingData(
6767
template <typename T>
6868
inline T* Realm::GetBindingData(v8::Local<v8::Context> context) {
6969
Realm* realm = GetCurrent(context);
70-
DCHECK_NOT_NULL(realm);
71-
BindingDataStore* map = realm->binding_data_store();
72-
DCHECK_NOT_NULL(map);
70+
return realm->GetBindingData<T>();
71+
}
72+
73+
template <typename T>
74+
inline T* Realm::GetBindingData() {
7375
constexpr size_t binding_index = static_cast<size_t>(T::binding_type_int);
7476
static_assert(binding_index < std::tuple_size_v<BindingDataStore>);
75-
auto ptr = (*map)[binding_index];
77+
auto ptr = binding_data_store_[binding_index];
7678
if (UNLIKELY(!ptr)) return nullptr;
7779
T* result = static_cast<T*>(ptr.get());
7880
DCHECK_NOT_NULL(result);
79-
DCHECK_EQ(result->realm(), GetCurrent(context));
8081
return result;
8182
}
8283

0 commit comments

Comments
 (0)