Skip to content

Commit 9174ca2

Browse files
Matheus Marchinihyj1991
Matheus Marchini
authored and
hyj1991
committed
src: inspect context objects (Node.js v10.x+)
This patch teaches llnode how to inspect Context objects. This is useful to inspect context variables. PR-URL: nodejs#201 Fixes: nodejs#211 Reviewed-By: Joyee Cheung <[email protected]>
1 parent fa74421 commit 9174ca2

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

src/llv8-constants.cc

+4
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,9 @@ void Types::Load() {
487487
kFirstJSObjectType =
488488
LoadConstant("type_JSGlobalObject__JS_GLOBAL_OBJECT_TYPE");
489489

490+
kFirstContextType = LoadConstant("FirstContextType");
491+
kLastContextType = LoadConstant("LastContextType");
492+
490493
kHeapNumberType = LoadConstant("type_HeapNumber__HEAP_NUMBER_TYPE");
491494
kMapType = LoadConstant("type_Map__MAP_TYPE");
492495
kGlobalObjectType =
@@ -508,6 +511,7 @@ void Types::Load() {
508511
kSharedFunctionInfoType =
509512
LoadConstant("type_SharedFunctionInfo__SHARED_FUNCTION_INFO_TYPE");
510513
kScriptType = LoadConstant("type_Script__SCRIPT_TYPE");
514+
kScopeInfoType = LoadConstant("type_ScopeInfo__SCOPE_INFO_TYPE");
511515

512516
if (kJSAPIObjectType == -1) {
513517
common_->Load();

src/llv8-constants.h

+4
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,9 @@ class Types : public Module {
475475
int64_t kFirstNonstringType;
476476
int64_t kFirstJSObjectType;
477477

478+
int64_t kFirstContextType;
479+
int64_t kLastContextType;
480+
478481
int64_t kHeapNumberType;
479482
int64_t kMapType;
480483
int64_t kGlobalObjectType;
@@ -493,6 +496,7 @@ class Types : public Module {
493496
int64_t kJSDateType;
494497
int64_t kSharedFunctionInfoType;
495498
int64_t kScriptType;
499+
int64_t kScopeInfoType;
496500

497501
protected:
498502
void Load();

src/llv8.cc

+31-15
Original file line numberDiff line numberDiff line change
@@ -641,10 +641,15 @@ std::string JSFunction::Inspect(InspectOptions* options, Error& err) {
641641
snprintf(tmp, sizeof(tmp), "\n context=0x%016" PRIx64, context.raw());
642642
res += tmp;
643643

644-
std::string context_str = context.Inspect(err);
645-
if (err.Fail()) return std::string();
644+
{
645+
InspectOptions ctx_options;
646+
ctx_options.detailed = true;
647+
ctx_options.indent_depth = options->indent_depth + 1;
648+
std::string context_str = context.Inspect(&ctx_options, err);
649+
if (err.Fail()) return std::string();
646650

647-
if (!context_str.empty()) res += ":" + context_str;
651+
if (!context_str.empty()) res += ":" + context_str;
652+
}
648653

649654
if (options->print_source) {
650655
SharedFunctionInfo info = Info(err);
@@ -1215,6 +1220,12 @@ std::string HeapObject::Inspect(InspectOptions* options, Error& err) {
12151220
return pre + str.Inspect(options, err);
12161221
}
12171222

1223+
if (type >= v8()->types()->kFirstContextType &&
1224+
type <= v8()->types()->kLastContextType) {
1225+
Context ctx(this);
1226+
return pre + ctx.Inspect(options, err);
1227+
}
1228+
12181229
if (type == v8()->types()->kFixedArrayType) {
12191230
FixedArray arr(this);
12201231
return pre + arr.Inspect(options, err);
@@ -1765,13 +1776,19 @@ HeapObject Context::GetScopeInfo(Error& err) {
17651776
return info.GetScopeInfo(err);
17661777
}
17671778

1768-
std::string Context::Inspect(Error& err) {
1779+
std::string Context::Inspect(InspectOptions* options, Error& err) {
17691780
// Not enough postmortem information, return bare minimum
17701781
if (v8()->shared_info()->kScopeInfoOffset == -1 &&
17711782
v8()->shared_info()->kNameOrScopeInfoOffset == -1)
17721783
return std::string();
17731784

1774-
std::string res = "<Context: {\n";
1785+
std::string res = "<Context";
1786+
1787+
if (!options->detailed) {
1788+
return res + ">";
1789+
}
1790+
1791+
res += ": {\n";
17751792

17761793
Value previous = Previous(err);
17771794
if (err.Fail()) return std::string();
@@ -1788,12 +1805,10 @@ std::string Context::Inspect(Error& err) {
17881805
Smi local_count_smi = scope.ContextLocalCount(err);
17891806
if (err.Fail()) return std::string();
17901807

1791-
InspectOptions options;
1792-
17931808
HeapObject heap_previous = HeapObject(previous);
17941809
if (heap_previous.Check()) {
17951810
char tmp[128];
1796-
snprintf(tmp, sizeof(tmp), " (previous)=0x%016" PRIx64, previous.raw());
1811+
snprintf(tmp, sizeof(tmp), (options->get_indent_spaces() + "(previous)=0x%016" PRIx64).c_str(), previous.raw());
17971812
res += std::string(tmp) + ":<Context>,";
17981813
}
17991814

@@ -1803,16 +1818,16 @@ std::string Context::Inspect(Error& err) {
18031818
JSFunction closure = Closure(err);
18041819
if (err.Fail()) return std::string();
18051820
char tmp[128];
1806-
snprintf(tmp, sizeof(tmp), " (closure)=0x%016" PRIx64 " {",
1821+
snprintf(tmp, sizeof(tmp), (options->get_indent_spaces() + "(closure)=0x%016" PRIx64 " {").c_str(),
18071822
closure.raw());
18081823
res += tmp;
18091824

1810-
InspectOptions options;
1811-
res += closure.Inspect(&options, err) + "}";
1825+
InspectOptions closure_options;
1826+
res += closure.Inspect(&closure_options, err) + "}";
18121827
if (err.Fail()) return std::string();
18131828
} else {
18141829
char tmp[128];
1815-
snprintf(tmp, sizeof(tmp), " (scope_info)=0x%016" PRIx64,
1830+
snprintf(tmp, sizeof(tmp), (options->get_indent_spaces() + "(scope_info)=0x%016" PRIx64).c_str(),
18161831
scope.raw());
18171832

18181833
res += std::string(tmp) + ":<ScopeInfo";
@@ -1836,17 +1851,18 @@ std::string Context::Inspect(Error& err) {
18361851

18371852
if (!res.empty()) res += ",\n";
18381853

1839-
res += " " + name.ToString(err) + "=";
1854+
res += options->get_indent_spaces() + name.ToString(err) + "=";
18401855
if (err.Fail()) return std::string();
18411856

18421857
Value value = ContextSlot(i, err);
18431858
if (err.Fail()) return std::string();
18441859

1845-
res += value.Inspect(&options, err);
1860+
InspectOptions val_options;
1861+
res += value.Inspect(&val_options, err);
18461862
if (err.Fail()) return std::string();
18471863
}
18481864

1849-
return res + " }>";
1865+
return res + "}>";
18501866
}
18511867

18521868
context_t* Context::InspectX(Error& err) {

src/llv8.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,14 @@ class Value {
6969
: detailed(false),
7070
print_map(false),
7171
print_source(false),
72-
length(kLength) {}
72+
length(kLength),
73+
indent_depth(1) {}
7374

7475
static const unsigned int kLength = 16;
76+
static const unsigned int kIndentSize = 2;
77+
inline std::string get_indent_spaces() {
78+
return std::string(indent_depth * kIndentSize, ' ');
79+
}
7580

7681
bool detailed;
7782
bool print_map;
@@ -80,6 +85,7 @@ class Value {
8085
unsigned int current = 0;
8186
unsigned int limit = 0;
8287
int64_t start_address;
88+
unsigned int indent_depth;
8389
};
8490

8591
Value(const Value& v) = default;
@@ -445,8 +451,8 @@ class Context : public FixedArray {
445451
inline T GetEmbedderData(int64_t index, Error& err);
446452
inline Value ContextSlot(int index, Error& err);
447453

448-
std::string Inspect(Error& err);
449454
context_t* InspectX(Error& err);
455+
std::string Inspect(InspectOptions *options, Error& err);
450456

451457
private:
452458
inline JSFunction Closure(Error& err);

0 commit comments

Comments
 (0)