88#include " v8-fast-api-calls.h"
99#include " v8.h"
1010
11+ #include < string>
1112#include < string_view>
1213#include < unordered_map>
1314#endif // DEBUG
@@ -23,9 +24,20 @@ using v8::Number;
2324using v8::Object;
2425using v8::Value;
2526
27+ thread_local std::unordered_map<std::string, int > generic_usage_counters;
2628thread_local std::unordered_map<FastStringKey, int , FastStringKey::Hash>
2729 v8_fast_api_call_counts;
2830
31+ void CountGenericUsage (const char * counter_name) {
32+ if (generic_usage_counters.find (counter_name) == generic_usage_counters.end ())
33+ generic_usage_counters[counter_name] = 0 ;
34+ generic_usage_counters[counter_name]++;
35+ }
36+
37+ int GetGenericUsageCount (const char * counter_name) {
38+ return generic_usage_counters[counter_name];
39+ }
40+
2941void TrackV8FastApiCall (FastStringKey key) {
3042 v8_fast_api_call_counts[key]++;
3143}
@@ -34,6 +46,17 @@ int GetV8FastApiCallCount(FastStringKey key) {
3446 return v8_fast_api_call_counts[key];
3547}
3648
49+ void GetGenericUsageCount (const FunctionCallbackInfo<Value>& args) {
50+ Environment* env = Environment::GetCurrent (args);
51+ if (!args[0 ]->IsString ()) {
52+ env->ThrowError (" getGenericUsageCount must be called with a string" );
53+ return ;
54+ }
55+ Utf8Value utf8_key (env->isolate (), args[0 ]);
56+ args.GetReturnValue ().Set (
57+ GetGenericUsageCount (utf8_key.ToStringView ().data ()));
58+ }
59+
3760void GetV8FastApiCallCount (const FunctionCallbackInfo<Value>& args) {
3861 Environment* env = Environment::GetCurrent (args);
3962 if (!args[0 ]->IsString ()) {
@@ -89,6 +112,7 @@ void Initialize(Local<Object> target,
89112 Local<Context> context,
90113 void * priv) {
91114 SetMethod (context, target, " getV8FastApiCallCount" , GetV8FastApiCallCount);
115+ SetMethod (context, target, " getGenericUsageCount" , GetGenericUsageCount);
92116 SetFastMethod (context, target, " isEven" , SlowIsEven, &fast_is_even);
93117 SetFastMethod (context, target, " isOdd" , SlowIsOdd, &fast_is_odd);
94118}
0 commit comments