Skip to content

Commit 54a8cd9

Browse files
committed
src: add contextify interceptor debug logs
1 parent 38647b3 commit 54a8cd9

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/debug_utils-inl.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@ struct ToStringHelper {
6262
static std::string Convert(const std::string& value) { return value; }
6363
static std::string_view Convert(std::string_view value) { return value; }
6464
static std::string Convert(bool value) { return value ? "true" : "false"; }
65+
66+
static std::string Convert(v8::Local<v8::Value> value) {
67+
v8::Isolate* isolate = v8::Isolate::GetCurrent();
68+
v8::TryCatch scope(isolate);
69+
if (value->IsSymbol()) {
70+
Utf8Value utf8_value(isolate,
71+
value.As<v8::Symbol>()->Description(isolate));
72+
return SPrintF("<Symbol: %s>", utf8_value.ToString());
73+
}
74+
if (value->IsString()) {
75+
Utf8Value utf8_value(isolate, value);
76+
return SPrintF("\"%s\"", utf8_value.ToString());
77+
}
78+
Utf8Value utf8_value(isolate, value);
79+
if (scope.HasCaught()) {
80+
return "<Unable to stringify v8::Value>";
81+
}
82+
return utf8_value.ToString();
83+
}
84+
6585
template <unsigned BASE_BITS,
6686
typename T,
6787
typename = std::enable_if_t<std::is_integral_v<T>>>

src/debug_utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ void NODE_EXTERN_PRIVATE FWrite(FILE* file, const std::string& str);
4646
NODE_ASYNC_PROVIDER_TYPES(V) \
4747
V(CRYPTO) \
4848
V(COMPILE_CACHE) \
49+
V(CONTEXTIFY) \
4950
V(DIAGNOSTICS) \
5051
V(HUGEPAGES) \
5152
V(INSPECTOR_SERVER) \

src/node_contextify.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "base_object-inl.h"
2525
#include "cppgc/allocation.h"
26+
#include "debug_utils-inl.h"
2627
#include "memory_tracker-inl.h"
2728
#include "module_wrap.h"
2829
#include "node_context_data.h"
@@ -484,6 +485,9 @@ Intercepted ContextifyContext::PropertyQueryCallback(
484485
return Intercepted::kNo;
485486
}
486487

488+
per_process::Debug(
489+
DebugCategory::CONTEXTIFY, "PropertyQuery(%s)\n", property);
490+
487491
Local<Context> context = ctx->context();
488492
Local<Object> sandbox = ctx->sandbox();
489493

@@ -530,6 +534,9 @@ Intercepted ContextifyContext::PropertyGetterCallback(
530534
return Intercepted::kNo;
531535
}
532536

537+
per_process::Debug(
538+
DebugCategory::CONTEXTIFY, "PropertyGetter(name: %s)\n", property);
539+
533540
Local<Context> context = ctx->context();
534541
Local<Object> sandbox = ctx->sandbox();
535542

@@ -567,6 +574,12 @@ Intercepted ContextifyContext::PropertySetterCallback(
567574
return Intercepted::kNo;
568575
}
569576

577+
per_process::Debug(DebugCategory::CONTEXTIFY,
578+
"PropertySetter(name: %s, value: %s), use-strict(%s)\n",
579+
property,
580+
value,
581+
args.ShouldThrowOnError());
582+
570583
Local<Context> context = ctx->context();
571584
PropertyAttribute attributes = PropertyAttribute::None;
572585
bool is_declared_on_global_proxy = ctx->global_proxy()
@@ -644,6 +657,9 @@ Intercepted ContextifyContext::PropertyDescriptorCallback(
644657
return Intercepted::kNo;
645658
}
646659

660+
per_process::Debug(
661+
DebugCategory::CONTEXTIFY, "PropertyDescriptor(name: %s)\n", property);
662+
647663
Local<Context> context = ctx->context();
648664

649665
Local<Object> sandbox = ctx->sandbox();
@@ -670,6 +686,9 @@ Intercepted ContextifyContext::PropertyDefinerCallback(
670686
return Intercepted::kNo;
671687
}
672688

689+
per_process::Debug(
690+
DebugCategory::CONTEXTIFY, "PropertyDefiner(name: %s)\n", property);
691+
673692
Local<Context> context = ctx->context();
674693
Isolate* isolate = Isolate::GetCurrent();
675694

@@ -740,6 +759,9 @@ Intercepted ContextifyContext::PropertyDeleterCallback(
740759
return Intercepted::kNo;
741760
}
742761

762+
per_process::Debug(
763+
DebugCategory::CONTEXTIFY, "PropertyDeleter(name: %s)\n", property);
764+
743765
Maybe<bool> success = ctx->sandbox()->Delete(ctx->context(), property);
744766

745767
if (success.FromMaybe(false)) {
@@ -767,6 +789,8 @@ void ContextifyContext::PropertyEnumeratorCallback(
767789
// Still initializing
768790
if (IsStillInitializing(ctx)) return;
769791

792+
per_process::Debug(DebugCategory::CONTEXTIFY, "PropertyEnumerator()\n");
793+
770794
Local<Array> properties;
771795
// Only get own named properties, exclude indices.
772796
if (!ctx->sandbox()
@@ -798,6 +822,9 @@ void ContextifyContext::IndexedPropertyEnumeratorCallback(
798822
// Still initializing
799823
if (IsStillInitializing(ctx)) return;
800824

825+
per_process::Debug(DebugCategory::CONTEXTIFY,
826+
"IndexedPropertyEnumerator()\n");
827+
801828
Local<Array> properties;
802829

803830
// Only get own index properties.

0 commit comments

Comments
 (0)