Commit f77aa2f

Browse files
jasnelladuh95
authored andcommitted
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: #65158 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent be4e7de commit f77aa2f

5 files changed

Lines changed: 81 additions & 23 deletions

File tree

‎src/env-inl.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,14 @@ void Environment::set_process_exit_handler(
841841
#undef VY
842842
#undef VP
843843

844+
#defineV(Name, label, _, __) \
845+
inline v8::Local<v8::String> \
846+
IsolateData::Name##_permission_string() const { \
847+
return Name##_permission_string##_.Get(isolate_); \
848+
}
849+
PERMISSIONS(V)
850+
#undef V
851+
844852
#defineVM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate)
845853
#defineV(PropertyName, TypeName) \
846854
inline v8::Local<TypeName> IsolateData::PropertyName() const { \
@@ -870,6 +878,14 @@ void Environment::set_process_exit_handler(
870878
#undef VY
871879
#undef VP
872880

881+
#defineV(Name, label, _, __) \
882+
inline v8::Local<v8::String> \
883+
Environment::Name##_permission_string() const { \
884+
returnisolate_data()->Name##_permission_string(); \
885+
}
886+
PERMISSIONS(V)
887+
#undef V
888+
873889
#defineV(PropertyName, TypeName) \
874890
inline v8::Local<TypeName> Environment::PropertyName() const { \
875891
returnisolate_data()->PropertyName(); \

‎src/env.cc‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ IsolateDataSerializeInfo IsolateData::Serialize(SnapshotCreator* creator) {
360360
#undef VS
361361
#undef VP
362362

363+
#defineV(Name, label, _, __) \
364+
info.primitive_values.push_back( \
365+
creator->AddData(Name##_permission_string##_.Get(isolate)));
366+
PERMISSIONS(V)
367+
#undef V
368+
363369
info.primitive_values.reserve(info.primitive_values.size() +
364370
AsyncWrap::PROVIDERS_LENGTH);
365371
for (size_t i = 0; i < AsyncWrap::PROVIDERS_LENGTH; i++) {
@@ -419,6 +425,21 @@ void IsolateData::DeserializeProperties(const IsolateDataSerializeInfo* info) {
419425
#undef VS
420426
#undef VP
421427

428+
#defineV(Name, label, _, __) \
429+
do { \
430+
MaybeLocal<String> maybe_field = \
431+
isolate_->GetDataFromSnapshotOnce<String>( \
432+
info->primitive_values[i++]); \
433+
Local<String> field; \
434+
if (!maybe_field.ToLocal(&field)) { \
435+
fprintf(stderr, \
436+
"Failed to deserialize " #Name "_permission_string\n"); \
437+
} \
438+
Name##_permission_string##_.Set(isolate_, field); \
439+
} while (0);
440+
PERMISSIONS(V)
441+
#undef V
442+
422443
for (size_t j = 0; j < AsyncWrap::PROVIDERS_LENGTH; j++) {
423444
MaybeLocal<String> maybe_field =
424445
isolate_->GetDataFromSnapshotOnce<String>(info->primitive_values[i++]);
@@ -520,6 +541,17 @@ void IsolateData::CreateProperties() {
520541
PER_ISOLATE_STRING_PROPERTIES(V)
521542
#undef V
522543

544+
#defineV(Name, label, _, __) \
545+
Name##_permission_string##_.Set( \
546+
isolate_, \
547+
String::NewFromOneByte(isolate_, \
548+
reinterpret_cast<constuint8_t*>(#Name), \
549+
NewStringType::kInternalized, \
550+
sizeof(#Name) - 1) \
551+
.ToLocalChecked());
552+
PERMISSIONS(V)
553+
#undef V
554+
523555
// Create all the provider strings that will be passed to JS. Place them in
524556
// an array so the array index matches the PROVIDER id offset. This way the
525557
// strings can be retrieved quickly.
@@ -630,6 +662,11 @@ void IsolateData::MemoryInfo(MemoryTracker* tracker) const {
630662
PER_ISOLATE_STRING_PROPERTIES(V)
631663
#undef V
632664

665+
#defineV(Name, label, _, __) \
666+
tracker->TrackField(#Name "_permission_string", Name##_permission_string());
667+
PERMISSIONS(V)
668+
#undef V
669+
633670
tracker->TrackField("async_wrap_providers", async_wrap_providers_);
634671

635672
if (node_allocator_ != nullptr) {

‎src/env.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
189189
#undef VS
190190
#undef VP
191191

192+
#defineV(Name, label, _, __) \
193+
inline v8::Local<v8::String> Name##_permission_string() const;
194+
PERMISSIONS(V)
195+
#undef V
196+
192197
#defineVM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate)
193198
#defineV(PropertyName, TypeName) \
194199
inline v8::Local<TypeName> PropertyName() const; \
@@ -234,6 +239,12 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
234239
#undef VS
235240
#undef VY
236241
#undef VP
242+
243+
#defineV(Name, label, _, __) \
244+
v8::Eternal<v8::String> Name##_permission_string##_;
245+
PERMISSIONS(V)
246+
#undef V
247+
237248
// Keep a list of all Persistent strings used for AsyncWrap Provider types.
238249
std::array<v8::Eternal<v8::String>, AsyncWrap::PROVIDERS_LENGTH>
239250
async_wrap_providers_;
@@ -875,6 +886,11 @@ class Environment final : public MemoryRetainer {
875886
#undef VY
876887
#undef VP
877888

889+
#defineV(Name, label, _, __) \
890+
inline v8::Local<v8::String> Name##_permission_string() const;
891+
PERMISSIONS(V)
892+
#undef V
893+
878894
#defineV(PropertyName, TypeName) \
879895
inline v8::Local<TypeName> PropertyName() const; \
880896
inlinevoid set_ ## PropertyName(v8::Local<TypeName> value);

‎src/permission/permission.cc‎

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ static void Has(const FunctionCallbackInfo<Value>& args) {
106106
} // namespace
107107

108108
#defineV(Name, label, _, __) \
109-
if (perm == PermissionScope::k##Name) return #Name;
110-
constchar* Permission::PermissionToString(const PermissionScope perm) {
109+
if (perm == PermissionScope::k##Name) return env->Name##_permission_string();
110+
v8::Local<v8::String> Permission::PermissionToString(
111+
Environment* env, const PermissionScope perm) {
111112
PERMISSIONS(V)
112-
returnnullptr;
113+
UNREACHABLE();
113114
}
114115
#undef V
115116

@@ -192,12 +193,9 @@ MaybeLocal<Value> CreateAccessDeniedError(Environment* env,
192193
Local<Object> err = ERR_ACCESS_DENIED(
193194
env->isolate(), "Access to this API has been restricted. %s", suggestion);
194195

195-
Local<Value> perm_string;
196196
Local<Value> resource_string;
197-
std::string_view perm_str = Permission::PermissionToString(perm);
198-
if (!ToV8Value(env->context(), perm_str, env->isolate())
199-
.ToLocal(&perm_string) ||
200-
!ToV8Value(env->context(), res, env->isolate())
197+
Local<Value> perm_string = Permission::PermissionToString(env, perm);
198+
if (!ToV8Value(env->context(), res, env->isolate())
201199
.ToLocal(&resource_string) ||
202200
err->Set(env->context(), env->permission_string(), perm_string)
203201
.IsNothing() ||
@@ -263,18 +261,13 @@ bool Permission::is_scope_granted(Environment* env,
263261
v8::Local<v8::Context> context = env->context();
264262
v8::Local<v8::Object> msg =
265263
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
266-
constchar* perm_str = PermissionToString(permission);
267264
msg->Set(context,
268265
env->permission_string(),
269-
v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked())
266+
PermissionToString(env, permission))
270267
.Check();
271268
msg->Set(context,
272269
env->resource_string(),
273-
v8::String::NewFromUtf8(isolate,
274-
res.data(),
275-
v8::NewStringType::kNormal,
276-
static_cast<int>(res.size()))
277-
.ToLocalChecked())
270+
ToV8Value(context, res).ToLocalChecked())
278271
.Check();
279272
ch->Publish(env, msg);
280273
publishing_ = false;
@@ -333,18 +326,13 @@ void Permission::Drop(Environment* env,
333326
v8::Local<v8::Context> context = env->context();
334327
v8::Local<v8::Object> msg =
335328
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
336-
constchar* perm_str = PermissionToString(scope);
337329
msg->Set(context,
338330
env->permission_string(),
339-
v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked())
331+
PermissionToString(env, scope))
340332
.Check();
341333
msg->Set(context,
342334
env->resource_string(),
343-
v8::String::NewFromUtf8(isolate,
344-
param.data(),
345-
v8::NewStringType::kNormal,
346-
static_cast<int>(param.size()))
347-
.ToLocalChecked())
335+
ToV8Value(context, param).ToLocalChecked())
348336
.Check();
349337
msg->Set(context,
350338
FIXED_ONE_BYTE_STRING(isolate, "drop"),

‎src/permission/permission.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ class Permission {
113113
FORCE_INLINEboolwarning_only() const { return warning_only_; }
114114

115115
static PermissionScope StringToPermission(const std::string& perm);
116-
staticconstchar* PermissionToString(PermissionScope perm);
116+
static v8::Local<v8::String> PermissionToString(Environment* env,
117+
PermissionScope perm);
117118
staticvoidThrowAccessDenied(Environment* env,
118119
PermissionScope perm,
119120
const std::string_view& res);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit f77aa2f

Browse files
jasnelladuh95
authored andcommitted
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: #65158 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent be4e7de commit f77aa2f

5 files changed

Lines changed: 81 additions & 23 deletions

File tree

‎src/env-inl.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,14 @@ void Environment::set_process_exit_handler(
841841
#undef VY
842842
#undef VP
843843

844+
#defineV(Name, label, _, __) \
845+
inline v8::Local<v8::String> \
846+
IsolateData::Name##_permission_string() const { \
847+
return Name##_permission_string##_.Get(isolate_); \
848+
}
849+
PERMISSIONS(V)
850+
#undef V
851+
844852
#defineVM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate)
845853
#defineV(PropertyName, TypeName) \
846854
inline v8::Local<TypeName> IsolateData::PropertyName() const { \
@@ -870,6 +878,14 @@ void Environment::set_process_exit_handler(
870878
#undef VY
871879
#undef VP
872880

881+
#defineV(Name, label, _, __) \
882+
inline v8::Local<v8::String> \
883+
Environment::Name##_permission_string() const { \
884+
returnisolate_data()->Name##_permission_string(); \
885+
}
886+
PERMISSIONS(V)
887+
#undef V
888+
873889
#defineV(PropertyName, TypeName) \
874890
inline v8::Local<TypeName> Environment::PropertyName() const { \
875891
returnisolate_data()->PropertyName(); \

‎src/env.cc‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ IsolateDataSerializeInfo IsolateData::Serialize(SnapshotCreator* creator) {
360360
#undef VS
361361
#undef VP
362362

363+
#defineV(Name, label, _, __) \
364+
info.primitive_values.push_back( \
365+
creator->AddData(Name##_permission_string##_.Get(isolate)));
366+
PERMISSIONS(V)
367+
#undef V
368+
363369
info.primitive_values.reserve(info.primitive_values.size() +
364370
AsyncWrap::PROVIDERS_LENGTH);
365371
for (size_t i = 0; i < AsyncWrap::PROVIDERS_LENGTH; i++) {
@@ -419,6 +425,21 @@ void IsolateData::DeserializeProperties(const IsolateDataSerializeInfo* info) {
419425
#undef VS
420426
#undef VP
421427

428+
#defineV(Name, label, _, __) \
429+
do { \
430+
MaybeLocal<String> maybe_field = \
431+
isolate_->GetDataFromSnapshotOnce<String>( \
432+
info->primitive_values[i++]); \
433+
Local<String> field; \
434+
if (!maybe_field.ToLocal(&field)) { \
435+
fprintf(stderr, \
436+
"Failed to deserialize " #Name "_permission_string\n"); \
437+
} \
438+
Name##_permission_string##_.Set(isolate_, field); \
439+
} while (0);
440+
PERMISSIONS(V)
441+
#undef V
442+
422443
for (size_t j = 0; j < AsyncWrap::PROVIDERS_LENGTH; j++) {
423444
MaybeLocal<String> maybe_field =
424445
isolate_->GetDataFromSnapshotOnce<String>(info->primitive_values[i++]);
@@ -520,6 +541,17 @@ void IsolateData::CreateProperties() {
520541
PER_ISOLATE_STRING_PROPERTIES(V)
521542
#undef V
522543

544+
#defineV(Name, label, _, __) \
545+
Name##_permission_string##_.Set( \
546+
isolate_, \
547+
String::NewFromOneByte(isolate_, \
548+
reinterpret_cast<constuint8_t*>(#Name), \
549+
NewStringType::kInternalized, \
550+
sizeof(#Name) - 1) \
551+
.ToLocalChecked());
552+
PERMISSIONS(V)
553+
#undef V
554+
523555
// Create all the provider strings that will be passed to JS. Place them in
524556
// an array so the array index matches the PROVIDER id offset. This way the
525557
// strings can be retrieved quickly.
@@ -630,6 +662,11 @@ void IsolateData::MemoryInfo(MemoryTracker* tracker) const {
630662
PER_ISOLATE_STRING_PROPERTIES(V)
631663
#undef V
632664

665+
#defineV(Name, label, _, __) \
666+
tracker->TrackField(#Name "_permission_string", Name##_permission_string());
667+
PERMISSIONS(V)
668+
#undef V
669+
633670
tracker->TrackField("async_wrap_providers", async_wrap_providers_);
634671

635672
if (node_allocator_ != nullptr) {

‎src/env.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
189189
#undef VS
190190
#undef VP
191191

192+
#defineV(Name, label, _, __) \
193+
inline v8::Local<v8::String> Name##_permission_string() const;
194+
PERMISSIONS(V)
195+
#undef V
196+
192197
#defineVM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate)
193198
#defineV(PropertyName, TypeName) \
194199
inline v8::Local<TypeName> PropertyName() const; \
@@ -234,6 +239,12 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
234239
#undef VS
235240
#undef VY
236241
#undef VP
242+
243+
#defineV(Name, label, _, __) \
244+
v8::Eternal<v8::String> Name##_permission_string##_;
245+
PERMISSIONS(V)
246+
#undef V
247+
237248
// Keep a list of all Persistent strings used for AsyncWrap Provider types.
238249
std::array<v8::Eternal<v8::String>, AsyncWrap::PROVIDERS_LENGTH>
239250
async_wrap_providers_;
@@ -875,6 +886,11 @@ class Environment final : public MemoryRetainer {
875886
#undef VY
876887
#undef VP
877888

889+
#defineV(Name, label, _, __) \
890+
inline v8::Local<v8::String> Name##_permission_string() const;
891+
PERMISSIONS(V)
892+
#undef V
893+
878894
#defineV(PropertyName, TypeName) \
879895
inline v8::Local<TypeName> PropertyName() const; \
880896
inlinevoid set_ ## PropertyName(v8::Local<TypeName> value);

‎src/permission/permission.cc‎

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ static void Has(const FunctionCallbackInfo<Value>& args) {
106106
} // namespace
107107

108108
#defineV(Name, label, _, __) \
109-
if (perm == PermissionScope::k##Name) return #Name;
110-
constchar* Permission::PermissionToString(const PermissionScope perm) {
109+
if (perm == PermissionScope::k##Name) return env->Name##_permission_string();
110+
v8::Local<v8::String> Permission::PermissionToString(
111+
Environment* env, const PermissionScope perm) {
111112
PERMISSIONS(V)
112-
returnnullptr;
113+
UNREACHABLE();
113114
}
114115
#undef V
115116

@@ -192,12 +193,9 @@ MaybeLocal<Value> CreateAccessDeniedError(Environment* env,
192193
Local<Object> err = ERR_ACCESS_DENIED(
193194
env->isolate(), "Access to this API has been restricted. %s", suggestion);
194195

195-
Local<Value> perm_string;
196196
Local<Value> resource_string;
197-
std::string_view perm_str = Permission::PermissionToString(perm);
198-
if (!ToV8Value(env->context(), perm_str, env->isolate())
199-
.ToLocal(&perm_string) ||
200-
!ToV8Value(env->context(), res, env->isolate())
197+
Local<Value> perm_string = Permission::PermissionToString(env, perm);
198+
if (!ToV8Value(env->context(), res, env->isolate())
201199
.ToLocal(&resource_string) ||
202200
err->Set(env->context(), env->permission_string(), perm_string)
203201
.IsNothing() ||
@@ -263,18 +261,13 @@ bool Permission::is_scope_granted(Environment* env,
263261
v8::Local<v8::Context> context = env->context();
264262
v8::Local<v8::Object> msg =
265263
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
266-
constchar* perm_str = PermissionToString(permission);
267264
msg->Set(context,
268265
env->permission_string(),
269-
v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked())
266+
PermissionToString(env, permission))
270267
.Check();
271268
msg->Set(context,
272269
env->resource_string(),
273-
v8::String::NewFromUtf8(isolate,
274-
res.data(),
275-
v8::NewStringType::kNormal,
276-
static_cast<int>(res.size()))
277-
.ToLocalChecked())
270+
ToV8Value(context, res).ToLocalChecked())
278271
.Check();
279272
ch->Publish(env, msg);
280273
publishing_ = false;
@@ -333,18 +326,13 @@ void Permission::Drop(Environment* env,
333326
v8::Local<v8::Context> context = env->context();
334327
v8::Local<v8::Object> msg =
335328
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
336-
constchar* perm_str = PermissionToString(scope);
337329
msg->Set(context,
338330
env->permission_string(),
339-
v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked())
331+
PermissionToString(env, scope))
340332
.Check();
341333
msg->Set(context,
342334
env->resource_string(),
343-
v8::String::NewFromUtf8(isolate,
344-
param.data(),
345-
v8::NewStringType::kNormal,
346-
static_cast<int>(param.size()))
347-
.ToLocalChecked())
335+
ToV8Value(context, param).ToLocalChecked())
348336
.Check();
349337
msg->Set(context,
350338
FIXED_ONE_BYTE_STRING(isolate, "drop"),

‎src/permission/permission.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ class Permission {
113113
FORCE_INLINEboolwarning_only() const { return warning_only_; }
114114

115115
static PermissionScope StringToPermission(const std::string& perm);
116-
staticconstchar* PermissionToString(PermissionScope perm);
116+
static v8::Local<v8::String> PermissionToString(Environment* env,
117+
PermissionScope perm);
117118
staticvoidThrowAccessDenied(Environment* env,
118119
PermissionScope perm,
119120
const std::string_view& res);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit f77aa2f

Browse files
jasnelladuh95
authored andcommitted
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: #65158 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent be4e7de commit f77aa2f

5 files changed

Lines changed: 81 additions & 23 deletions

File tree

‎src/env-inl.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,14 @@ void Environment::set_process_exit_handler(
841841
#undef VY
842842
#undef VP
843843

844+
#defineV(Name, label, _, __) \
845+
inline v8::Local<v8::String> \
846+
IsolateData::Name##_permission_string() const { \
847+
return Name##_permission_string##_.Get(isolate_); \
848+
}
849+
PERMISSIONS(V)
850+
#undef V
851+
844852
#defineVM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate)
845853
#defineV(PropertyName, TypeName) \
846854
inline v8::Local<TypeName> IsolateData::PropertyName() const { \
@@ -870,6 +878,14 @@ void Environment::set_process_exit_handler(
870878
#undef VY
871879
#undef VP
872880

881+
#defineV(Name, label, _, __) \
882+
inline v8::Local<v8::String> \
883+
Environment::Name##_permission_string() const { \
884+
returnisolate_data()->Name##_permission_string(); \
885+
}
886+
PERMISSIONS(V)
887+
#undef V
888+
873889
#defineV(PropertyName, TypeName) \
874890
inline v8::Local<TypeName> Environment::PropertyName() const { \
875891
returnisolate_data()->PropertyName(); \

‎src/env.cc‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ IsolateDataSerializeInfo IsolateData::Serialize(SnapshotCreator* creator) {
360360
#undef VS
361361
#undef VP
362362

363+
#defineV(Name, label, _, __) \
364+
info.primitive_values.push_back( \
365+
creator->AddData(Name##_permission_string##_.Get(isolate)));
366+
PERMISSIONS(V)
367+
#undef V
368+
363369
info.primitive_values.reserve(info.primitive_values.size() +
364370
AsyncWrap::PROVIDERS_LENGTH);
365371
for (size_t i = 0; i < AsyncWrap::PROVIDERS_LENGTH; i++) {
@@ -419,6 +425,21 @@ void IsolateData::DeserializeProperties(const IsolateDataSerializeInfo* info) {
419425
#undef VS
420426
#undef VP
421427

428+
#defineV(Name, label, _, __) \
429+
do { \
430+
MaybeLocal<String> maybe_field = \
431+
isolate_->GetDataFromSnapshotOnce<String>( \
432+
info->primitive_values[i++]); \
433+
Local<String> field; \
434+
if (!maybe_field.ToLocal(&field)) { \
435+
fprintf(stderr, \
436+
"Failed to deserialize " #Name "_permission_string\n"); \
437+
} \
438+
Name##_permission_string##_.Set(isolate_, field); \
439+
} while (0);
440+
PERMISSIONS(V)
441+
#undef V
442+
422443
for (size_t j = 0; j < AsyncWrap::PROVIDERS_LENGTH; j++) {
423444
MaybeLocal<String> maybe_field =
424445
isolate_->GetDataFromSnapshotOnce<String>(info->primitive_values[i++]);
@@ -520,6 +541,17 @@ void IsolateData::CreateProperties() {
520541
PER_ISOLATE_STRING_PROPERTIES(V)
521542
#undef V
522543

544+
#defineV(Name, label, _, __) \
545+
Name##_permission_string##_.Set( \
546+
isolate_, \
547+
String::NewFromOneByte(isolate_, \
548+
reinterpret_cast<constuint8_t*>(#Name), \
549+
NewStringType::kInternalized, \
550+
sizeof(#Name) - 1) \
551+
.ToLocalChecked());
552+
PERMISSIONS(V)
553+
#undef V
554+
523555
// Create all the provider strings that will be passed to JS. Place them in
524556
// an array so the array index matches the PROVIDER id offset. This way the
525557
// strings can be retrieved quickly.
@@ -630,6 +662,11 @@ void IsolateData::MemoryInfo(MemoryTracker* tracker) const {
630662
PER_ISOLATE_STRING_PROPERTIES(V)
631663
#undef V
632664

665+
#defineV(Name, label, _, __) \
666+
tracker->TrackField(#Name "_permission_string", Name##_permission_string());
667+
PERMISSIONS(V)
668+
#undef V
669+
633670
tracker->TrackField("async_wrap_providers", async_wrap_providers_);
634671

635672
if (node_allocator_ != nullptr) {

‎src/env.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
189189
#undef VS
190190
#undef VP
191191

192+
#defineV(Name, label, _, __) \
193+
inline v8::Local<v8::String> Name##_permission_string() const;
194+
PERMISSIONS(V)
195+
#undef V
196+
192197
#defineVM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate)
193198
#defineV(PropertyName, TypeName) \
194199
inline v8::Local<TypeName> PropertyName() const; \
@@ -234,6 +239,12 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
234239
#undef VS
235240
#undef VY
236241
#undef VP
242+
243+
#defineV(Name, label, _, __) \
244+
v8::Eternal<v8::String> Name##_permission_string##_;
245+
PERMISSIONS(V)
246+
#undef V
247+
237248
// Keep a list of all Persistent strings used for AsyncWrap Provider types.
238249
std::array<v8::Eternal<v8::String>, AsyncWrap::PROVIDERS_LENGTH>
239250
async_wrap_providers_;
@@ -875,6 +886,11 @@ class Environment final : public MemoryRetainer {
875886
#undef VY
876887
#undef VP
877888

889+
#defineV(Name, label, _, __) \
890+
inline v8::Local<v8::String> Name##_permission_string() const;
891+
PERMISSIONS(V)
892+
#undef V
893+
878894
#defineV(PropertyName, TypeName) \
879895
inline v8::Local<TypeName> PropertyName() const; \
880896
inlinevoid set_ ## PropertyName(v8::Local<TypeName> value);

‎src/permission/permission.cc‎

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ static void Has(const FunctionCallbackInfo<Value>& args) {
106106
} // namespace
107107

108108
#defineV(Name, label, _, __) \
109-
if (perm == PermissionScope::k##Name) return #Name;
110-
constchar* Permission::PermissionToString(const PermissionScope perm) {
109+
if (perm == PermissionScope::k##Name) return env->Name##_permission_string();
110+
v8::Local<v8::String> Permission::PermissionToString(
111+
Environment* env, const PermissionScope perm) {
111112
PERMISSIONS(V)
112-
returnnullptr;
113+
UNREACHABLE();
113114
}
114115
#undef V
115116

@@ -192,12 +193,9 @@ MaybeLocal<Value> CreateAccessDeniedError(Environment* env,
192193
Local<Object> err = ERR_ACCESS_DENIED(
193194
env->isolate(), "Access to this API has been restricted. %s", suggestion);
194195

195-
Local<Value> perm_string;
196196
Local<Value> resource_string;
197-
std::string_view perm_str = Permission::PermissionToString(perm);
198-
if (!ToV8Value(env->context(), perm_str, env->isolate())
199-
.ToLocal(&perm_string) ||
200-
!ToV8Value(env->context(), res, env->isolate())
197+
Local<Value> perm_string = Permission::PermissionToString(env, perm);
198+
if (!ToV8Value(env->context(), res, env->isolate())
201199
.ToLocal(&resource_string) ||
202200
err->Set(env->context(), env->permission_string(), perm_string)
203201
.IsNothing() ||
@@ -263,18 +261,13 @@ bool Permission::is_scope_granted(Environment* env,
263261
v8::Local<v8::Context> context = env->context();
264262
v8::Local<v8::Object> msg =
265263
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
266-
constchar* perm_str = PermissionToString(permission);
267264
msg->Set(context,
268265
env->permission_string(),
269-
v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked())
266+
PermissionToString(env, permission))
270267
.Check();
271268
msg->Set(context,
272269
env->resource_string(),
273-
v8::String::NewFromUtf8(isolate,
274-
res.data(),
275-
v8::NewStringType::kNormal,
276-
static_cast<int>(res.size()))
277-
.ToLocalChecked())
270+
ToV8Value(context, res).ToLocalChecked())
278271
.Check();
279272
ch->Publish(env, msg);
280273
publishing_ = false;
@@ -333,18 +326,13 @@ void Permission::Drop(Environment* env,
333326
v8::Local<v8::Context> context = env->context();
334327
v8::Local<v8::Object> msg =
335328
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
336-
constchar* perm_str = PermissionToString(scope);
337329
msg->Set(context,
338330
env->permission_string(),
339-
v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked())
331+
PermissionToString(env, scope))
340332
.Check();
341333
msg->Set(context,
342334
env->resource_string(),
343-
v8::String::NewFromUtf8(isolate,
344-
param.data(),
345-
v8::NewStringType::kNormal,
346-
static_cast<int>(param.size()))
347-
.ToLocalChecked())
335+
ToV8Value(context, param).ToLocalChecked())
348336
.Check();
349337
msg->Set(context,
350338
FIXED_ONE_BYTE_STRING(isolate, "drop"),

‎src/permission/permission.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ class Permission {
113113
FORCE_INLINEboolwarning_only() const { return warning_only_; }
114114

115115
static PermissionScope StringToPermission(const std::string& perm);
116-
staticconstchar* PermissionToString(PermissionScope perm);
116+
static v8::Local<v8::String> PermissionToString(Environment* env,
117+
PermissionScope perm);
117118
staticvoidThrowAccessDenied(Environment* env,
118119
PermissionScope perm,
119120
const std::string_view& res);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit f77aa2f

Browse files
jasnelladuh95
authored andcommitted
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: #65158 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent be4e7de commit f77aa2f

5 files changed

Lines changed: 81 additions & 23 deletions

File tree

‎src/env-inl.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,14 @@ void Environment::set_process_exit_handler(
841841
#undef VY
842842
#undef VP
843843

844+
#defineV(Name, label, _, __) \
845+
inline v8::Local<v8::String> \
846+
IsolateData::Name##_permission_string() const { \
847+
return Name##_permission_string##_.Get(isolate_); \
848+
}
849+
PERMISSIONS(V)
850+
#undef V
851+
844852
#defineVM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate)
845853
#defineV(PropertyName, TypeName) \
846854
inline v8::Local<TypeName> IsolateData::PropertyName() const { \
@@ -870,6 +878,14 @@ void Environment::set_process_exit_handler(
870878
#undef VY
871879
#undef VP
872880

881+
#defineV(Name, label, _, __) \
882+
inline v8::Local<v8::String> \
883+
Environment::Name##_permission_string() const { \
884+
returnisolate_data()->Name##_permission_string(); \
885+
}
886+
PERMISSIONS(V)
887+
#undef V
888+
873889
#defineV(PropertyName, TypeName) \
874890
inline v8::Local<TypeName> Environment::PropertyName() const { \
875891
returnisolate_data()->PropertyName(); \

‎src/env.cc‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ IsolateDataSerializeInfo IsolateData::Serialize(SnapshotCreator* creator) {
360360
#undef VS
361361
#undef VP
362362

363+
#defineV(Name, label, _, __) \
364+
info.primitive_values.push_back( \
365+
creator->AddData(Name##_permission_string##_.Get(isolate)));
366+
PERMISSIONS(V)
367+
#undef V
368+
363369
info.primitive_values.reserve(info.primitive_values.size() +
364370
AsyncWrap::PROVIDERS_LENGTH);
365371
for (size_t i = 0; i < AsyncWrap::PROVIDERS_LENGTH; i++) {
@@ -419,6 +425,21 @@ void IsolateData::DeserializeProperties(const IsolateDataSerializeInfo* info) {
419425
#undef VS
420426
#undef VP
421427

428+
#defineV(Name, label, _, __) \
429+
do { \
430+
MaybeLocal<String> maybe_field = \
431+
isolate_->GetDataFromSnapshotOnce<String>( \
432+
info->primitive_values[i++]); \
433+
Local<String> field; \
434+
if (!maybe_field.ToLocal(&field)) { \
435+
fprintf(stderr, \
436+
"Failed to deserialize " #Name "_permission_string\n"); \
437+
} \
438+
Name##_permission_string##_.Set(isolate_, field); \
439+
} while (0);
440+
PERMISSIONS(V)
441+
#undef V
442+
422443
for (size_t j = 0; j < AsyncWrap::PROVIDERS_LENGTH; j++) {
423444
MaybeLocal<String> maybe_field =
424445
isolate_->GetDataFromSnapshotOnce<String>(info->primitive_values[i++]);
@@ -520,6 +541,17 @@ void IsolateData::CreateProperties() {
520541
PER_ISOLATE_STRING_PROPERTIES(V)
521542
#undef V
522543

544+
#defineV(Name, label, _, __) \
545+
Name##_permission_string##_.Set( \
546+
isolate_, \
547+
String::NewFromOneByte(isolate_, \
548+
reinterpret_cast<constuint8_t*>(#Name), \
549+
NewStringType::kInternalized, \
550+
sizeof(#Name) - 1) \
551+
.ToLocalChecked());
552+
PERMISSIONS(V)
553+
#undef V
554+
523555
// Create all the provider strings that will be passed to JS. Place them in
524556
// an array so the array index matches the PROVIDER id offset. This way the
525557
// strings can be retrieved quickly.
@@ -630,6 +662,11 @@ void IsolateData::MemoryInfo(MemoryTracker* tracker) const {
630662
PER_ISOLATE_STRING_PROPERTIES(V)
631663
#undef V
632664

665+
#defineV(Name, label, _, __) \
666+
tracker->TrackField(#Name "_permission_string", Name##_permission_string());
667+
PERMISSIONS(V)
668+
#undef V
669+
633670
tracker->TrackField("async_wrap_providers", async_wrap_providers_);
634671

635672
if (node_allocator_ != nullptr) {

‎src/env.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
189189
#undef VS
190190
#undef VP
191191

192+
#defineV(Name, label, _, __) \
193+
inline v8::Local<v8::String> Name##_permission_string() const;
194+
PERMISSIONS(V)
195+
#undef V
196+
192197
#defineVM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate)
193198
#defineV(PropertyName, TypeName) \
194199
inline v8::Local<TypeName> PropertyName() const; \
@@ -234,6 +239,12 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
234239
#undef VS
235240
#undef VY
236241
#undef VP
242+
243+
#defineV(Name, label, _, __) \
244+
v8::Eternal<v8::String> Name##_permission_string##_;
245+
PERMISSIONS(V)
246+
#undef V
247+
237248
// Keep a list of all Persistent strings used for AsyncWrap Provider types.
238249
std::array<v8::Eternal<v8::String>, AsyncWrap::PROVIDERS_LENGTH>
239250
async_wrap_providers_;
@@ -875,6 +886,11 @@ class Environment final : public MemoryRetainer {
875886
#undef VY
876887
#undef VP
877888

889+
#defineV(Name, label, _, __) \
890+
inline v8::Local<v8::String> Name##_permission_string() const;
891+
PERMISSIONS(V)
892+
#undef V
893+
878894
#defineV(PropertyName, TypeName) \
879895
inline v8::Local<TypeName> PropertyName() const; \
880896
inlinevoid set_ ## PropertyName(v8::Local<TypeName> value);

‎src/permission/permission.cc‎

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ static void Has(const FunctionCallbackInfo<Value>& args) {
106106
} // namespace
107107

108108
#defineV(Name, label, _, __) \
109-
if (perm == PermissionScope::k##Name) return #Name;
110-
constchar* Permission::PermissionToString(const PermissionScope perm) {
109+
if (perm == PermissionScope::k##Name) return env->Name##_permission_string();
110+
v8::Local<v8::String> Permission::PermissionToString(
111+
Environment* env, const PermissionScope perm) {
111112
PERMISSIONS(V)
112-
returnnullptr;
113+
UNREACHABLE();
113114
}
114115
#undef V
115116

@@ -192,12 +193,9 @@ MaybeLocal<Value> CreateAccessDeniedError(Environment* env,
192193
Local<Object> err = ERR_ACCESS_DENIED(
193194
env->isolate(), "Access to this API has been restricted. %s", suggestion);
194195

195-
Local<Value> perm_string;
196196
Local<Value> resource_string;
197-
std::string_view perm_str = Permission::PermissionToString(perm);
198-
if (!ToV8Value(env->context(), perm_str, env->isolate())
199-
.ToLocal(&perm_string) ||
200-
!ToV8Value(env->context(), res, env->isolate())
197+
Local<Value> perm_string = Permission::PermissionToString(env, perm);
198+
if (!ToV8Value(env->context(), res, env->isolate())
201199
.ToLocal(&resource_string) ||
202200
err->Set(env->context(), env->permission_string(), perm_string)
203201
.IsNothing() ||
@@ -263,18 +261,13 @@ bool Permission::is_scope_granted(Environment* env,
263261
v8::Local<v8::Context> context = env->context();
264262
v8::Local<v8::Object> msg =
265263
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
266-
constchar* perm_str = PermissionToString(permission);
267264
msg->Set(context,
268265
env->permission_string(),
269-
v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked())
266+
PermissionToString(env, permission))
270267
.Check();
271268
msg->Set(context,
272269
env->resource_string(),
273-
v8::String::NewFromUtf8(isolate,
274-
res.data(),
275-
v8::NewStringType::kNormal,
276-
static_cast<int>(res.size()))
277-
.ToLocalChecked())
270+
ToV8Value(context, res).ToLocalChecked())
278271
.Check();
279272
ch->Publish(env, msg);
280273
publishing_ = false;
@@ -333,18 +326,13 @@ void Permission::Drop(Environment* env,
333326
v8::Local<v8::Context> context = env->context();
334327
v8::Local<v8::Object> msg =
335328
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
336-
constchar* perm_str = PermissionToString(scope);
337329
msg->Set(context,
338330
env->permission_string(),
339-
v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked())
331+
PermissionToString(env, scope))
340332
.Check();
341333
msg->Set(context,
342334
env->resource_string(),
343-
v8::String::NewFromUtf8(isolate,
344-
param.data(),
345-
v8::NewStringType::kNormal,
346-
static_cast<int>(param.size()))
347-
.ToLocalChecked())
335+
ToV8Value(context, param).ToLocalChecked())
348336
.Check();
349337
msg->Set(context,
350338
FIXED_ONE_BYTE_STRING(isolate, "drop"),

‎src/permission/permission.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ class Permission {
113113
FORCE_INLINEboolwarning_only() const { return warning_only_; }
114114

115115
static PermissionScope StringToPermission(const std::string& perm);
116-
staticconstchar* PermissionToString(PermissionScope perm);
116+
static v8::Local<v8::String> PermissionToString(Environment* env,
117+
PermissionScope perm);
117118
staticvoidThrowAccessDenied(Environment* env,
118119
PermissionScope perm,
119120
const std::string_view& res);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit f77aa2f

Browse files
jasnelladuh95
authored andcommitted
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: #65158 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent be4e7de commit f77aa2f

5 files changed

Lines changed: 81 additions & 23 deletions

File tree

‎src/env-inl.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,14 @@ void Environment::set_process_exit_handler(
841841
#undef VY
842842
#undef VP
843843

844+
#defineV(Name, label, _, __) \
845+
inline v8::Local<v8::String> \
846+
IsolateData::Name##_permission_string() const { \
847+
return Name##_permission_string##_.Get(isolate_); \
848+
}
849+
PERMISSIONS(V)
850+
#undef V
851+
844852
#defineVM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate)
845853
#defineV(PropertyName, TypeName) \
846854
inline v8::Local<TypeName> IsolateData::PropertyName() const { \
@@ -870,6 +878,14 @@ void Environment::set_process_exit_handler(
870878
#undef VY
871879
#undef VP
872880

881+
#defineV(Name, label, _, __) \
882+
inline v8::Local<v8::String> \
883+
Environment::Name##_permission_string() const { \
884+
returnisolate_data()->Name##_permission_string(); \
885+
}
886+
PERMISSIONS(V)
887+
#undef V
888+
873889
#defineV(PropertyName, TypeName) \
874890
inline v8::Local<TypeName> Environment::PropertyName() const { \
875891
returnisolate_data()->PropertyName(); \

‎src/env.cc‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ IsolateDataSerializeInfo IsolateData::Serialize(SnapshotCreator* creator) {
360360
#undef VS
361361
#undef VP
362362

363+
#defineV(Name, label, _, __) \
364+
info.primitive_values.push_back( \
365+
creator->AddData(Name##_permission_string##_.Get(isolate)));
366+
PERMISSIONS(V)
367+
#undef V
368+
363369
info.primitive_values.reserve(info.primitive_values.size() +
364370
AsyncWrap::PROVIDERS_LENGTH);
365371
for (size_t i = 0; i < AsyncWrap::PROVIDERS_LENGTH; i++) {
@@ -419,6 +425,21 @@ void IsolateData::DeserializeProperties(const IsolateDataSerializeInfo* info) {
419425
#undef VS
420426
#undef VP
421427

428+
#defineV(Name, label, _, __) \
429+
do { \
430+
MaybeLocal<String> maybe_field = \
431+
isolate_->GetDataFromSnapshotOnce<String>( \
432+
info->primitive_values[i++]); \
433+
Local<String> field; \
434+
if (!maybe_field.ToLocal(&field)) { \
435+
fprintf(stderr, \
436+
"Failed to deserialize " #Name "_permission_string\n"); \
437+
} \
438+
Name##_permission_string##_.Set(isolate_, field); \
439+
} while (0);
440+
PERMISSIONS(V)
441+
#undef V
442+
422443
for (size_t j = 0; j < AsyncWrap::PROVIDERS_LENGTH; j++) {
423444
MaybeLocal<String> maybe_field =
424445
isolate_->GetDataFromSnapshotOnce<String>(info->primitive_values[i++]);
@@ -520,6 +541,17 @@ void IsolateData::CreateProperties() {
520541
PER_ISOLATE_STRING_PROPERTIES(V)
521542
#undef V
522543

544+
#defineV(Name, label, _, __) \
545+
Name##_permission_string##_.Set( \
546+
isolate_, \
547+
String::NewFromOneByte(isolate_, \
548+
reinterpret_cast<constuint8_t*>(#Name), \
549+
NewStringType::kInternalized, \
550+
sizeof(#Name) - 1) \
551+
.ToLocalChecked());
552+
PERMISSIONS(V)
553+
#undef V
554+
523555
// Create all the provider strings that will be passed to JS. Place them in
524556
// an array so the array index matches the PROVIDER id offset. This way the
525557
// strings can be retrieved quickly.
@@ -630,6 +662,11 @@ void IsolateData::MemoryInfo(MemoryTracker* tracker) const {
630662
PER_ISOLATE_STRING_PROPERTIES(V)
631663
#undef V
632664

665+
#defineV(Name, label, _, __) \
666+
tracker->TrackField(#Name "_permission_string", Name##_permission_string());
667+
PERMISSIONS(V)
668+
#undef V
669+
633670
tracker->TrackField("async_wrap_providers", async_wrap_providers_);
634671

635672
if (node_allocator_ != nullptr) {

‎src/env.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
189189
#undef VS
190190
#undef VP
191191

192+
#defineV(Name, label, _, __) \
193+
inline v8::Local<v8::String> Name##_permission_string() const;
194+
PERMISSIONS(V)
195+
#undef V
196+
192197
#defineVM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate)
193198
#defineV(PropertyName, TypeName) \
194199
inline v8::Local<TypeName> PropertyName() const; \
@@ -234,6 +239,12 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
234239
#undef VS
235240
#undef VY
236241
#undef VP
242+
243+
#defineV(Name, label, _, __) \
244+
v8::Eternal<v8::String> Name##_permission_string##_;
245+
PERMISSIONS(V)
246+
#undef V
247+
237248
// Keep a list of all Persistent strings used for AsyncWrap Provider types.
238249
std::array<v8::Eternal<v8::String>, AsyncWrap::PROVIDERS_LENGTH>
239250
async_wrap_providers_;
@@ -875,6 +886,11 @@ class Environment final : public MemoryRetainer {
875886
#undef VY
876887
#undef VP
877888

889+
#defineV(Name, label, _, __) \
890+
inline v8::Local<v8::String> Name##_permission_string() const;
891+
PERMISSIONS(V)
892+
#undef V
893+
878894
#defineV(PropertyName, TypeName) \
879895
inline v8::Local<TypeName> PropertyName() const; \
880896
inlinevoid set_ ## PropertyName(v8::Local<TypeName> value);

‎src/permission/permission.cc‎

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ static void Has(const FunctionCallbackInfo<Value>& args) {
106106
} // namespace
107107

108108
#defineV(Name, label, _, __) \
109-
if (perm == PermissionScope::k##Name) return #Name;
110-
constchar* Permission::PermissionToString(const PermissionScope perm) {
109+
if (perm == PermissionScope::k##Name) return env->Name##_permission_string();
110+
v8::Local<v8::String> Permission::PermissionToString(
111+
Environment* env, const PermissionScope perm) {
111112
PERMISSIONS(V)
112-
returnnullptr;
113+
UNREACHABLE();
113114
}
114115
#undef V
115116

@@ -192,12 +193,9 @@ MaybeLocal<Value> CreateAccessDeniedError(Environment* env,
192193
Local<Object> err = ERR_ACCESS_DENIED(
193194
env->isolate(), "Access to this API has been restricted. %s", suggestion);
194195

195-
Local<Value> perm_string;
196196
Local<Value> resource_string;
197-
std::string_view perm_str = Permission::PermissionToString(perm);
198-
if (!ToV8Value(env->context(), perm_str, env->isolate())
199-
.ToLocal(&perm_string) ||
200-
!ToV8Value(env->context(), res, env->isolate())
197+
Local<Value> perm_string = Permission::PermissionToString(env, perm);
198+
if (!ToV8Value(env->context(), res, env->isolate())
201199
.ToLocal(&resource_string) ||
202200
err->Set(env->context(), env->permission_string(), perm_string)
203201
.IsNothing() ||
@@ -263,18 +261,13 @@ bool Permission::is_scope_granted(Environment* env,
263261
v8::Local<v8::Context> context = env->context();
264262
v8::Local<v8::Object> msg =
265263
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
266-
constchar* perm_str = PermissionToString(permission);
267264
msg->Set(context,
268265
env->permission_string(),
269-
v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked())
266+
PermissionToString(env, permission))
270267
.Check();
271268
msg->Set(context,
272269
env->resource_string(),
273-
v8::String::NewFromUtf8(isolate,
274-
res.data(),
275-
v8::NewStringType::kNormal,
276-
static_cast<int>(res.size()))
277-
.ToLocalChecked())
270+
ToV8Value(context, res).ToLocalChecked())
278271
.Check();
279272
ch->Publish(env, msg);
280273
publishing_ = false;
@@ -333,18 +326,13 @@ void Permission::Drop(Environment* env,
333326
v8::Local<v8::Context> context = env->context();
334327
v8::Local<v8::Object> msg =
335328
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
336-
constchar* perm_str = PermissionToString(scope);
337329
msg->Set(context,
338330
env->permission_string(),
339-
v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked())
331+
PermissionToString(env, scope))
340332
.Check();
341333
msg->Set(context,
342334
env->resource_string(),
343-
v8::String::NewFromUtf8(isolate,
344-
param.data(),
345-
v8::NewStringType::kNormal,
346-
static_cast<int>(param.size()))
347-
.ToLocalChecked())
335+
ToV8Value(context, param).ToLocalChecked())
348336
.Check();
349337
msg->Set(context,
350338
FIXED_ONE_BYTE_STRING(isolate, "drop"),

‎src/permission/permission.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ class Permission {
113113
FORCE_INLINEboolwarning_only() const { return warning_only_; }
114114

115115
static PermissionScope StringToPermission(const std::string& perm);
116-
staticconstchar* PermissionToString(PermissionScope perm);
116+
static v8::Local<v8::String> PermissionToString(Environment* env,
117+
PermissionScope perm);
117118
staticvoidThrowAccessDenied(Environment* env,
118119
PermissionScope perm,
119120
const std::string_view& res);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit f77aa2f

Browse files
jasnelladuh95
authored andcommitted
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: #65158 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent be4e7de commit f77aa2f

5 files changed

Lines changed: 81 additions & 23 deletions

File tree

‎src/env-inl.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,14 @@ void Environment::set_process_exit_handler(
841841
#undef VY
842842
#undef VP
843843

844+
#defineV(Name, label, _, __) \
845+
inline v8::Local<v8::String> \
846+
IsolateData::Name##_permission_string() const { \
847+
return Name##_permission_string##_.Get(isolate_); \
848+
}
849+
PERMISSIONS(V)
850+
#undef V
851+
844852
#defineVM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate)
845853
#defineV(PropertyName, TypeName) \
846854
inline v8::Local<TypeName> IsolateData::PropertyName() const { \
@@ -870,6 +878,14 @@ void Environment::set_process_exit_handler(
870878
#undef VY
871879
#undef VP
872880

881+
#defineV(Name, label, _, __) \
882+
inline v8::Local<v8::String> \
883+
Environment::Name##_permission_string() const { \
884+
returnisolate_data()->Name##_permission_string(); \
885+
}
886+
PERMISSIONS(V)
887+
#undef V
888+
873889
#defineV(PropertyName, TypeName) \
874890
inline v8::Local<TypeName> Environment::PropertyName() const { \
875891
returnisolate_data()->PropertyName(); \

‎src/env.cc‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ IsolateDataSerializeInfo IsolateData::Serialize(SnapshotCreator* creator) {
360360
#undef VS
361361
#undef VP
362362

363+
#defineV(Name, label, _, __) \
364+
info.primitive_values.push_back( \
365+
creator->AddData(Name##_permission_string##_.Get(isolate)));
366+
PERMISSIONS(V)
367+
#undef V
368+
363369
info.primitive_values.reserve(info.primitive_values.size() +
364370
AsyncWrap::PROVIDERS_LENGTH);
365371
for (size_t i = 0; i < AsyncWrap::PROVIDERS_LENGTH; i++) {
@@ -419,6 +425,21 @@ void IsolateData::DeserializeProperties(const IsolateDataSerializeInfo* info) {
419425
#undef VS
420426
#undef VP
421427

428+
#defineV(Name, label, _, __) \
429+
do { \
430+
MaybeLocal<String> maybe_field = \
431+
isolate_->GetDataFromSnapshotOnce<String>( \
432+
info->primitive_values[i++]); \
433+
Local<String> field; \
434+
if (!maybe_field.ToLocal(&field)) { \
435+
fprintf(stderr, \
436+
"Failed to deserialize " #Name "_permission_string\n"); \
437+
} \
438+
Name##_permission_string##_.Set(isolate_, field); \
439+
} while (0);
440+
PERMISSIONS(V)
441+
#undef V
442+
422443
for (size_t j = 0; j < AsyncWrap::PROVIDERS_LENGTH; j++) {
423444
MaybeLocal<String> maybe_field =
424445
isolate_->GetDataFromSnapshotOnce<String>(info->primitive_values[i++]);
@@ -520,6 +541,17 @@ void IsolateData::CreateProperties() {
520541
PER_ISOLATE_STRING_PROPERTIES(V)
521542
#undef V
522543

544+
#defineV(Name, label, _, __) \
545+
Name##_permission_string##_.Set( \
546+
isolate_, \
547+
String::NewFromOneByte(isolate_, \
548+
reinterpret_cast<constuint8_t*>(#Name), \
549+
NewStringType::kInternalized, \
550+
sizeof(#Name) - 1) \
551+
.ToLocalChecked());
552+
PERMISSIONS(V)
553+
#undef V
554+
523555
// Create all the provider strings that will be passed to JS. Place them in
524556
// an array so the array index matches the PROVIDER id offset. This way the
525557
// strings can be retrieved quickly.
@@ -630,6 +662,11 @@ void IsolateData::MemoryInfo(MemoryTracker* tracker) const {
630662
PER_ISOLATE_STRING_PROPERTIES(V)
631663
#undef V
632664

665+
#defineV(Name, label, _, __) \
666+
tracker->TrackField(#Name "_permission_string", Name##_permission_string());
667+
PERMISSIONS(V)
668+
#undef V
669+
633670
tracker->TrackField("async_wrap_providers", async_wrap_providers_);
634671

635672
if (node_allocator_ != nullptr) {

‎src/env.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
189189
#undef VS
190190
#undef VP
191191

192+
#defineV(Name, label, _, __) \
193+
inline v8::Local<v8::String> Name##_permission_string() const;
194+
PERMISSIONS(V)
195+
#undef V
196+
192197
#defineVM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate)
193198
#defineV(PropertyName, TypeName) \
194199
inline v8::Local<TypeName> PropertyName() const; \
@@ -234,6 +239,12 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
234239
#undef VS
235240
#undef VY
236241
#undef VP
242+
243+
#defineV(Name, label, _, __) \
244+
v8::Eternal<v8::String> Name##_permission_string##_;
245+
PERMISSIONS(V)
246+
#undef V
247+
237248
// Keep a list of all Persistent strings used for AsyncWrap Provider types.
238249
std::array<v8::Eternal<v8::String>, AsyncWrap::PROVIDERS_LENGTH>
239250
async_wrap_providers_;
@@ -875,6 +886,11 @@ class Environment final : public MemoryRetainer {
875886
#undef VY
876887
#undef VP
877888

889+
#defineV(Name, label, _, __) \
890+
inline v8::Local<v8::String> Name##_permission_string() const;
891+
PERMISSIONS(V)
892+
#undef V
893+
878894
#defineV(PropertyName, TypeName) \
879895
inline v8::Local<TypeName> PropertyName() const; \
880896
inlinevoid set_ ## PropertyName(v8::Local<TypeName> value);

‎src/permission/permission.cc‎

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ static void Has(const FunctionCallbackInfo<Value>& args) {
106106
} // namespace
107107

108108
#defineV(Name, label, _, __) \
109-
if (perm == PermissionScope::k##Name) return #Name;
110-
constchar* Permission::PermissionToString(const PermissionScope perm) {
109+
if (perm == PermissionScope::k##Name) return env->Name##_permission_string();
110+
v8::Local<v8::String> Permission::PermissionToString(
111+
Environment* env, const PermissionScope perm) {
111112
PERMISSIONS(V)
112-
returnnullptr;
113+
UNREACHABLE();
113114
}
114115
#undef V
115116

@@ -192,12 +193,9 @@ MaybeLocal<Value> CreateAccessDeniedError(Environment* env,
192193
Local<Object> err = ERR_ACCESS_DENIED(
193194
env->isolate(), "Access to this API has been restricted. %s", suggestion);
194195

195-
Local<Value> perm_string;
196196
Local<Value> resource_string;
197-
std::string_view perm_str = Permission::PermissionToString(perm);
198-
if (!ToV8Value(env->context(), perm_str, env->isolate())
199-
.ToLocal(&perm_string) ||
200-
!ToV8Value(env->context(), res, env->isolate())
197+
Local<Value> perm_string = Permission::PermissionToString(env, perm);
198+
if (!ToV8Value(env->context(), res, env->isolate())
201199
.ToLocal(&resource_string) ||
202200
err->Set(env->context(), env->permission_string(), perm_string)
203201
.IsNothing() ||
@@ -263,18 +261,13 @@ bool Permission::is_scope_granted(Environment* env,
263261
v8::Local<v8::Context> context = env->context();
264262
v8::Local<v8::Object> msg =
265263
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
266-
constchar* perm_str = PermissionToString(permission);
267264
msg->Set(context,
268265
env->permission_string(),
269-
v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked())
266+
PermissionToString(env, permission))
270267
.Check();
271268
msg->Set(context,
272269
env->resource_string(),
273-
v8::String::NewFromUtf8(isolate,
274-
res.data(),
275-
v8::NewStringType::kNormal,
276-
static_cast<int>(res.size()))
277-
.ToLocalChecked())
270+
ToV8Value(context, res).ToLocalChecked())
278271
.Check();
279272
ch->Publish(env, msg);
280273
publishing_ = false;
@@ -333,18 +326,13 @@ void Permission::Drop(Environment* env,
333326
v8::Local<v8::Context> context = env->context();
334327
v8::Local<v8::Object> msg =
335328
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
336-
constchar* perm_str = PermissionToString(scope);
337329
msg->Set(context,
338330
env->permission_string(),
339-
v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked())
331+
PermissionToString(env, scope))
340332
.Check();
341333
msg->Set(context,
342334
env->resource_string(),
343-
v8::String::NewFromUtf8(isolate,
344-
param.data(),
345-
v8::NewStringType::kNormal,
346-
static_cast<int>(param.size()))
347-
.ToLocalChecked())
335+
ToV8Value(context, param).ToLocalChecked())
348336
.Check();
349337
msg->Set(context,
350338
FIXED_ONE_BYTE_STRING(isolate, "drop"),

‎src/permission/permission.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ class Permission {
113113
FORCE_INLINEboolwarning_only() const { return warning_only_; }
114114

115115
static PermissionScope StringToPermission(const std::string& perm);
116-
staticconstchar* PermissionToString(PermissionScope perm);
116+
static v8::Local<v8::String> PermissionToString(Environment* env,
117+
PermissionScope perm);
117118
staticvoidThrowAccessDenied(Environment* env,
118119
PermissionScope perm,
119120
const std::string_view& res);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit f77aa2f

Browse files
jasnelladuh95
authored andcommitted
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: #65158 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent be4e7de commit f77aa2f

5 files changed

Lines changed: 81 additions & 23 deletions

File tree

‎src/env-inl.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,14 @@ void Environment::set_process_exit_handler(
841841
#undef VY
842842
#undef VP
843843

844+
#defineV(Name, label, _, __) \
845+
inline v8::Local<v8::String> \
846+
IsolateData::Name##_permission_string() const { \
847+
return Name##_permission_string##_.Get(isolate_); \
848+
}
849+
PERMISSIONS(V)
850+
#undef V
851+
844852
#defineVM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate)
845853
#defineV(PropertyName, TypeName) \
846854
inline v8::Local<TypeName> IsolateData::PropertyName() const { \
@@ -870,6 +878,14 @@ void Environment::set_process_exit_handler(
870878
#undef VY
871879
#undef VP
872880

881+
#defineV(Name, label, _, __) \
882+
inline v8::Local<v8::String> \
883+
Environment::Name##_permission_string() const { \
884+
returnisolate_data()->Name##_permission_string(); \
885+
}
886+
PERMISSIONS(V)
887+
#undef V
888+
873889
#defineV(PropertyName, TypeName) \
874890
inline v8::Local<TypeName> Environment::PropertyName() const { \
875891
returnisolate_data()->PropertyName(); \

‎src/env.cc‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ IsolateDataSerializeInfo IsolateData::Serialize(SnapshotCreator* creator) {
360360
#undef VS
361361
#undef VP
362362

363+
#defineV(Name, label, _, __) \
364+
info.primitive_values.push_back( \
365+
creator->AddData(Name##_permission_string##_.Get(isolate)));
366+
PERMISSIONS(V)
367+
#undef V
368+
363369
info.primitive_values.reserve(info.primitive_values.size() +
364370
AsyncWrap::PROVIDERS_LENGTH);
365371
for (size_t i = 0; i < AsyncWrap::PROVIDERS_LENGTH; i++) {
@@ -419,6 +425,21 @@ void IsolateData::DeserializeProperties(const IsolateDataSerializeInfo* info) {
419425
#undef VS
420426
#undef VP
421427

428+
#defineV(Name, label, _, __) \
429+
do { \
430+
MaybeLocal<String> maybe_field = \
431+
isolate_->GetDataFromSnapshotOnce<String>( \
432+
info->primitive_values[i++]); \
433+
Local<String> field; \
434+
if (!maybe_field.ToLocal(&field)) { \
435+
fprintf(stderr, \
436+
"Failed to deserialize " #Name "_permission_string\n"); \
437+
} \
438+
Name##_permission_string##_.Set(isolate_, field); \
439+
} while (0);
440+
PERMISSIONS(V)
441+
#undef V
442+
422443
for (size_t j = 0; j < AsyncWrap::PROVIDERS_LENGTH; j++) {
423444
MaybeLocal<String> maybe_field =
424445
isolate_->GetDataFromSnapshotOnce<String>(info->primitive_values[i++]);
@@ -520,6 +541,17 @@ void IsolateData::CreateProperties() {
520541
PER_ISOLATE_STRING_PROPERTIES(V)
521542
#undef V
522543

544+
#defineV(Name, label, _, __) \
545+
Name##_permission_string##_.Set( \
546+
isolate_, \
547+
String::NewFromOneByte(isolate_, \
548+
reinterpret_cast<constuint8_t*>(#Name), \
549+
NewStringType::kInternalized, \
550+
sizeof(#Name) - 1) \
551+
.ToLocalChecked());
552+
PERMISSIONS(V)
553+
#undef V
554+
523555
// Create all the provider strings that will be passed to JS. Place them in
524556
// an array so the array index matches the PROVIDER id offset. This way the
525557
// strings can be retrieved quickly.
@@ -630,6 +662,11 @@ void IsolateData::MemoryInfo(MemoryTracker* tracker) const {
630662
PER_ISOLATE_STRING_PROPERTIES(V)
631663
#undef V
632664

665+
#defineV(Name, label, _, __) \
666+
tracker->TrackField(#Name "_permission_string", Name##_permission_string());
667+
PERMISSIONS(V)
668+
#undef V
669+
633670
tracker->TrackField("async_wrap_providers", async_wrap_providers_);
634671

635672
if (node_allocator_ != nullptr) {

‎src/env.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
189189
#undef VS
190190
#undef VP
191191

192+
#defineV(Name, label, _, __) \
193+
inline v8::Local<v8::String> Name##_permission_string() const;
194+
PERMISSIONS(V)
195+
#undef V
196+
192197
#defineVM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate)
193198
#defineV(PropertyName, TypeName) \
194199
inline v8::Local<TypeName> PropertyName() const; \
@@ -234,6 +239,12 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
234239
#undef VS
235240
#undef VY
236241
#undef VP
242+
243+
#defineV(Name, label, _, __) \
244+
v8::Eternal<v8::String> Name##_permission_string##_;
245+
PERMISSIONS(V)
246+
#undef V
247+
237248
// Keep a list of all Persistent strings used for AsyncWrap Provider types.
238249
std::array<v8::Eternal<v8::String>, AsyncWrap::PROVIDERS_LENGTH>
239250
async_wrap_providers_;
@@ -875,6 +886,11 @@ class Environment final : public MemoryRetainer {
875886
#undef VY
876887
#undef VP
877888

889+
#defineV(Name, label, _, __) \
890+
inline v8::Local<v8::String> Name##_permission_string() const;
891+
PERMISSIONS(V)
892+
#undef V
893+
878894
#defineV(PropertyName, TypeName) \
879895
inline v8::Local<TypeName> PropertyName() const; \
880896
inlinevoid set_ ## PropertyName(v8::Local<TypeName> value);

‎src/permission/permission.cc‎

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ static void Has(const FunctionCallbackInfo<Value>& args) {
106106
} // namespace
107107

108108
#defineV(Name, label, _, __) \
109-
if (perm == PermissionScope::k##Name) return #Name;
110-
constchar* Permission::PermissionToString(const PermissionScope perm) {
109+
if (perm == PermissionScope::k##Name) return env->Name##_permission_string();
110+
v8::Local<v8::String> Permission::PermissionToString(
111+
Environment* env, const PermissionScope perm) {
111112
PERMISSIONS(V)
112-
returnnullptr;
113+
UNREACHABLE();
113114
}
114115
#undef V
115116

@@ -192,12 +193,9 @@ MaybeLocal<Value> CreateAccessDeniedError(Environment* env,
192193
Local<Object> err = ERR_ACCESS_DENIED(
193194
env->isolate(), "Access to this API has been restricted. %s", suggestion);
194195

195-
Local<Value> perm_string;
196196
Local<Value> resource_string;
197-
std::string_view perm_str = Permission::PermissionToString(perm);
198-
if (!ToV8Value(env->context(), perm_str, env->isolate())
199-
.ToLocal(&perm_string) ||
200-
!ToV8Value(env->context(), res, env->isolate())
197+
Local<Value> perm_string = Permission::PermissionToString(env, perm);
198+
if (!ToV8Value(env->context(), res, env->isolate())
201199
.ToLocal(&resource_string) ||
202200
err->Set(env->context(), env->permission_string(), perm_string)
203201
.IsNothing() ||
@@ -263,18 +261,13 @@ bool Permission::is_scope_granted(Environment* env,
263261
v8::Local<v8::Context> context = env->context();
264262
v8::Local<v8::Object> msg =
265263
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
266-
constchar* perm_str = PermissionToString(permission);
267264
msg->Set(context,
268265
env->permission_string(),
269-
v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked())
266+
PermissionToString(env, permission))
270267
.Check();
271268
msg->Set(context,
272269
env->resource_string(),
273-
v8::String::NewFromUtf8(isolate,
274-
res.data(),
275-
v8::NewStringType::kNormal,
276-
static_cast<int>(res.size()))
277-
.ToLocalChecked())
270+
ToV8Value(context, res).ToLocalChecked())
278271
.Check();
279272
ch->Publish(env, msg);
280273
publishing_ = false;
@@ -333,18 +326,13 @@ void Permission::Drop(Environment* env,
333326
v8::Local<v8::Context> context = env->context();
334327
v8::Local<v8::Object> msg =
335328
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
336-
constchar* perm_str = PermissionToString(scope);
337329
msg->Set(context,
338330
env->permission_string(),
339-
v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked())
331+
PermissionToString(env, scope))
340332
.Check();
341333
msg->Set(context,
342334
env->resource_string(),
343-
v8::String::NewFromUtf8(isolate,
344-
param.data(),
345-
v8::NewStringType::kNormal,
346-
static_cast<int>(param.size()))
347-
.ToLocalChecked())
335+
ToV8Value(context, param).ToLocalChecked())
348336
.Check();
349337
msg->Set(context,
350338
FIXED_ONE_BYTE_STRING(isolate, "drop"),

‎src/permission/permission.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ class Permission {
113113
FORCE_INLINEboolwarning_only() const { return warning_only_; }
114114

115115
static PermissionScope StringToPermission(const std::string& perm);
116-
staticconstchar* PermissionToString(PermissionScope perm);
116+
static v8::Local<v8::String> PermissionToString(Environment* env,
117+
PermissionScope perm);
117118
staticvoidThrowAccessDenied(Environment* env,
118119
PermissionScope perm,
119120
const std::string_view& res);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit f77aa2f

Browse files
jasnelladuh95
authored andcommitted
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: #65158 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent be4e7de commit f77aa2f

5 files changed

Lines changed: 81 additions & 23 deletions

File tree

‎src/env-inl.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,14 @@ void Environment::set_process_exit_handler(
841841
#undef VY
842842
#undef VP
843843

844+
#defineV(Name, label, _, __) \
845+
inline v8::Local<v8::String> \
846+
IsolateData::Name##_permission_string() const { \
847+
return Name##_permission_string##_.Get(isolate_); \
848+
}
849+
PERMISSIONS(V)
850+
#undef V
851+
844852
#defineVM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate)
845853
#defineV(PropertyName, TypeName) \
846854
inline v8::Local<TypeName> IsolateData::PropertyName() const { \
@@ -870,6 +878,14 @@ void Environment::set_process_exit_handler(
870878
#undef VY
871879
#undef VP
872880

881+
#defineV(Name, label, _, __) \
882+
inline v8::Local<v8::String> \
883+
Environment::Name##_permission_string() const { \
884+
returnisolate_data()->Name##_permission_string(); \
885+
}
886+
PERMISSIONS(V)
887+
#undef V
888+
873889
#defineV(PropertyName, TypeName) \
874890
inline v8::Local<TypeName> Environment::PropertyName() const { \
875891
returnisolate_data()->PropertyName(); \

‎src/env.cc‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ IsolateDataSerializeInfo IsolateData::Serialize(SnapshotCreator* creator) {
360360
#undef VS
361361
#undef VP
362362

363+
#defineV(Name, label, _, __) \
364+
info.primitive_values.push_back( \
365+
creator->AddData(Name##_permission_string##_.Get(isolate)));
366+
PERMISSIONS(V)
367+
#undef V
368+
363369
info.primitive_values.reserve(info.primitive_values.size() +
364370
AsyncWrap::PROVIDERS_LENGTH);
365371
for (size_t i = 0; i < AsyncWrap::PROVIDERS_LENGTH; i++) {
@@ -419,6 +425,21 @@ void IsolateData::DeserializeProperties(const IsolateDataSerializeInfo* info) {
419425
#undef VS
420426
#undef VP
421427

428+
#defineV(Name, label, _, __) \
429+
do { \
430+
MaybeLocal<String> maybe_field = \
431+
isolate_->GetDataFromSnapshotOnce<String>( \
432+
info->primitive_values[i++]); \
433+
Local<String> field; \
434+
if (!maybe_field.ToLocal(&field)) { \
435+
fprintf(stderr, \
436+
"Failed to deserialize " #Name "_permission_string\n"); \
437+
} \
438+
Name##_permission_string##_.Set(isolate_, field); \
439+
} while (0);
440+
PERMISSIONS(V)
441+
#undef V
442+
422443
for (size_t j = 0; j < AsyncWrap::PROVIDERS_LENGTH; j++) {
423444
MaybeLocal<String> maybe_field =
424445
isolate_->GetDataFromSnapshotOnce<String>(info->primitive_values[i++]);
@@ -520,6 +541,17 @@ void IsolateData::CreateProperties() {
520541
PER_ISOLATE_STRING_PROPERTIES(V)
521542
#undef V
522543

544+
#defineV(Name, label, _, __) \
545+
Name##_permission_string##_.Set( \
546+
isolate_, \
547+
String::NewFromOneByte(isolate_, \
548+
reinterpret_cast<constuint8_t*>(#Name), \
549+
NewStringType::kInternalized, \
550+
sizeof(#Name) - 1) \
551+
.ToLocalChecked());
552+
PERMISSIONS(V)
553+
#undef V
554+
523555
// Create all the provider strings that will be passed to JS. Place them in
524556
// an array so the array index matches the PROVIDER id offset. This way the
525557
// strings can be retrieved quickly.
@@ -630,6 +662,11 @@ void IsolateData::MemoryInfo(MemoryTracker* tracker) const {
630662
PER_ISOLATE_STRING_PROPERTIES(V)
631663
#undef V
632664

665+
#defineV(Name, label, _, __) \
666+
tracker->TrackField(#Name "_permission_string", Name##_permission_string());
667+
PERMISSIONS(V)
668+
#undef V
669+
633670
tracker->TrackField("async_wrap_providers", async_wrap_providers_);
634671

635672
if (node_allocator_ != nullptr) {

‎src/env.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
189189
#undef VS
190190
#undef VP
191191

192+
#defineV(Name, label, _, __) \
193+
inline v8::Local<v8::String> Name##_permission_string() const;
194+
PERMISSIONS(V)
195+
#undef V
196+
192197
#defineVM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate)
193198
#defineV(PropertyName, TypeName) \
194199
inline v8::Local<TypeName> PropertyName() const; \
@@ -234,6 +239,12 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
234239
#undef VS
235240
#undef VY
236241
#undef VP
242+
243+
#defineV(Name, label, _, __) \
244+
v8::Eternal<v8::String> Name##_permission_string##_;
245+
PERMISSIONS(V)
246+
#undef V
247+
237248
// Keep a list of all Persistent strings used for AsyncWrap Provider types.
238249
std::array<v8::Eternal<v8::String>, AsyncWrap::PROVIDERS_LENGTH>
239250
async_wrap_providers_;
@@ -875,6 +886,11 @@ class Environment final : public MemoryRetainer {
875886
#undef VY
876887
#undef VP
877888

889+
#defineV(Name, label, _, __) \
890+
inline v8::Local<v8::String> Name##_permission_string() const;
891+
PERMISSIONS(V)
892+
#undef V
893+
878894
#defineV(PropertyName, TypeName) \
879895
inline v8::Local<TypeName> PropertyName() const; \
880896
inlinevoid set_ ## PropertyName(v8::Local<TypeName> value);

‎src/permission/permission.cc‎

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ static void Has(const FunctionCallbackInfo<Value>& args) {
106106
} // namespace
107107

108108
#defineV(Name, label, _, __) \
109-
if (perm == PermissionScope::k##Name) return #Name;
110-
constchar* Permission::PermissionToString(const PermissionScope perm) {
109+
if (perm == PermissionScope::k##Name) return env->Name##_permission_string();
110+
v8::Local<v8::String> Permission::PermissionToString(
111+
Environment* env, const PermissionScope perm) {
111112
PERMISSIONS(V)
112-
returnnullptr;
113+
UNREACHABLE();
113114
}
114115
#undef V
115116

@@ -192,12 +193,9 @@ MaybeLocal<Value> CreateAccessDeniedError(Environment* env,
192193
Local<Object> err = ERR_ACCESS_DENIED(
193194
env->isolate(), "Access to this API has been restricted. %s", suggestion);
194195

195-
Local<Value> perm_string;
196196
Local<Value> resource_string;
197-
std::string_view perm_str = Permission::PermissionToString(perm);
198-
if (!ToV8Value(env->context(), perm_str, env->isolate())
199-
.ToLocal(&perm_string) ||
200-
!ToV8Value(env->context(), res, env->isolate())
197+
Local<Value> perm_string = Permission::PermissionToString(env, perm);
198+
if (!ToV8Value(env->context(), res, env->isolate())
201199
.ToLocal(&resource_string) ||
202200
err->Set(env->context(), env->permission_string(), perm_string)
203201
.IsNothing() ||
@@ -263,18 +261,13 @@ bool Permission::is_scope_granted(Environment* env,
263261
v8::Local<v8::Context> context = env->context();
264262
v8::Local<v8::Object> msg =
265263
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
266-
constchar* perm_str = PermissionToString(permission);
267264
msg->Set(context,
268265
env->permission_string(),
269-
v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked())
266+
PermissionToString(env, permission))
270267
.Check();
271268
msg->Set(context,
272269
env->resource_string(),
273-
v8::String::NewFromUtf8(isolate,
274-
res.data(),
275-
v8::NewStringType::kNormal,
276-
static_cast<int>(res.size()))
277-
.ToLocalChecked())
270+
ToV8Value(context, res).ToLocalChecked())
278271
.Check();
279272
ch->Publish(env, msg);
280273
publishing_ = false;
@@ -333,18 +326,13 @@ void Permission::Drop(Environment* env,
333326
v8::Local<v8::Context> context = env->context();
334327
v8::Local<v8::Object> msg =
335328
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
336-
constchar* perm_str = PermissionToString(scope);
337329
msg->Set(context,
338330
env->permission_string(),
339-
v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked())
331+
PermissionToString(env, scope))
340332
.Check();
341333
msg->Set(context,
342334
env->resource_string(),
343-
v8::String::NewFromUtf8(isolate,
344-
param.data(),
345-
v8::NewStringType::kNormal,
346-
static_cast<int>(param.size()))
347-
.ToLocalChecked())
335+
ToV8Value(context, param).ToLocalChecked())
348336
.Check();
349337
msg->Set(context,
350338
FIXED_ONE_BYTE_STRING(isolate, "drop"),

‎src/permission/permission.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ class Permission {
113113
FORCE_INLINEboolwarning_only() const { return warning_only_; }
114114

115115
static PermissionScope StringToPermission(const std::string& perm);
116-
staticconstchar* PermissionToString(PermissionScope perm);
116+
static v8::Local<v8::String> PermissionToString(Environment* env,
117+
PermissionScope perm);
117118
staticvoidThrowAccessDenied(Environment* env,
118119
PermissionScope perm,
119120
const std::string_view& res);

0 commit comments

Comments
 (0)