Skip to content

Commit

Permalink
src: add per-realm GetBindingData() method
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Rafael Gonzaga <[email protected]>
Reviewed-By: Stephen Belanger <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
  • Loading branch information
joyeecheung authored and UlisesGascon committed Sep 10, 2023
1 parent 08197aa commit 184bbdd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
34 changes: 19 additions & 15 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -265,17 +265,17 @@ FileHandle* FileHandle::New(BindingData* binding_data,
}

void FileHandle::New(const FunctionCallbackInfo<Value>& args) {
BindingData* binding_data = Realm::GetBindingData<BindingData>(args);
Environment* env = binding_data->env();
CHECK(args.IsConstructCall());
CHECK(args[0]->IsInt32());
Realm* realm = Realm::GetCurrent(args);
BindingData* binding_data = realm->GetBindingData<BindingData>();

std::optional<int64_t> maybeOffset = std::nullopt;
std::optional<int64_t> maybeLength = std::nullopt;
if (args[1]->IsNumber())
maybeOffset = args[1]->IntegerValue(env->context()).FromJust();
maybeOffset = args[1]->IntegerValue(realm->context()).FromJust();
if (args[2]->IsNumber())
maybeLength = args[2]->IntegerValue(env->context()).FromJust();
maybeLength = args[2]->IntegerValue(realm->context()).FromJust();

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

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

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

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

static void LStat(const FunctionCallbackInfo<Value>& args) {
BindingData* binding_data = Realm::GetBindingData<BindingData>(args);
Environment* env = binding_data->env();
Realm* realm = Realm::GetCurrent(args);
BindingData* binding_data = realm->GetBindingData<BindingData>();
Environment* env = realm->env();

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

BufferValue path(env->isolate(), args[0]);
BufferValue path(realm->isolate(), args[0]);
CHECK_NOT_NULL(*path);

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

static void FStat(const FunctionCallbackInfo<Value>& args) {
BindingData* binding_data = Realm::GetBindingData<BindingData>(args);
Environment* env = binding_data->env();
Realm* realm = Realm::GetCurrent(args);
BindingData* binding_data = realm->GetBindingData<BindingData>();
Environment* env = realm->env();

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

static void StatFs(const FunctionCallbackInfo<Value>& args) {
BindingData* binding_data = Realm::GetBindingData<BindingData>(args);
Environment* env = binding_data->env();
Realm* realm = Realm::GetCurrent(args);
BindingData* binding_data = realm->GetBindingData<BindingData>();
Environment* env = realm->env();

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

BufferValue path(env->isolate(), args[0]);
BufferValue path(realm->isolate(), args[0]);
CHECK_NOT_NULL(*path);
THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kFileSystemRead, path.ToStringView());
Expand Down
11 changes: 6 additions & 5 deletions src/node_realm-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,17 @@ inline T* Realm::GetBindingData(
template <typename T>
inline T* Realm::GetBindingData(v8::Local<v8::Context> context) {
Realm* realm = GetCurrent(context);
DCHECK_NOT_NULL(realm);
BindingDataStore* map = realm->binding_data_store();
DCHECK_NOT_NULL(map);
return realm->GetBindingData<T>();
}

template <typename T>
inline T* Realm::GetBindingData() {
constexpr size_t binding_index = static_cast<size_t>(T::binding_type_int);
static_assert(binding_index < std::tuple_size_v<BindingDataStore>);
auto ptr = (*map)[binding_index];
auto ptr = binding_data_store_[binding_index];
if (UNLIKELY(!ptr)) return nullptr;
T* result = static_cast<T*>(ptr.get());
DCHECK_NOT_NULL(result);
DCHECK_EQ(result->realm(), GetCurrent(context));
return result;
}

Expand Down

0 comments on commit 184bbdd

Please sign in to comment.