mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
src: cache permission strings
Use env_property strings for permissions since those are fixed. Avoid creating new string instances each time. Also use ToV8Value for a couple since we're in here. Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: https://github.com/nodejs/node/pull/65158 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
This commit is contained in:
1 parent
075d748655
commit
79720e98c1
5 files changed
+81
-23
No files matched your search
@@ -841,6 +841,14 @@ void Environment::set_process_exit_handler(
|
||||
#undef VY
|
||||
#undef VP
|
||||
|
||||
#define V(Name, label, _, __) \
|
||||
inline v8::Local<v8::String> \
|
||||
IsolateData::Name##_permission_string() const { \
|
||||
return Name##_permission_string##_.Get(isolate_); \
|
||||
}
|
||||
PERMISSIONS(V)
|
||||
#undef V
|
||||
|
||||
#define VM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate)
|
||||
#define V(PropertyName, TypeName) \
|
||||
inline v8::Local<TypeName> IsolateData::PropertyName() const { \
|
||||
@@ -870,6 +878,14 @@ void Environment::set_process_exit_handler(
|
||||
#undef VY
|
||||
#undef VP
|
||||
|
||||
#define V(Name, label, _, __) \
|
||||
inline v8::Local<v8::String> \
|
||||
Environment::Name##_permission_string() const { \
|
||||
return isolate_data()->Name##_permission_string(); \
|
||||
}
|
||||
PERMISSIONS(V)
|
||||
#undef V
|
||||
|
||||
#define V(PropertyName, TypeName) \
|
||||
inline v8::Local<TypeName> Environment::PropertyName() const { \
|
||||
return isolate_data()->PropertyName(); \
|
||||
|
||||
+37
@@ -360,6 +360,12 @@ IsolateDataSerializeInfo IsolateData::Serialize(SnapshotCreator* creator) {
|
||||
#undef VS
|
||||
#undef VP
|
||||
|
||||
#define V(Name, label, _, __) \
|
||||
info.primitive_values.push_back( \
|
||||
creator->AddData(Name##_permission_string##_.Get(isolate)));
|
||||
PERMISSIONS(V)
|
||||
#undef V
|
||||
|
||||
info.primitive_values.reserve(info.primitive_values.size() +
|
||||
AsyncWrap::PROVIDERS_LENGTH);
|
||||
for (size_t i = 0; i < AsyncWrap::PROVIDERS_LENGTH; i++) {
|
||||
@@ -419,6 +425,21 @@ void IsolateData::DeserializeProperties(const IsolateDataSerializeInfo* info) {
|
||||
#undef VS
|
||||
#undef VP
|
||||
|
||||
#define V(Name, label, _, __) \
|
||||
do { \
|
||||
MaybeLocal<String> maybe_field = \
|
||||
isolate_->GetDataFromSnapshotOnce<String>( \
|
||||
info->primitive_values[i++]); \
|
||||
Local<String> field; \
|
||||
if (!maybe_field.ToLocal(&field)) { \
|
||||
fprintf(stderr, \
|
||||
"Failed to deserialize " #Name "_permission_string\n"); \
|
||||
} \
|
||||
Name##_permission_string##_.Set(isolate_, field); \
|
||||
} while (0);
|
||||
PERMISSIONS(V)
|
||||
#undef V
|
||||
|
||||
for (size_t j = 0; j < AsyncWrap::PROVIDERS_LENGTH; j++) {
|
||||
MaybeLocal<String> maybe_field =
|
||||
isolate_->GetDataFromSnapshotOnce<String>(info->primitive_values[i++]);
|
||||
@@ -520,6 +541,17 @@ void IsolateData::CreateProperties() {
|
||||
PER_ISOLATE_STRING_PROPERTIES(V)
|
||||
#undef V
|
||||
|
||||
#define V(Name, label, _, __) \
|
||||
Name##_permission_string##_.Set( \
|
||||
isolate_, \
|
||||
String::NewFromOneByte(isolate_, \
|
||||
reinterpret_cast<const uint8_t*>(#Name), \
|
||||
NewStringType::kInternalized, \
|
||||
sizeof(#Name) - 1) \
|
||||
.ToLocalChecked());
|
||||
PERMISSIONS(V)
|
||||
#undef V
|
||||
|
||||
// Create all the provider strings that will be passed to JS. Place them in
|
||||
// an array so the array index matches the PROVIDER id offset. This way the
|
||||
// strings can be retrieved quickly.
|
||||
@@ -630,6 +662,11 @@ void IsolateData::MemoryInfo(MemoryTracker* tracker) const {
|
||||
PER_ISOLATE_STRING_PROPERTIES(V)
|
||||
#undef V
|
||||
|
||||
#define V(Name, label, _, __) \
|
||||
tracker->TrackField(#Name "_permission_string", Name##_permission_string());
|
||||
PERMISSIONS(V)
|
||||
#undef V
|
||||
|
||||
tracker->TrackField("async_wrap_providers", async_wrap_providers_);
|
||||
|
||||
if (node_allocator_ != nullptr) {
|
||||
|
||||
@@ -189,6 +189,11 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
|
||||
#undef VS
|
||||
#undef VP
|
||||
|
||||
#define V(Name, label, _, __) \
|
||||
inline v8::Local<v8::String> Name##_permission_string() const;
|
||||
PERMISSIONS(V)
|
||||
#undef V
|
||||
|
||||
#define VM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate)
|
||||
#define V(PropertyName, TypeName) \
|
||||
inline v8::Local<TypeName> PropertyName() const; \
|
||||
@@ -234,6 +239,12 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
|
||||
#undef VS
|
||||
#undef VY
|
||||
#undef VP
|
||||
|
||||
#define V(Name, label, _, __) \
|
||||
v8::Eternal<v8::String> Name##_permission_string##_;
|
||||
PERMISSIONS(V)
|
||||
#undef V
|
||||
|
||||
// Keep a list of all Persistent strings used for AsyncWrap Provider types.
|
||||
std::array<v8::Eternal<v8::String>, AsyncWrap::PROVIDERS_LENGTH>
|
||||
async_wrap_providers_;
|
||||
@@ -875,6 +886,11 @@ class Environment final : public MemoryRetainer {
|
||||
#undef VY
|
||||
#undef VP
|
||||
|
||||
#define V(Name, label, _, __) \
|
||||
inline v8::Local<v8::String> Name##_permission_string() const;
|
||||
PERMISSIONS(V)
|
||||
#undef V
|
||||
|
||||
#define V(PropertyName, TypeName) \
|
||||
inline v8::Local<TypeName> PropertyName() const; \
|
||||
inline void set_ ## PropertyName(v8::Local<TypeName> value);
|
||||
|
||||
@@ -106,10 +106,11 @@ static void Has(const FunctionCallbackInfo<Value>& args) {
|
||||
} // namespace
|
||||
|
||||
#define V(Name, label, _, __) \
|
||||
if (perm == PermissionScope::k##Name) return #Name;
|
||||
const char* Permission::PermissionToString(const PermissionScope perm) {
|
||||
if (perm == PermissionScope::k##Name) return env->Name##_permission_string();
|
||||
v8::Local<v8::String> Permission::PermissionToString(
|
||||
Environment* env, const PermissionScope perm) {
|
||||
PERMISSIONS(V)
|
||||
return nullptr;
|
||||
UNREACHABLE();
|
||||
}
|
||||
#undef V
|
||||
|
||||
@@ -192,12 +193,9 @@ MaybeLocal<Value> CreateAccessDeniedError(Environment* env,
|
||||
Local<Object> err = ERR_ACCESS_DENIED(
|
||||
env->isolate(), "Access to this API has been restricted. %s", suggestion);
|
||||
|
||||
Local<Value> perm_string;
|
||||
Local<Value> resource_string;
|
||||
std::string_view perm_str = Permission::PermissionToString(perm);
|
||||
if (!ToV8Value(env->context(), perm_str, env->isolate())
|
||||
.ToLocal(&perm_string) ||
|
||||
!ToV8Value(env->context(), res, env->isolate())
|
||||
Local<Value> perm_string = Permission::PermissionToString(env, perm);
|
||||
if (!ToV8Value(env->context(), res, env->isolate())
|
||||
.ToLocal(&resource_string) ||
|
||||
err->Set(env->context(), env->permission_string(), perm_string)
|
||||
.IsNothing() ||
|
||||
@@ -263,18 +261,13 @@ bool Permission::is_scope_granted(Environment* env,
|
||||
v8::Local<v8::Context> context = env->context();
|
||||
v8::Local<v8::Object> msg =
|
||||
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
|
||||
const char* perm_str = PermissionToString(permission);
|
||||
msg->Set(context,
|
||||
env->permission_string(),
|
||||
v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked())
|
||||
PermissionToString(env, permission))
|
||||
.Check();
|
||||
msg->Set(context,
|
||||
env->resource_string(),
|
||||
v8::String::NewFromUtf8(isolate,
|
||||
res.data(),
|
||||
v8::NewStringType::kNormal,
|
||||
static_cast<int>(res.size()))
|
||||
.ToLocalChecked())
|
||||
ToV8Value(context, res).ToLocalChecked())
|
||||
.Check();
|
||||
ch->Publish(env, msg);
|
||||
publishing_ = false;
|
||||
@@ -333,18 +326,13 @@ void Permission::Drop(Environment* env,
|
||||
v8::Local<v8::Context> context = env->context();
|
||||
v8::Local<v8::Object> msg =
|
||||
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
|
||||
const char* perm_str = PermissionToString(scope);
|
||||
msg->Set(context,
|
||||
env->permission_string(),
|
||||
v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked())
|
||||
PermissionToString(env, scope))
|
||||
.Check();
|
||||
msg->Set(context,
|
||||
env->resource_string(),
|
||||
v8::String::NewFromUtf8(isolate,
|
||||
param.data(),
|
||||
v8::NewStringType::kNormal,
|
||||
static_cast<int>(param.size()))
|
||||
.ToLocalChecked())
|
||||
ToV8Value(context, param).ToLocalChecked())
|
||||
.Check();
|
||||
msg->Set(context,
|
||||
FIXED_ONE_BYTE_STRING(isolate, "drop"),
|
||||
|
||||
@@ -113,7 +113,8 @@ class Permission {
|
||||
FORCE_INLINE bool warning_only() const { return warning_only_; }
|
||||
|
||||
static PermissionScope StringToPermission(const std::string& perm);
|
||||
static const char* PermissionToString(PermissionScope perm);
|
||||
static v8::Local<v8::String> PermissionToString(Environment* env,
|
||||
PermissionScope perm);
|
||||
static void ThrowAccessDenied(Environment* env,
|
||||
PermissionScope perm,
|
||||
const std::string_view& res);
|
||||
|
||||
Reference in new issue
Block a user