GeezerVison is showing github.com — src: cache permission strings · nodejs/node@f77aa2f
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/env-inl.h +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -841,6 +841,14 @@ void Environment::set_process_exit_handler( | 841 | 841 | #undef VY | 842 | 842 | #undef VP | 843 | 843 |
| | 844 | +#define V(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 | + | 844 | 852 | #define VM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate) | 845 | 853 | #define V(PropertyName, TypeName) \ | 846 | 854 | inline v8::Local<TypeName> IsolateData::PropertyName() const { \ | @@ -870,6 +878,14 @@ void Environment::set_process_exit_handler( | 870 | 878 | #undef VY | 871 | 879 | #undef VP | 872 | 880 |
| | 881 | +#define V(Name, label, _, __) \ | | 882 | + inline v8::Local<v8::String> \ | | 883 | + Environment::Name##_permission_string() const { \ | | 884 | + return isolate_data()->Name##_permission_string(); \ | | 885 | + } | | 886 | + PERMISSIONS(V) | | 887 | +#undef V | | 888 | + | 873 | 889 | #define V(PropertyName, TypeName) \ | 874 | 890 | inline v8::Local<TypeName> Environment::PropertyName() const { \ | 875 | 891 | return isolate_data()->PropertyName(); \ | |
Collapse file Copy file name to clipboardExpand all lines: src/env.cc +37Lines changed: 37 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -360,6 +360,12 @@ IsolateDataSerializeInfo IsolateData::Serialize(SnapshotCreator* creator) { | 360 | 360 | #undef VS | 361 | 361 | #undef VP | 362 | 362 |
| | 363 | +#define V(Name, label, _, __) \ | | 364 | + info.primitive_values.push_back( \ | | 365 | + creator->AddData(Name##_permission_string##_.Get(isolate))); | | 366 | + PERMISSIONS(V) | | 367 | +#undef V | | 368 | + | 363 | 369 | info.primitive_values.reserve(info.primitive_values.size() + | 364 | 370 | AsyncWrap::PROVIDERS_LENGTH); | 365 | 371 | for (size_t i = 0; i < AsyncWrap::PROVIDERS_LENGTH; i++) { | @@ -419,6 +425,21 @@ void IsolateData::DeserializeProperties(const IsolateDataSerializeInfo* info) { | 419 | 425 | #undef VS | 420 | 426 | #undef VP | 421 | 427 |
| | 428 | +#define V(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 | + | 422 | 443 | for (size_t j = 0; j < AsyncWrap::PROVIDERS_LENGTH; j++) { | 423 | 444 | MaybeLocal<String> maybe_field = | 424 | 445 | isolate_->GetDataFromSnapshotOnce<String>(info->primitive_values[i++]); | @@ -520,6 +541,17 @@ void IsolateData::CreateProperties() { | 520 | 541 | PER_ISOLATE_STRING_PROPERTIES(V) | 521 | 542 | #undef V | 522 | 543 |
| | 544 | +#define V(Name, label, _, __) \ | | 545 | + Name##_permission_string##_.Set( \ | | 546 | + isolate_, \ | | 547 | + String::NewFromOneByte(isolate_, \ | | 548 | + reinterpret_cast<const uint8_t*>(#Name), \ | | 549 | + NewStringType::kInternalized, \ | | 550 | + sizeof(#Name) - 1) \ | | 551 | + .ToLocalChecked()); | | 552 | + PERMISSIONS(V) | | 553 | +#undef V | | 554 | + | 523 | 555 | // Create all the provider strings that will be passed to JS. Place them in | 524 | 556 | // an array so the array index matches the PROVIDER id offset. This way the | 525 | 557 | // strings can be retrieved quickly. | @@ -630,6 +662,11 @@ void IsolateData::MemoryInfo(MemoryTracker* tracker) const { | 630 | 662 | PER_ISOLATE_STRING_PROPERTIES(V) | 631 | 663 | #undef V | 632 | 664 |
| | 665 | +#define V(Name, label, _, __) \ | | 666 | + tracker->TrackField(#Name "_permission_string", Name##_permission_string()); | | 667 | + PERMISSIONS(V) | | 668 | +#undef V | | 669 | + | 633 | 670 | tracker->TrackField("async_wrap_providers", async_wrap_providers_); | 634 | 671 |
| 635 | 672 | if (node_allocator_ != nullptr) { | |
Collapse file Copy file name to clipboardExpand all lines: src/env.h +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -189,6 +189,11 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer { | 189 | 189 | #undef VS | 190 | 190 | #undef VP | 191 | 191 |
| | 192 | +#define V(Name, label, _, __) \ | | 193 | + inline v8::Local<v8::String> Name##_permission_string() const; | | 194 | + PERMISSIONS(V) | | 195 | +#undef V | | 196 | + | 192 | 197 | #define VM(PropertyName) V(PropertyName##_binding_template, v8::ObjectTemplate) | 193 | 198 | #define V(PropertyName, TypeName) \ | 194 | 199 | inline v8::Local<TypeName> PropertyName() const; \ | @@ -234,6 +239,12 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer { | 234 | 239 | #undef VS | 235 | 240 | #undef VY | 236 | 241 | #undef VP | | 242 | + | | 243 | +#define V(Name, label, _, __) \ | | 244 | + v8::Eternal<v8::String> Name##_permission_string##_; | | 245 | + PERMISSIONS(V) | | 246 | +#undef V | | 247 | + | 237 | 248 | // Keep a list of all Persistent strings used for AsyncWrap Provider types. | 238 | 249 | std::array<v8::Eternal<v8::String>, AsyncWrap::PROVIDERS_LENGTH> | 239 | 250 | async_wrap_providers_; | @@ -875,6 +886,11 @@ class Environment final : public MemoryRetainer { | 875 | 886 | #undef VY | 876 | 887 | #undef VP | 877 | 888 |
| | 889 | +#define V(Name, label, _, __) \ | | 890 | + inline v8::Local<v8::String> Name##_permission_string() const; | | 891 | + PERMISSIONS(V) | | 892 | +#undef V | | 893 | + | 878 | 894 | #define V(PropertyName, TypeName) \ | 879 | 895 | inline v8::Local<TypeName> PropertyName() const; \ | 880 | 896 | inline void set_ ## PropertyName(v8::Local<TypeName> value); | |
Collapse file Copy file name to clipboardExpand all lines: src/permission/permission.cc +10-22Lines changed: 10 additions & 22 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -106,10 +106,11 @@ static void Has(const FunctionCallbackInfo<Value>& args) { | 106 | 106 | } // namespace | 107 | 107 |
| 108 | 108 | #define V(Name, label, _, __) \ | 109 | | - if (perm == PermissionScope::k##Name) return #Name; | 110 | | -const char* 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) { | 111 | 112 | PERMISSIONS(V) | 112 | | - return nullptr; | | 113 | + UNREACHABLE(); | 113 | 114 | } | 114 | 115 | #undef V | 115 | 116 |
| @@ -192,12 +193,9 @@ MaybeLocal<Value> CreateAccessDeniedError(Environment* env, | 192 | 193 | Local<Object> err = ERR_ACCESS_DENIED( | 193 | 194 | env->isolate(), "Access to this API has been restricted. %s", suggestion); | 194 | 195 |
| 195 | | - Local<Value> perm_string; | 196 | 196 | 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()) | 201 | 199 | .ToLocal(&resource_string) || | 202 | 200 | err->Set(env->context(), env->permission_string(), perm_string) | 203 | 201 | .IsNothing() || | @@ -263,18 +261,13 @@ bool Permission::is_scope_granted(Environment* env, | 263 | 261 | v8::Local<v8::Context> context = env->context(); | 264 | 262 | v8::Local<v8::Object> msg = | 265 | 263 | v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0); | 266 | | - const char* perm_str = PermissionToString(permission); | 267 | 264 | msg->Set(context, | 268 | 265 | env->permission_string(), | 269 | | - v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked()) | | 266 | + PermissionToString(env, permission)) | 270 | 267 | .Check(); | 271 | 268 | msg->Set(context, | 272 | 269 | 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()) | 278 | 271 | .Check(); | 279 | 272 | ch->Publish(env, msg); | 280 | 273 | publishing_ = false; | @@ -333,18 +326,13 @@ void Permission::Drop(Environment* env, | 333 | 326 | v8::Local<v8::Context> context = env->context(); | 334 | 327 | v8::Local<v8::Object> msg = | 335 | 328 | v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0); | 336 | | - const char* perm_str = PermissionToString(scope); | 337 | 329 | msg->Set(context, | 338 | 330 | env->permission_string(), | 339 | | - v8::String::NewFromUtf8(isolate, perm_str).ToLocalChecked()) | | 331 | + PermissionToString(env, scope)) | 340 | 332 | .Check(); | 341 | 333 | msg->Set(context, | 342 | 334 | 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()) | 348 | 336 | .Check(); | 349 | 337 | msg->Set(context, | 350 | 338 | FIXED_ONE_BYTE_STRING(isolate, "drop"), | |
Collapse file Copy file name to clipboardExpand all lines: src/permission/permission.h +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -113,7 +113,8 @@ class Permission { | 113 | 113 | FORCE_INLINE bool warning_only() const { return warning_only_; } | 114 | 114 |
| 115 | 115 | static PermissionScope StringToPermission(const std::string& perm); | 116 | | - static const char* PermissionToString(PermissionScope perm); | | 116 | + static v8::Local<v8::String> PermissionToString(Environment* env, | | 117 | + PermissionScope perm); | 117 | 118 | static void ThrowAccessDenied(Environment* env, | 118 | 119 | PermissionScope perm, | 119 | 120 | const std::string_view& res); | |
|