Skip to content

Commit

Permalink
src: use per-realm GetBindingData() wherever applicable
Browse files Browse the repository at this point in the history
This reduce the number of embedder slot accesses and also removes
the assumption in a few binding methods that the current realm is
the principal realm of the current environment (which is not true
for shadow realms).

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>
  • Loading branch information
joyeecheung authored and nodejs-github-bot committed Aug 16, 2023
1 parent 3578bee commit 6391b3b
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 64 deletions.
12 changes: 6 additions & 6 deletions src/dataqueue/queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -876,12 +876,12 @@ class FdEntry final : public EntryImpl {
}
Realm* realm = entry->env()->principal_realm();
return std::make_shared<ReaderImpl>(
BaseObjectPtr<fs::FileHandle>(fs::FileHandle::New(
realm->GetBindingData<fs::BindingData>(realm->context()),
file,
Local<Object>(),
entry->start_,
entry->end_ - entry->start_)),
BaseObjectPtr<fs::FileHandle>(
fs::FileHandle::New(realm->GetBindingData<fs::BindingData>(),
file,
Local<Object>(),
entry->start_,
entry->end_ - entry->start_)),
entry);
}

Expand Down
7 changes: 4 additions & 3 deletions src/encoding_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,13 @@ void BindingData::Deserialize(Local<Context> context,
}

void BindingData::EncodeInto(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
CHECK_GE(args.Length(), 2);
CHECK(args[0]->IsString());
CHECK(args[1]->IsUint8Array());
BindingData* binding_data = Realm::GetBindingData<BindingData>(args);

Realm* realm = Realm::GetCurrent(args);
Isolate* isolate = realm->isolate();
BindingData* binding_data = realm->GetBindingData<BindingData>();

Local<String> source = args[0].As<String>();

Expand Down
52 changes: 25 additions & 27 deletions src/node_blob.cc
Original file line number Diff line number Diff line change
Expand Up @@ -400,20 +400,22 @@ std::unique_ptr<worker::TransferData> Blob::CloneForMessaging() const {
}

void Blob::StoreDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
Environment* env = Environment::GetCurrent(args);
BlobBindingData* binding_data = Realm::GetBindingData<BlobBindingData>(args);
Realm* realm = Realm::GetCurrent(args);

CHECK(args[0]->IsString()); // ID key
CHECK(Blob::HasInstance(env, args[1])); // Blob
CHECK(Blob::HasInstance(realm->env(), args[1])); // Blob
CHECK(args[2]->IsUint32()); // Length
CHECK(args[3]->IsString()); // Type

Utf8Value key(env->isolate(), args[0]);
BlobBindingData* binding_data = realm->GetBindingData<BlobBindingData>();
Isolate* isolate = realm->isolate();

Utf8Value key(isolate, args[0]);
Blob* blob;
ASSIGN_OR_RETURN_UNWRAP(&blob, args[1]);

size_t length = args[2].As<Uint32>()->Value();
Utf8Value type(env->isolate(), args[3]);
Utf8Value type(isolate, args[3]);

binding_data->store_data_object(
std::string(*key, key.length()),
Expand All @@ -427,9 +429,11 @@ void Blob::StoreDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
void Blob::RevokeObjectURL(const FunctionCallbackInfo<Value>& args) {
CHECK_GE(args.Length(), 1);
CHECK(args[0]->IsString());
BlobBindingData* binding_data = Realm::GetBindingData<BlobBindingData>(args);
Environment* env = Environment::GetCurrent(args);
Utf8Value input(env->isolate(), args[0].As<String>());
Realm* realm = Realm::GetCurrent(args);
BlobBindingData* binding_data = realm->GetBindingData<BlobBindingData>();
Isolate* isolate = realm->isolate();

Utf8Value input(isolate, args[0].As<String>());
auto out = ada::parse<ada::url_aggregator>(input.ToStringView());

if (!out) {
Expand All @@ -449,36 +453,30 @@ void Blob::RevokeObjectURL(const FunctionCallbackInfo<Value>& args) {
}

void Blob::GetDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
BlobBindingData* binding_data = Realm::GetBindingData<BlobBindingData>(args);

Environment* env = Environment::GetCurrent(args);
CHECK(args[0]->IsString());
Realm* realm = Realm::GetCurrent(args);
BlobBindingData* binding_data = realm->GetBindingData<BlobBindingData>();
Isolate* isolate = realm->isolate();

Utf8Value key(env->isolate(), args[0]);
Utf8Value key(isolate, args[0]);

BlobBindingData::StoredDataObject stored =
binding_data->get_data_object(std::string(*key, key.length()));
if (stored.blob) {
Local<Value> type;
if (!String::NewFromUtf8(
env->isolate(),
stored.type.c_str(),
v8::NewStringType::kNormal,
static_cast<int>(stored.type.length())).ToLocal(&type)) {
if (!String::NewFromUtf8(isolate,
stored.type.c_str(),
v8::NewStringType::kNormal,
static_cast<int>(stored.type.length()))
.ToLocal(&type)) {
return;
}

Local<Value> values[] = {
stored.blob->object(),
Uint32::NewFromUnsigned(env->isolate(), stored.length),
type
};
Local<Value> values[] = {stored.blob->object(),
Uint32::NewFromUnsigned(isolate, stored.length),
type};

args.GetReturnValue().Set(
Array::New(
env->isolate(),
values,
arraysize(values)));
args.GetReturnValue().Set(Array::New(isolate, values, arraysize(values)));
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/node_file-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,10 @@ FSReqBase* GetReqWrap(const v8::FunctionCallbackInfo<v8::Value>& args,
return Unwrap<FSReqBase>(value.As<v8::Object>());
}

BindingData* binding_data = Realm::GetBindingData<BindingData>(args);
Environment* env = binding_data->env();
if (value->StrictEquals(env->fs_use_promises_symbol())) {
Realm* realm = Realm::GetCurrent(args);
BindingData* binding_data = realm->GetBindingData<BindingData>();

if (value->StrictEquals(realm->isolate_data()->fs_use_promises_symbol())) {
if (use_bigint) {
return FSReqPromise<AliasedBigInt64Array>::New(binding_data, use_bigint);
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2070,14 +2070,14 @@ static void Open(const FunctionCallbackInfo<Value>& args) {
}

static void OpenFileHandle(const FunctionCallbackInfo<Value>& args) {
BindingData* binding_data = Realm::GetBindingData<BindingData>(args);
Environment* env = binding_data->env();
Isolate* isolate = env->isolate();
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(isolate, args[0]);
BufferValue path(realm->isolate(), args[0]);
CHECK_NOT_NULL(*path);

CHECK(args[1]->IsInt32());
Expand Down
10 changes: 5 additions & 5 deletions src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2650,12 +2650,12 @@ void Http2Session::RefreshState(const FunctionCallbackInfo<Value>& args) {

// Constructor for new Http2Session instances.
void Http2Session::New(const FunctionCallbackInfo<Value>& args) {
Http2State* state = Realm::GetBindingData<Http2State>(args);
Environment* env = state->env();
Realm* realm = Realm::GetCurrent(args);
Http2State* state = realm->GetBindingData<Http2State>();

CHECK(args.IsConstructCall());
SessionType type =
static_cast<SessionType>(
args[0]->Int32Value(env->context()).ToChecked());
SessionType type = static_cast<SessionType>(
args[0]->Int32Value(realm->context()).ToChecked());
Http2Session* session = new Http2Session(state, args.This(), type);
Debug(session, "session created");
}
Expand Down
2 changes: 2 additions & 0 deletions src/node_realm.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class Realm : public MemoryRetainer {
const v8::FunctionCallbackInfo<v8::Value>& info);
template <typename T>
static inline T* GetBindingData(v8::Local<v8::Context> context);
template <typename T>
inline T* GetBindingData();
inline BindingDataStore* binding_data_store();

// The BaseObject count is a debugging helper that makes sure that there are
Expand Down
27 changes: 12 additions & 15 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,16 @@ void BindingData::Parse(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsString()); // input
// args[1] // base url

BindingData* binding_data = Realm::GetBindingData<BindingData>(args);
Environment* env = Environment::GetCurrent(args);
HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context());
Realm* realm = Realm::GetCurrent(args);
BindingData* binding_data = realm->GetBindingData<BindingData>();
Isolate* isolate = realm->isolate();

Utf8Value input(env->isolate(), args[0]);
Utf8Value input(isolate, args[0]);
ada::result<ada::url_aggregator> base;
ada::url_aggregator* base_pointer = nullptr;
if (args[1]->IsString()) {
base = ada::parse<ada::url_aggregator>(
Utf8Value(env->isolate(), args[1]).ToString());
base =
ada::parse<ada::url_aggregator>(Utf8Value(isolate, args[1]).ToString());
if (!base) {
return args.GetReturnValue().Set(false);
}
Expand All @@ -255,21 +254,20 @@ void BindingData::Parse(const FunctionCallbackInfo<Value>& args) {
binding_data->UpdateComponents(out->get_components(), out->type);

args.GetReturnValue().Set(
ToV8Value(env->context(), out->get_href(), env->isolate())
.ToLocalChecked());
ToV8Value(realm->context(), out->get_href(), isolate).ToLocalChecked());
}

void BindingData::Update(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsString()); // href
CHECK(args[1]->IsNumber()); // action type
CHECK(args[2]->IsString()); // new value

BindingData* binding_data = Realm::GetBindingData<BindingData>(args);
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
Realm* realm = Realm::GetCurrent(args);
BindingData* binding_data = realm->GetBindingData<BindingData>();
Isolate* isolate = realm->isolate();

enum url_update_action action = static_cast<enum url_update_action>(
args[1]->Uint32Value(env->context()).FromJust());
args[1]->Uint32Value(realm->context()).FromJust());
Utf8Value input(isolate, args[0].As<String>());
Utf8Value new_value(isolate, args[2].As<String>());

Expand Down Expand Up @@ -330,8 +328,7 @@ void BindingData::Update(const FunctionCallbackInfo<Value>& args) {

binding_data->UpdateComponents(out->get_components(), out->type);
args.GetReturnValue().Set(
ToV8Value(env->context(), out->get_href(), env->isolate())
.ToLocalChecked());
ToV8Value(realm->context(), out->get_href(), isolate).ToLocalChecked());
}

void BindingData::UpdateComponents(const ada::url_components& components,
Expand Down
2 changes: 1 addition & 1 deletion src/quic/bindingdata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ using v8::Value;
namespace quic {

BindingData& BindingData::Get(Environment* env) {
return *Realm::GetBindingData<BindingData>(env->context());
return *(env->principal_realm()->GetBindingData<BindingData>());
}

BindingData::operator ngtcp2_mem() {
Expand Down

0 comments on commit 6391b3b

Please sign in to comment.