Skip to content

Commit 4b657bd

Browse files
jasnelladuh95
authored andcommitted
src: use DictionaryTemplate for permission diag channel message
Since DiagnosticChannel permission messages always have the same shape and should be as low cost as possible, use a cached DictionaryTemplate for creating them Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #65158 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent cd670f4 commit 4b657bd

4 files changed

Lines changed: 41 additions & 35 deletions

File tree

src/env-inl.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -842,9 +842,8 @@ void Environment::set_process_exit_handler(
842842
#undef VP
843843

844844
#define V(Name, label, _, __) \
845-
inline v8::Local<v8::String> \
846-
IsolateData::Name##_permission_string() const { \
847-
return Name##_permission_string##_.Get(isolate_); \
845+
inline v8::Local<v8::String> IsolateData::Name##_permission_string() const { \
846+
return Name##_permission_string##_.Get(isolate_); \
848847
}
849848
PERMISSIONS(V)
850849
#undef V
@@ -879,9 +878,8 @@ void Environment::set_process_exit_handler(
879878
#undef VP
880879

881880
#define V(Name, label, _, __) \
882-
inline v8::Local<v8::String> \
883-
Environment::Name##_permission_string() const { \
884-
return isolate_data()->Name##_permission_string(); \
881+
inline v8::Local<v8::String> Environment::Name##_permission_string() const { \
882+
return isolate_data()->Name##_permission_string(); \
885883
}
886884
PERMISSIONS(V)
887885
#undef V

src/env.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,9 @@ void IsolateData::DeserializeProperties(const IsolateDataSerializeInfo* info) {
432432
info->primitive_values[i++]); \
433433
Local<String> field; \
434434
if (!maybe_field.ToLocal(&field)) { \
435-
fprintf(stderr, \
436-
"Failed to deserialize " #Name "_permission_string\n"); \
435+
fprintf(stderr, "Failed to deserialize " #Name "_permission_string\n"); \
437436
} \
438-
Name##_permission_string##_.Set(isolate_, field); \
437+
Name##_permission_string##_.Set(isolate_, field); \
439438
} while (0);
440439
PERMISSIONS(V)
441440
#undef V

src/env_properties.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@
460460
V(naptr_record_template, v8::DictionaryTemplate) \
461461
V(object_stats_template, v8::DictionaryTemplate) \
462462
V(page_stats_template, v8::DictionaryTemplate) \
463+
V(permission_diagnostic_channel_message, v8::DictionaryTemplate) \
463464
V(pipe_constructor_template, v8::FunctionTemplate) \
464465
V(script_context_constructor_template, v8::FunctionTemplate) \
465466
V(secure_context_constructor_template, v8::FunctionTemplate) \

src/permission/permission.cc

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "node_external_reference.h"
99
#include "node_file.h"
1010

11+
#include "v8-template.h"
1112
#include "v8.h"
1213

1314
#include <memory>
@@ -17,11 +18,13 @@
1718
namespace node {
1819

1920
using v8::Context;
21+
using v8::DictionaryTemplate;
2022
using v8::FunctionCallbackInfo;
2123
using v8::IntegrityLevel;
2224
using v8::Local;
2325
using v8::MaybeLocal;
2426
using v8::Object;
27+
using v8::Undefined;
2528
using v8::Value;
2629

2730
namespace permission {
@@ -55,6 +58,20 @@ constexpr std::string_view GetDiagnosticsChannelName(PermissionScope scope) {
5558
}
5659
}
5760

61+
Local<DictionaryTemplate> GetPermissionDiagnosicsTemplate(Environment* env) {
62+
auto tmpl = env->permission_diagnostic_channel_message();
63+
if (tmpl.IsEmpty()) {
64+
static constexpr std::string_view names[] = {
65+
"permission",
66+
"resource",
67+
"drop",
68+
};
69+
tmpl = DictionaryTemplate::New(env->isolate(), names);
70+
env->set_permission_diagnostic_channel_message(tmpl);
71+
}
72+
return tmpl;
73+
}
74+
5875
// permission.drop('fs.read', '/tmp/')
5976
// permission.drop('child')
6077
static void Drop(const FunctionCallbackInfo<Value>& args) {
@@ -259,17 +276,14 @@ bool Permission::is_scope_granted(Environment* env,
259276
v8::Isolate* isolate = env->isolate();
260277
v8::HandleScope handle_scope(isolate);
261278
v8::Local<v8::Context> context = env->context();
262-
v8::Local<v8::Object> msg =
263-
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
264-
msg->Set(context,
265-
env->permission_string(),
266-
PermissionToString(env, permission))
267-
.Check();
268-
msg->Set(context,
269-
env->resource_string(),
270-
ToV8Value(context, res).ToLocalChecked())
271-
.Check();
272-
ch->Publish(env, msg);
279+
v8::MaybeLocal<v8::Value> values[] = {
280+
PermissionToString(env, permission),
281+
ToV8Value(context, res),
282+
Undefined(isolate),
283+
};
284+
ch->Publish(
285+
env,
286+
GetPermissionDiagnosicsTemplate(env)->NewInstance(context, values));
273287
publishing_ = false;
274288
}
275289
}
@@ -324,21 +338,15 @@ void Permission::Drop(Environment* env,
324338
v8::Isolate* isolate = env->isolate();
325339
v8::HandleScope handle_scope(isolate);
326340
v8::Local<v8::Context> context = env->context();
327-
v8::Local<v8::Object> msg =
328-
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
329-
msg->Set(context,
330-
env->permission_string(),
331-
PermissionToString(env, scope))
332-
.Check();
333-
msg->Set(context,
334-
env->resource_string(),
335-
ToV8Value(context, param).ToLocalChecked())
336-
.Check();
337-
msg->Set(context,
338-
FIXED_ONE_BYTE_STRING(isolate, "drop"),
339-
v8::Boolean::New(isolate, true))
340-
.Check();
341-
ch->Publish(env, msg);
341+
342+
v8::MaybeLocal<v8::Value> values[] = {
343+
PermissionToString(env, scope),
344+
ToV8Value(context, param),
345+
v8::True(isolate),
346+
};
347+
ch->Publish(
348+
env,
349+
GetPermissionDiagnosicsTemplate(env)->NewInstance(context, values));
342350
publishing_ = false;
343351
}
344352
}

0 commit comments

Comments
 (0)