1+ #include " node_trace_events.h"
12#include " base_object-inl.h"
23#include " env-inl.h"
34#include " memory_tracker-inl.h"
@@ -20,11 +21,14 @@ using v8::Context;
2021using v8::Function;
2122using v8::FunctionCallbackInfo;
2223using v8::FunctionTemplate;
24+ using v8::HandleScope;
2325using v8::Isolate;
2426using v8::Local;
2527using v8::NewStringType;
2628using v8::Object;
29+ using v8::ObjectTemplate;
2730using v8::String;
31+ using v8::Uint8Array;
2832using v8::Value;
2933
3034class NodeCategorySet : public BaseObject {
@@ -165,9 +169,97 @@ void NodeCategorySet::RegisterExternalReferences(
165169 registry->Register (NodeCategorySet::Disable);
166170}
167171
172+ namespace trace_events {
173+
174+ void BindingData::MemoryInfo (MemoryTracker* tracker) const {}
175+
176+ BindingData::BindingData (Realm* realm, v8::Local<v8::Object> object)
177+ : SnapshotableObject(realm, object, type_int) {
178+ // Get the pointer of the memory for the flag that
179+ // store if trace is enabled for http the pointer will
180+ // always be the same and if the category does not exist
181+ // it creates:
182+ // https://github.com/nodejs/node/blob/6bbf2a57fcf33266c5859497f8cc32e1389a358a/deps/v8/src/libplatform/tracing/tracing-controller.cc#L322-L342
183+ auto p = const_cast <uint8_t *>(
184+ TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED (" node.http" ));
185+
186+ // NO_OP deleter
187+ // the idea of this chunk of code is to construct
188+ auto nop = [](void *, size_t , void *) {};
189+ auto bs = v8::ArrayBuffer::NewBackingStore (p, 1 , nop, nullptr );
190+ auto ab = v8::ArrayBuffer::New (realm->isolate (), std::move (bs));
191+ v8::Local<Uint8Array> u8 = v8::Uint8Array::New (ab, 0 , 1 );
192+
193+ object
194+ ->Set (realm->context (),
195+ FIXED_ONE_BYTE_STRING (realm->isolate (), " tracingCategories" ),
196+ u8 )
197+ .Check ();
198+ }
199+
200+ bool BindingData::PrepareForSerialization (v8::Local<v8::Context> context,
201+ v8::SnapshotCreator* creator) {
202+ return true ;
203+ }
204+
205+ InternalFieldInfoBase* BindingData::Serialize (int index) {
206+ DCHECK_EQ (index, BaseObject::kEmbedderType );
207+ InternalFieldInfo* info =
208+ InternalFieldInfoBase::New<InternalFieldInfo>(type ());
209+ return info;
210+ }
211+
212+ void BindingData::Deserialize (v8::Local<v8::Context> context,
213+ v8::Local<v8::Object> holder,
214+ int index,
215+ InternalFieldInfoBase* info) {
216+ DCHECK_EQ (index, BaseObject::kEmbedderType );
217+ v8::HandleScope scope (context->GetIsolate ());
218+ Realm* realm = Realm::GetCurrent (context);
219+ BindingData* binding = realm->AddBindingData <BindingData>(context, holder);
220+ CHECK_NOT_NULL (binding);
221+ }
222+
223+ void BindingData::CreatePerIsolateProperties (IsolateData* isolate_data,
224+ Local<ObjectTemplate> ctor) {}
225+
226+ void BindingData::CreatePerContextProperties (Local<Object> target,
227+ Local<Value> unused,
228+ Local<Context> context,
229+ void * priv) {
230+ Realm* realm = Realm::GetCurrent (context);
231+ realm->AddBindingData <BindingData>(context, target);
232+ }
233+
234+ void BindingData::RegisterExternalReferences (
235+ ExternalReferenceRegistry* registry) {}
236+
237+ static void CreatePerIsolateProperties (IsolateData* isolate_data,
238+ Local<ObjectTemplate> ctor) {
239+ BindingData::CreatePerIsolateProperties (isolate_data, ctor);
240+ }
241+
242+ static void CreatePerContextProperties (Local<Object> target,
243+ Local<Value> unused,
244+ Local<Context> context,
245+ void * priv) {
246+ BindingData::CreatePerContextProperties (target, unused, context, priv);
247+
248+ NodeCategorySet::Initialize (target, unused, context, priv);
249+ }
250+
251+ void RegisterExternalReferences (ExternalReferenceRegistry* registry) {
252+ NodeCategorySet::RegisterExternalReferences (registry);
253+ BindingData::RegisterExternalReferences (registry);
254+ }
255+
256+ } // namespace trace_events
257+
168258} // namespace node
169259
170- NODE_BINDING_CONTEXT_AWARE_INTERNAL (trace_events,
171- node::NodeCategorySet::Initialize)
172- NODE_BINDING_EXTERNAL_REFERENCE(
173- trace_events, node::NodeCategorySet::RegisterExternalReferences)
260+ NODE_BINDING_CONTEXT_AWARE_INTERNAL (
261+ trace_events, node::trace_events::CreatePerContextProperties)
262+ NODE_BINDING_PER_ISOLATE_INIT(trace_events,
263+ node::trace_events::CreatePerIsolateProperties)
264+ NODE_BINDING_EXTERNAL_REFERENCE(trace_events,
265+ node::trace_events::RegisterExternalReferences)
0 commit comments