1+ #include " hooks_wrapper.h"
2+ #include " hooks/includes/core.h"
3+ #include < memory>
4+
5+ namespace codspeed_native {
6+ namespace instruments {
7+ namespace hooks_wrapper {
8+
9+ // Global instance to maintain state across calls
10+ static std::unique_ptr<InstrumentHooks, decltype (&instrument_hooks_deinit)>
11+ g_hooks{nullptr , &instrument_hooks_deinit};
12+
13+ // Initialize instrument hooks if not already done
14+ static InstrumentHooks *ensureInitialized () {
15+ if (!g_hooks) {
16+ InstrumentHooks *hooks = instrument_hooks_init ();
17+ if (hooks) {
18+ g_hooks.reset (hooks);
19+ }
20+ }
21+ return g_hooks.get ();
22+ }
23+
24+ Napi::Boolean IsInstrumented (const Napi::CallbackInfo &info) {
25+ Napi::Env env = info.Env ();
26+ InstrumentHooks *hooks = ensureInitialized ();
27+
28+ if (!hooks) {
29+ return Napi::Boolean::New (env, false );
30+ }
31+
32+ bool instrumented = instrument_hooks_is_instrumented (hooks);
33+ return Napi::Boolean::New (env, instrumented);
34+ }
35+
36+ Napi::Number StartBenchmark (const Napi::CallbackInfo &info) {
37+ Napi::Env env = info.Env ();
38+ InstrumentHooks *hooks = ensureInitialized ();
39+
40+ if (!hooks) {
41+ return Napi::Number::New (env, 1 ); // Return error code
42+ }
43+
44+ uint8_t result = instrument_hooks_start_benchmark (hooks);
45+ return Napi::Number::New (env, result);
46+ }
47+
48+ Napi::Number StopBenchmark (const Napi::CallbackInfo &info) {
49+ Napi::Env env = info.Env ();
50+ InstrumentHooks *hooks = ensureInitialized ();
51+
52+ if (!hooks) {
53+ return Napi::Number::New (env, 1 ); // Return error code
54+ }
55+
56+ uint8_t result = instrument_hooks_stop_benchmark (hooks);
57+ return Napi::Number::New (env, result);
58+ }
59+
60+ Napi::Number SetExecutedBenchmark (const Napi::CallbackInfo &info) {
61+ Napi::Env env = info.Env ();
62+
63+ if (info.Length () != 2 ) {
64+ Napi::TypeError::New (env, " Expected 2 arguments: pid and uri" )
65+ .ThrowAsJavaScriptException ();
66+ return Napi::Number::New (env, 1 );
67+ }
68+
69+ if (!info[0 ].IsNumber () || !info[1 ].IsString ()) {
70+ Napi::TypeError::New (env, " Expected number (pid) and string (uri)" )
71+ .ThrowAsJavaScriptException ();
72+ return Napi::Number::New (env, 1 );
73+ }
74+
75+ InstrumentHooks *hooks = ensureInitialized ();
76+ if (!hooks) {
77+ return Napi::Number::New (env, 1 );
78+ }
79+
80+ uint32_t pid = info[0 ].As <Napi::Number>().Uint32Value ();
81+ std::string uri = info[1 ].As <Napi::String>().Utf8Value ();
82+
83+ uint8_t result =
84+ instrument_hooks_set_executed_benchmark (hooks, pid, uri.c_str ());
85+ return Napi::Number::New (env, result);
86+ }
87+
88+ Napi::Number SetIntegration (const Napi::CallbackInfo &info) {
89+ Napi::Env env = info.Env ();
90+
91+ if (info.Length () != 2 ) {
92+ Napi::TypeError::New (env, " Expected 2 arguments: name and version" )
93+ .ThrowAsJavaScriptException ();
94+ return Napi::Number::New (env, 1 );
95+ }
96+
97+ if (!info[0 ].IsString () || !info[1 ].IsString ()) {
98+ Napi::TypeError::New (env, " Expected string (name) and string (version)" )
99+ .ThrowAsJavaScriptException ();
100+ return Napi::Number::New (env, 1 );
101+ }
102+
103+ InstrumentHooks *hooks = ensureInitialized ();
104+ if (!hooks) {
105+ return Napi::Number::New (env, 1 );
106+ }
107+
108+ std::string name = info[0 ].As <Napi::String>().Utf8Value ();
109+ std::string version = info[1 ].As <Napi::String>().Utf8Value ();
110+
111+ uint8_t result =
112+ instrument_hooks_set_integration (hooks, name.c_str (), version.c_str ());
113+ return Napi::Number::New (env, result);
114+ }
115+
116+ Napi::Object Initialize (Napi::Env env, Napi::Object exports) {
117+ Napi::Object instrumentHooksObj = Napi::Object::New (env);
118+
119+ instrumentHooksObj.Set (Napi::String::New (env, " isInstrumented" ),
120+ Napi::Function::New (env, IsInstrumented));
121+ instrumentHooksObj.Set (Napi::String::New (env, " startBenchmark" ),
122+ Napi::Function::New (env, StartBenchmark));
123+ instrumentHooksObj.Set (Napi::String::New (env, " stopBenchmark" ),
124+ Napi::Function::New (env, StopBenchmark));
125+ instrumentHooksObj.Set (Napi::String::New (env, " setExecutedBenchmark" ),
126+ Napi::Function::New (env, SetExecutedBenchmark));
127+ instrumentHooksObj.Set (Napi::String::New (env, " setIntegration" ),
128+ Napi::Function::New (env, SetIntegration));
129+
130+ exports.Set (Napi::String::New (env, " InstrumentHooks" ), instrumentHooksObj);
131+
132+ return exports;
133+ }
134+
135+ } // namespace hooks_wrapper
136+ } // namespace instruments
137+ } // namespace codspeed_native
0 commit comments